blob: 82c9fd4a86be19582078f29f1333f33deaeeda37 [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000016#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 Kyrtzidisec8605f2011-03-01 01:16:21 +000027 : public Checker< check::PostStmt<BlockExpr> > {
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000028 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
Ted Kremenek18c66fd2011-08-15 22:09:50 +000058 const ProgramState *state = C.getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000059 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.
Anna Zaks39ac1872011-10-26 21:06:44 +000075 const LocationContext *LC = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000076 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaks50bbc162011-08-19 22:33:38 +000077 SVal VRVal = state->getSVal(VR);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000078
Anna Zaks50bbc162011-08-19 22:33:38 +000079 if (VRVal.isUndef())
Ted Kremenekd048c6e2010-12-20 21:19:09 +000080 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000081 if (!BT)
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000082 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000083
84 // Generate a bug report.
85 llvm::SmallString<128> buf;
86 llvm::raw_svector_ostream os(buf);
87
Ted Kremenek937596f2011-01-25 19:13:42 +000088 os << "Variable '" << VD->getName()
89 << "' is uninitialized when captured by block";
Ted Kremenek94fd0b82010-02-16 08:33:59 +000090
Anna Zakse172e8b2011-08-17 23:00:25 +000091 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000092 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
93 R->addRange(Ex->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +000094 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000095 // need location of block
96 C.EmitReport(R);
97 }
98 }
99}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +0000100
101void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
102 mgr.registerChecker<UndefCapturedBlockVarChecker>();
103}