[compiler-rt] [compiler-rt] [libtsan] fix fstat{, 64} interceptors on glibc (PR #75578)

Miod Vallat via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 01:23:30 PST 2023


https://github.com/dustanddreams created https://github.com/llvm/llvm-project/pull/75578

When targeting a glibc environment, the `fstat` and `fstat64` interceptors directly invoke `__fxstat64` rather than `fstat` and `fstat64`.

Unfortunately, the `__fxstat64` Linux interface is not machine-independent, as it takes a "`struct stat` version number" argument, which values vary across architectures and do not seem to be exposed in userland headers, as reported and analyzed in issue #75346.

This PR implements the suggested fix in the issue by invoking `fstat` and `fstat64` as expected, and letting them invoke `__fxstat64` with the proper, machine-specific, arguments.

>From 633884748c82acf4026ad773a08fc44fba9ef3f9 Mon Sep 17 00:00:00 2001
From: Miod Vallat <miod at tarides.com>
Date: Fri, 15 Dec 2023 07:42:07 +0000
Subject: [PATCH] Make the fstat{,64} interceptors wrap fstat{,64} on glibc.

Invoking the underlying __fxstat64 routine is not portable, for the
stat struct version argument is platform-dependent and not exposed in
the userland headers.

Fixes #75346.
---
 compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 80f86ca98ed9cd..28f6ccffcc4420 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -1603,17 +1603,10 @@ TSAN_INTERCEPTOR(int, __fxstat, int version, int fd, void *buf) {
 #endif
 
 TSAN_INTERCEPTOR(int, fstat, int fd, void *buf) {
-#if SANITIZER_GLIBC
-  SCOPED_TSAN_INTERCEPTOR(__fxstat, 0, fd, buf);
-  if (fd > 0)
-    FdAccess(thr, pc, fd);
-  return REAL(__fxstat)(0, fd, buf);
-#else
   SCOPED_TSAN_INTERCEPTOR(fstat, fd, buf);
   if (fd > 0)
     FdAccess(thr, pc, fd);
   return REAL(fstat)(fd, buf);
-#endif
 }
 
 #if SANITIZER_GLIBC
@@ -1630,10 +1623,10 @@ TSAN_INTERCEPTOR(int, __fxstat64, int version, int fd, void *buf) {
 
 #if SANITIZER_GLIBC
 TSAN_INTERCEPTOR(int, fstat64, int fd, void *buf) {
-  SCOPED_TSAN_INTERCEPTOR(__fxstat64, 0, fd, buf);
+  SCOPED_TSAN_INTERCEPTOR(fstat64, fd, buf);
   if (fd > 0)
     FdAccess(thr, pc, fd);
-  return REAL(__fxstat64)(0, fd, buf);
+  return REAL(fstat64)(fd, buf);
 }
 #define TSAN_MAYBE_INTERCEPT_FSTAT64 TSAN_INTERCEPT(fstat64)
 #else



More information about the llvm-commits mailing list