UndefCapturedBlockVarChecker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This checker detects blocks that capture uninitialized values.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ClangSACheckers.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"
  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 (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 = llvm::make_unique<BugReport>(*BT, os.str(), N);
  72. if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
  73. R->addRange(Ex->getSourceRange());
  74. R->addVisitor(llvm::make_unique<FindLastStoreBRVisitor>(
  75. *V, VR, /*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. }