Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 1 | //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 9 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 10 | #include "clang/StaticAnalyzer/Checkers/SValExplainer.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" |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Core/IssueHash.h" |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
Pavel Labath | d570a61 | 2017-01-16 15:57:07 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ScopedPrinter.h" |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | using namespace ento; |
| 20 | |
| 21 | namespace { |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 22 | class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols, |
| 23 | check::EndAnalysis> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 24 | mutable std::unique_ptr<BugType> BT; |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 25 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 26 | // These stats are per-analysis, not per-branch, hence they shouldn't |
| 27 | // stay inside the program state. |
| 28 | struct ReachedStat { |
| 29 | ExplodedNode *ExampleNode; |
| 30 | unsigned NumTimesReached; |
| 31 | }; |
| 32 | mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats; |
| 33 | |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 34 | void analyzerEval(const CallExpr *CE, CheckerContext &C) const; |
| 35 | void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 36 | void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 37 | void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 38 | void analyzerCrash(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 39 | void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 40 | void analyzerDump(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 41 | void analyzerExplain(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 42 | void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 43 | void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const; |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 44 | void analyzerHashDump(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 45 | void analyzerDenote(const CallExpr *CE, CheckerContext &C) const; |
| 46 | void analyzerExpress(const CallExpr *CE, CheckerContext &C) const; |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 47 | |
| 48 | typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *, |
| 49 | CheckerContext &C) const; |
| 50 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 51 | ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C) const; |
| 52 | ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR, |
| 53 | ExplodedNode *N) const; |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 54 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 55 | public: |
| 56 | bool evalCall(const CallExpr *CE, CheckerContext &C) const; |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 57 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 58 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR, |
| 59 | ExprEngine &Eng) const; |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 61 | } |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 62 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 63 | REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef) |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 64 | REGISTER_MAP_WITH_PROGRAMSTATE(DenotedSymbols, SymbolRef, const StringLiteral *) |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 65 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 66 | bool ExprInspectionChecker::evalCall(const CallExpr *CE, |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 67 | CheckerContext &C) const { |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 68 | // These checks should have no effect on the surrounding environment |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 69 | // (globals should not be invalidated, etc), hence the use of evalCall. |
| 70 | FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE)) |
| 71 | .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval) |
| 72 | .Case("clang_analyzer_checkInlined", |
| 73 | &ExprInspectionChecker::analyzerCheckInlined) |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 74 | .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash) |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 75 | .Case("clang_analyzer_warnIfReached", |
| 76 | &ExprInspectionChecker::analyzerWarnIfReached) |
| 77 | .Case("clang_analyzer_warnOnDeadSymbol", |
| 78 | &ExprInspectionChecker::analyzerWarnOnDeadSymbol) |
Anna Zaks | 37faed9 | 2017-03-09 00:01:10 +0000 | [diff] [blame] | 79 | .StartsWith("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain) |
| 80 | .StartsWith("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump) |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 81 | .Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent) |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 82 | .Case("clang_analyzer_printState", |
| 83 | &ExprInspectionChecker::analyzerPrintState) |
| 84 | .Case("clang_analyzer_numTimesReached", |
| 85 | &ExprInspectionChecker::analyzerNumTimesReached) |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 86 | .Case("clang_analyzer_hashDump", &ExprInspectionChecker::analyzerHashDump) |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 87 | .Case("clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote) |
| 88 | .Case("clang_analyzer_express", &ExprInspectionChecker::analyzerExpress) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 89 | .Default(nullptr); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 90 | |
| 91 | if (!Handler) |
| 92 | return false; |
| 93 | |
| 94 | (this->*Handler)(CE, C); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | static const char *getArgumentValueString(const CallExpr *CE, |
| 99 | CheckerContext &C) { |
| 100 | if (CE->getNumArgs() == 0) |
| 101 | return "Missing assertion argument"; |
| 102 | |
| 103 | ExplodedNode *N = C.getPredecessor(); |
| 104 | const LocationContext *LC = N->getLocationContext(); |
| 105 | ProgramStateRef State = N->getState(); |
| 106 | |
| 107 | const Expr *Assertion = CE->getArg(0); |
| 108 | SVal AssertionVal = State->getSVal(Assertion, LC); |
| 109 | |
| 110 | if (AssertionVal.isUndef()) |
| 111 | return "UNDEFINED"; |
| 112 | |
| 113 | ProgramStateRef StTrue, StFalse; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 114 | std::tie(StTrue, StFalse) = |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 115 | State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>()); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 116 | |
| 117 | if (StTrue) { |
| 118 | if (StFalse) |
| 119 | return "UNKNOWN"; |
| 120 | else |
| 121 | return "TRUE"; |
| 122 | } else { |
| 123 | if (StFalse) |
| 124 | return "FALSE"; |
| 125 | else |
| 126 | llvm_unreachable("Invalid constraint; neither true or false."); |
| 127 | } |
| 128 | } |
| 129 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 130 | ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg, |
| 131 | CheckerContext &C) const { |
| 132 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
| 133 | reportBug(Msg, C.getBugReporter(), N); |
| 134 | return N; |
| 135 | } |
| 136 | |
| 137 | ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg, |
| 138 | BugReporter &BR, |
| 139 | ExplodedNode *N) const { |
| 140 | if (!N) |
| 141 | return nullptr; |
| 142 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 143 | if (!BT) |
| 144 | BT.reset(new BugType(this, "Checking analyzer assumptions", "debug")); |
| 145 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 146 | BR.emitReport(llvm::make_unique<BugReport>(*BT, Msg, N)); |
| 147 | return N; |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 150 | void ExprInspectionChecker::analyzerEval(const CallExpr *CE, |
| 151 | CheckerContext &C) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 152 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 153 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 154 | // A specific instantiation of an inlined function may have more constrained |
| 155 | // values than can generally be assumed. Skip the check. |
George Karpenkov | dd18b11 | 2018-06-27 01:51:55 +0000 | [diff] [blame] | 156 | if (LC->getStackFrame()->getParent() != nullptr) |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 157 | return; |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 158 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 159 | reportBug(getArgumentValueString(CE, C), C); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 160 | } |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 161 | |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 162 | void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE, |
| 163 | CheckerContext &C) const { |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 164 | reportBug("REACHABLE", C); |
Jordan Rose | 9db2d9a | 2013-10-03 16:57:03 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 167 | void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE, |
| 168 | CheckerContext &C) const { |
| 169 | ++ReachedStats[CE].NumTimesReached; |
| 170 | if (!ReachedStats[CE].ExampleNode) { |
| 171 | // Later, in checkEndAnalysis, we'd throw a report against it. |
| 172 | ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode(); |
| 173 | } |
| 174 | } |
| 175 | |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 176 | void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE, |
| 177 | CheckerContext &C) const { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 178 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 179 | |
| 180 | // An inlined function could conceivably also be analyzed as a top-level |
| 181 | // function. We ignore this case and only emit a message (TRUE or FALSE) |
| 182 | // when we are analyzing it as an inlined function. This means that |
| 183 | // clang_analyzer_checkInlined(true) should always print TRUE, but |
| 184 | // clang_analyzer_checkInlined(false) should never actually print anything. |
George Karpenkov | dd18b11 | 2018-06-27 01:51:55 +0000 | [diff] [blame] | 185 | if (LC->getStackFrame()->getParent() == nullptr) |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 186 | return; |
| 187 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 188 | reportBug(getArgumentValueString(CE, C), C); |
| 189 | } |
Jordan Rose | 13937b1 | 2012-08-10 22:26:29 +0000 | [diff] [blame] | 190 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 191 | void ExprInspectionChecker::analyzerExplain(const CallExpr *CE, |
| 192 | CheckerContext &C) const { |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 193 | if (CE->getNumArgs() == 0) { |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 194 | reportBug("Missing argument for explaining", C); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 197 | |
| 198 | SVal V = C.getSVal(CE->getArg(0)); |
| 199 | SValExplainer Ex(C.getASTContext()); |
| 200 | reportBug(Ex.Visit(V), C); |
| 201 | } |
| 202 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 203 | void ExprInspectionChecker::analyzerDump(const CallExpr *CE, |
| 204 | CheckerContext &C) const { |
| 205 | if (CE->getNumArgs() == 0) { |
| 206 | reportBug("Missing argument for dumping", C); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | SVal V = C.getSVal(CE->getArg(0)); |
| 211 | |
| 212 | llvm::SmallString<32> Str; |
| 213 | llvm::raw_svector_ostream OS(Str); |
| 214 | V.dumpToStream(OS); |
| 215 | reportBug(OS.str(), C); |
| 216 | } |
| 217 | |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 218 | void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE, |
| 219 | CheckerContext &C) const { |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 220 | if (CE->getNumArgs() == 0) { |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 221 | reportBug("Missing region for obtaining extent", C); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 222 | return; |
| 223 | } |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 224 | |
| 225 | auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion()); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 226 | if (!MR) { |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 227 | reportBug("Obtaining extent of a non-region", C); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 228 | return; |
| 229 | } |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 230 | |
| 231 | ProgramStateRef State = C.getState(); |
| 232 | State = State->BindExpr(CE, C.getLocationContext(), |
| 233 | MR->getExtent(C.getSValBuilder())); |
| 234 | C.addTransition(State); |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 237 | void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE, |
| 238 | CheckerContext &C) const { |
| 239 | C.getState()->dump(); |
| 240 | } |
| 241 | |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 242 | void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE, |
| 243 | CheckerContext &C) const { |
| 244 | if (CE->getNumArgs() == 0) |
| 245 | return; |
| 246 | SVal Val = C.getSVal(CE->getArg(0)); |
| 247 | SymbolRef Sym = Val.getAsSymbol(); |
| 248 | if (!Sym) |
| 249 | return; |
| 250 | |
| 251 | ProgramStateRef State = C.getState(); |
| 252 | State = State->add<MarkedSymbols>(Sym); |
| 253 | C.addTransition(State); |
| 254 | } |
| 255 | |
| 256 | void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 257 | CheckerContext &C) const { |
| 258 | ProgramStateRef State = C.getState(); |
| 259 | const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>(); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 260 | ExplodedNode *N = C.getPredecessor(); |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 261 | for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) { |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 262 | SymbolRef Sym = *I; |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 263 | if (!SymReaper.isDead(Sym)) |
| 264 | continue; |
| 265 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 266 | // The non-fatal error node should be the same for all reports. |
| 267 | if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C)) |
| 268 | N = BugNode; |
Artem Dergachev | 895242f | 2016-01-15 15:22:05 +0000 | [diff] [blame] | 269 | State = State->remove<MarkedSymbols>(Sym); |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 270 | } |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 271 | |
| 272 | for (auto I : State->get<DenotedSymbols>()) { |
| 273 | SymbolRef Sym = I.first; |
| 274 | if (!SymReaper.isLive(Sym)) |
| 275 | State = State->remove<DenotedSymbols>(Sym); |
| 276 | } |
| 277 | |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 278 | C.addTransition(State, N); |
| 279 | } |
| 280 | |
| 281 | void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR, |
| 282 | ExprEngine &Eng) const { |
| 283 | for (auto Item: ReachedStats) { |
| 284 | unsigned NumTimesReached = Item.second.NumTimesReached; |
| 285 | ExplodedNode *N = Item.second.ExampleNode; |
| 286 | |
Pavel Labath | d570a61 | 2017-01-16 15:57:07 +0000 | [diff] [blame] | 287 | reportBug(llvm::to_string(NumTimesReached), BR, N); |
Artem Dergachev | 30ed546 | 2016-11-30 17:57:18 +0000 | [diff] [blame] | 288 | } |
Peter Szecsi | 657ac14 | 2017-07-25 19:23:23 +0000 | [diff] [blame] | 289 | ReachedStats.clear(); |
Artem Dergachev | 733e71b | 2015-12-10 09:28:06 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 292 | void ExprInspectionChecker::analyzerCrash(const CallExpr *CE, |
| 293 | CheckerContext &C) const { |
| 294 | LLVM_BUILTIN_TRAP; |
| 295 | } |
| 296 | |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 297 | void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE, |
| 298 | CheckerContext &C) const { |
| 299 | const LangOptions &Opts = C.getLangOpts(); |
| 300 | const SourceManager &SM = C.getSourceManager(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 301 | FullSourceLoc FL(CE->getArg(0)->getBeginLoc(), SM); |
Gabor Horvath | 3bd24f9 | 2017-10-30 12:02:23 +0000 | [diff] [blame] | 302 | std::string HashContent = |
| 303 | GetIssueString(SM, FL, getCheckName().getName(), "Category", |
| 304 | C.getLocationContext()->getDecl(), Opts); |
| 305 | |
| 306 | reportBug(HashContent, C); |
| 307 | } |
| 308 | |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 309 | void ExprInspectionChecker::analyzerDenote(const CallExpr *CE, |
| 310 | CheckerContext &C) const { |
| 311 | if (CE->getNumArgs() < 2) { |
| 312 | reportBug("clang_analyzer_denote() requires a symbol and a string literal", |
| 313 | C); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol(); |
| 318 | if (!Sym) { |
| 319 | reportBug("Not a symbol", C); |
| 320 | return; |
| 321 | } |
| 322 | |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 323 | const auto *E = dyn_cast<StringLiteral>(CE->getArg(1)->IgnoreParenCasts()); |
| 324 | if (!E) { |
| 325 | reportBug("Not a string literal", C); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | ProgramStateRef State = C.getState(); |
| 330 | |
| 331 | C.addTransition(C.getState()->set<DenotedSymbols>(Sym, E)); |
| 332 | } |
| 333 | |
Benjamin Kramer | c55e997 | 2018-10-13 22:18:22 +0000 | [diff] [blame] | 334 | namespace { |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 335 | class SymbolExpressor |
| 336 | : public SymExprVisitor<SymbolExpressor, Optional<std::string>> { |
| 337 | ProgramStateRef State; |
| 338 | |
| 339 | public: |
| 340 | SymbolExpressor(ProgramStateRef State) : State(State) {} |
| 341 | |
Artem Dergachev | 02955af | 2018-12-22 02:06:51 +0000 | [diff] [blame] | 342 | Optional<std::string> lookup(const SymExpr *S) { |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 343 | if (const StringLiteral *const *SLPtr = State->get<DenotedSymbols>(S)) { |
| 344 | const StringLiteral *SL = *SLPtr; |
| 345 | return std::string(SL->getBytes()); |
| 346 | } |
| 347 | return None; |
| 348 | } |
| 349 | |
Artem Dergachev | 02955af | 2018-12-22 02:06:51 +0000 | [diff] [blame] | 350 | Optional<std::string> VisitSymExpr(const SymExpr *S) { |
| 351 | return lookup(S); |
| 352 | } |
| 353 | |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 354 | Optional<std::string> VisitSymIntExpr(const SymIntExpr *S) { |
Artem Dergachev | 02955af | 2018-12-22 02:06:51 +0000 | [diff] [blame] | 355 | if (Optional<std::string> Str = lookup(S)) |
| 356 | return Str; |
| 357 | if (Optional<std::string> Str = Visit(S->getLHS())) |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 358 | return (*Str + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + " " + |
| 359 | std::to_string(S->getRHS().getLimitedValue()) + |
| 360 | (S->getRHS().isUnsigned() ? "U" : "")) |
| 361 | .str(); |
| 362 | return None; |
| 363 | } |
| 364 | |
| 365 | Optional<std::string> VisitSymSymExpr(const SymSymExpr *S) { |
Artem Dergachev | 02955af | 2018-12-22 02:06:51 +0000 | [diff] [blame] | 366 | if (Optional<std::string> Str = lookup(S)) |
| 367 | return Str; |
| 368 | if (Optional<std::string> Str1 = Visit(S->getLHS())) |
| 369 | if (Optional<std::string> Str2 = Visit(S->getRHS())) |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 370 | return (*Str1 + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + |
| 371 | " " + *Str2).str(); |
| 372 | return None; |
| 373 | } |
Artem Dergachev | 02955af | 2018-12-22 02:06:51 +0000 | [diff] [blame] | 374 | |
| 375 | Optional<std::string> VisitSymbolCast(const SymbolCast *S) { |
| 376 | if (Optional<std::string> Str = lookup(S)) |
| 377 | return Str; |
| 378 | if (Optional<std::string> Str = Visit(S->getOperand())) |
| 379 | return (Twine("(") + S->getType().getAsString() + ")" + *Str).str(); |
| 380 | return None; |
| 381 | } |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 382 | }; |
Benjamin Kramer | c55e997 | 2018-10-13 22:18:22 +0000 | [diff] [blame] | 383 | } // namespace |
Artem Dergachev | e527df0 | 2018-09-25 23:50:53 +0000 | [diff] [blame] | 384 | |
| 385 | void ExprInspectionChecker::analyzerExpress(const CallExpr *CE, |
| 386 | CheckerContext &C) const { |
| 387 | if (CE->getNumArgs() == 0) { |
| 388 | reportBug("clang_analyzer_express() requires a symbol", C); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol(); |
| 393 | if (!Sym) { |
| 394 | reportBug("Not a symbol", C); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | SymbolExpressor V(C.getState()); |
| 399 | auto Str = V.Visit(Sym); |
| 400 | if (!Str) { |
| 401 | reportBug("Unable to express", C); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | reportBug(*Str, C); |
| 406 | } |
| 407 | |
Jordy Rose | 31ae259 | 2012-05-16 16:01:07 +0000 | [diff] [blame] | 408 | void ento::registerExprInspectionChecker(CheckerManager &Mgr) { |
| 409 | Mgr.registerChecker<ExprInspectionChecker>(); |
| 410 | } |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 411 | |
| 412 | bool ento::shouldRegisterExprInspectionChecker(const LangOptions &LO) { |
| 413 | return true; |
| 414 | } |