-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
UBSan output is incorrect for non power-of-2 sized _BitInt types #88093
Comments
@llvm/issue-subscribers-bug Author: Björn Pettersson (bjope)
Consider this test case:
```
unsigned foo(unsigned _BitInt(44) x) {
_BitInt(9) c = -2;
return x >> c;
}
int main() {
/app/example.c:3:14: runtime error: shift exponent 510 is too large for 64-bit type 'unsigned _BitInt(44)'
|
Probably a bit related to #64100 |
I still think it looks weird when it says that the 44-bit bitint is a See the example here, https://godbolt.org/z/E6j7E6GWb , resulting in:
So I think the fix in #93612 only solved parts of the problem described. |
The values displayed are correct, but the bitness itself might need to be corrected. However i might say the current message is better in a sense as it gives information about storage size for the _BitInt, but it needs to be properly formulated. As for bit int with N above 130 it has other issues. |
I don't think "stored as" is of much interest in these error messages. The size of the bitint type should be more interesting (and you can derive the type store size from knowing the size in bits of the _BitInt, but not the other way around). One thing that looks a bit weird right now is this example: https://godbolt.org/z/czsjszzxa When shifting a _BitInt(64) 67 steps to the left we get:
But when shifting a _BitInt(65) 67 steps to the left we get this more confusing message:
I think the above is due to comparing the shift count with the weird rounded size from the TypeInfo rather using the width of the _BitInt. That wouldn't happen if using the size in bits of the bitint instead of using the log2 size. |
Then what type message do you propose? At least in my eyes, it is important to distinguish those 2 cases. The more confusing case is this: https://godbolt.org/z/q8xfxPa67. |
It is UB to shift a _BitInt(65) more than 65 bits. That is why we report the ubsan shift out-of-bounds. The store type has nothing to do with it. I expect errors like these:
|
Consider this test case:
As shown by https://godbolt.org/z/M614qnhco , the result when compiling and running this program (with ubsan) is weird:
The expected error here would be "shift exponent -2 is negative". But it is not only the detection of isNegative for the exponent that is wrong, it also reports an incorrect bitwidth (64-bit type) for the shifted value.
Afaict the problem is that the TypeDescriptor encoding (compiler-rt/lib/ubsan/ubsan_value.h) encodes the _BitInt types like ordinary integer types:
But using log_2(bit width) does not work that well when having non-power-of-two bit widths.
One idea could be to introduce a new TK_BitInt kind, that encodes the bit wifth in some other way? Although I have no idea how big impact that has on the rest of the code.
This problem was found when running tests with the fix from #88004
The text was updated successfully, but these errors were encountered: