clang 23.0.0git
FactsGenerator.cpp
Go to the documentation of this file.
1//===- FactsGenerator.cpp - Lifetime Facts Generation -----------*- 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#include <cassert>
10#include <string>
11
18#include "llvm/Support/Casting.h"
19#include "llvm/Support/Signals.h"
20#include "llvm/Support/TimeProfiler.h"
21
23using llvm::isa_and_present;
24
25OriginList *FactsGenerator::getOriginsList(const ValueDecl &D) {
26 return FactMgr.getOriginMgr().getOrCreateList(&D);
27}
28OriginList *FactsGenerator::getOriginsList(const Expr &E) {
29 return FactMgr.getOriginMgr().getOrCreateList(&E);
30}
31
32/// Propagates origin information from Src to Dst through all levels of
33/// indirection, creating OriginFlowFacts at each level.
34///
35/// This function enforces a critical type-safety invariant: both lists must
36/// have the same shape (same depth/structure). This invariant ensures that
37/// origins flow only between compatible types during expression evaluation.
38///
39/// Examples:
40/// - `int* p = &x;` flows origins from `&x` (depth 1) to `p` (depth 1)
41/// - `int** pp = &p;` flows origins from `&p` (depth 2) to `pp` (depth 2)
42/// * Level 1: pp <- p's address
43/// * Level 2: (*pp) <- what p points to (i.e., &x)
44/// - `View v = obj;` flows origins from `obj` (depth 1) to `v` (depth 1)
45void FactsGenerator::flow(OriginList *Dst, OriginList *Src, bool Kill) {
46 if (!Dst)
47 return;
48 assert(Src &&
49 "Dst is non-null but Src is null. List must have the same length");
50 assert(Dst->getLength() == Src->getLength() &&
51 "Lists must have the same length");
52
53 while (Dst && Src) {
54 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
55 Dst->getOuterOriginID(), Src->getOuterOriginID(), Kill));
56 Dst = Dst->peelOuterOrigin();
57 Src = Src->peelOuterOrigin();
58 }
59}
60
61/// Creates a loan for the storage path of a given declaration reference.
62/// This function should be called whenever a DeclRefExpr represents a borrow.
63/// \param DRE The declaration reference expression that initiates the borrow.
64/// \return The new Loan on success, nullptr otherwise.
65static const PathLoan *createLoan(FactManager &FactMgr,
66 const DeclRefExpr *DRE) {
67 if (const auto *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
68 AccessPath Path(VD);
69 // The loan is created at the location of the DeclRefExpr.
70 return FactMgr.getLoanMgr().createLoan<PathLoan>(Path, DRE);
71 }
72 return nullptr;
73}
74
75/// Creates a loan for the storage location of a temporary object.
76/// \param MTE The MaterializeTemporaryExpr that represents the temporary
77/// binding. \return The new Loan.
78static const PathLoan *createLoan(FactManager &FactMgr,
79 const MaterializeTemporaryExpr *MTE) {
80 AccessPath Path(MTE);
81 return FactMgr.getLoanMgr().createLoan<PathLoan>(Path, MTE);
82}
83
84/// Try to find a CXXBindTemporaryExpr that descends from MTE, stripping away
85/// any implicit casts.
86/// \param MTE MaterializeTemporaryExpr whose descendants we are interested in.
87/// \return Pointer to descendant CXXBindTemporaryExpr or nullptr when not
88/// found.
89static const CXXBindTemporaryExpr *
91 const Expr *Child = MTE->getSubExpr()->IgnoreImpCasts();
92 return dyn_cast<CXXBindTemporaryExpr>(Child);
93}
94
96 llvm::TimeTraceScope TimeProfile("FactGenerator");
97 const CFG &Cfg = *AC.getCFG();
98 llvm::SmallVector<Fact *> PlaceholderLoanFacts = issuePlaceholderLoans();
99 // Iterate through the CFG blocks in reverse post-order to ensure that
100 // initializations and destructions are processed in the correct sequence.
101 for (const CFGBlock *Block : *AC.getAnalysis<PostOrderCFGView>()) {
102 CurrentBlockFacts.clear();
103 EscapesInCurrentBlock.clear();
104 if (Block == &Cfg.getEntry())
105 CurrentBlockFacts.append(PlaceholderLoanFacts.begin(),
106 PlaceholderLoanFacts.end());
107 for (unsigned I = 0; I < Block->size(); ++I) {
108 const CFGElement &Element = Block->Elements[I];
109 if (std::optional<CFGStmt> CS = Element.getAs<CFGStmt>())
110 Visit(CS->getStmt());
111 else if (std::optional<CFGInitializer> Initializer =
112 Element.getAs<CFGInitializer>())
113 handleCXXCtorInitializer(Initializer->getInitializer());
114 else if (std::optional<CFGLifetimeEnds> LifetimeEnds =
115 Element.getAs<CFGLifetimeEnds>())
116 handleLifetimeEnds(*LifetimeEnds);
117 else if (std::optional<CFGTemporaryDtor> TemporaryDtor =
118 Element.getAs<CFGTemporaryDtor>())
119 handleTemporaryDtor(*TemporaryDtor);
120 }
121 if (Block == &Cfg.getExit())
122 handleExitBlock();
123
124 CurrentBlockFacts.append(EscapesInCurrentBlock.begin(),
125 EscapesInCurrentBlock.end());
126 FactMgr.addBlockFacts(Block, CurrentBlockFacts);
127 }
128}
129
130/// Simulates LValueToRValue conversion by peeling the outer lvalue origin
131/// if the expression is a GLValue. For pointer/view GLValues, this strips
132/// the origin representing the storage location to get the origins of the
133/// pointed-to value.
134///
135/// Example: For `View& v`, returns the origin of what v points to, not v's
136/// storage.
137static OriginList *getRValueOrigins(const Expr *E, OriginList *List) {
138 if (!List)
139 return nullptr;
140 return E->isGLValue() ? List->peelOuterOrigin() : List;
141}
142
144 for (const Decl *D : DS->decls())
145 if (const auto *VD = dyn_cast<VarDecl>(D))
146 if (const Expr *InitExpr = VD->getInit()) {
147 OriginList *VDList = getOriginsList(*VD);
148 if (!VDList)
149 continue;
150 OriginList *InitList = getOriginsList(*InitExpr);
151 assert(InitList && "VarDecl had origins but InitExpr did not");
152 flow(VDList, InitList, /*Kill=*/true);
153 }
154}
155
157 // Skip function references as their lifetimes are not interesting. Skip non
158 // GLValues (like EnumConstants).
159 if (DRE->getFoundDecl()->isFunctionOrFunctionTemplate() || !DRE->isGLValue())
160 return;
161 handleUse(DRE);
162 // For all declarations with storage (non-references), we issue a loan
163 // representing the borrow of the variable's storage itself.
164 //
165 // Examples:
166 // - `int x; x` issues loan to x's storage
167 // - `int* p; p` issues loan to p's storage (the pointer variable)
168 // - `View v; v` issues loan to v's storage (the view object)
169 // - `int& r = x; r` issues no loan (r has no storage, it's an alias to x)
170 if (doesDeclHaveStorage(DRE->getDecl())) {
171 const Loan *L = createLoan(FactMgr, DRE);
172 assert(L);
173 OriginList *List = getOriginsList(*DRE);
174 assert(List &&
175 "gl-value DRE of non-pointer type should have an origin list");
176 // This loan specifically tracks borrowing the variable's storage location
177 // itself and is issued to outermost origin (List->OID).
178 CurrentBlockFacts.push_back(
179 FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
180 }
181}
182
184 if (isGslPointerType(CCE->getType())) {
185 handleGSLPointerConstruction(CCE);
186 return;
187 }
188}
189
190void FactsGenerator::handleCXXCtorInitializer(const CXXCtorInitializer *CII) {
191 // Flows origins from the initializer expression to the field.
192 // Example: `MyObj(std::string s) : view(s) {}`
193 if (const FieldDecl *FD = CII->getAnyMember())
194 killAndFlowOrigin(*FD, *CII->getInit());
195}
196
198 // Specifically for conversion operators,
199 // like `std::string_view p = std::string{};`
200 if (isGslPointerType(MCE->getType()) &&
201 isa_and_present<CXXConversionDecl>(MCE->getCalleeDecl()) &&
203 // The argument is the implicit object itself.
204 handleFunctionCall(MCE, MCE->getMethodDecl(),
205 {MCE->getImplicitObjectArgument()},
206 /*IsGslConstruction=*/true);
207 return;
208 }
209 if (const CXXMethodDecl *Method = MCE->getMethodDecl()) {
210 // Construct the argument list, with the implicit 'this' object as the
211 // first argument.
213 Args.push_back(MCE->getImplicitObjectArgument());
214 Args.append(MCE->getArgs(), MCE->getArgs() + MCE->getNumArgs());
215
216 handleFunctionCall(MCE, Method, Args, /*IsGslConstruction=*/false);
217 }
218}
219
221 auto *MD = ME->getMemberDecl();
222 if (isa<FieldDecl>(MD) && doesDeclHaveStorage(MD)) {
223 assert(ME->isGLValue() && "Field member should be GL value");
224 OriginList *Dst = getOriginsList(*ME);
225 assert(Dst && "Field member should have an origin list as it is GL value");
226 OriginList *Src = getOriginsList(*ME->getBase());
227 assert(Src && "Base expression should be a pointer/reference type");
228 // The field's glvalue (outermost origin) holds the same loans as the base
229 // expression.
230 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
231 Dst->getOuterOriginID(), Src->getOuterOriginID(),
232 /*Kill=*/true));
233 }
234}
235
236static bool isStdMove(const FunctionDecl *FD) {
237 return FD && FD->isInStdNamespace() && FD->getIdentifier() &&
238 FD->getName() == "move";
239}
240
242 handleFunctionCall(CE, CE->getDirectCallee(),
243 {CE->getArgs(), CE->getNumArgs()});
244 // Track declarations that are moved via std::move.
245 // This is a flow-insensitive approximation: once a declaration is moved
246 // anywhere in the function, it's treated as moved everywhere. We do not
247 // generate expire facts for moved decls to avoid false alarms.
248 if (isStdMove(CE->getDirectCallee()))
249 if (CE->getNumArgs() == 1)
250 if (auto *DRE =
251 dyn_cast<DeclRefExpr>(CE->getArg(0)->IgnoreParenImpCasts()))
252 MovedDecls.insert(DRE->getDecl());
253}
254
256 const CXXNullPtrLiteralExpr *N) {
257 /// TODO: Handle nullptr expr as a special 'null' loan. Uninitialized
258 /// pointers can use the same type of loan.
259 getOriginsList(*N);
260}
261
263 OriginList *Dest = getOriginsList(*ICE);
264 if (!Dest)
265 return;
266 const Expr *SubExpr = ICE->getSubExpr();
267 OriginList *Src = getOriginsList(*SubExpr);
268
269 switch (ICE->getCastKind()) {
270 case CK_LValueToRValue:
271 // TODO: Decide what to do for x-values here.
272 if (!SubExpr->isLValue())
273 return;
274
275 assert(Src && "LValue being cast to RValue has no origin list");
276 // The result of an LValue-to-RValue cast on a pointer lvalue (like `q` in
277 // `int *p, *q; p = q;`) should propagate the inner origin (what the pointer
278 // points to), not the outer origin (the pointer's storage location). Strip
279 // the outer lvalue origin.
280 flow(getOriginsList(*ICE), getRValueOrigins(SubExpr, Src),
281 /*Kill=*/true);
282 return;
283 case CK_NullToPointer:
284 getOriginsList(*ICE);
285 // TODO: Flow into them a null origin.
286 return;
287 case CK_NoOp:
288 case CK_ConstructorConversion:
289 case CK_UserDefinedConversion:
290 flow(Dest, Src, /*Kill=*/true);
291 return;
292 case CK_UncheckedDerivedToBase:
293 case CK_DerivedToBase:
294 // It is possible that the derived class and base class have different
295 // gsl::Pointer annotations. Skip if their origin shape differ.
296 if (Dest && Src && Dest->getLength() == Src->getLength())
297 flow(Dest, Src, /*Kill=*/true);
298 return;
299 case CK_FunctionToPointerDecay:
300 case CK_BuiltinFnToFnPtr:
301 case CK_ArrayToPointerDecay:
302 // Ignore function-to-pointer decays.
303 return;
304 default:
305 return;
306 }
307}
308
310 switch (UO->getOpcode()) {
311 case UO_AddrOf: {
312 const Expr *SubExpr = UO->getSubExpr();
313 // The origin of an address-of expression (e.g., &x) is the origin of
314 // its sub-expression (x). This fact will cause the dataflow analysis
315 // to propagate any loans held by the sub-expression's origin to the
316 // origin of this UnaryOperator expression.
317 killAndFlowOrigin(*UO, *SubExpr);
318 return;
319 }
320 case UO_Deref: {
321 const Expr *SubExpr = UO->getSubExpr();
322 killAndFlowOrigin(*UO, *SubExpr);
323 return;
324 }
325 default:
326 return;
327 }
328}
329
331 if (const Expr *RetExpr = RS->getRetValue()) {
332 if (OriginList *List = getOriginsList(*RetExpr))
333 for (OriginList *L = List; L != nullptr; L = L->peelOuterOrigin())
334 EscapesInCurrentBlock.push_back(FactMgr.createFact<ReturnEscapeFact>(
335 L->getOuterOriginID(), RetExpr));
336 }
337}
338
339void FactsGenerator::handleAssignment(const Expr *LHSExpr,
340 const Expr *RHSExpr) {
341 LHSExpr = LHSExpr->IgnoreParenImpCasts();
342 OriginList *LHSList = nullptr;
343
344 if (const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr)) {
345 LHSList = getOriginsList(*DRE_LHS);
346 assert(LHSList && "LHS is a DRE and should have an origin list");
347 }
348 // Handle assignment to member fields (e.g., `this->view = s` or `view = s`).
349 // This enables detection of dangling fields when local values escape to
350 // fields.
351 if (const auto *ME_LHS = dyn_cast<MemberExpr>(LHSExpr)) {
352 LHSList = getOriginsList(*ME_LHS);
353 assert(LHSList && "LHS is a MemberExpr and should have an origin list");
354 }
355 if (!LHSList)
356 return;
357 OriginList *RHSList = getOriginsList(*RHSExpr);
358 // For operator= with reference parameters (e.g.,
359 // `View& operator=(const View&)`), the RHS argument stays an lvalue,
360 // unlike built-in assignment where LValueToRValue cast strips the outer
361 // lvalue origin. Strip it manually to get the actual value origins being
362 // assigned.
363 RHSList = getRValueOrigins(RHSExpr, RHSList);
364
365 if (const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr))
366 markUseAsWrite(DRE_LHS);
367 // Kill the old loans of the destination origin and flow the new loans
368 // from the source origin.
369 flow(LHSList->peelOuterOrigin(), RHSList, /*Kill=*/true);
370}
371
373 // TODO: Handle pointer arithmetic (e.g., `p + 1` or `1 + p`) where the
374 // result should have the same loans as the pointer operand.
375 if (BO->isCompoundAssignmentOp())
376 return;
377 if (BO->isAssignmentOp())
378 handleAssignment(BO->getLHS(), BO->getRHS());
379 // TODO: Handle assignments involving dereference like `*p = q`.
380}
381
383 if (hasOrigins(CO)) {
384 // Merge origins from both branches of the conditional operator.
385 // We kill to clear the initial state and merge both origins into it.
386 killAndFlowOrigin(*CO, *CO->getTrueExpr());
387 flowOrigin(*CO, *CO->getFalseExpr());
388 }
389}
390
392 // Assignment operators have special "kill-then-propagate" semantics
393 // and are handled separately.
394 if (OCE->getOperator() == OO_Equal && OCE->getNumArgs() == 2 &&
395 hasOrigins(OCE->getArg(0)->getType())) {
396 handleAssignment(OCE->getArg(0), OCE->getArg(1));
397 return;
398 }
399 VisitCallExpr(OCE);
400}
401
403 const CXXFunctionalCastExpr *FCE) {
404 // Check if this is a test point marker. If so, we are done with this
405 // expression.
406 if (handleTestPoint(FCE))
407 return;
408 if (isGslPointerType(FCE->getType()))
409 killAndFlowOrigin(*FCE, *FCE->getSubExpr());
410}
411
413 if (!hasOrigins(ILE))
414 return;
415 // For list initialization with a single element, like `View{...}`, the
416 // origin of the list itself is the origin of its single element.
417 if (ILE->getNumInits() == 1)
418 killAndFlowOrigin(*ILE, *ILE->getInit(0));
419}
420
422 const CXXBindTemporaryExpr *BTE) {
423 killAndFlowOrigin(*BTE, *BTE->getSubExpr());
424}
425
427 const MaterializeTemporaryExpr *MTE) {
428 assert(MTE->isGLValue());
429 OriginList *MTEList = getOriginsList(*MTE);
430 if (!MTEList)
431 return;
432 OriginList *SubExprList = getOriginsList(*MTE->getSubExpr());
433 assert((!SubExprList ||
434 MTEList->getLength() == (SubExprList->getLength() + 1)) &&
435 "MTE top level origin should contain a loan to the MTE itself");
436
437 OriginList *RValMTEList = getRValueOrigins(MTE, MTEList);
438 flow(RValMTEList, SubExprList, /*Kill=*/true);
439 OriginID OuterMTEID = MTEList->getOuterOriginID();
440 if (getChildBinding(MTE)) {
441 // Issue a loan to MTE for the storage location represented by MTE.
442 const Loan *L = createLoan(FactMgr, MTE);
443 CurrentBlockFacts.push_back(
444 FactMgr.createFact<IssueFact>(L->getID(), OuterMTEID));
445 }
446}
447
448void FactsGenerator::handleLifetimeEnds(const CFGLifetimeEnds &LifetimeEnds) {
449 /// TODO: Handle loans to temporaries.
450 const VarDecl *LifetimeEndsVD = LifetimeEnds.getVarDecl();
451 if (!LifetimeEndsVD)
452 return;
453 // Iterate through all loans to see if any expire.
454 for (const auto *Loan : FactMgr.getLoanMgr().getLoans()) {
455 if (const auto *BL = dyn_cast<PathLoan>(Loan)) {
456 // Skip loans for declarations that have been moved. When a value is
457 // moved, the original owner no longer has ownership and its destruction
458 // should not cause the loan to expire, preventing false positives.
459 if (MovedDecls.contains(BL->getAccessPath().getAsValueDecl()))
460 continue;
461 // Check if the loan is for a stack variable and if that variable
462 // is the one being destructed.
463 const AccessPath AP = BL->getAccessPath();
464 const ValueDecl *Path = AP.getAsValueDecl();
465 if (Path == LifetimeEndsVD)
466 CurrentBlockFacts.push_back(FactMgr.createFact<ExpireFact>(
467 BL->getID(), LifetimeEnds.getTriggerStmt()->getEndLoc()));
468 }
469 }
470}
471
472void FactsGenerator::handleTemporaryDtor(
473 const CFGTemporaryDtor &TemporaryDtor) {
474 const CXXBindTemporaryExpr *ExpiringBTE =
475 TemporaryDtor.getBindTemporaryExpr();
476 if (!ExpiringBTE)
477 return;
478 // Iterate through all loans to see if any expire.
479 for (const auto *Loan : FactMgr.getLoanMgr().getLoans()) {
480 if (const auto *PL = dyn_cast<PathLoan>(Loan)) {
481 // Check if the loan is for a temporary materialization and if that
482 // storage location is the one being destructed.
483 const AccessPath &AP = PL->getAccessPath();
484 const MaterializeTemporaryExpr *Path = AP.getAsMaterializeTemporaryExpr();
485 if (!Path)
486 continue;
487 if (ExpiringBTE == getChildBinding(Path)) {
488 CurrentBlockFacts.push_back(FactMgr.createFact<ExpireFact>(
489 PL->getID(), TemporaryDtor.getBindTemporaryExpr()->getEndLoc()));
490 }
491 }
492 }
493}
494
495void FactsGenerator::handleExitBlock() {
496 // Creates FieldEscapeFacts for all field origins that remain live at exit.
497 for (const Origin &O : FactMgr.getOriginMgr().getOrigins())
498 if (auto *FD = dyn_cast_if_present<FieldDecl>(O.getDecl()))
499 EscapesInCurrentBlock.push_back(
500 FactMgr.createFact<FieldEscapeFact>(O.ID, FD));
501}
502
503void FactsGenerator::handleGSLPointerConstruction(const CXXConstructExpr *CCE) {
504 assert(isGslPointerType(CCE->getType()));
505 if (CCE->getNumArgs() != 1)
506 return;
507
508 const Expr *Arg = CCE->getArg(0);
509 if (isGslPointerType(Arg->getType())) {
510 OriginList *ArgList = getOriginsList(*Arg);
511 assert(ArgList && "GSL pointer argument should have an origin list");
512 // GSL pointer is constructed from another gsl pointer.
513 // Example:
514 // View(View v);
515 // View(const View &v);
516 ArgList = getRValueOrigins(Arg, ArgList);
517 flow(getOriginsList(*CCE), ArgList, /*Kill=*/true);
518 } else if (Arg->getType()->isPointerType()) {
519 // GSL pointer is constructed from a raw pointer. Flow only the outermost
520 // raw pointer. Example:
521 // View(const char*);
522 // Span<int*>(const in**);
523 OriginList *ArgList = getOriginsList(*Arg);
524 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
525 getOriginsList(*CCE)->getOuterOriginID(), ArgList->getOuterOriginID(),
526 /*Kill=*/true));
527 } else {
528 // This could be a new borrow.
529 // TODO: Add code example here.
530 handleFunctionCall(CCE, CCE->getConstructor(),
531 {CCE->getArgs(), CCE->getNumArgs()},
532 /*IsGslConstruction=*/true);
533 }
534}
535
536/// Checks if a call-like expression creates a borrow by passing a value to a
537/// reference parameter, creating an IssueFact if it does.
538/// \param IsGslConstruction True if this is a GSL construction where all
539/// argument origins should flow to the returned origin.
540void FactsGenerator::handleFunctionCall(const Expr *Call,
541 const FunctionDecl *FD,
542 ArrayRef<const Expr *> Args,
543 bool IsGslConstruction) {
544 OriginList *CallList = getOriginsList(*Call);
545 // Ignore functions returning values with no origin.
547 if (!FD || !CallList)
548 return;
549 auto IsArgLifetimeBound = [FD](unsigned I) -> bool {
550 const ParmVarDecl *PVD = nullptr;
551 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD);
552 Method && Method->isInstance()) {
553 if (I == 0)
554 // For the 'this' argument, the attribute is on the method itself.
557 Method, /*RunningUnderLifetimeSafety=*/true);
558 if ((I - 1) < Method->getNumParams())
559 // For explicit arguments, find the corresponding parameter
560 // declaration.
561 PVD = Method->getParamDecl(I - 1);
562 } else if (I == 0 && shouldTrackFirstArgument(FD)) {
563 return true;
564 } else if (I < FD->getNumParams()) {
565 // For free functions or static methods.
566 PVD = FD->getParamDecl(I);
567 }
568 return PVD ? PVD->hasAttr<clang::LifetimeBoundAttr>() : false;
569 };
570 auto shouldTrackPointerImplicitObjectArg = [FD](unsigned I) -> bool {
571 const auto *Method = dyn_cast<CXXMethodDecl>(FD);
572 if (!Method || !Method->isInstance())
573 return false;
574 return I == 0 &&
575 isGslPointerType(Method->getFunctionObjectParameterType()) &&
577 /*RunningUnderLifetimeSafety=*/true);
578 };
579 if (Args.empty())
580 return;
581 bool KillSrc = true;
582 for (unsigned I = 0; I < Args.size(); ++I) {
583 OriginList *ArgList = getOriginsList(*Args[I]);
584 if (!ArgList)
585 continue;
586 if (IsGslConstruction) {
587 // TODO: document with code example.
588 // std::string_view(const std::string_view& from)
589 if (isGslPointerType(Args[I]->getType())) {
590 assert(!Args[I]->isGLValue() || ArgList->getLength() >= 2);
591 ArgList = getRValueOrigins(Args[I], ArgList);
592 }
593 if (isGslOwnerType(Args[I]->getType())) {
594 // GSL construction creates a view that borrows from arguments.
595 // This implies flowing origins through the list structure.
596 flow(CallList, ArgList, KillSrc);
597 KillSrc = false;
598 }
599 } else if (shouldTrackPointerImplicitObjectArg(I)) {
600 assert(ArgList->getLength() >= 2 &&
601 "Object arg of pointer type should have atleast two origins");
602 // See through the GSLPointer reference to see the pointer's value.
603 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
604 CallList->getOuterOriginID(),
605 ArgList->peelOuterOrigin()->getOuterOriginID(), KillSrc));
606 KillSrc = false;
607 } else if (IsArgLifetimeBound(I)) {
608 // Lifetimebound on a non-GSL-ctor function means the returned
609 // pointer/reference itself must not outlive the arguments. This
610 // only constraints the top-level origin.
611 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
612 CallList->getOuterOriginID(), ArgList->getOuterOriginID(), KillSrc));
613 KillSrc = false;
614 }
615 }
616}
617
618/// Checks if the expression is a `void("__lifetime_test_point_...")` cast.
619/// If so, creates a `TestPointFact` and returns true.
620bool FactsGenerator::handleTestPoint(const CXXFunctionalCastExpr *FCE) {
621 if (!FCE->getType()->isVoidType())
622 return false;
623
624 const auto *SubExpr = FCE->getSubExpr()->IgnoreParenImpCasts();
625 if (const auto *SL = dyn_cast<StringLiteral>(SubExpr)) {
626 llvm::StringRef LiteralValue = SL->getString();
627 const std::string Prefix = "__lifetime_test_point_";
628
629 if (LiteralValue.starts_with(Prefix)) {
630 StringRef Annotation = LiteralValue.drop_front(Prefix.length());
631 CurrentBlockFacts.push_back(
632 FactMgr.createFact<TestPointFact>(Annotation));
633 return true;
634 }
635 }
636 return false;
637}
638
639// A DeclRefExpr will be treated as a use of the referenced decl. It will be
640// checked for use-after-free unless it is later marked as being written to
641// (e.g. on the left-hand side of an assignment).
642void FactsGenerator::handleUse(const DeclRefExpr *DRE) {
643 OriginList *List = getOriginsList(*DRE);
644 if (!List)
645 return;
646 // Remove the outer layer of origin which borrows from the decl directly
647 // (e.g., when this is not a reference). This is a use of the underlying decl.
648 if (!DRE->getDecl()->getType()->isReferenceType())
649 List = getRValueOrigins(DRE, List);
650 // Skip if there is no inner origin (e.g., when it is not a pointer type).
651 if (!List)
652 return;
653 UseFact *UF = FactMgr.createFact<UseFact>(DRE, List);
654 CurrentBlockFacts.push_back(UF);
655 assert(!UseFacts.contains(DRE));
656 UseFacts[DRE] = UF;
657}
658
659void FactsGenerator::markUseAsWrite(const DeclRefExpr *DRE) {
660 if (UseFacts.contains(DRE))
661 UseFacts[DRE]->markAsWritten();
662}
663
664// Creates an IssueFact for a new placeholder loan for each pointer or reference
665// parameter at the function's entry.
666llvm::SmallVector<Fact *> FactsGenerator::issuePlaceholderLoans() {
667 const auto *FD = dyn_cast<FunctionDecl>(AC.getDecl());
668 if (!FD)
669 return {};
670
671 llvm::SmallVector<Fact *> PlaceholderLoanFacts;
672 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD); MD && MD->isInstance()) {
673 OriginList *List = *FactMgr.getOriginMgr().getThisOrigins();
674 const PlaceholderLoan *L =
675 FactMgr.getLoanMgr().createLoan<PlaceholderLoan>(MD);
676 PlaceholderLoanFacts.push_back(
677 FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
678 }
679 for (const ParmVarDecl *PVD : FD->parameters()) {
680 OriginList *List = getOriginsList(*PVD);
681 if (!List)
682 continue;
683 const PlaceholderLoan *L =
684 FactMgr.getLoanMgr().createLoan<PlaceholderLoan>(PVD);
685 PlaceholderLoanFacts.push_back(
686 FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
687 }
688 return PlaceholderLoanFacts;
689}
690
691} // namespace clang::lifetimes::internal
TokenType getType() const
Returns the token's type, e.g.
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4038
Expr * getLHS() const
Definition Expr.h:4088
Expr * getRHS() const
Definition Expr.h:4090
static bool isAssignmentOp(Opcode Opc)
Definition Expr.h:4174
static bool isCompoundAssignmentOp(Opcode Opc)
Definition Expr.h:4179
Represents a single basic block in a source-level CFG.
Definition CFG.h:605
Represents a top-level expression in a basic block.
Definition CFG.h:55
std::optional< T > getAs() const
Convert to the specified CFGElement type, returning std::nullopt if this CFGElement is not of the des...
Definition CFG.h:109
Represents C++ base or member initializer from constructor's initialization list.
Definition CFG.h:228
Represents the point where the lifetime of an automatic object ends.
Definition CFG.h:293
const Stmt * getTriggerStmt() const
Definition CFG.h:302
const VarDecl * getVarDecl() const
Definition CFG.h:298
Represents C++ object destructor implicitly generated at the end of full expression for temporary obj...
Definition CFG.h:511
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1218
CFGBlock & getExit()
Definition CFG.h:1334
CFGBlock & getEntry()
Definition CFG.h:1332
Represents binding an expression to a temporary.
Definition ExprCXX.h:1493
const Expr * getSubExpr() const
Definition ExprCXX.h:1515
Represents a call to a C++ constructor.
Definition ExprCXX.h:1548
Represents a C++ base or member initializer.
Definition DeclCXX.h:2369
Expr * getInit() const
Get the initializer.
Definition DeclCXX.h:2571
FieldDecl * getAnyMember() const
Definition DeclCXX.h:2515
Represents an explicit C++ type conversion that uses "functional" notation (C++ [expr....
Definition ExprCXX.h:1831
Represents a call to a member function that may be written either with member call syntax (e....
Definition ExprCXX.h:179
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Definition ExprCXX.cpp:741
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
Definition ExprCXX.cpp:722
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:768
A call to an overloaded operator written using operator syntax.
Definition ExprCXX.h:84
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
Definition ExprCXX.h:114
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2943
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3147
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3126
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3134
Expr ** getArgs()
Retrieve the call arguments.
Definition Expr.h:3137
Decl * getCalleeDecl()
Definition Expr.h:3120
CastKind getCastKind() const
Definition Expr.h:3720
Expr * getSubExpr()
Definition Expr.h:3726
ConditionalOperator - The ?
Definition Expr.h:4391
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4423
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4418
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
NamedDecl * getFoundDecl()
Get the NamedDecl through which this reference occurred.
Definition Expr.h:1381
ValueDecl * getDecl()
Definition Expr.h:1338
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1623
decl_range decls()
Definition Stmt.h:1671
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInStdNamespace() const
Definition DeclBase.cpp:449
bool isFunctionOrFunctionTemplate() const
Whether this declaration is a function or function template.
Definition DeclBase.h:1119
This represents one expression.
Definition Expr.h:112
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3089
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3069
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3160
Represents a function declaration or definition.
Definition Decl.h:2000
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition Expr.h:3853
Describes an C or C++ initializer list.
Definition Expr.h:5299
unsigned getNumInits() const
Definition Expr.h:5329
const Expr * getInit(unsigned Init) const
Definition Expr.h:5353
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4937
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3364
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3447
Expr * getBase() const
Definition Expr.h:3441
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3152
Expr * getRetValue()
Definition Stmt.h:3179
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition StmtVisitor.h:45
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.cpp:362
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2244
Expr * getSubExpr() const
Definition Expr.h:2285
Opcode getOpcode() const
Definition Expr.h:2280
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
Represents a variable declaration or definition.
Definition Decl.h:926
FactType * createFact(Args &&...args)
Definition Facts.h:256
void VisitDeclRefExpr(const DeclRefExpr *DRE)
void VisitBinaryOperator(const BinaryOperator *BO)
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE)
void VisitCXXConstructExpr(const CXXConstructExpr *CCE)
void VisitImplicitCastExpr(const ImplicitCastExpr *ICE)
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE)
void VisitInitListExpr(const InitListExpr *ILE)
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N)
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE)
void VisitUnaryOperator(const UnaryOperator *UO)
void VisitConditionalOperator(const ConditionalOperator *CO)
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE)
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE)
llvm::ArrayRef< const Loan * > getLoans() const
Definition Loans.h:167
LoanType * createLoan(Args &&...args)
Definition Loans.h:151
An abstract base class for a single "Loan" which represents lending a storage in memory.
Definition Loans.h:59
A list of origins representing levels of indirection for pointer-like types.
Definition Origins.h:94
OriginList * peelOuterOrigin() const
Definition Origins.h:98
PathLoan represents lending a storage location that is visible within the function's scope (e....
Definition Loans.h:87
Represents that an origin escapes via a return statement.
Definition Facts.h:163
static const CXXBindTemporaryExpr * getChildBinding(const MaterializeTemporaryExpr *MTE)
Try to find a CXXBindTemporaryExpr that descends from MTE, stripping away any implicit casts.
static const PathLoan * createLoan(FactManager &FactMgr, const DeclRefExpr *DRE)
Creates a loan for the storage path of a given declaration reference.
utils::ID< struct OriginTag > OriginID
Definition Origins.h:27
static OriginList * getRValueOrigins(const Expr *E, OriginList *List)
Simulates LValueToRValue conversion by peeling the outer lvalue origin if the expression is a GLValue...
static bool isStdMove(const FunctionDecl *FD)
bool doesDeclHaveStorage(const ValueDecl *D)
Returns true if the declaration has its own storage that can be borrowed.
Definition Origins.cpp:86
bool hasOrigins(QualType QT)
Definition Origins.cpp:53
bool isGslPointerType(QualType QT)
bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee, bool RunningUnderLifetimeSafety)
bool shouldTrackFirstArgument(const FunctionDecl *FD)
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
bool isGslOwnerType(QualType QT)
bool isa(CodeGen::Address addr)
Definition Address.h:330
#define false
Definition stdbool.h:26
Represents the storage location being borrowed, e.g., a specific stack variable.
Definition Loans.h:33
const clang::ValueDecl * getAsValueDecl() const
Definition Loans.h:48