
Latest diff:
f1ab4c2677..adf9bc902b
We address two additional bugs here:
### Problem 1: Deactivated normal cleanup still runs, leading to
double-free
Consider the following:
```cpp
struct A { };
struct B { B(const A&); };
struct S {
A a;
B b;
};
int AcceptS(S s);
void Accept2(int x, int y);
void Test() {
Accept2(AcceptS({.a = A{}, .b = A{}}), ({ return; 0; }));
}
```
We add cleanups as follows:
1. push dtor for field `S::a`
2. push dtor for temp `A{}` (used by ` B(const A&)` in `.b = A{}`)
3. push dtor for field `S::b`
4. Deactivate 3 `S::b`-> This pops the cleanup.
5. Deactivate 1 `S::a` -> Does not pop the cleanup as *2* is top. Should
create _active flag_!!
6. push dtor for `~S()`.
7. ...
It is important to deactivate **5** using active flags. Without the
active flags, the `return` will fallthrough it and would run both `~S()`
and dtor `S::a` leading to **double free** of `~A()`.
In this patch, we unconditionally emit active flags while deactivating
normal cleanups. These flags are deleted later by the `AllocaTracker` if
the cleanup is not emitted.
### Problem 2: Missing cleanup for conditional lifetime extension
We push 2 cleanups for lifetime-extended cleanup. The first cleanup is
useful if we exit from the middle of the expression (stmt-expr/coro
suspensions). This is deactivated after full-expr, and a new cleanup is
pushed, extending the lifetime of the temporaries (to the scope of the
reference being initialized).
If this lifetime extension happens to be conditional, then we use active
flags to remember whether the branch was taken and if the object was
initialized.
Previously, we used a **single** active flag, which was used by both
cleanups. This is wrong because the first cleanup will be forced to
deactivate after the full-expr and therefore this **active** flag will
always be **inactive**. The dtor for the lifetime extended entity would
not run as it always sees an **inactive** flag.
In this patch, we solve this using two separate active flags for both
cleanups. Both of them are activated if the conditional branch is taken,
but only one of them is deactivated after the full-expr.
---
Fixes https://github.com/llvm/llvm-project/issues/63818
Fixes https://github.com/llvm/llvm-project/issues/88478
---
Previous PR logs:
1. https://github.com/llvm/llvm-project/pull/85398
2. https://github.com/llvm/llvm-project/pull/88670
3. https://github.com/llvm/llvm-project/pull/88751
4. https://github.com/llvm/llvm-project/pull/88884
41 lines
1.5 KiB
Objective-C
41 lines
1.5 KiB
Objective-C
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -fexceptions -disable-llvm-passes -o - %s | FileCheck %s
|
|
|
|
void test1(_Bool c) {
|
|
void test1_fn(void (^blk)(void));
|
|
__weak id weakId = 0;
|
|
test1_fn(c ? ^{ (void)weakId; } : 0);
|
|
|
|
// CHECK: [[CLEANUP_SAVE:%cond-cleanup.save.*]] = alloca ptr
|
|
// CHECK-NEXT: [[CLEANUP_COND:%.*]] = alloca i1
|
|
// CHECK-NEXT: [[CLEANUP_COND1:%.*]] = alloca i1
|
|
|
|
// CHECK: store i1 false, ptr [[CLEANUP_COND]]
|
|
// CHECK-NEXT: store i1 false, ptr [[CLEANUP_COND1]]
|
|
|
|
// CHECK: store ptr {{.*}}, ptr [[CLEANUP_SAVE]]
|
|
// CHECK-NEXT: store i1 true, ptr [[CLEANUP_COND]]
|
|
// CHECK-NEXT: store i1 true, ptr [[CLEANUP_COND1]]
|
|
|
|
// CHECK: invoke void @test1_fn(
|
|
// CHECK-NEXT: to label %[[INVOKE_CONT:.*]] unwind label %[[LANDING_PAD_LAB:.*]]
|
|
|
|
// CHECK: [[INVOKE_CONT]]:
|
|
// CHECK-NEXT: [[LOAD:%.*]] = load i1, ptr [[CLEANUP_COND1]]
|
|
// CHECK-NEXT: br i1 [[LOAD]], label %[[END_OF_SCOPE_LAB:.*]], label
|
|
|
|
// CHECK: [[END_OF_SCOPE_LAB]]:
|
|
// CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[CLEANUP_SAVE]]
|
|
// CHECK-NEXT: call void @llvm.objc.destroyWeak(ptr [[LOAD]])
|
|
// CHECK-NEXT: br label
|
|
|
|
// CHECK: [[LANDING_PAD_LAB]]:
|
|
// /* some EH stuff */
|
|
// CHECK: [[LOAD:%.*]] = load i1, ptr [[CLEANUP_COND]]
|
|
// CHECK-NEXT: br i1 [[LOAD]], label %[[EH_CLEANUP_LAB:.*]], label
|
|
|
|
// CHECK: [[EH_CLEANUP_LAB]]:
|
|
// CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[CLEANUP_SAVE]]
|
|
// CHECK-NEXT: call void @llvm.objc.destroyWeak(ptr [[LOAD]])
|
|
// CHECK-NEXT: br label
|
|
}
|