DeleteWithNonVirtualDtorChecker.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //===-- DeleteWithNonVirtualDtorChecker.cpp -----------------------*- 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. // Defines a checker for the OOP52-CPP CERT rule: Do not delete a polymorphic
  11. // object without a virtual destructor.
  12. //
  13. // Diagnostic flags -Wnon-virtual-dtor and -Wdelete-non-virtual-dtor report if
  14. // an object with a virtual function but a non-virtual destructor exists or is
  15. // deleted, respectively.
  16. //
  17. // This check exceeds them by comparing the dynamic and static types of the
  18. // object at the point of destruction and only warns if it happens through a
  19. // pointer to a base type without a virtual destructor. The check places a note
  20. // at the last point where the conversion from derived to base happened.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include "ClangSACheckers.h"
  24. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  25. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  26. #include "clang/StaticAnalyzer/Core/Checker.h"
  27. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  28. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  29. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  30. #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
  31. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  32. using namespace clang;
  33. using namespace ento;
  34. namespace {
  35. class DeleteWithNonVirtualDtorChecker
  36. : public Checker<check::PreStmt<CXXDeleteExpr>> {
  37. mutable std::unique_ptr<BugType> BT;
  38. class DeleteBugVisitor : public BugReporterVisitorImpl<DeleteBugVisitor> {
  39. public:
  40. DeleteBugVisitor() : Satisfied(false) {}
  41. void Profile(llvm::FoldingSetNodeID &ID) const override {
  42. static int X = 0;
  43. ID.AddPointer(&X);
  44. }
  45. std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
  46. const ExplodedNode *PrevN,
  47. BugReporterContext &BRC,
  48. BugReport &BR) override;
  49. private:
  50. bool Satisfied;
  51. };
  52. public:
  53. void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
  54. };
  55. } // end anonymous namespace
  56. void DeleteWithNonVirtualDtorChecker::checkPreStmt(const CXXDeleteExpr *DE,
  57. CheckerContext &C) const {
  58. const Expr *DeletedObj = DE->getArgument();
  59. const MemRegion *MR = C.getSVal(DeletedObj).getAsRegion();
  60. if (!MR)
  61. return;
  62. const auto *BaseClassRegion = MR->getAs<TypedValueRegion>();
  63. const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>();
  64. if (!BaseClassRegion || !DerivedClassRegion)
  65. return;
  66. const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
  67. const auto *DerivedClass =
  68. DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
  69. if (!BaseClass || !DerivedClass)
  70. return;
  71. if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
  72. return;
  73. if (BaseClass->getDestructor()->isVirtual())
  74. return;
  75. if (!DerivedClass->isDerivedFrom(BaseClass))
  76. return;
  77. if (!BT)
  78. BT.reset(new BugType(this,
  79. "Destruction of a polymorphic object with no "
  80. "virtual destructor",
  81. "Logic error"));
  82. ExplodedNode *N = C.generateNonFatalErrorNode();
  83. auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N);
  84. // Mark region of problematic base class for later use in the BugVisitor.
  85. R->markInteresting(BaseClassRegion);
  86. R->addVisitor(llvm::make_unique<DeleteBugVisitor>());
  87. C.emitReport(std::move(R));
  88. }
  89. std::shared_ptr<PathDiagnosticPiece>
  90. DeleteWithNonVirtualDtorChecker::DeleteBugVisitor::VisitNode(
  91. const ExplodedNode *N, const ExplodedNode *PrevN, BugReporterContext &BRC,
  92. BugReport &BR) {
  93. // Stop traversal after the first conversion was found on a path.
  94. if (Satisfied)
  95. return nullptr;
  96. const Stmt *S = PathDiagnosticLocation::getStmt(N);
  97. if (!S)
  98. return nullptr;
  99. const auto *CastE = dyn_cast<CastExpr>(S);
  100. if (!CastE)
  101. return nullptr;
  102. // Only interested in DerivedToBase implicit casts.
  103. // Explicit casts can have different CastKinds.
  104. if (const auto *ImplCastE = dyn_cast<ImplicitCastExpr>(CastE)) {
  105. if (ImplCastE->getCastKind() != CK_DerivedToBase)
  106. return nullptr;
  107. }
  108. // Region associated with the current cast expression.
  109. const MemRegion *M = N->getSVal(CastE).getAsRegion();
  110. if (!M)
  111. return nullptr;
  112. // Check if target region was marked as problematic previously.
  113. if (!BR.isInteresting(M))
  114. return nullptr;
  115. // Stop traversal on this path.
  116. Satisfied = true;
  117. SmallString<256> Buf;
  118. llvm::raw_svector_ostream OS(Buf);
  119. OS << "Conversion from derived to base happened here";
  120. PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
  121. N->getLocationContext());
  122. return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true,
  123. nullptr);
  124. }
  125. void ento::registerDeleteWithNonVirtualDtorChecker(CheckerManager &mgr) {
  126. mgr.registerChecker<DeleteWithNonVirtualDtorChecker>();
  127. }