-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
illegal instruction from zig cc
product
#4830
Comments
output:
|
I posted a workaround for this on the IRC channel. This doesn't fix the underlying issue however. This is what I posted: (02:25:09 AM) wilsonk: watzon: it appears that zig cc is perhaps defaulting to some stricter casting checks or something. There is a 'ud2' illegal instruction being inserted into the function 'hash_string' in packcc.c when zig cc is run. This illegal instruction has two ops after it that designate the file and line of the issue (I can't seem to see those values in gdb though)...anyways, after hash_string iterates over 5 incoming letters of the string to hash then the hash exceeds the size of an int. This causes the 'ud2' instruction to run. (02:27:38 AM) wilsonk: The fix: change the 'h' variable in hash_string to a long and then cast to an int for the return (ie. int i=0; long h=0; ... return (int)h; ). That should fix things but I am not exactly sure how to fix the overall problem or if andrew would still prefer this stricter check on casting? (02:28:30 AM) wilsonk: watzon: then your test grammar should work and the example at the bottom of the packcc documentation also works So the fixed code would be: static int hash_string(const char *str) {
int i = 0;
long h = 0;
for (i = 0; str[i]; i++) {
h = h * 31 + str[i];
}
return (int)h;
} |
Zig is compiling the c code with clang's undefined behavior sanitizer when built in debug mode. So when you overflow h as a signed type, an illegal instruction is executed. Is the hash depending on wrap around semantics? If it is, then making h unsigned is the correct fix, since unsigned overflow is well defined to wrap around. |
TL;DR
Why?The
If you check out the ASM produced by
Figuring out why such checks are always enabled in |
I can speak to this. Note that the presence of |
I was shown by @nektro that there's some helpful, relevant info in the zig FAQ here -> why-do-i-get-illegal-instruction-when-using-with-zig-cc-to-build-c-code |
In debug mode, zig cc enables `-fsanitize-debug` which causes the program to get killed with SIGILL or SIGTRAP because of undefined behavior. See ziglang/zig#4830 and llvm/llvm-project#91144
In debug mode, zig cc enables `-fsanitize-debug` which causes the program to get killed with SIGILL or SIGTRAP because of undefined behavior. See ziglang/zig#4830 and llvm/llvm-project#91144
first reported by watzon on IRC:
test.peg
shell session
./packcc test.peg zsh: illegal hardware instruction ./packcc < test.peg
output:
zig targets
output:
zig cc -### -o packcc src/packcc.c
The text was updated successfully, but these errors were encountered: