https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90241
liu zhenyu <lzy20000419 at outlook dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |lzy20000419 at outlook dot com
--- Comment #3 from liu zhenyu <lzy20000419 at outlook dot com> ---
This overflow also causes wrong code on valid input when p points into an
object larger than 2^60 bytes. DSE deletes a live store:
#define OFF (1ULL << 61)
void g (char *p)
{
p[0] = 1; /* live */
__builtin_memset (p + OFF, 7, 1000); /* does not touch p[0] */
}
At -O1 and above, trunk (17.0.0 20261119) compiles g to only the memset call,
and the dse1 dump has "Deleted dead store: *p_3(D) = 1;". -fno-tree-dse keeps
the store. 4.8.5 keeps it; 4.9.0, 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 delete it at -O2.
FRE folds a load through the same wrap. This returns 1 instead of 7 with 8.5,
9.5, 11.5, 13.4, 14.4, 15.3, 16.2 and trunk (4.9.0, 5.4 and 7.5 emit the load):
#define OFF (1ULL << 61)
int h (char *p)
{
p[OFF] = 1;
__builtin_memset (p + OFF, 7, 1000);
return p[OFF];
}
And an overlapping memmove is turned into memcpy (16.2 and trunk emit jmp
memcpy at -O2):
#define A (1ULL << 60)
void f (char *p)
{
__builtin_memmove (p + A, p + A - 8, 1000); /* overlapping */
}
All three go through ao_ref_init_from_ptr_and_range (gcc/tree-ssa-alias.cc on
current trunk). r16-2670 (PR121323) added a coeffs_in_range_p check for the
ADDR_EXPR case (:926), but the POINTER_PLUS_EXPR case still does extra_offset
*= BITS_PER_UNIT (:918) without a check, and the final ref->offset +=
extra_offset + offset (:943) is not checked either. For p + 2^61 the bit offset
wraps to 0, so the memset looks like a store to p[0 .. 999] and kills p[0]. For
the memmove, gimple_fold_builtin_memory_op builds both refs with
ao_ref_init_from_ptr_and_size and asks refs_may_alias_p_1
(gcc/gimple-fold.cc:1157). The destination offset 2^60 bytes is 2^63 bits,
which wraps, while the source offset 2^60 - 8 bytes still fits, so the two refs
are considered disjoint. The POINTER_PLUS_EXPR case and the final sum probably
need the same kind of range check as the ADDR_EXPR case.
Godbolt (trunk and 4.8.5, DSE case): https://godbolt.org/z/fPoc6r7bM
Please consider adding the wrong-code keyword (PR121323 has it for the same
function).
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.