| 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 | |
| 9 | int fd; |
| 10 | |
| 11 | void *Thread(void *x) { |
| 12 | barrier_wait(barrier: &barrier); |
| 13 | if (close(fd: fd) == -1) |
| 14 | exit(status: printf(format: "close failed\n" )); |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | int main() { |
| 19 | barrier_init(barrier: &barrier, count: 2); |
| 20 | fd = open(file: "/dev/random" , O_RDONLY); |
| 21 | int fd2 = open(file: "/dev/random" , O_RDONLY); |
| 22 | if (fd == -1 || fd2 == -1) |
| 23 | exit(status: printf(format: "open failed\n" )); |
| 24 | pthread_t th; |
| 25 | pthread_create(newthread: &th, attr: 0, start_routine: Thread, arg: 0); |
| 26 | if (dup2(fd: fd2, fd2: fd) == -1) |
| 27 | exit(status: printf(format: "dup2 failed\n" )); |
| 28 | barrier_wait(barrier: &barrier); |
| 29 | pthread_join(th: th, thread_return: 0); |
| 30 | fprintf(stderr, format: "DONE\n" ); |
| 31 | } |
| 32 | |
| 33 | // CHECK: WARNING: ThreadSanitizer: data race |
| 34 | |