UndefCapturedBlockVarChecker.cpp 3.7 KB

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