Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 1 | // 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 Kyrtzidis | 60b6da7 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 15 | #include "clang/AST/Attr.h" |
| 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 60b6da7 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 25 | using namespace ento; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | class UndefCapturedBlockVarChecker |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 29 | : public Checker< check::PostStmt<BlockExpr> > { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 30 | mutable std::unique_ptr<BugType> BT; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Argyrios Kyrtzidis | 60b6da7 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 33 | void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 37 | static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S, |
| 38 | const VarDecl *VD) { |
| 39 | if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S)) |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 40 | if (BR->getDecl() == VD) |
| 41 | return BR; |
| 42 | |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame^] | 43 | for (const Stmt *Child : S->children()) |
| 44 | if (Child) |
| 45 | if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD)) |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 46 | return BR; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 47 | |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 48 | return nullptr; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
Argyrios Kyrtzidis | 60b6da7 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 52 | UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE, |
| 53 | CheckerContext &C) const { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 54 | if (!BE->getBlockDecl()->hasCaptures()) |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 55 | return; |
| 56 | |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 57 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 58 | const BlockDataRegion *R = |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 59 | cast<BlockDataRegion>(state->getSVal(BE, |
| 60 | C.getLocationContext()).getAsRegion()); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 61 | |
| 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 Kremenek | bcf9053 | 2012-12-06 07:17:20 +0000 | [diff] [blame] | 68 | const VarRegion *VR = I.getCapturedRegion(); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 69 | const VarDecl *VD = VR->getDecl(); |
| 70 | |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 71 | if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 72 | continue; |
| 73 | |
| 74 | // Get the VarRegion associated with VD in the local stack frame. |
Ted Kremenek | e3cf171 | 2013-02-24 07:20:53 +0000 | [diff] [blame] | 75 | if (Optional<UndefinedVal> V = |
| 76 | state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) { |
Ted Kremenek | 750b7ac | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 77 | if (ExplodedNode *N = C.generateSink()) { |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 78 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 79 | BT.reset( |
| 80 | new BuiltinBug(this, "uninitialized variable captured by block")); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 81 | |
| 82 | // Generate a bug report. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 83 | SmallString<128> buf; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 84 | llvm::raw_svector_ostream os(buf); |
| 85 | |
Ted Kremenek | 7fd987d | 2011-01-25 19:13:42 +0000 | [diff] [blame] | 86 | os << "Variable '" << VD->getName() |
| 87 | << "' is uninitialized when captured by block"; |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 88 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 89 | auto R = llvm::make_unique<BugReport>(*BT, os.str(), N); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 90 | if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD)) |
| 91 | R->addRange(Ex->getSourceRange()); |
David Blaikie | 91e7902 | 2014-09-04 23:54:33 +0000 | [diff] [blame] | 92 | R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( |
| 93 | *V, VR, /*EnableNullFPSuppression*/ false)); |
Ted Kremenek | 16704bb | 2012-05-31 06:03:17 +0000 | [diff] [blame] | 94 | R->disablePathPruning(); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 95 | // need location of block |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 96 | C.emitReport(std::move(R)); |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | e3cf171 | 2013-02-24 07:20:53 +0000 | [diff] [blame] | 98 | } |
Ted Kremenek | 2286397 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
Argyrios Kyrtzidis | 60b6da7 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 101 | |
| 102 | void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) { |
| 103 | mgr.registerChecker<UndefCapturedBlockVarChecker>(); |
| 104 | } |