| Issue |
238305
|
| Summary |
[tsan][AArch64] setjmp interceptors tail-jump through x1/x2, which libc's `bti c` refuses: SIGILL on BTI systems
|
| Labels |
|
| Assignees |
|
| Reporter |
petersen65
|
Since 7b34070 (#95839, "[tsan][aarch64] Fix branch protection in interceptors"), `tsan_rtl_aarch64.S` makes the runtime BTI-marked:
- the `setjmp`, `_setjmp`, `sigsetjmp` and `__sigsetjmp` interceptors start with `BTI_C`;
- the file ends with `GNU_PROPERTY_BTI_PAC_GCS`.
So the runtime's pages are guarded. But the four interceptors still end with a tail jump through `x1` or `x2`:
```
adrp x2, :got:_ZN14__interception14real_sigsetjmpE
ldr x2, [x2, #:got_lo12:_ZN14__interception14real_sigsetjmpE]
ldr x2, [x2]
br x2
```
>From a guarded page, a `br` through any register other than `x16`/`x17` sets BTYPE to 0b11. glibc's `__sigsetjmp` begins with `bti c`, which accepts only BTYPE 0b01 and 0b10. The jump therefore raises a branch target exception, and on a CPU with BTI the process dies by SIGILL.
Every `pthread_cond_wait` under TSan takes this path:
1. The interceptor's `cond_wait` calls `call_pthread_cancel_with_cleanup`.
2. Its `pthread_cleanup_push` calls `__sigsetjmp`, the form glibc uses for code built without exceptions.
3. That call binds to the `__sigsetjmp` interceptor, whose tail jump faults.
### Reproducer
```c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t c = PTHREAD_COND_INITIALIZER; static int done;
static void *run(void *a) { usleep(100000); pthread_mutex_lock(&m); done = 1; pthread_cond_broadcast(&c); pthread_mutex_unlock(&m); return a; }
int main(void) { pthread_t t; pthread_create(&t, 0, run, 0); pthread_mutex_lock(&m); while (!done) pthread_cond_wait(&c, &m); pthread_mutex_unlock(&m); pthread_join(t, 0); puts("woke"); return 0; }
```
```
$ gcc -fsanitize=thread -g -O1 condwait.c -o condwait -lpthread && ./condwait
Illegal instruction (core dumped)
```
The expected output is `woke`. The program fails on every run, with `-mbranch-protection=standard` and with `-mbranch-protection=none` alike.
Under gdb:
```
Thread 1 "condwait" received signal SIGILL, Illegal instruction.
=> <__GI___sigsetjmp>: bti c
#0 __GI___sigsetjmp () at ../sysdeps/aarch64/setjmp.S:36
#1 __tsan::call_pthread_cancel_with_cleanup (...) at libsanitizer/tsan/tsan_platform_linux.cpp:636
#2 cond_wait<...> (...) at libsanitizer/tsan/tsan_interceptors_posix.cpp:1312
#3 ___interceptor_pthread_cond_wait (...) at libsanitizer/tsan/tsan_interceptors_posix.cpp:1327
#4 main () at condwait.c:6
```
The interceptor's own frame is gone by the time of the fault, because it pops its frame before the tail jump.
### Environment
- Ubuntu 26.04.1 LTS on arm64, kernel 7.0.14; the CPU reports `bti`.
- gcc 15.2.0-16ubuntu1. The runtime in use is libtsan2 16-20260322-1ubuntu1, which is GCC's libsanitizer copy and carries 7b34070. `readelf -n libtsan.so.2` shows `AArch64 feature: BTI, PAC, GCS`.
- glibc 2.43-2ubuntu2.4.
- llvm-project main has the same tail jumps (checked on 2026-11-03). Clang's runtime was not tried; it is built from the same file.
### Suggested fix
Load the target into `x16`, an intra-procedure-call scratch register, and branch through it. A `br x16` sets BTYPE 0b01, which `bti c` accepts, and `x16` carries no argument of any of the four functions.
```diff
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S
@@ -49,14 +49,14 @@
// tail jump to libc setjmp
#if !defined(__APPLE__)
- adrp x1, :got:_ZN14__interception11real_setjmpE
- ldr x1, [x1, #:got_lo12:_ZN14__interception11real_setjmpE]
- ldr x1, [x1]
+ adrp x16, :got:_ZN14__interception11real_setjmpE
+ ldr x16, [x16, #:got_lo12:_ZN14__interception11real_setjmpE]
+ ldr x16, [x16]
#else
- adrp x1, _setjmp@GOTPAGE
- ldr x1, [x1, _setjmp@GOTPAGEOFF]
+ adrp x16, _setjmp@GOTPAGE
+ ldr x16, [x16, _setjmp@GOTPAGEOFF]
#endif
- br x1
+ br x16
```
The same change applies to `_setjmp` (also `x1`) and to `sigsetjmp` and `__sigsetjmp` (`x2`). The full diff for all four is below.
### Verified
A copy of the installed `libtsan.so.2` was made with exactly these four tail jumps moved to `x16`, by a binary patch of the register fields of the `adrp`, both `ldr` instructions and the `br`. With that copy on `LD_LIBRARY_PATH`, the reproducer prints `woke` and exits 0 in 3 of 3 runs. The unpatched library ends by SIGILL on every run.
<details><summary>Full diff (all four interceptors)</summary>
```diff
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S
@@ -49,14 +49,14 @@
// tail jump to libc setjmp
#if !defined(__APPLE__)
- adrp x1, :got:_ZN14__interception11real_setjmpE
- ldr x1, [x1, #:got_lo12:_ZN14__interception11real_setjmpE]
- ldr x1, [x1]
+ adrp x16, :got:_ZN14__interception11real_setjmpE
+ ldr x16, [x16, #:got_lo12:_ZN14__interception11real_setjmpE]
+ ldr x16, [x16]
#else
- adrp x1, _setjmp@GOTPAGE
- ldr x1, [x1, _setjmp@GOTPAGEOFF]
+ adrp x16, _setjmp@GOTPAGE
+ ldr x16, [x16, _setjmp@GOTPAGEOFF]
#endif
- br x1
+ br x16
CFI_ENDPROC
ASM_SIZE(ASM_SYMBOL_INTERCEPTOR(setjmp))
@@ -100,14 +100,14 @@
// tail jump to libc setjmp
#if !defined(__APPLE__)
- adrp x1, :got:_ZN14__interception12real__setjmpE
- ldr x1, [x1, #:got_lo12:_ZN14__interception12real__setjmpE]
- ldr x1, [x1]
+ adrp x16, :got:_ZN14__interception12real__setjmpE
+ ldr x16, [x16, #:got_lo12:_ZN14__interception12real__setjmpE]
+ ldr x16, [x16]
#else
- adrp x1, __setjmp@GOTPAGE
- ldr x1, [x1, __setjmp@GOTPAGEOFF]
+ adrp x16, __setjmp@GOTPAGE
+ ldr x16, [x16, __setjmp@GOTPAGEOFF]
#endif
- br x1
+ br x16
CFI_ENDPROC
ASM_SIZE(ASM_SYMBOL_INTERCEPTOR(_setjmp))
@@ -153,14 +153,14 @@
// tail jump to libc sigsetjmp
#if !defined(__APPLE__)
- adrp x2, :got:_ZN14__interception14real_sigsetjmpE
- ldr x2, [x2, #:got_lo12:_ZN14__interception14real_sigsetjmpE]
- ldr x2, [x2]
+ adrp x16, :got:_ZN14__interception14real_sigsetjmpE
+ ldr x16, [x16, #:got_lo12:_ZN14__interception14real_sigsetjmpE]
+ ldr x16, [x16]
#else
- adrp x2, _sigsetjmp@GOTPAGE
- ldr x2, [x2, _sigsetjmp@GOTPAGEOFF]
+ adrp x16, _sigsetjmp@GOTPAGE
+ ldr x16, [x16, _sigsetjmp@GOTPAGEOFF]
#endif
- br x2
+ br x16
CFI_ENDPROC
ASM_SIZE(ASM_SYMBOL_INTERCEPTOR(sigsetjmp))
@@ -206,14 +206,14 @@
// tail jump to libc __sigsetjmp
#if !defined(__APPLE__)
- adrp x2, :got:_ZN14__interception16real___sigsetjmpE
- ldr x2, [x2, #:got_lo12:_ZN14__interception16real___sigsetjmpE]
- ldr x2, [x2]
+ adrp x16, :got:_ZN14__interception16real___sigsetjmpE
+ ldr x16, [x16, #:got_lo12:_ZN14__interception16real___sigsetjmpE]
+ ldr x16, [x16]
#else
- adrp x2, ASM_SYMBOL(__sigsetjmp)@page
- add x2, x2, ASM_SYMBOL(__sigsetjmp)@pageoff
+ adrp x16, ASM_SYMBOL(__sigsetjmp)@page
+ add x16, x16, ASM_SYMBOL(__sigsetjmp)@pageoff
#endif
- br x2
+ br x16
CFI_ENDPROC
ASM_SIZE(ASM_SYMBOL_INTERCEPTOR(__sigsetjmp))
#endif
```
</details>
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs