theRockLiu / thread-sanitizer

Automatically exported from code.google.com/p/thread-sanitizer
0 stars 0 forks source link

False positive after fork #60

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The attached program do the following:
* set a global variable
* create a few threads that read that variable
* fork and:
** do nothing in the parent process
** change the value of the global variable in the child process

I expect this to produce no race report since the variable is written only when 
there's no thread to conflict with. However, when compile with thread-sanitizer 
from clang 3.4, this produce a warning:

% clang -g -fsanitize=thread blah.c
% ./a.out
I'm thread 1 of 2
I'm thread 2 of 2
==================
WARNING: ThreadSanitizer: data race (pid=2480)
  Write of size 4 at 0x7faf547c34bc by main thread:
    #0 main /home/fruneau/dev/platform/lib-common/blah.c:30 (a.out+0x0000000a52e1)

  Previous read of size 4 at 0x7faf547c34bc by thread T1:
    #0 run /home/fruneau/dev/platform/lib-common/blah.c:9 (a.out+0x0000000a534f)

  As if synchronized via sleep:
    #0 sleep /home/fruneau/dev/tools/llvm_3.4/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:239 (a.out+0x00000004453d)
    #1 main /home/fruneau/dev/platform/lib-common/blah.c:27 (a.out+0x0000000a52bf)

  Location is global 'thread_count' of size 4 at 0x7faf547c34bc (a.out+0x0000016ea4bc)

  Thread T1 (tid=2478, running) created by main thread at:
    #0 pthread_create /home/fruneau/dev/tools/llvm_3.4/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:877 (a.out+0x00000004a80b)
    #1 main /home/fruneau/dev/platform/lib-common/blah.c:25 (a.out+0x0000000a528f)

SUMMARY: ThreadSanitizer: data race 
/home/fruneau/dev/platform/lib-common/blah.c:30 main
==================

The write done in the child process is said to conflict with the read performed 
in the parent process which cannot be true since that thread does not exist in 
the child process.

Original issue reported on code.google.com by florent....@intersec.com on 27 Apr 2014 at 4:46

Attachments:

GoogleCodeExporter commented 9 years ago
We've changed handling of fork in clang 3.5, I've tested this program and it 
does not produce a report.
However note that if parent process writes to thread_count (under a mutex), 
then this would be a real harmful race. Because parent process can leave 
garbage in thread_count at the moment of fork.

Original comment by dvyu...@google.com on 2 May 2014 at 7:53

GoogleCodeExporter commented 9 years ago

Original comment by dvyu...@google.com on 2 May 2014 at 7:54