public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103887] New: -fsanitize=shift affects constness of an expression
@ 2022-01-01 23:47 mike@mk-sys.cz
2022-01-02 0:06 ` [Bug c/103887] " pinskia at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: mike@mk-sys.cz @ 2022-01-01 23:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
Bug ID: 103887
Summary: -fsanitize=shift affects constness of an expression
Product: gcc
Version: 11.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: mike@mk-sys.cz
Target Milestone: ---
This simplified testcase
---------------------------------------------------------------------------
#include <stdint.h>
#define ASSERT_ON_COMPILE_SELECTOR_SIZE(e) \
_Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0)) || \
(sizeof(e) <= 2), "problem")
int f(uint16_t tr)
{
ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
return 0;
}
---------------------------------------------------------------------------
builds cleanly with "-O2" but fails with "-O2 -fsanitize=shift":
---------------------------------------------------------------------------
mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -c foo.c
mike@lion:/tmp/gcc> gcc-11 -Wall -Wextra -O2 -fsanitize=shift -c foo.c
foo.c: In function ‘f’:
foo.c:4:72: error: expression in static assertion is not constant
4 | _Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0))
|| \
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
5 | (sizeof(e) <= 2), "problem")
| ~~~~~~~~~~~~~~~~
foo.c:9:9: note: in expansion of macro ‘ASSERT_ON_COMPILE_SELECTOR_SIZE’
9 | ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---------------------------------------------------------------------------
I was able to reproduce the same with various gcc version from gcc7 to gcc12.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/103887] -fsanitize=shift affects constness of an expression
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
@ 2022-01-02 0:06 ` pinskia at gcc dot gnu.org
2022-01-02 0:25 ` mike@mk-sys.cz
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-02 0:06 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |DUPLICATE
Status|UNCONFIRMED |RESOLVED
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The problem is rather __builtin_constant_p not resolving to a constant. This is
a dup of bug 79482.
As shown by:
#include <stdint.h>
uint16_t f1(void);
#define ASSERT_ON_COMPILE_SELECTOR_SIZE(e) \
_Static_assert(__builtin_constant_p(e) ? ((f1()) >> 16) == 0 : \
(sizeof(f1()) <= 2), "problem")
int f(uint16_t tr)
{
ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
return 0;
}
*** This bug has been marked as a duplicate of bug 79482 ***
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/103887] -fsanitize=shift affects constness of an expression
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
2022-01-02 0:06 ` [Bug c/103887] " pinskia at gcc dot gnu.org
@ 2022-01-02 0:25 ` mike@mk-sys.cz
2022-01-02 0:45 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: mike@mk-sys.cz @ 2022-01-02 0:25 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
--- Comment #2 from Michal Kubecek <mike@mk-sys.cz> ---
(In reply to Andrew Pinski from comment #1)
> The problem is rather __builtin_constant_p not resolving to a constant. This
> is a dup of bug 79482.
There is a difference: your modified testcase fails to compile regardless of
-fsanitize=shift while mine fails only with this option and succeeds without
it. If __builtin_constant_p(tr) not being handled as constant were the problem,
the result should not depend on presence of "-fsanitize=shift".
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/103887] -fsanitize=shift affects constness of an expression
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
2022-01-02 0:06 ` [Bug c/103887] " pinskia at gcc dot gnu.org
2022-01-02 0:25 ` mike@mk-sys.cz
@ 2022-01-02 0:45 ` pinskia at gcc dot gnu.org
2022-01-02 1:34 ` mike@mk-sys.cz
2022-01-02 1:42 ` pinskia at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-02 0:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Michal Kubecek from comment #2)
> (In reply to Andrew Pinski from comment #1)
> > The problem is rather __builtin_constant_p not resolving to a constant. This
> > is a dup of bug 79482.
>
> There is a difference: your modified testcase fails to compile regardless of
> -fsanitize=shift while mine fails only with this option and succeeds without
> it. If __builtin_constant_p(tr) not being handled as constant were the
> problem,
> the result should not depend on presence of "-fsanitize=shift".
Right, but the problem is __builtin_constant_p is not resolved to 0 forcing
what is inside the static_assert to be non-constant in both cases.
Even take a look at this:
#include <stdint.h>
#define ASSERT_ON_COMPILE_SELECTOR_SIZE(e) \
_Static_assert(((__builtin_constant_p(e) && ((e) >> 16) == 0)) || \
(sizeof(e) <= 2), "problem")
int f(uint32_t tr)
{
ASSERT_ON_COMPILE_SELECTOR_SIZE(tr);
return 0;
}
The static assert fails but fails because the expression is non-constant at -O2
and above. This is similar to your original case except without using
-fsanitize=shift even.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/103887] -fsanitize=shift affects constness of an expression
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
` (2 preceding siblings ...)
2022-01-02 0:45 ` pinskia at gcc dot gnu.org
@ 2022-01-02 1:34 ` mike@mk-sys.cz
2022-01-02 1:42 ` pinskia at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: mike@mk-sys.cz @ 2022-01-02 1:34 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
--- Comment #4 from Michal Kubecek <mike@mk-sys.cz> ---
It's probably even more complicated as
#define ASSERT_ON_COMPILE_SELECTOR_SIZE(expr) \
_Static_assert( \
__builtin_choose_expr(__builtin_constant_p(expr), \
((expr) >> 16) == 0, \
sizeof(expr) <= 2), \
"problem")
works as expected (tested with "uint16_t tr", "uint32_t tr", 60000 and 70000)
even if the documentation says first argument of __builtin_choose_expr() has
to be a constant expression.
As this is nicer than the original code, I guess I'll use it and hope it is
not just a happy coincidence that it works.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/103887] -fsanitize=shift affects constness of an expression
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
` (3 preceding siblings ...)
2022-01-02 1:34 ` mike@mk-sys.cz
@ 2022-01-02 1:42 ` pinskia at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-02 1:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103887
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Michal Kubecek from comment #4)
> As this is nicer than the original code, I guess I'll use it and hope it is
> not just a happy coincidence that it works.
It is not, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19449#c6 (since GCC
4.9.0) for the commit which fixed the issue with __builtin_choose_expr and
__builtin_constant_p. Basically __builtin_constant_p(x) is forced to false if x
was not a constant expression. _Static_assert folding should be handled the
same way and that is what PR 79482 is about really.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-01-02 1:42 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-01 23:47 [Bug c/103887] New: -fsanitize=shift affects constness of an expression mike@mk-sys.cz
2022-01-02 0:06 ` [Bug c/103887] " pinskia at gcc dot gnu.org
2022-01-02 0:25 ` mike@mk-sys.cz
2022-01-02 0:45 ` pinskia at gcc dot gnu.org
2022-01-02 1:34 ` mike@mk-sys.cz
2022-01-02 1:42 ` pinskia at gcc dot gnu.org
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).