xref: /aosp_15_r20/external/compiler-rt/test/tsan/fork_multithreaded.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-DIE
2*7c3d14c8STreehugger Robot // RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=die_after_fork=0 %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-NODIE
3*7c3d14c8STreehugger Robot #include "test.h"
4*7c3d14c8STreehugger Robot #include <errno.h>
5*7c3d14c8STreehugger Robot #include <sys/types.h>
6*7c3d14c8STreehugger Robot #include <sys/wait.h>
7*7c3d14c8STreehugger Robot 
sleeper(void * p)8*7c3d14c8STreehugger Robot static void *sleeper(void *p) {
9*7c3d14c8STreehugger Robot   sleep(1000);  // not intended to exit during test
10*7c3d14c8STreehugger Robot   return 0;
11*7c3d14c8STreehugger Robot }
12*7c3d14c8STreehugger Robot 
nop(void * p)13*7c3d14c8STreehugger Robot static void *nop(void *p) {
14*7c3d14c8STreehugger Robot   return 0;
15*7c3d14c8STreehugger Robot }
16*7c3d14c8STreehugger Robot 
main()17*7c3d14c8STreehugger Robot int main() {
18*7c3d14c8STreehugger Robot   barrier_init(&barrier, 2);
19*7c3d14c8STreehugger Robot   pthread_t th;
20*7c3d14c8STreehugger Robot   pthread_create(&th, 0, sleeper, 0);
21*7c3d14c8STreehugger Robot   switch (fork()) {
22*7c3d14c8STreehugger Robot   default:  // parent
23*7c3d14c8STreehugger Robot     while (wait(0) < 0) {}
24*7c3d14c8STreehugger Robot     break;
25*7c3d14c8STreehugger Robot   case 0:  // child
26*7c3d14c8STreehugger Robot     {
27*7c3d14c8STreehugger Robot       pthread_t th2;
28*7c3d14c8STreehugger Robot       pthread_create(&th2, 0, nop, 0);
29*7c3d14c8STreehugger Robot       exit(0);
30*7c3d14c8STreehugger Robot       break;
31*7c3d14c8STreehugger Robot     }
32*7c3d14c8STreehugger Robot   case -1:  // error
33*7c3d14c8STreehugger Robot     fprintf(stderr, "failed to fork (%d)\n", errno);
34*7c3d14c8STreehugger Robot     exit(1);
35*7c3d14c8STreehugger Robot   }
36*7c3d14c8STreehugger Robot   fprintf(stderr, "OK\n");
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot // CHECK-DIE: ThreadSanitizer: starting new threads after multi-threaded fork is not supported
40*7c3d14c8STreehugger Robot // CHECK-DIE: OK
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot // CHECK-NODIE-NOT: ThreadSanitizer: starting new threads after multi-threaded fork is not supported
43*7c3d14c8STreehugger Robot // CHECK-NODIE: OK
44*7c3d14c8STreehugger Robot 
45