Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/UndefCapturedBlockVarChecker.cpp
Line
Count
Source
1
// UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- 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 checker detects blocks that capture uninitialized values.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14
#include "clang/AST/Attr.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 "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20
#include "llvm/ADT/SmallString.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
using namespace clang;
24
using namespace ento;
25
26
namespace {
27
class UndefCapturedBlockVarChecker
28
  : public Checker< check::PostStmt<BlockExpr> > {
29
  mutable std::unique_ptr<BugType> BT;
30
31
public:
32
  void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
33
};
34
} // end anonymous namespace
35
36
static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
37
30
                                               const VarDecl *VD) {
38
30
  if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
39
10
    if (BR->getDecl() == VD)
40
6
      return BR;
41
24
42
24
  for (const Stmt *Child : S->children())
43
24
    if (Child)
44
24
      if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
45
20
        return BR;
46
24
47
24
  
return nullptr4
;
48
24
}
49
50
void
51
UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
52
363
                                            CheckerContext &C) const {
53
363
  if (!BE->getBlockDecl()->hasCaptures())
54
143
    return;
55
220
56
220
  ProgramStateRef state = C.getState();
57
220
  auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
58
220
59
220
  BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
60
220
                                            E = R->referenced_vars_end();
61
220
62
503
  for (; I != E; 
++I283
) {
63
283
    // This VarRegion is the region associated with the block; we need
64
283
    // the one associated with the encompassing context.
65
283
    const VarRegion *VR = I.getCapturedRegion();
66
283
    const VarDecl *VD = VR->getDecl();
67
283
68
283
    if (VD->hasAttr<BlocksAttr>() || 
!VD->hasLocalStorage()215
)
69
84
      continue;
70
199
71
199
    // Get the VarRegion associated with VD in the local stack frame.
72
199
    if (Optional<UndefinedVal> V =
73
6
          state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
74
6
      if (ExplodedNode *N = C.generateErrorNode()) {
75
6
        if (!BT)
76
4
          BT.reset(
77
4
              new BuiltinBug(this, "uninitialized variable captured by block"));
78
6
79
6
        // Generate a bug report.
80
6
        SmallString<128> buf;
81
6
        llvm::raw_svector_ostream os(buf);
82
6
83
6
        os << "Variable '" << VD->getName()
84
6
           << "' is uninitialized when captured by block";
85
6
86
6
        auto R = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
87
6
        if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
88
6
          R->addRange(Ex->getSourceRange());
89
6
        R->addVisitor(std::make_unique<FindLastStoreBRVisitor>(
90
6
            *V, VR, /*EnableNullFPSuppression*/ false,
91
6
            bugreporter::TrackingKind::Thorough));
92
6
        R->disablePathPruning();
93
6
        // need location of block
94
6
        C.emitReport(std::move(R));
95
6
      }
96
6
    }
97
199
  }
98
220
}
99
100
1.00k
void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101
1.00k
  mgr.registerChecker<UndefCapturedBlockVarChecker>();
102
1.00k
}
103
104
1.00k
bool ento::shouldRegisterUndefCapturedBlockVarChecker(const LangOptions &LO) {
105
1.00k
  return true;
106
1.00k
}