https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127971
Bug ID: 127971
Summary: wrong code at -O1 and above reading the bytes of an
array of scalar_storage_order structs
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kyuwoncho18 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
struct __attribute__((scalar_storage_order("big-endian"))) S { unsigned x; };
static const struct S a[1] = { { 1 } };
int main(void) { return *(const unsigned char *)a; }
godbolt: https://godbolt.org/z/6GsKseMax
With -O0 it returns 0 (a[0].x is big-endian, so its first byte is 0), but with
-O1, -O2, -O3, -Os or -Og it returns 1 (GCC 6.5 through 16.2 as well). The -O2
binary itself stores a as the bytes 00 00 00 01: reading the same pointer
through a noipa function returns 0. The same object declared as a single struct
instead of an array returns 0 at every level. Related: PR127925.
Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.