1d99bd55aSTed Kremenek //=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
2d99bd55aSTed Kremenek //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d99bd55aSTed Kremenek //
7d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
8d99bd55aSTed Kremenek //
9d99bd55aSTed Kremenek // This file defines UndefBranchChecker, which checks for undefined branch
10d99bd55aSTed Kremenek // condition.
11d99bd55aSTed Kremenek //
12d99bd55aSTed Kremenek //===----------------------------------------------------------------------===//
13d99bd55aSTed Kremenek
14*b9bca883SKristóf Umann #include "clang/AST/StmtObjC.h"
15*b9bca883SKristóf Umann #include "clang/AST/Type.h"
1676a21502SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
173a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
186a5674ffSArgyrios Kyrtzidis #include "clang/StaticAnalyzer/Core/Checker.h"
19753b3ca3SArgyrios Kyrtzidis #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20753b3ca3SArgyrios Kyrtzidis #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21cfeacf56SBenjamin Kramer #include <utility>
22d99bd55aSTed Kremenek
23d99bd55aSTed Kremenek using namespace clang;
24d99bd55aSTed Kremenek using namespace ento;
25d99bd55aSTed Kremenek
26d99bd55aSTed Kremenek namespace {
27d99bd55aSTed Kremenek
286a5674ffSArgyrios Kyrtzidis class UndefBranchChecker : public Checker<check::BranchCondition> {
29b8984329SAhmed Charles mutable std::unique_ptr<BuiltinBug> BT;
30d99bd55aSTed Kremenek
31d99bd55aSTed Kremenek struct FindUndefExpr {
3249b1e38eSTed Kremenek ProgramStateRef St;
33632e3b7eSTed Kremenek const LocationContext *LCtx;
34d99bd55aSTed Kremenek
FindUndefExpr__anon1a2a7ff30111::UndefBranchChecker::FindUndefExpr3549b1e38eSTed Kremenek FindUndefExpr(ProgramStateRef S, const LocationContext *L)
36cfeacf56SBenjamin Kramer : St(std::move(S)), LCtx(L) {}
37d99bd55aSTed Kremenek
FindExpr__anon1a2a7ff30111::UndefBranchChecker::FindUndefExpr38d99bd55aSTed Kremenek const Expr *FindExpr(const Expr *Ex) {
39d99bd55aSTed Kremenek if (!MatchesCriteria(Ex))
400dbb783cSCraig Topper return nullptr;
41d99bd55aSTed Kremenek
42973431b2SBenjamin Kramer for (const Stmt *SubStmt : Ex->children())
43973431b2SBenjamin Kramer if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
44973431b2SBenjamin Kramer if (const Expr *E2 = FindExpr(ExI))
45973431b2SBenjamin Kramer return E2;
46d99bd55aSTed Kremenek
47d99bd55aSTed Kremenek return Ex;
48d99bd55aSTed Kremenek }
49d99bd55aSTed Kremenek
MatchesCriteria__anon1a2a7ff30111::UndefBranchChecker::FindUndefExpr50632e3b7eSTed Kremenek bool MatchesCriteria(const Expr *Ex) {
51632e3b7eSTed Kremenek return St->getSVal(Ex, LCtx).isUndef();
52632e3b7eSTed Kremenek }
53d99bd55aSTed Kremenek };
54d99bd55aSTed Kremenek
55d99bd55aSTed Kremenek public:
56f380534aSAnna Zaks void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
57d99bd55aSTed Kremenek };
58d99bd55aSTed Kremenek
59*b9bca883SKristóf Umann } // namespace
60d99bd55aSTed Kremenek
checkBranchCondition(const Stmt * Condition,CheckerContext & Ctx) const61753b3ca3SArgyrios Kyrtzidis void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
62f380534aSAnna Zaks CheckerContext &Ctx) const {
63*b9bca883SKristóf Umann // ObjCForCollection is a loop, but has no actual condition.
64*b9bca883SKristóf Umann if (isa<ObjCForCollectionStmt>(Condition))
65*b9bca883SKristóf Umann return;
66d703ec94SGeorge Karpenkov SVal X = Ctx.getSVal(Condition);
67d99bd55aSTed Kremenek if (X.isUndef()) {
686d285c58SAnna Zaks // Generate a sink node, which implicitly marks both outgoing branches as
696d285c58SAnna Zaks // infeasible.
70e39bd407SDevin Coughlin ExplodedNode *N = Ctx.generateErrorNode();
71d99bd55aSTed Kremenek if (N) {
72d99bd55aSTed Kremenek if (!BT)
734aca9b1cSAlexander Kornienko BT.reset(new BuiltinBug(
744aca9b1cSAlexander Kornienko this, "Branch condition evaluates to a garbage value"));
75d99bd55aSTed Kremenek
76d99bd55aSTed Kremenek // What's going on here: we want to highlight the subexpression of the
77d99bd55aSTed Kremenek // condition that is the most likely source of the "uninitialized
78d99bd55aSTed Kremenek // branch condition." We do a recursive walk of the condition's
79d99bd55aSTed Kremenek // subexpressions and roughly look for the most nested subexpression
80d99bd55aSTed Kremenek // that binds to Undefined. We then highlight that expression's range.
81d99bd55aSTed Kremenek
82d99bd55aSTed Kremenek // Get the predecessor node and check if is a PostStmt with the Stmt
83d99bd55aSTed Kremenek // being the terminator condition. We want to inspect the state
84d99bd55aSTed Kremenek // of that node instead because it will contain main information about
85d99bd55aSTed Kremenek // the subexpressions.
86d99bd55aSTed Kremenek
87d99bd55aSTed Kremenek // Note: any predecessor will do. They should have identical state,
88d99bd55aSTed Kremenek // since all the BlockEdge did was act as an error sink since the value
89d99bd55aSTed Kremenek // had to already be undefined.
90c42197d0SAnna Zaks assert (!N->pred_empty());
91c42197d0SAnna Zaks const Expr *Ex = cast<Expr>(Condition);
92d99bd55aSTed Kremenek ExplodedNode *PrevN = *N->pred_begin();
93d99bd55aSTed Kremenek ProgramPoint P = PrevN->getLocation();
9449b1e38eSTed Kremenek ProgramStateRef St = N->getState();
95d99bd55aSTed Kremenek
9687396b9bSDavid Blaikie if (Optional<PostStmt> PS = P.getAs<PostStmt>())
97d99bd55aSTed Kremenek if (PS->getStmt() == Ex)
98d99bd55aSTed Kremenek St = PrevN->getState();
99d99bd55aSTed Kremenek
100632e3b7eSTed Kremenek FindUndefExpr FindIt(St, Ctx.getLocationContext());
101d99bd55aSTed Kremenek Ex = FindIt.FindExpr(Ex);
102d99bd55aSTed Kremenek
103d99bd55aSTed Kremenek // Emit the bug report.
1042f169e7cSArtem Dergachev auto R = std::make_unique<PathSensitiveBugReport>(
1052f169e7cSArtem Dergachev *BT, BT->getDescription(), N);
106b2cf0063SGeorge Karpenkov bugreporter::trackExpressionValue(N, Ex, *R);
107d99bd55aSTed Kremenek R->addRange(Ex->getSourceRange());
108d99bd55aSTed Kremenek
1098d3a7a56SAaron Ballman Ctx.emitReport(std::move(R));
110d99bd55aSTed Kremenek }
111d99bd55aSTed Kremenek }
112d99bd55aSTed Kremenek }
113753b3ca3SArgyrios Kyrtzidis
registerUndefBranchChecker(CheckerManager & mgr)114753b3ca3SArgyrios Kyrtzidis void ento::registerUndefBranchChecker(CheckerManager &mgr) {
115753b3ca3SArgyrios Kyrtzidis mgr.registerChecker<UndefBranchChecker>();
116753b3ca3SArgyrios Kyrtzidis }
117058a7a45SKristof Umann
shouldRegisterUndefBranchChecker(const CheckerManager & mgr)118bda3dd0dSKirstóf Umann bool ento::shouldRegisterUndefBranchChecker(const CheckerManager &mgr) {
119058a7a45SKristof Umann return true;
120058a7a45SKristof Umann }
121