| Issue |
233465
|
| Summary |
[clang][ObjC] @finally runs at end of enclosing scope instead of end of @try on windows-msvc (GNUstep runtime)
|
| Labels |
|
| Assignees |
|
| Reporter |
Alepacho
|
Labels: clang:codegen, objective-c, platform:windows, miscompilation
When targeting `*-pc-windows-msvc` with the GNUstep runtime, the `@finally` block of a `@try` statement is not executed when the `@try` statement completes normally. It is executed only when the *enclosing* scope exits, so code after the `@try` statement runs before the `@finally` body.
```objc
// late_finally.m
int f(void) {
int x = 0;
@try {
} @finally {
x = 1;
}
return x; // expected 1
}
```
```
clang -fobjc-runtime=gnustep-2.0 -fobjc-exceptions -fexceptions -O0 -S -emit-llvm late_finally.m
```
Generated IR (clang 23.1.3): `x` is loaded for the `return` **before** the outlined `@finally` (`?fin$0`) is called, so `f()` returns 0.
```llvm
define dso_local i32 @f() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
call void (...) @llvm.localescape(ptr %1)
store i32 0, ptr %1, align 4
%3 = load i32, ptr %1, align 4 ; return value read here
store i32 1, ptr %2, align 4
%4 = call ptr @llvm.localaddress()
%5 = load i32, ptr %2, align 4
%6 = icmp ne i32 %5, 0
%7 = zext i1 %6 to i8
call void @"?fin$0@0@f@@"(i8 noundef %7, ptr noundef %4) ; @finally runs here
%8 = load i32, ptr %2, align 4
switch i32 %8, label %10 [
i32 1, label %9
]
9:
ret i32 %3
...
```
Wrapping the statement in its own block gives the expected order (`?fin$0` is called, then `x` is loaded):
```objc
int f(void) {
int x = 0;
{ @try { } @finally { x = 1; } }
return x;
}
```
It looks like the `@finally` cleanup is pushed onto the EH stack but not popped at the end of the `@try` statement. In a real project, a check of a flag set in `@finally`, placed right after the `@try` statement, failed at runtime for this reason. The same happens when the `@try` body throws and the exception is caught by an inner handler.
- Reproduces with: `x86_64-pc-windows-msvc` and `i686-pc-windows-msvc`, `-fobjc-runtime=gnustep-2.0` and `gnustep-1.9`.
- Does **not** reproduce with `x86_64-unknown-linux-gnu`: there the `@finally` runs before the load.
Versions: clang 22.1.8 and 23.1.3 (official Windows release builds), Windows 11 x64.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs