DeleteWithNonVirtualDtorChecker.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===-- DeleteWithNonVirtualDtorChecker.cpp -----------------------*- 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. // Defines a checker for the OOP52-CPP CERT rule: Do not delete a polymorphic
  10. // object without a virtual destructor.
  11. //
  12. // Diagnostic flags -Wnon-virtual-dtor and -Wdelete-non-virtual-dtor report if
  13. // an object with a virtual function but a non-virtual destructor exists or is
  14. // deleted, respectively.
  15. //
  16. // This check exceeds them by comparing the dynamic and static types of the
  17. // object at the point of destruction and only warns if it happens through a
  18. // pointer to a base type without a virtual destructor. The check places a note
  19. // at the last point where the conversion from derived to base happened.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  23. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  24. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  25. #include "clang/StaticAnalyzer/Core/Checker.h"
  26. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  27. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  28. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  29. #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
  30. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  31. using namespace clang;
  32. using namespace ento;
  33. namespace {
  34. class DeleteWithNonVirtualDtorChecker
  35. : public Checker<check::PreStmt<CXXDeleteExpr>> {
  36. mutable std::unique_ptr<BugType> BT;
  37. class DeleteBugVisitor : public BugReporterVisitor {
  38. public:
  39. DeleteBugVisitor() : Satisfied(false) {}
  40. void Profile(llvm::FoldingSetNodeID &ID) const override {
  41. static int X = 0;
  42. ID.AddPointer(&X);
  43. }
  44. PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
  45. BugReporterContext &BRC,
  46. PathSensitiveBugReport &BR) override;
  47. private:
  48. bool Satisfied;
  49. };
  50. public:
  51. void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
  52. };
  53. } // end anonymous namespace
  54. void DeleteWithNonVirtualDtorChecker::checkPreStmt(const CXXDeleteExpr *DE,
  55. CheckerContext &C) const {
  56. const Expr *DeletedObj = DE->getArgument();
  57. const MemRegion *MR = C.getSVal(DeletedObj).getAsRegion();
  58. if (!MR)
  59. return;
  60. const auto *BaseClassRegion = MR->getAs<TypedValueRegion>();
  61. const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>();
  62. if (!BaseClassRegion || !DerivedClassRegion)
  63. return;
  64. const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
  65. const auto *DerivedClass =
  66. DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
  67. if (!BaseClass || !DerivedClass)
  68. return;
  69. if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
  70. return;
  71. if (BaseClass->getDestructor()->isVirtual())
  72. return;
  73. if (!DerivedClass->isDerivedFrom(BaseClass))
  74. return;
  75. if (!BT)
  76. BT.reset(new BugType(this,
  77. "Destruction of a polymorphic object with no "
  78. "virtual destructor",
  79. "Logic error"));
  80. ExplodedNode *N = C.generateNonFatalErrorNode();
  81. if (!N)
  82. return;
  83. auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
  84. // Mark region of problematic base class for later use in the BugVisitor.
  85. R->markInteresting(BaseClassRegion);
  86. R->addVisitor(std::make_unique<DeleteBugVisitor>());
  87. C.emitReport(std::move(R));
  88. }
  89. PathDiagnosticPieceRef
  90. DeleteWithNonVirtualDtorChecker::DeleteBugVisitor::VisitNode(
  91. const ExplodedNode *N, BugReporterContext &BRC,
  92. PathSensitiveBugReport &BR) {
  93. // Stop traversal after the first conversion was found on a path.
  94. if (Satisfied)
  95. return nullptr;
  96. const Stmt *S = N->getStmtForDiagnostics();
  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. }
  124. void ento::registerDeleteWithNonVirtualDtorChecker(CheckerManager &mgr) {
  125. mgr.registerChecker<DeleteWithNonVirtualDtorChecker>();
  126. }
  127. bool ento::shouldRegisterDeleteWithNonVirtualDtorChecker(
  128. const CheckerManager &mgr) {
  129. return true;
  130. }