19a199699SDimitry Andric //===-- DeleteWithNonVirtualDtorChecker.cpp -----------------------*- C++ -*--//
29a199699SDimitry Andric //
39a199699SDimitry Andric // The LLVM Compiler Infrastructure
49a199699SDimitry Andric //
59a199699SDimitry Andric // This file is distributed under the University of Illinois Open Source
69a199699SDimitry Andric // License. See LICENSE.TXT for details.
79a199699SDimitry Andric //
89a199699SDimitry Andric //===----------------------------------------------------------------------===//
99a199699SDimitry Andric //
109a199699SDimitry Andric // Defines a checker for the OOP52-CPP CERT rule: Do not delete a polymorphic
119a199699SDimitry Andric // object without a virtual destructor.
129a199699SDimitry Andric //
139a199699SDimitry Andric // Diagnostic flags -Wnon-virtual-dtor and -Wdelete-non-virtual-dtor report if
149a199699SDimitry Andric // an object with a virtual function but a non-virtual destructor exists or is
159a199699SDimitry Andric // deleted, respectively.
169a199699SDimitry Andric //
179a199699SDimitry Andric // This check exceeds them by comparing the dynamic and static types of the
189a199699SDimitry Andric // object at the point of destruction and only warns if it happens through a
199a199699SDimitry Andric // pointer to a base type without a virtual destructor. The check places a note
209a199699SDimitry Andric // at the last point where the conversion from derived to base happened.
219a199699SDimitry Andric //
229a199699SDimitry Andric //===----------------------------------------------------------------------===//
239a199699SDimitry Andric
24*b5893f02SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
259a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
269a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
279a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
289a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
299a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
309a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
319a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
329a199699SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
339a199699SDimitry Andric
349a199699SDimitry Andric using namespace clang;
359a199699SDimitry Andric using namespace ento;
369a199699SDimitry Andric
379a199699SDimitry Andric namespace {
389a199699SDimitry Andric class DeleteWithNonVirtualDtorChecker
399a199699SDimitry Andric : public Checker<check::PreStmt<CXXDeleteExpr>> {
409a199699SDimitry Andric mutable std::unique_ptr<BugType> BT;
419a199699SDimitry Andric
424ba319b5SDimitry Andric class DeleteBugVisitor : public BugReporterVisitor {
439a199699SDimitry Andric public:
DeleteBugVisitor()449a199699SDimitry Andric DeleteBugVisitor() : Satisfied(false) {}
Profile(llvm::FoldingSetNodeID & ID) const459a199699SDimitry Andric void Profile(llvm::FoldingSetNodeID &ID) const override {
469a199699SDimitry Andric static int X = 0;
479a199699SDimitry Andric ID.AddPointer(&X);
489a199699SDimitry Andric }
499a199699SDimitry Andric std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
509a199699SDimitry Andric BugReporterContext &BRC,
519a199699SDimitry Andric BugReport &BR) override;
529a199699SDimitry Andric
539a199699SDimitry Andric private:
549a199699SDimitry Andric bool Satisfied;
559a199699SDimitry Andric };
569a199699SDimitry Andric
579a199699SDimitry Andric public:
589a199699SDimitry Andric void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
599a199699SDimitry Andric };
609a199699SDimitry Andric } // end anonymous namespace
619a199699SDimitry Andric
checkPreStmt(const CXXDeleteExpr * DE,CheckerContext & C) const629a199699SDimitry Andric void DeleteWithNonVirtualDtorChecker::checkPreStmt(const CXXDeleteExpr *DE,
639a199699SDimitry Andric CheckerContext &C) const {
649a199699SDimitry Andric const Expr *DeletedObj = DE->getArgument();
659a199699SDimitry Andric const MemRegion *MR = C.getSVal(DeletedObj).getAsRegion();
669a199699SDimitry Andric if (!MR)
679a199699SDimitry Andric return;
689a199699SDimitry Andric
699a199699SDimitry Andric const auto *BaseClassRegion = MR->getAs<TypedValueRegion>();
709a199699SDimitry Andric const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>();
719a199699SDimitry Andric if (!BaseClassRegion || !DerivedClassRegion)
729a199699SDimitry Andric return;
739a199699SDimitry Andric
749a199699SDimitry Andric const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
759a199699SDimitry Andric const auto *DerivedClass =
769a199699SDimitry Andric DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
779a199699SDimitry Andric if (!BaseClass || !DerivedClass)
789a199699SDimitry Andric return;
799a199699SDimitry Andric
809a199699SDimitry Andric if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
819a199699SDimitry Andric return;
829a199699SDimitry Andric
839a199699SDimitry Andric if (BaseClass->getDestructor()->isVirtual())
849a199699SDimitry Andric return;
859a199699SDimitry Andric
869a199699SDimitry Andric if (!DerivedClass->isDerivedFrom(BaseClass))
879a199699SDimitry Andric return;
889a199699SDimitry Andric
899a199699SDimitry Andric if (!BT)
909a199699SDimitry Andric BT.reset(new BugType(this,
919a199699SDimitry Andric "Destruction of a polymorphic object with no "
929a199699SDimitry Andric "virtual destructor",
939a199699SDimitry Andric "Logic error"));
949a199699SDimitry Andric
959a199699SDimitry Andric ExplodedNode *N = C.generateNonFatalErrorNode();
969a199699SDimitry Andric auto R = llvm::make_unique<BugReport>(*BT, BT->getName(), N);
979a199699SDimitry Andric
989a199699SDimitry Andric // Mark region of problematic base class for later use in the BugVisitor.
999a199699SDimitry Andric R->markInteresting(BaseClassRegion);
1009a199699SDimitry Andric R->addVisitor(llvm::make_unique<DeleteBugVisitor>());
1019a199699SDimitry Andric C.emitReport(std::move(R));
1029a199699SDimitry Andric }
1039a199699SDimitry Andric
1049a199699SDimitry Andric std::shared_ptr<PathDiagnosticPiece>
VisitNode(const ExplodedNode * N,BugReporterContext & BRC,BugReport & BR)1059a199699SDimitry Andric DeleteWithNonVirtualDtorChecker::DeleteBugVisitor::VisitNode(
106*b5893f02SDimitry Andric const ExplodedNode *N, BugReporterContext &BRC,
1079a199699SDimitry Andric BugReport &BR) {
1089a199699SDimitry Andric // Stop traversal after the first conversion was found on a path.
1099a199699SDimitry Andric if (Satisfied)
1109a199699SDimitry Andric return nullptr;
1119a199699SDimitry Andric
1129a199699SDimitry Andric const Stmt *S = PathDiagnosticLocation::getStmt(N);
1139a199699SDimitry Andric if (!S)
1149a199699SDimitry Andric return nullptr;
1159a199699SDimitry Andric
1169a199699SDimitry Andric const auto *CastE = dyn_cast<CastExpr>(S);
1179a199699SDimitry Andric if (!CastE)
1189a199699SDimitry Andric return nullptr;
1199a199699SDimitry Andric
1209a199699SDimitry Andric // Only interested in DerivedToBase implicit casts.
1219a199699SDimitry Andric // Explicit casts can have different CastKinds.
1229a199699SDimitry Andric if (const auto *ImplCastE = dyn_cast<ImplicitCastExpr>(CastE)) {
1239a199699SDimitry Andric if (ImplCastE->getCastKind() != CK_DerivedToBase)
1249a199699SDimitry Andric return nullptr;
1259a199699SDimitry Andric }
1269a199699SDimitry Andric
1279a199699SDimitry Andric // Region associated with the current cast expression.
1284ba319b5SDimitry Andric const MemRegion *M = N->getSVal(CastE).getAsRegion();
1299a199699SDimitry Andric if (!M)
1309a199699SDimitry Andric return nullptr;
1319a199699SDimitry Andric
1329a199699SDimitry Andric // Check if target region was marked as problematic previously.
1339a199699SDimitry Andric if (!BR.isInteresting(M))
1349a199699SDimitry Andric return nullptr;
1359a199699SDimitry Andric
1369a199699SDimitry Andric // Stop traversal on this path.
1379a199699SDimitry Andric Satisfied = true;
1389a199699SDimitry Andric
1399a199699SDimitry Andric SmallString<256> Buf;
1409a199699SDimitry Andric llvm::raw_svector_ostream OS(Buf);
1419a199699SDimitry Andric OS << "Conversion from derived to base happened here";
1429a199699SDimitry Andric PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
1439a199699SDimitry Andric N->getLocationContext());
1449a199699SDimitry Andric return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true,
1459a199699SDimitry Andric nullptr);
1469a199699SDimitry Andric }
1479a199699SDimitry Andric
registerDeleteWithNonVirtualDtorChecker(CheckerManager & mgr)1489a199699SDimitry Andric void ento::registerDeleteWithNonVirtualDtorChecker(CheckerManager &mgr) {
1499a199699SDimitry Andric mgr.registerChecker<DeleteWithNonVirtualDtorChecker>();
1509a199699SDimitry Andric }
151