Blame SOURCES/valgrind-3.15.0-copy_file_range.patch

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