clang 22.0.0git
CXXDeleteChecker.cpp
Go to the documentation of this file.
1//=== CXXDeleteChecker.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// This file defines the following new checkers for C++ delete expressions:
10//
11// * DeleteWithNonVirtualDtorChecker
12// Defines a checker for the OOP52-CPP CERT rule: Do not delete a
13// polymorphic object without a virtual destructor.
14//
15// Diagnostic flags -Wnon-virtual-dtor and -Wdelete-non-virtual-dtor
16// report if an object with a virtual function but a non-virtual
17// destructor exists or is deleted, respectively.
18//
19// This check exceeds them by comparing the dynamic and static types of
20// the object at the point of destruction and only warns if it happens
21// through a pointer to a base type without a virtual destructor. The
22// check places a note at the last point where the conversion from
23// derived to base happened.
24//
25// * CXXArrayDeleteChecker
26// Defines a checker for the EXP51-CPP CERT rule: Do not delete an array
27// through a pointer of the incorrect type.
28//
29//===----------------------------------------------------------------------===//
30
37
38using namespace clang;
39using namespace ento;
40
41namespace {
42class CXXDeleteChecker : public Checker<check::PreStmt<CXXDeleteExpr>> {
43protected:
44 class PtrCastVisitor : public BugReporterVisitor {
45 public:
46 void Profile(llvm::FoldingSetNodeID &ID) const override {
47 static int X = 0;
48 ID.AddPointer(&X);
49 }
50 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
51 BugReporterContext &BRC,
52 PathSensitiveBugReport &BR) override;
53 };
54
55 virtual void
56 checkTypedDeleteExpr(const CXXDeleteExpr *DE, CheckerContext &C,
57 const TypedValueRegion *BaseClassRegion,
58 const SymbolicRegion *DerivedClassRegion) const = 0;
59
60public:
61 void checkPreStmt(const CXXDeleteExpr *DE, CheckerContext &C) const;
62};
63
64class DeleteWithNonVirtualDtorChecker : public CXXDeleteChecker {
65 const BugType BT{
66 this, "Destruction of a polymorphic object with no virtual destructor"};
67
68 void
69 checkTypedDeleteExpr(const CXXDeleteExpr *DE, CheckerContext &C,
70 const TypedValueRegion *BaseClassRegion,
71 const SymbolicRegion *DerivedClassRegion) const override;
72};
73
74class CXXArrayDeleteChecker : public CXXDeleteChecker {
75 const BugType BT{this,
76 "Deleting an array of polymorphic objects is undefined"};
77
78 void
79 checkTypedDeleteExpr(const CXXDeleteExpr *DE, CheckerContext &C,
80 const TypedValueRegion *BaseClassRegion,
81 const SymbolicRegion *DerivedClassRegion) const override;
82};
83} // namespace
84
85void CXXDeleteChecker::checkPreStmt(const CXXDeleteExpr *DE,
86 CheckerContext &C) const {
87 const Expr *DeletedObj = DE->getArgument();
88 const MemRegion *MR = C.getSVal(DeletedObj).getAsRegion();
89 if (!MR)
90 return;
91
92 OverloadedOperatorKind DeleteKind =
94
95 if (DeleteKind != OO_Delete && DeleteKind != OO_Array_Delete)
96 return;
97
98 const auto *BaseClassRegion = MR->getAs<TypedValueRegion>();
99 const auto *DerivedClassRegion = MR->getBaseRegion()->getAs<SymbolicRegion>();
100 if (!BaseClassRegion || !DerivedClassRegion)
101 return;
102
103 checkTypedDeleteExpr(DE, C, BaseClassRegion, DerivedClassRegion);
104}
105
106void DeleteWithNonVirtualDtorChecker::checkTypedDeleteExpr(
107 const CXXDeleteExpr *DE, CheckerContext &C,
108 const TypedValueRegion *BaseClassRegion,
109 const SymbolicRegion *DerivedClassRegion) const {
110 const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
111 const auto *DerivedClass =
112 DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
113 if (!BaseClass || !DerivedClass)
114 return;
115
116 if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
117 return;
118
119 if (BaseClass->getDestructor()->isVirtual())
120 return;
121
122 if (!DerivedClass->isDerivedFrom(BaseClass))
123 return;
124
125 ExplodedNode *N = C.generateNonFatalErrorNode();
126 if (!N)
127 return;
128 auto R = std::make_unique<PathSensitiveBugReport>(BT, BT.getDescription(), N);
129
130 // Mark region of problematic base class for later use in the BugVisitor.
131 R->markInteresting(BaseClassRegion);
132 R->addVisitor<PtrCastVisitor>();
133 C.emitReport(std::move(R));
134}
135
136void CXXArrayDeleteChecker::checkTypedDeleteExpr(
137 const CXXDeleteExpr *DE, CheckerContext &C,
138 const TypedValueRegion *BaseClassRegion,
139 const SymbolicRegion *DerivedClassRegion) const {
140 const auto *BaseClass = BaseClassRegion->getValueType()->getAsCXXRecordDecl();
141 const auto *DerivedClass =
142 DerivedClassRegion->getSymbol()->getType()->getPointeeCXXRecordDecl();
143 if (!BaseClass || !DerivedClass)
144 return;
145
146 if (!BaseClass->hasDefinition() || !DerivedClass->hasDefinition())
147 return;
148
149 if (DE->getOperatorDelete()->getOverloadedOperator() != OO_Array_Delete)
150 return;
151
152 if (!DerivedClass->isDerivedFrom(BaseClass))
153 return;
154
155 ExplodedNode *N = C.generateNonFatalErrorNode();
156 if (!N)
157 return;
158
159 SmallString<256> Buf;
160 llvm::raw_svector_ostream OS(Buf);
161
162 QualType SourceType = BaseClassRegion->getValueType();
163 QualType TargetType =
164 DerivedClassRegion->getSymbol()->getType()->getPointeeType();
165
166 OS << "Deleting an array of '" << TargetType.getAsString()
167 << "' objects as their base class '"
168 << SourceType.getAsString(C.getASTContext().getPrintingPolicy())
169 << "' is undefined";
170
171 auto R = std::make_unique<PathSensitiveBugReport>(BT, OS.str(), N);
172
173 // Mark region of problematic base class for later use in the BugVisitor.
174 R->markInteresting(BaseClassRegion);
175 R->addVisitor<PtrCastVisitor>();
176 C.emitReport(std::move(R));
177}
178
180CXXDeleteChecker::PtrCastVisitor::VisitNode(const ExplodedNode *N,
181 BugReporterContext &BRC,
182 PathSensitiveBugReport &BR) {
183 const Stmt *S = N->getStmtForDiagnostics();
184 if (!S)
185 return nullptr;
186
187 const auto *CastE = dyn_cast<CastExpr>(S);
188 if (!CastE)
189 return nullptr;
190
191 // FIXME: This way of getting base types does not support reference types.
192 QualType SourceType = CastE->getSubExpr()->getType()->getPointeeType();
193 QualType TargetType = CastE->getType()->getPointeeType();
194
195 if (SourceType.isNull() || TargetType.isNull() || SourceType == TargetType)
196 return nullptr;
197
198 // Region associated with the current cast expression.
199 const MemRegion *M = N->getSVal(CastE).getAsRegion();
200 if (!M)
201 return nullptr;
202
203 // Check if target region was marked as problematic previously.
204 if (!BR.isInteresting(M))
205 return nullptr;
206
207 SmallString<256> Buf;
208 llvm::raw_svector_ostream OS(Buf);
209
210 OS << "Casting from '" << SourceType.getAsString() << "' to '"
211 << TargetType.getAsString() << "' here";
212
213 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
214 N->getLocationContext());
215 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(),
216 /*addPosRange=*/true);
217}
218
219void ento::registerArrayDeleteChecker(CheckerManager &mgr) {
220 mgr.registerChecker<CXXArrayDeleteChecker>();
221}
222
223bool ento::shouldRegisterArrayDeleteChecker(const CheckerManager &mgr) {
224 return true;
225}
226
227void ento::registerDeleteWithNonVirtualDtorChecker(CheckerManager &mgr) {
228 mgr.registerChecker<DeleteWithNonVirtualDtorChecker>();
229}
230
231bool ento::shouldRegisterDeleteWithNonVirtualDtorChecker(
232 const CheckerManager &mgr) {
233 return true;
234}
#define X(type, name)
Definition Value.h:97
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2620
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2659
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any.
Definition Decl.cpp:4071
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition TypeBase.h:1332
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
Definition Type.cpp:1909
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:752
const SourceManager & getSourceManager() const
StringRef getDescription() const
Definition BugType.h:58
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
const LocationContext * getLocationContext() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
const RegionTy * getAs() const
Definition MemRegion.h:1416
bool isInteresting(SymbolRef sym) const
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
virtual QualType getType() const =0
SymbolRef getSymbol() const
It might return null.
Definition MemRegion.h:827
virtual QualType getValueType() const =0
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.