Skip to content

Commit

Permalink
[compiler-rt] Replace direct calls to pipe with internal_pipe
Browse files Browse the repository at this point in the history
This patch tries to resolve google/sanitizers#1106 by introducing a new
internal_pipe function which will not be intercepted by TSAN.
  • Loading branch information
Lancern committed Oct 28, 2024
1 parent 1164bd7 commit 23e4389
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down
30 changes: 30 additions & 0 deletions compiler-rt/test/tsan/pipe.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 23e4389

Please sign in to comment.