blob: 31e9150cc15b3182dd4029357bf8f987a736399a [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"
Jordy Rose31ae2592012-05-16 16:01:07 +000011#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "clang/StaticAnalyzer/Core/Checker.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000013#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Artem Dergachev895242f2016-01-15 15:22:05 +000014#include "clang/StaticAnalyzer/Checkers/SValExplainer.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000015#include "llvm/ADT/StringSwitch.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000016
17using namespace clang;
18using namespace ento;
19
20namespace {
Artem Dergachev733e71b2015-12-10 09:28:06 +000021class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000022 mutable std::unique_ptr<BugType> BT;
Jordan Rose13937b12012-08-10 22:26:29 +000023
24 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
25 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose9db2d9a2013-10-03 16:57:03 +000026 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosee9c57222013-07-19 00:59:08 +000027 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000028 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev895242f2016-01-15 15:22:05 +000029 void analyzerExplain(const CallExpr *CE, CheckerContext &C) const;
30 void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose13937b12012-08-10 22:26:29 +000031
32 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
33 CheckerContext &C) const;
34
Artem Dergachev895242f2016-01-15 15:22:05 +000035 void reportBug(llvm::StringRef Msg, CheckerContext &C) const;
36
Jordy Rose31ae2592012-05-16 16:01:07 +000037public:
38 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000039 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Jordy Rose31ae2592012-05-16 16:01:07 +000040};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000041}
Jordy Rose31ae2592012-05-16 16:01:07 +000042
Artem Dergachev895242f2016-01-15 15:22:05 +000043REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
Artem Dergachev733e71b2015-12-10 09:28:06 +000044
Jordy Rose31ae2592012-05-16 16:01:07 +000045bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rose13937b12012-08-10 22:26:29 +000046 CheckerContext &C) const {
Jordy Rose31ae2592012-05-16 16:01:07 +000047 // These checks should have no effect on the surrounding environment
Jordan Rose13937b12012-08-10 22:26:29 +000048 // (globals should not be invalidated, etc), hence the use of evalCall.
49 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
50 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
51 .Case("clang_analyzer_checkInlined",
52 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Rosee9c57222013-07-19 00:59:08 +000053 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Artem Dergachev733e71b2015-12-10 09:28:06 +000054 .Case("clang_analyzer_warnIfReached",
55 &ExprInspectionChecker::analyzerWarnIfReached)
56 .Case("clang_analyzer_warnOnDeadSymbol",
57 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
Artem Dergachev895242f2016-01-15 15:22:05 +000058 .Case("clang_analyzer_explain", &ExprInspectionChecker::analyzerExplain)
59 .Case("clang_analyzer_getExtent", &ExprInspectionChecker::analyzerGetExtent)
Craig Topper0dbb7832014-05-27 02:45:47 +000060 .Default(nullptr);
Jordan Rose13937b12012-08-10 22:26:29 +000061
62 if (!Handler)
63 return false;
64
65 (this->*Handler)(CE, C);
66 return true;
67}
68
69static const char *getArgumentValueString(const CallExpr *CE,
70 CheckerContext &C) {
71 if (CE->getNumArgs() == 0)
72 return "Missing assertion argument";
73
74 ExplodedNode *N = C.getPredecessor();
75 const LocationContext *LC = N->getLocationContext();
76 ProgramStateRef State = N->getState();
77
78 const Expr *Assertion = CE->getArg(0);
79 SVal AssertionVal = State->getSVal(Assertion, LC);
80
81 if (AssertionVal.isUndef())
82 return "UNDEFINED";
83
84 ProgramStateRef StTrue, StFalse;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +000085 std::tie(StTrue, StFalse) =
David Blaikie2fdacbc2013-02-20 05:52:05 +000086 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rose13937b12012-08-10 22:26:29 +000087
88 if (StTrue) {
89 if (StFalse)
90 return "UNKNOWN";
91 else
92 return "TRUE";
93 } else {
94 if (StFalse)
95 return "FALSE";
96 else
97 llvm_unreachable("Invalid constraint; neither true or false.");
98 }
99}
100
Artem Dergachev895242f2016-01-15 15:22:05 +0000101void ExprInspectionChecker::reportBug(llvm::StringRef Msg,
102 CheckerContext &C) const {
103 if (!BT)
104 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
105
106 ExplodedNode *N = C.generateNonFatalErrorNode();
107 if (!N)
108 return;
109
110 C.emitReport(llvm::make_unique<BugReport>(*BT, Msg, N));
111}
112
Jordan Rose13937b12012-08-10 22:26:29 +0000113void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
114 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000115 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordy Rose31ae2592012-05-16 16:01:07 +0000116
Jordy Rose31ae2592012-05-16 16:01:07 +0000117 // A specific instantiation of an inlined function may have more constrained
118 // values than can generally be assumed. Skip the check.
Craig Topper0dbb7832014-05-27 02:45:47 +0000119 if (LC->getCurrentStackFrame()->getParent() != nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000120 return;
Jordy Rose31ae2592012-05-16 16:01:07 +0000121
Artem Dergachev895242f2016-01-15 15:22:05 +0000122 reportBug(getArgumentValueString(CE, C), C);
Jordan Rose13937b12012-08-10 22:26:29 +0000123}
Jordy Rose31ae2592012-05-16 16:01:07 +0000124
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000125void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
126 CheckerContext &C) const {
Artem Dergachev895242f2016-01-15 15:22:05 +0000127 reportBug("REACHABLE", C);
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000128}
129
Jordan Rose13937b12012-08-10 22:26:29 +0000130void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
131 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000132 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordan Rose13937b12012-08-10 22:26:29 +0000133
134 // An inlined function could conceivably also be analyzed as a top-level
135 // function. We ignore this case and only emit a message (TRUE or FALSE)
136 // when we are analyzing it as an inlined function. This means that
137 // clang_analyzer_checkInlined(true) should always print TRUE, but
138 // clang_analyzer_checkInlined(false) should never actually print anything.
Craig Topper0dbb7832014-05-27 02:45:47 +0000139 if (LC->getCurrentStackFrame()->getParent() == nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000140 return;
141
Artem Dergachev895242f2016-01-15 15:22:05 +0000142 reportBug(getArgumentValueString(CE, C), C);
143}
Jordan Rose13937b12012-08-10 22:26:29 +0000144
Artem Dergachev895242f2016-01-15 15:22:05 +0000145void ExprInspectionChecker::analyzerExplain(const CallExpr *CE,
146 CheckerContext &C) const {
147 if (CE->getNumArgs() == 0)
148 reportBug("Missing argument for explaining", C);
149
150 SVal V = C.getSVal(CE->getArg(0));
151 SValExplainer Ex(C.getASTContext());
152 reportBug(Ex.Visit(V), C);
153}
154
155void ExprInspectionChecker::analyzerGetExtent(const CallExpr *CE,
156 CheckerContext &C) const {
157 if (CE->getNumArgs() == 0)
158 reportBug("Missing region for obtaining extent", C);
159
160 auto MR = dyn_cast_or_null<SubRegion>(C.getSVal(CE->getArg(0)).getAsRegion());
161 if (!MR)
162 reportBug("Obtaining extent of a non-region", C);
163
164 ProgramStateRef State = C.getState();
165 State = State->BindExpr(CE, C.getLocationContext(),
166 MR->getExtent(C.getSValBuilder()));
167 C.addTransition(State);
Jordy Rose31ae2592012-05-16 16:01:07 +0000168}
169
Artem Dergachev733e71b2015-12-10 09:28:06 +0000170void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
171 CheckerContext &C) const {
172 if (CE->getNumArgs() == 0)
173 return;
174 SVal Val = C.getSVal(CE->getArg(0));
175 SymbolRef Sym = Val.getAsSymbol();
176 if (!Sym)
177 return;
178
179 ProgramStateRef State = C.getState();
180 State = State->add<MarkedSymbols>(Sym);
181 C.addTransition(State);
182}
183
184void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
185 CheckerContext &C) const {
186 ProgramStateRef State = C.getState();
187 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
188 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
Artem Dergachev895242f2016-01-15 15:22:05 +0000189 SymbolRef Sym = *I;
Artem Dergachev733e71b2015-12-10 09:28:06 +0000190 if (!SymReaper.isDead(Sym))
191 continue;
192
Artem Dergachev895242f2016-01-15 15:22:05 +0000193 reportBug("SYMBOL DEAD", C);
194 State = State->remove<MarkedSymbols>(Sym);
Artem Dergachev733e71b2015-12-10 09:28:06 +0000195 }
Artem Dergachev895242f2016-01-15 15:22:05 +0000196 C.addTransition(State);
Artem Dergachev733e71b2015-12-10 09:28:06 +0000197}
198
Jordan Rosee9c57222013-07-19 00:59:08 +0000199void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
200 CheckerContext &C) const {
201 LLVM_BUILTIN_TRAP;
202}
203
Jordy Rose31ae2592012-05-16 16:01:07 +0000204void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
205 Mgr.registerChecker<ExprInspectionChecker>();
206}
207