]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Modularised the malloc/free stuff into two modules: m_mallocfree for the
authorNicholas Nethercote <njn@valgrind.org>
Tue, 10 May 2005 02:47:21 +0000 (02:47 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Tue, 10 May 2005 02:47:21 +0000 (02:47 +0000)
malloc/free implementation, and m_replacemalloc with the stuff for the tools
that replace malloc with their own version.  Previously these two areas of
functionality were mixed up somewhat.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3648

28 files changed:
Makefile.tool.am
addrcheck/Makefile.am
cachegrind/cg_main.c
configure.in
coregrind/Makefile.am
coregrind/README_MODULES.txt
coregrind/core.h
coregrind/m_mallocfree.c [moved from coregrind/vg_malloc2.c with 89% similarity]
coregrind/m_replacemalloc/Makefile.am [new file with mode: 0644]
coregrind/m_replacemalloc/README_REPLACEMALLOC.txt [new file with mode: 0644]
coregrind/m_replacemalloc/replacemalloc_core.c [new file with mode: 0644]
coregrind/m_replacemalloc/vg_replace_malloc.c [moved from coregrind/vg_replace_malloc.c with 99% similarity]
coregrind/pub_core_mallocfree.h [new file with mode: 0644]
coregrind/pub_core_replacemalloc.h [new file with mode: 0644]
coregrind/vg_pthreadmodel.c
coregrind/vg_scheduler.c
helgrind/Makefile.am
helgrind/hg_main.c
include/Makefile.am
include/pub_tool_mallocfree.h [new file with mode: 0644]
include/pub_tool_replacemalloc.h [new file with mode: 0644]
include/tool.h
massif/Makefile.am
massif/ms_main.c
memcheck/Makefile.am
memcheck/mac_shared.c
memcheck/mac_shared.h
memcheck/mc_main.c

index 39aa6a15af1faa7d01d0591fe72c6ccc38e48964..f835685066c97b4c5e1dcbe2efecf42384a34889 100644 (file)
@@ -4,3 +4,6 @@ SUBDIRS = . tests docs
 include $(top_srcdir)/Makefile.all.am
 include $(top_srcdir)/Makefile.tool-flags.am
 include $(top_srcdir)/Makefile.tool-inplace.am
+
+LIBREPLACEMALLOC = $(top_builddir)/coregrind/m_replacemalloc/libreplacemalloc_toolpreload.a
+
index e49f5faac147fa340fafa4269a2ef8a3a8b82567..88c53f81a030d647d418ea5a57f5828ae3c2e4c1 100644 (file)
@@ -16,10 +16,10 @@ vgpreload_addrcheck_so_SOURCES =
 vgpreload_addrcheck_so_LDADD = \
        ../memcheck/mac_replace_strmem.o
 vgpreload_addrcheck_so_DEPENDENCIES = \
-       $(top_builddir)/coregrind/lib_replace_malloc.a \
+       $(LIBREPLACEMALLOC) \
        ../memcheck/mac_replace_strmem.o
 vgpreload_addrcheck_so_LDFLAGS = -shared -Wl,-z,interpose,-z,initfirst \
        -Wl,--whole-archive \
-       $(top_builddir)/coregrind/lib_replace_malloc.a \
+       $(LIBREPLACEMALLOC) \
        -Wl,--no-whole-archive
 
index 2b36d43cad005a539340f73968fda3be0be8dba7..02fccc1bb876f27d7731b6e72eef165e1fb6bfa1 100644 (file)
@@ -30,7 +30,7 @@
 */
 
 #include "tool.h"
-//#include "vg_profile.c"
+#include "pub_tool_mallocfree.h"
 
 #include "cg_arch.h"
 #include "cg_sim.c"
index 9c36557396ff84a810d6a38444a90c707ec0edaf..897693572645bb7acbde22b1a643f0b3f90c691b 100644 (file)
@@ -395,6 +395,7 @@ AC_OUTPUT(
    coregrind/Makefile 
    coregrind/demangle/Makefile 
    coregrind/m_aspacemgr/Makefile 
+   coregrind/m_replacemalloc/Makefile 
    coregrind/m_sigframe/Makefile 
    coregrind/m_syscalls/Makefile 
    coregrind/amd64/Makefile
index 5d4f7534c9d4381a5665466e1b7cc93cadd0f287..4a9ddc9937fa8b20f63b79b9e2648b48c73245e9 100644 (file)
@@ -4,6 +4,7 @@ include $(top_srcdir)/Makefile.core-AM_CPPFLAGS.am
 MODULES = \
        demangle \
        m_aspacemgr \
+       m_replacemalloc \
        m_sigframe \
        m_syscalls
 
@@ -33,8 +34,6 @@ val_PROGRAMS = \
        stage2 \
        vg_inject.so
 
-noinst_LIBRARIES = lib_replace_malloc.a
-
 noinst_HEADERS = \
        core.h                  \
        core_asm.h              \
@@ -42,6 +41,8 @@ noinst_HEADERS = \
        pub_core_debuglog.h     \
        pub_core_errormgr.h     \
        pub_core_execontext.h   \
+       pub_core_mallocfree.h   \
+       pub_core_replacemalloc.h\
        pub_core_sigframe.h     \
        pub_core_stacktrace.h   \
        pub_core_syscalls.h     \
@@ -66,6 +67,7 @@ valgrind_LDADD=
 stage2_SOURCES = \
        m_errormgr.c \
        m_execontext.c \
+       m_mallocfree.c \
        m_stacktrace.c \
        m_debuglog.c \
        ume.c \
@@ -73,9 +75,7 @@ stage2_SOURCES = \
        vg_scheduler.c \
        vg_demangle.c \
        vg_hashtable.c \
-       vg_replace_malloc.c \
        vg_main.c \
-       vg_malloc2.c \
        vg_messages.c \
        vg_mylibc.c \
        vg_needs.c \
@@ -103,28 +103,41 @@ stage2_extra= \
        ${VG_OS}/libos.a \
        @VEX_DIR@/libvex.a
 
-## Test repeated in both arms of the if-then-else because older versions of
-## automake don't seem to like having += within an if-then-else.
+## These ones must be linked in with the --whole-archive flag, because they
+## wouldn't get pulled into stage otherwise (because they contain symbols
+## only referred to by tool shared objects).
+stage2_extra2 = \
+       m_replacemalloc/libreplacemalloc_core.a
+       
+## Nb: older versions of automake don't seem to like having += within an
+## if-then-else, so we have to use these variables for the common parts.
+st2_DEPS_common = \
+       $(srcdir)/valgrind.vs \
+       $(stage2_extra) \
+       $(stage2_extra2)
+
+st2_LDFLAGS_common = \
+       -Wl,--export-dynamic -g \
+       -Wl,-version-script $(srcdir)/valgrind.vs
+
 if USE_PIE
 stage2_CFLAGS = $(AM_CFLAGS) -fpie
-stage2_DEPENDENCIES = \
-       $(srcdir)/valgrind.vs \
-       $(stage2_extra)
-stage2_LDFLAGS = -Wl,--export-dynamic -g \
-       -Wl,-version-script $(srcdir)/valgrind.vs \
+stage2_DEPENDENCIES = $(st2_DEPS_common)
+stage2_LDFLAGS = \
+       $(st2_LDFLAGS_common) \
+       -Wl,--whole-archive $(stage2_extra2) -Wl,--no-whole-archive \
        -pie
 else
 stage2_CFLAGS = $(AM_CFLAGS)
-stage2_DEPENDENCIES = \
-       $(srcdir)/valgrind.vs ${VG_ARCH}/stage2.lds \
-       $(stage2_extra)
-stage2_LDFLAGS = -Wl,--export-dynamic -g \
-       -Wl,-version-script $(srcdir)/valgrind.vs \
+stage2_DEPENDENCIES = $(st2_DEPS_common) ${VG_ARCH}/stage2.lds
+stage2_LDFLAGS = \
+       $(st2_LDFLAGS_common) \
+       -Wl,--whole-archive $(stage2_extra2) -Wl,--no-whole-archive \
        -Wl,-defsym,kickstart_base=@KICKSTART_BASE@ -Wl,-T,${VG_ARCH}/stage2.lds
 endif
 
-stage2_LDADD= $(stage2_extra) -ldl
-
+stage2_LDADD= $(stage2_extra) \
+       -ldl
 
 vg_inject_so_SOURCES = vg_intercept.c
 vg_inject_so_CFLAGS = $(AM_CFLAGS) -fpic
@@ -134,9 +147,6 @@ vg_inject_so_LDFLAGS = \
        -Wl,--soname,vg_inject.so \
        -Wl,-z,initfirst
 
-lib_replace_malloc_a_SOURCES = vg_replace_malloc.c
-lib_replace_malloc_a_CFLAGS  = $(AM_CFLAGS) -fpic -fno-omit-frame-pointer
-
 MANUAL_DEPS = $(noinst_HEADERS) $(include_HEADERS)
 
 all-local:
index 9c6ee01993bf3936872b7f4740b79608b3310b6b..4f66de242c4a5d9c29ac5ec76acef7beff8e709b 100644 (file)
@@ -43,6 +43,8 @@ functionality.  Consequently, pub_core_foo.h *must* #include
 pub_tool_foo.h, if it exists.  pub_tool_foo.h *must not* #include
 pub_core_foo.h, nor any other pub_core_ header for that matter.
 
+Module-private headers are named "priv_foo.h".
+
 No module may include the private headers of any other module.  If a
 type/enum/function/struct/whatever is stated in neither
 include/pub_tool_foo.h nor coregrind/pub_core_foo.h then module 'foo'
index f52bf5774d287ef67dfb3d149ec9734d6a5f22c6..8c2aece47f8f61d43909fb5f952709679b44902d 100644 (file)
@@ -1,7 +1,6 @@
 
 /*--------------------------------------------------------------------*/
-/*--- A header file for all private parts of Valgrind's core.      ---*/
-/*--- Include no other! (more or less...)                          ---*/
+/*--- A header file for various private parts of Valgrind's core.  ---*/
 /*---                                                       core.h ---*/
 /*--------------------------------------------------------------------*/
 
@@ -97,6 +96,7 @@ typedef struct _ThreadState ThreadState;
                            //   eg. x86-linux/core_platform.h
 #include "core_os.h"       // OS-specific stuff,    eg. linux/core_os.h
 
+#include "pub_core_mallocfree.h"  // for type 'ArenaId'
 #include "pub_core_stacktrace.h"  // for type 'StackTrace'
 
 #include "valgrind.h"
@@ -457,57 +457,6 @@ extern VgToolInterface VG_(tdict);
 void VG_(sanity_check_needs)(void);
 
 
-/* ---------------------------------------------------------------------
-   Exports of vg_malloc2.c
-   ------------------------------------------------------------------ */
-
-/* Allocation arenas.  
-
-      CORE      for the core's general use.
-      TOOL      for the tool to use (and the only one it uses).
-      SYMTAB    for Valgrind's symbol table storage.
-      CLIENT    for the client's mallocs/frees, if the tool replaces glibc's
-                    malloc() et al -- redzone size is chosen by the tool.
-      DEMANGLE  for the C++ demangler.
-      EXECTXT   for storing ExeContexts.
-      ERRORS    for storing CoreErrors.
-
-   When adding a new arena, remember also to add it to ensure_mm_init(). 
-*/
-typedef Int ArenaId;
-
-#define VG_N_ARENAS        7
-
-#define VG_AR_CORE         0
-#define VG_AR_TOOL         1
-#define VG_AR_SYMTAB       2
-#define VG_AR_CLIENT       3
-#define VG_AR_DEMANGLE     4
-#define VG_AR_EXECTXT      5
-#define VG_AR_ERRORS       6
-
-// This is both the minimum payload size of a malloc'd block, and its
-// minimum alignment.  Must be a power of 2 greater than 4, and should be
-// greater than 8.
-#define VG_MIN_MALLOC_SZB        8
-
-extern void* VG_(arena_malloc)  ( ArenaId arena, SizeT nbytes );
-extern void  VG_(arena_free)    ( ArenaId arena, void* ptr );
-extern void* VG_(arena_calloc)  ( ArenaId arena, 
-                                  SizeT nmemb, SizeT bytes_per_memb );
-extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, SizeT size );
-
-/* Sets the size of the redzones at the start and end of heap blocks.  This
-   must be called before any of VG_(malloc) and friends are called. */
-extern void  VG_(set_client_malloc_redzone_szB) ( SizeT rz_szB );
-
-extern SizeT VG_(arena_payload_szB) ( ArenaId aid, void* payload );
-
-extern void  VG_(sanity_check_malloc_all) ( void );
-
-extern void  VG_(print_all_arena_stats) ( void );
-
-
 /* ---------------------------------------------------------------------
    Exports of vg_intercept.c
    ------------------------------------------------------------------ */
@@ -575,10 +524,10 @@ extern void  VG_(print_all_arena_stats) ( void );
    A synonym for exit. */
 #define VG_USERREQ__LIBC_FREERES_DONE       0x3029
 
-/* Intercept prefix stuff.  See coregrind/vg_replace_malloc.c for
-   details.  Unfortunately the "_vgi_" literal is also hardcoded in
-   that file, so if you change this one you must also change the other
-   one. */
+/* Intercept prefix stuff.  See
+   coregrind/m_replace_malloc/vg_replace_malloc.c for details.
+   Unfortunately the "_vgi_" literal is also hardcoded in that file, so if
+   you change this one you must also change the other one. */
 #define VG_INTERCEPT_PREFIX "_vgi_"
 #define VG_INTERCEPT_PREFIX_LEN 5
 
similarity index 89%
rename from coregrind/vg_malloc2.c
rename to coregrind/m_mallocfree.c
index e7aebdd2e7a2dbceaf4720e30ab009cff7cbbfde..f2908a6a3ab0d4af91adba4b4828e64d3c2d5ba5 100644 (file)
@@ -1,7 +1,7 @@
 
 /*--------------------------------------------------------------------*/
 /*--- An implementation of malloc/free which doesn't use sbrk.     ---*/
-/*---                                                 vg_malloc2.c ---*/
+/*---                                               m_mallocfree.c ---*/
 /*--------------------------------------------------------------------*/
 
 /*
@@ -518,61 +518,6 @@ Superblock* findSb ( Arena* a, Block* b )
 }
 
 
-/*------------------------------------------------------------*/
-/*--- Command line options                                 ---*/
-/*------------------------------------------------------------*/
-
-/* Nb: the allocator always rounds blocks up to a multiple of
-   VG_MIN_MALLOC_SZB.
-*/
-
-/* DEBUG: print malloc details?  default: NO */
-Bool VG_(clo_trace_malloc)  = False;
-
-/* Minimum alignment in functions that don't specify alignment explicitly.
-   default: 0, i.e. use VG_MIN_MALLOC_SZB. */
-UInt VG_(clo_alignment)     = VG_MIN_MALLOC_SZB;
-
-
-Bool VG_(replacement_malloc_process_cmd_line_option)(Char* arg)
-{
-   if (VG_CLO_STREQN(12, arg, "--alignment=")) {
-      VG_(clo_alignment) = (UInt)VG_(atoll)(&arg[12]);
-
-      if (VG_(clo_alignment) < VG_MIN_MALLOC_SZB
-          || VG_(clo_alignment) > 4096
-          || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) {
-         VG_(message)(Vg_UserMsg, "");
-         VG_(message)(Vg_UserMsg, 
-            "Invalid --alignment= setting.  "
-            "Should be a power of 2, >= %d, <= 4096.", VG_MIN_MALLOC_SZB);
-         VG_(bad_option)("--alignment");
-      }
-   }
-
-   else VG_BOOL_CLO(arg, "--trace-malloc",  VG_(clo_trace_malloc))
-   else 
-      return False;
-
-   return True;
-}
-
-void VG_(replacement_malloc_print_usage)(void)
-{
-   VG_(printf)(
-"    --alignment=<number>      set minimum alignment of allocations [%d]\n",
-   VG_MIN_MALLOC_SZB
-   );
-}
-
-void VG_(replacement_malloc_print_debug_usage)(void)
-{
-   VG_(printf)(
-"    --trace-malloc=no|yes     show client malloc details? [no]\n"
-   );
-}
-
-
 /*------------------------------------------------------------*/
 /*--- Functions for working with freelists.                ---*/
 /*------------------------------------------------------------*/
@@ -1150,8 +1095,7 @@ void VG_(arena_free) ( ArenaId aid, void* ptr )
    .    .               .   .   .               .   .
 
 */
-static 
-void* arena_malloc_aligned ( ArenaId aid, SizeT req_alignB, SizeT req_pszB )
+void* VG_(arena_memalign) ( ArenaId aid, SizeT req_alignB, SizeT req_pszB )
 {
    SizeT  base_pszB_req, base_pszB_act, frag_bszB;
    Block  *base_b, *align_b;
@@ -1170,10 +1114,10 @@ void* arena_malloc_aligned ( ArenaId aid, SizeT req_alignB, SizeT req_pszB )
    // a power of 2.
    if (req_alignB < VG_MIN_MALLOC_SZB
        || req_alignB > 1048576
-       || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) {
-      VG_(printf)("arena_malloc_aligned(%p, %d, %d)\nbad alignment", 
+       || VG_(log2)( req_alignB ) == -1 /* not a power of 2 */) {
+      VG_(printf)("VG_(arena_memalign)(%p, %d, %d)\nbad alignment", 
                   a, req_alignB, req_pszB );
-      VG_(core_panic)("arena_malloc_aligned");
+      VG_(core_panic)("VG_(arena_memalign)");
       /*NOTREACHED*/
    }
    // Paranoid
@@ -1346,103 +1290,6 @@ void* VG_(realloc) ( void* ptr, SizeT size )
    return VG_(arena_realloc) ( VG_AR_TOOL, ptr, size );
 }
 
-void* VG_(cli_malloc) ( SizeT align, SizeT nbytes )                 
-{                                                                             
-   // 'align' should be valid (ie. big enough and a power of two) by now.
-   // arena_malloc_aligned() will abort if it's not.
-   if (VG_MIN_MALLOC_SZB == align)
-      return VG_(arena_malloc)    ( VG_AR_CLIENT, nbytes ); 
-   else                                                                       
-      return arena_malloc_aligned ( VG_AR_CLIENT, align, nbytes );
-}                                                                             
-
-void VG_(cli_free) ( void* p )                                   
-{                                                                             
-   VG_(arena_free) ( VG_AR_CLIENT, p );                          
-}
-
-
-Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size )
-{  
-   return (start - client_malloc_redzone_szB <= a
-           && a < start + size + client_malloc_redzone_szB);
-}
-
-
-/*------------------------------------------------------------*/
-/*--- The original test driver machinery.                  ---*/
-/*------------------------------------------------------------*/
-
-#if 0
-
-#if 1
-#define N_TEST_TRANSACTIONS 100000000
-#define N_TEST_ARR 200000
-#define M_TEST_MALLOC 1000
-#else
-#define N_TEST_TRANSACTIONS 500000
-#define N_TEST_ARR 30000
-#define M_TEST_MALLOC 500
-#endif
-
-
-void* test_arr[N_TEST_ARR];
-
-int main ( int argc, char** argv )
-{
-   Int i, j, k, nbytes, qq;
-   unsigned char* chp;
-   Arena* a = &arena[VG_AR_CORE];
-   srandom(1);
-   for (i = 0; i < N_TEST_ARR; i++)
-      test_arr[i] = NULL;
-
-   for (i = 0; i < N_TEST_TRANSACTIONS; i++) {
-      if (i % 50000 == 0) mallocSanityCheck(a);
-      j = random() % N_TEST_ARR;
-      if (test_arr[j]) {
-         vg_free(a, test_arr[j]);
-         test_arr[j] = NULL;
-      } else {
-         nbytes = 1 + random() % M_TEST_MALLOC;
-         qq = random()%64;
-         if (qq == 32) 
-            nbytes *= 17;
-         else if (qq == 33)
-            nbytes = 0;
-         test_arr[j] 
-           = (i % 17) == 0
-                ? vg_memalign(a, nbytes, 1<< (3+(random()%10)))
-                : vg_malloc( a, nbytes );
-         chp = test_arr[j];
-         for (k = 0; k < nbytes; k++) 
-            chp[k] = (unsigned char)(k + 99);
-      }
-   }
-
-
-   for (i = 0; i < N_TEST_ARR; i++) {
-      if (test_arr[i]) {
-         vg_free(a, test_arr[i]);
-         test_arr[i] = NULL;
-      }
-   }
-   mallocSanityCheck(a);
-
-   fprintf(stderr, "ALL DONE\n");
-
-   show_arena_stats(a);
-   fprintf(stderr, "%d max useful, %d bytes mmap'd (%4.1f%%), %d useful\n",
-           a->bytes_on_loan_max, 
-           a->bytes_mmaped, 
-           100.0 * (double)a->bytes_on_loan_max / (double)a->bytes_mmaped,
-           a->bytes_on_loan );
-
-   return 0;
-}
-#endif /* 0 */
-
-
 /*--------------------------------------------------------------------*/
-/*--- end                                             vg_malloc2.c ---*/
+/*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
diff --git a/coregrind/m_replacemalloc/Makefile.am b/coregrind/m_replacemalloc/Makefile.am
new file mode 100644 (file)
index 0000000..096492c
--- /dev/null
@@ -0,0 +1,27 @@
+include $(top_srcdir)/Makefile.all.am
+include $(top_srcdir)/Makefile.core-AM_CPPFLAGS.am
+
+AM_CFLAGS = $(WERROR) -Wmissing-prototypes -Winline -Wall -Wshadow -O -g
+
+EXTRA_DIST = \
+       README_REPLACEMALLOC.txt
+
+noinst_LIBRARIES = \
+       libreplacemalloc_core.a \
+       libreplacemalloc_toolpreload.a
+
+libreplacemalloc_core_a_SOURCES = \
+       replacemalloc_core.c
+
+if USE_PIE
+libreplacemalloc_core_a_CFLAGS         = $(AM_CFLAGS) -fpie
+else
+libreplacemalloc_core_a_CFLAGS = $(AM_CFLAGS)
+endif
+
+libreplacemalloc_toolpreload_a_SOURCES = \
+       vg_replace_malloc.c
+libreplacemalloc_toolpreload_a_CFLAGS = \
+       $(AM_CFLAGS) -fpic -fno-omit-frame-pointer
+
+
diff --git a/coregrind/m_replacemalloc/README_REPLACEMALLOC.txt b/coregrind/m_replacemalloc/README_REPLACEMALLOC.txt
new file mode 100644 (file)
index 0000000..607e060
--- /dev/null
@@ -0,0 +1,29 @@
+The structure of this module is worth noting.
+
+The main part is in vg_replace_malloc.c.  It gets compiled into the tool's
+'preload' shared object, which goes into the client's area of memory, and
+runs on the simulated CPU just like client code.  As a result, it cannot
+use any functions in the core directly;  it can only communicated with the
+core using client requests, just like any other client code.
+
+And yet it must call the tool's malloc wrappers.  How does it know where
+they are?  The init function uses a client request which asks for the list
+of all the core functions (and variables) that it needs to access.  It then
+uses a client request each time it needs to call one of these.
+
+This means that the following sequence occurs each time a tool that uses
+this module starts up:
+
+ - Tool does initialisation, including calling VG_(malloc_funcs)() to tell
+   the core the names of its malloc wrappers.  These are stored in
+   VG_(tdict).
+
+ - On the first allocation, vg_replace_malloc.c:init() calls the
+   GET_MALLOCFUNCS client request to get the names of the malloc wrappers
+   out of VG_(tdict), storing them in 'info'.
+
+ - All calls to these functions are done using 'info'.
+
+This is a bit complex, but it's hard to see how it can be done more simply. 
+
+
diff --git a/coregrind/m_replacemalloc/replacemalloc_core.c b/coregrind/m_replacemalloc/replacemalloc_core.c
new file mode 100644 (file)
index 0000000..9ef336e
--- /dev/null
@@ -0,0 +1,114 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Malloc replacement.                     replacemalloc_core.c ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward 
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#include "core.h"
+#include "pub_core_replacemalloc.h"
+
+/*------------------------------------------------------------*/
+/*--- Command line options                                 ---*/
+/*------------------------------------------------------------*/
+
+/* Nb: the allocator always rounds blocks up to a multiple of
+   VG_MIN_MALLOC_SZB.
+*/
+
+/* DEBUG: print malloc details?  default: NO */
+Bool VG_(clo_trace_malloc)  = False;
+
+/* Minimum alignment in functions that don't specify alignment explicitly.
+   default: 0, i.e. use VG_MIN_MALLOC_SZB. */
+UInt VG_(clo_alignment)     = VG_MIN_MALLOC_SZB;
+
+
+Bool VG_(replacement_malloc_process_cmd_line_option)(Char* arg)
+{
+   if (VG_CLO_STREQN(12, arg, "--alignment=")) {
+      VG_(clo_alignment) = (UInt)VG_(atoll)(&arg[12]);
+
+      if (VG_(clo_alignment) < VG_MIN_MALLOC_SZB
+          || VG_(clo_alignment) > 4096
+          || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) {
+         VG_(message)(Vg_UserMsg, "");
+         VG_(message)(Vg_UserMsg, 
+            "Invalid --alignment= setting.  "
+            "Should be a power of 2, >= %d, <= 4096.", VG_MIN_MALLOC_SZB);
+         VG_(bad_option)("--alignment");
+      }
+   }
+
+   else VG_BOOL_CLO(arg, "--trace-malloc",  VG_(clo_trace_malloc))
+   else 
+      return False;
+
+   return True;
+}
+
+void VG_(replacement_malloc_print_usage)(void)
+{
+   VG_(printf)(
+"    --alignment=<number>      set minimum alignment of allocations [%d]\n",
+   VG_MIN_MALLOC_SZB
+   );
+}
+
+void VG_(replacement_malloc_print_debug_usage)(void)
+{
+   VG_(printf)(
+"    --trace-malloc=no|yes     show client malloc details? [no]\n"
+   );
+}
+
+/*------------------------------------------------------------*/
+/*--- Useful functions                                     ---*/
+/*------------------------------------------------------------*/
+
+void* VG_(cli_malloc) ( SizeT align, SizeT nbytes )                 
+{                                                                             
+   // 'align' should be valid (ie. big enough and a power of two) by now.
+   // VG_(arena_memalign)() will abort if it's not.
+   if (VG_MIN_MALLOC_SZB == align)
+      return VG_(arena_malloc)   ( VG_AR_CLIENT, nbytes ); 
+   else                                                                       
+      return VG_(arena_memalign) ( VG_AR_CLIENT, align, nbytes );
+}                                                                             
+
+void VG_(cli_free) ( void* p )                                   
+{                                                                             
+   VG_(arena_free) ( VG_AR_CLIENT, p );                          
+}
+
+Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size, SizeT rz_szB )
+{
+   return ( start - rz_szB <= a  &&  a < start + size + rz_szB );
+}
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
similarity index 99%
rename from coregrind/vg_replace_malloc.c
rename to coregrind/m_replacemalloc/vg_replace_malloc.c
index 39ca3f3a85ba0c8b04122a915ec5567436277079..39cb997f871c87eb38c631e6bf1cdd921b378173 100644 (file)
    This file can be linked into the injected so file for any tool that
    wishes to know about calls to malloc().  The tool must define all
    the functions that will be called via 'info'.
+
+   It's called vg_replace_malloc.c because this filename appears in stack
+   traces, so we want it to be something that should be obvious what it
+   means to users.
    ------------------------------------------------------------------ */
 
 #include "valgrind.h"            /* for VALGRIND_NON_SIMD_CALL[12] */
diff --git a/coregrind/pub_core_mallocfree.h b/coregrind/pub_core_mallocfree.h
new file mode 100644 (file)
index 0000000..f6d0ea1
--- /dev/null
@@ -0,0 +1,93 @@
+
+/*--------------------------------------------------------------------*/
+/*--- High-level memory management.          pub_core_mallocfree.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_CORE_MALLOCFREE_H
+#define __PUB_CORE_MALLOCFREE_H
+
+#include "pub_tool_mallocfree.h"
+
+//--------------------------------------------------------------------
+// PURPOSE: high-level memory allocation (malloc/free), for the core and
+// tools.
+//--------------------------------------------------------------------
+
+/* Allocation arenas.  
+
+      CORE      for the core's general use.
+      TOOL      for the tool to use (and the only one it uses).
+      SYMTAB    for Valgrind's symbol table storage.
+      CLIENT    for the client's mallocs/frees, if the tool replaces glibc's
+                    malloc() et al -- redzone size is chosen by the tool.
+      DEMANGLE  for the C++ demangler.
+      EXECTXT   for storing ExeContexts.
+      ERRORS    for storing CoreErrors.
+
+   When adding a new arena, remember also to add it to ensure_mm_init(). 
+*/
+typedef Int ArenaId;
+
+#define VG_N_ARENAS        7
+
+#define VG_AR_CORE         0
+#define VG_AR_TOOL         1
+#define VG_AR_SYMTAB       2
+#define VG_AR_CLIENT       3
+#define VG_AR_DEMANGLE     4
+#define VG_AR_EXECTXT      5
+#define VG_AR_ERRORS       6
+
+// This is both the minimum payload size of a malloc'd block, and its
+// minimum alignment.  Must be a power of 2 greater than 4, and should be
+// greater than 8.
+#define VG_MIN_MALLOC_SZB        8
+
+extern void* VG_(arena_malloc)  ( ArenaId arena, SizeT nbytes );
+extern void  VG_(arena_free)    ( ArenaId arena, void* ptr );
+extern void* VG_(arena_calloc)  ( ArenaId arena, 
+                                  SizeT nmemb, SizeT bytes_per_memb );
+extern void* VG_(arena_realloc) ( ArenaId arena, void* ptr, SizeT size );
+extern void* VG_(arena_memalign)( ArenaId aid, SizeT req_alignB, 
+                                               SizeT req_pszB );
+
+/* Sets the size of the redzones at the start and end of heap blocks.  This
+   must be called before any of VG_(malloc) and friends are called. */
+extern void  VG_(set_client_malloc_redzone_szB) ( SizeT rz_szB );
+
+extern SizeT VG_(arena_payload_szB) ( ArenaId aid, void* payload );
+
+extern void  VG_(sanity_check_malloc_all) ( void );
+
+extern void  VG_(print_all_arena_stats) ( void );
+
+#endif   // __PUB_CORE_MALLOCFREE_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/pub_core_replacemalloc.h b/coregrind/pub_core_replacemalloc.h
new file mode 100644 (file)
index 0000000..20ec1bc
--- /dev/null
@@ -0,0 +1,48 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Replacing malloc.                   pub_core_replacemalloc.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_CORE_REPLACEMALLOC_H
+#define __PUB_CORE_REPLACEMALLOC_H
+
+//--------------------------------------------------------------------
+// PURPOSE: This module provides a short-cut for tools that need to
+// replace malloc/free with their own versions.
+//--------------------------------------------------------------------
+
+// Nb: there are no exports in this header;  all exports for this module
+// are in include/pub_tool_replacemalloc.h
+
+#include "pub_tool_replacemalloc.h"
+
+#endif   // __PUB_CORE_REPLACEMALLOC_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
index 84f2dc5428e4c8cef2cbb55e4cdb7bd7ee29bd4e..a54c62bbe2c1f549afc7b4bd9315ad539c6e0623 100644 (file)
@@ -207,9 +207,7 @@ static void *before_pthread_create(va_list va)
    // TL_(malloc) being a weak symbol, and it just doesn't fit with the
    // VG_(tdict) approach that we've switched to.  The right way to do this
    // will be to provide a function in the core that checks if
-   // VG_(tdict).malloc_malloc has been set (or perhaps it would be better
-   // to check whether VG_USERREQ__GET_MALLOCFUNCS has been called, ie.
-   // whether replace_malloc.c:init() has been called);  if so, it should
+   // VG_(tdict).malloc_malloc has been set;  if so, it should
    // call it, if not, it should call VG_(cli_malloc)().
 //   VG_(tl_malloc_called_deliberately) = True;
 //   data = TL_(malloc)(sizeof(*data));
index 5d7bf02d92d2438d720e22e8ea45f089e454086a..585086d792c70eb18abc5ff6b653232a723c4d88 100644 (file)
@@ -61,6 +61,7 @@
 #include "core.h"
 
 #include "pub_core_errormgr.h"
+#include "pub_core_replacemalloc.h"
 #include "pub_core_stacktrace.h"
 #include "pub_core_syscalls.h"
 
index a9593916cc7527e993cc807b342ae380e39d863c..320e2764ed0ba9218941d4bd431b821e34ca2aec 100644 (file)
@@ -6,10 +6,11 @@ vgtool_helgrind_so_SOURCES = hg_main.c
 vgtool_helgrind_so_LDFLAGS = -shared
 
 vgpreload_helgrind_so_SOURCES = 
-vgpreload_helgrind_so_DEPENDENCIES = $(top_builddir)/coregrind/lib_replace_malloc.a
+vgpreload_helgrind_so_DEPENDENCIES = \
+       $(LIBREPLACEMALLOC)
 vgpreload_helgrind_so_LDFLAGS = -shared -Wl,-z,interpose,-z,initfirst \
        -Wl,--whole-archive \
-       $(top_builddir)/coregrind/lib_replace_malloc.a \
+       $(LIBREPLACEMALLOC) \
        -Wl,--no-whole-archive
 
 hgincludedir = $(includedir)/valgrind
index acb612c7a62454d14910e4346bfcec396ef455aa..0677c8098b8b380cddacbd5aa1f5766db00c7cca 100644 (file)
@@ -30,6 +30,9 @@
 */
 
 #include "tool.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_replacemalloc.h"
+
 #include "helgrind.h"
 
 static UInt n_hg_warnings = 0;
index 503c5dd9bd25ec8db822c80a272ca25df9698132..75fdaeac569bf2c4dbe8ba81f6d7f5b0e6569b48 100644 (file)
@@ -14,6 +14,8 @@ incinc_HEADERS = \
        tool_asm.h              \
        pub_tool_errormgr.h     \
        pub_tool_execontext.h   \
+       pub_tool_mallocfree.h   \
+       pub_tool_replacemalloc.h\
        pub_tool_stacktrace.h   \
        valgrind.h
 
diff --git a/include/pub_tool_mallocfree.h b/include/pub_tool_mallocfree.h
new file mode 100644 (file)
index 0000000..d89edaf
--- /dev/null
@@ -0,0 +1,46 @@
+
+/*--------------------------------------------------------------------*/
+/*--- MallocFree: high-level memory management.                    ---*/
+/*---                                        pub_tool_mallocfree.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_TOOL_MALLOCFREE_H
+#define __PUB_TOOL_MALLOCFREE_H
+
+// These can be for allocating memory used by tools.
+extern void* VG_(malloc)         ( SizeT nbytes );
+extern void  VG_(free)           ( void* p );
+extern void* VG_(calloc)         ( SizeT n, SizeT bytes_per_elem );
+extern void* VG_(realloc)        ( void* p, SizeT size );
+
+#endif   // __PUB_TOOL_MALLOCFREE_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
+
diff --git a/include/pub_tool_replacemalloc.h b/include/pub_tool_replacemalloc.h
new file mode 100644 (file)
index 0000000..250bba8
--- /dev/null
@@ -0,0 +1,67 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Malloc replacement.                 pub_tool_replacemalloc.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2005 Julian Seward
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_TOOL_REPLACEMALLOC_H
+#define __PUB_TOOL_REPLACEMALLOC_H
+
+/* If a tool replaces malloc() et al, the easiest way to do so is to
+   link libreplacemalloc_toolpreload.o into its vgpreload_*.so file, and
+   use the functions declared below.  You can do it from scratch,
+   though, if you enjoy that sort of thing. */
+
+/* Can be called from VG_(tdict).malloc_malloc et al to do the actual
+ * alloc/freeing. */
+extern void* VG_(cli_malloc) ( SizeT align, SizeT nbytes );
+extern void  VG_(cli_free)   ( void* p );
+
+/* Check if an address is within a range, allowing for redzones at edges */
+extern Bool VG_(addr_is_in_block)( Addr a, Addr start,
+                                   SizeT size, SizeT rz_szB );
+
+/* ------------------------------------------------------------------ */
+/* Some options that can be used by a tool if malloc() et al are replaced.
+   The tool should call the functions in the appropriate places to give
+   control over these aspects of Valgrind's version of malloc(). */
+
+/* DEBUG: print malloc details?  default: NO */
+extern Bool VG_(clo_trace_malloc);
+/* Minimum alignment in functions that don't specify alignment explicitly.
+   default: VG_MIN_MALLOC_SZB */
+extern UInt VG_(clo_alignment);
+
+extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
+extern void VG_(replacement_malloc_print_usage)             ( void );
+extern void VG_(replacement_malloc_print_debug_usage)       ( void );
+
+#endif   // __PUB_TOOL_REPLACEMALLOC_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/
index 3571fb23b867d28e93400d400cf813084c17429a..39caa89b999e9c8181b8938a30747a9ed5d423c4 100644 (file)
@@ -317,11 +317,6 @@ extern Int  VG_(rename) ( Char* old_name, Char* new_name );
 /* ------------------------------------------------------------------ */
 /* stdlib.h */
 
-extern void* VG_(malloc)         ( SizeT nbytes );
-extern void  VG_(free)           ( void* p );
-extern void* VG_(calloc)         ( SizeT n, SizeT bytes_per_elem );
-extern void* VG_(realloc)        ( void* p, SizeT size );
-
 /* terminate everything */
 extern void VG_(exit)( Int status )
             __attribute__ ((__noreturn__));
@@ -781,39 +776,6 @@ extern void VG_(get_shadow_regs_area) ( ThreadId tid, OffT guest_state_offset,
 extern void VG_(set_shadow_regs_area) ( ThreadId tid, OffT guest_state_offset,
                                         SizeT size, const UChar* area );
 
-/*====================================================================*/
-/*=== Specific stuff for replacing malloc() and friends            ===*/
-/*====================================================================*/
-
-/* If a tool replaces malloc() et al, the easiest way to do so is to
-   link with vg_replace_malloc.o into its vgpreload_*.so file, and
-   follow the following instructions.  You can do it from scratch,
-   though, if you enjoy that sort of thing. */
-
-/* Can be called from VG_(tdict).malloc_malloc et al to do the actual
- * alloc/freeing. */
-extern void* VG_(cli_malloc) ( SizeT align, SizeT nbytes );
-extern void  VG_(cli_free)   ( void* p );
-
-/* Check if an address is within a range, allowing for redzones at edges */
-extern Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size );
-
-/* ------------------------------------------------------------------ */
-/* Some options that can be used by a tool if malloc() et al are replaced.
-   The tool should call the functions in the appropriate places to give
-   control over these aspects of Valgrind's version of malloc(). */
-
-/* DEBUG: print malloc details?  default: NO */
-extern Bool VG_(clo_trace_malloc);
-/* Minimum alignment in functions that don't specify alignment explicitly.
-   default: VG_MIN_MALLOC_SZB */
-extern UInt VG_(clo_alignment);
-
-extern Bool VG_(replacement_malloc_process_cmd_line_option) ( Char* arg );
-extern void VG_(replacement_malloc_print_usage)             ( void );
-extern void VG_(replacement_malloc_print_debug_usage)       ( void );
-
-
 /*====================================================================*/
 /*=== Tool-specific stuff                                          ===*/
 /*====================================================================*/
index 468794dac3f965894d2997ca7bac1d5f27f1454f..ffe94d9127c57d3fb757c6c509f1ed0895b58c7e 100644 (file)
@@ -8,10 +8,11 @@ vgtool_massif_so_SOURCES = ms_main.c
 vgtool_massif_so_LDFLAGS = -shared
 
 vgpreload_massif_so_SOURCES = 
-vgpreload_massif_so_DEPENDENCIES = $(top_builddir)/coregrind/lib_replace_malloc.a
+vgpreload_massif_so_DEPENDENCIES = \
+       $(LIBREPLACEMALLOC)
 vgpreload_massif_so_LDFLAGS = -shared -Wl,-z,interpose,-z,initfirst \
        -Wl,--whole-archive \
-       $(top_builddir)/coregrind/lib_replace_malloc.a \
+       $(LIBREPLACEMALLOC) \
        -Wl,--no-whole-archive
 
 
index 530ef21f8ee7c43ea31da43e5ec021ac97229629..df2ca807b8f81a9686e42f3442d11792d2c45fa0 100644 (file)
@@ -35,8 +35,9 @@
 // structures below for more info on how things work.
 
 #include "tool.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_replacemalloc.h"
 #include "pub_tool_stacktrace.h"
-//#include "vg_profile.c"
 
 #include "valgrind.h"           // For {MALLOC,FREE}LIKE_BLOCK
 
index 6deade31d511edf4fd5c15ed2836547aa1d25844..7c710a0420d62bcae5e83ba5c655002c6dc05b39 100644 (file)
@@ -7,10 +7,11 @@ val_PROGRAMS = vgtool_memcheck.so vgpreload_memcheck.so
 
 vgpreload_memcheck_so_SOURCES = \
        mac_replace_strmem.c
-vgpreload_memcheck_so_DEPENDENCIES = $(top_builddir)/coregrind/lib_replace_malloc.a
+vgpreload_memcheck_so_DEPENDENCIES = \
+       $(LIBREPLACEMALLOC)
 vgpreload_memcheck_so_LDFLAGS = -shared -Wl,-z,interpose,-z,initfirst \
        -Wl,--whole-archive \
-       $(top_builddir)/coregrind/lib_replace_malloc.a \
+       $(LIBREPLACEMALLOC) \
        -Wl,--no-whole-archive
 
 vgtool_memcheck_so_SOURCES = \
index 55a2bec0b93e5a43a871db65741149aec5cd7816..9594474f1eabbeb36ec6db219851323ae9c77921 100644 (file)
@@ -379,7 +379,8 @@ static Bool addr_is_in_MAC_Chunk(MAC_Chunk* mc, void *ap)
 {
    Addr a = *(Addr *)ap;
   
-   return VG_(addr_is_in_block)( a, mc->data, mc->size );
+   return VG_(addr_is_in_block)( a, mc->data, mc->size,
+                                 MAC_MALLOC_REDZONE_SZB );
 }
 
 /* Callback for searching malloc'd lists */
index c541fc9ad50a5709b37b91923c080f6b5d8164af..82303e8e953891ed65d78b5d20c7de5d13a230ec 100644 (file)
@@ -37,6 +37,8 @@
 #define __MAC_SHARED_H
 
 #include "tool.h"
+#include "pub_tool_mallocfree.h"
+#include "pub_tool_replacemalloc.h"
 
 #define MAC_(str)    VGAPPEND(vgMAC_,str)
 
index 547c5a94a7a960aac0be0565f9a04de2a0810a51..e1692507e6426ae82f38f7a3e557ae23b0ba5939 100644 (file)
@@ -2156,7 +2156,7 @@ static Bool find_addr(VgHashNode* sh_ch, void* ap)
   MAC_Chunk *m = (MAC_Chunk*)sh_ch;
   Addr a = *(Addr*)ap;
 
-  return VG_(addr_is_in_block)(a, m->data, m->size);
+  return VG_(addr_is_in_block)(a, m->data, m->size, MAC_MALLOC_REDZONE_SZB);
 }
 
 static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai )
@@ -2168,7 +2168,8 @@ static Bool client_perm_maybe_describe( Addr a, AddrInfo* ai )
    for (i = 0; i < cgb_used; i++) {
       if (cgbs[i].start == 0 && cgbs[i].size == 0) 
          continue;
-      if (VG_(addr_is_in_block)(a, cgbs[i].start, cgbs[i].size)) {
+      // Use zero as the redzone for client blocks.
+      if (VG_(addr_is_in_block)(a, cgbs[i].start, cgbs[i].size, 0)) {
          MAC_Mempool **d, *mp;
 
          /* OK - maybe it's a mempool, too? */