Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/UndefBranchChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines UndefBranchChecker, which checks for undefined branch
10
// condition.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/StmtObjC.h"
15
#include "clang/AST/Type.h"
16
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18
#include "clang/StaticAnalyzer/Core/Checker.h"
19
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21
#include <utility>
22
23
using namespace clang;
24
using namespace ento;
25
26
namespace {
27
28
class UndefBranchChecker : public Checker<check::BranchCondition> {
29
  mutable std::unique_ptr<BuiltinBug> BT;
30
31
  struct FindUndefExpr {
32
    ProgramStateRef St;
33
    const LocationContext *LCtx;
34
35
    FindUndefExpr(ProgramStateRef S, const LocationContext *L)
36
27
        : St(std::move(S)), LCtx(L) {}
37
38
72
    const Expr *FindExpr(const Expr *Ex) {
39
72
      if (!MatchesCriteria(Ex))
40
27
        return nullptr;
41
42
45
      for (const Stmt *SubStmt : Ex->children())
43
45
        if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
44
45
          if (const Expr *E2 = FindExpr(ExI))
45
18
            return E2;
46
47
27
      return Ex;
48
45
    }
49
50
72
    bool MatchesCriteria(const Expr *Ex) {
51
72
      return St->getSVal(Ex, LCtx).isUndef();
52
72
    }
53
  };
54
55
public:
56
  void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
57
};
58
59
} // namespace
60
61
void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
62
43.1k
                                              CheckerContext &Ctx) const {
63
  // ObjCForCollection is a loop, but has no actual condition.
64
43.1k
  if (isa<ObjCForCollectionStmt>(Condition))
65
434
    return;
66
42.7k
  SVal X = Ctx.getSVal(Condition);
67
42.7k
  if (X.isUndef()) {
68
    // Generate a sink node, which implicitly marks both outgoing branches as
69
    // infeasible.
70
27
    ExplodedNode *N = Ctx.generateErrorNode();
71
27
    if (N) {
72
27
      if (!BT)
73
7
        BT.reset(new BuiltinBug(
74
7
            this, "Branch condition evaluates to a garbage value"));
75
76
      // What's going on here: we want to highlight the subexpression of the
77
      // condition that is the most likely source of the "uninitialized
78
      // branch condition."  We do a recursive walk of the condition's
79
      // subexpressions and roughly look for the most nested subexpression
80
      // that binds to Undefined.  We then highlight that expression's range.
81
82
      // Get the predecessor node and check if is a PostStmt with the Stmt
83
      // being the terminator condition.  We want to inspect the state
84
      // of that node instead because it will contain main information about
85
      // the subexpressions.
86
87
      // Note: any predecessor will do.  They should have identical state,
88
      // since all the BlockEdge did was act as an error sink since the value
89
      // had to already be undefined.
90
27
      assert (!N->pred_empty());
91
0
      const Expr *Ex = cast<Expr>(Condition);
92
27
      ExplodedNode *PrevN = *N->pred_begin();
93
27
      ProgramPoint P = PrevN->getLocation();
94
27
      ProgramStateRef St = N->getState();
95
96
27
      if (Optional<PostStmt> PS = P.getAs<PostStmt>())
97
27
        if (PS->getStmt() == Ex)
98
27
          St = PrevN->getState();
99
100
27
      FindUndefExpr FindIt(St, Ctx.getLocationContext());
101
27
      Ex = FindIt.FindExpr(Ex);
102
103
      // Emit the bug report.
104
27
      auto R = std::make_unique<PathSensitiveBugReport>(
105
27
          *BT, BT->getDescription(), N);
106
27
      bugreporter::trackExpressionValue(N, Ex, *R);
107
27
      R->addRange(Ex->getSourceRange());
108
109
27
      Ctx.emitReport(std::move(R));
110
27
    }
111
27
  }
112
42.7k
}
113
114
1.15k
void ento::registerUndefBranchChecker(CheckerManager &mgr) {
115
1.15k
  mgr.registerChecker<UndefBranchChecker>();
116
1.15k
}
117
118
2.31k
bool ento::shouldRegisterUndefBranchChecker(const CheckerManager &mgr) {
119
2.31k
  return true;
120
2.31k
}