Skip to content

Commit 076766a

Browse files
committed
lifetime-analysis-lifetimebound
1 parent 19659ee commit 076766a

File tree

6 files changed

+636
-133
lines changed

6 files changed

+636
-133
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ template <typename Tag> struct ID {
7575
}
7676
};
7777

78-
template <typename Tag>
79-
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ID<Tag> ID) {
80-
return OS << ID.Value;
81-
}
82-
8378
using LoanID = ID<struct LoanTag>;
8479
using OriginID = ID<struct OriginTag>;
80+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
81+
return OS << ID.Value;
82+
}
83+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
84+
return OS << ID.Value;
85+
}
8586

8687
// Using LLVM's immutable collections is efficient for dataflow analysis
8788
// as it avoids deep copies during state transitions.

clang/lib/Analysis/LifetimeSafety.cpp

Lines changed: 146 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ class Fact {
212212
/// A loan expires as its underlying storage is freed (e.g., variable goes
213213
/// out of scope).
214214
Expire,
215+
/// The loan set of an origin is cleared.
216+
KillOrigin,
215217
/// An origin is propagated from a source to a destination (e.g., p = q).
216-
AssignOrigin,
218+
OriginFlow,
217219
/// An origin escapes the function by flowing into the return value.
218220
ReturnOfOrigin,
219221
/// An origin is used (eg. dereferencing a pointer).
@@ -285,22 +287,24 @@ class ExpireFact : public Fact {
285287
}
286288
};
287289

288-
class AssignOriginFact : public Fact {
290+
class OriginFlowFact : public Fact {
289291
OriginID OIDDest;
290292
OriginID OIDSrc;
291293

292294
public:
293295
static bool classof(const Fact *F) {
294-
return F->getKind() == Kind::AssignOrigin;
296+
return F->getKind() == Kind::OriginFlow;
295297
}
296298

297-
AssignOriginFact(OriginID OIDDest, OriginID OIDSrc)
298-
: Fact(Kind::AssignOrigin), OIDDest(OIDDest), OIDSrc(OIDSrc) {}
299+
OriginFlowFact(OriginID OIDDest, OriginID OIDSrc)
300+
: Fact(Kind::OriginFlow), OIDDest(OIDDest), OIDSrc(OIDSrc) {}
301+
299302
OriginID getDestOriginID() const { return OIDDest; }
300303
OriginID getSrcOriginID() const { return OIDSrc; }
304+
301305
void dump(llvm::raw_ostream &OS, const LoanManager &,
302306
const OriginManager &OM) const override {
303-
OS << "AssignOrigin (Dest: ";
307+
OS << "OriginFlow (Dest: ";
304308
OM.dump(getDestOriginID(), OS);
305309
OS << ", Src: ";
306310
OM.dump(getSrcOriginID(), OS);
@@ -353,6 +357,23 @@ class UseFact : public Fact {
353357
}
354358
};
355359

360+
class KillOriginFact : public Fact {
361+
OriginID OID;
362+
363+
public:
364+
static bool classof(const Fact *F) {
365+
return F->getKind() == Kind::KillOrigin;
366+
}
367+
KillOriginFact(OriginID OID) : Fact(Kind::KillOrigin), OID(OID) {}
368+
OriginID getOriginID() const { return OID; }
369+
370+
void dump(llvm::raw_ostream &OS, const LoanManager &,
371+
const OriginManager &OM) const override {
372+
OS << "KillOrigin (";
373+
OM.dump(getOriginID(), OS);
374+
OS << ")\n";
375+
}
376+
};
356377
/// A dummy-fact used to mark a specific point in the code for testing.
357378
/// It is generated by recognizing a `void("__lifetime_test_point_...")` cast.
358379
class TestPointFact : public Fact {
@@ -454,7 +475,7 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
454475
if (const auto *VD = dyn_cast<VarDecl>(D))
455476
if (hasOrigin(VD))
456477
if (const Expr *InitExpr = VD->getInit())
457-
addAssignOriginFact(*VD, *InitExpr);
478+
addOriginFlowFact(*VD, *InitExpr);
458479
}
459480

460481
void VisitDeclRefExpr(const DeclRefExpr *DRE) {
@@ -492,9 +513,23 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
492513
isa<CXXConversionDecl>(MCE->getCalleeDecl())) {
493514
// The argument is the implicit object itself.
494515
handleFunctionCall(MCE, MCE->getMethodDecl(),
495-
{MCE->getImplicitObjectArgument()});
516+
{MCE->getImplicitObjectArgument()},
517+
/*IsGslConstruction=*/true);
496518
}
497-
// FIXME: A more general VisitCallExpr could also be used here.
519+
if (const CXXMethodDecl *Method = MCE->getMethodDecl()) {
520+
// Construct the argument list, with the implicit 'this' object as the
521+
// first argument.
522+
llvm::SmallVector<const Expr *, 4> Args;
523+
Args.push_back(MCE->getImplicitObjectArgument());
524+
Args.append(MCE->getArgs(), MCE->getArgs() + MCE->getNumArgs());
525+
526+
handleFunctionCall(MCE, Method, Args, /*IsGslConstruction=*/false);
527+
}
528+
}
529+
530+
void VisitCallExpr(const CallExpr *CE) {
531+
handleFunctionCall(CE, CE->getDirectCallee(),
532+
{CE->getArgs(), CE->getNumArgs()});
498533
}
499534

500535
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N) {
@@ -508,7 +543,7 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
508543
return;
509544
// An ImplicitCastExpr node itself gets an origin, which flows from the
510545
// origin of its sub-expression (after stripping its own parens/casts).
511-
addAssignOriginFact(*ICE, *ICE->getSubExpr());
546+
addOriginFlowFact(*ICE, *ICE->getSubExpr());
512547
}
513548

514549
void VisitUnaryOperator(const UnaryOperator *UO) {
@@ -522,7 +557,7 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
522557
// its sub-expression (x). This fact will cause the dataflow analysis
523558
// to propagate any loans held by the sub-expression's origin to the
524559
// origin of this UnaryOperator expression.
525-
addAssignOriginFact(*UO, *SubExpr);
560+
addOriginFlowFact(*UO, *SubExpr);
526561
}
527562
}
528563

@@ -542,8 +577,15 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
542577
}
543578

544579
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) {
545-
if (OCE->isAssignmentOp() && OCE->getNumArgs() == 2)
580+
// Assignment operators have special "kill-then-propagate" semantics
581+
// and are handled separately.
582+
if (OCE->isAssignmentOp() && OCE->getNumArgs() == 2) {
546583
handleAssignment(OCE->getArg(0), OCE->getArg(1));
584+
return;
585+
}
586+
handleFunctionCall(OCE, OCE->getDirectCallee(),
587+
{OCE->getArgs(), OCE->getNumArgs()},
588+
/*IsGslConstruction=*/false);
547589
}
548590

549591
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE) {
@@ -552,7 +594,7 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
552594
if (handleTestPoint(FCE))
553595
return;
554596
if (isGslPointerType(FCE->getType()))
555-
addAssignOriginFact(*FCE, *FCE->getSubExpr());
597+
addOriginFlowFact(*FCE, *FCE->getSubExpr());
556598
}
557599

558600
void VisitInitListExpr(const InitListExpr *ILE) {
@@ -561,15 +603,15 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
561603
// For list initialization with a single element, like `View{...}`, the
562604
// origin of the list itself is the origin of its single element.
563605
if (ILE->getNumInits() == 1)
564-
addAssignOriginFact(*ILE, *ILE->getInit(0));
606+
addOriginFlowFact(*ILE, *ILE->getInit(0));
565607
}
566608

567609
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE) {
568610
if (!hasOrigin(MTE))
569611
return;
570612
// A temporary object's origin is the same as the origin of the
571613
// expression that initializes it.
572-
addAssignOriginFact(*MTE, *MTE->getSubExpr());
614+
addOriginFlowFact(*MTE, *MTE->getSubExpr());
573615
}
574616

575617
void handleDestructor(const CFGAutomaticObjDtor &DtorOpt) {
@@ -624,34 +666,68 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
624666
if (CCE->getNumArgs() != 1)
625667
return;
626668
if (hasOrigin(CCE->getArg(0)))
627-
addAssignOriginFact(*CCE, *CCE->getArg(0));
669+
addOriginFlowFact(*CCE, *CCE->getArg(0));
628670
else
629671
// This could be a new borrow.
630672
handleFunctionCall(CCE, CCE->getConstructor(),
631-
{CCE->getArgs(), CCE->getNumArgs()});
673+
{CCE->getArgs(), CCE->getNumArgs()},
674+
/*IsGslConstruction=*/true);
675+
}
676+
static const FunctionDecl *
677+
getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD) {
678+
return FD != nullptr ? FD->getMostRecentDecl() : nullptr;
632679
}
633680

681+
static const CXXMethodDecl *
682+
getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD) {
683+
const FunctionDecl *FD = CMD;
684+
return cast_if_present<CXXMethodDecl>(
685+
getDeclWithMergedLifetimeBoundAttrs(FD));
686+
}
687+
static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
688+
FD = getDeclWithMergedLifetimeBoundAttrs(FD);
689+
const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
690+
if (!TSI)
691+
return false;
692+
// Don't declare this variable in the second operand of the for-statement;
693+
// GCC miscompiles that by ending its lifetime before evaluating the
694+
// third operand. See gcc.gnu.org/PR86769.
695+
AttributedTypeLoc ATL;
696+
for (TypeLoc TL = TSI->getTypeLoc();
697+
(ATL = TL.getAsAdjusted<AttributedTypeLoc>());
698+
TL = ATL.getModifiedLoc()) {
699+
if (ATL.getAttrAs<LifetimeBoundAttr>())
700+
return true;
701+
}
702+
return false;
703+
}
634704
/// Checks if a call-like expression creates a borrow by passing a value to a
635705
/// reference parameter, creating an IssueFact if it does.
636706
void handleFunctionCall(const Expr *Call, const FunctionDecl *FD,
637-
ArrayRef<const Expr *> Args) {
638-
if (!FD)
707+
ArrayRef<const Expr *> Args,
708+
bool IsGslConstruction = false) {
709+
// Ignore functions returning values with no origin.
710+
if (!FD || !hasOrigin(Call))
639711
return;
640-
// TODO: Handle more than one arguments.
641-
for (unsigned I = 0; I <= 0 /*Args.size()*/; ++I) {
642-
const Expr *ArgExpr = Args[I];
643-
644-
// Propagate origins for CXX this.
645-
if (FD->isCXXClassMember() && I == 0) {
646-
addAssignOriginFact(*Call, *ArgExpr);
647-
continue;
648-
}
649-
// The parameter is a pointer, reference, or gsl::Pointer.
650-
// This is a borrow. We propagate the origin from the argument expression
651-
// at the call site to the parameter declaration in the callee.
652-
if (hasOrigin(ArgExpr))
653-
addAssignOriginFact(*Call, *ArgExpr);
654-
}
712+
auto IsArgLifetimeBound = [FD](unsigned I) -> bool {
713+
const ParmVarDecl *PVD = nullptr;
714+
if (const auto *Method = dyn_cast<CXXMethodDecl>(FD);
715+
Method && Method->isInstance()) {
716+
if (I == 0)
717+
// For the 'this' argument, the attribute is on the method itself.
718+
return implicitObjectParamIsLifetimeBound(Method);
719+
if ((I - 1) < Method->getNumParams())
720+
// For explicit arguments, find the corresponding parameter
721+
// declaration.
722+
PVD = Method->getParamDecl(I - 1);
723+
} else if (I < FD->getNumParams())
724+
// For free functions or static methods.
725+
PVD = FD->getParamDecl(I);
726+
return PVD ? PVD->hasAttr<clang::LifetimeBoundAttr>() : false;
727+
};
728+
for (unsigned I = 0; I < Args.size(); ++I)
729+
if (IsGslConstruction || IsArgLifetimeBound(I))
730+
addOriginFlowFact(*Call, *Args[I]);
655731
}
656732

657733
/// Creates a loan for the storage path of a given declaration reference.
@@ -668,11 +744,16 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
668744
}
669745

670746
template <typename Destination, typename Source>
671-
void addAssignOriginFact(const Destination &D, const Source &S) {
747+
void addOriginFlowFact(const Destination &D, const Source &S) {
672748
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(D);
673749
OriginID SrcOID = FactMgr.getOriginMgr().get(S);
674750
CurrentBlockFacts.push_back(
675-
FactMgr.createFact<AssignOriginFact>(DestOID, SrcOID));
751+
FactMgr.createFact<OriginFlowFact>(DestOID, SrcOID));
752+
}
753+
754+
void killOrigin(const ValueDecl *D) {
755+
OriginID DestOID = FactMgr.getOriginMgr().getOrCreate(*D);
756+
CurrentBlockFacts.push_back(FactMgr.createFact<KillOriginFact>(DestOID));
676757
}
677758

678759
/// Checks if the expression is a `void("__lifetime_test_point_...")` cast.
@@ -703,12 +784,12 @@ class FactGenerator : public ConstStmtVisitor<FactGenerator> {
703784
if (const auto *DRE_LHS =
704785
dyn_cast<DeclRefExpr>(LHSExpr->IgnoreParenImpCasts())) {
705786
markUseAsWrite(DRE_LHS);
706-
if (const auto *VD_LHS = dyn_cast<ValueDecl>(DRE_LHS->getDecl()))
707-
// We are interested in assignments like `ptr1 = ptr2` or `ptr = &var`.
708-
// LHS must be a pointer/reference type that can be an origin. RHS must
709-
// also represent an origin (either another pointer/ref or an
710-
// address-of).
711-
addAssignOriginFact(*VD_LHS, *RHSExpr);
787+
if (const auto *VD_LHS = dyn_cast<ValueDecl>(DRE_LHS->getDecl())) {
788+
// Kill the old loans of the destination origin and flow the new loans
789+
// from the source origin.
790+
killOrigin(VD_LHS);
791+
addOriginFlowFact(*VD_LHS, *RHSExpr);
792+
}
712793
}
713794
}
714795

@@ -882,8 +963,10 @@ class DataflowAnalysis {
882963
return D->transfer(In, *F->getAs<IssueFact>());
883964
case Fact::Kind::Expire:
884965
return D->transfer(In, *F->getAs<ExpireFact>());
885-
case Fact::Kind::AssignOrigin:
886-
return D->transfer(In, *F->getAs<AssignOriginFact>());
966+
case Fact::Kind::OriginFlow:
967+
return D->transfer(In, *F->getAs<OriginFlowFact>());
968+
case Fact::Kind::KillOrigin:
969+
return D->transfer(In, *F->getAs<KillOriginFact>());
887970
case Fact::Kind::ReturnOfOrigin:
888971
return D->transfer(In, *F->getAs<ReturnOfOriginFact>());
889972
case Fact::Kind::Use:
@@ -897,7 +980,8 @@ class DataflowAnalysis {
897980
public:
898981
Lattice transfer(Lattice In, const IssueFact &) { return In; }
899982
Lattice transfer(Lattice In, const ExpireFact &) { return In; }
900-
Lattice transfer(Lattice In, const AssignOriginFact &) { return In; }
983+
Lattice transfer(Lattice In, const OriginFlowFact &) { return In; }
984+
Lattice transfer(Lattice In, const KillOriginFact &) { return In; }
901985
Lattice transfer(Lattice In, const ReturnOfOriginFact &) { return In; }
902986
Lattice transfer(Lattice In, const UseFact &) { return In; }
903987
Lattice transfer(Lattice In, const TestPointFact &) { return In; }
@@ -1049,14 +1133,27 @@ class LoanPropagationAnalysis
10491133
LoanSetFactory.add(LoanSetFactory.getEmptySet(), LID)));
10501134
}
10511135

1052-
/// The destination origin's loan set is replaced by the source's.
1053-
/// This implicitly "resets" the old loans of the destination.
1054-
Lattice transfer(Lattice In, const AssignOriginFact &F) {
1136+
/// A flow from source to destination adds the source's loans to the
1137+
/// destination's, without clearing the destination's existing loans.
1138+
Lattice transfer(Lattice In, const OriginFlowFact &F) {
10551139
OriginID DestOID = F.getDestOriginID();
10561140
OriginID SrcOID = F.getSrcOriginID();
1141+
1142+
LoanSet DestLoans = getLoans(In, DestOID);
10571143
LoanSet SrcLoans = getLoans(In, SrcOID);
1144+
LoanSet MergedLoans = utils::join(DestLoans, SrcLoans, LoanSetFactory);
1145+
10581146
return LoanPropagationLattice(
1059-
OriginLoanMapFactory.add(In.Origins, DestOID, SrcLoans));
1147+
OriginLoanMapFactory.add(In.Origins, DestOID, MergedLoans));
1148+
}
1149+
1150+
/// Clears the loan set of the specified origin. This is used on the
1151+
/// left-hand side of an assignment to invalidate the variable's old lifetime.
1152+
Lattice transfer(Lattice In, const KillOriginFact &F) {
1153+
OriginID OID = F.getOriginID();
1154+
// Replace the origin's loan set with an empty set.
1155+
return LoanPropagationLattice(OriginLoanMapFactory.add(
1156+
In.Origins, OID, LoanSetFactory.getEmptySet()));
10601157
}
10611158

10621159
LoanSet getLoans(OriginID OID, ProgramPoint P) {

clang/test/Analysis/LifetimeSafety/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def run_single_test(
340340
"name": "cycle",
341341
"title": "Pointer Cycle in Loop",
342342
"generator_func": generate_cpp_cycle_test,
343-
"n_values": [25, 50, 75, 100],
343+
"n_values": [50, 75, 100, 200, 300],
344344
},
345345
{
346346
"name": "merge",

0 commit comments

Comments
 (0)