10b57cec5SDimitry Andric // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This checker detects blocks that capture uninitialized values.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
140b57cec5SDimitry Andric #include "clang/AST/Attr.h"
150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
22bdd1243dSDimitry Andric #include <optional>
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric using namespace clang;
250b57cec5SDimitry Andric using namespace ento;
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric namespace {
280b57cec5SDimitry Andric class UndefCapturedBlockVarChecker
290b57cec5SDimitry Andric : public Checker< check::PostStmt<BlockExpr> > {
30*647cbc5dSDimitry Andric const BugType BT{this, "uninitialized variable captured by block"};
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric public:
330b57cec5SDimitry Andric void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
340b57cec5SDimitry Andric };
350b57cec5SDimitry Andric } // end anonymous namespace
360b57cec5SDimitry Andric
FindBlockDeclRefExpr(const Stmt * S,const VarDecl * VD)370b57cec5SDimitry Andric static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
380b57cec5SDimitry Andric const VarDecl *VD) {
390b57cec5SDimitry Andric if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
400b57cec5SDimitry Andric if (BR->getDecl() == VD)
410b57cec5SDimitry Andric return BR;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric for (const Stmt *Child : S->children())
440b57cec5SDimitry Andric if (Child)
450b57cec5SDimitry Andric if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
460b57cec5SDimitry Andric return BR;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric return nullptr;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric void
checkPostStmt(const BlockExpr * BE,CheckerContext & C) const520b57cec5SDimitry Andric UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
530b57cec5SDimitry Andric CheckerContext &C) const {
540b57cec5SDimitry Andric if (!BE->getBlockDecl()->hasCaptures())
550b57cec5SDimitry Andric return;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric ProgramStateRef state = C.getState();
580b57cec5SDimitry Andric auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
590b57cec5SDimitry Andric
6006c3fb27SDimitry Andric for (auto Var : R->referenced_vars()) {
610b57cec5SDimitry Andric // This VarRegion is the region associated with the block; we need
620b57cec5SDimitry Andric // the one associated with the encompassing context.
6306c3fb27SDimitry Andric const VarRegion *VR = Var.getCapturedRegion();
640b57cec5SDimitry Andric const VarDecl *VD = VR->getDecl();
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
670b57cec5SDimitry Andric continue;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Get the VarRegion associated with VD in the local stack frame.
70bdd1243dSDimitry Andric if (std::optional<UndefinedVal> V =
7106c3fb27SDimitry Andric state->getSVal(Var.getOriginalRegion()).getAs<UndefinedVal>()) {
720b57cec5SDimitry Andric if (ExplodedNode *N = C.generateErrorNode()) {
730b57cec5SDimitry Andric // Generate a bug report.
740b57cec5SDimitry Andric SmallString<128> buf;
750b57cec5SDimitry Andric llvm::raw_svector_ostream os(buf);
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric os << "Variable '" << VD->getName()
780b57cec5SDimitry Andric << "' is uninitialized when captured by block";
790b57cec5SDimitry Andric
80*647cbc5dSDimitry Andric auto R = std::make_unique<PathSensitiveBugReport>(BT, os.str(), N);
810b57cec5SDimitry Andric if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
820b57cec5SDimitry Andric R->addRange(Ex->getSourceRange());
83fe6060f1SDimitry Andric bugreporter::trackStoredValue(*V, VR, *R,
84fe6060f1SDimitry Andric {bugreporter::TrackingKind::Thorough,
85fe6060f1SDimitry Andric /*EnableNullFPSuppression*/ false});
860b57cec5SDimitry Andric R->disablePathPruning();
870b57cec5SDimitry Andric // need location of block
880b57cec5SDimitry Andric C.emitReport(std::move(R));
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
registerUndefCapturedBlockVarChecker(CheckerManager & mgr)940b57cec5SDimitry Andric void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
950b57cec5SDimitry Andric mgr.registerChecker<UndefCapturedBlockVarChecker>();
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
shouldRegisterUndefCapturedBlockVarChecker(const CheckerManager & mgr)985ffd83dbSDimitry Andric bool ento::shouldRegisterUndefCapturedBlockVarChecker(const CheckerManager &mgr) {
990b57cec5SDimitry Andric return true;
1000b57cec5SDimitry Andric }
101