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