]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Malloc replacement worked very much like a 'need', but wasn't one.
authorNicholas Nethercote <njn@valgrind.org>
Tue, 21 Jun 2005 03:20:17 +0000 (03:20 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Tue, 21 Jun 2005 03:20:17 +0000 (03:20 +0000)
I've changed it so it now is, which makes it consistent with the
other 'needs'.  Because of this, I was also able to invert the dependence
between m_mallocfree and m_tooliface, which is related to setting
the redzone size for client heap blocks.  As a result, m_tooliface
now doesn't depend on anything except pub_core_basics.h, hooray!

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

addrcheck/ac_main.c
coregrind/m_mallocfree.c
coregrind/m_scheduler/scheduler.c
coregrind/m_tooliface.c
coregrind/pub_core_tooliface.h
helgrind/hg_main.c
include/pub_tool_tooliface.h
massif/ms_main.c
memcheck/mc_main.c

index d36684d45d8ed65a74a8942ad8ab913c033ebd71..d33564a2faa2630b3ae28492481fdc07e8956d68 100644 (file)
@@ -1344,7 +1344,7 @@ static void ac_pre_clo_init(void)
                                    ac_expensive_sanity_check);
    VG_(needs_shadow_memory)       ();
 
-   VG_(malloc_funcs)              (MAC_(malloc),
+   VG_(needs_malloc_replacement)  (MAC_(malloc),
                                    MAC_(__builtin_new),
                                    MAC_(__builtin_vec_new),
                                    MAC_(memalign),
index 35730c147dc356a4453c437973348e0eb74c52f6..cc203c44f4befbabce5114fdf8aa2708f0ffab0f 100644 (file)
@@ -37,6 +37,7 @@
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
 #include "pub_core_profile.h"
+#include "pub_core_tooliface.h"
 #include "valgrind.h"
 
 //zz#include "memcheck/memcheck.h"
@@ -386,31 +387,6 @@ void VG_(print_all_arena_stats) ( void )
    }
 }
 
-static Bool init_done = False;
-static SizeT client_malloc_redzone_szB = 8;   // default: be paranoid
-
-// Nb: this must be called before the client arena is initialised, ie.
-// before any memory is allocated.
-void VG_(set_client_malloc_redzone_szB)(SizeT rz_szB)
-{
-   if (init_done) {
-      VG_(printf)(
-         "\nTool error:\n"
-         "%s cannot be called after the first allocation.\n",
-         __PRETTY_FUNCTION__);
-      VG_(exit)(1);
-   }
-   // This limit is no special figure, just something not too big
-   if (rz_szB > 128) {
-      VG_(printf)(
-         "\nTool error:\n"
-         "  %s passed a too-big value (%llu)", 
-         __PRETTY_FUNCTION__, (ULong)rz_szB);
-      VG_(exit)(1);
-   }
-   client_malloc_redzone_szB = rz_szB;
-}
-
 /* This library is self-initialising, as it makes this more self-contained,
    less coupled with the outside world.  Hence VG_(arena_malloc)() and
    VG_(arena_free)() below always call ensure_mm_init() to ensure things are
@@ -418,10 +394,29 @@ void VG_(set_client_malloc_redzone_szB)(SizeT rz_szB)
 static
 void ensure_mm_init ( void )
 {
+   static Bool  init_done = False;
+   static SizeT client_redzone_szB = 8;   // default: be paranoid
+
    if (init_done) {
+      // This assertion ensures that a tool cannot try to change the client
+      // redzone size with VG_(needs_malloc_replacement)() after this module
+      // has done its first allocation.
+      if (VG_(needs).malloc_replacement)
+         vg_assert(client_redzone_szB == VG_(tdict).tool_client_redzone_szB);
       return;
    }
 
+   if (VG_(needs).malloc_replacement) {
+      client_redzone_szB = VG_(tdict).tool_client_redzone_szB;
+      // 128 is no special figure, just something not too big
+      if (client_redzone_szB > 128) {
+         VG_(printf)( "\nTool error:\n"
+                      "  specified redzone size is too big (%llu)\n", 
+                      (ULong)client_redzone_szB);
+         VG_(exit)(1);
+      }
+   }
+
    /* Use checked red zones (of various sizes) for our internal stuff,
       and an unchecked zone of arbitrary size for the client.  Of
       course the client's red zone can be checked by the tool, eg. 
@@ -437,7 +432,7 @@ void ensure_mm_init ( void )
    arena_init ( VG_AR_CORE,      "core",     4, CORE_ARENA_MIN_SZB );
    arena_init ( VG_AR_TOOL,      "tool",     4,            1048576 );
    arena_init ( VG_AR_SYMTAB,    "symtab",   4,            1048576 );
-   arena_init ( VG_AR_CLIENT,    "client", client_malloc_redzone_szB, 1048576 );
+   arena_init ( VG_AR_CLIENT,    "client", client_redzone_szB, 1048576 );
    arena_init ( VG_AR_DEMANGLE,  "demangle", 12/*paranoid*/, 65536 );
    arena_init ( VG_AR_EXECTXT,   "exectxt",  4,              65536 );
    arena_init ( VG_AR_ERRORS,    "errors",   4,              65536 );
index afbedd0cfba8fe73835a874792ba2519ec14bbd8..26def4df89670e141397b4826c01f96bba700ce7 100644 (file)
@@ -980,15 +980,15 @@ void do_client_request ( ThreadId tid )
       case VG_USERREQ__GET_MALLOCFUNCS: {
         struct vg_mallocfunc_info *info = (struct vg_mallocfunc_info *)arg[1];
 
-        info->tl_malloc               = VG_(tdict).malloc_malloc;
-        info->tl_calloc               = VG_(tdict).malloc_calloc;
-        info->tl_realloc              = VG_(tdict).malloc_realloc;
-        info->tl_memalign             = VG_(tdict).malloc_memalign;
-        info->tl___builtin_new        = VG_(tdict).malloc___builtin_new;
-        info->tl___builtin_vec_new    = VG_(tdict).malloc___builtin_vec_new;
-        info->tl_free                 = VG_(tdict).malloc_free;
-        info->tl___builtin_delete     = VG_(tdict).malloc___builtin_delete;
-        info->tl___builtin_vec_delete = VG_(tdict).malloc___builtin_vec_delete;
+        info->tl_malloc               = VG_(tdict).tool_malloc;
+        info->tl_calloc               = VG_(tdict).tool_calloc;
+        info->tl_realloc              = VG_(tdict).tool_realloc;
+        info->tl_memalign             = VG_(tdict).tool_memalign;
+        info->tl___builtin_new        = VG_(tdict).tool___builtin_new;
+        info->tl___builtin_vec_new    = VG_(tdict).tool___builtin_vec_new;
+        info->tl_free                 = VG_(tdict).tool_free;
+        info->tl___builtin_delete     = VG_(tdict).tool___builtin_delete;
+        info->tl___builtin_vec_delete = VG_(tdict).tool___builtin_vec_delete;
 
         info->arena_payload_szB       = VG_(arena_payload_szB);
         info->clo_trace_malloc        = VG_(clo_trace_malloc);
index db8962ec47bee3b9f8de70eb4375252d8682786c..619f2d3deb5816033243c62785f20469c59297cd 100644 (file)
@@ -30,7 +30,6 @@
 */
 
 #include "pub_core_basics.h"
-#include "pub_core_mallocfree.h"    // For VG_(set_client_malloc_redzone_szB)()
 #include "pub_core_tooliface.h"
 
 // The core/tool dictionary of functions (initially zeroed, as we want it)
@@ -87,14 +86,13 @@ VgNeeds VG_(needs) = {
    .tool_errors          = False,
    .libc_freeres         = False,
    .basic_block_discards = False,
-   .no_longer_used_1     = False,
    .command_line_options = False,
    .client_requests      = False,
-   .no_longer_used_0     = False,
    .syscall_wrapper      = False,
    .sanity_checks        = False,
    .data_syms           = False,
    .shadow_memory        = False,
+   .malloc_replacement   = False,
 };
 
 /* static */
@@ -233,11 +231,7 @@ void VG_(needs_sanity_checks)(
    VG_(tdict).tool_expensive_sanity_check = expen;
 }
 
-
-/*--------------------------------------------------------------------*/
-/* Replacing malloc() */
-
-extern void VG_(malloc_funcs)(
+void VG_(needs_malloc_replacement)(
    void* (*malloc)               ( ThreadId, SizeT ),
    void* (*__builtin_new)        ( ThreadId, SizeT ),
    void* (*__builtin_vec_new)    ( ThreadId, SizeT ),
@@ -250,17 +244,17 @@ extern void VG_(malloc_funcs)(
    SizeT client_malloc_redzone_szB
 )
 {
-   VG_(tdict).malloc_malloc               = malloc;
-   VG_(tdict).malloc___builtin_new        = __builtin_new;
-   VG_(tdict).malloc___builtin_vec_new    = __builtin_vec_new;
-   VG_(tdict).malloc_memalign             = memalign;
-   VG_(tdict).malloc_calloc               = calloc;
-   VG_(tdict).malloc_free                 = free;
-   VG_(tdict).malloc___builtin_delete     = __builtin_delete;
-   VG_(tdict).malloc___builtin_vec_delete = __builtin_vec_delete;
-   VG_(tdict).malloc_realloc              = realloc;
-
-   VG_(set_client_malloc_redzone_szB)( client_malloc_redzone_szB );
+   VG_(needs).malloc_replacement        = True;
+   VG_(tdict).tool_malloc               = malloc;
+   VG_(tdict).tool___builtin_new        = __builtin_new;
+   VG_(tdict).tool___builtin_vec_new    = __builtin_vec_new;
+   VG_(tdict).tool_memalign             = memalign;
+   VG_(tdict).tool_calloc               = calloc;
+   VG_(tdict).tool_free                 = free;
+   VG_(tdict).tool___builtin_delete     = __builtin_delete;
+   VG_(tdict).tool___builtin_vec_delete = __builtin_vec_delete;
+   VG_(tdict).tool_realloc              = realloc;
+   VG_(tdict).tool_client_redzone_szB   = client_malloc_redzone_szB;
 }
 
 
index dcd30ee8d2c35032857d47ebbcc237d60d8868fe..2b2d10b005b04fbdd152b5cecf49fdf12c4b7faa 100644 (file)
@@ -84,14 +84,13 @@ typedef
       Bool core_errors;
       Bool tool_errors;
       Bool basic_block_discards;
-      Bool no_longer_used_1;     // for backwards compatibility
       Bool command_line_options;
       Bool client_requests;
-      Bool no_longer_used_0;     // for backwards compatibility
       Bool syscall_wrapper;
       Bool sanity_checks;
       Bool data_syms;
       Bool shadow_memory;
+      Bool malloc_replacement;
    } 
    VgNeeds;
 
@@ -141,6 +140,18 @@ typedef struct {
    Bool (*tool_cheap_sanity_check)(void);
    Bool (*tool_expensive_sanity_check)(void);
 
+   // VG_(needs).malloc_replacement
+   void* (*tool_malloc)              (ThreadId, SizeT);
+   void* (*tool___builtin_new)       (ThreadId, SizeT);
+   void* (*tool___builtin_vec_new)   (ThreadId, SizeT);
+   void* (*tool_memalign)            (ThreadId, SizeT, SizeT);
+   void* (*tool_calloc)              (ThreadId, SizeT, SizeT);
+   void  (*tool_free)                (ThreadId, void*);
+   void  (*tool___builtin_delete)    (ThreadId, void*);
+   void  (*tool___builtin_vec_delete)(ThreadId, void*);
+   void* (*tool_realloc)             (ThreadId, void*, SizeT);
+   SizeT tool_client_redzone_szB;
+
    // -- Event tracking functions ------------------------------------
    void (*track_new_mem_startup)     (Addr, SizeT, Bool, Bool, Bool);
    void (*track_new_mem_stack_signal)(Addr, SizeT);
@@ -192,17 +203,6 @@ typedef struct {
 
    void (*track_init_shadow_page)(Addr);
 
-   // -- malloc/free replacements -----------------------------------
-   void* (*malloc_malloc)              (ThreadId, SizeT);
-   void* (*malloc___builtin_new)       (ThreadId, SizeT);
-   void* (*malloc___builtin_vec_new)   (ThreadId, SizeT);
-   void* (*malloc_memalign)            (ThreadId, SizeT, SizeT);
-   void* (*malloc_calloc)              (ThreadId, SizeT, SizeT);
-   void  (*malloc_free)                (ThreadId, void*);
-   void  (*malloc___builtin_delete)    (ThreadId, void*);
-   void  (*malloc___builtin_vec_delete)(ThreadId, void*);
-   void* (*malloc_realloc)             (ThreadId, void*, SizeT);
-
 } VgToolInterface;
 
 extern VgToolInterface VG_(tdict);
index 372f6f8d6f82039e4e9f97f7d3da9e195a28abd2..f66448db41e8144d3ee0dbbf113832e4264d33da 100644 (file)
@@ -3392,7 +3392,7 @@ static void hg_pre_clo_init(void)
                                    hg_print_debug_usage);
    VG_(needs_shadow_memory)       ();
 
-   VG_(malloc_funcs)              (hg_malloc,
+   VG_(needs_malloc_replacement)  (hg_malloc,
                                    hg___builtin_new,
                                    hg___builtin_vec_new,
                                    hg_memalign,
index 5514080b0923ac4a35277ed5764d92ce1f61aae5..ab39e7c2f9e524b7c74d3aff3a18fc0b54633cb6 100644 (file)
@@ -253,11 +253,9 @@ extern void VG_(needs_data_syms) ( void );
 /* Does the tool need shadow memory allocated? */
 extern void VG_(needs_shadow_memory)( void );
 
-/* ------------------------------------------------------------------ */
-/* Malloc replacement */
-
+/* Does the tool replace malloc() and friends with its own versions? */
 // The 'p' prefix avoids GCC complaints about overshadowing global names.
-extern void VG_(malloc_funcs)(
+extern void VG_(needs_malloc_replacement)(
    void* (*pmalloc)               ( ThreadId tid, SizeT n ),
    void* (*p__builtin_new)        ( ThreadId tid, SizeT n ),
    void* (*p__builtin_vec_new)    ( ThreadId tid, SizeT n ),
index 18890a1f9d981d5ff1132a26982219963b3df2c3..16f387d470babae35037bf529b8176886f061825 100644 (file)
@@ -1802,9 +1802,7 @@ static void ms_pre_clo_init()
                                    ms_print_usage,
                                    ms_print_debug_usage);
    VG_(needs_client_requests)     (ms_handle_client_request);
-
-   // Malloc replacement
-   VG_(malloc_funcs)              (ms_malloc,
+   VG_(needs_malloc_replacement)  (ms_malloc,
                                    ms___builtin_new,
                                    ms___builtin_vec_new,
                                    ms_memalign,
index 6a1365be4ac0ace826e13cdf7592dd0a2b5fdab0..cb75c2fb1fb76595e52765952eed6ee57dc152ce 100644 (file)
@@ -2554,7 +2554,7 @@ static void mc_pre_clo_init(void)
                                    mc_expensive_sanity_check);
    VG_(needs_shadow_memory)       ();
 
-   VG_(malloc_funcs)              (MAC_(malloc),
+   VG_(needs_malloc_replacement)  (MAC_(malloc),
                                    MAC_(__builtin_new),
                                    MAC_(__builtin_vec_new),
                                    MAC_(memalign),