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
22 using namespace clang;
23 using namespace ento;
24
25 namespace {
26 class UndefCapturedBlockVarChecker
27 : public Checker< check::PostStmt<BlockExpr> > {
28 mutable llvm::OwningPtr<BugType> BT;
29
30 public:
31 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
32 };
33 } // end anonymous namespace
34
FindBlockDeclRefExpr(const Stmt * S,const VarDecl * VD)35 static 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
52 void
checkPostStmt(const BlockExpr * BE,CheckerContext & C) const53 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
54 CheckerContext &C) const {
55 if (!BE->getBlockDecl()->hasCaptures())
56 return;
57
58 const GRState *state = C.getState();
59 const BlockDataRegion *R =
60 cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
61
62 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
63 E = R->referenced_vars_end();
64
65 for (; I != E; ++I) {
66 // This VarRegion is the region associated with the block; we need
67 // the one associated with the encompassing context.
68 const VarRegion *VR = *I;
69 const VarDecl *VD = VR->getDecl();
70
71 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
72 continue;
73
74 // Get the VarRegion associated with VD in the local stack frame.
75 const LocationContext *LC = C.getPredecessor()->getLocationContext();
76 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
77
78 if (state->getSVal(VR).isUndef())
79 if (ExplodedNode *N = C.generateSink()) {
80 if (!BT)
81 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
82
83 // Generate a bug report.
84 llvm::SmallString<128> buf;
85 llvm::raw_svector_ostream os(buf);
86
87 os << "Variable '" << VD->getName()
88 << "' is uninitialized when captured by block";
89
90 EnhancedBugReport *R = new EnhancedBugReport(*BT, os.str(), N);
91 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
92 R->addRange(Ex->getSourceRange());
93 R->addVisitorCreator(bugreporter::registerFindLastStore, VR);
94 // need location of block
95 C.EmitReport(R);
96 }
97 }
98 }
99
registerUndefCapturedBlockVarChecker(CheckerManager & mgr)100 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
102 }
103