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/AST/Attr.h"
     16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
     17 #include "clang/StaticAnalyzer/Core/Checker.h"
     18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
     19 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
     20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
     21 #include "llvm/ADT/SmallString.h"
     22 #include "llvm/Support/raw_ostream.h"
     23 
     24 using namespace clang;
     25 using namespace ento;
     26 
     27 namespace {
     28 class UndefCapturedBlockVarChecker
     29   : public Checker< check::PostStmt<BlockExpr> > {
     30  mutable OwningPtr<BugType> BT;
     31 
     32 public:
     33   void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
     34 };
     35 } // end anonymous namespace
     36 
     37 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
     38                                                const VarDecl *VD) {
     39   if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
     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) {
     46       const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
     47       if (BR)
     48         return BR;
     49     }
     50 
     51   return NULL;
     52 }
     53 
     54 void
     55 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
     56                                             CheckerContext &C) const {
     57   if (!BE->getBlockDecl()->hasCaptures())
     58     return;
     59 
     60   ProgramStateRef state = C.getState();
     61   const BlockDataRegion *R =
     62     cast<BlockDataRegion>(state->getSVal(BE,
     63                                          C.getLocationContext()).getAsRegion());
     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.
     71     const VarRegion *VR = I.getCapturedRegion();
     72     const VarDecl *VD = VR->getDecl();
     73 
     74     if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
     75       continue;
     76 
     77     // Get the VarRegion associated with VD in the local stack frame.
     78     if (Optional<UndefinedVal> V =
     79           state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
     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         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(*V, VR,
     95                                              /*EnableNullFPSuppression*/false));
     96         R->disablePathPruning();
     97         // need location of block
     98         C.emitReport(R);
     99       }
    100     }
    101   }
    102 }
    103 
    104 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
    105   mgr.registerChecker<UndefCapturedBlockVarChecker>();
    106 }
    107