-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
zig cc creating incorrect amd64 binary #22463
Comments
You're hitting a "shift out of bounds" UBSan trap. Your shift is at least incorrect in that it should be |
Oops, you're correct on that. I'm currently on my phone and thus I may be mistaken, but after applying your suggested change, the codegen apparent mistake produces the same output. |
Then I suspect it may be an upstream Clang bug. I get the ~same LLVM IR and machine code with Zig and vanilla Clang using |
Strange. https://godbolt.org/z/s4PbadEex multi-pane, zig and clang side by side, with the assembly code next to it. same build flags, same code (with @alexrp suggested change -- though if that failed i think a lot of other software would as well), still fails in the same way. |
It's probably specific to |
Zig Version
0.11.0
Steps to Reproduce and Observed Behavior
While working on an article, since I'm currently using my Steam Deck as a work machine, I decided to use Zig, as it is portable. I wrote my code, everything worked, and then I introduced an AND then IF check, and started getting illegal instruction outputs.
Code:
(godbolt where issue can be seen aswell: https://godbolt.org/z/1vvdjvb5a ctrl+f ud1)
It is
(ecx & (1<<31))!=0
that brings the trouble. Alternatives such asecx >> 31
or removing thehv
printf altogether work fine.I decided to analyze what was going on and I checked the binary in IDAPro. Then I noticed the following code:
This is visibly wrong. You would never randomly see
40 14
in x86 code especially after an undefined instruction.40
is justinc eax/rax
and14
is nothing we know of.67 0f b9
is an undefined instruction, and it seems that it is disassembling67 0f b9
as its own instruction.I tried putting a larger block of code into defense.ca-s x64 assembler and it produced this
Seems that
0f b9
is just a good old undefined instruction, appropriately defined asud1 r/m32
,Raise invalid opcode exception.
.Long story short, I do not think this is expected.
Expected Behavior
I would like the exact same behaviour as I got when i thought of alternatively trying using ecx>>31, which I think what gcc compiles to in this scenario aswell.
The text was updated successfully, but these errors were encountered: