-
-
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
Illegal instruction in code generated from C with default optimisation. #17065
Comments
You're hitting the undefined behavior sanitizer that Zig enables by default. You can disable it by passing Note: the relevant issue for more feedback when UB is hit is #5163 |
Okay, then please stop wasting my time making ubisan look like a compiler bug. Lets change the expected behaviour to something like current GCC/Clang (the below) and not something that throws a tantrum when you don't optimise.
|
That is the plan, see #5163 |
@rdebath maybe you should use a different programming language if you can't write correct C code. |
It's 100% legal to do that according to the C standard, so what Zig does is fine and definitly better than what gcc/clang do by default. Zig is not breaking code here, code was already broken, the defect is just made visible. |
I assume that was the April first release, because they're not usually so stupid as to destroy their "quality of implementation" reputation. The simple fact is that for integers the intent of the standard was to allow different physical representations of integers along with their defined by the CPU method of implementation. This was reduced to "sign and magnitude", "ones complement" and "twos complement" in later versions of the standard. AIUI it'll be reduced to just "twos complement" in the next version as nobody uses anything else. Now, this might seem like there's no need for something like ubsan (on integers) after this, but that's not true because some of us like Personally, I'd much prefer Luckily, both sets of compiler writers seem to understand that sometimes you really do want the defined-by-the-cpu operation of integers (even if it disables some optimisations) so So, in summary, for signed integers, the UB is supposed to be as defined by the CPU so the compiler writer can use the easiest/best implementation of signed integers on a particular CPU. The essential claim is that it's easier to not skip the optimisation even if a signed integer might overflow might invalidate it. This it technically a valid rationalisation of the reason the UB exists in the first place even if it is (probably) not the original intent. (Butt monkeys are not a valid rationalisation!) One last thing, they didn't give a list of implementations because at least some of them were mathematicians and they knew there are number representations that have never been used in computer hardware ... but they could be. PS: @squeek502 Sorry for the noise. |
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 |
The problem is nobody asks that question because the answer is already obvious. The more correct thing to do on an x86, as I mentioned before, is an PS: I guess "Why is zig's C compiler broken" isn't a question you want to answer though 😈 |
This is behaviour coming from LLVM/clang, not Zig, if you want to change how UBSAN works upstream in clang, i highly encourage you to try |
@Beyley Actually, no. Clang's UBsan has already been changed to give verbose messages and Zig is planned see #5163 And just to be clear, squeek502 answered this issue in full right at the beginning. |
Zig Version
0.11.0
Steps to Reproduce and Observed Behavior
Given this C code.
Run like this:
If you turn on optimisation it works, if you turn on
-fwrapv
there is no SIGILL.Expected Behavior
I would expect somewhat consistent behaviour.
It's likely you'll do this, as the optimiser is configured to assume integers are unbounded when it's turned on.
(This is what happens with
-O2
)It should probably do this if you're actually following POSIX. (Or if the optimiser is actually turned off)
The text was updated successfully, but these errors were encountered: