Ted Kremenek | 94fd0b8 | 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 | 265c674 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 14 | #include "ClangSACheckers.h" |
Benjamin Kramer | 2fa67ef | 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 | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 265c674 | 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 | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 25 | using namespace ento; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | class UndefCapturedBlockVarChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 29 | : public Checker< check::PostStmt<BlockExpr> > { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 30 | mutable std::unique_ptr<BugType> BT; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Argyrios Kyrtzidis | 265c674 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 33 | void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 34 | }; |
| 35 | } // end anonymous namespace |
| 36 | |
John McCall | f4b88a4 | 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 | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 40 | 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 McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 46 | const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 47 | if (BR) |
| 48 | return BR; |
| 49 | } |
| 50 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 51 | return nullptr; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void |
Argyrios Kyrtzidis | 265c674 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 55 | UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE, |
| 56 | CheckerContext &C) const { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 57 | if (!BE->getBlockDecl()->hasCaptures()) |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 58 | return; |
| 59 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 60 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 61 | const BlockDataRegion *R = |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 62 | cast<BlockDataRegion>(state->getSVal(BE, |
| 63 | C.getLocationContext()).getAsRegion()); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 64 | |
| 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 Kremenek | e3ce2c1 | 2012-12-06 07:17:20 +0000 | [diff] [blame] | 71 | const VarRegion *VR = I.getCapturedRegion(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 72 | const VarDecl *VD = VR->getDecl(); |
| 73 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 74 | if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 75 | continue; |
| 76 | |
| 77 | // Get the VarRegion associated with VD in the local stack frame. |
Ted Kremenek | 0dd15d7 | 2013-02-24 07:20:53 +0000 | [diff] [blame] | 78 | if (Optional<UndefinedVal> V = |
| 79 | state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 80 | if (ExplodedNode *N = C.generateSink()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 81 | if (!BT) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 82 | BT.reset( |
| 83 | new BuiltinBug(this, "uninitialized variable captured by block")); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 84 | |
| 85 | // Generate a bug report. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 86 | SmallString<128> buf; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 87 | llvm::raw_svector_ostream os(buf); |
| 88 | |
Ted Kremenek | 937596f | 2011-01-25 19:13:42 +0000 | [diff] [blame] | 89 | os << "Variable '" << VD->getName() |
| 90 | << "' is uninitialized when captured by block"; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 91 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 92 | BugReport *R = new BugReport(*BT, os.str(), N); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 93 | if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD)) |
| 94 | R->addRange(Ex->getSourceRange()); |
Anna Zaks | aabb4c5 | 2013-03-28 23:15:22 +0000 | [diff] [blame] | 95 | R->addVisitor(new FindLastStoreBRVisitor(*V, VR, |
| 96 | /*EnableNullFPSuppression*/false)); |
Ted Kremenek | ed7948b | 2012-05-31 06:03:17 +0000 | [diff] [blame] | 97 | R->disablePathPruning(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 98 | // need location of block |
Jordan Rose | 785950e | 2012-11-02 01:53:40 +0000 | [diff] [blame] | 99 | C.emitReport(R); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | 0dd15d7 | 2013-02-24 07:20:53 +0000 | [diff] [blame] | 101 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Argyrios Kyrtzidis | 265c674 | 2011-02-28 01:27:26 +0000 | [diff] [blame] | 104 | |
| 105 | void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) { |
| 106 | mgr.registerChecker<UndefCapturedBlockVarChecker>(); |
| 107 | } |