1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: ios
3
4#include <pthread.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <sys/types.h>
9
10int fds[2];
11int X;
12
13void *Thread1(void *x) {
14 X = 42;
15 write(fd: fds[1], buf: "a", n: 1);
16 return NULL;
17}
18
19void *Thread2(void *x) {
20 char buf;
21 while (read(fd: fds[0], buf: &buf, nbytes: 1) != 1) {
22 }
23 X = 43;
24 return NULL;
25}
26
27int main() {
28 pipe(pipedes: fds);
29 int pid = vfork();
30 if (pid < 0) {
31 fprintf(stderr, format: "FAIL to vfork\n");
32 exit(status: 1);
33 }
34 if (pid == 0) { // child
35 // Closing of fds must not affect parent process.
36 // Strictly saying this is undefined behavior, because vfork child is not
37 // allowed to call any functions other than exec/exit. But this is what
38 // openjdk does.
39 close(fd: fds[0]);
40 close(fd: fds[1]);
41 _exit(status: 0);
42 }
43 pthread_t t[2];
44 pthread_create(newthread: &t[0], NULL, start_routine: Thread1, NULL);
45 pthread_create(newthread: &t[1], NULL, start_routine: Thread2, NULL);
46 pthread_join(th: t[0], NULL);
47 pthread_join(th: t[1], NULL);
48 fprintf(stderr, format: "DONE\n");
49}
50
51// CHECK-NOT: WARNING: ThreadSanitizer: data race
52// CHECK-NOT: FAIL to vfork
53// CHECK: DONE
54

source code of compiler-rt/test/tsan/vfork.cpp