blob: 6023048601f2b316e2ea2e3702d9392eff532736 [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"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenek22863972010-02-16 08:33:59 +000022#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000025using namespace ento;
Ted Kremenek22863972010-02-16 08:33:59 +000026
27namespace {
28class UndefCapturedBlockVarChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000029 : public Checker< check::PostStmt<BlockExpr> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000030 mutable OwningPtr<BugType> BT;
Ted Kremenek22863972010-02-16 08:33:59 +000031
32public:
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000033 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
Ted Kremenek22863972010-02-16 08:33:59 +000034};
35} // end anonymous namespace
36
John McCall113bee02012-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 Kremenek22863972010-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 McCall113bee02012-03-10 09:33:50 +000046 const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
Ted Kremenek22863972010-02-16 08:33:59 +000047 if (BR)
48 return BR;
49 }
50
51 return NULL;
52}
53
54void
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000055UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
56 CheckerContext &C) const {
John McCallc63de662011-02-02 13:00:07 +000057 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek22863972010-02-16 08:33:59 +000058 return;
59
Ted Kremenek49b1e382012-01-26 21:29:00 +000060 ProgramStateRef state = C.getState();
Ted Kremenek22863972010-02-16 08:33:59 +000061 const BlockDataRegion *R =
Ted Kremenek632e3b72012-01-06 22:09:28 +000062 cast<BlockDataRegion>(state->getSVal(BE,
63 C.getLocationContext()).getAsRegion());
Ted Kremenek22863972010-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 Zaksc9abbe22011-10-26 21:06:44 +000078 const LocationContext *LC = C.getLocationContext();
Ted Kremenek90af9092010-12-02 07:49:45 +000079 VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
Anna Zaksf86615c2011-08-19 22:33:38 +000080 SVal VRVal = state->getSVal(VR);
Ted Kremenek22863972010-02-16 08:33:59 +000081
Anna Zaksf86615c2011-08-19 22:33:38 +000082 if (VRVal.isUndef())
Ted Kremenek750b7ac2010-12-20 21:19:09 +000083 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek22863972010-02-16 08:33:59 +000084 if (!BT)
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000085 BT.reset(new BuiltinBug("uninitialized variable captured by block"));
Ted Kremenek22863972010-02-16 08:33:59 +000086
87 // Generate a bug report.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000088 SmallString<128> buf;
Ted Kremenek22863972010-02-16 08:33:59 +000089 llvm::raw_svector_ostream os(buf);
90
Ted Kremenek7fd987d2011-01-25 19:13:42 +000091 os << "Variable '" << VD->getName()
92 << "' is uninitialized when captured by block";
Ted Kremenek22863972010-02-16 08:33:59 +000093
Anna Zaks3a6bdf82011-08-17 23:00:25 +000094 BugReport *R = new BugReport(*BT, os.str(), N);
Ted Kremenek22863972010-02-16 08:33:59 +000095 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
96 R->addRange(Ex->getSourceRange());
Anna Zaksf86615c2011-08-19 22:33:38 +000097 R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
Ted Kremenek16704bb2012-05-31 06:03:17 +000098 R->disablePathPruning();
Ted Kremenek22863972010-02-16 08:33:59 +000099 // need location of block
Jordan Rosee10d5a72012-11-02 01:53:40 +0000100 C.emitReport(R);
Ted Kremenek22863972010-02-16 08:33:59 +0000101 }
102 }
103}
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +0000104
105void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
106 mgr.registerChecker<UndefCapturedBlockVarChecker>();
107}