blob: 93687db073f95b179876535b821db93b8ce55983 [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> > {
Stephen Hines651f13c2014-04-23 16:59:28 -070030 mutable std::unique_ptr<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
Stephen Hines6bcf27b2014-05-29 04:14:42 -070051 return nullptr;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000052}
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.
Ted Kremeneke3ce2c12012-12-06 07:17:20 +000071 const VarRegion *VR = I.getCapturedRegion();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000072 const VarDecl *VD = VR->getDecl();
73
Stephen Hines651f13c2014-04-23 16:59:28 -070074 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
Ted Kremenek94fd0b82010-02-16 08:33:59 +000075 continue;
76
77 // Get the VarRegion associated with VD in the local stack frame.
Ted Kremenek0dd15d72013-02-24 07:20:53 +000078 if (Optional<UndefinedVal> V =
79 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000080 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek94fd0b82010-02-16 08:33:59 +000081 if (!BT)
Stephen Hines651f13c2014-04-23 16:59:28 -070082 BT.reset(
83 new BuiltinBug(this, "uninitialized variable captured by block"));
Ted Kremenek94fd0b82010-02-16 08:33:59 +000084
85 // Generate a bug report.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000086 SmallString<128> buf;
Ted Kremenek94fd0b82010-02-16 08:33:59 +000087 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 Zaksaabb4c52013-03-28 23:15:22 +000095 R->addVisitor(new FindLastStoreBRVisitor(*V, VR,
96 /*EnableNullFPSuppression*/false));
Ted Kremeneked7948b2012-05-31 06:03:17 +000097 R->disablePathPruning();
Ted Kremenek94fd0b82010-02-16 08:33:59 +000098 // need location of block
Jordan Rose785950e2012-11-02 01:53:40 +000099 C.emitReport(R);
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000100 }
Ted Kremenek0dd15d72013-02-24 07:20:53 +0000101 }
Ted Kremenek94fd0b82010-02-16 08:33:59 +0000102 }
103}
Argyrios Kyrtzidis265c6742011-02-28 01:27:26 +0000104
105void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
106 mgr.registerChecker<UndefCapturedBlockVarChecker>();
107}