Skip to content

Commit 23e4389

Browse files
committed
[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.
1 parent 1164bd7 commit 23e4389

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,10 @@ uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
847847
return internal_syscall(SYSCALL(lseek), fd, offset, whence);
848848
}
849849

850+
uptr internal_pipe(fd_t pipefd[2]) {
851+
return internal_syscall(SYSCALL(pipe), pipefd);
852+
}
853+
850854
# if SANITIZER_LINUX
851855
uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
852856
return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ uptr internal_unlink(const char *path) {
213213
return unlink(path);
214214
}
215215

216+
uptr internal_pipe(fd_t fildes[2]) { return pipe(fildes); }
217+
216218
uptr internal_sched_yield() {
217219
return sched_yield();
218220
}

compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ uptr internal_rename(const char *oldpath, const char *newpath) {
204204
return _REAL(rename, oldpath, newpath);
205205
}
206206

207+
uptr internal_pipe(fd_t fildes[2]) {
208+
DEFINE__REAL(int, pipe, int a[2]);
209+
return _REAL(pipe, fildes);
210+
}
211+
207212
uptr internal_sched_yield() {
208213
CHECK(&_sys_sched_yield);
209214
return _sys_sched_yield();

compiler-rt/lib/sanitizer_common/sanitizer_posix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ uptr internal_unlink(const char *path);
5656
uptr internal_rename(const char *oldpath, const char *newpath);
5757
uptr internal_lseek(fd_t fd, OFF_T offset, int whence);
5858

59-
#if SANITIZER_NETBSD
59+
uptr internal_pipe(fd_t pipefd[2]);
60+
61+
# if SANITIZER_NETBSD
6062
uptr internal_ptrace(int request, int pid, void *addr, int data);
6163
#else
6264
uptr internal_ptrace(int request, int pid, void *addr, void *data);

compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ bool IsAccessibleMemoryRange(uptr beg, uptr size) {
302302
// `read` from `fds[0]` into a dummy buffer to free up the pipe buffer for
303303
// more `write` is slower than just recreating a pipe.
304304
int fds[2];
305-
CHECK_EQ(0, pipe(fds));
305+
CHECK_EQ(0, internal_pipe(fds));
306306

307307
auto cleanup = at_scope_exit([&]() {
308308
internal_close(fds[0]);
@@ -330,7 +330,7 @@ bool TryMemCpy(void *dest, const void *src, uptr n) {
330330
if (!n)
331331
return true;
332332
int fds[2];
333-
CHECK_EQ(0, pipe(fds));
333+
CHECK_EQ(0, internal_pipe(fds));
334334

335335
auto cleanup = at_scope_exit([&]() {
336336
internal_close(fds[0]);

compiler-rt/test/tsan/pipe.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clangxx_tsan -fsanitize=undefined -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2+
3+
#include <iostream>
4+
#include <thread>
5+
6+
class Foo {
7+
public:
8+
void produce(int) {}
9+
void consume() {}
10+
11+
void run() {
12+
w1_ = std::thread{&Foo::produce, this, 0};
13+
w2_ = std::thread{&Foo::consume, this};
14+
w1_.join();
15+
w2_.join();
16+
}
17+
18+
private:
19+
std::thread w1_;
20+
std::thread w2_;
21+
};
22+
23+
int main() {
24+
Foo f;
25+
f.run();
26+
std::cerr << "Pass\n";
27+
// CHECK-NOT: data race
28+
// CHECK: Pass
29+
return 0;
30+
}

0 commit comments

Comments
 (0)