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