1 // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This checker detects blocks that capture uninitialized values.
11 //===----------------------------------------------------------------------===//
13 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
27 class UndefCapturedBlockVarChecker
28 : public Checker< check::PostStmt<BlockExpr> > {
29 mutable std::unique_ptr<BugType> BT;
32 void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
34 } // end anonymous namespace
36 static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
38 if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
39 if (BR->getDecl() == VD)
42 for (const Stmt *Child : S->children())
44 if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
51 UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
52 CheckerContext &C) const {
53 if (!BE->getBlockDecl()->hasCaptures())
56 ProgramStateRef state = C.getState();
57 auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
59 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
60 E = R->referenced_vars_end();
63 // This VarRegion is the region associated with the block; we need
64 // the one associated with the encompassing context.
65 const VarRegion *VR = I.getCapturedRegion();
66 const VarDecl *VD = VR->getDecl();
68 if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
71 // Get the VarRegion associated with VD in the local stack frame.
72 if (Optional<UndefinedVal> V =
73 state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
74 if (ExplodedNode *N = C.generateErrorNode()) {
77 new BuiltinBug(this, "uninitialized variable captured by block"));
79 // Generate a bug report.
81 llvm::raw_svector_ostream os(buf);
83 os << "Variable '" << VD->getName()
84 << "' is uninitialized when captured by block";
86 auto R = llvm::make_unique<BugReport>(*BT, os.str(), N);
87 if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
88 R->addRange(Ex->getSourceRange());
89 R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
90 *V, VR, /*EnableNullFPSuppression*/ false));
91 R->disablePathPruning();
92 // need location of block
93 C.emitReport(std::move(R));
99 void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
100 mgr.registerChecker<UndefCapturedBlockVarChecker>();
103 bool ento::shouldRegisterUndefCapturedBlockVarChecker(const LangOptions &LO) {