summaryrefslogtreecommitdiffstats
path: root/compiler-rt/test/tsan/fd_dup_norace2.cpp
diff options
context:
space:
mode:
authorFangrui Song <maskray@google.com>2019-08-02 07:18:07 +0000
committerFangrui Song <maskray@google.com>2019-08-02 07:18:07 +0000
commitbcaeed49cb063de9fe504aa29e1cadff8a7be710 (patch)
tree01b6111d0c244bb2c86f705a06f575442cf58aeb /compiler-rt/test/tsan/fd_dup_norace2.cpp
parent8113c93fb1b7d1cc595882bca17381c1bd3b2069 (diff)
downloadbcm5719-llvm-bcaeed49cb063de9fe504aa29e1cadff8a7be710.tar.gz
bcm5719-llvm-bcaeed49cb063de9fe504aa29e1cadff8a7be710.zip
compiler-rt: Rename .cc file in test/tsan to .cpp
Like r367463, but for test/tsan. llvm-svn: 367656
Diffstat (limited to 'compiler-rt/test/tsan/fd_dup_norace2.cpp')
-rw-r--r--compiler-rt/test/tsan/fd_dup_norace2.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/compiler-rt/test/tsan/fd_dup_norace2.cpp b/compiler-rt/test/tsan/fd_dup_norace2.cpp
new file mode 100644
index 00000000000..31aaed9d356
--- /dev/null
+++ b/compiler-rt/test/tsan/fd_dup_norace2.cpp
@@ -0,0 +1,60 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+#include "test.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+// dup2(oldfd, newfd) races with read(newfd).
+// This is not reported as race because:
+// 1. Some software dups a closed pipe in place of a socket before closing
+// the socket (to prevent races actually).
+// 2. Some daemons dup /dev/null in place of stdin/stdout.
+
+int fd;
+
+void *Thread(void *x) {
+ char buf;
+ int n = read(fd, &buf, 1);
+ if (n != 1) {
+ // This read can "legitimately" fail regadless of the fact that glibc claims
+ // that "there is no instant in the middle of calling dup2 at which new is
+ // closed and not yet a duplicate of old". Strace of the failing runs
+ // looks as follows:
+ //
+ // [pid 122196] open("/dev/urandom", O_RDONLY) = 3
+ // [pid 122196] open("/dev/urandom", O_RDONLY) = 4
+ // Process 122382 attached
+ // [pid 122382] read(3, <unfinished ...>
+ // [pid 122196] dup2(4, 3 <unfinished ...>
+ // [pid 122382] <... read resumed> 0x7fcd139960b7, 1) = -1 EBADF (Bad file descriptor)
+ // [pid 122196] <... dup2 resumed> ) = 3
+ // read failed: n=-1 errno=9
+ //
+ // The failing read does not interfere with what this test tests,
+ // so we just ignore the failure.
+ //
+ // exit(printf("read failed: n=%d errno=%d\n", n, errno));
+ }
+ return 0;
+}
+
+int main() {
+ fd = open("/dev/urandom", O_RDONLY);
+ int fd2 = open("/dev/urandom", 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"));
+ pthread_join(th, 0);
+ if (close(fd) == -1)
+ exit(printf("close failed\n"));
+ if (close(fd2) == -1)
+ exit(printf("close failed\n"));
+ fprintf(stderr, "DONE\n");
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer: data race
+// CHECK: DONE
OpenPOWER on IntegriCloud