clang 19.0.0git
InnerPointerChecker.cpp
Go to the documentation of this file.
1//=== InnerPointerChecker.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 a check that marks a raw pointer to a C++ container's
10// inner buffer released when the object is destroyed. This information can
11// be used by MallocChecker to detect use-after-free problems.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AllocationState.h"
16#include "InterCheckerAPI.h"
24
25using namespace clang;
26using namespace ento;
27
28// Associate container objects with a set of raw pointer symbols.
30REGISTER_MAP_WITH_PROGRAMSTATE(RawPtrMap, const MemRegion *, PtrSet)
31
32
33namespace {
34
35class InnerPointerChecker
36 : public Checker<check::DeadSymbols, check::PostCall> {
37
38 CallDescription AppendFn, AssignFn, AddressofFn, AddressofFn_, ClearFn,
39 CStrFn, DataFn, DataMemberFn, EraseFn, InsertFn, PopBackFn, PushBackFn,
40 ReplaceFn, ReserveFn, ResizeFn, ShrinkToFitFn, SwapFn;
41
42public:
43 class InnerPointerBRVisitor : public BugReporterVisitor {
44 SymbolRef PtrToBuf;
45
46 public:
47 InnerPointerBRVisitor(SymbolRef Sym) : PtrToBuf(Sym) {}
48
49 static void *getTag() {
50 static int Tag = 0;
51 return &Tag;
52 }
53
54 void Profile(llvm::FoldingSetNodeID &ID) const override {
55 ID.AddPointer(getTag());
56 }
57
58 PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
60 PathSensitiveBugReport &BR) override;
61
62 // FIXME: Scan the map once in the visitor's constructor and do a direct
63 // lookup by region.
64 bool isSymbolTracked(ProgramStateRef State, SymbolRef Sym) {
65 RawPtrMapTy Map = State->get<RawPtrMap>();
66 for (const auto &Entry : Map) {
67 if (Entry.second.contains(Sym))
68 return true;
69 }
70 return false;
71 }
72 };
73
74 InnerPointerChecker()
75 : AppendFn({"std", "basic_string", "append"}),
76 AssignFn({"std", "basic_string", "assign"}),
77 AddressofFn({"std", "addressof"}), AddressofFn_({"std", "__addressof"}),
78 ClearFn({"std", "basic_string", "clear"}),
79 CStrFn({"std", "basic_string", "c_str"}), DataFn({"std", "data"}, 1),
80 DataMemberFn({"std", "basic_string", "data"}),
81 EraseFn({"std", "basic_string", "erase"}),
82 InsertFn({"std", "basic_string", "insert"}),
83 PopBackFn({"std", "basic_string", "pop_back"}),
84 PushBackFn({"std", "basic_string", "push_back"}),
85 ReplaceFn({"std", "basic_string", "replace"}),
86 ReserveFn({"std", "basic_string", "reserve"}),
87 ResizeFn({"std", "basic_string", "resize"}),
88 ShrinkToFitFn({"std", "basic_string", "shrink_to_fit"}),
89 SwapFn({"std", "basic_string", "swap"}) {}
90
91 /// Check whether the called member function potentially invalidates
92 /// pointers referring to the container object's inner buffer.
93 bool isInvalidatingMemberFunction(const CallEvent &Call) const;
94
95 /// Check whether the called function returns a raw inner pointer.
96 bool isInnerPointerAccessFunction(const CallEvent &Call) const;
97
98 /// Mark pointer symbols associated with the given memory region released
99 /// in the program state.
100 void markPtrSymbolsReleased(const CallEvent &Call, ProgramStateRef State,
101 const MemRegion *ObjRegion,
102 CheckerContext &C) const;
103
104 /// Standard library functions that take a non-const `basic_string` argument by
105 /// reference may invalidate its inner pointers. Check for these cases and
106 /// mark the pointers released.
107 void checkFunctionArguments(const CallEvent &Call, ProgramStateRef State,
108 CheckerContext &C) const;
109
110 /// Record the connection between raw pointers referring to a container
111 /// object's inner buffer and the object's memory region in the program state.
112 /// Mark potentially invalidated pointers released.
113 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
114
115 /// Clean up the program state map.
116 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
117};
118
119} // end anonymous namespace
120
121bool InnerPointerChecker::isInvalidatingMemberFunction(
122 const CallEvent &Call) const {
123 if (const auto *MemOpCall = dyn_cast<CXXMemberOperatorCall>(&Call)) {
124 OverloadedOperatorKind Opc = MemOpCall->getOriginExpr()->getOperator();
125 if (Opc == OO_Equal || Opc == OO_PlusEqual)
126 return true;
127 return false;
128 }
129 return isa<CXXDestructorCall>(Call) ||
130 matchesAny(Call, AppendFn, AssignFn, ClearFn, EraseFn, InsertFn,
131 PopBackFn, PushBackFn, ReplaceFn, ReserveFn, ResizeFn,
132 ShrinkToFitFn, SwapFn);
133}
134
135bool InnerPointerChecker::isInnerPointerAccessFunction(
136 const CallEvent &Call) const {
137 return matchesAny(Call, CStrFn, DataFn, DataMemberFn);
138}
139
140void InnerPointerChecker::markPtrSymbolsReleased(const CallEvent &Call,
141 ProgramStateRef State,
142 const MemRegion *MR,
143 CheckerContext &C) const {
144 if (const PtrSet *PS = State->get<RawPtrMap>(MR)) {
145 const Expr *Origin = Call.getOriginExpr();
146 for (const auto Symbol : *PS) {
147 // NOTE: `Origin` may be null, and will be stored so in the symbol's
148 // `RefState` in MallocChecker's `RegionState` program state map.
149 State = allocation_state::markReleased(State, Symbol, Origin);
150 }
151 State = State->remove<RawPtrMap>(MR);
152 C.addTransition(State);
153 return;
154 }
155}
156
157void InnerPointerChecker::checkFunctionArguments(const CallEvent &Call,
158 ProgramStateRef State,
159 CheckerContext &C) const {
160 if (const auto *FC = dyn_cast<AnyFunctionCall>(&Call)) {
161 const FunctionDecl *FD = FC->getDecl();
162 if (!FD || !FD->isInStdNamespace())
163 return;
164
165 for (unsigned I = 0, E = FD->getNumParams(); I != E; ++I) {
166 QualType ParamTy = FD->getParamDecl(I)->getType();
167 if (!ParamTy->isReferenceType() ||
168 ParamTy->getPointeeType().isConstQualified())
169 continue;
170
171 // In case of member operator calls, `this` is counted as an
172 // argument but not as a parameter.
173 bool isaMemberOpCall = isa<CXXMemberOperatorCall>(FC);
174 unsigned ArgI = isaMemberOpCall ? I+1 : I;
175
176 SVal Arg = FC->getArgSVal(ArgI);
177 const auto *ArgRegion =
178 dyn_cast_or_null<TypedValueRegion>(Arg.getAsRegion());
179 if (!ArgRegion)
180 continue;
181
182 // std::addressof functions accepts a non-const reference as an argument,
183 // but doesn't modify it.
184 if (matchesAny(Call, AddressofFn, AddressofFn_))
185 continue;
186
187 markPtrSymbolsReleased(Call, State, ArgRegion, C);
188 }
189 }
190}
191
192// [string.require]
193//
194// "References, pointers, and iterators referring to the elements of a
195// basic_string sequence may be invalidated by the following uses of that
196// basic_string object:
197//
198// -- As an argument to any standard library function taking a reference
199// to non-const basic_string as an argument. For example, as an argument to
200// non-member functions swap(), operator>>(), and getline(), or as an argument
201// to basic_string::swap().
202//
203// -- Calling non-const member functions, except operator[], at, front, back,
204// begin, rbegin, end, and rend."
205
206void InnerPointerChecker::checkPostCall(const CallEvent &Call,
207 CheckerContext &C) const {
208 ProgramStateRef State = C.getState();
209
210 // TODO: Do we need these to be typed?
211 const TypedValueRegion *ObjRegion = nullptr;
212
213 if (const auto *ICall = dyn_cast<CXXInstanceCall>(&Call)) {
214 ObjRegion = dyn_cast_or_null<TypedValueRegion>(
215 ICall->getCXXThisVal().getAsRegion());
216
217 // Check [string.require] / second point.
218 if (isInvalidatingMemberFunction(Call)) {
219 markPtrSymbolsReleased(Call, State, ObjRegion, C);
220 return;
221 }
222 }
223
224 if (isInnerPointerAccessFunction(Call)) {
225
226 if (isa<SimpleFunctionCall>(Call)) {
227 // NOTE: As of now, we only have one free access function: std::data.
228 // If we add more functions like this in the list, hardcoded
229 // argument index should be changed.
230 ObjRegion =
231 dyn_cast_or_null<TypedValueRegion>(Call.getArgSVal(0).getAsRegion());
232 }
233
234 if (!ObjRegion)
235 return;
236
237 SVal RawPtr = Call.getReturnValue();
238 if (SymbolRef Sym = RawPtr.getAsSymbol(/*IncludeBaseRegions=*/true)) {
239 // Start tracking this raw pointer by adding it to the set of symbols
240 // associated with this container object in the program state map.
241
242 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
243 const PtrSet *SetPtr = State->get<RawPtrMap>(ObjRegion);
244 PtrSet Set = SetPtr ? *SetPtr : F.getEmptySet();
245 assert(C.wasInlined || !Set.contains(Sym));
246 Set = F.add(Set, Sym);
247
248 State = State->set<RawPtrMap>(ObjRegion, Set);
249 C.addTransition(State);
250 }
251
252 return;
253 }
254
255 // Check [string.require] / first point.
256 checkFunctionArguments(Call, State, C);
257}
258
259void InnerPointerChecker::checkDeadSymbols(SymbolReaper &SymReaper,
260 CheckerContext &C) const {
261 ProgramStateRef State = C.getState();
262 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
263 RawPtrMapTy RPM = State->get<RawPtrMap>();
264 for (const auto &Entry : RPM) {
265 if (!SymReaper.isLiveRegion(Entry.first)) {
266 // Due to incomplete destructor support, some dead regions might
267 // remain in the program state map. Clean them up.
268 State = State->remove<RawPtrMap>(Entry.first);
269 }
270 if (const PtrSet *OldSet = State->get<RawPtrMap>(Entry.first)) {
271 PtrSet CleanedUpSet = *OldSet;
272 for (const auto Symbol : Entry.second) {
273 if (!SymReaper.isLive(Symbol))
274 CleanedUpSet = F.remove(CleanedUpSet, Symbol);
275 }
276 State = CleanedUpSet.isEmpty()
277 ? State->remove<RawPtrMap>(Entry.first)
278 : State->set<RawPtrMap>(Entry.first, CleanedUpSet);
279 }
280 }
281 C.addTransition(State);
282}
283
284namespace clang {
285namespace ento {
286namespace allocation_state {
287
288std::unique_ptr<BugReporterVisitor> getInnerPointerBRVisitor(SymbolRef Sym) {
289 return std::make_unique<InnerPointerChecker::InnerPointerBRVisitor>(Sym);
290}
291
293 RawPtrMapTy Map = State->get<RawPtrMap>();
294 for (const auto &Entry : Map) {
295 if (Entry.second.contains(Sym)) {
296 return Entry.first;
297 }
298 }
299 return nullptr;
300}
301
302} // end namespace allocation_state
303} // end namespace ento
304} // end namespace clang
305
306PathDiagnosticPieceRef InnerPointerChecker::InnerPointerBRVisitor::VisitNode(
308 if (!isSymbolTracked(N->getState(), PtrToBuf) ||
309 isSymbolTracked(N->getFirstPred()->getState(), PtrToBuf))
310 return nullptr;
311
312 const Stmt *S = N->getStmtForDiagnostics();
313 if (!S)
314 return nullptr;
315
316 const MemRegion *ObjRegion =
318 const auto *TypedRegion = cast<TypedValueRegion>(ObjRegion);
319 QualType ObjTy = TypedRegion->getValueType();
320
322 llvm::raw_svector_ostream OS(Buf);
323 OS << "Pointer to inner buffer of '" << ObjTy << "' obtained here";
325 N->getLocationContext());
326 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true);
327}
328
329void ento::registerInnerPointerChecker(CheckerManager &Mgr) {
331 Mgr.registerChecker<InnerPointerChecker>();
332}
333
334bool ento::shouldRegisterInnerPointerChecker(const CheckerManager &mgr) {
335 return true;
336}
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
#define REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set type Name and registers the factory for such sets in the program state,...
bool isInStdNamespace() const
Definition: DeclBase.cpp:403
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1971
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2707
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3692
A (possibly-)qualified type.
Definition: Type.h:738
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:7222
Stmt - This represents one statement.
Definition: Stmt.h:84
bool isReferenceType() const
Definition: Type.h:7414
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:694
QualType getType() const
Definition: Decl.h:717
const SourceManager & getSourceManager() const
Definition: BugReporter.h:737
BugReporterVisitors are used to add custom diagnostics along a path.
A CallDescription is a pattern that can be used to match calls based on the qualified name and the ar...
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:153
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const ProgramStateRef & getState() const
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
const LocationContext * getLocationContext() const
ExplodedNode * getFirstPred()
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:96
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition: SVals.cpp:104
const MemRegion * getAsRegion() const
Definition: SVals.cpp:120
Symbolic value.
Definition: SymExpr.h:30
A class responsible for cleaning up unused symbols.
bool isLiveRegion(const MemRegion *region)
bool isLive(SymbolRef sym)
TypedRegion - An abstract class representing regions that are typed.
Definition: MemRegion.h:506
TypedValueRegion - An abstract class representing regions having a typed value.
Definition: MemRegion.h:530
ProgramStateRef markReleased(ProgramStateRef State, SymbolRef Sym, const Expr *Origin)
std::unique_ptr< BugReporterVisitor > getInnerPointerBRVisitor(SymbolRef Sym)
This function provides an additional visitor that augments the bug report with information relevant t...
const MemRegion * getContainerObjRegion(ProgramStateRef State, SymbolRef Sym)
'Sym' represents a pointer to the inner buffer of a container object.
void registerInnerPointerCheckerAux(CheckerManager &Mgr)
Register the part of MallocChecker connected to InnerPointerChecker.
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.
Definition: OperatorKinds.h:21