1 // Test that chained origins are fork-safe.
2 // Run a number of threads that create new chained origins, then fork
3 // and verify that origin reads do not deadlock in the child process.
5 // RUN: %clangxx_msan -std=c++11 -fsanitize-memory-track-origins=2 -g -O3 %s -o %t
6 // RUN: MSAN_OPTIONS=store_context_size=1000,origin_history_size=0,origin_history_per_stack_limit=0 %run %t 2>&1 | FileCheck %s
8 // Fun fact: if test output is redirected to a file (as opposed to
9 // being piped directly to FileCheck), we may lose some "done"s due to
11 // https://lkml.org/lkml/2014/2/17/324
14 // UNSUPPORTED: powerpc64-target-arch
15 // UNSUPPORTED: powerpc64le-target-arch
18 // UNSUPPORTED: target={{.*netbsd.*}}
24 #include <sys/types.h>
30 #include <sanitizer/msan_interface.h>
34 void copy_uninit_thread2() {
40 if (__atomic_load_n(&done
, __ATOMIC_RELAXED
))
45 void copy_uninit_thread1(int level
) {
47 copy_uninit_thread2();
49 copy_uninit_thread1(level
- 1);
52 void *copy_uninit_thread(void *id
) {
53 copy_uninit_thread1((long)id
);
57 // Run through stackdepot in the child process.
58 // If any of the hash table cells are locked, this may deadlock.
62 for (int i
= 0; i
< 10000; ++i
) {
66 write(2, "done\n", 5);
70 const int kThreads
= 10;
71 pthread_t t
[kThreads
];
72 for (int i
= 0; i
< kThreads
; ++i
)
73 pthread_create(&t
[i
], NULL
, copy_uninit_thread
, (void*)(long)i
);
78 __atomic_store_n(&done
, 1, __ATOMIC_RELAXED
);
80 while ((p
= wait(NULL
)) == -1) { }
88 const int kChildren
= 20;
89 for (int i
= 0; i
< kChildren
; ++i
) {
99 for (int i
= 0; i
< kChildren
; ++i
) {
101 while ((p
= wait(NULL
)) == -1) { }
107 // Expect 20 (== kChildren) "done" messages.