-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalid codegen when block
ends with lent
#20107
Comments
@ringabout that's not specific to ARC/ORC (the example doesn't use ARC/ORC) and I don't think it's OS / Arch specific either (also happens on my linux), it's just codegen |
Does it work on Windows by accident, or is the code still technically undefined behaviour? On macOS, Clang may start deleting surrounding code with -O3, as it optimizes more and more sections away. |
We've discovered a critical `lent` issue affecting Nim 1.6: nim-lang/Nim#20107
Yeah, it is an undefined behaviour in c, optimizations may free the stack variables when out of the scope. https://stackoverflow.com/questions/13888268/what-happens-when-a-variable-goes-out-of-scope |
I checked the code generated by if expr (which works). Another possible solution is to deref the pointer in the block scope like below: {
tyObject_Foo__9bOqVyAHO8e6D7f036GENHQ i;
nimZeroMem((void*)(&i), sizeof(tyObject_Foo__9bOqVyAHO8e6D7f036GENHQ));
c__test_8(42ULL, (&i));
T1_ = x__test_20((&i)); // Note: This returns a pointer to `i` (`lent`)
f = (*T1_);
} |
@Araq The fix is incomplete and leads to compiler crashes with
The problem can be visualized, by adding this helper function to
And then, in Compiling with
Both of these cases are problematic. |
* fixes the regressions caused by the fix for #20107 [backport]
…nim-lang#20287) * fixes the regressions caused by the fix for nim-lang#20107 [backport]
When a
block
ends with an inlined call to alent
returning function, C code exhibiting a use-after-free bug is generated.Example
test.nim
Build with
-O3
to force aggressive optimizations.Current Output
nimcache/@mtest.nim.c
Expected Output
Possible Solution
lent
.Beside that, there are a few workarounds for individual occurrences. The challenge is to find all of them (maybe there are similar issues outside
block
contexts).block
with a function calling into alent
.lent
(especially prevalent inoptions.nim
>get
).lent
is not inlined (noinline
, or putting logs inside).Additional Information
Discovered as part of status-im/nimbus-eth2#3907 on GitHub Actions (
macos-11
andmacos-12
) and reproduced on macOS 12.5 with Xcode 13.4.1 on Nim compiler 1.6 (5f61f15) on an Intel MacBook Pro (16-inch, 2019).The text was updated successfully, but these errors were encountered: