UndefCapturedBlockVarChecker.cpp revision 5eca482fe895ea57bc82410222e6426c09e63284
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/StaticAnalyzer/Core/Checker.h"
16#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
23using namespace ento;
24
25namespace {
26class UndefCapturedBlockVarChecker
27  : public Checker< check::PostStmt<BlockExpr> > {
28 mutable llvm::OwningPtr<BugType> BT;
29
30public:
31  void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
32};
33} // end anonymous namespace
34
35static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
36                                                    const VarDecl *VD){
37  if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S))
38    if (BR->getDecl() == VD)
39      return BR;
40
41  for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
42       I!=E; ++I)
43    if (const Stmt *child = *I) {
44      const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
45      if (BR)
46        return BR;
47    }
48
49  return NULL;
50}
51
52void
53UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
54                                            CheckerContext &C) const {
55  if (!BE->getBlockDecl()->hasCaptures())
56    return;
57
58  const ProgramState *state = C.getState();
59  const BlockDataRegion *R =
60    cast<BlockDataRegion>(state->getSVal(BE,
61                                         C.getLocationContext()).getAsRegion());
62
63  BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
64                                            E = R->referenced_vars_end();
65
66  for (; I != E; ++I) {
67    // This VarRegion is the region associated with the block; we need
68    // the one associated with the encompassing context.
69    const VarRegion *VR = *I;
70    const VarDecl *VD = VR->getDecl();
71
72    if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
73      continue;
74
75    // Get the VarRegion associated with VD in the local stack frame.
76    const LocationContext *LC = C.getLocationContext();
77    VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
78    SVal VRVal = state->getSVal(VR);
79
80    if (VRVal.isUndef())
81      if (ExplodedNode *N = C.generateSink()) {
82        if (!BT)
83          BT.reset(new BuiltinBug("uninitialized variable captured by block"));
84
85        // Generate a bug report.
86        llvm::SmallString<128> buf;
87        llvm::raw_svector_ostream os(buf);
88
89        os << "Variable '" << VD->getName()
90           << "' is uninitialized when captured by block";
91
92        BugReport *R = new BugReport(*BT, os.str(), N);
93        if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
94          R->addRange(Ex->getSourceRange());
95        R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
96        // need location of block
97        C.EmitReport(R);
98      }
99  }
100}
101
102void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
103  mgr.registerChecker<UndefCapturedBlockVarChecker>();
104}
105