Skip to content

[compiler-rt] adding preadv2/pwritev2 interceptions. #97216

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

Merged
merged 3 commits into from
Jul 15, 2024

Conversation

devnexen
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jun 30, 2024

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

Author: David CARLIER (devnexen)

Changes

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

2 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc (+34)
  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h (+3)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 1df61e79f7d84..eae0e8ebba1f7 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -10259,6 +10259,38 @@ INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T
 #define INIT_CPUSET_GETAFFINITY
 #endif
 
+#if SANITIZER_INTERCEPT_PREADV2
+INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
+            OFF_T offset, int flags) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
+  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
+  SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
+  if (res > 0) write_iovec(ctx, iov, iovcnt, res);
+  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
+  return res;
+}
+#define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
+#else
+#define INIT_PREADV2
+#endif
+
+#if SANITIZER_INTERCEPT_PWRITEV2
+INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
+            OFF_T offset, int flags) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
+  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
+  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
+  SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
+  if (res > 0) read_iovec(ctx, iov, iovcnt, res);
+  return res;
+}
+#define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
+#else
+#define INIT_PWRITEV2
+#endif
+
 #include "sanitizer_common_interceptors_netbsd_compat.inc"
 
 namespace __sanitizer {
@@ -10578,6 +10610,8 @@ static void InitializeCommonInterceptors() {
   INIT___XUNAME;
   INIT_ARGP_PARSE;
   INIT_CPUSET_GETAFFINITY;
+  INIT_PREADV2;
+  INIT_PWRITEV2;
 
   INIT___PRINTF_CHK;
 }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index de55c736d0e14..c94368b6b0ebb 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -598,6 +598,9 @@
 #define SANITIZER_INTERCEPT_PROCCTL SI_FREEBSD
 #define SANITIZER_INTERCEPT_ARGP_PARSE SI_GLIBC
 #define SANITIZER_INTERCEPT_CPUSET_GETAFFINITY SI_FREEBSD
+// FIXME: also available from musl 1.2.5
+#define SANITIZER_INTERCEPT_PREADV2 SI_GLIBC
+#define SANITIZER_INTERCEPT_PWRITEV2 SI_GLIBC
 
 // This macro gives a way for downstream users to override the above
 // interceptor macros irrespective of the platform they are on. They have

@devnexen devnexen requested a review from vitalybuka June 30, 2024 14:32
Copy link

github-actions bot commented Jun 30, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff d0d05aec3b6792136a9f75eb85dd2ea66005ae12 ccf1df8a2ade7413e6f133865afe9b81ba906b9a --extensions inc,cpp,h -- compiler-rt/test/sanitizer_common/TestCases/Linux/preadv2.cpp compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
View the diff from clang-format here.
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index eae0e8ebba..bac4fd82d5 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -10266,13 +10266,15 @@ INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
   COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
   SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
-  if (res > 0) write_iovec(ctx, iov, iovcnt, res);
-  if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
+  if (res > 0)
+    write_iovec(ctx, iov, iovcnt, res);
+  if (res >= 0 && fd >= 0)
+    COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
   return res;
 }
-#define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
+#  define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
 #else
-#define INIT_PREADV2
+#  define INIT_PREADV2
 #endif
 
 #if SANITIZER_INTERCEPT_PWRITEV2
@@ -10281,14 +10283,16 @@ INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
   COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
-  if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
+  if (fd >= 0)
+    COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
   SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
-  if (res > 0) read_iovec(ctx, iov, iovcnt, res);
+  if (res > 0)
+    read_iovec(ctx, iov, iovcnt, res);
   return res;
 }
-#define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
+#  define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
 #else
-#define INIT_PWRITEV2
+#  define INIT_PWRITEV2
 #endif
 
 #include "sanitizer_common_interceptors_netbsd_compat.inc"

@devnexen devnexen force-pushed the preadv2_pwritev2_san branch 2 times, most recently from dc52d47 to 3187014 Compare July 11, 2024 22:13
@devnexen devnexen force-pushed the preadv2_pwritev2_san branch from 3187014 to 47894f1 Compare July 15, 2024 19:06
@devnexen devnexen merged commit bc1c84a into llvm:main Jul 15, 2024
3 of 5 checks passed
@mysterymath
Copy link
Contributor

This broke the Fuchsia toolchain build:
https://luci-milo.appspot.com/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8742307122405229761/overview

Reverted, but let me know if the fix doesn't end up being obvious.

@devnexen
Copy link
Member Author

How should I disable the test for fuchsia ? it required glibc though, weird..

@mysterymath
Copy link
Contributor

mysterymath commented Jul 16, 2024

We're not running the test on Fuchsia, it's run on Linux as part of the host build. Our glibc version is old enough that it doesn't include preadv2 or pwritev2. There needs to be a CMake compilation check that enables or disables the test.

yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary: Co-authored-by: Vitaly Buka <[email protected]>

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251657
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.

4 participants