blob: 718aa75da7c76a30acef5175999124bd1a94336b [file] [log] [blame]
Ted Kremenek94fd0b82010-02-16 08:33:59 +00001// 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
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000014#include "ClangSACheckers.h"
15#include "clang/StaticAnalyzer/Core/CheckerV2.h"
16#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek94fd0b82010-02-16 08:33:59 +000020#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000024
25namespace {
26class UndefCapturedBlockVarChecker
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000027 : public CheckerV2< check::PostStmt<BlockExpr> > {
28 mutable llvm::OwningPtr<BugType> BT;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000029
30public:
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000031 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000032};
33} // end anonymous namespace
34
Ted Kremenek94fd0b82010-02-16 08:33:59 +000035static 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
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000053UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
54 CheckerContext &C) const {
John McCall469a1eb2011-02-02 13:00:07 +000055 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek94fd0b82010-02-16 08:33:59 +000056 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();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000076 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000077
78 if (state->getSVal(VR).isUndef())
Ted Kremenekd048c6e2010-12-20 21:19:09 +000079 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000080 if (!BT)
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000081 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000082
83 // Generate a bug report.
84 llvm::SmallString<128> buf;
85 llvm::raw_svector_ostream os(buf);
86
Ted Kremenek937596f2011-01-25 19:13:42 +000087 os << "Variable '" << VD->getName()
88 << "' is uninitialized when captured by block";
Ted Kremenek94fd0b82010-02-16 08:33:59 +000089
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}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000099
100void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
102}