public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions
@ 2024-01-23 19:19 janschultke at googlemail dot com
2024-01-23 19:32 ` [Bug c++/113565] " pinskia at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: janschultke at googlemail dot com @ 2024-01-23 19:19 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
Bug ID: 113565
Summary: __builtin_clz(0) is undefined behavior, but not
detected in constant expressions
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: janschultke at googlemail dot com
Target Milestone: ---
Code to reproduce
=================
static_assert(__builtin_clz(0));
Actual output (https://godbolt.org/z/v6v5nGxv8)
===============================================
None.
Expected output (Clang)
=======================
<source>:1:15: error: static assertion expression is not an integral constant
expression
1 | static_assert(__builtin_clz(0));
| ^~~~~~~~~~~~~~~~
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/113565] __builtin_clz(0) is undefined behavior, but not detected in constant expressions
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
@ 2024-01-23 19:32 ` pinskia at gcc dot gnu.org
2024-01-23 19:33 ` pinskia at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-23 19:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2024-01-23
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This should fix it (but I am have not tested it either and I am not 100% sure
we want/need this):
```
[apinski@xeond2 gcc]$ git diff fold-const-call.cc
diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc
index 47bf8d64391..c9fa51e35ee 100644
--- a/gcc/fold-const-call.cc
+++ b/gcc/fold-const-call.cc
@@ -1031,7 +1031,7 @@ fold_const_call_ss (wide_int *result, combined_fn fn,
const wide_int_ref &arg,
tmp = TYPE_PRECISION (arg_type);
else if (!CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
tmp))
- tmp = TYPE_PRECISION (arg_type);
+ return false;
*result = wi::shwi (tmp, precision);
return true;
}
@@ -1046,7 +1046,7 @@ fold_const_call_ss (wide_int *result, combined_fn fn,
const wide_int_ref &arg,
tmp = TYPE_PRECISION (arg_type);
else if (!CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (arg_type),
tmp))
- tmp = TYPE_PRECISION (arg_type);
+ return false;
*result = wi::shwi (tmp, precision);
return true;
}
```
Confirmed.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/113565] __builtin_clz(0) is undefined behavior, but not detected in constant expressions
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
2024-01-23 19:32 ` [Bug c++/113565] " pinskia at gcc dot gnu.org
@ 2024-01-23 19:33 ` pinskia at gcc dot gnu.org
2024-01-24 9:54 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-23 19:33 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note starting in GCC 14, it is better to use __builtin_clzg with the 2
arguments anyways so you can control exactly what value you want/need for 0.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/113565] __builtin_clz(0) is undefined behavior, but not detected in constant expressions
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
2024-01-23 19:32 ` [Bug c++/113565] " pinskia at gcc dot gnu.org
2024-01-23 19:33 ` pinskia at gcc dot gnu.org
@ 2024-01-24 9:54 ` redi at gcc dot gnu.org
2024-01-24 10:04 ` janschultke at googlemail dot com
2024-01-24 10:16 ` janschultke at googlemail dot com
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-24 9:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I'm not sure it's reasonable to expect an error here. The C++ standard says
nothing about whether __builtin_clz is a constant expression, obviously.
So I'm changing this to severity=enhancement.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/113565] __builtin_clz(0) is undefined behavior, but not detected in constant expressions
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
` (2 preceding siblings ...)
2024-01-24 9:54 ` redi at gcc dot gnu.org
@ 2024-01-24 10:04 ` janschultke at googlemail dot com
2024-01-24 10:16 ` janschultke at googlemail dot com
4 siblings, 0 replies; 6+ messages in thread
From: janschultke at googlemail dot com @ 2024-01-24 10:04 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
--- Comment #4 from Jan Schultke <janschultke at googlemail dot com> ---
I would expect an error here because things that are undefined behavior are
generally supposed to fail in constant expressions. I don't see a good reason
why builtins should be exempt from that rule.
The lack of diagnostic has cost me a few minutes of debugging yesterday. I had
a static_assert:
> static_assert(my_function(0u) == 32);
This succeeded at compile time, but my_function(0) would return 0 at run-time
as a result of passing through to __builtin_clz. UBSan may have caught it, but
regardless, it's not sane to have different results inside/outside constant
expressions like that.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/113565] __builtin_clz(0) is undefined behavior, but not detected in constant expressions
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
` (3 preceding siblings ...)
2024-01-24 10:04 ` janschultke at googlemail dot com
@ 2024-01-24 10:16 ` janschultke at googlemail dot com
4 siblings, 0 replies; 6+ messages in thread
From: janschultke at googlemail dot com @ 2024-01-24 10:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113565
--- Comment #5 from Jan Schultke <janschultke at googlemail dot com> ---
You can reproduce this as follows:
> static_assert(__builtin_clz(0u) == 32);
>
> unsigned x = 0;
>
> int main() {
> return __builtin_clz(x);
> }
For base x86_64, GCC emits: (https://godbolt.org/z/nqzYrTWd1)
> main:
> bsr eax, DWORD PTR x[rip]
> xor eax, 31
> ret
> x:
> .zero 4
Even though __builtin_clz(0u) == 32 passes, this program returns 63. This is
obviously not in the interest of the developer, regardless of what the standard
mandates.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-01-24 10:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 19:19 [Bug c++/113565] New: __builtin_clz(0) is undefined behavior, but not detected in constant expressions janschultke at googlemail dot com
2024-01-23 19:32 ` [Bug c++/113565] " pinskia at gcc dot gnu.org
2024-01-23 19:33 ` pinskia at gcc dot gnu.org
2024-01-24 9:54 ` redi at gcc dot gnu.org
2024-01-24 10:04 ` janschultke at googlemail dot com
2024-01-24 10:16 ` janschultke at googlemail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).