clang 24.0.0git
LifetimeModeling.cpp
Go to the documentation of this file.
1#include "LifetimeModeling.h"
2#include "clang/AST/Attr.h"
9#include "llvm/Support/raw_ostream.h"
10
11using namespace clang;
12using namespace ento;
13
14REGISTER_SET_FACTORY_WITH_PROGRAMSTATE(LifetimeSourceSet, const MemRegion *)
15REGISTER_MAP_WITH_PROGRAMSTATE(LifetimeBoundMap, SVal, LifetimeSourceSet)
16
17REGISTER_SET_WITH_PROGRAMSTATE(DeallocatedSourceSet, const MemRegion *)
18REGISTER_SET_WITH_PROGRAMSTATE(ReportedDeadRegions, const MemRegion *)
19
20namespace {
21
22class LifetimeModeling
23 : public Checker<check::PostCall, check::DeadSymbols,
24 check::PreStmt<DeclStmt>, check::LifetimeEnd> {
25public:
26 void printState(raw_ostream &Out, ProgramStateRef State, const char *NL,
27 const char *Sep) const override;
28 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
29 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
30 void checkLifetimeEnd(const VarDecl *VD, CheckerContext &C) const;
31 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
32};
33
34} // namespace
35
36static bool isDanglingStackSource(const MemRegion *Source,
38 // FIXME: The checker currently handles stack-region sources. Other
39 // region kinds require separate methodology. For example, heap
40 // regions do not go out of scope at the end of a stack frame, so
41 // in order to detect those type of dangling sources the function
42 // needs to be expanded to an event-driven approach as well.
43 if (const auto *StackSpace =
44 Source->getMemorySpaceAs<StackSpaceRegion>(State)) {
45 const StackFrame *SF = StackSpace->getStackFrame();
46 const StackFrame *CurrentSF = C.getStackFrame();
47 // If any frame on the current stack belongs to a destructor
48 // the warning should be suppressed. When a lifetimebound method
49 // is called from a destructor then its return value is not expected
50 // to outlive the object being destroyed.
51 if (llvm::any_of(C.stackframes(), [&](const StackFrame &Frame) {
52 return isa<CXXDestructorDecl>(Frame.getDecl());
53 })) {
54 return false;
55 }
56 // Only a source whose frame is still live on the current stack can
57 // dangle. If that frame is not on the stack then the source outlives
58 // the returned value. The source is still alive when the returned value
59 // is used, so it does not dangle.
60 if (is_contained(make_pointer_range(C.stackframes()), SF)) {
61 if (SF == CurrentSF || !SF->isParentOf(CurrentSF))
62 return true;
63 }
64 }
65 return false;
66}
67
69 SVal Val, ProgramStateRef State, CheckerContext &C) {
70 std::vector<const MemRegion *> Regions;
71 if (auto *SourceSet = State->get<LifetimeBoundMap>(Val)) {
72 for (const MemRegion *Region : *SourceSet) {
73 if (isDanglingStackSource(Region, State, C))
74 Regions.push_back(Region);
75 }
76 }
77 return Regions;
78}
79
81 SVal Val) {
82 return State->get<LifetimeBoundMap>(Val) != nullptr;
83}
84
86 const MemRegion *Region) {
87 return State->contains<DeallocatedSourceSet>(Region->getBaseRegion());
88}
89
91 const MemRegion *Region) {
92 ProgramStateRef NewState =
93 State->add<ReportedDeadRegions>(Region->getBaseRegion());
94
95 return (NewState != State) ? NewState : nullptr;
96}
97
99 const MemRegion *Source) {
100 LifetimeSourceSet::Factory &F = State->get_context<LifetimeSourceSet>();
101 const LifetimeSourceSet *LSet = State->get<LifetimeBoundMap>(RetVal);
102
103 LifetimeSourceSet Set = LSet ? *LSet : F.getEmptySet();
104 Set = F.add(Set, Source);
105 State = State->set<LifetimeBoundMap>(RetVal, Set);
106 return State;
107}
108
110 // FIXME: Once the checker supports heap allocation, more region kinds
111 // should be handled to produce the correct descriptive name.
112 if (const std::string RegName = Reg->getDescriptiveName(); !RegName.empty())
113 return RegName;
114 return "the region";
115}
116
117void LifetimeModeling::checkPostCall(const CallEvent &Call,
118 CheckerContext &C) const {
119 ProgramStateRef State = C.getState();
120
121 const auto *FC = dyn_cast<AnyFunctionCall>(&Call);
122 if (!FC)
123 return;
124
125 const FunctionDecl *FD = FC->getDecl();
126 if (!FD)
127 return;
128
129 SVal RetVal = Call.getReturnValue();
130
131 for (const ParmVarDecl *PVD : FD->parameters()) {
132 if (PVD->hasAttr<LifetimeBoundAttr>()) {
133 unsigned Idx = PVD->getFunctionScopeIndex();
134 SVal Arg = Call.getArgSVal(Idx);
135 if (const MemRegion *ArgValRegion = Arg.getAsRegion())
136 State = bindSource(State, RetVal, ArgValRegion);
137 }
138 }
139
140 const auto *IC = dyn_cast<CXXInstanceCall>(&Call);
142 if (const MemRegion *ThisRegion = IC->getCXXThisVal().getAsRegion())
143 State = bindSource(State, RetVal, ThisRegion);
144 }
145 C.addTransition(State);
146}
147
148void LifetimeModeling::checkLifetimeEnd(const VarDecl *VD,
149 CheckerContext &C) const {
150 ProgramStateRef State = C.getState();
151
152 SVal SourceVal = State->getLValue(VD, C.getStackFrame());
153 if (const MemRegion *SourceValRegion = SourceVal.getAsRegion()) {
154 State = State->add<DeallocatedSourceSet>(SourceValRegion);
155 C.addTransition(State);
156 }
157}
158
159void LifetimeModeling::checkPreStmt(const DeclStmt *DS,
160 CheckerContext &C) const {
161 ProgramStateRef State = C.getState();
162 for (const auto *I : DS->decls()) {
163 if (const VarDecl *VD = dyn_cast<VarDecl>(I)) {
164 SVal Val = State->getLValue(VD, C.getStackFrame());
165 if (const MemRegion *ValRegion = Val.getAsRegion())
166 State = State->remove<DeallocatedSourceSet>(ValRegion);
167 }
168 }
169 C.addTransition(State);
170}
171
172void LifetimeModeling::checkDeadSymbols(SymbolReaper &SymReaper,
173 CheckerContext &C) const {
174 ProgramStateRef State = C.getState();
175 LifetimeBoundMapTy LBMap = State->get<LifetimeBoundMap>();
176 DeallocatedSourceSetTy Sources = State->get<DeallocatedSourceSet>();
177 ReportedDeadRegionsTy Reported = State->get<ReportedDeadRegions>();
178
179 for (SVal Val : llvm::make_first_range(LBMap)) {
180 if (const auto *R = Val.getAsRegion(); R && SymReaper.isLiveRegion(R))
181 continue;
182
183 if (SymbolRef S = Val.getAsSymbol(/*IncludeBaseRegions=*/true);
184 S && SymReaper.isLive(S))
185 continue;
186
187 State = State->remove<LifetimeBoundMap>(Val);
188 }
189
190 for (const MemRegion *Region : Sources) {
191 if (!SymReaper.isLiveRegion(Region))
192 State = State->remove<DeallocatedSourceSet>(Region);
193 }
194
195 for (const MemRegion *Region : Reported) {
196 if (!SymReaper.isLiveRegion(Region))
197 State = State->remove<ReportedDeadRegions>(Region);
198 }
199 C.addTransition(State);
200}
201
202void LifetimeModeling::printState(raw_ostream &Out, ProgramStateRef State,
203 const char *NL, const char *Sep) const {
204 auto LBMap = State->get<LifetimeBoundMap>();
205 ReportedDeadRegionsTy Reported = State->get<ReportedDeadRegions>();
206
207 if (!LBMap.isEmpty()) {
208 Out << Sep << "LifetimeBound bindings:" << NL;
209 for (auto &&[OriginSym, SourceSet] : LBMap) {
210 for (const auto *Region : SourceSet)
211 Out << " Origin " << OriginSym << " contains Loan " << Region << NL;
212 }
213 }
214
215 if (!Reported.isEmpty()) {
216 Out << Sep << "Reported regions: " << NL;
217 for (const auto *Region : Reported) {
218 Out << " " << Region << NL;
219 }
220 }
221}
222
223// FIXME: Eventually move the debug checker to its own source file once
224// it has more functionality.
225namespace {
226class DebugLifetimeModeling : public Checker<eval::Call> {
227public:
228 bool evalCall(const CallEvent &Call, CheckerContext &C) const;
229 void analyzerDumpLifetimeOriginsOf(const CallEvent &Call,
230 CheckerContext &C) const;
231 const BugType BugMsg{this, "DebugLifetimeModeling", "DebugLifetimeModeling"};
232 using FnCheck = void (DebugLifetimeModeling::*)(const CallEvent &Call,
233 CheckerContext &C) const;
234
235 const CallDescriptionMap<FnCheck> Callbacks = {
236 {{CDM::SimpleFunc, {"clang_analyzer_dumpLifetimeOriginsOf"}},
237 &DebugLifetimeModeling::analyzerDumpLifetimeOriginsOf},
238 };
239};
240
241} // namespace
242
243bool DebugLifetimeModeling::evalCall(const CallEvent &Call,
244 CheckerContext &C) const {
245 if (!isa_and_nonnull<CallExpr>(Call.getOriginExpr()))
246 return false;
247
248 const FnCheck *Handler = Callbacks.lookup(Call);
249 if (!Handler)
250 return false;
251
252 (this->*(*Handler))(Call, C);
253 return true;
254}
255
256void DebugLifetimeModeling::analyzerDumpLifetimeOriginsOf(
257 const CallEvent &Call, CheckerContext &C) const {
258 ProgramStateRef State = C.getState();
259
260 if (Call.getNumArgs() != 1) {
261 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
262 auto BR = std::make_unique<PathSensitiveBugReport>(
263 BugMsg,
264 "clang_analyzer_dumpLifetimeOriginsOf requires exactly 1 argument",
265 N);
266 C.emitReport(std::move(BR));
267 }
268 return;
269 }
270
271 SVal ArgSVal = Call.getArgSVal(0);
272 const LifetimeSourceSet *SourceSet = State->get<LifetimeBoundMap>(ArgSVal);
273
274 if (!SourceSet)
275 return;
276
277 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
278 llvm::SmallVector<std::string> RegionNames =
279 to_vector(map_range(llvm::make_pointee_range(*SourceSet),
280 std::mem_fn(&MemRegion::getString)));
281 llvm::sort(RegionNames);
282
283 llvm::SmallString<128> Str;
284 llvm::raw_svector_ostream OS(Str);
285 OS << " Origin '" << ArgSVal << "' bound to ";
286 llvm::interleaveComma(RegionNames, OS,
287 [&](StringRef Name) { OS << "'" << Name << "'"; });
288 C.emitReport(std::make_unique<PathSensitiveBugReport>(BugMsg, OS.str(), N));
289 }
290}
291
292void ento::registerLifetimeModeling(CheckerManager &Mgr) {
293 Mgr.registerChecker<LifetimeModeling>();
294}
295
296bool ento::shouldRegisterLifetimeModeling(const CheckerManager &Mgr) {
297 return true;
298}
299
300void ento::registerDebugLifetimeModeling(CheckerManager &Mgr) {
301 Mgr.registerChecker<DebugLifetimeModeling>();
302}
303
304bool ento::shouldRegisterDebugLifetimeModeling(const CheckerManager &Mgr) {
305 return true;
306}
static bool isDanglingStackSource(const MemRegion *Source, ProgramStateRef State, CheckerContext &C)
static ProgramStateRef bindSource(ProgramStateRef State, SVal RetVal, const MemRegion *Source)
#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,...
#define REGISTER_SET_WITH_PROGRAMSTATE(Name, Elem)
Declares an immutable set of type NameTy, suitable for placement into the ProgramState.
decl_range decls()
Definition Stmt.h:1691
Represents a function declaration or definition.
Definition Decl.h:2058
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2904
Represents a parameter to a function.
Definition Decl.h:1819
It represents a stack frame of the call stack.
bool isParentOf(const StackFrame *SF) const
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
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:565
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
std::string getDescriptiveName(bool UseQuotes=true, bool AllowFallback=false) const
Get descriptive name for memory region.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
std::string getString() const
Get a string representation of a region for debug use.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition SVals.cpp:103
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
bool isLiveRegion(const MemRegion *region)
bool isLive(SymbolRef sym)
std::string getRegionName(const MemRegion *Reg)
Returns the descriptive name of the memory region or a placeholder if a descriptive name cannot be co...
bool isBoundToLifetimeSource(ProgramStateRef State, SVal Val)
Returns true if Val is a key in the LifetimeBoundMap.
bool isDeallocated(ProgramStateRef State, const MemRegion *Region)
Returns true if the underlying MemRegion is deallocated.
std::vector< const MemRegion * > getDanglingRegionsAfterReturn(SVal Source, ProgramStateRef State, CheckerContext &C)
Returns the set of lifetime sources bound to Source that are dangling stack regions.
ProgramStateRef markAsReported(ProgramStateRef State, const MemRegion *Region)
Returns the updated State with R marked as reported if R is seen the first time.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
Top level wrappers for InstallAPI frontend operations.