1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// The test tries to provoke internal allocator to be locked during fork
4// and then force the child process to use the internal allocator.
5
6#include "../test.h"
7#include <errno.h>
8#include <sys/types.h>
9#include <sys/wait.h>
10
11static void *forker(void *arg) {
12 void *p = calloc(nmemb: 1, size: 16);
13 static_cast<volatile int *>(p)[0]++;
14 __atomic_fetch_add(static_cast<int *>(p), 1, __ATOMIC_SEQ_CST);
15 int pid = fork();
16 if (pid < 0) {
17 fprintf(stderr, format: "failed to fork (%d)\n", errno);
18 exit(status: 1);
19 }
20 if (pid == 0) {
21 __atomic_fetch_add(&static_cast<int *>(p)[1], 1, __ATOMIC_SEQ_CST);
22 exit(status: 0);
23 }
24 int status = 0;
25 while (waitpid(pid: pid, stat_loc: &status, options: 0) != pid) {
26 }
27 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
28 fprintf(stderr, format: "subprocess failed (%d)\n", status);
29 exit(status: 1);
30 }
31 free(ptr: p);
32 return 0;
33}
34
35int main() {
36 for (int i = 0; i < 10; i++) {
37 pthread_t threads[100];
38 for (auto &th : threads)
39 pthread_create(newthread: &th, attr: 0, start_routine: forker, arg: 0);
40 for (auto th : threads)
41 pthread_join(th: th, thread_return: 0);
42 }
43 fprintf(stderr, format: "DONE\n");
44}
45
46// CHECK: DONE
47

source code of compiler-rt/test/tsan/Linux/fork_multithreaded4.cpp