1 // RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=atexit_sleep_ms=0 %run %t 2>&1 | FileCheck %s
3 // This test models what happens on Mac when fork
4 // calls malloc/free inside of our atfork callbacks.
5 // and ensures that we don't deadlock on malloc/free calls.
11 #include <sys/types.h>
14 // disable_sanitizer_instrumentation on __tsan_test_only_on_fork is not
15 // transitive, so we must apply it here as well.
16 // Instrumenting alloc_free_blocks() will result in deadlocks in TSan.
17 __attribute__((disable_sanitizer_instrumentation
)) void alloc_free_blocks() {
18 // Allocate a bunch of blocks to drain local allocator cache
19 // and provoke it to lock allocator global mutexes.
20 const int kBlocks
= 1000;
21 void *blocks
[kBlocks
];
22 for (int i
= 0; i
< kBlocks
; i
++) {
24 *(volatile char *)p
= 0;
27 for (int i
= 0; i
< kBlocks
; i
++)
31 __attribute__((disable_sanitizer_instrumentation
)) extern "C" void
32 __tsan_test_only_on_fork() {
33 const char *msg
= "__tsan_test_only_on_fork\n";
34 write(2, msg
, strlen(msg
));
38 static void *background(void *p
) {
46 pthread_create(&th
, 0, background
, 0);
48 for (int i
= 0; i
< 10; i
++) {
51 fprintf(stderr
, "failed to fork (%d)\n", errno
);
62 fprintf(stderr
, "DONE\n");
65 // CHECK: __tsan_test_only_on_fork