blob: 0005ec470d203b38371c38522db6af29620ea057 [file] [log] [blame]
Jordy Rose31ae2592012-05-16 16:01:07 +00001//==- 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"
Gabor Horvath3bd24f92017-10-30 12:02:23 +000011#include "clang/StaticAnalyzer/Checkers/SValExplainer.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000012#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/StaticAnalyzer/Core/Checker.h"
Gabor Horvath3bd24f92017-10-30 12:02:23 +000014#include "clang/StaticAnalyzer/Core/IssueHash.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000015#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000016#include "llvm/ADT/StringSwitch.h"
Pavel Labathd570a612017-01-16 15:57:07 +000017#include "llvm/Support/ScopedPrinter.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000018
19using namespace clang;
20using namespace ento;
21
22namespace {
Artem Dergachev30ed5462016-11-30 17:57:18 +000023class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols,
24 check::EndAnalysis> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000025 mutable std::unique_ptr<BugType> BT;
Jordan Rose13937b12012-08-10 22:26:29 +000026
Artem Dergachev30ed5462016-11-30 17:57:18 +000027 // These stats are per-analysis, not per-branch, hence they shouldn't
28 // stay inside the program state.
29 struct ReachedStat {
30 ExplodedNode *ExampleNode;
31 unsigned NumTimesReached;
32 };
33 mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats;
34
Jordan Rose13937b12012-08-10 22:26:29 +000035 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
36 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose9db2d9a2013-10-03 16:57:03 +000037 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev30ed5462016-11-30 17:57:18 +000038 void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosee9c57222013-07-19 00:59:08 +000039 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000040 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev30ed5462016-11-30 17:57:18 +000041 void analyzerDump(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev895242f2016-01-15 15:22:05 +000042 void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev30ed5462016-11-30 17:57:18 +000043 void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev895242f2016-01-15 15:22:05 +000044 void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
Gabor Horvath3bd24f92017-10-30 12:02:23 +000045 void analyzerHashDump(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose13937b12012-08-10 22:26:29 +000046
47 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
48 CheckerContext &C) const;
49
Artem Dergachev30ed5462016-11-30 17:57:18 +000050 ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C) const;
51 ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR,
52 ExplodedNode *N) const;
Artem Dergachev895242f2016-01-15 15:22:05 +000053
Jordy Rose31ae2592012-05-16 16:01:07 +000054public:
55 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000056 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Artem Dergachev30ed5462016-11-30 17:57:18 +000057 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
58 ExprEngine &Eng) const;
Jordy Rose31ae2592012-05-16 16:01:07 +000059};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000060}
Jordy Rose31ae2592012-05-16 16:01:07 +000061
Artem Dergachev895242f2016-01-15 15:22:05 +000062REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
Artem Dergachev733e71b2015-12-10 09:28:06 +000063
Jordy Rose31ae2592012-05-16 16:01:07 +000064bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rose13937b12012-08-10 22:26:29 +000065 CheckerContext &C) const {
Jordy Rose31ae2592012-05-16 16:01:07 +000066 // These checks should have no effect on the surrounding environment
Jordan Rose13937b12012-08-10 22:26:29 +000067 // (globals should not be invalidated, etc), hence the use of evalCall.
68 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
69 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
70 .Case("clang_analyzer_checkInlined",
71 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Rosee9c57222013-07-19 00:59:08 +000072 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Artem Dergachev733e71b2015-12-10 09:28:06 +000073 .Case("clang_analyzer_warnIfReached",
74 &ExprInspectionChecker::analyzerWarnIfReached)
75 .Case("clang_analyzer_warnOnDeadSymbol",
76 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
Anna Zaks37faed92017-03-09 00:01:10 +000077 .StartsWith("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
78 .StartsWith("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump)
Artem Dergachev895242f2016-01-15 15:22:05 +000079 .Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent)
Artem Dergachev30ed5462016-11-30 17:57:18 +000080 .Case("clang_analyzer_printState",
81 &ExprInspectionChecker::analyzerPrintState)
82 .Case("clang_analyzer_numTimesReached",
83 &ExprInspectionChecker::analyzerNumTimesReached)
Gabor Horvath3bd24f92017-10-30 12:02:23 +000084 .Case("clang_analyzer_hashDump", &ExprInspectionChecker::analyzerHashDump)
Craig Topper0dbb7832014-05-27 02:45:47 +000085 .Default(nullptr);
Jordan Rose13937b12012-08-10 22:26:29 +000086
87 if (!Handler)
88 return false;
89
90 (this->*Handler)(CE, C);
91 return true;
92}
93
94static const char *getArgumentValueString(const CallExpr *CE,
95 CheckerContext &C) {
96 if (CE->getNumArgs() == 0)
97 return "Missing assertion argument";
98
99 ExplodedNode *N = C.getPredecessor();
100 const LocationContext *LC = N->getLocationContext();
101 ProgramStateRef State = N->getState();
102
103 const Expr *Assertion = CE->getArg(0);
104 SVal AssertionVal = State->getSVal(Assertion, LC);
105
106 if (AssertionVal.isUndef())
107 return "UNDEFINED";
108
109 ProgramStateRef StTrue, StFalse;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000110 std::tie(StTrue, StFalse) =
David Blaikie2fdacbc2013-02-20 05:52:05 +0000111 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rose13937b12012-08-10 22:26:29 +0000112
113 if (StTrue) {
114 if (StFalse)
115 return "UNKNOWN";
116 else
117 return "TRUE";
118 } else {
119 if (StFalse)
120 return "FALSE";
121 else
122 llvm_unreachable("Invalid constraint; neither true or false.");
123 }
124}
125
Artem Dergachev30ed5462016-11-30 17:57:18 +0000126ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
127 CheckerContext &C) const {
128 ExplodedNode *N = C.generateNonFatalErrorNode();
129 reportBug(Msg, C.getBugReporter(), N);
130 return N;
131}
132
133ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
134 BugReporter &BR,
135 ExplodedNode *N) const {
136 if (!N)
137 return nullptr;
138
Artem Dergachev895242f2016-01-15 15:22:05 +0000139 if (!BT)
140 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
141
Artem Dergachev30ed5462016-11-30 17:57:18 +0000142 BR.emitReport(llvm::make_unique<BugReport>(*BT, Msg, N));
143 return N;
Artem Dergachev895242f2016-01-15 15:22:05 +0000144}
145
Jordan Rose13937b12012-08-10 22:26:29 +0000146void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
147 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000148 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordy Rose31ae2592012-05-16 16:01:07 +0000149
Jordy Rose31ae2592012-05-16 16:01:07 +0000150 // A specific instantiation of an inlined function may have more constrained
151 // values than can generally be assumed. Skip the check.
Craig Topper0dbb7832014-05-27 02:45:47 +0000152 if (LC->getCurrentStackFrame()->getParent() != nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000153 return;
Jordy Rose31ae2592012-05-16 16:01:07 +0000154
Artem Dergachev895242f2016-01-15 15:22:05 +0000155 reportBug(getArgumentValueString(CE, C), C);
Jordan Rose13937b12012-08-10 22:26:29 +0000156}
Jordy Rose31ae2592012-05-16 16:01:07 +0000157
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000158void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
159 CheckerContext &C) const {
Artem Dergachev895242f2016-01-15 15:22:05 +0000160 reportBug("REACHABLE", C);
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000161}
162
Artem Dergachev30ed5462016-11-30 17:57:18 +0000163void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE,
164 CheckerContext &C) const {
165 ++ReachedStats[CE].NumTimesReached;
166 if (!ReachedStats[CE].ExampleNode) {
167 // Later, in checkEndAnalysis, we'd throw a report against it.
168 ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode();
169 }
170}
171
Jordan Rose13937b12012-08-10 22:26:29 +0000172void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
173 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000174 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordan Rose13937b12012-08-10 22:26:29 +0000175
176 // An inlined function could conceivably also be analyzed as a top-level
177 // function. We ignore this case and only emit a message (TRUE or FALSE)
178 // when we are analyzing it as an inlined function. This means that
179 // clang_analyzer_checkInlined(true) should always print TRUE, but
180 // clang_analyzer_checkInlined(false) should never actually print anything.
Craig Topper0dbb7832014-05-27 02:45:47 +0000181 if (LC->getCurrentStackFrame()->getParent() == nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000182 return;
183
Artem Dergachev895242f2016-01-15 15:22:05 +0000184 reportBug(getArgumentValueString(CE, C), C);
185}
Jordan Rose13937b12012-08-10 22:26:29 +0000186
Artem Dergachev895242f2016-01-15 15:22:05 +0000187void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
188 CheckerContext &C) const {
Artem Dergachev30ed5462016-11-30 17:57:18 +0000189 if (CE->getNumArgs() == 0) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000190 reportBug("Missing argument for explaining", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000191 return;
192 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000193
194 SVal V = C.getSVal(CE->getArg(0));
195 SValExplainer Ex(C.getASTContext());
196 reportBug(Ex.Visit(V), C);
197}
198
Artem Dergachev30ed5462016-11-30 17:57:18 +0000199void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
200 CheckerContext &C) const {
201 if (CE->getNumArgs() == 0) {
202 reportBug("Missing argument for dumping", C);
203 return;
204 }
205
206 SVal V = C.getSVal(CE->getArg(0));
207
208 llvm::SmallString<32> Str;
209 llvm::raw_svector_ostream OS(Str);
210 V.dumpToStream(OS);
211 reportBug(OS.str(), C);
212}
213
Artem Dergachev895242f2016-01-15 15:22:05 +0000214void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
215 CheckerContext &C) const {
Artem Dergachev30ed5462016-11-30 17:57:18 +0000216 if (CE->getNumArgs() == 0) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000217 reportBug("Missing region for obtaining extent", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000218 return;
219 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000220
221 auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion());
Artem Dergachev30ed5462016-11-30 17:57:18 +0000222 if (!MR) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000223 reportBug("Obtaining extent of a non-region", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000224 return;
225 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000226
227 ProgramStateRef State = C.getState();
228 State = State->BindExpr(CE, C.getLocationContext(),
229 MR->getExtent(C.getSValBuilder()));
230 C.addTransition(State);
Jordy Rose31ae2592012-05-16 16:01:07 +0000231}
232
Artem Dergachev30ed5462016-11-30 17:57:18 +0000233void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE,
234 CheckerContext &C) const {
235 C.getState()->dump();
236}
237
Artem Dergachev733e71b2015-12-10 09:28:06 +0000238void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
239 CheckerContext &C) const {
240 if (CE->getNumArgs() == 0)
241 return;
242 SVal Val = C.getSVal(CE->getArg(0));
243 SymbolRef Sym = Val.getAsSymbol();
244 if (!Sym)
245 return;
246
247 ProgramStateRef State = C.getState();
248 State = State->add<MarkedSymbols>(Sym);
249 C.addTransition(State);
250}
251
252void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
253 CheckerContext &C) const {
254 ProgramStateRef State = C.getState();
255 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
Artem Dergachev30ed5462016-11-30 17:57:18 +0000256 ExplodedNode *N = C.getPredecessor();
Artem Dergachev733e71b2015-12-10 09:28:06 +0000257 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000258 SymbolRef Sym = *I;
Artem Dergachev733e71b2015-12-10 09:28:06 +0000259 if (!SymReaper.isDead(Sym))
260 continue;
261
Artem Dergachev30ed5462016-11-30 17:57:18 +0000262 // The non-fatal error node should be the same for all reports.
263 if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C))
264 N = BugNode;
Artem Dergachev895242f2016-01-15 15:22:05 +0000265 State = State->remove<MarkedSymbols>(Sym);
Artem Dergachev733e71b2015-12-10 09:28:06 +0000266 }
Artem Dergachev30ed5462016-11-30 17:57:18 +0000267 C.addTransition(State, N);
268}
269
270void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
271 ExprEngine &Eng) const {
272 for (auto Item: ReachedStats) {
273 unsigned NumTimesReached = Item.second.NumTimesReached;
274 ExplodedNode *N = Item.second.ExampleNode;
275
Pavel Labathd570a612017-01-16 15:57:07 +0000276 reportBug(llvm::to_string(NumTimesReached), BR, N);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000277 }
Peter Szecsi657ac142017-07-25 19:23:23 +0000278 ReachedStats.clear();
Artem Dergachev733e71b2015-12-10 09:28:06 +0000279}
280
Jordan Rosee9c57222013-07-19 00:59:08 +0000281void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
282 CheckerContext &C) const {
283 LLVM_BUILTIN_TRAP;
284}
285
Gabor Horvath3bd24f92017-10-30 12:02:23 +0000286void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
287 CheckerContext &C) const {
288 const LangOptions &Opts = C.getLangOpts();
289 const SourceManager &SM = C.getSourceManager();
290 FullSourceLoc FL(CE->getArg(0)->getLocStart(), SM);
291 std::string HashContent =
292 GetIssueString(SM, FL, getCheckName().getName(), "Category",
293 C.getLocationContext()->getDecl(), Opts);
294
295 reportBug(HashContent, C);
296}
297
Jordy Rose31ae2592012-05-16 16:01:07 +0000298void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
299 Mgr.registerChecker<ExprInspectionChecker>();
300}