Hi ports,

The Valgrind specifies 'READ | WRITE | EXEC' when allocating memory
for target program as follows:

    int fd = open("a.out", O_RDONLY);
    void *addr = mmap(..., prot = PROT_READ | PROT_WRITE | PROT_EXEC, ...);
    read(fd, addr, LENGTH);
    /* Execute target program */

I modified to specify 'READ | EXEC' after readind the target
program as folloes:

    int fd = open("a.out", O_RDONLY);
    void *addr = mmap(..., prot = PROT_READ | PROT_WRITE, ...);
    read(fd, addr, LENGTH);
    mmap(addr, LENGTH, prot = PROT_READ | PROT_EXEC, ...);
    /* Execute target program */

Therefor, the target program area can not be rewritten.

How about it?
--
ASOU Masato

Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/valgrind/Makefile,v
retrieving revision 1.21
diff -u -p -r1.21 Makefile
--- Makefile    23 Oct 2019 02:35:05 -0000      1.21
+++ Makefile    25 Oct 2019 04:23:10 -0000
@@ -1,7 +1,5 @@
 # $OpenBSD: Makefile,v 1.21 2019/10/23 02:35:05 asou Exp $
 
-USE_WXNEEDED =         Yes
-
 ONLY_FOR_ARCHS =       amd64
 
 COMMENT =              instrumentation framework for dynamic analysis tools
Index: patches/patch-coregrind_link_tool_exe_openbsd_in
===================================================================
RCS file: 
/cvs/ports/devel/valgrind/patches/patch-coregrind_link_tool_exe_openbsd_in,v
retrieving revision 1.3
diff -u -p -r1.3 patch-coregrind_link_tool_exe_openbsd_in
--- patches/patch-coregrind_link_tool_exe_openbsd_in    2 Oct 2019 17:19:29 
-0000       1.3
+++ patches/patch-coregrind_link_tool_exe_openbsd_in    25 Oct 2019 04:23:10 
-0000
@@ -5,12 +5,12 @@
  system(sprintf "sed -e 's|%x|%x|g' < $ldscript > $temp", $origbase, 
$notebase);
  
 -my $cmd = sprintf "$cc -static -nopie -Wl,-Ttext=0x%x -Wl,-T,$temp", 
$textbase;
-+my $cmd = sprintf "$cc -static -nopie -Wl,--strip-all -Wl,-zwxneeded 
-Wl,-Ttext=0x%x -Wl,-T,$temp", $textbase;
 +# XXX The '-s' option was not specified when executing the install command.
 +# Instead '--strip-all' is now executed at link time.
 +# strip command rewrite offset and align in ELF file. Therefor, when valgrind
 +# launch memcheck-amd64-openbsd, an Abort trap occurs in the execvp() system
 +# call.
++my $cmd = sprintf "$cc -static -nopie -Wl,--strip-all -Wl,-Ttext=0x%x 
-Wl,-T,$temp", $textbase;
  
  # Add the rest of the parameters
  foreach my $n (2 .. $#ARGV) {
Index: patches/patch-coregrind_m_aspacemgr_aspacemgr-linux_c
===================================================================
RCS file: 
/cvs/ports/devel/valgrind/patches/patch-coregrind_m_aspacemgr_aspacemgr-linux_c,v
retrieving revision 1.1
diff -u -p -r1.1 patch-coregrind_m_aspacemgr_aspacemgr-linux_c
--- patches/patch-coregrind_m_aspacemgr_aspacemgr-linux_c       2 Oct 2019 
17:19:29 -0000       1.1
+++ patches/patch-coregrind_m_aspacemgr_aspacemgr-linux_c       25 Oct 2019 
04:23:10 -0000
@@ -1,5 +1,17 @@
 --- coregrind/m_aspacemgr/aspacemgr-linux.c.orig
 +++ coregrind/m_aspacemgr/aspacemgr-linux.c
+@@ -2498,7 +2498,11 @@
+       a client request to call the outer VG_(am_get_advisory). */
+    sres = VG_(am_do_mmap_NO_NOTIFY)( 
+              advised, length, 
++#if defined(__OpenBSD__)
++             VKI_PROT_READ|VKI_PROT_WRITE,
++#else
+              VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, 
++#endif
+              VKI_MAP_FIXED|VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS, 
+              VM_TAG_VALGRIND, 0
+           );
 @@ -2543,6 +2543,54 @@ SysRes VG_(am_mmap_anon_float_valgrind)( SizeT length )
     return sres;
  }
@@ -30,7 +42,7 @@
 +      any resulting failure immediately. */
 +   sres = VG_(am_do_mmap_NO_NOTIFY)(
 +             advised, length,
-+             VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC,
++             VKI_PROT_READ|VKI_PROT_WRITE,
 +             VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS|VKI_MAP_STACK,
 +             VM_TAG_VALGRIND, 0
 +          );
Index: patches/patch-coregrind_m_initimg_initimg-openbsd_c
===================================================================
RCS file: 
/cvs/ports/devel/valgrind/patches/patch-coregrind_m_initimg_initimg-openbsd_c,v
retrieving revision 1.1
diff -u -p -r1.1 patch-coregrind_m_initimg_initimg-openbsd_c
--- patches/patch-coregrind_m_initimg_initimg-openbsd_c 4 Feb 2019 23:34:10 
-0000       1.1
+++ patches/patch-coregrind_m_initimg_initimg-openbsd_c 25 Oct 2019 04:23:10 
-0000
@@ -12,3 +12,21 @@ Index: coregrind/m_initimg/initimg-openb
  /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
  
  
+@@ -575,7 +575,7 @@
+         res = VG_(am_mmap_anon_fixed_client)(
+                  anon_start -inner_HACK,
+                  anon_size +inner_HACK,
+-               VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC
++               VKI_PROT_READ|VKI_PROT_WRITE
+             );
+      }
+      if ((!ok) || sr_isError(res)) {
+@@ -758,7 +758,7 @@
+    sres = VG_(am_mmap_anon_fixed_client)( 
+              anon_start, 
+              anon_size, 
+-             VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC
++             VKI_PROT_READ|VKI_PROT_WRITE
+           );
+    vg_assert(!sr_isError(sres));
+    vg_assert(sr_Res(sres) == anon_start);
Index: patches/patch-coregrind_m_mallocfree_c
===================================================================
RCS file: patches/patch-coregrind_m_mallocfree_c
diff -N patches/patch-coregrind_m_mallocfree_c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-coregrind_m_mallocfree_c      25 Oct 2019 04:23:10 -0000
@@ -0,0 +1,14 @@
+--- coregrind/m_mallocfree.c.orig
++++ coregrind/m_mallocfree.c
+@@ -854,7 +854,11 @@
+    if (a->clientmem) {
+       // client allocation -- return 0 to client if it fails
+       sres = VG_(am_mmap_anon_float_client)
++#if defined(__OpenBSD__)
++         ( cszB, VKI_PROT_READ|VKI_PROT_WRITE );
++#else
+          ( cszB, VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC );
++#endif
+       if (sr_isError(sres))
+          return 0;
+       sb = (Superblock*)(AddrH)sr_Res(sres);
Index: patches/patch-coregrind_m_scheduler_scheduler_c
===================================================================
RCS file: 
/cvs/ports/devel/valgrind/patches/patch-coregrind_m_scheduler_scheduler_c,v
retrieving revision 1.1
diff -u -p -r1.1 patch-coregrind_m_scheduler_scheduler_c
--- patches/patch-coregrind_m_scheduler_scheduler_c     2 Oct 2019 17:19:29 
-0000       1.1
+++ patches/patch-coregrind_m_scheduler_scheduler_c     25 Oct 2019 04:23:10 
-0000
@@ -1,74 +0,0 @@
---- coregrind/m_scheduler/scheduler.c.orig
-+++ coregrind/m_scheduler/scheduler.c
-@@ -854,6 +854,9 @@ void run_thread_for_a_while ( /*OUT*/HWord* two_words,
-    volatile ThreadState* tst            = NULL; /* stop gcc complaining */
-    volatile Int          done_this_time = 0;
-    volatile HWord        host_code_addr = 0;
-+#if defined(__OpenBSD__)
-+   volatile UInt         host_code_len  = 0;
-+#endif
-
-    /* Paranoia */
-    vg_assert(VG_(is_valid_tid)(tid));
-@@ -879,8 +882,15 @@ void run_thread_for_a_while ( /*OUT*/HWord* two_words,
-    } else {
-       /* normal case -- redir translation */
-       UInt cno = (UInt)VG_TT_FAST_HASH((Addr)tst->arch.vex.VG_INSTR_PTR);
-+#if defined(__OpenBSD__)
-+      if (LIKELY(VG_(tt_fast)[cno].guest == 
(Addr)tst->arch.vex.VG_INSTR_PTR)) {
-+         host_code_addr = VG_(tt_fast)[cno].host;
-+         host_code_len  = VG_(tt_fast)[cno].len;
-+      }
-+#else
-       if (LIKELY(VG_(tt_fast)[cno].guest == (Addr)tst->arch.vex.VG_INSTR_PTR))
-          host_code_addr = VG_(tt_fast)[cno].host;
-+#endif
-       else {
-          AddrH res   = 0;
-          /* not found in VG_(tt_fast). Searching here the transtab
-@@ -932,6 +942,22 @@ void run_thread_for_a_while ( /*OUT*/HWord* two_words,
-    vg_assert(VG_(in_generated_code) == False);
-    VG_(in_generated_code) = True;
-
-+#if defined(__OpenBSD__)
-+   if (host_code_len > 0) {
-+      SysRes  sres;
-+
-+      /* Protect the guard areas. */
-+      sres = VG_(am_do_mprotect_NO_NOTIFY)(
-+         host_code_addr, host_code_len,
-+         VKI_PROT_READ | VKI_PROT_EXEC
-+      );
-+      if (sr_isError(sres)) {
-+         VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
-+         vg_assert(0);
-+      }
-+   }
-+#endif
-+
-    SCHEDSETJMP(
-       tid,
-       jumped,
-@@ -942,6 +968,22 @@ void run_thread_for_a_while ( /*OUT*/HWord* two_words,
-       )
-    );
-
-+#if defined(__OpenBSD__)
-+   if (host_code_len > 0) {
-+      SysRes  sres;
-+
-+      /* Protect the guard areas. */
-+      sres = VG_(am_do_mprotect_NO_NOTIFY)(
-+         host_code_addr, host_code_len,
-+         VKI_PROT_READ | VKI_PROT_WRITE | VKI_PROT_EXEC
-+      );
-+      if (sr_isError(sres)) {
-+         VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
-+         vg_assert(0);
-+      }
-+   }
-+#endif
-+
-    vg_assert(VG_(in_generated_code) == True);
-    VG_(in_generated_code) = False;
-
Index: patches/patch-coregrind_m_transtab_c
===================================================================
RCS file: /cvs/ports/devel/valgrind/patches/patch-coregrind_m_transtab_c,v
retrieving revision 1.1
diff -u -p -r1.1 patch-coregrind_m_transtab_c
--- patches/patch-coregrind_m_transtab_c        2 Oct 2019 17:19:29 -0000       
1.1
+++ patches/patch-coregrind_m_transtab_c        25 Oct 2019 04:23:10 -0000
@@ -1,60 +1,95 @@
 --- coregrind/m_transtab.c.orig        Wed Nov 26 04:41:21 2014
 +++ coregrind/m_transtab.c     Mon Dec 10 17:05:02 2018
-@@ -1322,11 +1321,18 @@
-    return k32 % N_TTES_PER_SECTOR;
- }
+@@ -749,6 +749,10 @@
+    /* Get the CPU info established at startup. */
+    VexArch     arch_host = VexArch_INVALID;
+    VexArchInfo archinfo_host;
++#if defined(__OpenBSD__)
++   UInt        len;
++   SysRes      sres;
++#endif
+    VG_(bzero_inline)(&archinfo_host, sizeof(archinfo_host));
+    VG_(machine_get_VexArchInfo)( &arch_host, &archinfo_host );
+    VexEndness endness_host = archinfo_host.endness;
+@@ -789,6 +793,18 @@
+
+    TTEntry* from_tte = index_tte(from_sNo, from_tteNo);
 
 +#if defined(__OpenBSD__)
-+static void setFastCacheEntry ( Addr64 key, ULong* tcptr, UInt len )
-+#else
- static void setFastCacheEntry ( Addr64 key, ULong* tcptr )
-+#endif
- {
-    UInt cno = (UInt)VG_TT_FAST_HASH(key);
-    VG_(tt_fast)[cno].guest = (Addr)key;
-    VG_(tt_fast)[cno].host  = (Addr)tcptr;
-+#if defined(__OpenBSD__)
-+   VG_(tt_fast)[cno].len   = (Addr)len;
-+#endif
-    n_fast_updates++;
-    /* This shouldn't fail.  It should be assured by m_translate
-       which should reject any attempt to make translation of code
-@@ -1670,7 +1676,11 @@
-    }
++   /* Protect the host code areas. */
++   len = (Long)sectors[to_sNo].tc_next - (Long)host_code;
++   sres = VG_(am_do_mprotect_NO_NOTIFY)(
++      (Addr)sectors[to_sNo].tc, 8 * tc_sector_szQ, VKI_PROT_READ | 
VKI_PROT_WRITE
++   );
++   if (sr_isError(sres)) {
++      VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
++      vg_assert(0);
++   }
++#endif
++
+    /* Get VEX to do the patching itself.  We have to hand it off
+       since it is host-dependent. */
+    VexInvalRange vir
+@@ -802,6 +818,17 @@
+         );
+    VG_(invalidate_icache)( (void*)vir.start, vir.len );
 
-    /* Update the fast-cache. */
 +#if defined(__OpenBSD__)
-+   setFastCacheEntry( entry, tcptr, code_len );
-+#else
-    setFastCacheEntry( entry, tcptr );
++   /* Protect the host code areas. */
++   sres = VG_(am_do_mprotect_NO_NOTIFY)(
++      (Addr)sectors[to_sNo].tc, 8 * tc_sector_szQ, VKI_PROT_READ | 
VKI_PROT_EXEC
++   );
++   if (sr_isError(sres)) {
++      VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
++      vg_assert(0);
++   }
++#endif
++
+    /* Now do the tricky bit -- update the ch_succs and ch_preds info
+       for the two translations involved, so we can undo the chaining
+       later, which we will have to do if the to_ block gets removed
+@@ -1530,6 +1550,9 @@
+    ULong  *tcptr, *tcptr2;
+    UChar* srcP;
+    UChar* dstP;
++#if defined(__OpenBSD__)
++   SysRes sres;
 +#endif
 
-    /* Note the eclass numbers for this translation. */
-    upd_eclasses_after_add( &sectors[y], i );
-@@ -1712,8 +1722,13 @@
-              && sectors[sno].tt[k].entry == guest_addr) {
-             /* found it */
-             if (upd_cache)
-+#if defined(__OpenBSD__)
-+               setFastCacheEntry(
-+                  guest_addr, sectors[sno].tt[k].tcptr, 0 );
-+#else
-                setFastCacheEntry( 
-                   guest_addr, sectors[sno].tt[k].tcptr );
-+#endif
-             if (res_hcode)
-                *res_hcode = (AddrH)sectors[sno].tt[k].tcptr;
-             if (res_sNo)
-@@ -2204,7 +2219,12 @@
-    vg_assert(sizeof(Addr64) == 8);
-    /* check fast cache entries really are 2 words long */
-    vg_assert(sizeof(Addr) == sizeof(void*));
-+#if defined(__OpenBSD__)
-+   vg_assert(sizeof(FastCacheEntry) ==
-+      (2 * sizeof(Addr) + (2 * sizeof (UInt))));
-+#else
-    vg_assert(sizeof(FastCacheEntry) == 2 * sizeof(Addr));
-+#endif
-    /* check fast cache entries are packed back-to-back with no spaces */
-    vg_assert(sizeof( VG_(tt_fast) ) == VG_TT_FAST_SIZE * 
sizeof(FastCacheEntry));
-    /* check fast cache is aligned as we requested.  Not fatal if it
+    vg_assert(init_done);
+    vg_assert(vge->n_used >= 1 && vge->n_used <= 3);
+@@ -1629,12 +1629,34 @@
+    tcptr = sectors[y].tc_next;
+    vg_assert(tcptr >= &sectors[y].tc[0]);
+    vg_assert(tcptr <= &sectors[y].tc[tc_sector_szQ]);
++ 
++#if defined(__OpenBSD__)
++   /* Protect the host code areas. */
++   sres = VG_(am_do_mprotect_NO_NOTIFY)(
++      (Addr)sectors[y].tc, 8 * tc_sector_szQ, VKI_PROT_READ | VKI_PROT_WRITE
++   );
++   if (sr_isError(sres)) {
++      VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
++      vg_assert(0);
++   }
++#endif
+ 
+    dstP = (UChar*)tcptr;
+    srcP = (UChar*)code;
+    VG_(memcpy)(dstP, srcP, code_len);
+    sectors[y].tc_next += reqdQ;
+    sectors[y].tt_n_inuse++;
++ 
++#if defined(__OpenBSD__)
++   /* Protect the host code areas. */
++   sres = VG_(am_do_mprotect_NO_NOTIFY)(
++      (Addr)sectors[y].tc, 8 * tc_sector_szQ, VKI_PROT_READ | VKI_PROT_EXEC
++   );
++   if (sr_isError(sres)) {
++      VG_(printf)("valgrind: m_ume.c: mprotect failed\n");
++      vg_assert(0);
++   }
++#endif
+ 
+    /* more paranoia */
+    tcptr2 = sectors[y].tc_next;
Index: patches/patch-coregrind_pub_core_transtab_h
===================================================================
RCS file: 
/cvs/ports/devel/valgrind/patches/patch-coregrind_pub_core_transtab_h,v
retrieving revision 1.1
diff -u -p -r1.1 patch-coregrind_pub_core_transtab_h
--- patches/patch-coregrind_pub_core_transtab_h 2 Oct 2019 17:19:29 -0000       
1.1
+++ patches/patch-coregrind_pub_core_transtab_h 25 Oct 2019 04:23:10 -0000
@@ -1,12 +0,0 @@
---- coregrind/pub_core_transtab.h.orig
-+++ coregrind/pub_core_transtab.h
-@@ -45,6 +45,9 @@ typedef
-    struct {
-       Addr guest;
-       Addr host;
-+#if defined(__OpenBSD__)
-+      UInt len;
-+#endif
-    }
-    FastCacheEntry;
-

Reply via email to