Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 1 | //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangSACheckers.h" |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 11 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringSwitch.h" |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang; |
| 17 | using namespace ento; |
| 18 | |
| 19 | namespace { |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 20 | class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 21 | mutable std::unique_ptr<BugType> BT; |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 22 | |
| 23 | void analyzerEval(const CallExpr *CE, CheckerContext &C) const; |
| 24 | void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 25 | void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 26 | void analyzerCrash(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 27 | void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 28 | |
| 29 | typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *, |
| 30 | CheckerContext &C) const; |
| 31 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 32 | public: |
| 33 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 34 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 35 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 36 | } |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 37 | |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 38 | REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, const void *) |
| 39 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 40 | bool ExprInspectionChecker::evalCall(const CallExpr *CE, |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 41 | CheckerContext &C) const { |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 42 | // These checks should have no effect on the surrounding environment |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 43 | // (globals should not be invalidated, etc), hence the use of evalCall. |
| 44 | FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE)) |
| 45 | .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval) |
| 46 | .Case("clang_analyzer_checkInlined", |
| 47 | &ExprInspectionChecker::analyzerCheckInlined) |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 48 | .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash) |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 49 | .Case("clang_analyzer_warnIfReached", |
| 50 | &ExprInspectionChecker::analyzerWarnIfReached) |
| 51 | .Case("clang_analyzer_warnOnDeadSymbol", |
| 52 | &ExprInspectionChecker::analyzerWarnOnDeadSymbol) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 53 | .Default(nullptr); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 54 | |
| 55 | if (!Handler) |
| 56 | return false; |
| 57 | |
| 58 | (this->*Handler)(CE, C); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | static const char *getArgumentValueString(const CallExpr *CE, |
| 63 | CheckerContext &C) { |
| 64 | if (CE->getNumArgs() == 0) |
| 65 | return "Missing assertion argument"; |
| 66 | |
| 67 | ExplodedNode *N = C.getPredecessor(); |
| 68 | const LocationContext *LC = N->getLocationContext(); |
| 69 | ProgramStateRef State = N->getState(); |
| 70 | |
| 71 | const Expr *Assertion = CE->getArg(0); |
| 72 | SVal AssertionVal = State->getSVal(Assertion, LC); |
| 73 | |
| 74 | if (AssertionVal.isUndef()) |
| 75 | return "UNDEFINED"; |
| 76 | |
| 77 | ProgramStateRef StTrue, StFalse; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 78 | std::tie(StTrue, StFalse) = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 79 | State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>()); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 80 | |
| 81 | if (StTrue) { |
| 82 | if (StFalse) |
| 83 | return "UNKNOWN"; |
| 84 | else |
| 85 | return "TRUE"; |
| 86 | } else { |
| 87 | if (StFalse) |
| 88 | return "FALSE"; |
| 89 | else |
| 90 | llvm_unreachable("Invalid constraint; neither true or false."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void ExprInspectionChecker::analyzerEval(const CallExpr *CE, |
| 95 | CheckerContext &C) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 96 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 97 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 98 | // A specific instantiation of an inlined function may have more constrained |
| 99 | // values than can generally be assumed. Skip the check. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 100 | if (LC->getCurrentStackFrame()->getParent() != nullptr) |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 101 | return; |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 102 | |
| 103 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 104 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 105 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 106 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 107 | if (!N) |
| 108 | return; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 109 | C.emitReport( |
| 110 | llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N)); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 111 | } |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 112 | |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 113 | void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE, |
| 114 | CheckerContext &C) const { |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 115 | |
| 116 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 117 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 118 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 119 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 120 | if (!N) |
| 121 | return; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 122 | C.emitReport(llvm::make_unique<BugReport>(*BT, "REACHABLE", N)); |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 125 | void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE, |
| 126 | CheckerContext &C) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 127 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 128 | |
| 129 | // An inlined function could conceivably also be analyzed as a top-level |
| 130 | // function. We ignore this case and only emit a message (TRUE or FALSE) |
| 131 | // when we are analyzing it as an inlined function. This means that |
| 132 | // clang_analyzer_checkInlined(true) should always print TRUE, but |
| 133 | // clang_analyzer_checkInlined(false) should never actually print anything. |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 134 | if (LC->getCurrentStackFrame()->getParent() == nullptr) |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 138 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 139 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 140 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 141 | if (!N) |
| 142 | return; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 143 | C.emitReport( |
| 144 | llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N)); |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame^] | 147 | void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE, |
| 148 | CheckerContext &C) const { |
| 149 | if (CE->getNumArgs() == 0) |
| 150 | return; |
| 151 | SVal Val = C.getSVal(CE->getArg(0)); |
| 152 | SymbolRef Sym = Val.getAsSymbol(); |
| 153 | if (!Sym) |
| 154 | return; |
| 155 | |
| 156 | ProgramStateRef State = C.getState(); |
| 157 | State = State->add<MarkedSymbols>(Sym); |
| 158 | C.addTransition(State); |
| 159 | } |
| 160 | |
| 161 | void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 162 | CheckerContext &C) const { |
| 163 | ProgramStateRef State = C.getState(); |
| 164 | const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>(); |
| 165 | for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) { |
| 166 | SymbolRef Sym = static_cast<SymbolRef>(*I); |
| 167 | if (!SymReaper.isDead(Sym)) |
| 168 | continue; |
| 169 | |
| 170 | if (!BT) |
| 171 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
| 172 | |
| 173 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 174 | if (!N) |
| 175 | return; |
| 176 | |
| 177 | C.emitReport(llvm::make_unique<BugReport>(*BT, "SYMBOL DEAD", N)); |
| 178 | C.addTransition(State->remove<MarkedSymbols>(Sym), N); |
| 179 | } |
| 180 | } |
| 181 | |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 182 | void ExprInspectionChecker::analyzerCrash(const CallExpr *CE, |
| 183 | CheckerContext &C) const { |
| 184 | LLVM_BUILTIN_TRAP; |
| 185 | } |
| 186 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 187 | void ento::registerExprInspectionChecker(CheckerManager &Mgr) { |
| 188 | Mgr.registerChecker<ExprInspectionChecker>(); |
| 189 | } |
| 190 | |