https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129137
Bug ID: 129137
Summary: wrong code at -O1: return slot optimization passes a
misaligned #pragma pack member as the return buffer,
callee movaps crashes (x86-64)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: jeffchen006 at gmail dot com
Target Milestone: ---
Target: x86_64-pc-linux-gnu
int printf(const char *, ...);
struct B { long double x, y; };
#pragma pack(1)
struct P { char c; struct B b; };
struct B g = {1, 2};
struct B get(void) { return g; }
struct B (*fp)(void) = get;
int main(void) {
struct P q;
q.b = fp();
printf("%d\n", (int)q.b.y);
}
$ gcc -O0 t.c && ./a.out
2
$ gcc -O1 t.c && ./a.out
Segmentation fault
-O2 and -O3 too, and g++ with the same code. -Os and -O1 -fdisable-tree-retslot
are fine.
q.b = fp() gets the return slot optimization (tree-nrv.cc; dest_safe_for_nrv_p
only looks at aliasing). expand_call in calls.cc refuses an under-aligned
target only when targetm.slow_unaligned_access is true, which it is not on
x86-64. So the caller passes leaq 17(%rsp), %rdi, and the callee stores the
32-byte B with movaps %xmm0, (%rdi), which needs 16-byte alignment. The
alignment of the return buffer is an ABI requirement here, not a performance
question.
At -O1 it bisects to r8-4009-gaf8630301d5 (x86 move cost change), which
probably only exposes it; at -O2/-O3 GCC 7.5 crashes as well. The call goes
through fp only so that get is not inlined.
gcc version 17.0.0 20261128 (experimental), r17-6781-gd10e6c3818a,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release