Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/UndefBranchChecker.cpp
Line
Count
Source
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/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16
#include "clang/StaticAnalyzer/Core/Checker.h"
17
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19
#include <utility>
20
21
using namespace clang;
22
using namespace ento;
23
24
namespace {
25
26
class UndefBranchChecker : public Checker<check::BranchCondition> {
27
  mutable std::unique_ptr<BuiltinBug> BT;
28
29
  struct FindUndefExpr {
30
    ProgramStateRef St;
31
    const LocationContext *LCtx;
32
33
    FindUndefExpr(ProgramStateRef S, const LocationContext *L)
34
26
        : St(std::move(S)), LCtx(L) {}
35
36
70
    const Expr *FindExpr(const Expr *Ex) {
37
70
      if (!MatchesCriteria(Ex))
38
26
        return nullptr;
39
44
40
44
      for (const Stmt *SubStmt : Ex->children())
41
44
        if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
42
44
          if (const Expr *E2 = FindExpr(ExI))
43
18
            return E2;
44
44
45
44
      
return Ex26
;
46
44
    }
47
48
70
    bool MatchesCriteria(const Expr *Ex) {
49
70
      return St->getSVal(Ex, LCtx).isUndef();
50
70
    }
51
  };
52
53
public:
54
  void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
55
};
56
57
}
58
59
void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
60
40.2k
                                              CheckerContext &Ctx) const {
61
40.2k
  SVal X = Ctx.getSVal(Condition);
62
40.2k
  if (X.isUndef()) {
63
26
    // Generate a sink node, which implicitly marks both outgoing branches as
64
26
    // infeasible.
65
26
    ExplodedNode *N = Ctx.generateErrorNode();
66
26
    if (N) {
67
26
      if (!BT)
68
6
        BT.reset(new BuiltinBug(
69
6
            this, "Branch condition evaluates to a garbage value"));
70
26
71
26
      // What's going on here: we want to highlight the subexpression of the
72
26
      // condition that is the most likely source of the "uninitialized
73
26
      // branch condition."  We do a recursive walk of the condition's
74
26
      // subexpressions and roughly look for the most nested subexpression
75
26
      // that binds to Undefined.  We then highlight that expression's range.
76
26
77
26
      // Get the predecessor node and check if is a PostStmt with the Stmt
78
26
      // being the terminator condition.  We want to inspect the state
79
26
      // of that node instead because it will contain main information about
80
26
      // the subexpressions.
81
26
82
26
      // Note: any predecessor will do.  They should have identical state,
83
26
      // since all the BlockEdge did was act as an error sink since the value
84
26
      // had to already be undefined.
85
26
      assert (!N->pred_empty());
86
26
      const Expr *Ex = cast<Expr>(Condition);
87
26
      ExplodedNode *PrevN = *N->pred_begin();
88
26
      ProgramPoint P = PrevN->getLocation();
89
26
      ProgramStateRef St = N->getState();
90
26
91
26
      if (Optional<PostStmt> PS = P.getAs<PostStmt>())
92
26
        if (PS->getStmt() == Ex)
93
26
          St = PrevN->getState();
94
26
95
26
      FindUndefExpr FindIt(St, Ctx.getLocationContext());
96
26
      Ex = FindIt.FindExpr(Ex);
97
26
98
26
      // Emit the bug report.
99
26
      auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
100
26
      bugreporter::trackExpressionValue(N, Ex, *R);
101
26
      R->addRange(Ex->getSourceRange());
102
26
103
26
      Ctx.emitReport(std::move(R));
104
26
    }
105
26
  }
106
40.2k
}
107
108
675
void ento::registerUndefBranchChecker(CheckerManager &mgr) {
109
675
  mgr.registerChecker<UndefBranchChecker>();
110
675
}
111
112
676
bool ento::shouldRegisterUndefBranchChecker(const LangOptions &LO) {
113
676
  return true;
114
676
}