Home | History | Annotate | Download | only in Checkers
      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 "ClangSACheckers.h"
     15 #include "clang/StaticAnalyzer/Core/Checker.h"
     16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
     17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
     18 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
     19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
     20 #include "llvm/Support/raw_ostream.h"
     21 
     22 using namespace clang;
     23 using namespace ento;
     24 
     25 namespace {
     26 class UndefCapturedBlockVarChecker
     27   : public Checker< check::PostStmt<BlockExpr> > {
     28  mutable llvm::OwningPtr<BugType> BT;
     29 
     30 public:
     31   void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
     32 };
     33 } // end anonymous namespace
     34 
     35 static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
     36                                                     const VarDecl *VD){
     37   if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S))
     38     if (BR->getDecl() == VD)
     39       return BR;
     40 
     41   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
     42        I!=E; ++I)
     43     if (const Stmt *child = *I) {
     44       const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
     45       if (BR)
     46         return BR;
     47     }
     48 
     49   return NULL;
     50 }
     51 
     52 void
     53 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
     54                                             CheckerContext &C) const {
     55   if (!BE->getBlockDecl()->hasCaptures())
     56     return;
     57 
     58   const ProgramState *state = C.getState();
     59   const BlockDataRegion *R =
     60     cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
     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.
     68     const VarRegion *VR = *I;
     69     const VarDecl *VD = VR->getDecl();
     70 
     71     if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
     72       continue;
     73 
     74     // Get the VarRegion associated with VD in the local stack frame.
     75     const LocationContext *LC = C.getPredecessor()->getLocationContext();
     76     VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
     77     SVal VRVal = state->getSVal(VR);
     78 
     79     if (VRVal.isUndef())
     80       if (ExplodedNode *N = C.generateSink()) {
     81         if (!BT)
     82           BT.reset(new BuiltinBug("uninitialized variable captured by block"));
     83 
     84         // Generate a bug report.
     85         llvm::SmallString<128> buf;
     86         llvm::raw_svector_ostream os(buf);
     87 
     88         os << "Variable '" << VD->getName()
     89            << "' is uninitialized when captured by block";
     90 
     91         BugReport *R = new BugReport(*BT, os.str(), N);
     92         if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
     93           R->addRange(Ex->getSourceRange());
     94         R->addVisitor(new FindLastStoreBRVisitor(VRVal, VR));
     95         // need location of block
     96         C.EmitReport(R);
     97       }
     98   }
     99 }
    100 
    101 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
    102   mgr.registerChecker<UndefCapturedBlockVarChecker>();
    103 }
    104