xref: /aosp_15_r20/external/compiler-rt/test/msan/fork.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test that chained origins are fork-safe.
2*7c3d14c8STreehugger Robot // Run a number of threads that create new chained origins, then fork
3*7c3d14c8STreehugger Robot // and verify that origin reads do not deadlock in the child process.
4*7c3d14c8STreehugger Robot 
5*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins=2 -g -O3 %s -o %t
6*7c3d14c8STreehugger Robot // RUN: MSAN_OPTIONS=store_context_size=1000,origin_history_size=0,origin_history_per_stack_limit=0 %run %t |& FileCheck %s
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot // Fun fact: if test output is redirected to a file (as opposed to
9*7c3d14c8STreehugger Robot // being piped directly to FileCheck), we may lose some "done"s due to
10*7c3d14c8STreehugger Robot // a kernel bug:
11*7c3d14c8STreehugger Robot // https://lkml.org/lkml/2014/2/17/324
12*7c3d14c8STreehugger Robot 
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #include <pthread.h>
15*7c3d14c8STreehugger Robot #include <unistd.h>
16*7c3d14c8STreehugger Robot #include <stdio.h>
17*7c3d14c8STreehugger Robot #include <stdlib.h>
18*7c3d14c8STreehugger Robot #include <sys/types.h>
19*7c3d14c8STreehugger Robot #include <sys/wait.h>
20*7c3d14c8STreehugger Robot #include <sys/time.h>
21*7c3d14c8STreehugger Robot #include <signal.h>
22*7c3d14c8STreehugger Robot #include <errno.h>
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot #include <sanitizer/msan_interface.h>
25*7c3d14c8STreehugger Robot 
26*7c3d14c8STreehugger Robot int done;
27*7c3d14c8STreehugger Robot 
copy_uninit_thread2()28*7c3d14c8STreehugger Robot void copy_uninit_thread2() {
29*7c3d14c8STreehugger Robot   volatile int x;
30*7c3d14c8STreehugger Robot   volatile int v;
31*7c3d14c8STreehugger Robot   while (true) {
32*7c3d14c8STreehugger Robot     v = x;
33*7c3d14c8STreehugger Robot     x = v;
34*7c3d14c8STreehugger Robot     if (__atomic_load_n(&done, __ATOMIC_RELAXED))
35*7c3d14c8STreehugger Robot       return;
36*7c3d14c8STreehugger Robot   }
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot 
copy_uninit_thread1(int level)39*7c3d14c8STreehugger Robot void copy_uninit_thread1(int level) {
40*7c3d14c8STreehugger Robot   if (!level)
41*7c3d14c8STreehugger Robot     copy_uninit_thread2();
42*7c3d14c8STreehugger Robot   else
43*7c3d14c8STreehugger Robot     copy_uninit_thread1(level - 1);
44*7c3d14c8STreehugger Robot }
45*7c3d14c8STreehugger Robot 
copy_uninit_thread(void * id)46*7c3d14c8STreehugger Robot void *copy_uninit_thread(void *id) {
47*7c3d14c8STreehugger Robot   copy_uninit_thread1((long)id);
48*7c3d14c8STreehugger Robot   return 0;
49*7c3d14c8STreehugger Robot }
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot // Run through stackdepot in the child process.
52*7c3d14c8STreehugger Robot // If any of the hash table cells are locked, this may deadlock.
child()53*7c3d14c8STreehugger Robot void child() {
54*7c3d14c8STreehugger Robot   volatile int x;
55*7c3d14c8STreehugger Robot   volatile int v;
56*7c3d14c8STreehugger Robot   for (int i = 0; i < 10000; ++i) {
57*7c3d14c8STreehugger Robot     v = x;
58*7c3d14c8STreehugger Robot     x = v;
59*7c3d14c8STreehugger Robot   }
60*7c3d14c8STreehugger Robot   write(2, "done\n", 5);
61*7c3d14c8STreehugger Robot }
62*7c3d14c8STreehugger Robot 
test()63*7c3d14c8STreehugger Robot void test() {
64*7c3d14c8STreehugger Robot   const int kThreads = 10;
65*7c3d14c8STreehugger Robot   pthread_t t[kThreads];
66*7c3d14c8STreehugger Robot   for (int i = 0; i < kThreads; ++i)
67*7c3d14c8STreehugger Robot     pthread_create(&t[i], NULL, copy_uninit_thread, (void*)(long)i);
68*7c3d14c8STreehugger Robot   usleep(100000);
69*7c3d14c8STreehugger Robot   pid_t pid = fork();
70*7c3d14c8STreehugger Robot   if (pid) {
71*7c3d14c8STreehugger Robot     // parent
72*7c3d14c8STreehugger Robot     __atomic_store_n(&done, 1, __ATOMIC_RELAXED);
73*7c3d14c8STreehugger Robot     pid_t p;
74*7c3d14c8STreehugger Robot     while ((p = wait(NULL)) == -1) {  }
75*7c3d14c8STreehugger Robot   } else {
76*7c3d14c8STreehugger Robot     // child
77*7c3d14c8STreehugger Robot     child();
78*7c3d14c8STreehugger Robot   }
79*7c3d14c8STreehugger Robot }
80*7c3d14c8STreehugger Robot 
main()81*7c3d14c8STreehugger Robot int main() {
82*7c3d14c8STreehugger Robot   const int kChildren = 20;
83*7c3d14c8STreehugger Robot   for (int i = 0; i < kChildren; ++i) {
84*7c3d14c8STreehugger Robot     pid_t pid = fork();
85*7c3d14c8STreehugger Robot     if (pid) {
86*7c3d14c8STreehugger Robot       // parent
87*7c3d14c8STreehugger Robot     } else {
88*7c3d14c8STreehugger Robot       test();
89*7c3d14c8STreehugger Robot       exit(0);
90*7c3d14c8STreehugger Robot     }
91*7c3d14c8STreehugger Robot   }
92*7c3d14c8STreehugger Robot 
93*7c3d14c8STreehugger Robot   for (int i = 0; i < kChildren; ++i) {
94*7c3d14c8STreehugger Robot     pid_t p;
95*7c3d14c8STreehugger Robot     while ((p = wait(NULL)) == -1) {  }
96*7c3d14c8STreehugger Robot   }
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot   return 0;
99*7c3d14c8STreehugger Robot }
100*7c3d14c8STreehugger Robot 
101*7c3d14c8STreehugger Robot // Expect 20 (== kChildren) "done" messages.
102*7c3d14c8STreehugger Robot // CHECK: done
103*7c3d14c8STreehugger Robot // CHECK: done
104*7c3d14c8STreehugger Robot // CHECK: done
105*7c3d14c8STreehugger Robot // CHECK: done
106*7c3d14c8STreehugger Robot // CHECK: done
107*7c3d14c8STreehugger Robot // CHECK: done
108*7c3d14c8STreehugger Robot // CHECK: done
109*7c3d14c8STreehugger Robot // CHECK: done
110*7c3d14c8STreehugger Robot // CHECK: done
111*7c3d14c8STreehugger Robot // CHECK: done
112*7c3d14c8STreehugger Robot // CHECK: done
113*7c3d14c8STreehugger Robot // CHECK: done
114*7c3d14c8STreehugger Robot // CHECK: done
115*7c3d14c8STreehugger Robot // CHECK: done
116*7c3d14c8STreehugger Robot // CHECK: done
117*7c3d14c8STreehugger Robot // CHECK: done
118*7c3d14c8STreehugger Robot // CHECK: done
119*7c3d14c8STreehugger Robot // CHECK: done
120*7c3d14c8STreehugger Robot // CHECK: done
121*7c3d14c8STreehugger Robot // CHECK: done
122