blob: 7c53b2d21a5c5ab4496d350b1c187d79f4871ef1 [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;
Artem Dergacheve527df02018-09-25 23:50:53 +000046 void analyzerDenote(const CallExpr *CE, CheckerContext &C) const;
47 void analyzerExpress(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose13937b12012-08-10 22:26:29 +000048
49 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
50 CheckerContext &C) const;
51
Artem Dergachev30ed5462016-11-30 17:57:18 +000052 ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C) const;
53 ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR,
54 ExplodedNode *N) const;
Artem Dergachev895242f2016-01-15 15:22:05 +000055
Jordy Rose31ae2592012-05-16 16:01:07 +000056public:
57 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000058 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Artem Dergachev30ed5462016-11-30 17:57:18 +000059 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
60 ExprEngine &Eng) const;
Jordy Rose31ae2592012-05-16 16:01:07 +000061};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000062}
Jordy Rose31ae2592012-05-16 16:01:07 +000063
Artem Dergachev895242f2016-01-15 15:22:05 +000064REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
Artem Dergacheve527df02018-09-25 23:50:53 +000065REGISTER_MAP_WITH_PROGRAMSTATE(DenotedSymbols, SymbolRef, const StringLiteral *)
Artem Dergachev733e71b2015-12-10 09:28:06 +000066
Jordy Rose31ae2592012-05-16 16:01:07 +000067bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rose13937b12012-08-10 22:26:29 +000068 CheckerContext &C) const {
Jordy Rose31ae2592012-05-16 16:01:07 +000069 // These checks should have no effect on the surrounding environment
Jordan Rose13937b12012-08-10 22:26:29 +000070 // (globals should not be invalidated, etc), hence the use of evalCall.
71 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
72 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
73 .Case("clang_analyzer_checkInlined",
74 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Rosee9c57222013-07-19 00:59:08 +000075 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Artem Dergachev733e71b2015-12-10 09:28:06 +000076 .Case("clang_analyzer_warnIfReached",
77 &ExprInspectionChecker::analyzerWarnIfReached)
78 .Case("clang_analyzer_warnOnDeadSymbol",
79 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
Anna Zaks37faed92017-03-09 00:01:10 +000080 .StartsWith("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
81 .StartsWith("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump)
Artem Dergachev895242f2016-01-15 15:22:05 +000082 .Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent)
Artem Dergachev30ed5462016-11-30 17:57:18 +000083 .Case("clang_analyzer_printState",
84 &ExprInspectionChecker::analyzerPrintState)
85 .Case("clang_analyzer_numTimesReached",
86 &ExprInspectionChecker::analyzerNumTimesReached)
Gabor Horvath3bd24f92017-10-30 12:02:23 +000087 .Case("clang_analyzer_hashDump", &ExprInspectionChecker::analyzerHashDump)
Artem Dergacheve527df02018-09-25 23:50:53 +000088 .Case("clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote)
89 .Case("clang_analyzer_express", &ExprInspectionChecker::analyzerExpress)
Craig Topper0dbb7832014-05-27 02:45:47 +000090 .Default(nullptr);
Jordan Rose13937b12012-08-10 22:26:29 +000091
92 if (!Handler)
93 return false;
94
95 (this->*Handler)(CE, C);
96 return true;
97}
98
99static const char *getArgumentValueString(const CallExpr *CE,
100 CheckerContext &C) {
101 if (CE->getNumArgs() == 0)
102 return "Missing assertion argument";
103
104 ExplodedNode *N = C.getPredecessor();
105 const LocationContext *LC = N->getLocationContext();
106 ProgramStateRef State = N->getState();
107
108 const Expr *Assertion = CE->getArg(0);
109 SVal AssertionVal = State->getSVal(Assertion, LC);
110
111 if (AssertionVal.isUndef())
112 return "UNDEFINED";
113
114 ProgramStateRef StTrue, StFalse;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000115 std::tie(StTrue, StFalse) =
David Blaikie2fdacbc2013-02-20 05:52:05 +0000116 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rose13937b12012-08-10 22:26:29 +0000117
118 if (StTrue) {
119 if (StFalse)
120 return "UNKNOWN";
121 else
122 return "TRUE";
123 } else {
124 if (StFalse)
125 return "FALSE";
126 else
127 llvm_unreachable("Invalid constraint; neither true or false.");
128 }
129}
130
Artem Dergachev30ed5462016-11-30 17:57:18 +0000131ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
132 CheckerContext &C) const {
133 ExplodedNode *N = C.generateNonFatalErrorNode();
134 reportBug(Msg, C.getBugReporter(), N);
135 return N;
136}
137
138ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
139 BugReporter &BR,
140 ExplodedNode *N) const {
141 if (!N)
142 return nullptr;
143
Artem Dergachev895242f2016-01-15 15:22:05 +0000144 if (!BT)
145 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
146
Artem Dergachev30ed5462016-11-30 17:57:18 +0000147 BR.emitReport(llvm::make_unique<BugReport>(*BT, Msg, N));
148 return N;
Artem Dergachev895242f2016-01-15 15:22:05 +0000149}
150
Jordan Rose13937b12012-08-10 22:26:29 +0000151void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
152 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000153 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordy Rose31ae2592012-05-16 16:01:07 +0000154
Jordy Rose31ae2592012-05-16 16:01:07 +0000155 // A specific instantiation of an inlined function may have more constrained
156 // values than can generally be assumed. Skip the check.
George Karpenkovdd18b112018-06-27 01:51:55 +0000157 if (LC->getStackFrame()->getParent() != nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000158 return;
Jordy Rose31ae2592012-05-16 16:01:07 +0000159
Artem Dergachev895242f2016-01-15 15:22:05 +0000160 reportBug(getArgumentValueString(CE, C), C);
Jordan Rose13937b12012-08-10 22:26:29 +0000161}
Jordy Rose31ae2592012-05-16 16:01:07 +0000162
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000163void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
164 CheckerContext &C) const {
Artem Dergachev895242f2016-01-15 15:22:05 +0000165 reportBug("REACHABLE", C);
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000166}
167
Artem Dergachev30ed5462016-11-30 17:57:18 +0000168void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE,
169 CheckerContext &C) const {
170 ++ReachedStats[CE].NumTimesReached;
171 if (!ReachedStats[CE].ExampleNode) {
172 // Later, in checkEndAnalysis, we'd throw a report against it.
173 ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode();
174 }
175}
176
Jordan Rose13937b12012-08-10 22:26:29 +0000177void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
178 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000179 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordan Rose13937b12012-08-10 22:26:29 +0000180
181 // An inlined function could conceivably also be analyzed as a top-level
182 // function. We ignore this case and only emit a message (TRUE or FALSE)
183 // when we are analyzing it as an inlined function. This means that
184 // clang_analyzer_checkInlined(true) should always print TRUE, but
185 // clang_analyzer_checkInlined(false) should never actually print anything.
George Karpenkovdd18b112018-06-27 01:51:55 +0000186 if (LC->getStackFrame()->getParent() == nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000187 return;
188
Artem Dergachev895242f2016-01-15 15:22:05 +0000189 reportBug(getArgumentValueString(CE, C), C);
190}
Jordan Rose13937b12012-08-10 22:26:29 +0000191
Artem Dergachev895242f2016-01-15 15:22:05 +0000192void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
193 CheckerContext &C) const {
Artem Dergachev30ed5462016-11-30 17:57:18 +0000194 if (CE->getNumArgs() == 0) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000195 reportBug("Missing argument for explaining", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000196 return;
197 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000198
199 SVal V = C.getSVal(CE->getArg(0));
200 SValExplainer Ex(C.getASTContext());
201 reportBug(Ex.Visit(V), C);
202}
203
Artem Dergachev30ed5462016-11-30 17:57:18 +0000204void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
205 CheckerContext &C) const {
206 if (CE->getNumArgs() == 0) {
207 reportBug("Missing argument for dumping", C);
208 return;
209 }
210
211 SVal V = C.getSVal(CE->getArg(0));
212
213 llvm::SmallString<32> Str;
214 llvm::raw_svector_ostream OS(Str);
215 V.dumpToStream(OS);
216 reportBug(OS.str(), C);
217}
218
Artem Dergachev895242f2016-01-15 15:22:05 +0000219void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
220 CheckerContext &C) const {
Artem Dergachev30ed5462016-11-30 17:57:18 +0000221 if (CE->getNumArgs() == 0) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000222 reportBug("Missing region for obtaining extent", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000223 return;
224 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000225
226 auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion());
Artem Dergachev30ed5462016-11-30 17:57:18 +0000227 if (!MR) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000228 reportBug("Obtaining extent of a non-region", C);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000229 return;
230 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000231
232 ProgramStateRef State = C.getState();
233 State = State->BindExpr(CE, C.getLocationContext(),
234 MR->getExtent(C.getSValBuilder()));
235 C.addTransition(State);
Jordy Rose31ae2592012-05-16 16:01:07 +0000236}
237
Artem Dergachev30ed5462016-11-30 17:57:18 +0000238void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE,
239 CheckerContext &C) const {
240 C.getState()->dump();
241}
242
Artem Dergachev733e71b2015-12-10 09:28:06 +0000243void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
244 CheckerContext &C) const {
245 if (CE->getNumArgs() == 0)
246 return;
247 SVal Val = C.getSVal(CE->getArg(0));
248 SymbolRef Sym = Val.getAsSymbol();
249 if (!Sym)
250 return;
251
252 ProgramStateRef State = C.getState();
253 State = State->add<MarkedSymbols>(Sym);
254 C.addTransition(State);
255}
256
257void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
258 CheckerContext &C) const {
259 ProgramStateRef State = C.getState();
260 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
Artem Dergachev30ed5462016-11-30 17:57:18 +0000261 ExplodedNode *N = C.getPredecessor();
Artem Dergachev733e71b2015-12-10 09:28:06 +0000262 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000263 SymbolRef Sym = *I;
Artem Dergachev733e71b2015-12-10 09:28:06 +0000264 if (!SymReaper.isDead(Sym))
265 continue;
266
Artem Dergachev30ed5462016-11-30 17:57:18 +0000267 // The non-fatal error node should be the same for all reports.
268 if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C))
269 N = BugNode;
Artem Dergachev895242f2016-01-15 15:22:05 +0000270 State = State->remove<MarkedSymbols>(Sym);
Artem Dergachev733e71b2015-12-10 09:28:06 +0000271 }
Artem Dergacheve527df02018-09-25 23:50:53 +0000272
273 for (auto I : State->get<DenotedSymbols>()) {
274 SymbolRef Sym = I.first;
275 if (!SymReaper.isLive(Sym))
276 State = State->remove<DenotedSymbols>(Sym);
277 }
278
Artem Dergachev30ed5462016-11-30 17:57:18 +0000279 C.addTransition(State, N);
280}
281
282void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
283 ExprEngine &Eng) const {
284 for (auto Item: ReachedStats) {
285 unsigned NumTimesReached = Item.second.NumTimesReached;
286 ExplodedNode *N = Item.second.ExampleNode;
287
Pavel Labathd570a612017-01-16 15:57:07 +0000288 reportBug(llvm::to_string(NumTimesReached), BR, N);
Artem Dergachev30ed5462016-11-30 17:57:18 +0000289 }
Peter Szecsi657ac142017-07-25 19:23:23 +0000290 ReachedStats.clear();
Artem Dergachev733e71b2015-12-10 09:28:06 +0000291}
292
Jordan Rosee9c57222013-07-19 00:59:08 +0000293void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
294 CheckerContext &C) const {
295 LLVM_BUILTIN_TRAP;
296}
297
Gabor Horvath3bd24f92017-10-30 12:02:23 +0000298void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
299 CheckerContext &C) const {
300 const LangOptions &Opts = C.getLangOpts();
301 const SourceManager &SM = C.getSourceManager();
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000302 FullSourceLoc FL(CE->getArg(0)->getBeginLoc(), SM);
Gabor Horvath3bd24f92017-10-30 12:02:23 +0000303 std::string HashContent =
304 GetIssueString(SM, FL, getCheckName().getName(), "Category",
305 C.getLocationContext()->getDecl(), Opts);
306
307 reportBug(HashContent, C);
308}
309
Artem Dergacheve527df02018-09-25 23:50:53 +0000310void ExprInspectionChecker::analyzerDenote(const CallExpr *CE,
311 CheckerContext &C) const {
312 if (CE->getNumArgs() < 2) {
313 reportBug("clang_analyzer_denote() requires a symbol and a string literal",
314 C);
315 return;
316 }
317
318 SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
319 if (!Sym) {
320 reportBug("Not a symbol", C);
321 return;
322 }
323
324 if (!isa<SymbolData>(Sym)) {
325 reportBug("Not an atomic symbol", C);
326 return;
327 }
328
329 const auto *E = dyn_cast<StringLiteral>(CE->getArg(1)->IgnoreParenCasts());
330 if (!E) {
331 reportBug("Not a string literal", C);
332 return;
333 }
334
335 ProgramStateRef State = C.getState();
336
337 C.addTransition(C.getState()->set<DenotedSymbols>(Sym, E));
338}
339
340class SymbolExpressor
341 : public SymExprVisitor<SymbolExpressor, Optional<std::string>> {
342 ProgramStateRef State;
343
344public:
345 SymbolExpressor(ProgramStateRef State) : State(State) {}
346
347 Optional<std::string> VisitSymExpr(const SymExpr *S) {
348 if (const StringLiteral *const *SLPtr = State->get<DenotedSymbols>(S)) {
349 const StringLiteral *SL = *SLPtr;
350 return std::string(SL->getBytes());
351 }
352 return None;
353 }
354
355 Optional<std::string> VisitSymIntExpr(const SymIntExpr *S) {
356 if (auto Str = Visit(S->getLHS()))
357 return (*Str + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + " " +
358 std::to_string(S->getRHS().getLimitedValue()) +
359 (S->getRHS().isUnsigned() ? "U" : ""))
360 .str();
361 return None;
362 }
363
364 Optional<std::string> VisitSymSymExpr(const SymSymExpr *S) {
365 if (auto Str1 = Visit(S->getLHS()))
366 if (auto Str2 = Visit(S->getRHS()))
367 return (*Str1 + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) +
368 " " + *Str2).str();
369 return None;
370 }
371};
372
373void ExprInspectionChecker::analyzerExpress(const CallExpr *CE,
374 CheckerContext &C) const {
375 if (CE->getNumArgs() == 0) {
376 reportBug("clang_analyzer_express() requires a symbol", C);
377 return;
378 }
379
380 SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
381 if (!Sym) {
382 reportBug("Not a symbol", C);
383 return;
384 }
385
386 SymbolExpressor V(C.getState());
387 auto Str = V.Visit(Sym);
388 if (!Str) {
389 reportBug("Unable to express", C);
390 return;
391 }
392
393 reportBug(*Str, C);
394}
395
Jordy Rose31ae2592012-05-16 16:01:07 +0000396void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
397 Mgr.registerChecker<ExprInspectionChecker>();
398}