-
-
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
Generated C code (refc) uses &NULL->field
(undefined behavior)
#21509
Comments
It might be good to run the tests with I got this failure when running
|
&NULL->field
(undefined behavior)
&NULL->field
(undefined behavior)&NULL->field
(undefined behavior)
That's simply because zig cc has -fsanitize=undefined enabled by default, you can do the same with clang if you enable that option. SIGILL is specifically thrown when hitting a sanitizer. Also, as far as I know refc isn't exactly friendly to that sanitizer, ORC works much better in that regard as you said. |
Also likely a duplicate of #20961 |
Not duplicate. |
#20294 #20747 #20795 have more discussion of this pattern Nim has had of using a faux- https://reviews.llvm.org/D67122 describes LLVM/clang's view of this particular C rule. |
Description
I only discovered this bug with
zig cc
. I don't know why it works with clang.First, install
zigcc
from https://github.com/enthus1ast/zigccThen, set
zigcc
as c compiler & linkernim.cfg
this code will then SIGILL
othertest.nim
Relevant functions
@m..@s..@[email protected]@[email protected]@[email protected]
,new(Foo)
in Nimit simply zeros the memory of
Foo
, butfoo.timers
is not initialized.after that,
(*foo__othertest_30).timers)->Sup
is used when(*foo__othertest_30).timers)
is still NULL@mothertest.nim.c
:Nim Version
Nim Compiler Version 1.6.10 [Linux: amd64]
Current Output
Expected Output
Possible Solution
In codegen, use the ISO C function
offsetof
.Also, this bug only appears with
--mm:refc
.--mm:orc
doesn't have this problem.Additional Information
both clang and gcc by default allows accessing address of field of NULL pointer to struct.
I think this is undefined behavior in ISO C. gcc and clang treat this as taking the address of field. Zig has UBSan on by default, so this UB is caught.
https://en.cppreference.com/w/c/language/operator_member_access
P.S. I did not find the extension in https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/
The text was updated successfully, but these errors were encountered: