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