1 //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ClangSACheckers.h"
11 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
12 #include "clang/StaticAnalyzer/Core/Checker.h"
13 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
14 #include "clang/StaticAnalyzer/Checkers/SValExplainer.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Support/ScopedPrinter.h"
18 using namespace clang;
22 class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols,
24 mutable std::unique_ptr<BugType> BT;
26 // These stats are per-analysis, not per-branch, hence they shouldn't
27 // stay inside the program state.
29 ExplodedNode *ExampleNode;
30 unsigned NumTimesReached;
32 mutable llvm::DenseMap<const CallExpr *, ReachedStat> ReachedStats;
34 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
35 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
36 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
37 void analyzerNumTimesReached(const CallExpr *CE, CheckerContext &C) const;
38 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
39 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
40 void analyzerDump(const CallExpr *CE, CheckerContext &C) const;
41 void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
42 void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
43 void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
45 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
46 CheckerContext &C) const;
48 ExplodedNode *reportBug(llvm::StringRef Msg, CheckerContext &C) const;
49 ExplodedNode *reportBug(llvm::StringRef Msg, BugReporter &BR,
50 ExplodedNode *N) const;
53 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
54 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
55 void checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
56 ExprEngine &Eng) const;
60 REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
62 bool ExprInspectionChecker::evalCall(const CallExpr *CE,
63 CheckerContext &C) const {
64 // These checks should have no effect on the surrounding environment
65 // (globals should not be invalidated, etc), hence the use of evalCall.
66 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
67 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
68 .Case("clang_analyzer_checkInlined",
69 &ExprInspectionChecker::analyzerCheckInlined)
70 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
71 .Case("clang_analyzer_warnIfReached",
72 &ExprInspectionChecker::analyzerWarnIfReached)
73 .Case("clang_analyzer_warnOnDeadSymbol",
74 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
75 .StartsWith("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
76 .StartsWith("clang_analyzer_dump", &ExprInspectionChecker::analyzerDump)
77 .Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent)
78 .Case("clang_analyzer_printState",
79 &ExprInspectionChecker::analyzerPrintState)
80 .Case("clang_analyzer_numTimesReached",
81 &ExprInspectionChecker::analyzerNumTimesReached)
87 (this->*Handler)(CE, C);
91 static const char *getArgumentValueString(const CallExpr *CE,
93 if (CE->getNumArgs() == 0)
94 return "Missing assertion argument";
96 ExplodedNode *N = C.getPredecessor();
97 const LocationContext *LC = N->getLocationContext();
98 ProgramStateRef State = N->getState();
100 const Expr *Assertion = CE->getArg(0);
101 SVal AssertionVal = State->getSVal(Assertion, LC);
103 if (AssertionVal.isUndef())
106 ProgramStateRef StTrue, StFalse;
107 std::tie(StTrue, StFalse) =
108 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
119 llvm_unreachable("Invalid constraint; neither true or false.");
123 ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
124 CheckerContext &C) const {
125 ExplodedNode *N = C.generateNonFatalErrorNode();
126 reportBug(Msg, C.getBugReporter(), N);
130 ExplodedNode *ExprInspectionChecker::reportBug(llvm::StringRef Msg,
132 ExplodedNode *N) const {
137 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
139 BR.emitReport(llvm::make_unique<BugReport>(*BT, Msg, N));
143 void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
144 CheckerContext &C) const {
145 const LocationContext *LC = C.getPredecessor()->getLocationContext();
147 // A specific instantiation of an inlined function may have more constrained
148 // values than can generally be assumed. Skip the check.
149 if (LC->getCurrentStackFrame()->getParent() != nullptr)
152 reportBug(getArgumentValueString(CE, C), C);
155 void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
156 CheckerContext &C) const {
157 reportBug("REACHABLE", C);
160 void ExprInspectionChecker::analyzerNumTimesReached(const CallExpr *CE,
161 CheckerContext &C) const {
162 ++ReachedStats[CE].NumTimesReached;
163 if (!ReachedStats[CE].ExampleNode) {
164 // Later, in checkEndAnalysis, we'd throw a report against it.
165 ReachedStats[CE].ExampleNode = C.generateNonFatalErrorNode();
169 void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
170 CheckerContext &C) const {
171 const LocationContext *LC = C.getPredecessor()->getLocationContext();
173 // An inlined function could conceivably also be analyzed as a top-level
174 // function. We ignore this case and only emit a message (TRUE or FALSE)
175 // when we are analyzing it as an inlined function. This means that
176 // clang_analyzer_checkInlined(true) should always print TRUE, but
177 // clang_analyzer_checkInlined(false) should never actually print anything.
178 if (LC->getCurrentStackFrame()->getParent() == nullptr)
181 reportBug(getArgumentValueString(CE, C), C);
184 void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
185 CheckerContext &C) const {
186 if (CE->getNumArgs() == 0) {
187 reportBug("Missing argument for explaining", C);
191 SVal V = C.getSVal(CE->getArg(0));
192 SValExplainer Ex(C.getASTContext());
193 reportBug(Ex.Visit(V), C);
196 void ExprInspectionChecker::analyzerDump(const CallExpr *CE,
197 CheckerContext &C) const {
198 if (CE->getNumArgs() == 0) {
199 reportBug("Missing argument for dumping", C);
203 SVal V = C.getSVal(CE->getArg(0));
205 llvm::SmallString<32> Str;
206 llvm::raw_svector_ostream OS(Str);
208 reportBug(OS.str(), C);
211 void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
212 CheckerContext &C) const {
213 if (CE->getNumArgs() == 0) {
214 reportBug("Missing region for obtaining extent", C);
218 auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion());
220 reportBug("Obtaining extent of a non-region", C);
224 ProgramStateRef State = C.getState();
225 State = State->BindExpr(CE, C.getLocationContext(),
226 MR->getExtent(C.getSValBuilder()));
227 C.addTransition(State);
230 void ExprInspectionChecker::analyzerPrintState(const CallExpr *CE,
231 CheckerContext &C) const {
232 C.getState()->dump();
235 void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
236 CheckerContext &C) const {
237 if (CE->getNumArgs() == 0)
239 SVal Val = C.getSVal(CE->getArg(0));
240 SymbolRef Sym = Val.getAsSymbol();
244 ProgramStateRef State = C.getState();
245 State = State->add<MarkedSymbols>(Sym);
246 C.addTransition(State);
249 void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
250 CheckerContext &C) const {
251 ProgramStateRef State = C.getState();
252 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
253 ExplodedNode *N = C.getPredecessor();
254 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
256 if (!SymReaper.isDead(Sym))
259 // The non-fatal error node should be the same for all reports.
260 if (ExplodedNode *BugNode = reportBug("SYMBOL DEAD", C))
262 State = State->remove<MarkedSymbols>(Sym);
264 C.addTransition(State, N);
267 void ExprInspectionChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
268 ExprEngine &Eng) const {
269 for (auto Item: ReachedStats) {
270 unsigned NumTimesReached = Item.second.NumTimesReached;
271 ExplodedNode *N = Item.second.ExampleNode;
273 reportBug(llvm::to_string(NumTimesReached), BR, N);
277 void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
278 CheckerContext &C) const {
282 void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
283 Mgr.registerChecker<ExprInspectionChecker>();