[Bug libfortran/118935] Segmentation fault in 'libgomp.fortran/rwlock_1.f90' when compiling libgfortran with '-O0'

huangpei at loongson dot cn via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118935

--- Comment #15 from huangpei at loongson dot cn <huangpei at loongson dot cn> ---
Same issue on GCC 15.1.0, Tsan says:

==================
WARNING: ThreadSanitizer: data race (pid=3090994)
  Read of size 8 at 0x7fffee107808 by thread T3 (mutexes: read M0):
    #0 find_file0 ../../../gcc-master/libgfortran/io/unix.c:1721
(libgfortran.so.5+0x36bbfc)
    #1 find_file0 ../../../gcc-master/libgfortran/io/unix.c:1744
(libgfortran.so.5+0x36bcec)
    #2 find_file0 ../../../gcc-master/libgfortran/io/unix.c:1744
(libgfortran.so.5+0x36bcec)
    #3 find_file0 ../../../gcc-master/libgfortran/io/unix.c:1744
(libgfortran.so.5+0x36bcec)
    #4 find_file0 ../../../gcc-master/libgfortran/io/unix.c:1744
(libgfortran.so.5+0x36bcec)
    #5 _gfortrani_find_file ../../../gcc-master/libgfortran/io/unix.c:1778
(libgfortran.so.5+0x36bdd8)
    #6 _gfortran_st_inquire ../../../gcc-master/libgfortran/io/inquire.c:821
(libgfortran.so.5+0x33bc34)
    #7 MAIN__._omp_fn.0 <null> (rwlock_1.f90.exe+0x1200014d0)
    #8 gomp_thread_start ../../../gcc-master/libgomp/team.c:129
(libgomp.so.1+0x25f04)

  Previous write of size 8 at 0x7fffee107808 by thread T2 (mutexes: write M1):
    #0 _gfortrani_new_unit ../../../gcc-master/libgfortran/io/open.c:588
(libgfortran.so.5+0x34d824)
    #1 _gfortran_st_open ../../../gcc-master/libgfortran/io/open.c:931
(libgfortran.so.5+0x34ed5c)
    #2 MAIN__._omp_fn.0 <null> (rwlock_1.f90.exe+0x1200013f4)
    #3 gomp_thread_start ../../../gcc-master/libgomp/team.c:129
(libgomp.so.1+0x25f04)

  Location is heap block of size 752 at 0x7fffee107800 allocated by thread T2:
    #0 calloc
../../../../gcc-master/libsanitizer/tsan/tsan_interceptors_posix.cpp:698
(libtsan.so.2+0x966ac)
    #1 _gfortrani_xcalloc ../../../gcc-master/libgfortran/runtime/memory.c:78
(libgfortran.so.5+0x29c58)
    #2 insert_unit ../../../gcc-master/libgfortran/io/unit.c:239
(libgfortran.so.5+0x365434)
    #3 get_gfc_unit ../../../gcc-master/libgfortran/io/unit.c:391
(libgfortran.so.5+0x365b40)
    #4 _gfortrani_find_or_create_unit
../../../gcc-master/libgfortran/io/unit.c:456 (libgfortran.so.5+0x365db0)
    #5 _gfortran_st_open ../../../gcc-master/libgfortran/io/open.c:928
(libgfortran.so.5+0x34ed28)
    #6 MAIN__._omp_fn.0 <null> (rwlock_1.f90.exe+0x1200013f4)
    #7 gomp_thread_start ../../../gcc-master/libgomp/team.c:129
(libgomp.so.1+0x25f04)

  Mutex M0 (0x7ffff29d18a0) created at:
    #0 pthread_rwlock_wrlock
../../../../gcc-master/libsanitizer/tsan/tsan_interceptors_posix.cpp:1552
(libtsan.so.2+0x5f630)
    #1 __gthread_rwlock_wrlock ../libgcc/gthr-default.h:957
(libgfortran.so.5+0x364ccc)
    #2 _gfortrani_newunit_alloc ../../../gcc-master/libgfortran/io/unit.c:944
(libgfortran.so.5+0x367928)
    #3 _gfortrani_get_unit ../../../gcc-master/libgfortran/io/unit.c:590
(libgfortran.so.5+0x366810)
    #4 data_transfer_init ../../../gcc-master/libgfortran/io/transfer.c:3144
(libgfortran.so.5+0x35de08)
    #5 _gfortran_st_write ../../../gcc-master/libgfortran/io/transfer.c:4714
(libgfortran.so.5+0x363a10)
    #6 MAIN__._omp_fn.0 <null> (rwlock_1.f90.exe+0x12000127c)
    #7 GOMP_parallel ../../../gcc-master/libgomp/parallel.c:178
(libgomp.so.1+0x1ab20)
    #8 main <null> (rwlock_1.f90.exe+0x12000110c)

  Mutex M1 (0x7fffee1078e0) created at:
    #0 pthread_mutex_lock
../../../../gcc-master/libsanitizer/tsan/tsan_interceptors_posix.cpp:1371
(libtsan.so.2+0x60998)
    #1 __gthread_mutex_lock ../libgcc/gthr-default.h:795
(libgfortran.so.5+0x364b2c)
    #2 insert_unit ../../../gcc-master/libgfortran/io/unit.c:250
(libgfortran.so.5+0x3654dc)
    #3 get_gfc_unit ../../../gcc-master/libgfortran/io/unit.c:391
(libgfortran.so.5+0x365b40)
    #4 _gfortrani_find_or_create_unit
../../../gcc-master/libgfortran/io/unit.c:456 (libgfortran.so.5+0x365db0)
    #5 _gfortran_st_open ../../../gcc-master/libgfortran/io/open.c:928
(libgfortran.so.5+0x34ed28)
    #6 MAIN__._omp_fn.0 <null> (rwlock_1.f90.exe+0x1200013f4)
    #7 gomp_thread_start ../../../gcc-master/libgomp/team.c:129
(libgomp.so.1+0x25f04)

  Thread T3 (tid=3090999, running) created by main thread at:
    #0 pthread_create
../../../../gcc-master/libsanitizer/tsan/tsan_interceptors_posix.cpp:1041
(libtsan.so.2+0x613a8)
    #1 gomp_team_start ../../../gcc-master/libgomp/team.c:859
(libgomp.so.1+0x26834)
    #2 main <null> (rwlock_1.f90.exe+0x12000110c)

  Thread T2 (tid=3090998, running) created by main thread at:
    #0 pthread_create
../../../../gcc-master/libsanitizer/tsan/tsan_interceptors_posix.cpp:1041
(libtsan.so.2+0x613a8)
    #1 gomp_team_start ../../../gcc-master/libgomp/team.c:859
(libgomp.so.1+0x26834)
    #2 main <null> (rwlock_1.f90.exe+0x12000110c)

SUMMARY: ThreadSanitizer: data race
../../../gcc-master/libgfortran/io/unix.c:1721 in find_file0
==================================================

I guess this is what happened:

================================================================================
thread T3 (find_file0() <= find_file())    thread T3 (new_unit()  <=
get_unit())
───────────────────────────────         ──────────────────────────────────────
                                        hold/release unit_rwlock : u =
find_or_create_unit() <= st_open()

rdlock(unit_rwlock) M0

#3**(SARAROoO load u->s && s->st_dev → SEGV)

                                        #1 if (u->s == NULL)
                                                u = new_unit(); u->s = s  M1



#2 u->s != NULL 
#3reload u->s && s->st_dev
===============================================================================


Data race is clear, since neither M0 nor M1 cover this case; However, Loong64
is
week ordered( even same address read after read is out of order), #3 is
reordered by cpu and see u->s is NULL before #1;


Use aotmic operartion to fix this problem and the patch is on atttachment
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.