1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2#include "test.h"
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6
7// dup2(oldfd, newfd) races with close(newfd).
8
9int fd;
10
11void *Thread(void *x) {
12 barrier_wait(&barrier);
13 if (close(fd) == -1)
14 exit(printf("close failed\n"));
15 return 0;
16}
17
18int main() {
19 barrier_init(&barrier, 2);
20 fd = open("/dev/random", O_RDONLY);
21 int fd2 = open("/dev/random", O_RDONLY);
22 if (fd == -1 || fd2 == -1)
23 exit(printf("open failed\n"));
24 pthread_t th;
25 pthread_create(&th, 0, Thread, 0);
26 if (dup2(fd2, fd) == -1)
27 exit(printf("dup2 failed\n"));
28 barrier_wait(&barrier);
29 pthread_join(th, 0);
30 fprintf(stderr, "DONE\n");
31}
32
33// CHECK: WARNING: ThreadSanitizer: data race
34