https://github.com/kashika0112 created
https://github.com/llvm/llvm-project/pull/168855
Handling Trivially Destructed Types
This PR uses `AddLifetime` to handle expiry of loans to trivially destructed
types.
Example:
```
int * trivial_uar(){
int *ptr;
int x = 1;
ptr = &x;
ret
@@ -58,12 +58,11 @@ static llvm::BitVector computePersistentOrigins(const
FactManager &FactMgr,
CheckOrigin(OF->getSrcOriginID());
break;
}
- case Fact::Kind::ReturnOfOrigin:
-CheckOrigin(F->getAs()->getReturnedOriginID());
-break;
@@ -1202,5 +1284,198 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -122,6 +122,39 @@ class LifetimeTestHelper {
return LID;
}
+ std::optional getLiveLoansAtPoint(ProgramPoint P) const {
kashika0112 wrote:
Done.
https://github.com/llvm/llvm-project/pull/165370
___
cfe-com
@@ -43,7 +43,7 @@ struct LivenessInfo {
/// multiple uses along different paths, this will point to the use appearing
/// earlier in the translation unit.
/// This is 'null' when the origin is not live.
- const UseFact *CausingUseFact;
+ llvm::PointerUnion CausingFact;
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
@@ -1202,5 +1284,198 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -1202,5 +1284,198 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -93,6 +93,9 @@ class FactsGenerator : public
ConstStmtVisitor {
FactManager &FactMgr;
AnalysisDeclContext ∾
llvm::SmallVector CurrentBlockFacts;
+ // Collect origins that escape the function in this block
(OriginEscapesFact),
+ // appended at the end to ensure t
@@ -1202,5 +1286,209 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -43,7 +43,7 @@ struct LivenessInfo {
/// multiple uses along different paths, this will point to the use appearing
/// earlier in the translation unit.
/// This is 'null' when the origin is not live.
- const UseFact *CausingUseFact;
+ llvm::PointerUnion CausingFact;
@@ -1202,5 +1284,198 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -122,6 +122,39 @@ class LifetimeTestHelper {
return LID;
}
+ std::optional getLiveLoansAtPoint(ProgramPoint P) const {
+const auto &LiveOriginsAnalysis = Runner.getAnalysis().getLiveOrigins();
+const auto &LoanPropagation = Runner.getAnalysis().getLoanPropaga
@@ -53,6 +53,17 @@ struct Lattice {
}
};
+static SourceLocation
+GetFactLoc(llvm::PointerUnion F) {
+ if (const auto *UF = F.dyn_cast())
+return UF->getUseExpr()->getExprLoc();
+
kashika0112 wrote:
Done
https://github.com/llvm/llvm-project/pull/165370
@@ -396,6 +396,124 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -219,6 +222,7 @@ class FactManager {
const LoanManager &getLoanMgr() const { return LoanMgr; }
OriginManager &getOriginMgr() { return OriginMgr; }
const OriginManager &getOriginMgr() const { return OriginMgr; }
+ llvm::ArrayRef getBlockContaining(ProgramPoint P) const
@@ -99,4 +100,16 @@ void FactManager::dump(const CFG &Cfg, AnalysisDeclContext
&AC) const {
}
}
+llvm::ArrayRef
+FactManager::getBlockContaining(ProgramPoint P) const {
+ for (const auto &Entry : BlockToFactsMap) {
+const auto &Facts = Entry.second;
+for (const Fac
@@ -93,6 +93,9 @@ class FactsGenerator : public
ConstStmtVisitor {
FactManager &FactMgr;
AnalysisDeclContext ∾
llvm::SmallVector CurrentBlockFacts;
+ // Collect origins that escape the function in this block
(OriginEscapesFact),
+ // appended at the end to ensure t
@@ -396,6 +396,124 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -1202,5 +1202,241 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
kashika0112 wrote:
Could you clarify this more. As seen before, no program point is available
after the return statement. We alrea
@@ -1202,5 +1202,242 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -1202,5 +1202,242 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -582,3 +714,28 @@ void lifetimebound_ctor() {
}
(void)v;
}
+
+View lifetimebound_return_of_local(){
+ MyObj stack;
+ return Identity(stack); // expected-warning {{returning reference to stack
allocated object}}
+ // expected-note@-1 {{returned
@@ -396,6 +396,131 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -396,6 +396,131 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -396,6 +396,131 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -2808,6 +2808,17 @@ class LifetimeSafetyReporterImpl : public
LifetimeSafetyReporter {
<< UseExpr->getEndLoc();
}
+ void reportUseAfterReturn(const Expr *IssueExpr, const Expr *EscapeExpr,
+SourceLocation ExpiryLoc, Confidence C) over
@@ -120,6 +144,14 @@ class AnalysisImpl
LivenessInfo(&UF, LivenessKind::Must)));
}
+ // A return operation makes the origin live with definite confidence, as it
kashika0112 wrote:
Done
https://github.com/llvm/llvm-project/pu
@@ -74,11 +84,25 @@ class AnalysisImpl
/// one.
Lattice join(Lattice L1, Lattice L2) const {
LivenessMap Merged = L1.LiveOrigins;
-// Take the earliest UseFact to make the join hermetic and commutative.
-auto CombineUseFact = [](const UseFact &A,
-
@@ -74,11 +84,25 @@ class AnalysisImpl
/// one.
Lattice join(Lattice L1, Lattice L2) const {
LivenessMap Merged = L1.LiveOrigins;
-// Take the earliest UseFact to make the join hermetic and commutative.
-auto CombineUseFact = [](const UseFact &A,
-
@@ -53,6 +53,16 @@ struct Lattice {
}
};
+static SourceLocation GetFactLoc(const Fact &F) {
+ if (const auto *UF = F.getAs()) {
+return UF->getUseExpr()->getExprLoc();
+ }
+ if (const auto *OEF = F.getAs()) {
+return OEF->getEscapeExpr()->getExprLoc();
+ }
+ ret
@@ -53,6 +53,16 @@ struct Lattice {
}
};
+static SourceLocation GetFactLoc(const Fact &F) {
+ if (const auto *UF = F.getAs()) {
+return UF->getUseExpr()->getExprLoc();
+ }
kashika0112 wrote:
Done
https://github.com/llvm/llvm-project/pull/165370
_
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
@@ -43,7 +43,7 @@ struct LivenessInfo {
/// multiple uses along different paths, this will point to the use appearing
/// earlier in the translation unit.
/// This is 'null' when the origin is not live.
- const UseFact *CausingUseFact;
+ const Fact *CausingFact;
---
@@ -112,8 +114,17 @@ class LifetimeChecker {
for (const auto &[LID, Warning] : FinalWarningsMap) {
const Loan &L = FactMgr.getLoanMgr().getLoan(LID);
const Expr *IssueExpr = L.IssueExpr;
- Reporter->reportUseAfterFree(IssueExpr, Warning.UseExpr,
-
@@ -10728,8 +10728,19 @@ def warn_lifetime_safety_loan_expires_permissive :
Warning<
def warn_lifetime_safety_loan_expires_strict : Warning<
"object whose reference is captured may not live long enough">,
InGroup, DefaultIgnore;
+
+def warn_lifetime_safety_return_stack_a
@@ -93,6 +93,9 @@ class FactsGenerator : public
ConstStmtVisitor {
FactManager &FactMgr;
AnalysisDeclContext &AC;
llvm::SmallVector CurrentBlockFacts;
+ // Collect origins that escape the function in this block. These are handled
+ // at the end of the block to ensure
@@ -39,11 +39,11 @@ class Fact {
/// loan set.
OriginFlow,
/// An origin escapes the function by flowing into the return value.
-ReturnOfOrigin,
-/// An origin is used (eg. appears as l-value expression like DeclRefExpr).
Use,
/// A marker for a sp
@@ -112,8 +114,17 @@ class LifetimeChecker {
for (const auto &[LID, Warning] : FinalWarningsMap) {
const Loan &L = FactMgr.getLoanMgr().getLoan(LID);
const Expr *IssueExpr = L.IssueExpr;
- Reporter->reportUseAfterFree(IssueExpr, Warning.UseExpr,
-
https://github.com/kashika0112 updated
https://github.com/llvm/llvm-project/pull/165370
>From 2ab23aa98033c9f14cc6910786b6041c125b0726 Mon Sep 17 00:00:00 2001
From: Kashika Akhouri
Date: Tue, 28 Oct 2025 10:59:48 +
Subject: [PATCH 1/6] Adding use-after-return in Lifetime Analysis
---
...
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/kashika0112 updated
https://github.com/llvm/llvm-project/pull/165370
>From 2ab23aa98033c9f14cc6910786b6041c125b0726 Mon Sep 17 00:00:00 2001
From: Kashika Akhouri
Date: Tue, 28 Oct 2025 10:59:48 +
Subject: [PATCH 1/5] Adding use-after-return in Lifetime Analysis
---
...
@@ -93,6 +93,7 @@ class FactsGenerator : public
ConstStmtVisitor {
FactManager &FactMgr;
AnalysisDeclContext &AC;
llvm::SmallVector CurrentBlockFacts;
+ llvm::SmallVector EscapesInCurrentBlock;
kashika0112 wrote:
Done
https://github.com/llvm/llvm-proj
@@ -106,6 +120,63 @@ class LifetimeChecker {
/*ConfidenceLevel=*/CurConfidence};
}
+ void checkEscape(const OriginEscapesFact *OEF,
kashika0112 wrote:
The updated logic uses `checkExpiry` only to deal with `use-after-re
@@ -396,6 +396,118 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -396,6 +396,118 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -1202,5 +1202,242 @@ TEST_F(LifetimeAnalysisTest, LivenessOutsideLoop) {
EXPECT_THAT(Origins({"p"}), MaybeLiveAt("p1"));
}
+TEST_F(LifetimeAnalysisTest, SimpleReturnStackAddress) {
+ SetupTest(R"(
+MyObj* target() {
+ MyObj s;
+ MyObj* p = &s;
+ POINT(
@@ -396,6 +396,118 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -396,6 +396,118 @@ void loan_from_previous_iteration(MyObj safe, bool
condition) {
} // expected-note {{destroyed here}}
}
+//===--===//
+// Basic Definite Use-After-Return (Return-Stack-Addr
@@ -166,7 +169,8 @@ void FactsGenerator::VisitReturnStmt(const ReturnStmt *RS) {
if (const Expr *RetExpr = RS->getRetValue()) {
if (hasOrigin(RetExpr)) {
OriginID OID = FactMgr.getOriginMgr().getOrCreate(*RetExpr);
- CurrentBlockFacts.push_back(FactMgr.createFa
@@ -10730,6 +10730,7 @@ def warn_lifetime_safety_loan_expires_strict : Warning<
InGroup, DefaultIgnore;
def note_lifetime_safety_used_here : Note<"later used here">;
def note_lifetime_safety_destroyed_here : Note<"destroyed here">;
+def note_lifetime_safety_returned_here : N
@@ -42,6 +42,11 @@ class LifetimeSafetyReporter {
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
SourceLocation FreeLoc,
Confidence Confidence) {}
+
+ virtual void reportUseAfter
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/kashika0112 updated
https://github.com/llvm/llvm-project/pull/165370
>From 2ab23aa98033c9f14cc6910786b6041c125b0726 Mon Sep 17 00:00:00 2001
From: Kashika Akhouri
Date: Tue, 28 Oct 2025 10:59:48 +
Subject: [PATCH 1/2] Adding use-after-return in Lifetime Analysis
---
...
https://github.com/kashika0112 edited
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/kashika0112 ready_for_review
https://github.com/llvm/llvm-project/pull/165370
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
https://github.com/kashika0112 created
https://github.com/llvm/llvm-project/pull/165370
Adding "use-after-return" in Lifetime Analysis.
>From 2ab23aa98033c9f14cc6910786b6041c125b0726 Mon Sep 17 00:00:00 2001
From: Kashika Akhouri
Date: Tue, 28 Oct 2025 10:59:48 +
Subject: [PATCH] Adding us
58 matches
Mail list logo