blob: 53fd069bf150ce97c24167e535cb2f90f06b9e77 [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> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000030 mutable std::unique_ptr<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
Benjamin Kramer973431b2015-07-03 15:12:24 +000043 for (const Stmt *Child : S->children())
44 if (Child)
45 if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
Ted Kremenek22863972010-02-16 08:33:59 +000046 return BR;
Ted Kremenek22863972010-02-16 08:33:59 +000047
Craig Topper0dbb7832014-05-27 02:45:47 +000048 return nullptr;
Ted Kremenek22863972010-02-16 08:33:59 +000049}
50
51void
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000052UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
53 CheckerContext &C) const {
John McCallc63de662011-02-02 13:00:07 +000054 if (!BE->getBlockDecl()->hasCaptures())
Ted Kremenek22863972010-02-16 08:33:59 +000055 return;
56
Ted Kremenek49b1e382012-01-26 21:29:00 +000057 ProgramStateRef state = C.getState();
Ted Kremenek22863972010-02-16 08:33:59 +000058 const BlockDataRegion *R =
Ted Kremenek632e3b72012-01-06 22:09:28 +000059 cast<BlockDataRegion>(state->getSVal(BE,
60 C.getLocationContext()).getAsRegion());
Ted Kremenek22863972010-02-16 08:33:59 +000061
62 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
63 E = R->referenced_vars_end();
64
65 for (; I != E; ++I) {
66 // This VarRegion is the region associated with the block; we need
67 // the one associated with the encompassing context.
Ted Kremenekbcf90532012-12-06 07:17:20 +000068 const VarRegion *VR = I.getCapturedRegion();
Ted Kremenek22863972010-02-16 08:33:59 +000069 const VarDecl *VD = VR->getDecl();
70
Aaron Ballman9ead1242013-12-19 02:39:40 +000071 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
Ted Kremenek22863972010-02-16 08:33:59 +000072 continue;
73
74 // Get the VarRegion associated with VD in the local stack frame.
Ted Kremeneke3cf1712013-02-24 07:20:53 +000075 if (Optional<UndefinedVal> V =
76 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +000077 if (ExplodedNode *N = C.generateSink()) {
Ted Kremenek22863972010-02-16 08:33:59 +000078 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000079 BT.reset(
80 new BuiltinBug(this, "uninitialized variable captured by block"));
Ted Kremenek22863972010-02-16 08:33:59 +000081
82 // Generate a bug report.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000083 SmallString<128> buf;
Ted Kremenek22863972010-02-16 08:33:59 +000084 llvm::raw_svector_ostream os(buf);
85
Ted Kremenek7fd987d2011-01-25 19:13:42 +000086 os << "Variable '" << VD->getName()
87 << "' is uninitialized when captured by block";
Ted Kremenek22863972010-02-16 08:33:59 +000088
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000089 auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
Ted Kremenek22863972010-02-16 08:33:59 +000090 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
91 R->addRange(Ex->getSourceRange());
David Blaikie91e79022014-09-04 23:54:33 +000092 R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
93 *V, VR, /*EnableNullFPSuppression*/ false));
Ted Kremenek16704bb2012-05-31 06:03:17 +000094 R->disablePathPruning();
Ted Kremenek22863972010-02-16 08:33:59 +000095 // need location of block
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000096 C.emitReport(std::move(R));
Ted Kremenek22863972010-02-16 08:33:59 +000097 }
Ted Kremeneke3cf1712013-02-24 07:20:53 +000098 }
Ted Kremenek22863972010-02-16 08:33:59 +000099 }
100}
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +0000101
102void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
103 mgr.registerChecker<UndefCapturedBlockVarChecker>();
104}