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 | |
| 14 | #include "GRExprEngineInternalChecks.h" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 15 | #include "clang/GR/PathSensitive/CheckerVisitor.h" |
| 16 | #include "clang/GR/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/GR/BugReporter/BugType.h" |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 21 | using namespace GR; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | class UndefCapturedBlockVarChecker |
| 25 | : public CheckerVisitor<UndefCapturedBlockVarChecker> { |
| 26 | BugType *BT; |
| 27 | |
| 28 | public: |
| 29 | UndefCapturedBlockVarChecker() : BT(0) {} |
| 30 | static void *getTag() { static int tag = 0; return &tag; } |
| 31 | void PostVisitBlockExpr(CheckerContext &C, const BlockExpr *BE); |
| 32 | }; |
| 33 | } // end anonymous namespace |
| 34 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 35 | void GR::RegisterUndefCapturedBlockVarChecker(GRExprEngine &Eng) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 36 | Eng.registerCheck(new UndefCapturedBlockVarChecker()); |
| 37 | } |
| 38 | |
| 39 | static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S, |
| 40 | const VarDecl *VD){ |
| 41 | if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S)) |
| 42 | if (BR->getDecl() == VD) |
| 43 | return BR; |
| 44 | |
| 45 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 46 | I!=E; ++I) |
| 47 | if (const Stmt *child = *I) { |
| 48 | const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD); |
| 49 | if (BR) |
| 50 | return BR; |
| 51 | } |
| 52 | |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | UndefCapturedBlockVarChecker::PostVisitBlockExpr(CheckerContext &C, |
| 58 | const BlockExpr *BE) { |
| 59 | if (!BE->hasBlockDeclRefExprs()) |
| 60 | return; |
| 61 | |
| 62 | const GRState *state = C.getState(); |
| 63 | const BlockDataRegion *R = |
| 64 | cast<BlockDataRegion>(state->getSVal(BE).getAsRegion()); |
| 65 | |
| 66 | BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), |
| 67 | E = R->referenced_vars_end(); |
| 68 | |
| 69 | for (; I != E; ++I) { |
| 70 | // This VarRegion is the region associated with the block; we need |
| 71 | // the one associated with the encompassing context. |
| 72 | const VarRegion *VR = *I; |
| 73 | const VarDecl *VD = VR->getDecl(); |
| 74 | |
| 75 | if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage()) |
| 76 | continue; |
| 77 | |
| 78 | // Get the VarRegion associated with VD in the local stack frame. |
| 79 | const LocationContext *LC = C.getPredecessor()->getLocationContext(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 80 | VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 81 | |
| 82 | if (state->getSVal(VR).isUndef()) |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 83 | if (ExplodedNode *N = C.generateSink()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 84 | if (!BT) |
| 85 | BT = new BuiltinBug("Captured block variable is uninitialized"); |
| 86 | |
| 87 | // Generate a bug report. |
| 88 | llvm::SmallString<128> buf; |
| 89 | llvm::raw_svector_ostream os(buf); |
| 90 | |
| 91 | os << "Variable '" << VD->getName() << "' is captured by block with " |
| 92 | "a garbage value"; |
| 93 | |
| 94 | EnhancedBugReport *R = new EnhancedBugReport(*BT, os.str(), N); |
| 95 | if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD)) |
| 96 | R->addRange(Ex->getSourceRange()); |
| 97 | R->addVisitorCreator(bugreporter::registerFindLastStore, VR); |
| 98 | // need location of block |
| 99 | C.EmitReport(R); |
| 100 | } |
| 101 | } |
| 102 | } |