| Issue |
239326
|
| Summary |
[TSan] Add an interceptor for pthread_clockjoin_np — unintercepted join leaves a stale user_id in ThreadRegistry::live_, CHECK-aborts on pthread_t reuse
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
DropGuard
|
**Component:** compiler-rt (tsan) · **Seen on:** gcc-15.2.0 libtsan.so.2 and clang-21.1.8 libclang_rt.tsan (Ubuntu 26.04, glibc 2.4x) · Sibling gap: #62623 (mutex_clocklock / cond_clockwait)
## Summary
glibc 2.31 added `pthread_clockjoin_np(thread, retval, clockid, abstime)` (default symbol version since 2.34). TSan intercepts `pthread_join`, `pthread_tryjoin_np` and `pthread_timedjoin_np`, but **not** `pthread_clockjoin_np` — confirmed by `nm -D` on both runtimes above and by its absence from `tsan_interceptors_posix.cpp`.
A thread joined through the unintercepted call therefore never has its `user_id` consumed: `ThreadRegistry::live_` is inserted in `CreateThread` under the duplicate-user_id CHECK and erased only when an intercepted join runs `ThreadConsumeTid`, or on detach/never-start. The real join still happens glibc-side, the stack is freed, and **glibc hands the identical pthread_t value back immediately**. The next `pthread_create` calls `ThreadCreate(..., *(uptr*)th, ...)` with that reused value, `live_.try_emplace(user_id, tid)` fails, and the process aborts:
ThreadSanitizer: CHECK failed: sanitizer_thread_registry.cpp:186
"((live_.try_emplace(user_id, tid).second)) != (0)"
This is the registry's designed-in duplicate-user_id guard doing its job — the failure is loud, and the stale entry is the runtime's own.
## Blast radius
Any program that joins threads via `pthread_clockjoin_np`. Not hypothetical: **Qt 6.10's `QThread::wait` joins exclusively via `pthread_clockjoin_np`** on glibc ≥ 2.31 builds (`nm -D libQt6Core.so.6` references only `pthread_clockjoin_np` and `pthread_create` — no `pthread_join`/`tryjoin_np`/`timedjoin_np`/`detach`), so effectively every Qt 6.10 application under TSan that creates and joins QThreads is exposed. `QThreadPool::waitForDone()` (which since 6.10 resets the pool, joining every worker) and QtTest's per-class WatchDog thread both trip it deterministically.
## Reproducer
Qt-only, 30 lines (churn.cpp — one QThreadPool, start()+waitForDone() waves). Aborts on the **second** wave, 3/3 runs, both runtimes; the same binary runs clean uninstrumented. Full write-up, lifecycle log and the build lines: available on request (kept next to this draft).
The mechanism is four steps, each verified:
1. per wave: 1× `pthread_create` + 1× `pthread_clockjoin_np`, zero other join flavors (LD_PRELOAD lifecycle log);
2. TSan: `user_id` = pthread_t **value** (`*(uptr*)th`), erased only by intercepted joins;
3. glibc recycles the value immediately (identical `pthread_t` across all waves);
4. next wave's `pthread_create` → `try_emplace` → CHECK.
## Proposed fix
A `TSAN_INTERCEPTOR` for `pthread_clockjoin_np` with the same body as `pthread_timedjoin_np`: `ThreadConsumeTid` before the real call, `ThreadJoin`/`ThreadNotJoined` after. Happy to verify against the reproducer.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs