f6cbdf
commit b0861063a8d2a55bb7423e90d26806bab0f78a12
f6cbdf
Author: Alexandra Hájková <ahajkova@redhat.com>
f6cbdf
Date:   Tue Jun 4 13:47:14 2019 +0200
f6cbdf
f6cbdf
    Add support for preadv2 and pwritev2 syscalls
f6cbdf
    
f6cbdf
    Support for amd64, x86 - 64 and 32 bit, arm64, ppc64, ppc64le,
f6cbdf
    s390x, mips64. This should work identically on all
f6cbdf
    arches, tested on x86 32bit and 64bit one, but enabled on all.
f6cbdf
    
f6cbdf
    Refactor the code to be reusable between old/new syscalls. Resolve TODO
f6cbdf
    items in the code. Add the testcase for the preadv2/pwritev2 and also
f6cbdf
    add the (similar) testcase for the older preadv/pwritev syscalls.
f6cbdf
    
f6cbdf
    Trying to test handling an uninitialized flag argument for the v2 syscalls
f6cbdf
    does not work because the flag always comes out as defined zero.
f6cbdf
    Turns out glibc does this deliberately on 64bit architectures because
f6cbdf
    the kernel does actually have a low_offset and high_offset argument, but
f6cbdf
    ignores the high_offset/assumes it is zero.
f6cbdf
    https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=601cc11d054ae4b5e9b5babec3d8e4667a2cb9b5
f6cbdf
    
f6cbdf
    https://bugs.kde.org/408414
f6cbdf
f6cbdf
diff --git a/configure.ac b/configure.ac
f6cbdf
index 352892565..3596d2fec 100755
f6cbdf
--- a/configure.ac
f6cbdf
+++ b/configure.ac
f6cbdf
@@ -4173,6 +4173,10 @@ AC_CHECK_FUNCS([     \
f6cbdf
         process_vm_readv  \
f6cbdf
         process_vm_writev \
f6cbdf
         copy_file_range \
f6cbdf
+        preadv \
f6cbdf
+        pwritev \
f6cbdf
+        preadv2 \
f6cbdf
+        pwritev2 \
f6cbdf
         ])
f6cbdf
 
f6cbdf
 # AC_CHECK_LIB adds any library found to the variable LIBS, and links these
f6cbdf
@@ -4190,6 +4194,10 @@ AM_CONDITIONAL([HAVE_PTHREAD_SETNAME_NP],
f6cbdf
                [test x$ac_cv_func_pthread_setname_np = xyes])
f6cbdf
 AM_CONDITIONAL([HAVE_COPY_FILE_RANGE],
f6cbdf
                [test x$ac_cv_func_copy_file_range = xyes])
f6cbdf
+AM_CONDITIONAL([HAVE_PREADV_PWRITEV],
f6cbdf
+               [test x$ac_cv_func_preadv = xyes && test x$ac_cv_func_pwritev = xyes])
f6cbdf
+AM_CONDITIONAL([HAVE_PREADV2_PWRITEV2],
f6cbdf
+               [test x$ac_cv_func_preadv2 = xyes && test x$ac_cv_func_pwritev2 = xyes])
f6cbdf
 
f6cbdf
 if test x$VGCONF_PLATFORM_PRI_CAPS = xMIPS32_LINUX \
f6cbdf
      -o x$VGCONF_PLATFORM_PRI_CAPS = xMIPS64_LINUX ; then
f6cbdf
diff --git a/coregrind/m_syswrap/priv_syswrap-generic.h b/coregrind/m_syswrap/priv_syswrap-generic.h
f6cbdf
index 3e1c8b682..73f9224f7 100644
f6cbdf
--- a/coregrind/m_syswrap/priv_syswrap-generic.h
f6cbdf
+++ b/coregrind/m_syswrap/priv_syswrap-generic.h
f6cbdf
@@ -109,6 +109,19 @@ ML_(handle_auxv_open)(SyscallStatus *status, const HChar *filename,
f6cbdf
 /* Helper function for generic mprotect and linux pkey_mprotect. */
f6cbdf
 extern void handle_sys_mprotect (ThreadId tid, SyscallStatus *status,
f6cbdf
                                  Addr *addr, SizeT *len, Int *prot);
f6cbdf
+/* Helper functions for preadv/preadv2. */
f6cbdf
+extern
f6cbdf
+void handle_pre_sys_preadv(ThreadId tid, SyscallStatus* status,
f6cbdf
+                           Int fd, Addr vector, Int count,
f6cbdf
+                           const char *str);
f6cbdf
+extern
f6cbdf
+void handle_post_sys_preadv(ThreadId tid, SyscallStatus* status, Addr vector, Int count);
f6cbdf
+
f6cbdf
+/* Helper function for pwritev/pwritev2. */
f6cbdf
+extern
f6cbdf
+void handle_sys_pwritev(ThreadId tid, SyscallStatus* status,
f6cbdf
+                        Int fd, Addr vector, Int count,
f6cbdf
+                        const char *str);
f6cbdf
 
f6cbdf
 DECL_TEMPLATE(generic, sys_ni_syscall);            // * P -- unimplemented
f6cbdf
 DECL_TEMPLATE(generic, sys_exit);
f6cbdf
diff --git a/coregrind/m_syswrap/priv_syswrap-linux.h b/coregrind/m_syswrap/priv_syswrap-linux.h
f6cbdf
index be2f9bdde..8ce8ef3d5 100644
f6cbdf
--- a/coregrind/m_syswrap/priv_syswrap-linux.h
f6cbdf
+++ b/coregrind/m_syswrap/priv_syswrap-linux.h
f6cbdf
@@ -46,7 +46,9 @@ DECL_TEMPLATE(linux, sys_oldumount);
f6cbdf
 DECL_TEMPLATE(linux, sys_umount);
f6cbdf
 DECL_TEMPLATE(linux, sys_perf_event_open);
f6cbdf
 DECL_TEMPLATE(linux, sys_preadv);
f6cbdf
+DECL_TEMPLATE(linux, sys_preadv2);
f6cbdf
 DECL_TEMPLATE(linux, sys_pwritev);
f6cbdf
+DECL_TEMPLATE(linux, sys_pwritev2);
f6cbdf
 DECL_TEMPLATE(linux, sys_sendmmsg);
f6cbdf
 DECL_TEMPLATE(linux, sys_recvmmsg);
f6cbdf
 DECL_TEMPLATE(linux, sys_dup3);
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-amd64-linux.c b/coregrind/m_syswrap/syswrap-amd64-linux.c
f6cbdf
index 382dc65cf..9b8068d0f 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-amd64-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-amd64-linux.c
f6cbdf
@@ -857,6 +857,8 @@ static SyscallTableEntry syscall_table[] = {
f6cbdf
 //   LIN__(__NR_kexec_file_load,   sys_ni_syscall),       // 320
f6cbdf
    LINXY(__NR_bpf,               sys_bpf),              // 321
f6cbdf
 
f6cbdf
+   LINXY(__NR_preadv2,           sys_preadv2),           // 327
f6cbdf
+   LINX_(__NR_pwritev2,          sys_pwritev2),          // 328
f6cbdf
 
f6cbdf
    LINXY(__NR_statx,             sys_statx),             // 332
f6cbdf
 
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-arm64-linux.c b/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
index c700e3dbe..d12d40632 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
@@ -818,8 +818,8 @@ static SyscallTableEntry syscall_main_table[] = {
f6cbdf
    LINX_(__NR_membarrier,        sys_membarrier),        // 283
f6cbdf
    //   (__NR_mlock2,            sys_ni_syscall),        // 284
f6cbdf
    LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 285
f6cbdf
-   //   (__NR_preadv2,           sys_ni_syscall),        // 286
f6cbdf
-   //   (__NR_pwritev2,          sys_ni_syscall),        // 287
f6cbdf
+   LINX_(__NR_preadv2,           sys_ni_syscall),        // 286
f6cbdf
+   LINX_(__NR_pwritev2,          sys_ni_syscall),        // 287
f6cbdf
    //   (__NR_pkey_mprotect,     sys_ni_syscall),        // 288
f6cbdf
    //   (__NR_pkey_alloc,        sys_ni_syscall),        // 289
f6cbdf
    //   (__NR_pkey_free,         sys_ni_syscall),        // 290
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c
f6cbdf
index 36d09d6e0..2fe15d97b 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-linux.c
f6cbdf
@@ -5501,12 +5501,57 @@ POST(sys_open_by_handle_at)
f6cbdf
 /* ---------------------------------------------------------------------
f6cbdf
    p{read,write}v wrappers
f6cbdf
    ------------------------------------------------------------------ */
f6cbdf
+/* This handles the common part of the PRE macro for preadv and preadv2. */
f6cbdf
+void handle_pre_sys_preadv(ThreadId tid, SyscallStatus* status,
f6cbdf
+                           Int fd, Addr vector, Int count, const char *str)
f6cbdf
+{
f6cbdf
+   struct vki_iovec * vec;
f6cbdf
+   Int i;
f6cbdf
+   /* safe size for the "preadv/preadv2(vector[i])" string */
f6cbdf
+   char tmp[30];
f6cbdf
+
f6cbdf
+   if (!ML_(fd_allowed)(fd, str, tid, False)) {
f6cbdf
+      SET_STATUS_Failure( VKI_EBADF );
f6cbdf
+   } else if (count > 0) {
f6cbdf
+      VG_(strcpy) (tmp, str);
f6cbdf
+      VG_(strcat) (tmp, "(vector)");
f6cbdf
+      PRE_MEM_READ( tmp, vector, count * sizeof(struct vki_iovec) );
f6cbdf
+
f6cbdf
+      if (ML_(safe_to_deref) ((void *)(Addr)vector,
f6cbdf
+                              count * sizeof(struct vki_iovec))) {
f6cbdf
+         vec = (struct vki_iovec *)(Addr)vector;
f6cbdf
+         for (i = 0; i < count; i++) {
f6cbdf
+            VG_(snprintf) (tmp, 30, "%s(vector[%d])", str, i);
f6cbdf
+            PRE_MEM_WRITE( tmp, (Addr)vec[i].iov_base, vec[i].iov_len );
f6cbdf
+         }
f6cbdf
+      }
f6cbdf
+   }
f6cbdf
+}
f6cbdf
+
f6cbdf
+/* This handles the common part of the POST macro for preadv and preadv2. */
f6cbdf
+void handle_post_sys_preadv(ThreadId tid, SyscallStatus* status, Addr vector, Int count)
f6cbdf
+{
f6cbdf
+    vg_assert(SUCCESS);
f6cbdf
+    if (RES > 0) {
f6cbdf
+        Int i;
f6cbdf
+        struct vki_iovec * vec = (struct vki_iovec *)(Addr)vector;
f6cbdf
+        Int remains = RES;
f6cbdf
+
f6cbdf
+        /* RES holds the number of bytes read. */
f6cbdf
+        for (i = 0; i < count; i++) {
f6cbdf
+            Int nReadThisBuf = vec[i].iov_len;
f6cbdf
+            if (nReadThisBuf > remains) nReadThisBuf = remains;
f6cbdf
+            POST_MEM_WRITE( (Addr)vec[i].iov_base, nReadThisBuf );
f6cbdf
+            remains -= nReadThisBuf;
f6cbdf
+            if (remains < 0) VG_(core_panic)("preadv: remains < 0");
f6cbdf
+        }
f6cbdf
+    }
f6cbdf
+}
f6cbdf
 
f6cbdf
 PRE(sys_preadv)
f6cbdf
 {
f6cbdf
-   Int i;
f6cbdf
-   struct vki_iovec * vec;
f6cbdf
    *flags |= SfMayBlock;
f6cbdf
+   const char *str = "preadv";
f6cbdf
 #if VG_WORDSIZE == 4
f6cbdf
    /* Note that the offset argument here is in lo+hi order on both
f6cbdf
       big and little endian platforms... */
f6cbdf
@@ -5525,45 +5570,89 @@ PRE(sys_preadv)
f6cbdf
 #else
f6cbdf
 #  error Unexpected word size
f6cbdf
 #endif
f6cbdf
-   if (!ML_(fd_allowed)(ARG1, "preadv", tid, False)) {
f6cbdf
-      SET_STATUS_Failure( VKI_EBADF );
f6cbdf
-   } else {
f6cbdf
-      PRE_MEM_READ( "preadv(vector)", ARG2, ARG3 * sizeof(struct vki_iovec) );
f6cbdf
+   Int fd = ARG1;
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
+
f6cbdf
+   handle_pre_sys_preadv(tid, status, fd, vector, count, str);
f6cbdf
 
f6cbdf
-      if (ARG2 != 0) {
f6cbdf
-         /* ToDo: don't do any of the following if the vector is invalid */
f6cbdf
-         vec = (struct vki_iovec *)(Addr)ARG2;
f6cbdf
-         for (i = 0; i < (Int)ARG3; i++)
f6cbdf
-            PRE_MEM_WRITE( "preadv(vector[...])",
f6cbdf
-                           (Addr)vec[i].iov_base, vec[i].iov_len );
f6cbdf
-      }
f6cbdf
-   }
f6cbdf
 }
f6cbdf
 
f6cbdf
 POST(sys_preadv)
f6cbdf
 {
f6cbdf
-   vg_assert(SUCCESS);
f6cbdf
-   if (RES > 0) {
f6cbdf
-      Int i;
f6cbdf
-      struct vki_iovec * vec = (struct vki_iovec *)(Addr)ARG2;
f6cbdf
-      Int remains = RES;
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
 
f6cbdf
-      /* RES holds the number of bytes read. */
f6cbdf
-      for (i = 0; i < (Int)ARG3; i++) {
f6cbdf
-	 Int nReadThisBuf = vec[i].iov_len;
f6cbdf
-	 if (nReadThisBuf > remains) nReadThisBuf = remains;
f6cbdf
-	 POST_MEM_WRITE( (Addr)vec[i].iov_base, nReadThisBuf );
f6cbdf
-	 remains -= nReadThisBuf;
f6cbdf
-	 if (remains < 0) VG_(core_panic)("preadv: remains < 0");
f6cbdf
+   handle_post_sys_preadv(tid, status, vector, count);
f6cbdf
+}
f6cbdf
+
f6cbdf
+PRE(sys_preadv2)
f6cbdf
+{
f6cbdf
+   *flags |= SfMayBlock;
f6cbdf
+   const char *str = "preadv2";
f6cbdf
+#if VG_WORDSIZE == 4
f6cbdf
+   /* Note that the offset argument here is in lo+hi order on both
f6cbdf
+      big and little endian platforms... */
f6cbdf
+   PRINT("sys_preadv2 ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %" FMT_REGWORD
f6cbdf
+         "u, %lld, %" FMT_REGWORD "u )",
f6cbdf
+         ARG1, ARG2, ARG3, (Long)LOHI64(ARG4,ARG5), ARG6);
f6cbdf
+   PRE_REG_READ6(ssize_t, "preadv2",
f6cbdf
+                 unsigned long, fd, const struct iovec *, vector,
f6cbdf
+                 unsigned long, count, vki_u32, offset_low,
f6cbdf
+                 vki_u32, offset_high, unsigned long, flags);
f6cbdf
+#elif VG_WORDSIZE == 8
f6cbdf
+   PRINT("sys_preadv2 ( %lu, %#lx, %lu, %ld, %lu )", ARG1, ARG2, ARG3, SARG4, ARG5);
f6cbdf
+   PRE_REG_READ5(ssize_t, "preadv2",
f6cbdf
+                 unsigned long, fd, const struct iovec *, vector,
f6cbdf
+                 unsigned long, count, Word, offset, unsigned long, flags);
f6cbdf
+#else
f6cbdf
+#  error Unexpected word size
f6cbdf
+#endif
f6cbdf
+   Int fd = ARG1;
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
+
f6cbdf
+   handle_pre_sys_preadv(tid, status, fd, vector, count, str);
f6cbdf
+}
f6cbdf
+
f6cbdf
+POST(sys_preadv2)
f6cbdf
+{
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
+
f6cbdf
+   handle_post_sys_preadv(tid, status, vector, count);
f6cbdf
+}
f6cbdf
+
f6cbdf
+/* This handles the common part of the PRE macro for pwritev and pwritev2. */
f6cbdf
+void handle_sys_pwritev(ThreadId tid, SyscallStatus* status,
f6cbdf
+                        Int fd, Addr vector, Int count, const char *str)
f6cbdf
+{
f6cbdf
+   Int i;
f6cbdf
+   struct vki_iovec * vec;
f6cbdf
+   /* safe size for the "preadv/preadv2(vector[i])" string */
f6cbdf
+   char tmp[30];
f6cbdf
+
f6cbdf
+   if (!ML_(fd_allowed)(fd, str, tid, False)) {
f6cbdf
+      SET_STATUS_Failure( VKI_EBADF );
f6cbdf
+   } else if (count > 0) {
f6cbdf
+      VG_(strcpy) (tmp, str);
f6cbdf
+      VG_(strcat) (tmp, "(vector)");
f6cbdf
+      PRE_MEM_READ( tmp, vector, count * sizeof(struct vki_iovec) );
f6cbdf
+      if (ML_(safe_to_deref) ((void *)(Addr)vector,
f6cbdf
+                              count * sizeof(struct vki_iovec))) {
f6cbdf
+         vec = (struct vki_iovec *)(Addr)vector;
f6cbdf
+         for (i = 0; i < count; i++) {
f6cbdf
+            VG_(snprintf) (tmp, 30, "%s(vector[%d])", str, i);
f6cbdf
+            PRE_MEM_READ( tmp, (Addr)vec[i].iov_base, vec[i].iov_len );
f6cbdf
+         }
f6cbdf
       }
f6cbdf
    }
f6cbdf
 }
f6cbdf
 
f6cbdf
 PRE(sys_pwritev)
f6cbdf
 {
f6cbdf
-   Int i;
f6cbdf
-   struct vki_iovec * vec;
f6cbdf
    *flags |= SfMayBlock;
f6cbdf
+   const char *str = "pwritev";
f6cbdf
 #if VG_WORDSIZE == 4
f6cbdf
    /* Note that the offset argument here is in lo+hi order on both
f6cbdf
       big and little endian platforms... */
f6cbdf
@@ -5581,19 +5670,41 @@ PRE(sys_pwritev)
f6cbdf
 #else
f6cbdf
 #  error Unexpected word size
f6cbdf
 #endif
f6cbdf
-   if (!ML_(fd_allowed)(ARG1, "pwritev", tid, False)) {
f6cbdf
-      SET_STATUS_Failure( VKI_EBADF );
f6cbdf
-   } else {
f6cbdf
-      PRE_MEM_READ( "pwritev(vector)", 
f6cbdf
-		     ARG2, ARG3 * sizeof(struct vki_iovec) );
f6cbdf
-      if (ARG2 != 0) {
f6cbdf
-         /* ToDo: don't do any of the following if the vector is invalid */
f6cbdf
-         vec = (struct vki_iovec *)(Addr)ARG2;
f6cbdf
-         for (i = 0; i < (Int)ARG3; i++)
f6cbdf
-            PRE_MEM_READ( "pwritev(vector[...])",
f6cbdf
-                           (Addr)vec[i].iov_base, vec[i].iov_len );
f6cbdf
-      }
f6cbdf
-   }
f6cbdf
+   Int fd = ARG1;
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
+
f6cbdf
+   handle_sys_pwritev(tid, status, fd, vector, count, str);
f6cbdf
+}
f6cbdf
+
f6cbdf
+PRE(sys_pwritev2)
f6cbdf
+{
f6cbdf
+   *flags |= SfMayBlock;
f6cbdf
+   const char *str = "pwritev2";
f6cbdf
+#if VG_WORDSIZE == 4
f6cbdf
+   /* Note that the offset argument here is in lo+hi order on both
f6cbdf
+      big and little endian platforms... */
f6cbdf
+   PRINT("sys_pwritev2 ( %" FMT_REGWORD "u, %#" FMT_REGWORD "x, %" FMT_REGWORD
f6cbdf
+         "u, %lld, %" FMT_REGWORD "u )",
f6cbdf
+         ARG1, ARG2, ARG3, (Long)LOHI64(ARG4,ARG5), ARG6);
f6cbdf
+   PRE_REG_READ6(ssize_t, "pwritev2",
f6cbdf
+                 unsigned long, fd, const struct iovec *, vector,
f6cbdf
+                 unsigned long, count, vki_u32, offset_low,
f6cbdf
+                 vki_u32, offset_high, unsigned long, flags);
f6cbdf
+#elif VG_WORDSIZE == 8
f6cbdf
+   /* Note offset_high isn't actually used?  */
f6cbdf
+   PRE_REG_READ6(ssize_t, "pwritev2",
f6cbdf
+                 unsigned long, fd, const struct iovec *, vector,
f6cbdf
+                 unsigned long, count, Word, offset,
f6cbdf
+		 Word, offset_high, unsigned long, flags);
f6cbdf
+#else
f6cbdf
+#  error Unexpected word size
f6cbdf
+#endif
f6cbdf
+   Int fd = ARG1;
f6cbdf
+   Addr vector = ARG2;
f6cbdf
+   Int count = ARG3;
f6cbdf
+
f6cbdf
+   handle_sys_pwritev(tid, status, fd, vector, count, str);
f6cbdf
 }
f6cbdf
 
f6cbdf
 /* ---------------------------------------------------------------------
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-ppc64-linux.c b/coregrind/m_syswrap/syswrap-ppc64-linux.c
f6cbdf
index baa2934ab..d65a664dd 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-ppc64-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-ppc64-linux.c
f6cbdf
@@ -1006,6 +1006,8 @@ static SyscallTableEntry syscall_table[] = {
f6cbdf
    LINX_(__NR_membarrier,        sys_membarrier),       // 365
f6cbdf
 
f6cbdf
    LINX_(__NR_copy_file_range,   sys_copy_file_range),  // 379
f6cbdf
+   LINX_(__NR_preadv2,           sys_preadv2),          // 380
f6cbdf
+   LINX_(__NR_pwritev2,          sys_pwritev2),         // 381
f6cbdf
 
f6cbdf
    LINXY(__NR_statx,             sys_statx),            // 383
f6cbdf
 };
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-s390x-linux.c b/coregrind/m_syswrap/syswrap-s390x-linux.c
f6cbdf
index 1481e768b..3354d41c0 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-s390x-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-s390x-linux.c
f6cbdf
@@ -853,6 +853,8 @@ static SyscallTableEntry syscall_table[] = {
f6cbdf
    LINX_(__NR_shutdown, sys_shutdown),                                // 373
f6cbdf
 
f6cbdf
    LINX_(__NR_copy_file_range, sys_copy_file_range),                  // 375
f6cbdf
+   LINXY(__NR_preadv2, sys_preadv2),                                  // 376
f6cbdf
+   LINX_(__NR_pwritev2, sys_pwritev2),                                // 377
f6cbdf
 
f6cbdf
    LINXY(__NR_statx, sys_statx),                                      // 379
f6cbdf
 };
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-x86-linux.c b/coregrind/m_syswrap/syswrap-x86-linux.c
f6cbdf
index 9ff53a92a..33d1213a3 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-x86-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-x86-linux.c
f6cbdf
@@ -1607,6 +1607,8 @@ static SyscallTableEntry syscall_table[] = {
f6cbdf
    LINX_(__NR_membarrier,        sys_membarrier),       // 375
f6cbdf
 
f6cbdf
    LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 377
f6cbdf
+   LINXY(__NR_preadv2,           sys_preadv2),           // 378
f6cbdf
+   LINX_(__NR_pwritev2,          sys_pwritev2),          // 379
f6cbdf
 
f6cbdf
    LINXY(__NR_pkey_mprotect,     sys_pkey_mprotect),    // 380
f6cbdf
    LINX_(__NR_pkey_alloc,        sys_pkey_alloc),       // 381
f6cbdf
diff --git a/memcheck/tests/linux/Makefile.am b/memcheck/tests/linux/Makefile.am
f6cbdf
index 00e99a52a..e13325869 100644
f6cbdf
--- a/memcheck/tests/linux/Makefile.am
f6cbdf
+++ b/memcheck/tests/linux/Makefile.am
f6cbdf
@@ -26,7 +26,9 @@ EXTRA_DIST = \
f6cbdf
 	timerfd-syscall.vgtest timerfd-syscall.stderr.exp \
f6cbdf
 	with-space.stderr.exp with-space.stdout.exp with-space.vgtest \
f6cbdf
 	proc-auxv.vgtest proc-auxv.stderr.exp getregset.vgtest \
f6cbdf
-	getregset.stderr.exp getregset.stdout.exp
f6cbdf
+	getregset.stderr.exp getregset.stdout.exp \
f6cbdf
+	sys-preadv_pwritev.vgtest sys-preadv_pwritev.stderr.exp \
f6cbdf
+	sys-preadv2_pwritev2.vgtest sys-preadv2_pwritev2.stderr.exp
f6cbdf
 
f6cbdf
 check_PROGRAMS = \
f6cbdf
 	brk \
f6cbdf
@@ -54,6 +56,14 @@ if HAVE_COPY_FILE_RANGE
f6cbdf
         check_PROGRAMS += sys-copy_file_range
f6cbdf
 endif
f6cbdf
 
f6cbdf
+if HAVE_PREADV_PWRITEV
f6cbdf
+        check_PROGRAMS += sys-preadv_pwritev
f6cbdf
+endif
f6cbdf
+
f6cbdf
+if HAVE_PREADV2_PWRITEV2
f6cbdf
+        check_PROGRAMS += sys-preadv2_pwritev2
f6cbdf
+endif
f6cbdf
+
f6cbdf
 AM_CFLAGS   += $(AM_FLAG_M3264_PRI)
f6cbdf
 AM_CXXFLAGS += $(AM_FLAG_M3264_PRI)
f6cbdf
 
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv2_pwritev2.c b/memcheck/tests/linux/sys-preadv2_pwritev2.c
f6cbdf
new file mode 100644
f6cbdf
index 000000000..942eab68b
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv2_pwritev2.c
f6cbdf
@@ -0,0 +1,79 @@
f6cbdf
+#define _GNU_SOURCE
f6cbdf
+#include <fcntl.h>
f6cbdf
+#include <stdio.h>
f6cbdf
+#include <stdlib.h>
f6cbdf
+#include <sys/stat.h>
f6cbdf
+#include <unistd.h>
f6cbdf
+#include <sys/syscall.h>
f6cbdf
+#include <sys/uio.h>
f6cbdf
+#include <string.h>
f6cbdf
+#include "../../memcheck.h"
f6cbdf
+
f6cbdf
+#include <errno.h>
f6cbdf
+
f6cbdf
+int main(int argc, char **argv)
f6cbdf
+{
f6cbdf
+    char str0[] = "hello ";
f6cbdf
+    char str1[] = "world\n";
f6cbdf
+    struct iovec iov[2];
f6cbdf
+    int fd;
f6cbdf
+
f6cbdf
+    fd = open("prwv2_source", O_CREAT | O_RDWR, 0644);
f6cbdf
+    if (fd == -1) {
f6cbdf
+        perror("prwv2_source");
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    iov[0].iov_base = str0;
f6cbdf
+    iov[0].iov_len = strlen(str0);
f6cbdf
+    iov[1].iov_base = str1;
f6cbdf
+    iov[1].iov_len = strlen(str1);
f6cbdf
+
f6cbdf
+    /* Check pwritev2 and preadv2 called with the correct arguments works. */
f6cbdf
+    if (pwritev2(fd, iov, 2, 0, 0) == -1) {
f6cbdf
+        perror("pwritev2");
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    if (preadv2(fd, iov, 2, 0, 0) == -1) {
f6cbdf
+        perror("preadv2");
f6cbdf
+        printf("errno: %d\n", errno);
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    /* Check valgrind will produce expected warnings for the
f6cbdf
+       various wrong arguments. */
f6cbdf
+    do {
f6cbdf
+        /* always allocate 16 bytes to not to have different .exps for different reg sizes */
f6cbdf
+        char *mem = malloc(16);
f6cbdf
+        void *t = (void *) &mem[0];
f6cbdf
+        void *z = (void *) -1;
f6cbdf
+	int c = *((int *) &mem[4]);
f6cbdf
+        int flag = *((int *) &mem[8]);
f6cbdf
+        pwritev2(fd, NULL, 2, 0, 0);
f6cbdf
+        pwritev2(fd, z, 2, 0, 0);
f6cbdf
+        pwritev2(fd, t, 2, 0, 0);
f6cbdf
+        pwritev2(fd, iov, -1, 0, 0);
f6cbdf
+        pwritev2(fd, iov, c, 0, 0);
f6cbdf
+        pwritev2(fd, iov, 2, -5, 0);
f6cbdf
+        pwritev2(-1, iov, 2, -5, 0);
f6cbdf
+        pwritev2(fd, iov, 2, -5, flag);
f6cbdf
+
f6cbdf
+        preadv2(fd, NULL, 2, 0, 0);
f6cbdf
+        preadv2(fd, z, 2, 0, 0);
f6cbdf
+        preadv2(fd, t, 2, 0, 0);
f6cbdf
+        preadv2(fd, iov, -1, 0, 0);
f6cbdf
+        preadv2(fd, iov, c, 0, 0);
f6cbdf
+        preadv2(fd, iov, 2, -5, 0);
f6cbdf
+        preadv2(-1, iov, 2, -5, 0);
f6cbdf
+
f6cbdf
+        iov[1].iov_base = (void *) -1;
f6cbdf
+        pwritev2(fd, iov, 2, 0, 0);
f6cbdf
+        preadv2(fd, iov, 2, 0, 0);
f6cbdf
+        free(mem);
f6cbdf
+    } while (0);
f6cbdf
+
f6cbdf
+    close(fd);
f6cbdf
+    unlink("prwv2_source");
f6cbdf
+    exit(EXIT_SUCCESS);
f6cbdf
+}
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv2_pwritev2.stderr.exp b/memcheck/tests/linux/sys-preadv2_pwritev2.stderr.exp
f6cbdf
new file mode 100644
f6cbdf
index 000000000..e11f2a51d
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv2_pwritev2.stderr.exp
f6cbdf
@@ -0,0 +1,56 @@
f6cbdf
+Syscall param pwritev2(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:53)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param pwritev2(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:54)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param pwritev2(vector) points to uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:55)
f6cbdf
+ Address 0x........ is 0 bytes inside a block of size 16 alloc'd
f6cbdf
+   at 0x........: malloc (vg_replace_malloc.c:...)
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:48)
f6cbdf
+
f6cbdf
+Syscall param pwritev2(count) contains uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:57)
f6cbdf
+
f6cbdf
+Syscall param pwritev2(flags) contains uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:60)
f6cbdf
+
f6cbdf
+Syscall param preadv2(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:62)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv2(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:63)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv2(vector) points to uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:64)
f6cbdf
+ Address 0x........ is 0 bytes inside a block of size 16 alloc'd
f6cbdf
+   at 0x........: malloc (vg_replace_malloc.c:...)
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:48)
f6cbdf
+
f6cbdf
+Syscall param preadv2(count) contains uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:66)
f6cbdf
+
f6cbdf
+Syscall param pwritev2(vector[1]) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:71)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv2(vector[1]) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv2_pwritev2.c:72)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv2_pwritev2.vgtest b/memcheck/tests/linux/sys-preadv2_pwritev2.vgtest
f6cbdf
new file mode 100644
f6cbdf
index 000000000..5cd23aacd
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv2_pwritev2.vgtest
f6cbdf
@@ -0,0 +1,3 @@
f6cbdf
+prereq: test -e sys-preadv2_pwritev2
f6cbdf
+prog: sys-preadv2_pwritev2
f6cbdf
+vgopts: -q
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv_pwritev.c b/memcheck/tests/linux/sys-preadv_pwritev.c
f6cbdf
new file mode 100644
f6cbdf
index 000000000..f5087dddc
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv_pwritev.c
f6cbdf
@@ -0,0 +1,77 @@
f6cbdf
+#define _GNU_SOURCE
f6cbdf
+#include <fcntl.h>
f6cbdf
+#include <stdio.h>
f6cbdf
+#include <stdlib.h>
f6cbdf
+#include <sys/stat.h>
f6cbdf
+#include <unistd.h>
f6cbdf
+#include <sys/syscall.h>
f6cbdf
+#include <sys/uio.h>
f6cbdf
+#include <string.h>
f6cbdf
+#include "../../memcheck.h"
f6cbdf
+
f6cbdf
+#include <errno.h>
f6cbdf
+
f6cbdf
+int main(int argc, char **argv)
f6cbdf
+{
f6cbdf
+    char str0[] = "hello ";
f6cbdf
+    char str1[] = "world\n";
f6cbdf
+    struct iovec iov[2];
f6cbdf
+    int fd;
f6cbdf
+
f6cbdf
+    fd = open("prwv_source", O_CREAT | O_RDWR, 0644);
f6cbdf
+    if (fd == -1) {
f6cbdf
+        perror("prwv2_source");
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    iov[0].iov_base = str0;
f6cbdf
+    iov[0].iov_len = strlen(str0);
f6cbdf
+    iov[1].iov_base = str1;
f6cbdf
+    iov[1].iov_len = strlen(str1);
f6cbdf
+
f6cbdf
+    /* Check pwritev and preadv called with the correct arguments works. */
f6cbdf
+    if (pwritev(fd, iov, 2, 0) == -1) {
f6cbdf
+        perror("pwritev");
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    if (preadv(fd, iov, 2, 0) == -1) {
f6cbdf
+        perror("preadv");
f6cbdf
+        printf("errno: %d\n", errno);
f6cbdf
+        exit(EXIT_FAILURE);
f6cbdf
+    }
f6cbdf
+
f6cbdf
+    /* Check valgrind will produce expected warnings for the
f6cbdf
+       various wrong arguments. */
f6cbdf
+    do {
f6cbdf
+        /* always allocate 16 bytes to not to have different .exps for different reg sizes */
f6cbdf
+        char *mem = malloc(16);
f6cbdf
+        void *t = (void *) &mem[0];
f6cbdf
+        void *z = (void *) -1;
f6cbdf
+        int c = *((int *) &mem[4]);
f6cbdf
+        pwritev(fd, NULL, 2, 0);
f6cbdf
+        pwritev(fd, z, 2, 0);
f6cbdf
+        pwritev(fd, t, 2, 0);
f6cbdf
+        pwritev(fd, iov, -1, 0);
f6cbdf
+        pwritev(fd, iov, c, 0);
f6cbdf
+        pwritev(fd, iov, 2, -5);
f6cbdf
+        pwritev(-1, iov, 2, -5);
f6cbdf
+
f6cbdf
+        preadv(fd, NULL, 2, 0);
f6cbdf
+        preadv(fd, z, 2, 0);
f6cbdf
+        preadv(fd, t, 2, 0);
f6cbdf
+        preadv(fd, iov, -1, 0);
f6cbdf
+        preadv(fd, iov, c, 0);
f6cbdf
+        preadv(fd, iov, 2, -5);
f6cbdf
+        preadv(-1, iov, 2, -5);
f6cbdf
+
f6cbdf
+        iov[1].iov_base = (void *) -1;
f6cbdf
+        pwritev(fd, iov, 2, 0);
f6cbdf
+        preadv(fd, iov, 2, 0);
f6cbdf
+        free(mem);
f6cbdf
+    } while (0);
f6cbdf
+
f6cbdf
+    close(fd);
f6cbdf
+    unlink("prwv_source");
f6cbdf
+    exit(EXIT_SUCCESS);
f6cbdf
+}
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv_pwritev.stderr.exp b/memcheck/tests/linux/sys-preadv_pwritev.stderr.exp
f6cbdf
new file mode 100644
f6cbdf
index 000000000..4fede44d8
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv_pwritev.stderr.exp
f6cbdf
@@ -0,0 +1,52 @@
f6cbdf
+Syscall param pwritev(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:52)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param pwritev(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:53)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param pwritev(vector) points to uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:54)
f6cbdf
+ Address 0x........ is 0 bytes inside a block of size 16 alloc'd
f6cbdf
+   at 0x........: malloc (vg_replace_malloc.c:...)
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:48)
f6cbdf
+
f6cbdf
+Syscall param pwritev(count) contains uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:56)
f6cbdf
+
f6cbdf
+Syscall param preadv(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:60)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv(vector) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:61)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv(vector) points to uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:62)
f6cbdf
+ Address 0x........ is 0 bytes inside a block of size 16 alloc'd
f6cbdf
+   at 0x........: malloc (vg_replace_malloc.c:...)
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:48)
f6cbdf
+
f6cbdf
+Syscall param preadv(count) contains uninitialised byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:64)
f6cbdf
+
f6cbdf
+Syscall param pwritev(vector[1]) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:69)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
+Syscall param preadv(vector[1]) points to unaddressable byte(s)
f6cbdf
+   ...
f6cbdf
+   by 0x........: main (sys-preadv_pwritev.c:70)
f6cbdf
+ Address 0x........ is not stack'd, malloc'd or (recently) free'd
f6cbdf
+
f6cbdf
diff --git a/memcheck/tests/linux/sys-preadv_pwritev.vgtest b/memcheck/tests/linux/sys-preadv_pwritev.vgtest
f6cbdf
new file mode 100644
f6cbdf
index 000000000..f07dc2935
f6cbdf
--- /dev/null
f6cbdf
+++ b/memcheck/tests/linux/sys-preadv_pwritev.vgtest
f6cbdf
@@ -0,0 +1,3 @@
f6cbdf
+prereq: test -e sys-preadv_pwritev
f6cbdf
+prog: sys-preadv_pwritev
f6cbdf
+vgopts: -q
f6cbdf
f6cbdf
commit 514f899388e05142513ff3f679a9e0131145e34e
f6cbdf
Author: Mark Wielaard <mark@klomp.org>
f6cbdf
Date:   Wed Jul 3 10:27:17 2019 +0200
f6cbdf
f6cbdf
    Hook up preadv2 and pwritev2 correctly for arm64.
f6cbdf
    
f6cbdf
    Use the correct generic linux sys wrapper.
f6cbdf
    
f6cbdf
    Followup for https://bugs.kde.org/408414
f6cbdf
f6cbdf
diff --git a/coregrind/m_syswrap/syswrap-arm64-linux.c b/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
index d12d40632..91329b682 100644
f6cbdf
--- a/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
+++ b/coregrind/m_syswrap/syswrap-arm64-linux.c
f6cbdf
@@ -818,8 +818,8 @@ static SyscallTableEntry syscall_main_table[] = {
f6cbdf
    LINX_(__NR_membarrier,        sys_membarrier),        // 283
f6cbdf
    //   (__NR_mlock2,            sys_ni_syscall),        // 284
f6cbdf
    LINX_(__NR_copy_file_range,   sys_copy_file_range),   // 285
f6cbdf
-   LINX_(__NR_preadv2,           sys_ni_syscall),        // 286
f6cbdf
-   LINX_(__NR_pwritev2,          sys_ni_syscall),        // 287
f6cbdf
+   LINXY(__NR_preadv2,           sys_preadv2),           // 286
f6cbdf
+   LINX_(__NR_pwritev2,          sys_pwritev2),          // 287
f6cbdf
    //   (__NR_pkey_mprotect,     sys_ni_syscall),        // 288
f6cbdf
    //   (__NR_pkey_alloc,        sys_ni_syscall),        // 289
f6cbdf
    //   (__NR_pkey_free,         sys_ni_syscall),        // 290