[compiler-rt] Reland copy file range san (PR #129114)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 27 12:38:45 PST 2025


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/129114

>From 24084a913eda90830523788fb7661c674c72f0ee Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Thu, 27 Feb 2025 20:32:30 +0000
Subject: [PATCH 1/2] Reapply "[compiler-rt][sanitizer_common] copy_file_range
 syscall interception. (#125816)" and fix

This reverts commit e5d93100b656df86854b58433816b0b03ef9f231.
---
 .../sanitizer_common_syscalls.inc             | 22 ++++++++++
 .../TestCases/Linux/copy_file_range.c         | 42 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc
index 29fe4721ba40d..521fc116f2888 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_syscalls.inc
@@ -3205,6 +3205,28 @@ POST_SYSCALL(futex)
   COMMON_SYSCALL_BLOCKING_END();
 }
 
+PRE_SYSCALL(copy_file_range)
+(int fdin, __sanitizer___kernel_off_t *offin, int fdout,
+ __sanitizer___kernel_off_t *offout, SIZE_T size, unsigned int flags) {
+  if (offin != nullptr) {
+    PRE_READ(offin, sizeof(*offin));
+  }
+  if (offout != nullptr) {
+    PRE_READ(offout, sizeof(*offout));
+  }
+}
+
+POST_SYSCALL(copy_file_range)
+(SSIZE_T, int fdin, __sanitizer___kernel_off_t *offin, int fdout,
+ __sanitizer___kernel_off_t *offout, SIZE_T size, unsigned int flags) {
+  if (offin != nullptr) {
+    POST_WRITE(offin, sizeof(*offin));
+  }
+  if (offout != nullptr) {
+    POST_WRITE(offout, sizeof(*offout));
+  }
+}
+
 }  // extern "C"
 
 #  undef PRE_SYSCALL
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c
new file mode 100644
index 0000000000000..2cfdfdcb59ba4
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c
@@ -0,0 +1,42 @@
+// RUN: %clangxx -O0 %s -D_FILE_OFFSET_BITS=64 -o %t
+
+// REQUIRES: glibc
+
+#ifndef _GNU_SOURCE
+#  define _GNU_SOURCE
+#endif
+#include <assert.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#if !defined(__GLIBC_PREREQ)
+#  define __GLIBC_PREREQ(a, b) 0
+#endif
+
+#if !__GLIBC_PREREQ(2, 27)
+#  define copy_file_range(a, b, c, d, e)                                       \
+    (ssize_t) syscall(__NR_copy_file_range, a, b, c, d, e)
+#endif
+
+int main(void) {
+  int fdin = open("/proc/self/maps", O_RDONLY);
+  assert(fdin > 0);
+  char tmp[] = "/tmp/map.XXXXXX";
+  int fdout = mkstemp(tmp);
+  assert(fdout > 0);
+  off_t offin = -1, offout = 0;
+  ssize_t cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
+  assert(cpy < 0);
+  offin = 0;
+  offout = 16;
+  cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
+  assert(cpy < 0);
+  offout = 0;
+  cpy = copy_file_range(fdin, &offin, fdout, &offout, 8, 0);
+  assert(cpy == 8);
+  close(fdout);
+  close(fdin);
+  return 0;
+}

>From 2399b93e440dca431f2e6247d59aa1614960dd6d Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Thu, 27 Feb 2025 20:33:52 +0000
Subject: [PATCH 2/2] fix missing argument for the wrapper

---
 .../test/sanitizer_common/TestCases/Linux/copy_file_range.c   | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c
index 2cfdfdcb59ba4..c9e3817c697c0 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/copy_file_range.c
@@ -16,8 +16,8 @@
 #endif
 
 #if !__GLIBC_PREREQ(2, 27)
-#  define copy_file_range(a, b, c, d, e)                                       \
-    (ssize_t) syscall(__NR_copy_file_range, a, b, c, d, e)
+#  define copy_file_range(a, b, c, d, e, f)                                    \
+    (ssize_t) syscall(__NR_copy_file_range, a, b, c, d, e, f)
 #endif
 
 int main(void) {



More information about the llvm-commits mailing list