blob: 8a46035feec8f3fe0d93c2545c049c980ead1532 [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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000020#include "llvm/ADT/SmallString.h"
Ted Kremenek94fd0b82010-02-16 08:33:59 +000021#include "llvm/Support/raw_ostream.h"
22
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000025
26namespace {
27class UndefCapturedBlockVarChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000028 : public Checker< check::PostStmt<BlockExpr> > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000029 mutable OwningPtr<BugType> BT;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000030
31public:
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000032 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000033};
34} // end anonymous namespace
35
Ted Kremenek94fd0b82010-02-16 08:33:59 +000036static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
37 const VarDecl *VD){
38 if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S))
39 if (BR->getDecl() == VD)
40 return BR;
41
42 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
43 I!=E; ++I)
44 if (const Stmt *child = *I) {
45 const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
46 if (BR)
47 return BR;
48 }
49
50 return NULL;
51}
52
53void
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000054UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
55 CheckerContext &C) const {
John McCall469a1eb2011-02-02 13:00:07 +000056 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek94fd0b82010-02-16 08:33:59 +000057 return;
58
Ted Kremenek8bef8232012-01-26 21:29:00 +000059 ProgramStateRef state = C.getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000060 const BlockDataRegion *R =
Ted Kremenek5eca4822012-01-06 22:09:28 +000061 cast<BlockDataRegion>(state->getSVal(BE,
62 C.getLocationContext()).getAsRegion());
Ted Kremenek94fd0b82010-02-16 08:33:59 +000063
64 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
65 E = R->referenced_vars_end();
66
67 for (; I != E; ++I) {
68 // This VarRegion is the region associated with the block; we need
69 // the one associated with the encompassing context.
70 const VarRegion *VR = *I;
71 const VarDecl *VD = VR->getDecl();
72
73 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
74 continue;
75
76 // Get the VarRegion associated with VD in the local stack frame.
Anna Zaks39ac1872011-10-26 21:06:44 +000077 const LocationContext *LC = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000078 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaks50bbc162011-08-19 22:33:38 +000079 SVal VRVal = state->getSVal(VR);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000080
Anna Zaks50bbc162011-08-19 22:33:38 +000081 if (VRVal.isUndef())
Ted Kremenekd048c6e2010-12-20 21:19:09 +000082 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000083 if (!BT)
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000084 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000085
86 // Generate a bug report.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000087 SmallString<128> buf;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000088 llvm::raw_svector_ostream os(buf);
89
Ted Kremenek937596f2011-01-25 19:13:42 +000090 os << "Variable '" << VD->getName()
91 << "' is uninitialized when captured by block";
Ted Kremenek94fd0b82010-02-16 08:33:59 +000092
Anna Zakse172e8b2011-08-17 23:00:25 +000093 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000094 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
95 R->addRange(Ex->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +000096 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000097 // need location of block
98 C.EmitReport(R);
99 }
100 }
101}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +0000102
103void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
104 mgr.registerChecker<UndefCapturedBlockVarChecker>();
105}