17330f729Sjoerg //==- UnreachableCodeChecker.cpp - Generalized dead code checker -*- C++ -*-==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg // This file implements a generalized unreachable code checker using a
97330f729Sjoerg // path-sensitive analysis. We mark any path visited, and then walk the CFG as a
107330f729Sjoerg // post-analysis to determine what was never visited.
117330f729Sjoerg //
127330f729Sjoerg // A similar flow-sensitive only check exists in Analysis/ReachableCode.cpp
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg
157330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
167330f729Sjoerg #include "clang/AST/ParentMap.h"
177330f729Sjoerg #include "clang/Basic/Builtins.h"
187330f729Sjoerg #include "clang/Basic/SourceManager.h"
197330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
207330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
217330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
227330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
237330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
247330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
257330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
267330f729Sjoerg #include "llvm/ADT/SmallSet.h"
277330f729Sjoerg
287330f729Sjoerg using namespace clang;
297330f729Sjoerg using namespace ento;
307330f729Sjoerg
317330f729Sjoerg namespace {
327330f729Sjoerg class UnreachableCodeChecker : public Checker<check::EndAnalysis> {
337330f729Sjoerg public:
347330f729Sjoerg void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,
357330f729Sjoerg ExprEngine &Eng) const;
367330f729Sjoerg private:
377330f729Sjoerg typedef llvm::SmallSet<unsigned, 32> CFGBlocksSet;
387330f729Sjoerg
397330f729Sjoerg static inline const Stmt *getUnreachableStmt(const CFGBlock *CB);
407330f729Sjoerg static void FindUnreachableEntryPoints(const CFGBlock *CB,
417330f729Sjoerg CFGBlocksSet &reachable,
427330f729Sjoerg CFGBlocksSet &visited);
437330f729Sjoerg static bool isInvalidPath(const CFGBlock *CB, const ParentMap &PM);
447330f729Sjoerg static inline bool isEmptyCFGBlock(const CFGBlock *CB);
457330f729Sjoerg };
467330f729Sjoerg }
477330f729Sjoerg
checkEndAnalysis(ExplodedGraph & G,BugReporter & B,ExprEngine & Eng) const487330f729Sjoerg void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
497330f729Sjoerg BugReporter &B,
507330f729Sjoerg ExprEngine &Eng) const {
517330f729Sjoerg CFGBlocksSet reachable, visited;
527330f729Sjoerg
537330f729Sjoerg if (Eng.hasWorkRemaining())
547330f729Sjoerg return;
557330f729Sjoerg
567330f729Sjoerg const Decl *D = nullptr;
577330f729Sjoerg CFG *C = nullptr;
587330f729Sjoerg const ParentMap *PM = nullptr;
597330f729Sjoerg const LocationContext *LC = nullptr;
607330f729Sjoerg // Iterate over ExplodedGraph
617330f729Sjoerg for (ExplodedGraph::node_iterator I = G.nodes_begin(), E = G.nodes_end();
627330f729Sjoerg I != E; ++I) {
637330f729Sjoerg const ProgramPoint &P = I->getLocation();
647330f729Sjoerg LC = P.getLocationContext();
657330f729Sjoerg if (!LC->inTopFrame())
667330f729Sjoerg continue;
677330f729Sjoerg
687330f729Sjoerg if (!D)
697330f729Sjoerg D = LC->getAnalysisDeclContext()->getDecl();
707330f729Sjoerg
717330f729Sjoerg // Save the CFG if we don't have it already
727330f729Sjoerg if (!C)
737330f729Sjoerg C = LC->getAnalysisDeclContext()->getUnoptimizedCFG();
747330f729Sjoerg if (!PM)
757330f729Sjoerg PM = &LC->getParentMap();
767330f729Sjoerg
777330f729Sjoerg if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
787330f729Sjoerg const CFGBlock *CB = BE->getBlock();
797330f729Sjoerg reachable.insert(CB->getBlockID());
807330f729Sjoerg }
817330f729Sjoerg }
827330f729Sjoerg
837330f729Sjoerg // Bail out if we didn't get the CFG or the ParentMap.
847330f729Sjoerg if (!D || !C || !PM)
857330f729Sjoerg return;
867330f729Sjoerg
877330f729Sjoerg // Don't do anything for template instantiations. Proving that code
887330f729Sjoerg // in a template instantiation is unreachable means proving that it is
897330f729Sjoerg // unreachable in all instantiations.
907330f729Sjoerg if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
917330f729Sjoerg if (FD->isTemplateInstantiation())
927330f729Sjoerg return;
937330f729Sjoerg
947330f729Sjoerg // Find CFGBlocks that were not covered by any node
957330f729Sjoerg for (CFG::const_iterator I = C->begin(), E = C->end(); I != E; ++I) {
967330f729Sjoerg const CFGBlock *CB = *I;
977330f729Sjoerg // Check if the block is unreachable
987330f729Sjoerg if (reachable.count(CB->getBlockID()))
997330f729Sjoerg continue;
1007330f729Sjoerg
1017330f729Sjoerg // Check if the block is empty (an artificial block)
1027330f729Sjoerg if (isEmptyCFGBlock(CB))
1037330f729Sjoerg continue;
1047330f729Sjoerg
1057330f729Sjoerg // Find the entry points for this block
1067330f729Sjoerg if (!visited.count(CB->getBlockID()))
1077330f729Sjoerg FindUnreachableEntryPoints(CB, reachable, visited);
1087330f729Sjoerg
1097330f729Sjoerg // This block may have been pruned; check if we still want to report it
1107330f729Sjoerg if (reachable.count(CB->getBlockID()))
1117330f729Sjoerg continue;
1127330f729Sjoerg
1137330f729Sjoerg // Check for false positives
1147330f729Sjoerg if (isInvalidPath(CB, *PM))
1157330f729Sjoerg continue;
1167330f729Sjoerg
1177330f729Sjoerg // It is good practice to always have a "default" label in a "switch", even
1187330f729Sjoerg // if we should never get there. It can be used to detect errors, for
1197330f729Sjoerg // instance. Unreachable code directly under a "default" label is therefore
1207330f729Sjoerg // likely to be a false positive.
1217330f729Sjoerg if (const Stmt *label = CB->getLabel())
1227330f729Sjoerg if (label->getStmtClass() == Stmt::DefaultStmtClass)
1237330f729Sjoerg continue;
1247330f729Sjoerg
1257330f729Sjoerg // Special case for __builtin_unreachable.
1267330f729Sjoerg // FIXME: This should be extended to include other unreachable markers,
1277330f729Sjoerg // such as llvm_unreachable.
1287330f729Sjoerg if (!CB->empty()) {
1297330f729Sjoerg bool foundUnreachable = false;
1307330f729Sjoerg for (CFGBlock::const_iterator ci = CB->begin(), ce = CB->end();
1317330f729Sjoerg ci != ce; ++ci) {
1327330f729Sjoerg if (Optional<CFGStmt> S = (*ci).getAs<CFGStmt>())
1337330f729Sjoerg if (const CallExpr *CE = dyn_cast<CallExpr>(S->getStmt())) {
1347330f729Sjoerg if (CE->getBuiltinCallee() == Builtin::BI__builtin_unreachable ||
1357330f729Sjoerg CE->isBuiltinAssumeFalse(Eng.getContext())) {
1367330f729Sjoerg foundUnreachable = true;
1377330f729Sjoerg break;
1387330f729Sjoerg }
1397330f729Sjoerg }
1407330f729Sjoerg }
1417330f729Sjoerg if (foundUnreachable)
1427330f729Sjoerg continue;
1437330f729Sjoerg }
1447330f729Sjoerg
1457330f729Sjoerg // We found a block that wasn't covered - find the statement to report
1467330f729Sjoerg SourceRange SR;
1477330f729Sjoerg PathDiagnosticLocation DL;
1487330f729Sjoerg SourceLocation SL;
1497330f729Sjoerg if (const Stmt *S = getUnreachableStmt(CB)) {
1507330f729Sjoerg // In macros, 'do {...} while (0)' is often used. Don't warn about the
1517330f729Sjoerg // condition 0 when it is unreachable.
1527330f729Sjoerg if (S->getBeginLoc().isMacroID())
1537330f729Sjoerg if (const auto *I = dyn_cast<IntegerLiteral>(S))
1547330f729Sjoerg if (I->getValue() == 0ULL)
1557330f729Sjoerg if (const Stmt *Parent = PM->getParent(S))
1567330f729Sjoerg if (isa<DoStmt>(Parent))
1577330f729Sjoerg continue;
1587330f729Sjoerg SR = S->getSourceRange();
1597330f729Sjoerg DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
1607330f729Sjoerg SL = DL.asLocation();
1617330f729Sjoerg if (SR.isInvalid() || !SL.isValid())
1627330f729Sjoerg continue;
1637330f729Sjoerg }
1647330f729Sjoerg else
1657330f729Sjoerg continue;
1667330f729Sjoerg
1677330f729Sjoerg // Check if the SourceLocation is in a system header
1687330f729Sjoerg const SourceManager &SM = B.getSourceManager();
1697330f729Sjoerg if (SM.isInSystemHeader(SL) || SM.isInExternCSystemHeader(SL))
1707330f729Sjoerg continue;
1717330f729Sjoerg
172*e038c9c4Sjoerg B.EmitBasicReport(D, this, "Unreachable code", categories::UnusedCode,
1737330f729Sjoerg "This statement is never executed", DL, SR);
1747330f729Sjoerg }
1757330f729Sjoerg }
1767330f729Sjoerg
1777330f729Sjoerg // Recursively finds the entry point(s) for this dead CFGBlock.
FindUnreachableEntryPoints(const CFGBlock * CB,CFGBlocksSet & reachable,CFGBlocksSet & visited)1787330f729Sjoerg void UnreachableCodeChecker::FindUnreachableEntryPoints(const CFGBlock *CB,
1797330f729Sjoerg CFGBlocksSet &reachable,
1807330f729Sjoerg CFGBlocksSet &visited) {
1817330f729Sjoerg visited.insert(CB->getBlockID());
1827330f729Sjoerg
1837330f729Sjoerg for (CFGBlock::const_pred_iterator I = CB->pred_begin(), E = CB->pred_end();
1847330f729Sjoerg I != E; ++I) {
1857330f729Sjoerg if (!*I)
1867330f729Sjoerg continue;
1877330f729Sjoerg
1887330f729Sjoerg if (!reachable.count((*I)->getBlockID())) {
1897330f729Sjoerg // If we find an unreachable predecessor, mark this block as reachable so
1907330f729Sjoerg // we don't report this block
1917330f729Sjoerg reachable.insert(CB->getBlockID());
1927330f729Sjoerg if (!visited.count((*I)->getBlockID()))
1937330f729Sjoerg // If we haven't previously visited the unreachable predecessor, recurse
1947330f729Sjoerg FindUnreachableEntryPoints(*I, reachable, visited);
1957330f729Sjoerg }
1967330f729Sjoerg }
1977330f729Sjoerg }
1987330f729Sjoerg
1997330f729Sjoerg // Find the Stmt* in a CFGBlock for reporting a warning
getUnreachableStmt(const CFGBlock * CB)2007330f729Sjoerg const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) {
2017330f729Sjoerg for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) {
2027330f729Sjoerg if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) {
2037330f729Sjoerg if (!isa<DeclStmt>(S->getStmt()))
2047330f729Sjoerg return S->getStmt();
2057330f729Sjoerg }
2067330f729Sjoerg }
2077330f729Sjoerg if (const Stmt *S = CB->getTerminatorStmt())
2087330f729Sjoerg return S;
2097330f729Sjoerg else
2107330f729Sjoerg return nullptr;
2117330f729Sjoerg }
2127330f729Sjoerg
2137330f729Sjoerg // Determines if the path to this CFGBlock contained an element that infers this
2147330f729Sjoerg // block is a false positive. We assume that FindUnreachableEntryPoints has
2157330f729Sjoerg // already marked only the entry points to any dead code, so we need only to
2167330f729Sjoerg // find the condition that led to this block (the predecessor of this block.)
2177330f729Sjoerg // There will never be more than one predecessor.
isInvalidPath(const CFGBlock * CB,const ParentMap & PM)2187330f729Sjoerg bool UnreachableCodeChecker::isInvalidPath(const CFGBlock *CB,
2197330f729Sjoerg const ParentMap &PM) {
2207330f729Sjoerg // We only expect a predecessor size of 0 or 1. If it is >1, then an external
2217330f729Sjoerg // condition has broken our assumption (for example, a sink being placed by
2227330f729Sjoerg // another check). In these cases, we choose not to report.
2237330f729Sjoerg if (CB->pred_size() > 1)
2247330f729Sjoerg return true;
2257330f729Sjoerg
2267330f729Sjoerg // If there are no predecessors, then this block is trivially unreachable
2277330f729Sjoerg if (CB->pred_size() == 0)
2287330f729Sjoerg return false;
2297330f729Sjoerg
2307330f729Sjoerg const CFGBlock *pred = *CB->pred_begin();
2317330f729Sjoerg if (!pred)
2327330f729Sjoerg return false;
2337330f729Sjoerg
2347330f729Sjoerg // Get the predecessor block's terminator condition
2357330f729Sjoerg const Stmt *cond = pred->getTerminatorCondition();
2367330f729Sjoerg
2377330f729Sjoerg //assert(cond && "CFGBlock's predecessor has a terminator condition");
2387330f729Sjoerg // The previous assertion is invalid in some cases (eg do/while). Leaving
2397330f729Sjoerg // reporting of these situations on at the moment to help triage these cases.
2407330f729Sjoerg if (!cond)
2417330f729Sjoerg return false;
2427330f729Sjoerg
2437330f729Sjoerg // Run each of the checks on the conditions
2447330f729Sjoerg return containsMacro(cond) || containsEnum(cond) ||
2457330f729Sjoerg containsStaticLocal(cond) || containsBuiltinOffsetOf(cond) ||
2467330f729Sjoerg containsStmt<UnaryExprOrTypeTraitExpr>(cond);
2477330f729Sjoerg }
2487330f729Sjoerg
2497330f729Sjoerg // Returns true if the given CFGBlock is empty
isEmptyCFGBlock(const CFGBlock * CB)2507330f729Sjoerg bool UnreachableCodeChecker::isEmptyCFGBlock(const CFGBlock *CB) {
2517330f729Sjoerg return CB->getLabel() == nullptr // No labels
2527330f729Sjoerg && CB->size() == 0 // No statements
2537330f729Sjoerg && !CB->getTerminatorStmt(); // No terminator
2547330f729Sjoerg }
2557330f729Sjoerg
registerUnreachableCodeChecker(CheckerManager & mgr)2567330f729Sjoerg void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
2577330f729Sjoerg mgr.registerChecker<UnreachableCodeChecker>();
2587330f729Sjoerg }
2597330f729Sjoerg
shouldRegisterUnreachableCodeChecker(const CheckerManager & mgr)260*e038c9c4Sjoerg bool ento::shouldRegisterUnreachableCodeChecker(const CheckerManager &mgr) {
2617330f729Sjoerg return true;
2627330f729Sjoerg }
263