1*67e74705SXin Li //==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li // This file implements a generalized unreachable code checker using a
10*67e74705SXin Li // path-sensitive analysis. We mark any path visited, and then walk the CFG as a
11*67e74705SXin Li // post-analysis to determine what was never visited.
12*67e74705SXin Li //
13*67e74705SXin Li // A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
14*67e74705SXin Li //===----------------------------------------------------------------------===//
15*67e74705SXin Li
16*67e74705SXin Li #include "ClangSACheckers.h"
17*67e74705SXin Li #include "clang/AST/ParentMap.h"
18*67e74705SXin Li #include "clang/Basic/Builtins.h"
19*67e74705SXin Li #include "clang/Basic/SourceManager.h"
20*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
22*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
23*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
24*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
25*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
26*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
27*67e74705SXin Li #include "llvm/ADT/SmallSet.h"
28*67e74705SXin Li
29*67e74705SXin Li using namespace clang;
30*67e74705SXin Li using namespace ento;
31*67e74705SXin Li
32*67e74705SXin Li namespace {
33*67e74705SXin Li class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
34*67e74705SXin Li public:
35*67e74705SXin Li void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
36*67e74705SXin Li ExprEngine &Eng) const;
37*67e74705SXin Li private:
38*67e74705SXin Li typedef llvm::SmallSet<unsigned, 32> CFGBlocksSet;
39*67e74705SXin Li
40*67e74705SXin Li static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
41*67e74705SXin Li static void FindUnreachableEntryPoints(const CFGBlock *CB,
42*67e74705SXin Li CFGBlocksSet &reachable,
43*67e74705SXin Li CFGBlocksSet &visited);
44*67e74705SXin Li static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
45*67e74705SXin Li static inline bool isEmptyCFGBlock(const CFGBlock *CB);
46*67e74705SXin Li };
47*67e74705SXin Li }
48*67e74705SXin Li
checkEndAnalysis(ExplodedGraph & G,BugReporter & B,ExprEngine & Eng) const49*67e74705SXin Li void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
50*67e74705SXin Li BugReporter &B,
51*67e74705SXin Li ExprEngine &Eng) const {
52*67e74705SXin Li CFGBlocksSet reachable, visited;
53*67e74705SXin Li
54*67e74705SXin Li if (Eng.hasWorkRemaining())
55*67e74705SXin Li return;
56*67e74705SXin Li
57*67e74705SXin Li const Decl *D = nullptr;
58*67e74705SXin Li CFG *C = nullptr;
59*67e74705SXin Li ParentMap *PM = nullptr;
60*67e74705SXin Li const LocationContext *LC = nullptr;
61*67e74705SXin Li // Iterate over ExplodedGraph
62*67e74705SXin Li for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
63*67e74705SXin Li I != E; ++I) {
64*67e74705SXin Li const ProgramPoint &P = I->getLocation();
65*67e74705SXin Li LC = P.getLocationContext();
66*67e74705SXin Li if (!LC->inTopFrame())
67*67e74705SXin Li continue;
68*67e74705SXin Li
69*67e74705SXin Li if (!D)
70*67e74705SXin Li D = LC->getAnalysisDeclContext()->getDecl();
71*67e74705SXin Li
72*67e74705SXin Li // Save the CFG if we don't have it already
73*67e74705SXin Li if (!C)
74*67e74705SXin Li C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
75*67e74705SXin Li if (!PM)
76*67e74705SXin Li PM = &LC->getParentMap();
77*67e74705SXin Li
78*67e74705SXin Li if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
79*67e74705SXin Li const CFGBlock *CB = BE->getBlock();
80*67e74705SXin Li reachable.insert(CB->getBlockID());
81*67e74705SXin Li }
82*67e74705SXin Li }
83*67e74705SXin Li
84*67e74705SXin Li // Bail out if we didn't get the CFG or the ParentMap.
85*67e74705SXin Li if (!D || !C || !PM)
86*67e74705SXin Li return;
87*67e74705SXin Li
88*67e74705SXin Li // Don't do anything for template instantiations. Proving that code
89*67e74705SXin Li // in a template instantiation is unreachable means proving that it is
90*67e74705SXin Li // unreachable in all instantiations.
91*67e74705SXin Li if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
92*67e74705SXin Li if (FD->isTemplateInstantiation())
93*67e74705SXin Li return;
94*67e74705SXin Li
95*67e74705SXin Li // Find CFGBlocks that were not covered by any node
96*67e74705SXin Li for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
97*67e74705SXin Li const CFGBlock *CB = *I;
98*67e74705SXin Li // Check if the block is unreachable
99*67e74705SXin Li if (reachable.count(CB->getBlockID()))
100*67e74705SXin Li continue;
101*67e74705SXin Li
102*67e74705SXin Li // Check if the block is empty (an artificial block)
103*67e74705SXin Li if (isEmptyCFGBlock(CB))
104*67e74705SXin Li continue;
105*67e74705SXin Li
106*67e74705SXin Li // Find the entry points for this block
107*67e74705SXin Li if (!visited.count(CB->getBlockID()))
108*67e74705SXin Li FindUnreachableEntryPoints(CB, reachable, visited);
109*67e74705SXin Li
110*67e74705SXin Li // This block may have been pruned; check if we still want to report it
111*67e74705SXin Li if (reachable.count(CB->getBlockID()))
112*67e74705SXin Li continue;
113*67e74705SXin Li
114*67e74705SXin Li // Check for false positives
115*67e74705SXin Li if (CB->size() > 0 && isInvalidPath(CB, *PM))
116*67e74705SXin Li continue;
117*67e74705SXin Li
118*67e74705SXin Li // It is good practice to always have a "default" label in a "switch", even
119*67e74705SXin Li // if we should never get there. It can be used to detect errors, for
120*67e74705SXin Li // instance. Unreachable code directly under a "default" label is therefore
121*67e74705SXin Li // likely to be a false positive.
122*67e74705SXin Li if (const Stmt *label = CB->getLabel())
123*67e74705SXin Li if (label->getStmtClass() == Stmt::DefaultStmtClass)
124*67e74705SXin Li continue;
125*67e74705SXin Li
126*67e74705SXin Li // Special case for __builtin_unreachable.
127*67e74705SXin Li // FIXME: This should be extended to include other unreachable markers,
128*67e74705SXin Li // such as llvm_unreachable.
129*67e74705SXin Li if (!CB->empty()) {
130*67e74705SXin Li bool foundUnreachable = false;
131*67e74705SXin Li for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
132*67e74705SXin Li ci != ce; ++ci) {
133*67e74705SXin Li if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>())
134*67e74705SXin Li if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
135*67e74705SXin Li if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable) {
136*67e74705SXin Li foundUnreachable = true;
137*67e74705SXin Li break;
138*67e74705SXin Li }
139*67e74705SXin Li }
140*67e74705SXin Li }
141*67e74705SXin Li if (foundUnreachable)
142*67e74705SXin Li continue;
143*67e74705SXin Li }
144*67e74705SXin Li
145*67e74705SXin Li // We found a block that wasn't covered - find the statement to report
146*67e74705SXin Li SourceRange SR;
147*67e74705SXin Li PathDiagnosticLocation DL;
148*67e74705SXin Li SourceLocation SL;
149*67e74705SXin Li if (const Stmt *S = getUnreachableStmt(CB)) {
150*67e74705SXin Li SR = S->getSourceRange();
151*67e74705SXin Li DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
152*67e74705SXin Li SL = DL.asLocation();
153*67e74705SXin Li if (SR.isInvalid() || !SL.isValid())
154*67e74705SXin Li continue;
155*67e74705SXin Li }
156*67e74705SXin Li else
157*67e74705SXin Li continue;
158*67e74705SXin Li
159*67e74705SXin Li // Check if the SourceLocation is in a system header
160*67e74705SXin Li const SourceManager &SM = B.getSourceManager();
161*67e74705SXin Li if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
162*67e74705SXin Li continue;
163*67e74705SXin Li
164*67e74705SXin Li B.EmitBasicReport(D, this, "Unreachable code", "Dead code",
165*67e74705SXin Li "This statement is never executed", DL, SR);
166*67e74705SXin Li }
167*67e74705SXin Li }
168*67e74705SXin Li
169*67e74705SXin Li // Recursively finds the entry point(s) for this dead CFGBlock.
FindUnreachableEntryPoints(const CFGBlock * CB,CFGBlocksSet & reachable,CFGBlocksSet & visited)170*67e74705SXin Li void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
171*67e74705SXin Li CFGBlocksSet &reachable,
172*67e74705SXin Li CFGBlocksSet &visited) {
173*67e74705SXin Li visited.insert(CB->getBlockID());
174*67e74705SXin Li
175*67e74705SXin Li for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
176*67e74705SXin Li I != E; ++I) {
177*67e74705SXin Li if (!*I)
178*67e74705SXin Li continue;
179*67e74705SXin Li
180*67e74705SXin Li if (!reachable.count((*I)->getBlockID())) {
181*67e74705SXin Li // If we find an unreachable predecessor, mark this block as reachable so
182*67e74705SXin Li // we don't report this block
183*67e74705SXin Li reachable.insert(CB->getBlockID());
184*67e74705SXin Li if (!visited.count((*I)->getBlockID()))
185*67e74705SXin Li // If we haven't previously visited the unreachable predecessor, recurse
186*67e74705SXin Li FindUnreachableEntryPoints(*I, reachable, visited);
187*67e74705SXin Li }
188*67e74705SXin Li }
189*67e74705SXin Li }
190*67e74705SXin Li
191*67e74705SXin Li // Find the Stmt* in a CFGBlock for reporting a warning
getUnreachableStmt(const CFGBlock * CB)192*67e74705SXin Li const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
193*67e74705SXin Li for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
194*67e74705SXin Li if (Optional<CFGStmt> S = I->getAs<CFGStmt>())
195*67e74705SXin Li return S->getStmt();
196*67e74705SXin Li }
197*67e74705SXin Li if (const Stmt *S = CB->getTerminator())
198*67e74705SXin Li return S;
199*67e74705SXin Li else
200*67e74705SXin Li return nullptr;
201*67e74705SXin Li }
202*67e74705SXin Li
203*67e74705SXin Li // Determines if the path to this CFGBlock contained an element that infers this
204*67e74705SXin Li // block is a false positive. We assume that FindUnreachableEntryPoints has
205*67e74705SXin Li // already marked only the entry points to any dead code, so we need only to
206*67e74705SXin Li // find the condition that led to this block (the predecessor of this block.)
207*67e74705SXin Li // There will never be more than one predecessor.
isInvalidPath(const CFGBlock * CB,const ParentMap & PM)208*67e74705SXin Li bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
209*67e74705SXin Li const ParentMap &PM) {
210*67e74705SXin Li // We only expect a predecessor size of 0 or 1. If it is >1, then an external
211*67e74705SXin Li // condition has broken our assumption (for example, a sink being placed by
212*67e74705SXin Li // another check). In these cases, we choose not to report.
213*67e74705SXin Li if (CB->pred_size() > 1)
214*67e74705SXin Li return true;
215*67e74705SXin Li
216*67e74705SXin Li // If there are no predecessors, then this block is trivially unreachable
217*67e74705SXin Li if (CB->pred_size() == 0)
218*67e74705SXin Li return false;
219*67e74705SXin Li
220*67e74705SXin Li const CFGBlock *pred = *CB->pred_begin();
221*67e74705SXin Li if (!pred)
222*67e74705SXin Li return false;
223*67e74705SXin Li
224*67e74705SXin Li // Get the predecessor block's terminator conditon
225*67e74705SXin Li const Stmt *cond = pred->getTerminatorCondition();
226*67e74705SXin Li
227*67e74705SXin Li //assert(cond && "CFGBlock's predecessor has a terminator condition");
228*67e74705SXin Li // The previous assertion is invalid in some cases (eg do/while). Leaving
229*67e74705SXin Li // reporting of these situations on at the moment to help triage these cases.
230*67e74705SXin Li if (!cond)
231*67e74705SXin Li return false;
232*67e74705SXin Li
233*67e74705SXin Li // Run each of the checks on the conditions
234*67e74705SXin Li return containsMacro(cond) || containsEnum(cond) ||
235*67e74705SXin Li containsStaticLocal(cond) || containsBuiltinOffsetOf(cond) ||
236*67e74705SXin Li containsStmt<UnaryExprOrTypeTraitExpr>(cond);
237*67e74705SXin Li }
238*67e74705SXin Li
239*67e74705SXin Li // Returns true if the given CFGBlock is empty
isEmptyCFGBlock(const CFGBlock * CB)240*67e74705SXin Li bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
241*67e74705SXin Li return CB->getLabel() == nullptr // No labels
242*67e74705SXin Li && CB->size() == 0 // No statements
243*67e74705SXin Li && !CB->getTerminator(); // No terminator
244*67e74705SXin Li }
245*67e74705SXin Li
registerUnreachableCodeChecker(CheckerManager & mgr)246*67e74705SXin Li void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
247*67e74705SXin Li mgr.registerChecker<UnreachableCodeChecker>();
248*67e74705SXin Li }
249