https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99207
Bug ID: 99207
Summary: #pragma pack(1) and __int128 lead to bad optimization
under O2 and higher optimization
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: zhan3299 at purdue dot edu
Target Milestone: ---
Following code behaves differently with different optimizations.
$ cat test.c
#pragma pack(1)
struct {
char a;
__int128 b;
} c;
__int128 *d = &c.b;
int main() { *d = 0; }
In short, with O2 and higher optimization, gcc tries to use xmm register to
initialize *d. However, as #pragma pack(1) enforces a different alignments, xmm
operation will trigger a segment fault.
O0 (program returns 0): https://godbolt.org/z/nc997z
O1 (program returns 0): https://godbolt.org/z/GqGfnf
O2 (program returns 139): https://godbolt.org/z/4PWKMo
O3 (program returns 139): https://godbolt.org/z/Y81sKK
Os (program returns 139): https://godbolt.org/z/M3KGnh
With asan, we can get: https://godbolt.org/z/34oW7e
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x0000004010ae bp
0x000000000000 sp 0x7ffd361619b8 T0)
==1==The signal is caused by a READ memory access.
==1==Hint: this fault was caused by a dereference of a high value address (see
register values below). Dissassemble the provided pc to learn which register
was used.
#0 0x4010ae in main example.c:10
#1 0x7f8c4b3b60b2 in __libc_start_main
(/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
#2 0x40111d in _start (/app/output.s+0x40111d)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV example.c:10 in main
==1==ABORTING