UndefCapturedBlockVarChecker.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This checker detects blocks that capture uninitialized values.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  13. #include "clang/AST/Attr.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <optional>
  22. using namespace clang;
  23. using namespace ento;
  24. namespace {
  25. class UndefCapturedBlockVarChecker
  26. : public Checker< check::PostStmt<BlockExpr> > {
  27. mutable std::unique_ptr<BugType> BT;
  28. public:
  29. void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
  30. };
  31. } // end anonymous namespace
  32. static const DeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
  33. const VarDecl *VD) {
  34. if (const DeclRefExpr *BR = dyn_cast<DeclRefExpr>(S))
  35. if (BR->getDecl() == VD)
  36. return BR;
  37. for (const Stmt *Child : S->children())
  38. if (Child)
  39. if (const DeclRefExpr *BR = FindBlockDeclRefExpr(Child, VD))
  40. return BR;
  41. return nullptr;
  42. }
  43. void
  44. UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
  45. CheckerContext &C) const {
  46. if (!BE->getBlockDecl()->hasCaptures())
  47. return;
  48. ProgramStateRef state = C.getState();
  49. auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
  50. BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
  51. E = R->referenced_vars_end();
  52. for (; I != E; ++I) {
  53. // This VarRegion is the region associated with the block; we need
  54. // the one associated with the encompassing context.
  55. const VarRegion *VR = I.getCapturedRegion();
  56. const VarDecl *VD = VR->getDecl();
  57. if (VD->hasAttr<BlocksAttr>() || !VD->hasLocalStorage())
  58. continue;
  59. // Get the VarRegion associated with VD in the local stack frame.
  60. if (std::optional<UndefinedVal> V =
  61. state->getSVal(I.getOriginalRegion()).getAs<UndefinedVal>()) {
  62. if (ExplodedNode *N = C.generateErrorNode()) {
  63. if (!BT)
  64. BT.reset(
  65. new BuiltinBug(this, "uninitialized variable captured by block"));
  66. // Generate a bug report.
  67. SmallString<128> buf;
  68. llvm::raw_svector_ostream os(buf);
  69. os << "Variable '" << VD->getName()
  70. << "' is uninitialized when captured by block";
  71. auto R = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
  72. if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
  73. R->addRange(Ex->getSourceRange());
  74. bugreporter::trackStoredValue(*V, VR, *R,
  75. {bugreporter::TrackingKind::Thorough,
  76. /*EnableNullFPSuppression*/ false});
  77. R->disablePathPruning();
  78. // need location of block
  79. C.emitReport(std::move(R));
  80. }
  81. }
  82. }
  83. }
  84. void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
  85. mgr.registerChecker<UndefCapturedBlockVarChecker>();
  86. }
  87. bool ento::shouldRegisterUndefCapturedBlockVarChecker(const CheckerManager &mgr) {
  88. return true;
  89. }