blob: 60f1f5b8e345c2a445f361c4bd15cc10d9ceadfc [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 =
Ted Kremenek5eca4822012-01-06 22:09:28 +000060 cast<BlockDataRegion>(state->getSVal(BE,
61 C.getLocationContext()).getAsRegion());
Ted Kremenek94fd0b82010-02-16 08:33:59 +000062
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.
Anna Zaks39ac1872011-10-26 21:06:44 +000076 const LocationContext *LC = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000077 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaks50bbc162011-08-19 22:33:38 +000078 SVal VRVal = state->getSVal(VR);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000079
Anna Zaks50bbc162011-08-19 22:33:38 +000080 if (VRVal.isUndef())
Ted Kremenekd048c6e2010-12-20 21:19:09 +000081 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000082 if (!BT)
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000083 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000084
85 // Generate a bug report.
86 llvm::SmallString<128> buf;
87 llvm::raw_svector_ostream os(buf);
88
Ted Kremenek937596f2011-01-25 19:13:42 +000089 os << "Variable '" << VD->getName()
90 << "' is uninitialized when captured by block";
Ted Kremenek94fd0b82010-02-16 08:33:59 +000091
Anna Zakse172e8b2011-08-17 23:00:25 +000092 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000093 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
94 R->addRange(Ex->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +000095 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000096 // need location of block
97 C.EmitReport(R);
98 }
99 }
100}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +0000101
102void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
103 mgr.registerChecker<UndefCapturedBlockVarChecker>();
104}