Skip to content

Commit fe97a61

Browse files
authored
[tsan] Add callbacks for futex syscalls and mark them as blocking on tsan (#86537)
Fixes #83844. This PR adds callbacks to mark futex syscalls as blocking. Unfortunately we didn't have a mechanism before to mark syscalls as a blocking call, so I had to implement it, but it mostly reuses the `BlockingCall` implementation [here](https://github.com/llvm/llvm-project/blob/96819daa3d095cf9f662e0229dc82eaaa25480e8/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp#L362-L380). The issue includes some information but this issue was discovered because Rust uses futexes directly. So most likely we need to update Rust as well to use these callbacks. Also see the latest comments in #85188 for some context. I also sent another PR #84162 to mark `pthread_*_lock` calls as blocking.
1 parent f01377d commit fe97a61

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

compiler-rt/include/sanitizer/linux_syscall_hooks.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,15 @@
18561856
__sanitizer_syscall_pre_impl_sigaltstack((long)ss, (long)oss)
18571857
#define __sanitizer_syscall_post_sigaltstack(res, ss, oss) \
18581858
__sanitizer_syscall_post_impl_sigaltstack(res, (long)ss, (long)oss)
1859+
#define __sanitizer_syscall_pre_futex(uaddr, futex_op, val, timeout, uaddr2, \
1860+
val3) \
1861+
__sanitizer_syscall_pre_impl_futex((long)uaddr, (long)futex_op, (long)val, \
1862+
(long)timeout, (long)uaddr2, (long)val3)
1863+
#define __sanitizer_syscall_post_futex(res, uaddr, futex_op, val, timeout, \
1864+
uaddr2, val3) \
1865+
__sanitizer_syscall_post_impl_futex(res, (long)uaddr, (long)futex_op, \
1866+
(long)val, (long)timeout, (long)uaddr2, \
1867+
(long)val3)
18591868

18601869
// And now a few syscalls we don't handle yet.
18611870
#define __sanitizer_syscall_pre_afs_syscall(...)
@@ -1875,7 +1884,6 @@
18751884
#define __sanitizer_syscall_pre_fchown32(...)
18761885
#define __sanitizer_syscall_pre_ftime(...)
18771886
#define __sanitizer_syscall_pre_ftruncate64(...)
1878-
#define __sanitizer_syscall_pre_futex(...)
18791887
#define __sanitizer_syscall_pre_getegid32(...)
18801888
#define __sanitizer_syscall_pre_geteuid32(...)
18811889
#define __sanitizer_syscall_pre_getgid32(...)
@@ -1954,7 +1962,6 @@
19541962
#define __sanitizer_syscall_post_fchown32(res, ...)
19551963
#define __sanitizer_syscall_post_ftime(res, ...)
19561964
#define __sanitizer_syscall_post_ftruncate64(res, ...)
1957-
#define __sanitizer_syscall_post_futex(res, ...)
19581965
#define __sanitizer_syscall_post_getegid32(res, ...)
19591966
#define __sanitizer_syscall_post_geteuid32(res, ...)
19601967
#define __sanitizer_syscall_post_getgid32(res, ...)
@@ -3093,6 +3100,11 @@ void __sanitizer_syscall_post_impl_rt_sigaction(long res, long signum, long act,
30933100
long oldact, long sz);
30943101
void __sanitizer_syscall_pre_impl_sigaltstack(long ss, long oss);
30953102
void __sanitizer_syscall_post_impl_sigaltstack(long res, long ss, long oss);
3103+
void __sanitizer_syscall_pre_impl_futex(long uaddr, long futex_op, long val,
3104+
long timeout, long uaddr2, long val3);
3105+
void __sanitizer_syscall_post_impl_futex(long res, long uaddr, long futex_op,
3106+
long val, long timeout, long uaddr2,
3107+
long val3);
30963108
#ifdef __cplusplus
30973109
} // extern "C"
30983110
#endif

compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
// Called before fork syscall.
3939
// COMMON_SYSCALL_POST_FORK(long res)
4040
// Called after fork syscall.
41+
// COMMON_SYSCALL_BLOCKING_START()
42+
// Called before blocking syscall.
43+
// COMMON_SYSCALL_BLOCKING_END()
44+
// Called after blocking syscall.
4145
//===----------------------------------------------------------------------===//
4246

4347
#include "sanitizer_platform.h"
@@ -85,6 +89,16 @@
8589
{}
8690
# endif
8791

92+
# ifndef COMMON_SYSCALL_BLOCKING_START
93+
# define COMMON_SYSCALL_BLOCKING_START() \
94+
{}
95+
# endif
96+
97+
# ifndef COMMON_SYSCALL_BLOCKING_END
98+
# define COMMON_SYSCALL_BLOCKING_END() \
99+
{}
100+
# endif
101+
88102
// FIXME: do some kind of PRE_READ for all syscall arguments (int(s) and such).
89103

90104
extern "C" {
@@ -3176,6 +3190,18 @@ POST_SYSCALL(sigaltstack)(long res, void *ss, void *oss) {
31763190
}
31773191
}
31783192
}
3193+
3194+
PRE_SYSCALL(futex)
3195+
(void *uaddr, long futex_op, long val, void *timeout, void *uaddr2, long val3) {
3196+
COMMON_SYSCALL_BLOCKING_START();
3197+
}
3198+
3199+
POST_SYSCALL(futex)
3200+
(long res, void *uaddr, long futex_op, long val, void *timeout, void *uaddr2,
3201+
long val3) {
3202+
COMMON_SYSCALL_BLOCKING_END();
3203+
}
3204+
31793205
} // extern "C"
31803206

31813207
# undef PRE_SYSCALL

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,25 @@ static USED void syscall_fd_release(uptr pc, int fd) {
26732673
FdRelease(thr, pc, fd);
26742674
}
26752675

2676+
static USED void sycall_blocking_start() {
2677+
DPrintf("sycall_blocking_start()\n");
2678+
ThreadState *thr = cur_thread();
2679+
EnterBlockingFunc(thr);
2680+
// When we are in a "blocking call", we process signals asynchronously
2681+
// (right when they arrive). In this context we do not expect to be
2682+
// executing any user/runtime code. The known interceptor sequence when
2683+
// this is not true is: pthread_join -> munmap(stack). It's fine
2684+
// to ignore munmap in this case -- we handle stack shadow separately.
2685+
thr->ignore_interceptors++;
2686+
}
2687+
2688+
static USED void sycall_blocking_end() {
2689+
DPrintf("sycall_blocking_end()\n");
2690+
ThreadState *thr = cur_thread();
2691+
thr->ignore_interceptors--;
2692+
atomic_store(&thr->in_blocking_func, 0, memory_order_relaxed);
2693+
}
2694+
26762695
static void syscall_pre_fork(uptr pc) { ForkBefore(cur_thread(), pc); }
26772696

26782697
static void syscall_post_fork(uptr pc, int pid) {
@@ -2727,6 +2746,9 @@ static void syscall_post_fork(uptr pc, int pid) {
27272746
#define COMMON_SYSCALL_POST_FORK(res) \
27282747
syscall_post_fork(GET_CALLER_PC(), res)
27292748

2749+
#define COMMON_SYSCALL_BLOCKING_START() sycall_blocking_start()
2750+
#define COMMON_SYSCALL_BLOCKING_END() sycall_blocking_end()
2751+
27302752
#include "sanitizer_common/sanitizer_common_syscalls.inc"
27312753
#include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
27322754

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// RUN: %clang_tsan %s -lstdc++ -o %t && %run %t 2>&1 | FileCheck %s
2+
3+
#include "test.h"
4+
#include <errno.h>
5+
#include <linux/futex.h>
6+
#include <pthread.h>
7+
#include <signal.h>
8+
#include <stdio.h>
9+
#include <sys/syscall.h>
10+
11+
#include <cassert>
12+
#include <stdexcept>
13+
#include <thread>
14+
15+
#include <sanitizer/linux_syscall_hooks.h>
16+
17+
int futex(int *uaddr, int futex_op, int val, const struct timespec *timeout,
18+
int *uaddr2, int val3) {
19+
__sanitizer_syscall_pre_futex(uaddr, futex_op, val, timeout, uaddr2, val3);
20+
int result = syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
21+
__sanitizer_syscall_post_futex(result, uaddr, futex_op, val, timeout, uaddr2,
22+
val3);
23+
return result;
24+
}
25+
26+
// Simple mutex implementation using futex.
27+
class Mutex {
28+
public:
29+
Mutex() : value(0) {}
30+
31+
void lock() {
32+
int c;
33+
while ((c = __sync_val_compare_and_swap(&value, 0, 1)) != 0) {
34+
if (c != 1)
35+
continue;
36+
int r = futex(&value, FUTEX_WAIT_PRIVATE, 1, nullptr, nullptr, 0);
37+
if (r == -1 && errno != EAGAIN) {
38+
fprintf(stderr, "futex wait error\n");
39+
abort();
40+
}
41+
}
42+
}
43+
44+
void unlock() {
45+
value = 0;
46+
int r = futex(&value, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
47+
if (r == -1) {
48+
fprintf(stderr, "futex wake error\n");
49+
abort();
50+
}
51+
}
52+
53+
private:
54+
int value;
55+
};
56+
57+
Mutex mutex;
58+
59+
void *Thread(void *x) {
60+
// fprintf(stderr, "canova here thread 0\n");
61+
// Waiting for the futex.
62+
mutex.lock();
63+
// fprintf(stderr, "canova here thread 1\n");
64+
// Finished waiting.
65+
return nullptr;
66+
}
67+
68+
static void SigprofHandler(int signal, siginfo_t *info, void *context) {
69+
// fprintf(stderr, "canova here sigprof handler\n");
70+
// Unlock the futex.
71+
mutex.unlock();
72+
}
73+
74+
void InstallSignalHandler() {
75+
struct sigaction sa;
76+
sa.sa_sigaction = SigprofHandler;
77+
sigemptyset(&sa.sa_mask);
78+
sa.sa_flags = SA_RESTART | SA_SIGINFO;
79+
if (sigaction(SIGPROF, &sa, 0) != 0) {
80+
fprintf(stderr, "failed to install signal handler\n");
81+
abort();
82+
}
83+
}
84+
85+
int main() {
86+
alarm(60); // Kill the test if it hangs.
87+
88+
// Install the signal handler
89+
InstallSignalHandler();
90+
91+
// Lock the futex at first so the other thread will wait for it.
92+
mutex.lock();
93+
94+
// Create the thread to wait for the futex.
95+
pthread_t thread;
96+
pthread_create(&thread, NULL, Thread, NULL);
97+
98+
// Just waiting a bit to make sure the thead is at the FUTEX_WAIT_PRIVATE
99+
// syscall.
100+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
101+
102+
// Send the signal to the other thread, which will send the futex wake
103+
// syscall.
104+
int r = pthread_kill(thread, SIGPROF);
105+
assert(r == 0);
106+
107+
// Futex should be notified and the thread should be able to continue.
108+
pthread_join(thread, NULL);
109+
110+
// Exiting successfully.
111+
fprintf(stderr, "PASS\n");
112+
return 0;
113+
}
114+
115+
// CHECK-NOT: WARNING: ThreadSanitizer:
116+
// CHECK: PASS

0 commit comments

Comments
 (0)