Rémi Bernon : ntdll: Split valgrind memory access control to separate helpers.

Alexandre Julliard julliard at winehq.org
Tue May 31 15:57:59 CDT 2022


Module: wine
Branch: master
Commit: e4e8cabf2f71f2e5a6d80101060ed7a9022a00ae
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=e4e8cabf2f71f2e5a6d80101060ed7a9022a00ae

Author: Rémi Bernon <rbernon at codeweavers.com>
Date:   Mon May 30 20:44:59 2022 +0200

ntdll: Split valgrind memory access control to separate helpers.

Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>

---

 dlls/ntdll/heap.c | 62 +++++++++++++++++++++++++++----------------------------
 1 file changed, 30 insertions(+), 32 deletions(-)

diff --git a/dlls/ntdll/heap.c b/dlls/ntdll/heap.c
index 7697b8f0354..f9cdb6d8c26 100644
--- a/dlls/ntdll/heap.c
+++ b/dlls/ntdll/heap.c
@@ -307,54 +307,56 @@ static inline BOOL check_subheap( const SUBHEAP *subheap )
 
 static BOOL heap_validate( const HEAP *heap );
 
-/* mark a block of memory as free for debugging purposes */
-static inline void mark_block_free( void *ptr, SIZE_T size, DWORD flags )
+/* mark a block of memory as innacessible for debugging purposes */
+static inline void valgrind_make_noaccess( void const *ptr, SIZE_T size )
 {
-    if (flags & HEAP_FREE_CHECKING_ENABLED)
-    {
-        SIZE_T i;
-        for (i = 0; i < size / sizeof(DWORD); i++) ((DWORD *)ptr)[i] = ARENA_FREE_FILLER;
-    }
 #if defined(VALGRIND_MAKE_MEM_NOACCESS)
-    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr, size ));
-#elif defined( VALGRIND_MAKE_NOACCESS)
-    VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
+    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr, size ) );
+#elif defined(VALGRIND_MAKE_NOACCESS)
+    VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ) );
 #endif
 }
 
 /* mark a block of memory as initialized for debugging purposes */
-static inline void mark_block_initialized( void *ptr, SIZE_T size )
+static inline void valgrind_make_readable( void const *ptr, SIZE_T size )
 {
 #if defined(VALGRIND_MAKE_MEM_DEFINED)
-    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_DEFINED( ptr, size ));
+    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_DEFINED( ptr, size ) );
 #elif defined(VALGRIND_MAKE_READABLE)
-    VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ));
+    VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ) );
 #endif
 }
 
 /* mark a block of memory as uninitialized for debugging purposes */
-static inline void mark_block_uninitialized( void *ptr, SIZE_T size )
+static inline void valgrind_make_writable( void const *ptr, SIZE_T size )
 {
 #if defined(VALGRIND_MAKE_MEM_UNDEFINED)
-    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr, size ));
+    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr, size ) );
 #elif defined(VALGRIND_MAKE_WRITABLE)
-    VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
+    VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ) );
 #endif
 }
 
+/* mark a block of memory as free for debugging purposes */
+static inline void mark_block_free( void *ptr, SIZE_T size, DWORD flags )
+{
+    if (flags & HEAP_FREE_CHECKING_ENABLED)
+    {
+        SIZE_T i;
+        for (i = 0; i < size / sizeof(DWORD); i++) ((DWORD *)ptr)[i] = ARENA_FREE_FILLER;
+    }
+    valgrind_make_noaccess( ptr, size );
+}
+
 /* mark a block of memory as a tail block */
 static inline void mark_block_tail( void *ptr, SIZE_T size, DWORD flags )
 {
     if (flags & HEAP_TAIL_CHECKING_ENABLED)
     {
-        mark_block_uninitialized( ptr, size );
+        valgrind_make_writable( ptr, size );
         memset( ptr, ARENA_TAIL_FILLER, size );
     }
-#if defined(VALGRIND_MAKE_MEM_NOACCESS)
-    VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr, size ));
-#elif defined( VALGRIND_MAKE_NOACCESS)
-    VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
-#endif
+    valgrind_make_noaccess( ptr, size );
 }
 
 /* initialize contents of a newly created block of memory */
@@ -362,17 +364,13 @@ static inline void initialize_block( void *ptr, SIZE_T size, SIZE_T unused, DWOR
 {
     if (flags & HEAP_ZERO_MEMORY)
     {
-        mark_block_initialized( ptr, size );
+        valgrind_make_writable( ptr, size );
         memset( ptr, 0, size );
     }
-    else
+    else if (flags & HEAP_FREE_CHECKING_ENABLED)
     {
-        mark_block_uninitialized( ptr, size );
-        if (flags & HEAP_FREE_CHECKING_ENABLED)
-        {
-            memset( ptr, ARENA_INUSE_FILLER, size );
-            mark_block_uninitialized( ptr, size );
-        }
+        valgrind_make_writable( ptr, size );
+        memset( ptr, ARENA_INUSE_FILLER, size );
     }
 
     mark_block_tail( (char *)ptr + size, unused, flags );
@@ -679,7 +677,7 @@ static void create_free_block( SUBHEAP *subheap, struct block *block, SIZE_T blo
     DWORD flags = heap->flags;
     struct block *next;
 
-    mark_block_uninitialized( block, sizeof(*entry) );
+    valgrind_make_writable( block, sizeof(*entry) );
     block_set_type( block, ARENA_FREE_MAGIC );
     block_set_size( block, BLOCK_FLAG_FREE, block_size );
 
@@ -702,7 +700,7 @@ static void create_free_block( SUBHEAP *subheap, struct block *block, SIZE_T blo
     {
         /* set the next block PREV_FREE flag and back pointer */
         block_set_size( next, BLOCK_FLAG_PREV_FREE, block_get_size( next ) );
-        mark_block_initialized( (struct block **)next - 1, sizeof(struct block *) );
+        valgrind_make_writable( (struct block **)next - 1, sizeof(struct block *) );
         *((struct block **)next - 1) = block;
     }
 




More information about the wine-cvs mailing list