1 //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
10 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
11 #include "clang/StaticAnalyzer/Checkers/SValExplainer.h"
12 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
13 #include "clang/StaticAnalyzer/Core/Checker.h"
14 #include "clang/StaticAnalyzer/Core/IssueHash.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/Support/ScopedPrinter.h"
21 using namespace clang;
25 class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols,
27 mutable std::unique_ptr<BugType> BT;
29 // These stats are per-analysis, not per-branch, hence they shouldn't
30 // stay inside the program state.
32 ExplodedNode *ExampleNode;
33 unsigned NumTimesReached;
35 mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats;
37 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
38 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
39 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
40 void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const;
41 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
42 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
43 void analyzerDump(const CallExpr *CE, CheckerContext &C) const;
44 void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
45 void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
46 void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
47 void analyzerHashDump(const CallExpr *CE, CheckerContext &C) const;
48 void analyzerDenote(const CallExpr *CE, CheckerContext &C) const;
49 void analyzerExpress(const CallExpr *CE, CheckerContext &C) const;
50 void analyzerIsTainted(const CallExpr *CE, CheckerContext &C) const;
52 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
53 CheckerContext &C) const;
55 // Optional parameter `ExprVal` for expression value to be marked interesting.
56 ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C,
57 Optional<SVal> ExprVal = None) const;
58 ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR,
60 Optional<SVal> ExprVal = None) const;
63 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
64 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
65 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
66 ExprEngine &Eng) const;
70 REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
71 REGISTER_MAP_WITH_PROGRAMSTATE(DenotedSymbols, SymbolRef, const StringLiteral *)
73 bool ExprInspectionChecker::evalCall(const CallEvent &Call,
74 CheckerContext &C) const {
75 const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
79 // These checks should have no effect on the surrounding environment
80 // (globals should not be invalidated, etc), hence the use of evalCall.
82 llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
83 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
84 .Case("clang_analyzer_checkInlined",
85 &ExprInspectionChecker::analyzerCheckInlined)
86 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
87 .Case("clang_analyzer_warnIfReached",
88 &ExprInspectionChecker::analyzerWarnIfReached)
89 .Case("clang_analyzer_warnOnDeadSymbol",
90 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
91 .StartsWith("clang_analyzer_explain",
92 &ExprInspectionChecker::analyzerExplain)
93 .StartsWith("clang_analyzer_dump",
94 &ExprInspectionChecker::analyzerDump)
95 .Case("clang_analyzer_getExtent",
96 &ExprInspectionChecker::analyzerGetExtent)
97 .Case("clang_analyzer_printState",
98 &ExprInspectionChecker::analyzerPrintState)
99 .Case("clang_analyzer_numTimesReached",
100 &ExprInspectionChecker::analyzerNumTimesReached)
101 .Case("clang_analyzer_hashDump",
102 &ExprInspectionChecker::analyzerHashDump)
103 .Case("clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote)
104 .Case("clang_analyzer_express",
105 &ExprInspectionChecker::analyzerExpress)
106 .StartsWith("clang_analyzer_isTainted",
107 &ExprInspectionChecker::analyzerIsTainted)
113 (this->*Handler)(CE, C);
117 static const char *getArgumentValueString(const CallExpr *CE,
119 if (CE->getNumArgs() == 0)
120 return "Missing assertion argument";
122 ExplodedNode *N = C.getPredecessor();
123 const LocationContext *LC = N->getLocationContext();
124 ProgramStateRef State = N->getState();
126 const Expr *Assertion = CE->getArg(0);
127 SVal AssertionVal = State->getSVal(Assertion, LC);
129 if (AssertionVal.isUndef())
132 ProgramStateRef StTrue, StFalse;
133 std::tie(StTrue, StFalse) =
134 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
145 llvm_unreachable("Invalid constraint; neither true or false.");
149 ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
151 Optional<SVal> ExprVal) const {
152 ExplodedNode *N = C.generateNonFatalErrorNode();
153 reportBug(Msg, C.getBugReporter(), N, ExprVal);
157 ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
160 Optional<SVal> ExprVal) const {
165 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
167 auto R = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
169 R->markInteresting(*ExprVal);
171 BR.emitReport(std::move(R));
175 void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
176 CheckerContext &C) const {
177 const LocationContext *LC = C.getPredecessor()->getLocationContext();
179 // A specific instantiation of an inlined function may have more constrained
180 // values than can generally be assumed. Skip the check.
181 if (LC->getStackFrame()->getParent() != nullptr)
184 reportBug(getArgumentValueString(CE, C), C);
187 void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
188 CheckerContext &C) const {
189 reportBug("REACHABLE", C);
192 void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE,
193 CheckerContext &C) const {
194 ++ReachedStats[CE].NumTimesReached;
195 if (!ReachedStats[CE].ExampleNode) {
196 // Later, in checkEndAnalysis, we'd throw a report against it.
197 ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode();
201 void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
202 CheckerContext &C) const {
203 const LocationContext *LC = C.getPredecessor()->getLocationContext();
205 // An inlined function could conceivably also be analyzed as a top-level
206 // function. We ignore this case and only emit a message (TRUE or FALSE)
207 // when we are analyzing it as an inlined function. This means that
208 // clang_analyzer_checkInlined(true) should always print TRUE, but
209 // clang_analyzer_checkInlined(false) should never actually print anything.
210 if (LC->getStackFrame()->getParent() == nullptr)
213 reportBug(getArgumentValueString(CE, C), C);
216 void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
217 CheckerContext &C) const {
218 if (CE->getNumArgs() == 0) {
219 reportBug("Missing argument for explaining", C);
223 SVal V = C.getSVal(CE->getArg(0));
224 SValExplainer Ex(C.getASTContext());
225 reportBug(Ex.Visit(V), C);
228 void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
229 CheckerContext &C) const {
230 if (CE->getNumArgs() == 0) {
231 reportBug("Missing argument for dumping", C);
235 SVal V = C.getSVal(CE->getArg(0));
237 llvm::SmallString<32> Str;
238 llvm::raw_svector_ostream OS(Str);
240 reportBug(OS.str(), C);
243 void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
244 CheckerContext &C) const {
245 if (CE->getNumArgs() == 0) {
246 reportBug("Missing region for obtaining extent", C);
250 auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion());
252 reportBug("Obtaining extent of a non-region", C);
256 ProgramStateRef State = C.getState();
257 DefinedOrUnknownSVal Size = getDynamicSize(State, MR, C.getSValBuilder());
259 State = State->BindExpr(CE, C.getLocationContext(), Size);
260 C.addTransition(State);
263 void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE,
264 CheckerContext &C) const {
265 C.getState()->dump();
268 void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
269 CheckerContext &C) const {
270 if (CE->getNumArgs() == 0)
272 SVal Val = C.getSVal(CE->getArg(0));
273 SymbolRef Sym = Val.getAsSymbol();
277 ProgramStateRef State = C.getState();
278 State = State->add<MarkedSymbols>(Sym);
279 C.addTransition(State);
282 void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
283 CheckerContext &C) const {
284 ProgramStateRef State = C.getState();
285 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
286 ExplodedNode *N = C.getPredecessor();
287 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
289 if (!SymReaper.isDead(Sym))
292 // The non-fatal error node should be the same for all reports.
293 if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C))
295 State = State->remove<MarkedSymbols>(Sym);
298 for (auto I : State->get<DenotedSymbols>()) {
299 SymbolRef Sym = I.first;
300 if (!SymReaper.isLive(Sym))
301 State = State->remove<DenotedSymbols>(Sym);
304 C.addTransition(State, N);
307 void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
308 ExprEngine &Eng) const {
309 for (auto Item: ReachedStats) {
310 unsigned NumTimesReached = Item.second.NumTimesReached;
311 ExplodedNode *N = Item.second.ExampleNode;
313 reportBug(llvm::to_string(NumTimesReached), BR, N);
315 ReachedStats.clear();
318 void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
319 CheckerContext &C) const {
323 void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
324 CheckerContext &C) const {
325 const LangOptions &Opts = C.getLangOpts();
326 const SourceManager &SM = C.getSourceManager();
327 FullSourceLoc FL(CE->getArg(0)->getBeginLoc(), SM);
328 std::string HashContent =
329 GetIssueString(SM, FL, getCheckerName().getName(), "Category",
330 C.getLocationContext()->getDecl(), Opts);
332 reportBug(HashContent, C);
335 void ExprInspectionChecker::analyzerDenote(const CallExpr *CE,
336 CheckerContext &C) const {
337 if (CE->getNumArgs() < 2) {
338 reportBug("clang_analyzer_denote() requires a symbol and a string literal",
343 SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
345 reportBug("Not a symbol", C);
349 const auto *E = dyn_cast<StringLiteral>(CE->getArg(1)->IgnoreParenCasts());
351 reportBug("Not a string literal", C);
355 ProgramStateRef State = C.getState();
357 C.addTransition(C.getState()->set<DenotedSymbols>(Sym, E));
361 class SymbolExpressor
362 : public SymExprVisitor<SymbolExpressor, Optional<std::string>> {
363 ProgramStateRef State;
366 SymbolExpressor(ProgramStateRef State) : State(State) {}
368 Optional<std::string> lookup(const SymExpr *S) {
369 if (const StringLiteral *const *SLPtr = State->get<DenotedSymbols>(S)) {
370 const StringLiteral *SL = *SLPtr;
371 return std::string(SL->getBytes());
376 Optional<std::string> VisitSymExpr(const SymExpr *S) {
380 Optional<std::string> VisitSymIntExpr(const SymIntExpr *S) {
381 if (Optional<std::string> Str = lookup(S))
383 if (Optional<std::string> Str = Visit(S->getLHS()))
384 return (*Str + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + " " +
385 std::to_string(S->getRHS().getLimitedValue()) +
386 (S->getRHS().isUnsigned() ? "U" : ""))
391 Optional<std::string> VisitSymSymExpr(const SymSymExpr *S) {
392 if (Optional<std::string> Str = lookup(S))
394 if (Optional<std::string> Str1 = Visit(S->getLHS()))
395 if (Optional<std::string> Str2 = Visit(S->getRHS()))
396 return (*Str1 + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) +
401 Optional<std::string> VisitSymbolCast(const SymbolCast *S) {
402 if (Optional<std::string> Str = lookup(S))
404 if (Optional<std::string> Str = Visit(S->getOperand()))
405 return (Twine("(") + S->getType().getAsString() + ")" + *Str).str();
411 void ExprInspectionChecker::analyzerExpress(const CallExpr *CE,
412 CheckerContext &C) const {
413 if (CE->getNumArgs() == 0) {
414 reportBug("clang_analyzer_express() requires a symbol", C);
418 SVal ArgVal = C.getSVal(CE->getArg(0));
419 SymbolRef Sym = ArgVal.getAsSymbol();
421 reportBug("Not a symbol", C);
425 SymbolExpressor V(C.getState());
426 auto Str = V.Visit(Sym);
428 reportBug("Unable to express", C);
432 reportBug(*Str, C, ArgVal);
435 void ExprInspectionChecker::analyzerIsTainted(const CallExpr *CE,
436 CheckerContext &C) const {
437 if (CE->getNumArgs() != 1) {
438 reportBug("clang_analyzer_isTainted() requires exactly one argument", C);
441 const bool IsTainted =
442 taint::isTainted(C.getState(), CE->getArg(0), C.getLocationContext());
443 reportBug(IsTainted ? "YES" : "NO", C);
446 void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
447 Mgr.registerChecker<ExprInspectionChecker>();
450 bool ento::shouldRegisterExprInspectionChecker(const CheckerManager &mgr) {