Skip to content

Commit 8b25d76

Browse files
committed
fix-missing-lifetimeends-for-params
1 parent d44d329 commit 8b25d76

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

clang/lib/Analysis/CFG.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,12 @@ std::unique_ptr<CFG> CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) {
16661666
assert(Succ == &cfg->getExit());
16671667
Block = nullptr; // the EXIT block is empty. Create all other blocks lazily.
16681668

1669+
// Add parameters to the initial scope to handle their dtos and lifetime ends.
1670+
LocalScope *paramScope = nullptr;
1671+
if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D))
1672+
for (ParmVarDecl *PD : FD->parameters())
1673+
paramScope = addLocalScopeForVarDecl(PD, paramScope);
1674+
16691675
if (BuildOpts.AddImplicitDtors)
16701676
if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
16711677
addImplicitDtorsForDestructor(DD);
@@ -2246,6 +2252,9 @@ LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
22462252
if (!VD->hasLocalStorage())
22472253
return Scope;
22482254

2255+
if (isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType())
2256+
return Scope;
2257+
22492258
if (!BuildOpts.AddLifetime && !BuildOpts.AddScopes &&
22502259
!needsAutomaticDestruction(VD)) {
22512260
assert(BuildOpts.AddImplicitDtors);

clang/test/Analysis/scopes-cfg-output.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,12 +1437,14 @@ void test_cleanup_functions() {
14371437
// CHECK-NEXT: 4: return;
14381438
// CHECK-NEXT: 5: CleanupFunction (cleanup_int)
14391439
// CHECK-NEXT: 6: CFGScopeEnd(i)
1440+
// CHECK-NEXT: 7: CFGScopeEnd(m)
14401441
// CHECK-NEXT: Preds (1): B3
14411442
// CHECK-NEXT: Succs (1): B0
14421443
// CHECK: [B2]
14431444
// CHECK-NEXT: 1: return;
14441445
// CHECK-NEXT: 2: CleanupFunction (cleanup_int)
14451446
// CHECK-NEXT: 3: CFGScopeEnd(i)
1447+
// CHECK-NEXT: 4: CFGScopeEnd(m)
14461448
// CHECK-NEXT: Preds (1): B3
14471449
// CHECK-NEXT: Succs (1): B0
14481450
// CHECK: [B3]

clang/test/Sema/warn-lifetime-safety.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,14 @@ TriviallyDestructedClass* trivial_class_uar () {
529529
return ptr; // expected-note {{returned here}}
530530
}
531531

532-
// FIXME: No lifetime warning for this as no expire facts are generated for parameters
533532
const int& return_parameter(int a) {
534-
return a;
533+
return a; // expected-warning {{address of stack memory is returned later}}
534+
// expected-note@-1 {{returned here}}
535535
}
536536

537-
// FIXME: No lifetime warning for this as no expire facts are generated for parameters
538537
int* return_pointer_to_parameter(int a) {
539-
return &a;
538+
return &a; // expected-warning {{address of stack memory is returned later}}
539+
// expected-note@-1 {{returned here}}
540540
}
541541

542542
const int& return_reference_to_parameter(int a)
@@ -788,9 +788,17 @@ const MyObj& lifetimebound_return_ref_to_local() {
788788
// expected-note@-1 {{returned here}}
789789
}
790790

791-
// FIXME: Fails to diagnose UAR when a reference to a by-value param escapes via the return value.
792791
View lifetimebound_return_of_by_value_param(MyObj stack_param) {
793-
return Identity(stack_param);
792+
return Identity(stack_param); // expected-warning {{address of stack memory is returned later}}
793+
// expected-note@-1 {{returned here}}
794+
}
795+
796+
void LambdaUARParam() {
797+
auto lambda = [](MyObj stack_param) {
798+
return Identity(stack_param); // expected-warning {{address of stack memory is returned later}}
799+
// expected-note@-1 {{returned here}}
800+
};
801+
lambda(MyObj{});
794802
}
795803

796804
// FIXME: Fails to diagnose UAF when a reference to a by-value param escapes via an out-param.

clang/unittests/Analysis/FlowSensitive/LoggerTest.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,18 @@ recordState(Elements=8, Branches=2, Joins=1)
149149
enterElement(return b ? p : q;)
150150
transfer()
151151
recordState(Elements=9, Branches=2, Joins=1)
152+
enterElement((Lifetime ends))
153+
transfer()
154+
recordState(Elements=10, Branches=2, Joins=1)
155+
enterElement((Lifetime ends))
156+
transfer()
157+
recordState(Elements=11, Branches=2, Joins=1)
158+
enterElement((Lifetime ends))
159+
transfer()
160+
recordState(Elements=12, Branches=2, Joins=1)
152161
153162
enterBlock(0, false)
154-
recordState(Elements=9, Branches=2, Joins=1)
163+
recordState(Elements=12, Branches=2, Joins=1)
155164
156165
endAnalysis()
157166
)");

0 commit comments

Comments
 (0)