[compiler-rt] [compiler-rt] Replace direct calls to pipe with internal_pipe (PR #97186)
Sirui Mu via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 28 06:39:37 PDT 2024
https://github.com/Lancern updated https://github.com/llvm/llvm-project/pull/97186
>From 23e43898ed613428120d44066ab665d2fc0ef9d3 Mon Sep 17 00:00:00 2001
From: Sirui Mu <msrlancern at gmail.com>
Date: Mon, 28 Oct 2024 21:38:20 +0800
Subject: [PATCH] [compiler-rt] Replace direct calls to pipe with internal_pipe
This patch tries to resolve google/sanitizers#1106 by introducing a new
internal_pipe function which will not be intercepted by TSAN.
---
.../lib/sanitizer_common/sanitizer_linux.cpp | 4 +++
.../lib/sanitizer_common/sanitizer_mac.cpp | 2 ++
.../lib/sanitizer_common/sanitizer_netbsd.cpp | 5 ++++
.../lib/sanitizer_common/sanitizer_posix.h | 4 ++-
.../sanitizer_posix_libcdep.cpp | 4 +--
compiler-rt/test/tsan/pipe.cpp | 30 +++++++++++++++++++
6 files changed, 46 insertions(+), 3 deletions(-)
create mode 100644 compiler-rt/test/tsan/pipe.cpp
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 33107eb0b42993..7b8ebb710bea3d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -847,6 +847,10 @@ uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
return internal_syscall(SYSCALL(lseek), fd, offset, whence);
}
+uptr internal_pipe(fd_t pipefd[2]) {
+ return internal_syscall(SYSCALL(pipe), pipefd);
+}
+
# if SANITIZER_LINUX
uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 26d2e8d4ed7680..755dd91067d155 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -213,6 +213,8 @@ uptr internal_unlink(const char *path) {
return unlink(path);
}
+uptr internal_pipe(fd_t fildes[2]) { return pipe(fildes); }
+
uptr internal_sched_yield() {
return sched_yield();
}
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
index 5e601bdcde1e56..eff8ceb3b60b97 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
@@ -204,6 +204,11 @@ uptr internal_rename(const char *oldpath, const char *newpath) {
return _REAL(rename, oldpath, newpath);
}
+uptr internal_pipe(fd_t fildes[2]) {
+ DEFINE__REAL(int, pipe, int a[2]);
+ return _REAL(pipe, fildes);
+}
+
uptr internal_sched_yield() {
CHECK(&_sys_sched_yield);
return _sys_sched_yield();
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
index 1f0795caa420c7..cf6110d91d9b82 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
@@ -56,7 +56,9 @@ uptr internal_unlink(const char *path);
uptr internal_rename(const char *oldpath, const char *newpath);
uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
-#if SANITIZER_NETBSD
+uptr internal_pipe(fd_t pipefd[2]);
+
+# if SANITIZER_NETBSD
uptr internal_ptrace(int request, int pid, void *addr, int data);
#else
uptr internal_ptrace(int request, int pid, void *addr, void *data);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
index 7ee2319456d23e..715a58eae41634 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -302,7 +302,7 @@ bool IsAccessibleMemoryRange(uptr beg, uptr size) {
// `read` from `fds[0]` into a dummy buffer to free up the pipe buffer for
// more `write` is slower than just recreating a pipe.
int fds[2];
- CHECK_EQ(0, pipe(fds));
+ CHECK_EQ(0, internal_pipe(fds));
auto cleanup = at_scope_exit([&]() {
internal_close(fds[0]);
@@ -330,7 +330,7 @@ bool TryMemCpy(void *dest, const void *src, uptr n) {
if (!n)
return true;
int fds[2];
- CHECK_EQ(0, pipe(fds));
+ CHECK_EQ(0, internal_pipe(fds));
auto cleanup = at_scope_exit([&]() {
internal_close(fds[0]);
diff --git a/compiler-rt/test/tsan/pipe.cpp b/compiler-rt/test/tsan/pipe.cpp
new file mode 100644
index 00000000000000..c24d3e95c9ea2b
--- /dev/null
+++ b/compiler-rt/test/tsan/pipe.cpp
@@ -0,0 +1,30 @@
+// RUN: %clangxx_tsan -fsanitize=undefined -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <iostream>
+#include <thread>
+
+class Foo {
+public:
+ void produce(int) {}
+ void consume() {}
+
+ void run() {
+ w1_ = std::thread{&Foo::produce, this, 0};
+ w2_ = std::thread{&Foo::consume, this};
+ w1_.join();
+ w2_.join();
+ }
+
+private:
+ std::thread w1_;
+ std::thread w2_;
+};
+
+int main() {
+ Foo f;
+ f.run();
+ std::cerr << "Pass\n";
+ // CHECK-NOT: data race
+ // CHECK: Pass
+ return 0;
+}
More information about the llvm-commits
mailing list