[compiler-rt] [TSan] Add interceptors for flockfile, ftrylockfile, funlockfile (PR #203470)

James Lim via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 00:30:43 PDT 2026


https://github.com/lim-james updated https://github.com/llvm/llvm-project/pull/203470

>From 3fe88eaf48005dd03acd583d1d4f287fa45963f0 Mon Sep 17 00:00:00 2001
From: james <jameslimbj at gmail.com>
Date: Fri, 12 Jun 2026 14:47:39 +0800
Subject: [PATCH 1/3] [tsan] Add interceptors for flockfile, ftrylockfile,
 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.

Signed-off-by: james <jameslimbj at gmail.com>
---
 .../lib/tsan/rtl/tsan_interceptors_posix.cpp  | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

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);

>From 409499b681b4c6aa1de1d342c3f5a9d9242e9094 Mon Sep 17 00:00:00 2001
From: james <jameslimbj at gmail.com>
Date: Fri, 12 Jun 2026 15:14:37 +0800
Subject: [PATCH 2/3] [TSan] Add test for POXIS stdio locking primitives
 interceptors

Signed-off-by: james <jameslimbj at gmail.com>
---
 compiler-rt/test/tsan/flockfile.cpp | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 compiler-rt/test/tsan/flockfile.cpp

diff --git a/compiler-rt/test/tsan/flockfile.cpp b/compiler-rt/test/tsan/flockfile.cpp
new file mode 100644
index 0000000000000..8d0a96d7c0c11
--- /dev/null
+++ b/compiler-rt/test/tsan/flockfile.cpp
@@ -0,0 +1,22 @@
+// RUN: %clangxx_tsan -O1 %s -o %t %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: darwin
+
+#include <stdio.h>
+#include <thread>
+
+int shared = 0;
+
+void worker() {
+  flockfile(stdout);
+  shared++;
+  funlockfile(stdout);
+}
+
+int main() {
+  std::thread t1(worker), t2(worker);
+  t1.join();
+  t2.join();
+}
+
+// CHECK-NOT: TheadSanitizer: data race
+// CHECK: ThreadSanitizer: no issues found

>From 8781453ce45b9307b9f35f0483f345e57a1bb236 Mon Sep 17 00:00:00 2001
From: James Lim <jameslimbj at gmail.com>
Date: Fri, 12 Jun 2026 15:30:33 +0800
Subject: [PATCH 3/3] [TSan] Resolve typos in flockfile.cpp

---
 compiler-rt/test/tsan/flockfile.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/test/tsan/flockfile.cpp b/compiler-rt/test/tsan/flockfile.cpp
index 8d0a96d7c0c11..8355d653ddd4c 100644
--- a/compiler-rt/test/tsan/flockfile.cpp
+++ b/compiler-rt/test/tsan/flockfile.cpp
@@ -1,4 +1,4 @@
-// RUN: %clangxx_tsan -O1 %s -o %t %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
 // UNSUPPORTED: darwin
 
 #include <stdio.h>
@@ -18,5 +18,5 @@ int main() {
   t2.join();
 }
 
-// CHECK-NOT: TheadSanitizer: data race
+// CHECK-NOT: ThreadSanitizer: data race
 // CHECK: ThreadSanitizer: no issues found



More information about the llvm-commits mailing list