Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed

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

wants to merge 1 commit into from

Conversation

dustanddreams
Copy link

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.

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.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2023

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Miod Vallat (dustanddreams)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/75578.diff

1 Files Affected:

  • (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+2-9)
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

@dustanddreams
Copy link
Author

Fixed by #86625.

@dustanddreams dustanddreams deleted the glibc_fstat_interceptor_fix branch June 20, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants