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