[compiler-rt] [TSan] Add interceptors for flockfile, ftrylockfile, funlockfile (PR #203470)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 12 00:00:40 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: James Lim (lim-james)
<details>
<summary>Changes</summary>
This patch adds TSan interceptors for `flockfile`, `ftrylockfile`, and `funlockfile`.
`flockfile`/`funlockfile` are POSIX stdio locking primitives for mutual exclusion on FILE streams. However, TSan does not have interceptors to account for them, so happens-before relations are not tracked, thus causing false positive data race reports.
This fixes false positives for `std::println` on Glibc platforms as libstdc++'s _File_sink acquires `flockfile`, which was invisible to TSan.
Verified by:
- Confirming TSan reports a race on flockfile-protected code without the fix
- Confirming silence with the fix applied
- Confirming real unprotected races are still caught
Resolves #<!-- -->203451
---
Full diff: https://github.com/llvm/llvm-project/pull/203470.diff
1 Files Affected:
- (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+27)
``````````diff
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index e00853bd1b426..ed123cae3b8a9 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -1762,6 +1762,30 @@ TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
# define TSAN_MAYBE_INTERCEPT_FSTAT64
#endif
+TSAN_INTERCEPTOR(void, flockfile, void *file) {
+ SCOPED_TSAN_INTERCEPTOR(flockfile, file);
+ MutexPreLock(thr, pc, (uptr)file,
+ MutexFlagWriteReentrant | MutexFlagNotStatic);
+ BLOCK_REAL(flockfile)(file);
+ MutexPostLock(thr, pc, (uptr)file,
+ MutexFlagWriteReentrant | MutexFlagNotStatic);
+}
+
+TSAN_INTERCEPTOR(int, ftrylockfile, void *file) {
+ SCOPED_TSAN_INTERCEPTOR(ftrylockfile, file);
+ int res = REAL(ftrylockfile)(file);
+ if (res == 0)
+ MutexPostLock(thr, pc, (uptr)file,
+ MutexFlagWriteReentrant | MutexFlagNotStatic | MutexFlagTryLock);
+ return res;
+}
+
+TSAN_INTERCEPTOR(void, funlockfile, void *file) {
+ SCOPED_TSAN_INTERCEPTOR(funlockfile, file);
+ MutexUnlock(thr, pc, (uptr)file);
+ REAL(funlockfile)(file);
+}
+
TSAN_INTERCEPTOR(int, open, const char *name, int oflag, ...) {
mode_t mode = 0;
if (OpenReadsVaArgs(oflag)) {
@@ -3142,6 +3166,9 @@ void InitializeInterceptors() {
TSAN_MAYBE_INTERCEPT___FXSTAT;
TSAN_MAYBE_INTERCEPT_FSTAT;
TSAN_MAYBE_INTERCEPT_FSTAT64;
+ TSAN_INTERCEPT(flockfile);
+ TSAN_INTERCEPT(ftrylockfile);
+ TSAN_INTERCEPT(funlockfile);
TSAN_INTERCEPT(open);
TSAN_MAYBE_INTERCEPT_OPEN64;
TSAN_INTERCEPT(creat);
``````````
</details>
https://github.com/llvm/llvm-project/pull/203470
More information about the llvm-commits
mailing list