Jordy Rose | 93a9d82 | 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 | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 11 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringSwitch.h" |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang; |
| 17 | using namespace ento; |
| 18 | |
| 19 | namespace { |
| 20 | class ExprInspectionChecker : public Checker< eval::Call > { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 21 | mutable std::unique_ptr<BugType> BT; |
Jordan Rose | e5399f1 | 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 | d000b85 | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 25 | void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | ac7cc2d | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 26 | void analyzerCrash(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 27 | |
| 28 | typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *, |
| 29 | CheckerContext &C) const; |
| 30 | |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 31 | public: |
| 32 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | bool ExprInspectionChecker::evalCall(const CallExpr *CE, |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 37 | CheckerContext &C) const { |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 38 | // These checks should have no effect on the surrounding environment |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 39 | // (globals should not be invalidated, etc), hence the use of evalCall. |
| 40 | FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE)) |
| 41 | .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval) |
| 42 | .Case("clang_analyzer_checkInlined", |
| 43 | &ExprInspectionChecker::analyzerCheckInlined) |
Jordan Rose | ac7cc2d | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 44 | .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash) |
Jordan Rose | d000b85 | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 45 | .Case("clang_analyzer_warnIfReached", &ExprInspectionChecker::analyzerWarnIfReached) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 46 | .Default(nullptr); |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 47 | |
| 48 | if (!Handler) |
| 49 | return false; |
| 50 | |
| 51 | (this->*Handler)(CE, C); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | static const char *getArgumentValueString(const CallExpr *CE, |
| 56 | CheckerContext &C) { |
| 57 | if (CE->getNumArgs() == 0) |
| 58 | return "Missing assertion argument"; |
| 59 | |
| 60 | ExplodedNode *N = C.getPredecessor(); |
| 61 | const LocationContext *LC = N->getLocationContext(); |
| 62 | ProgramStateRef State = N->getState(); |
| 63 | |
| 64 | const Expr *Assertion = CE->getArg(0); |
| 65 | SVal AssertionVal = State->getSVal(Assertion, LC); |
| 66 | |
| 67 | if (AssertionVal.isUndef()) |
| 68 | return "UNDEFINED"; |
| 69 | |
| 70 | ProgramStateRef StTrue, StFalse; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 71 | std::tie(StTrue, StFalse) = |
David Blaikie | 5251abe | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 72 | State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>()); |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 73 | |
| 74 | if (StTrue) { |
| 75 | if (StFalse) |
| 76 | return "UNKNOWN"; |
| 77 | else |
| 78 | return "TRUE"; |
| 79 | } else { |
| 80 | if (StFalse) |
| 81 | return "FALSE"; |
| 82 | else |
| 83 | llvm_unreachable("Invalid constraint; neither true or false."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void ExprInspectionChecker::analyzerEval(const CallExpr *CE, |
| 88 | CheckerContext &C) const { |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 89 | ExplodedNode *N = C.getPredecessor(); |
| 90 | const LocationContext *LC = N->getLocationContext(); |
| 91 | |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 92 | // A specific instantiation of an inlined function may have more constrained |
| 93 | // values than can generally be assumed. Skip the check. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 94 | if (LC->getCurrentStackFrame()->getParent() != nullptr) |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 95 | return; |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 96 | |
| 97 | if (!BT) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 98 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 99 | |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 100 | BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 101 | C.emitReport(R); |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 102 | } |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 103 | |
Jordan Rose | d000b85 | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 104 | void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE, |
| 105 | CheckerContext &C) const { |
| 106 | ExplodedNode *N = C.getPredecessor(); |
| 107 | |
| 108 | if (!BT) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 109 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordan Rose | d000b85 | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 110 | |
| 111 | BugReport *R = new BugReport(*BT, "REACHABLE", N); |
| 112 | C.emitReport(R); |
| 113 | } |
| 114 | |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 115 | void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE, |
| 116 | CheckerContext &C) const { |
| 117 | ExplodedNode *N = C.getPredecessor(); |
| 118 | const LocationContext *LC = N->getLocationContext(); |
| 119 | |
| 120 | // An inlined function could conceivably also be analyzed as a top-level |
| 121 | // function. We ignore this case and only emit a message (TRUE or FALSE) |
| 122 | // when we are analyzing it as an inlined function. This means that |
| 123 | // clang_analyzer_checkInlined(true) should always print TRUE, but |
| 124 | // clang_analyzer_checkInlined(false) should never actually print anything. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 125 | if (LC->getCurrentStackFrame()->getParent() == nullptr) |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 126 | return; |
| 127 | |
| 128 | if (!BT) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 129 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
Jordan Rose | e5399f1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 130 | |
| 131 | BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N); |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 132 | C.emitReport(R); |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Jordan Rose | ac7cc2d | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 135 | void ExprInspectionChecker::analyzerCrash(const CallExpr *CE, |
| 136 | CheckerContext &C) const { |
| 137 | LLVM_BUILTIN_TRAP; |
| 138 | } |
| 139 | |
Jordy Rose | 93a9d82 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 140 | void ento::registerExprInspectionChecker(CheckerManager &Mgr) { |
| 141 | Mgr.registerChecker<ExprInspectionChecker>(); |
| 142 | } |
| 143 | |