Blame SOURCES/valgrind-3.15.0-copy_file_range.patch

058efa
commit 5f00db054a6f59502e9deeeb59ace2261207ee31
058efa
Author: Alexandra Hajkova <ahajkova@redhat.com>
058efa
Date:   Thu May 2 08:24:02 2019 -0400
058efa
058efa
    Add support for the copy_file_range syscall
058efa
    
058efa
    Support amd64, x86, arm64, ppc64, ppc32 and s390x architectures.
058efa
    Also add sys-copy_file_range test case.
058efa
058efa
diff --git a/configure.ac b/configure.ac
058efa
index d043ce3..3528925 100755
058efa
--- a/configure.ac
058efa
+++ b/configure.ac
058efa
@@ -4172,6 +4172,7 @@ AC_CHECK_FUNCS([     \
058efa
         utimensat    \
058efa
         process_vm_readv  \
058efa
         process_vm_writev \
058efa
+        copy_file_range \
058efa
         ])
058efa
 
058efa
 # AC_CHECK_LIB adds any library found to the variable LIBS, and links these
058efa
@@ -4187,6 +4188,8 @@ AM_CONDITIONAL([HAVE_PTHREAD_SPINLOCK],
058efa
                [test x$ac_cv_func_pthread_spin_lock = xyes])
058efa
 AM_CONDITIONAL([HAVE_PTHREAD_SETNAME_NP],
058efa
                [test x$ac_cv_func_pthread_setname_np = xyes])
058efa
+AM_CONDITIONAL([HAVE_COPY_FILE_RANGE],
058efa
+               [test x$ac_cv_func_copy_file_range = xyes])
058efa
 
058efa
 if test x$VGCONF_PLATFORM_PRI_CAPS = xMIPS32_LINUX \
058efa
      -o x$VGCONF_PLATFORM_PRI_CAPS = xMIPS64_LINUX ; then
058efa
diff --git a/coregrind/m_syswrap/priv_syswrap-linux.h b/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
index f76191a..1edf9eb 100644
058efa
--- a/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
+++ b/coregrind/m_syswrap/priv_syswrap-linux.h
058efa
@@ -379,6 +379,7 @@ DECL_TEMPLATE(linux, sys_getsockname);
058efa
 DECL_TEMPLATE(linux, sys_getpeername);
058efa
 DECL_TEMPLATE(linux, sys_socketpair);
058efa
 DECL_TEMPLATE(linux, sys_kcmp);
058efa
+DECL_TEMPLATE(linux, sys_copy_file_range);
058efa
 
058efa
 // Some arch specific functions called from syswrap-linux.c
058efa
 extern Int do_syscall_clone_x86_linux ( Word (*fn)(void *), 
058efa
diff --git a/coregrind/m_syswrap/syswrap-amd64-linux.c b/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
index 30e7d0e..0c1d8d1 100644
058efa
--- a/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-amd64-linux.c
058efa
@@ -863,6 +863,8 @@ static SyscallTableEntry syscall_table[] = {
058efa
    LINXY(__NR_statx,             sys_statx),             // 332
058efa
 
058efa
    LINX_(__NR_membarrier,        sys_membarrier),        // 324
058efa
+
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 326
058efa
 };
058efa
 
058efa
 SyscallTableEntry* ML_(get_linux_syscall_entry) ( UInt sysno )
058efa
diff --git a/coregrind/m_syswrap/syswrap-arm64-linux.c b/coregrind/m_syswrap/syswrap-arm64-linux.c
058efa
index 290320a..f66be2d 100644
058efa
--- a/coregrind/m_syswrap/syswrap-arm64-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-arm64-linux.c
058efa
@@ -819,7 +819,7 @@ static SyscallTableEntry syscall_main_table[] = {
058efa
    //   (__NR_userfaultfd,       sys_ni_syscall),        // 282
058efa
    LINX_(__NR_membarrier,        sys_membarrier),        // 283
058efa
    //   (__NR_mlock2,            sys_ni_syscall),        // 284
058efa
-   //   (__NR_copy_file_range,   sys_ni_syscall),        // 285
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 285
058efa
    //   (__NR_preadv2,           sys_ni_syscall),        // 286
058efa
    //   (__NR_pwritev2,          sys_ni_syscall),        // 287
058efa
    //   (__NR_pkey_mprotect,     sys_ni_syscall),        // 288
058efa
diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c
058efa
index 73ef98d..cd0ee74 100644
058efa
--- a/coregrind/m_syswrap/syswrap-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-linux.c
058efa
@@ -12093,6 +12093,36 @@ POST(sys_bpf)
058efa
    }
058efa
 }
058efa
 
058efa
+PRE(sys_copy_file_range)
058efa
+{
058efa
+  PRINT("sys_copy_file_range (%lu, %lu, %lu, %lu, %lu, %lu)", ARG1, ARG2, ARG3,
058efa
+        ARG4, ARG5, ARG6);
058efa
+
058efa
+  PRE_REG_READ6(vki_size_t, "copy_file_range",
058efa
+                int, "fd_in",
058efa
+                vki_loff_t *, "off_in",
058efa
+                int, "fd_out",
058efa
+                vki_loff_t *, "off_out",
058efa
+                vki_size_t, "len",
058efa
+                unsigned int, "flags");
058efa
+
058efa
+  /* File descriptors are "specially" tracked by valgrind.
058efa
+     valgrind itself uses some, so make sure someone didn't
058efa
+     put in one of our own...  */
058efa
+  if (!ML_(fd_allowed)(ARG1, "copy_file_range(fd_in)", tid, False) ||
058efa
+      !ML_(fd_allowed)(ARG3, "copy_file_range(fd_in)", tid, False)) {
058efa
+     SET_STATUS_Failure( VKI_EBADF );
058efa
+  } else {
058efa
+     /* Now see if the offsets are defined. PRE_MEM_READ will
058efa
+        double check it can dereference them. */
058efa
+     if (ARG2 != 0)
058efa
+        PRE_MEM_READ( "copy_file_range(off_in)", ARG2, sizeof(vki_loff_t));
058efa
+     if (ARG4 != 0)
058efa
+        PRE_MEM_READ( "copy_file_range(off_out)", ARG4, sizeof(vki_loff_t));
058efa
+  }
058efa
+}
058efa
+
058efa
+
058efa
 #undef PRE
058efa
 #undef POST
058efa
 
058efa
diff --git a/coregrind/m_syswrap/syswrap-ppc32-linux.c b/coregrind/m_syswrap/syswrap-ppc32-linux.c
058efa
index f812f1f..71f208d 100644
058efa
--- a/coregrind/m_syswrap/syswrap-ppc32-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-ppc32-linux.c
058efa
@@ -1021,6 +1021,8 @@ static SyscallTableEntry syscall_table[] = {
058efa
    LINXY(__NR_getrandom,         sys_getrandom),        // 359
058efa
    LINXY(__NR_memfd_create,      sys_memfd_create),     // 360
058efa
 
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),  // 379
058efa
+
058efa
    LINXY(__NR_statx,             sys_statx),            // 383
058efa
 };
058efa
 
058efa
diff --git a/coregrind/m_syswrap/syswrap-ppc64-linux.c b/coregrind/m_syswrap/syswrap-ppc64-linux.c
058efa
index eada099..1a42c1f 100644
058efa
--- a/coregrind/m_syswrap/syswrap-ppc64-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-ppc64-linux.c
058efa
@@ -1007,6 +1007,8 @@ static SyscallTableEntry syscall_table[] = {
058efa
 
058efa
    LINX_(__NR_membarrier,        sys_membarrier),       // 365
058efa
 
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),  // 379
058efa
+
058efa
    LINXY(__NR_statx,             sys_statx),            // 383
058efa
 };
058efa
 
058efa
diff --git a/coregrind/m_syswrap/syswrap-s390x-linux.c b/coregrind/m_syswrap/syswrap-s390x-linux.c
058efa
index ad78384..41ada8d 100644
058efa
--- a/coregrind/m_syswrap/syswrap-s390x-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-s390x-linux.c
058efa
@@ -854,6 +854,8 @@ static SyscallTableEntry syscall_table[] = {
058efa
    LINXY(__NR_recvmsg, sys_recvmsg),                                  // 372
058efa
    LINX_(__NR_shutdown, sys_shutdown),                                // 373
058efa
 
058efa
+   LINX_(__NR_copy_file_range, sys_copy_file_range),                  // 375
058efa
+
058efa
    LINXY(__NR_statx, sys_statx),                                      // 379
058efa
 };
058efa
 
058efa
diff --git a/coregrind/m_syswrap/syswrap-x86-linux.c b/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
index f05619e..f8d97ea 100644
058efa
--- a/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-x86-linux.c
058efa
@@ -1608,6 +1608,8 @@ static SyscallTableEntry syscall_table[] = {
058efa
 
058efa
    LINX_(__NR_membarrier,        sys_membarrier),       // 375
058efa
 
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 377
058efa
+
058efa
    LINXY(__NR_statx,             sys_statx),            // 383
058efa
 
058efa
    /* Explicitly not supported on i386 yet. */
058efa
diff --git a/memcheck/tests/linux/Makefile.am b/memcheck/tests/linux/Makefile.am
058efa
index d7515d9..00e99a5 100644
058efa
--- a/memcheck/tests/linux/Makefile.am
058efa
+++ b/memcheck/tests/linux/Makefile.am
058efa
@@ -20,6 +20,7 @@ EXTRA_DIST = \
058efa
 	stack_switch.stderr.exp stack_switch.vgtest \
058efa
 	syscalls-2007.vgtest syscalls-2007.stderr.exp \
058efa
 	syslog-syscall.vgtest syslog-syscall.stderr.exp \
058efa
+	sys-copy_file_range.vgtest sys-copy_file_range.stderr.exp \
058efa
 	sys-openat.vgtest sys-openat.stderr.exp sys-openat.stdout.exp \
058efa
 	sys-statx.vgtest sys-statx.stderr.exp \
058efa
 	timerfd-syscall.vgtest timerfd-syscall.stderr.exp \
058efa
@@ -49,6 +50,10 @@ if HAVE_AT_FDCWD
058efa
 check_PROGRAMS += sys-openat
058efa
 endif
058efa
 
058efa
+if HAVE_COPY_FILE_RANGE
058efa
+        check_PROGRAMS += sys-copy_file_range
058efa
+endif
058efa
+
058efa
 AM_CFLAGS   += $(AM_FLAG_M3264_PRI)
058efa
 AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
058efa
 
058efa
diff --git a/memcheck/tests/linux/sys-copy_file_range.c b/memcheck/tests/linux/sys-copy_file_range.c
058efa
new file mode 100644
058efa
index 0000000..83981c6
058efa
--- /dev/null
058efa
+++ b/memcheck/tests/linux/sys-copy_file_range.c
058efa
@@ -0,0 +1,67 @@
058efa
+#define _GNU_SOURCE
058efa
+#include <fcntl.h>
058efa
+#include <stdio.h>
058efa
+#include <stdlib.h>
058efa
+#include <sys/stat.h>
058efa
+#include <sys/syscall.h>
058efa
+#include <unistd.h>
058efa
+
058efa
+int main(int argc, char **argv)
058efa
+{
058efa
+    int fd_in, fd_out;
058efa
+    struct stat stat;
058efa
+    loff_t len, ret;
058efa
+
058efa
+    fd_in = open("copy_file_range_source", O_CREAT | O_RDWR);
058efa
+    if (fd_in == -1) {
058efa
+        perror("open copy_file_range_source");
058efa
+        exit(EXIT_FAILURE);
058efa
+    }
058efa
+
058efa
+    if (write(fd_in, "foo bar\n", 8) != 8) {
058efa
+        perror("writing to the copy_file_range_source");
058efa
+        exit(EXIT_FAILURE);
058efa
+    }
058efa
+    lseek(fd_in, 0, SEEK_SET);
058efa
+
058efa
+    if (fstat(fd_in, &stat) == -1) {
058efa
+        perror("fstat");
058efa
+        exit(EXIT_FAILURE);
058efa
+    }
058efa
+
058efa
+    len = stat.st_size;
058efa
+
058efa
+    fd_out = open("copy_file_range_dest", O_CREAT | O_WRONLY | O_TRUNC, 0644);
058efa
+    if (fd_out == -1) {
058efa
+        perror("open copy_file_range_dest");
058efa
+        exit(EXIT_FAILURE);
058efa
+    }
058efa
+
058efa
+    /* Check copy_file_range called with the correct arguments works. */
058efa
+    do {
058efa
+        ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
058efa
+        if (ret == -1) {
058efa
+            perror("copy_file_range");
058efa
+            exit(EXIT_FAILURE);
058efa
+        }
058efa
+
058efa
+        len -= ret;
058efa
+    } while (len > 0);
058efa
+
058efa
+    /* Check valgrind will produce expected warnings for the
058efa
+       various wrong arguments. */
058efa
+    do {
058efa
+        void *t;
058efa
+        void *z = (void *) -1;
058efa
+
058efa
+        ret = copy_file_range(fd_in, t, fd_out, NULL, len, 0);
058efa
+        ret = copy_file_range(fd_in, NULL, fd_out, z, len, 0);
058efa
+        ret = copy_file_range(- 1, NULL, - 1, NULL, len, 0);
058efa
+    } while (0);
058efa
+
058efa
+    close(fd_in);
058efa
+    close(fd_out);
058efa
+    unlink("copy_file_range_source");
058efa
+    unlink("copy_file_range_dest");
058efa
+    exit(EXIT_SUCCESS);
058efa
+}
058efa
diff --git a/memcheck/tests/linux/sys-copy_file_range.stderr.exp b/memcheck/tests/linux/sys-copy_file_range.stderr.exp
058efa
new file mode 100644
058efa
index 0000000..1aa4dc2
058efa
--- /dev/null
058efa
+++ b/memcheck/tests/linux/sys-copy_file_range.stderr.exp
058efa
@@ -0,0 +1,21 @@
058efa
+
058efa
+Syscall param copy_file_range("off_in") contains uninitialised byte(s)
058efa
+   ...
058efa
+   by 0x........: main (sys-copy_file_range.c:57)
058efa
+
058efa
+Syscall param copy_file_range(off_out) points to unaddressable byte(s)
058efa
+   ...
058efa
+   by 0x........: main (sys-copy_file_range.c:58)
058efa
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
058efa
+
058efa
+Warning: invalid file descriptor -1 in syscall copy_file_range(fd_in)()
058efa
+
058efa
+HEAP SUMMARY:
058efa
+    in use at exit: 0 bytes in 0 blocks
058efa
+  total heap usage: 0 allocs, 0 frees, 0 bytes allocated
058efa
+
058efa
+For a detailed leak analysis, rerun with: --leak-check=full
058efa
+
058efa
+Use --track-origins=yes to see where uninitialised values come from
058efa
+For lists of detected and suppressed errors, rerun with: -s
058efa
+ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
058efa
diff --git a/memcheck/tests/linux/sys-copy_file_range.vgtest b/memcheck/tests/linux/sys-copy_file_range.vgtest
058efa
new file mode 100644
058efa
index 0000000..b7741e8
058efa
--- /dev/null
058efa
+++ b/memcheck/tests/linux/sys-copy_file_range.vgtest
058efa
@@ -0,0 +1,2 @@
058efa
+prereq: test -e sys-copy_file_range
058efa
+prog: sys-copy_file_range
058efa
commit bd27ad3ff31555484b7fdb310c4b033620882e44
058efa
Author: Mark Wielaard <mark@klomp.org>
058efa
Date:   Sun May 5 16:01:41 2019 +0200
058efa
058efa
    Hook linux copy_file_range syscall on arm.
058efa
058efa
diff --git a/coregrind/m_syswrap/syswrap-arm-linux.c b/coregrind/m_syswrap/syswrap-arm-linux.c
058efa
index 9f1bdab..9ba0665 100644
058efa
--- a/coregrind/m_syswrap/syswrap-arm-linux.c
058efa
+++ b/coregrind/m_syswrap/syswrap-arm-linux.c
058efa
@@ -1016,6 +1016,8 @@ static SyscallTableEntry syscall_main_table[] = {
058efa
    LINXY(__NR_getrandom,         sys_getrandom),        // 384
058efa
    LINXY(__NR_memfd_create,      sys_memfd_create),     // 385
058efa
 
058efa
+   LINX_(__NR_copy_file_range,   sys_copy_file_range),  // 391
058efa
+
058efa
    LINXY(__NR_statx,             sys_statx),            // 397
058efa
 };
058efa
 
058efa
commit c212b72a63e43be323a4e028bbdbe8b023c22be8
058efa
Author: Mark Wielaard <mark@klomp.org>
058efa
Date:   Wed May 15 21:30:00 2019 +0200
058efa
058efa
    Explicitly make testcase variable for sys-copy_file_range undefined.
058efa
    
058efa
    On some systems an extra warning could occur when a variable in
058efa
    the memcheck/tests/linux/sys-copy_file_range testcase was undefined,
058efa
    but (accidentially) pointed to known bad memory. Fix by defining the
058efa
    variable as 0, but then marking it explicitly undefined using memcheck
058efa
    VALGRIND_MAKE_MEM_UNDEFINED.
058efa
    
058efa
    Followup for https://bugs.kde.org/show_bug.cgi?id=407218
058efa
058efa
diff --git a/memcheck/tests/linux/sys-copy_file_range.c b/memcheck/tests/linux/sys-copy_file_range.c
058efa
index 83981c6..589399c 100644
058efa
--- a/memcheck/tests/linux/sys-copy_file_range.c
058efa
+++ b/memcheck/tests/linux/sys-copy_file_range.c
058efa
@@ -3,8 +3,8 @@
058efa
 #include <stdio.h>
058efa
 #include <stdlib.h>
058efa
 #include <sys/stat.h>
058efa
-#include <sys/syscall.h>
058efa
 #include <unistd.h>
058efa
+#include "../../memcheck.h"
058efa
 
058efa
 int main(int argc, char **argv)
058efa
 {
058efa
@@ -51,7 +51,7 @@ int main(int argc, char **argv)
058efa
     /* Check valgrind will produce expected warnings for the
058efa
        various wrong arguments. */
058efa
     do {
058efa
-        void *t;
058efa
+        void *t = 0; VALGRIND_MAKE_MEM_UNDEFINED (&t, sizeof (void *));
058efa
         void *z = (void *) -1;
058efa
 
058efa
         ret = copy_file_range(fd_in, t, fd_out, NULL, len, 0);
058efa
commit 033d013bebeb3471c0da47060deb9a5771e6c913
058efa
Author: Mark Wielaard <mark@klomp.org>
058efa
Date:   Fri May 24 21:51:31 2019 +0200
058efa
058efa
    Fix memcheck/tests/linux/sys-copy_file_range open call (mode).
058efa
    
058efa
    sys-copy_file_range.c calls open with O_CREAT flag and so must provide
058efa
    a mode argument. valgrind memcheck actually caught this ommission on
058efa
    some arches (fedora rawhide i686 specifically).
058efa
    
058efa
    This is a small additional fixup for
058efa
    https://bugs.kde.org/show_bug.cgi?id=407218
058efa
058efa
diff --git a/memcheck/tests/linux/sys-copy_file_range.c b/memcheck/tests/linux/sys-copy_file_range.c
058efa
index 589399c..3022fa1 100644
058efa
--- a/memcheck/tests/linux/sys-copy_file_range.c
058efa
+++ b/memcheck/tests/linux/sys-copy_file_range.c
058efa
@@ -12,7 +12,7 @@ int main(int argc, char **argv)
058efa
     struct stat stat;
058efa
     loff_t len, ret;
058efa
 
058efa
-    fd_in = open("copy_file_range_source", O_CREAT | O_RDWR);
058efa
+    fd_in = open("copy_file_range_source", O_CREAT | O_RDWR, 0644);
058efa
     if (fd_in == -1) {
058efa
         perror("open copy_file_range_source");
058efa
         exit(EXIT_FAILURE);