blob: 6a93c10c764432fed7ff31fef7c1530f6c2c9390 [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();
George Karpenkovd703ec92018-01-17 20:27:29 +000058 auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
Ted Kremenek22863972010-02-16 08:33:59 +000059
60 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
61 E = R->referenced_vars_end();
62
63 for (; I != E; ++I) {
64 // This VarRegion is the region associated with the block; we need
65 // the one associated with the encompassing context.
Ted Kremenekbcf90532012-12-06 07:17:20 +000066 const VarRegion *VR = I.getCapturedRegion();
Ted Kremenek22863972010-02-16 08:33:59 +000067 const VarDecl *VD = VR->getDecl();
68
Aaron Ballman9ead1242013-12-19 02:39:40 +000069 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
Ted Kremenek22863972010-02-16 08:33:59 +000070 continue;
71
72 // Get the VarRegion associated with VD in the local stack frame.
Ted Kremeneke3cf1712013-02-24 07:20:53 +000073 if (Optional<UndefinedVal> V =
74 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
Devin Coughline39bd402015-09-16 22:03:05 +000075 if (ExplodedNode *N = C.generateErrorNode()) {
Ted Kremenek22863972010-02-16 08:33:59 +000076 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000077 BT.reset(
78 new BuiltinBug(this, "uninitialized variable captured by block"));
Ted Kremenek22863972010-02-16 08:33:59 +000079
80 // Generate a bug report.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000081 SmallString<128> buf;
Ted Kremenek22863972010-02-16 08:33:59 +000082 llvm::raw_svector_ostream os(buf);
83
Ted Kremenek3a0678e2015-09-08 03:50:52 +000084 os << "Variable '" << VD->getName()
Ted Kremenek7fd987d2011-01-25 19:13:42 +000085 << "' is uninitialized when captured by block";
Ted Kremenek22863972010-02-16 08:33:59 +000086
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000087 auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
Ted Kremenek22863972010-02-16 08:33:59 +000088 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
89 R->addRange(Ex->getSourceRange());
David Blaikie91e79022014-09-04 23:54:33 +000090 R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
91 *V, VR, /*EnableNullFPSuppression*/ false));
Ted Kremenek16704bb2012-05-31 06:03:17 +000092 R->disablePathPruning();
Ted Kremenek22863972010-02-16 08:33:59 +000093 // need location of block
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000094 C.emitReport(std::move(R));
Ted Kremenek22863972010-02-16 08:33:59 +000095 }
Ted Kremeneke3cf1712013-02-24 07:20:53 +000096 }
Ted Kremenek22863972010-02-16 08:33:59 +000097 }
98}
Argyrios Kyrtzidis60b6da72011-02-28 01:27:26 +000099
100void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
101 mgr.registerChecker<UndefCapturedBlockVarChecker>();
102}