clang API Documentation
00001 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This checker detects blocks that capture uninitialized values. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "ClangSACheckers.h" 00015 #include "clang/AST/Attr.h" 00016 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 00017 #include "clang/StaticAnalyzer/Core/Checker.h" 00018 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00019 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 00020 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 00021 #include "llvm/ADT/SmallString.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 00024 using namespace clang; 00025 using namespace ento; 00026 00027 namespace { 00028 class UndefCapturedBlockVarChecker 00029 : public Checker< check::PostStmt<BlockExpr> > { 00030 mutable std::unique_ptr<BugType> BT; 00031 00032 public: 00033 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const; 00034 }; 00035 } // end anonymous namespace 00036 00037 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S, 00038 const VarDecl *VD) { 00039 if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S)) 00040 if (BR->getDecl() == VD) 00041 return BR; 00042 00043 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); 00044 I!=E; ++I) 00045 if (const Stmt *child = *I) { 00046 const DeclRefExpr *BR = FindBlockDeclRefExpr(child, VD); 00047 if (BR) 00048 return BR; 00049 } 00050 00051 return nullptr; 00052 } 00053 00054 void 00055 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE, 00056 CheckerContext &C) const { 00057 if (!BE->getBlockDecl()->hasCaptures()) 00058 return; 00059 00060 ProgramStateRef state = C.getState(); 00061 const BlockDataRegion *R = 00062 cast<BlockDataRegion>(state->getSVal(BE, 00063 C.getLocationContext()).getAsRegion()); 00064 00065 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(), 00066 E = R->referenced_vars_end(); 00067 00068 for (; I != E; ++I) { 00069 // This VarRegion is the region associated with the block; we need 00070 // the one associated with the encompassing context. 00071 const VarRegion *VR = I.getCapturedRegion(); 00072 const VarDecl *VD = VR->getDecl(); 00073 00074 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage()) 00075 continue; 00076 00077 // Get the VarRegion associated with VD in the local stack frame. 00078 if (Optional<UndefinedVal> V = 00079 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) { 00080 if (ExplodedNode *N = C.generateSink()) { 00081 if (!BT) 00082 BT.reset( 00083 new BuiltinBug(this, "uninitialized variable captured by block")); 00084 00085 // Generate a bug report. 00086 SmallString<128> buf; 00087 llvm::raw_svector_ostream os(buf); 00088 00089 os << "Variable '" << VD->getName() 00090 << "' is uninitialized when captured by block"; 00091 00092 BugReport *R = new BugReport(*BT, os.str(), N); 00093 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD)) 00094 R->addRange(Ex->getSourceRange()); 00095 R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>( 00096 *V, VR, /*EnableNullFPSuppression*/ false)); 00097 R->disablePathPruning(); 00098 // need location of block 00099 C.emitReport(R); 00100 } 00101 } 00102 } 00103 } 00104 00105 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) { 00106 mgr.registerChecker<UndefCapturedBlockVarChecker>(); 00107 }