[patch, libfortran] Fix thead sanitizer issue with libgfortran

Bernd Edlinger bernd.edlinger@hotmail.de
Sun Oct 1 09:02:00 GMT 2017


Hi,

I think this might be a false positive tsan warning.
Maybe tsan does not see why a desctructor function
can not execute before the last detached thread
terminates.

I created a small test case that receives a warning
which is similar to the one you tried to fix with your
patch:

cat test.c
#include <stdio.h>
#include <pthread.h>

int test;

static void *
t1(void* p)
{
   printf("t1\n");
   test = 1;
   return NULL;
}

static void __attribute__((destructor))
t2 (void)
{
   test = 2;
   printf("t2\n");
}

int
main()
{
   pthread_t t;
   pthread_attr_t a;
   pthread_attr_init(&a);
   pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
   pthread_create(&t, &a, t1, NULL);
   pthread_attr_destroy(&a);
   return 0;
}

gcc -Wall -fsanitize=thread test.c
./a.out
t1
t2
==================
WARNING: ThreadSanitizer: data race (pid=3564)
   Write of size 4 at 0x00000060107c by thread T1:
     #0 t1 <null> (a.out+0x00000040088e)

   Previous write of size 4 at 0x00000060107c by main thread:
     #0 t2 <null> (a.out+0x0000004008c6)
     #1 <null> <null> (ld-linux-x86-64.so.2+0x0000000108d9)

   Location is global 'test' of size 4 at 0x00000060107c 
(a.out+0x00000060107c)

   Thread T1 (tid=3566, running) created by main thread at:
     #0 pthread_create 
../../../../gcc-trunk/libsanitizer/tsan/tsan_interceptors.cc:900 
(libtsan.so.0+0x00000002914e)
     #1 main <null> (a.out+0x00000040092e)

SUMMARY: ThreadSanitizer: data race 
(/home/ed/gnu/gcc-test/a.out+0x40088e) in t1
==================
ThreadSanitizer: reported 1 warnings


The warning goes away when the main function explicitly
joins the thread t1.

That is what I do with all my threads whenever possible,
maybe there is a way how you could explicitly join
all running threads?


Bernd.



More information about the Gcc-patches mailing list