clang  7.0.0svn
UndefCapturedBlockVarChecker.cpp
Go to the documentation of this file.
1 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This checker detects blocks that capture uninitialized values.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ClangSACheckers.h"
15 #include "clang/AST/Attr.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 namespace {
28 class UndefCapturedBlockVarChecker
29  : public Checker< check::PostStmt<BlockExpr> > {
30  mutable std::unique_ptr<BugType> BT;
31 
32 public:
33  void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
34 };
35 } // end anonymous namespace
36 
37 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
38  const VarDecl *VD) {
39  if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
40  if (BR->getDecl() == VD)
41  return BR;
42 
43  for (const Stmt *Child : S->children())
44  if (Child)
45  if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
46  return BR;
47 
48  return nullptr;
49 }
50 
51 void
52 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
53  CheckerContext &C) const {
54  if (!BE->getBlockDecl()->hasCaptures())
55  return;
56 
58  auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
59 
60  BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
61  E = R->referenced_vars_end();
62 
63  for (; I != E; ++I) {
64  // This VarRegion is the region associated with the block; we need
65  // the one associated with the encompassing context.
66  const VarRegion *VR = I.getCapturedRegion();
67  const VarDecl *VD = VR->getDecl();
68 
69  if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
70  continue;
71 
72  // Get the VarRegion associated with VD in the local stack frame.
74  state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
75  if (ExplodedNode *N = C.generateErrorNode()) {
76  if (!BT)
77  BT.reset(
78  new BuiltinBug(this, "uninitialized variable captured by block"));
79 
80  // Generate a bug report.
81  SmallString<128> buf;
82  llvm::raw_svector_ostream os(buf);
83 
84  os << "Variable '" << VD->getName()
85  << "' is uninitialized when captured by block";
86 
87  auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
88  if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
89  R->addRange(Ex->getSourceRange());
90  R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
91  *V, VR, /*EnableNullFPSuppression*/ false));
92  R->disablePathPruning();
93  // need location of block
94  C.emitReport(std::move(R));
95  }
96  }
97  }
98 }
99 
100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101  mgr.registerChecker<UndefCapturedBlockVarChecker>();
102 }
const BlockDecl * getBlockDecl() const
Definition: Expr.h:4896
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition: Decl.h:3960
static const DeclRefExpr * FindBlockDeclRefExpr(const Stmt *S, const VarDecl *VD)
Stmt - This represents one statement.
Definition: Stmt.h:66
Represents a variable declaration or definition.
Definition: Decl.h:812
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
child_range children()
Definition: Stmt.cpp:227
const Stmt * getBody() const
Definition: Expr.cpp:2015
bool hasAttr() const
Definition: DeclBase.h:536
Expr - This represents one expression.
Definition: Expr.h:106
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1033
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4882
const VarDecl * getDecl() const
Definition: MemRegion.h:943
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
const MemRegion * getAsRegion() const
Definition: SVals.cpp:151
Dataflow Directional Tag Classes.
const ProgramStateRef & getState() const
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:972