https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87042
liu zhenyu <lzy20000419 at outlook dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |lzy20000419 at outlook dot com
--- Comment #5 from liu zhenyu <lzy20000419 at outlook dot com> ---
This overflow is not only a UBSan report. It also causes wrong code on valid
input once an object is larger than 2^60 bytes (x86_64):
static const char a[1ULL << 62] = { 1 };
const char *volatile keep = a;
int f (void) { return a[1ULL << 61]; }
$ gcc -O1 -S t.c
f:
movl $1, %eax
ret
a[1ULL << 61] is 0, but FRE folds the load to the value of a[0]; the fre1 dump
already has "return 1;". With -fno-tree-fre the load is kept. In
fully_constant_vn_reference_p (gcc/tree-ssa-sccvn.cc:1696 on current trunk) the
byte offset is passed to fold_ctor_reference as off * BITS_PER_UNIT, and 2^61 *
8 wraps to 0 in poly_int64. The bit offset probably has to be computed in a
wider type, with the fold skipped when it does not fit, similar to the range
check that r16-2670 (PR121323) added to ao_ref_init_from_ptr_and_range.
On x86-64, 4.4.7 and 4.9.4 emit the load. 5.1, 5.4, 6.5, 7.5, 8.5, 9.5, 10.5,
11.5, 12.5, 13.4, 14.4, 15.3, 16.2 and trunk (17.0.0 20261119) return 1, so
this is a wrong-code regression since GCC 5.
Godbolt (trunk and 4.9.4): https://godbolt.org/z/jrcEd6Yae
Suggested fields: Keywords wrong-code; Known to work 4.4.7, 4.9.4; Known to
fail 5.1.0, 5.4.0, 6.5.0, 7.5.0, 8.5.0, 9.5.0, 10.5.0, 11.5.0, 12.5.0, 13.4.0,
14.4.0, 15.3.0, 16.2.0, 17.0.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.