1f4a2713aSLionel Sambuc //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
11f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
12f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
13f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
14f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc using namespace clang;
17f4a2713aSLionel Sambuc using namespace ento;
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc namespace {
20f4a2713aSLionel Sambuc class ExprInspectionChecker : public Checker< eval::Call > {
21*0a6a1f1dSLionel Sambuc mutable std::unique_ptr<BugType> BT;
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
24f4a2713aSLionel Sambuc void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
25f4a2713aSLionel Sambuc void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
26f4a2713aSLionel Sambuc void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
29f4a2713aSLionel Sambuc CheckerContext &C) const;
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc public:
32f4a2713aSLionel Sambuc bool evalCall(const CallExpr *CE, CheckerContext &C) const;
33f4a2713aSLionel Sambuc };
34f4a2713aSLionel Sambuc }
35f4a2713aSLionel Sambuc
evalCall(const CallExpr * CE,CheckerContext & C) const36f4a2713aSLionel Sambuc bool ExprInspectionChecker::evalCall(const CallExpr *CE,
37f4a2713aSLionel Sambuc CheckerContext &C) const {
38f4a2713aSLionel Sambuc // These checks should have no effect on the surrounding environment
39f4a2713aSLionel Sambuc // (globals should not be invalidated, etc), hence the use of evalCall.
40f4a2713aSLionel Sambuc FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
41f4a2713aSLionel Sambuc .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
42f4a2713aSLionel Sambuc .Case("clang_analyzer_checkInlined",
43f4a2713aSLionel Sambuc &ExprInspectionChecker::analyzerCheckInlined)
44f4a2713aSLionel Sambuc .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
45f4a2713aSLionel Sambuc .Case("clang_analyzer_warnIfReached", &ExprInspectionChecker::analyzerWarnIfReached)
46*0a6a1f1dSLionel Sambuc .Default(nullptr);
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc if (!Handler)
49f4a2713aSLionel Sambuc return false;
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc (this->*Handler)(CE, C);
52f4a2713aSLionel Sambuc return true;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
getArgumentValueString(const CallExpr * CE,CheckerContext & C)55f4a2713aSLionel Sambuc static const char *getArgumentValueString(const CallExpr *CE,
56f4a2713aSLionel Sambuc CheckerContext &C) {
57f4a2713aSLionel Sambuc if (CE->getNumArgs() == 0)
58f4a2713aSLionel Sambuc return "Missing assertion argument";
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc ExplodedNode *N = C.getPredecessor();
61f4a2713aSLionel Sambuc const LocationContext *LC = N->getLocationContext();
62f4a2713aSLionel Sambuc ProgramStateRef State = N->getState();
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc const Expr *Assertion = CE->getArg(0);
65f4a2713aSLionel Sambuc SVal AssertionVal = State->getSVal(Assertion, LC);
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc if (AssertionVal.isUndef())
68f4a2713aSLionel Sambuc return "UNDEFINED";
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc ProgramStateRef StTrue, StFalse;
71*0a6a1f1dSLionel Sambuc std::tie(StTrue, StFalse) =
72f4a2713aSLionel Sambuc State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc if (StTrue) {
75f4a2713aSLionel Sambuc if (StFalse)
76f4a2713aSLionel Sambuc return "UNKNOWN";
77f4a2713aSLionel Sambuc else
78f4a2713aSLionel Sambuc return "TRUE";
79f4a2713aSLionel Sambuc } else {
80f4a2713aSLionel Sambuc if (StFalse)
81f4a2713aSLionel Sambuc return "FALSE";
82f4a2713aSLionel Sambuc else
83f4a2713aSLionel Sambuc llvm_unreachable("Invalid constraint; neither true or false.");
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc
analyzerEval(const CallExpr * CE,CheckerContext & C) const87f4a2713aSLionel Sambuc void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
88f4a2713aSLionel Sambuc CheckerContext &C) const {
89f4a2713aSLionel Sambuc ExplodedNode *N = C.getPredecessor();
90f4a2713aSLionel Sambuc const LocationContext *LC = N->getLocationContext();
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc // A specific instantiation of an inlined function may have more constrained
93f4a2713aSLionel Sambuc // values than can generally be assumed. Skip the check.
94*0a6a1f1dSLionel Sambuc if (LC->getCurrentStackFrame()->getParent() != nullptr)
95f4a2713aSLionel Sambuc return;
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc if (!BT)
98*0a6a1f1dSLionel Sambuc BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N);
101f4a2713aSLionel Sambuc C.emitReport(R);
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
analyzerWarnIfReached(const CallExpr * CE,CheckerContext & C) const104f4a2713aSLionel Sambuc void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
105f4a2713aSLionel Sambuc CheckerContext &C) const {
106f4a2713aSLionel Sambuc ExplodedNode *N = C.getPredecessor();
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc if (!BT)
109*0a6a1f1dSLionel Sambuc BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc BugReport *R = new BugReport(*BT, "REACHABLE", N);
112f4a2713aSLionel Sambuc C.emitReport(R);
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc
analyzerCheckInlined(const CallExpr * CE,CheckerContext & C) const115f4a2713aSLionel Sambuc void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
116f4a2713aSLionel Sambuc CheckerContext &C) const {
117f4a2713aSLionel Sambuc ExplodedNode *N = C.getPredecessor();
118f4a2713aSLionel Sambuc const LocationContext *LC = N->getLocationContext();
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // An inlined function could conceivably also be analyzed as a top-level
121f4a2713aSLionel Sambuc // function. We ignore this case and only emit a message (TRUE or FALSE)
122f4a2713aSLionel Sambuc // when we are analyzing it as an inlined function. This means that
123f4a2713aSLionel Sambuc // clang_analyzer_checkInlined(true) should always print TRUE, but
124f4a2713aSLionel Sambuc // clang_analyzer_checkInlined(false) should never actually print anything.
125*0a6a1f1dSLionel Sambuc if (LC->getCurrentStackFrame()->getParent() == nullptr)
126f4a2713aSLionel Sambuc return;
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc if (!BT)
129*0a6a1f1dSLionel Sambuc BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N);
132f4a2713aSLionel Sambuc C.emitReport(R);
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
analyzerCrash(const CallExpr * CE,CheckerContext & C) const135f4a2713aSLionel Sambuc void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
136f4a2713aSLionel Sambuc CheckerContext &C) const {
137f4a2713aSLionel Sambuc LLVM_BUILTIN_TRAP;
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc
registerExprInspectionChecker(CheckerManager & Mgr)140f4a2713aSLionel Sambuc void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
141f4a2713aSLionel Sambuc Mgr.registerChecker<ExprInspectionChecker>();
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc
144