diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2014-01-24 12:33:35 +0000 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2014-01-24 12:33:35 +0000 |
| commit | 16e7a758b0f3fad11e41b11078051d19de648aba (patch) | |
| tree | 6fda7651c455d1c77ce209fd453fea0fd73b755e /compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc | |
| parent | 439137ea32280fd3208d009f68367c95d9a52099 (diff) | |
| download | bcm5719-llvm-16e7a758b0f3fad11e41b11078051d19de648aba.tar.gz bcm5719-llvm-16e7a758b0f3fad11e41b11078051d19de648aba.zip | |
tsan: do not deadlock on fork
Currently correct programs can deadlock after fork, because atomic operations and async-signal-safe calls are not async-signal-safe under tsan.
With this change:
- if a single-threaded program forks, the child continues running with verification enabled (the tsan background thread is recreated as well)
- if a multi-threaded program forks, then the child runs with verification disabled (memory accesses, atomic operations and interceptors are disabled); it's expected that it will exec soon anyway
- if the child tries to create more threads after multi-threaded fork, the program aborts with error message
- die_after_fork flag is added that allows to continue running, but all bets are off
http://llvm-reviews.chandlerc.com/D2614
llvm-svn: 199993
Diffstat (limited to 'compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc')
| -rw-r--r-- | compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc b/compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc new file mode 100644 index 00000000000..474b53e425a --- /dev/null +++ b/compiler-rt/lib/tsan/lit_tests/fork_multithreaded.cc @@ -0,0 +1,42 @@ +// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s -check-prefix=CHECK-DIE +// RUN: %clangxx_tsan -O1 %s -o %t && TSAN_OPTIONS="die_after_fork=0" %t 2>&1 | FileCheck %s -check-prefix=CHECK-NODIE +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <pthread.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> + +static void *sleeper(void *p) { + sleep(10); + return 0; +} + +int main() { + pthread_t th; + pthread_create(&th, 0, sleeper, 0); + switch (fork()) { + default: // parent + while (wait(0) < 0) {} + break; + case 0: // child + { + pthread_t th2; + pthread_create(&th2, 0, sleeper, 0); + exit(0); + break; + } + case -1: // error + fprintf(stderr, "failed to fork (%d)\n", errno); + exit(1); + } + fprintf(stderr, "OK\n"); +} + +// CHECK-DIE: ThreadSanitizer: starting new threads after muti-threaded fork is not supported +// CHECK-DIE: OK + +// CHECK-NODIE-NOT: ThreadSanitizer: starting new threads after muti-threaded fork is not supported +// CHECK-NODIE: OK + |

