blob: 8a46035feec8f3fe0d93c2545c049c980ead1532 [file] [log] [blame]
Ted Kremenek22863972010-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 Kyrtzidis60b6da72011-02-28 01:27:26 +000014#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000020#include "llvm/ADT/SmallString.h"
Ted Kremenek22863972010-02-16 08:33:59 +000021#include "llvm/Support/raw_ostream.h"
22
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek22863972010-02-16 08:33:59 +000025
26namespace {
27class UndefCapturedBlockVarChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000028 : public Checker< check::PostStmt<BlockExpr> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000029 mutable OwningPtr<BugType> BT;
Ted Kremenek22863972010-02-16 08:33:59 +000030
31public:
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000032 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
Ted Kremenek22863972010-02-16 08:33:59 +000033};
34} // end anonymous namespace
35
Ted Kremenek22863972010-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 Kyrtzidis60b6da72011-02-28 01:27:26 +000054UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
55 CheckerContext &C) const {
John McCallc63de662011-02-02 13:00:07 +000056 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek22863972010-02-16 08:33:59 +000057 return;
58
Ted Kremenek49b1e382012-01-26 21:29:00 +000059 ProgramStateRef state = C.getState();
Ted Kremenek22863972010-02-16 08:33:59 +000060 const BlockDataRegion *R =
Ted Kremenek632e3b72012-01-06 22:09:28 +000061 cast<BlockDataRegion>(state->getSVal(BE,
62 C.getLocationContext()).getAsRegion());
Ted Kremenek22863972010-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 Zaksc9abbe22011-10-26 21:06:44 +000077 const LocationContext *LC = C.getLocationContext();
Ted Kremenek90af9092010-12-02 07:49:45 +000078 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaksf86615c2011-08-19 22:33:38 +000079 SVal VRVal = state->getSVal(VR);
Ted Kremenek22863972010-02-16 08:33:59 +000080
Anna Zaksf86615c2011-08-19 22:33:38 +000081 if (VRVal.isUndef())
Ted Kremenek750b7ac2010-12-20 21:19:09 +000082 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek22863972010-02-16 08:33:59 +000083 if (!BT)
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000084 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek22863972010-02-16 08:33:59 +000085
86 // Generate a bug report.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000087 SmallString<128> buf;
Ted Kremenek22863972010-02-16 08:33:59 +000088 llvm::raw_svector_ostream os(buf);
89
Ted Kremenek7fd987d2011-01-25 19:13:42 +000090 os << "Variable '" << VD->getName()
91 << "' is uninitialized when captured by block";
Ted Kremenek22863972010-02-16 08:33:59 +000092
Anna Zaks3a6bdf82011-08-17 23:00:25 +000093 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek22863972010-02-16 08:33:59 +000094 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
95 R->addRange(Ex->getSourceRange());
Anna Zaksf86615c2011-08-19 22:33:38 +000096 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremenek22863972010-02-16 08:33:59 +000097 // need location of block
98 C.EmitReport(R);
99 }
100 }
101}
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +0000102
103void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
104 mgr.registerChecker<UndefCapturedBlockVarChecker>();
105}