• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
FindBlockDeclRefExpr(const Stmt * S,const VarDecl * VD)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 nullptr;
52 }
53 
54 void
checkPostStmt(const BlockExpr * BE,CheckerContext & C) const55 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->hasAttr<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(
83               new BuiltinBug(this, "uninitialized variable captured by block"));
84 
85         // Generate a bug report.
86         SmallString<128> buf;
87         llvm::raw_svector_ostream os(buf);
88 
89         os << "Variable '" << VD->getName()
90            << "' is uninitialized when captured by block";
91 
92         BugReport *R = new BugReport(*BT, os.str(), N);
93         if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
94           R->addRange(Ex->getSourceRange());
95         R->addVisitor(new FindLastStoreBRVisitor(*V, VR,
96                                              /*EnableNullFPSuppression*/false));
97         R->disablePathPruning();
98         // need location of block
99         C.emitReport(R);
100       }
101     }
102   }
103 }
104 
registerUndefCapturedBlockVarChecker(CheckerManager & mgr)105 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
106   mgr.registerChecker<UndefCapturedBlockVarChecker>();
107 }
108