https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129226
Bug ID: 129226
Summary: [14/15/16/17 Regression] riscv64: store data race: t.s
= give () of packed {char; float} rewrites the next
member
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: hhhuang at smu dot edu.sg
Target Milestone: ---
Target: riscv64-*-*
Created attachment 66367
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66367&action=edit
preprocessed t.c (riscv64-linux-gnu-gcc-17 -O2 -pthread -E t.c)
Compiler Explorer (assembly only): https://godbolt.org/z/q4n9dqGcW
Note:
- fails at -O0, -O1, -O2, -O3, -Os: riscv64 16.2, trunk bc49f1e37014
(20261127)
- 10.2 to 16.1 and trunk 20261201 have the same ld and sd; 8.5 and 9.4,
also lp64d, use five sb (Compiler Explorer, assembly only)
- with -mabi=lp64 the struct comes back in a0 alone and the store is
five sb (from t.i, assembly only)
- -malign-data=natural hides it; -mno-strict-align does not
- trunk here is built with --disable-threads; pthreads come from glibc,
and Debian's 16.2 (thread model posix) aborts the same way
- not bisected; see the end for a test of the cause
$ riscv64-linux-gnu-gcc-17 -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc-17
Target: riscv64-linux-gnu
Configured with: ../gcc-17-20261127/configure --prefix=/w/out/gcc-trunk
--program-suffix=-17 --enable-languages=c --disable-bootstrap
--disable-multilib --disable-nls --disable-libsanitizer --with-arch=rv64gc
--with-abi=lp64d --target=riscv64-linux-gnu --program-prefix=riscv64-linux-gnu-
--with-sysroot=/usr/riscv64-linux-gnu --with-native-system-header-dir=/include
--with-as=/usr/bin/riscv64-linux-gnu-as --with-ld=/usr/bin/riscv64-linux-gnu-ld
--disable-shared --disable-threads --disable-libssp --disable-libgomp
--disable-libquadmath --disable-libatomic
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 17.0.0 20261127 (experimental) (GCC)
$ riscv64-linux-gnu-gcc-17 -O2 -Wall -Wextra -static -pthread t.c
$ qemu-riscv64 ./a.out
qemu: uncaught target signal 6 (Aborted) - core dumped
Aborted (core dumped)
$ riscv64-linux-gnu-gcc-17 -O2 -malign-data=natural -static -pthread \
t.c
$ qemu-riscv64 ./a.out
$ cat t.c
#include <pthread.h>
#pragma pack(1)
struct S { signed char c; float f; };
#pragma pack()
struct { struct S s; volatile char n; } t;
__attribute__((noipa)) struct S
give (void)
{
struct S s = { 7, 1.5f };
return s;
}
static void *
writer (void *arg)
{
for (long i = 0; i < 20000000; i++)
t.s = give ();
return arg;
}
int
main (void)
{
pthread_t th;
pthread_create (&th, 0, writer, 0);
for (long i = 0; i < 1000000; i++)
{
t.n = (char) i;
for (int k = 0; k < 100; k++)
if (t.n != (char) i)
__builtin_abort ();
}
pthread_join (th, 0);
return 0;
}
t.s and t.n are different memory locations (C11 3.14): writer only stores t.s
and main only uses t.n, so the source has no data race and t.n must never
change under main.
The program should exit with 0; it aborts at once.
No warnings with -Wall -Wextra; ThreadSanitizer reports nothing (x86_64 16.2,
exit 0).
$ riscv64-linux-gnu-gcc-17 -O2 -S -o - t.c | sed -n '/call\tgive/,/sd\t/p'
call give
sb a0,%lo(t)(s1)
fmv.x.s a4,fa0
ld a5,%lo(t)(s1)
addi s0,s0,-1
slli t1,a4,32
srli t2,t1,24
and a0,a5,s2
or a1,a0,t2
sd a1,%lo(t)(s1)
The ld and sd insert t.s.f into the 8 bytes at t, which include t.n. t is 6
bytes but 8-byte aligned (-malign-data=xlen, the default).
emit_group_store stores the float piece (from fa0, at byte 1) with
store_bit_field and no bit region (0, 0). With the piece's own bits passed as
the region (tried in trunk), the store is five sb and t.c passes.