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 std::unique_ptr<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 (const Stmt *Child : S->children())
     44     if (Child)
     45       if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
     46         return BR;
     47 
     48   return nullptr;
     49 }
     50 
     51 void
     52 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
     53                                             CheckerContext &C) const {
     54   if (!BE->getBlockDecl()->hasCaptures())
     55     return;
     56 
     57   ProgramStateRef state = C.getState();
     58   const BlockDataRegion *R =
     59     cast<BlockDataRegion>(state->getSVal(BE,
     60                                          C.getLocationContext()).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.getCapturedRegion();
     69     const VarDecl *VD = VR->getDecl();
     70 
     71     if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
     72       continue;
     73 
     74     // Get the VarRegion associated with VD in the local stack frame.
     75     if (Optional<UndefinedVal> V =
     76           state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
     77       if (ExplodedNode *N = C.generateErrorNode()) {
     78         if (!BT)
     79           BT.reset(
     80               new BuiltinBug(this, "uninitialized variable captured by block"));
     81 
     82         // Generate a bug report.
     83         SmallString<128> buf;
     84         llvm::raw_svector_ostream os(buf);
     85 
     86         os << "Variable '" << VD->getName()
     87            << "' is uninitialized when captured by block";
     88 
     89         auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
     90         if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
     91           R->addRange(Ex->getSourceRange());
     92         R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
     93             *V, VR, /*EnableNullFPSuppression*/ false));
     94         R->disablePathPruning();
     95         // need location of block
     96         C.emitReport(std::move(R));
     97       }
     98     }
     99   }
    100 }
    101 
    102 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
    103   mgr.registerChecker<UndefCapturedBlockVarChecker>();
    104 }
    105