161e7adefSGabor Horvath //===-- DeleteWithNonVirtualDtorChecker.cpp -----------------------*- C++ -*--//
261e7adefSGabor Horvath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
661e7adefSGabor Horvath //
761e7adefSGabor Horvath //===----------------------------------------------------------------------===//
861e7adefSGabor Horvath //
961e7adefSGabor Horvath // Defines a checker for the OOP52-CPP CERT rule: Do not delete a polymorphic
1061e7adefSGabor Horvath // object without a virtual destructor.
1161e7adefSGabor Horvath //
1261e7adefSGabor Horvath // Diagnostic flags -Wnon-virtual-dtor and -Wdelete-non-virtual-dtor report if
1361e7adefSGabor Horvath // an object with a virtual function but a non-virtual destructor exists or is
1461e7adefSGabor Horvath // deleted, respectively.
1561e7adefSGabor Horvath //
1661e7adefSGabor Horvath // This check exceeds them by comparing the dynamic and static types of the
1761e7adefSGabor Horvath // object at the point of destruction and only warns if it happens through a
1861e7adefSGabor Horvath // pointer to a base type without a virtual destructor. The check places a note
1961e7adefSGabor Horvath // at the last point where the conversion from derived to base happened.
2061e7adefSGabor Horvath //
2161e7adefSGabor Horvath //===----------------------------------------------------------------------===//
2261e7adefSGabor Horvath
2376a21502SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
2461e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
2561e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
2661e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/Checker.h"
2761e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/CheckerManager.h"
2861e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
2961e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
30e4bf456fSCsaba Dabis #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
3161e7adefSGabor Horvath #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
3261e7adefSGabor Horvath
3361e7adefSGabor Horvath using namespace clang;
3461e7adefSGabor Horvath using namespace ento;
3561e7adefSGabor Horvath
3661e7adefSGabor Horvath namespace {
3761e7adefSGabor Horvath class DeleteWithNonVirtualDtorChecker
3861e7adefSGabor Horvath : public Checker<check::PreStmt<CXXDeleteExpr>> {
3961e7adefSGabor Horvath mutable std::unique_ptr<BugType> BT;
4061e7adefSGabor Horvath
4170ec1dd1SGeorge Karpenkov class DeleteBugVisitor : public BugReporterVisitor {
4261e7adefSGabor Horvath public:
DeleteBugVisitor()4361e7adefSGabor Horvath DeleteBugVisitor() : Satisfied(false) {}
Profile(llvm::FoldingSetNodeID & ID) const4461e7adefSGabor Horvath void Profile(llvm::FoldingSetNodeID &ID) const override {
4561e7adefSGabor Horvath static int X = 0;
4661e7adefSGabor Horvath ID.AddPointer(&X);
4761e7adefSGabor Horvath }
486d716ef1SKristof Umann PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
4961e7adefSGabor Horvath BugReporterContext &BRC,
502f169e7cSArtem Dergachev PathSensitiveBugReport &BR) override;
5161e7adefSGabor Horvath
5261e7adefSGabor Horvath private:
5361e7adefSGabor Horvath bool Satisfied;
5461e7adefSGabor Horvath };
5561e7adefSGabor Horvath
5661e7adefSGabor Horvath public:
5761e7adefSGabor Horvath void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
5861e7adefSGabor Horvath };
5961e7adefSGabor Horvath } // end anonymous namespace
6061e7adefSGabor Horvath
checkPreStmt(const CXXDeleteExpr * DE,CheckerContext & C) const6161e7adefSGabor Horvath void DeleteWithNonVirtualDtorChecker::checkPreStmt(const CXXDeleteExpr *DE,
6261e7adefSGabor Horvath CheckerContext &C) const {
6361e7adefSGabor Horvath const Expr *DeletedObj = DE->getArgument();
6461e7adefSGabor Horvath const MemRegion *MR = C.getSVal(DeletedObj).getAsRegion();
6561e7adefSGabor Horvath if (!MR)
6661e7adefSGabor Horvath return;
6761e7adefSGabor Horvath
6861e7adefSGabor Horvath const auto *BaseClassRegion = MR->getAs<TypedValueRegion>();
6961e7adefSGabor Horvath const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>();
7061e7adefSGabor Horvath if (!BaseClassRegion || !DerivedClassRegion)
7161e7adefSGabor Horvath return;
7261e7adefSGabor Horvath
7361e7adefSGabor Horvath const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
7461e7adefSGabor Horvath const auto *DerivedClass =
7561e7adefSGabor Horvath DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
7661e7adefSGabor Horvath if (!BaseClass || !DerivedClass)
7761e7adefSGabor Horvath return;
7861e7adefSGabor Horvath
7961e7adefSGabor Horvath if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
8061e7adefSGabor Horvath return;
8161e7adefSGabor Horvath
8261e7adefSGabor Horvath if (BaseClass->getDestructor()->isVirtual())
8361e7adefSGabor Horvath return;
8461e7adefSGabor Horvath
8561e7adefSGabor Horvath if (!DerivedClass->isDerivedFrom(BaseClass))
8661e7adefSGabor Horvath return;
8761e7adefSGabor Horvath
8861e7adefSGabor Horvath if (!BT)
8961e7adefSGabor Horvath BT.reset(new BugType(this,
9061e7adefSGabor Horvath "Destruction of a polymorphic object with no "
9161e7adefSGabor Horvath "virtual destructor",
9261e7adefSGabor Horvath "Logic error"));
9361e7adefSGabor Horvath
9461e7adefSGabor Horvath ExplodedNode *N = C.generateNonFatalErrorNode();
95*9d69072fSKirstóf Umann if (!N)
96*9d69072fSKirstóf Umann return;
9772649423SKristof Umann auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
9861e7adefSGabor Horvath
9961e7adefSGabor Horvath // Mark region of problematic base class for later use in the BugVisitor.
10061e7adefSGabor Horvath R->markInteresting(BaseClassRegion);
1012b3d49b6SJonas Devlieghere R->addVisitor(std::make_unique<DeleteBugVisitor>());
10261e7adefSGabor Horvath C.emitReport(std::move(R));
10361e7adefSGabor Horvath }
10461e7adefSGabor Horvath
1056d716ef1SKristof Umann PathDiagnosticPieceRef
VisitNode(const ExplodedNode * N,BugReporterContext & BRC,PathSensitiveBugReport & BR)10661e7adefSGabor Horvath DeleteWithNonVirtualDtorChecker::DeleteBugVisitor::VisitNode(
1072f169e7cSArtem Dergachev const ExplodedNode *N, BugReporterContext &BRC,
1082f169e7cSArtem Dergachev PathSensitiveBugReport &BR) {
10961e7adefSGabor Horvath // Stop traversal after the first conversion was found on a path.
11061e7adefSGabor Horvath if (Satisfied)
11161e7adefSGabor Horvath return nullptr;
11261e7adefSGabor Horvath
1136b85f8e9SArtem Dergachev const Stmt *S = N->getStmtForDiagnostics();
11461e7adefSGabor Horvath if (!S)
11561e7adefSGabor Horvath return nullptr;
11661e7adefSGabor Horvath
11761e7adefSGabor Horvath const auto *CastE = dyn_cast<CastExpr>(S);
11861e7adefSGabor Horvath if (!CastE)
11961e7adefSGabor Horvath return nullptr;
12061e7adefSGabor Horvath
12161e7adefSGabor Horvath // Only interested in DerivedToBase implicit casts.
12261e7adefSGabor Horvath // Explicit casts can have different CastKinds.
12361e7adefSGabor Horvath if (const auto *ImplCastE = dyn_cast<ImplicitCastExpr>(CastE)) {
12461e7adefSGabor Horvath if (ImplCastE->getCastKind() != CK_DerivedToBase)
12561e7adefSGabor Horvath return nullptr;
12661e7adefSGabor Horvath }
12761e7adefSGabor Horvath
12861e7adefSGabor Horvath // Region associated with the current cast expression.
129d703ec94SGeorge Karpenkov const MemRegion *M = N->getSVal(CastE).getAsRegion();
13061e7adefSGabor Horvath if (!M)
13161e7adefSGabor Horvath return nullptr;
13261e7adefSGabor Horvath
13361e7adefSGabor Horvath // Check if target region was marked as problematic previously.
13461e7adefSGabor Horvath if (!BR.isInteresting(M))
13561e7adefSGabor Horvath return nullptr;
13661e7adefSGabor Horvath
13761e7adefSGabor Horvath // Stop traversal on this path.
13861e7adefSGabor Horvath Satisfied = true;
13961e7adefSGabor Horvath
14061e7adefSGabor Horvath SmallString<256> Buf;
14161e7adefSGabor Horvath llvm::raw_svector_ostream OS(Buf);
14261e7adefSGabor Horvath OS << "Conversion from derived to base happened here";
14361e7adefSGabor Horvath PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
14461e7adefSGabor Horvath N->getLocationContext());
1458535b8ecSArtem Dergachev return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
14661e7adefSGabor Horvath }
14761e7adefSGabor Horvath
registerDeleteWithNonVirtualDtorChecker(CheckerManager & mgr)14861e7adefSGabor Horvath void ento::registerDeleteWithNonVirtualDtorChecker(CheckerManager &mgr) {
14961e7adefSGabor Horvath mgr.registerChecker<DeleteWithNonVirtualDtorChecker>();
15061e7adefSGabor Horvath }
151058a7a45SKristof Umann
shouldRegisterDeleteWithNonVirtualDtorChecker(const CheckerManager & mgr)152058a7a45SKristof Umann bool ento::shouldRegisterDeleteWithNonVirtualDtorChecker(
153bda3dd0dSKirstóf Umann const CheckerManager &mgr) {
154058a7a45SKristof Umann return true;
155058a7a45SKristof Umann }
156