blob: 6023048601f2b316e2ea2e3702d9392eff532736 [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenek94fd0b82010-02-16 08:33:59 +000022#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000026
27namespace {
28class UndefCapturedBlockVarChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000029 : public Checker< check::PostStmt<BlockExpr> > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000030 mutable OwningPtr<BugType> BT;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000031
32public:
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000033 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000034};
35} // end anonymous namespace
36
John McCallf4b88a42012-03-10 09:33:50 +000037static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
38 const VarDecl *VD) {
39 if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
Ted Kremenek94fd0b82010-02-16 08:33:59 +000040 if (BR->getDecl() == VD)
41 return BR;
42
43 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
44 I!=E; ++I)
45 if (const Stmt *child = *I) {
John McCallf4b88a42012-03-10 09:33:50 +000046 const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000047 if (BR)
48 return BR;
49 }
50
51 return NULL;
52}
53
54void
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000055UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
56 CheckerContext &C) const {
John McCall469a1eb2011-02-02 13:00:07 +000057 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek94fd0b82010-02-16 08:33:59 +000058 return;
59
Ted Kremenek8bef8232012-01-26 21:29:00 +000060 ProgramStateRef state = C.getState();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000061 const BlockDataRegion *R =
Ted Kremenek5eca4822012-01-06 22:09:28 +000062 cast<BlockDataRegion>(state->getSVal(BE,
63 C.getLocationContext()).getAsRegion());
Ted Kremenek94fd0b82010-02-16 08:33:59 +000064
65 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
66 E = R->referenced_vars_end();
67
68 for (; I != E; ++I) {
69 // This VarRegion is the region associated with the block; we need
70 // the one associated with the encompassing context.
71 const VarRegion *VR = *I;
72 const VarDecl *VD = VR->getDecl();
73
74 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
75 continue;
76
77 // Get the VarRegion associated with VD in the local stack frame.
Anna Zaks39ac1872011-10-26 21:06:44 +000078 const LocationContext *LC = C.getLocationContext();
Ted Kremenekc8413fd2010-12-02 07:49:45 +000079 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaks50bbc162011-08-19 22:33:38 +000080 SVal VRVal = state->getSVal(VR);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000081
Anna Zaks50bbc162011-08-19 22:33:38 +000082 if (VRVal.isUndef())
Ted Kremenekd048c6e2010-12-20 21:19:09 +000083 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000084 if (!BT)
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +000085 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000086
87 // Generate a bug report.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000088 SmallString<128> buf;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000089 llvm::raw_svector_ostream os(buf);
90
Ted Kremenek937596f2011-01-25 19:13:42 +000091 os << "Variable '" << VD->getName()
92 << "' is uninitialized when captured by block";
Ted Kremenek94fd0b82010-02-16 08:33:59 +000093
Anna Zakse172e8b2011-08-17 23:00:25 +000094 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek94fd0b82010-02-16 08:33:59 +000095 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
96 R->addRange(Ex->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +000097 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremeneked7948b2012-05-31 06:03:17 +000098 R->disablePathPruning();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000099 // need location of block
Jordan Rose785950e2012-11-02 01:53:40 +0000100 C.emitReport(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000101 }
102 }
103}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +0000104
105void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
106 mgr.registerChecker<UndefCapturedBlockVarChecker>();
107}