blob: 8f6c20ab190612f589b42db3dc97bb5df9241264 [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"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000014#include "llvm/ADT/StringSwitch.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000015
16using namespace clang;
17using namespace ento;
18
19namespace {
Artem Dergachev733e71b2015-12-10 09:28:06 +000020class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols> {
Ahmed Charlesb8984322014-03-07 20:03:18 +000021 mutable std::unique_ptr<BugType> BT;
Jordan Rose13937b12012-08-10 22:26:29 +000022
23 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
24 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose9db2d9a2013-10-03 16:57:03 +000025 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosee9c57222013-07-19 00:59:08 +000026 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000027 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose13937b12012-08-10 22:26:29 +000028
29 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
30 CheckerContext &C) const;
31
Jordy Rose31ae2592012-05-16 16:01:07 +000032public:
33 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
Artem Dergachev733e71b2015-12-10 09:28:06 +000034 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Jordy Rose31ae2592012-05-16 16:01:07 +000035};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000036}
Jordy Rose31ae2592012-05-16 16:01:07 +000037
Artem Dergachev733e71b2015-12-10 09:28:06 +000038REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, const void *)
39
Jordy Rose31ae2592012-05-16 16:01:07 +000040bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rose13937b12012-08-10 22:26:29 +000041 CheckerContext &C) const {
Jordy Rose31ae2592012-05-16 16:01:07 +000042 // These checks should have no effect on the surrounding environment
Jordan Rose13937b12012-08-10 22:26:29 +000043 // (globals should not be invalidated, etc), hence the use of evalCall.
44 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
45 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
46 .Case("clang_analyzer_checkInlined",
47 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Rosee9c57222013-07-19 00:59:08 +000048 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Artem Dergachev733e71b2015-12-10 09:28:06 +000049 .Case("clang_analyzer_warnIfReached",
50 &ExprInspectionChecker::analyzerWarnIfReached)
51 .Case("clang_analyzer_warnOnDeadSymbol",
52 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
Craig Topper0dbb7832014-05-27 02:45:47 +000053 .Default(nullptr);
Jordan Rose13937b12012-08-10 22:26:29 +000054
55 if (!Handler)
56 return false;
57
58 (this->*Handler)(CE, C);
59 return true;
60}
61
62static const char *getArgumentValueString(const CallExpr *CE,
63 CheckerContext &C) {
64 if (CE->getNumArgs() == 0)
65 return "Missing assertion argument";
66
67 ExplodedNode *N = C.getPredecessor();
68 const LocationContext *LC = N->getLocationContext();
69 ProgramStateRef State = N->getState();
70
71 const Expr *Assertion = CE->getArg(0);
72 SVal AssertionVal = State->getSVal(Assertion, LC);
73
74 if (AssertionVal.isUndef())
75 return "UNDEFINED";
76
77 ProgramStateRef StTrue, StFalse;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +000078 std::tie(StTrue, StFalse) =
David Blaikie2fdacbc2013-02-20 05:52:05 +000079 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rose13937b12012-08-10 22:26:29 +000080
81 if (StTrue) {
82 if (StFalse)
83 return "UNKNOWN";
84 else
85 return "TRUE";
86 } else {
87 if (StFalse)
88 return "FALSE";
89 else
90 llvm_unreachable("Invalid constraint; neither true or false.");
91 }
92}
93
94void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
95 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +000096 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordy Rose31ae2592012-05-16 16:01:07 +000097
Jordy Rose31ae2592012-05-16 16:01:07 +000098 // A specific instantiation of an inlined function may have more constrained
99 // values than can generally be assumed. Skip the check.
Craig Topper0dbb7832014-05-27 02:45:47 +0000100 if (LC->getCurrentStackFrame()->getParent() != nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000101 return;
Jordy Rose31ae2592012-05-16 16:01:07 +0000102
103 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000104 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordy Rose31ae2592012-05-16 16:01:07 +0000105
Devin Coughline39bd402015-09-16 22:03:05 +0000106 ExplodedNode *N = C.generateNonFatalErrorNode();
107 if (!N)
108 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000109 C.emitReport(
110 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
Jordan Rose13937b12012-08-10 22:26:29 +0000111}
Jordy Rose31ae2592012-05-16 16:01:07 +0000112
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000113void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
114 CheckerContext &C) const {
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000115
116 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000117 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000118
Devin Coughline39bd402015-09-16 22:03:05 +0000119 ExplodedNode *N = C.generateNonFatalErrorNode();
120 if (!N)
121 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000122 C.emitReport(llvm::make_unique<BugReport>(*BT, "REACHABLE", N));
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000123}
124
Jordan Rose13937b12012-08-10 22:26:29 +0000125void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
126 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000127 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordan Rose13937b12012-08-10 22:26:29 +0000128
129 // An inlined function could conceivably also be analyzed as a top-level
130 // function. We ignore this case and only emit a message (TRUE or FALSE)
131 // when we are analyzing it as an inlined function. This means that
132 // clang_analyzer_checkInlined(true) should always print TRUE, but
133 // clang_analyzer_checkInlined(false) should never actually print anything.
Craig Topper0dbb7832014-05-27 02:45:47 +0000134 if (LC->getCurrentStackFrame()->getParent() == nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000135 return;
136
137 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000138 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordan Rose13937b12012-08-10 22:26:29 +0000139
Devin Coughline39bd402015-09-16 22:03:05 +0000140 ExplodedNode *N = C.generateNonFatalErrorNode();
141 if (!N)
142 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000143 C.emitReport(
144 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
Jordy Rose31ae2592012-05-16 16:01:07 +0000145}
146
Artem Dergachev733e71b2015-12-10 09:28:06 +0000147void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
148 CheckerContext &C) const {
149 if (CE->getNumArgs() == 0)
150 return;
151 SVal Val = C.getSVal(CE->getArg(0));
152 SymbolRef Sym = Val.getAsSymbol();
153 if (!Sym)
154 return;
155
156 ProgramStateRef State = C.getState();
157 State = State->add<MarkedSymbols>(Sym);
158 C.addTransition(State);
159}
160
161void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
162 CheckerContext &C) const {
163 ProgramStateRef State = C.getState();
164 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
165 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
166 SymbolRef Sym = static_cast<SymbolRef>(*I);
167 if (!SymReaper.isDead(Sym))
168 continue;
169
170 if (!BT)
171 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
172
173 ExplodedNode *N = C.generateNonFatalErrorNode();
174 if (!N)
175 return;
176
177 C.emitReport(llvm::make_unique<BugReport>(*BT, "SYMBOL DEAD", N));
178 C.addTransition(State->remove<MarkedSymbols>(Sym), N);
179 }
180}
181
Jordan Rosee9c57222013-07-19 00:59:08 +0000182void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
183 CheckerContext &C) const {
184 LLVM_BUILTIN_TRAP;
185}
186
Jordy Rose31ae2592012-05-16 16:01:07 +0000187void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
188 Mgr.registerChecker<ExprInspectionChecker>();
189}
190