diff options
Diffstat (limited to 'test/tsan/fd_dup_race.cc')
-rw-r--r-- | test/tsan/fd_dup_race.cc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/tsan/fd_dup_race.cc b/test/tsan/fd_dup_race.cc new file mode 100644 index 000000000000..a1aee5500753 --- /dev/null +++ b/test/tsan/fd_dup_race.cc @@ -0,0 +1,33 @@ +// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s +#include "test.h" +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +// dup2(oldfd, newfd) races with close(newfd). + +int fd; + +void *Thread(void *x) { + barrier_wait(&barrier); + if (close(fd) == -1) + exit(printf("close failed\n")); + return 0; +} + +int main() { + barrier_init(&barrier, 2); + fd = open("/dev/random", O_RDONLY); + int fd2 = open("/dev/random", O_RDONLY); + if (fd == -1 || fd2 == -1) + exit(printf("open failed\n")); + pthread_t th; + pthread_create(&th, 0, Thread, 0); + if (dup2(fd2, fd) == -1) + exit(printf("dup2 failed\n")); + barrier_wait(&barrier); + pthread_join(th, 0); + printf("DONE\n"); +} + +// CHECK: WARNING: ThreadSanitizer: data race |