Yes because printf will invoke malloc which will call your wrapper
which will call printf etc etc until the stack is exhausted.

You can't use anything in your wrapper that might use malloc.


On 12/07/18 13:07, shuai xi wrote:
thank you for your answer.
But when i use  _WRAP_ macros like following code, it gives me an error.
long I_WRAP_SONAME_FNNAME_ZU(libcZdsoZd6,malloc) ( long n )
   char *  r;
   OrigFn fn;
   CALL_FN_W_W(r, fn, n);
   //printf("in wrapper1-post: fact(%d) = %x\n", n, r);
   return r;

/* --------------- */

int main ( void )
   char * r , *x, *y;
   //printf("computing fact(5)\n");
   //r = fact(5);
   r = malloc(0x20);
   x = malloc(0x20);
   printf("malloc(0x20) = %x \n",r );
   printf("malloc(0x20) = %x \n",x );
   return 0;
==14498== Stack overflow in thread #1: can't grow stack to 0xfe04d000
==14498== Process terminating with default action of signal 11 (SIGSEGV)
==14498==  Access not within mapped region at address 0xFE04DFFC
==14498== Stack overflow in thread #1: can't grow stack to 0xfe04d000
==14498==    at 0x40B1183: _IO_doallocbuf (genops.c:394)
==14498==  If you believe this happened as a result of a stack
==14498==  overflow in your program's main thread (unlikely but
==14498==  possible), you can try to increase the size of the
==14498==  main thread stack using the --main-stacksize= flag.
==14498==  The main thread stack size used in this run was 8388608.

But when i comment out the ' printf("11111\n"); ', It seems ok.....

On Thu, Jul 12, 2018 at 7:24 PM Tom Hughes < <>> wrote:

    On 12/07/18 10:26, shuai xi wrote:

     > I know that 'Memcheck's implementation of malloc has "nothing to do"
     > with glibc's implementation' after readind some code of valgrind,
    but i
     > want to use the glibc's malloc to avoid changing heap layout.
     > Memcheck sets the VG_(needs_malloc_replacement) to hook malloc
    and exec
     > MC_(malloc) instead of real malloc. It's very convenient. So can i
     > get the address of the original malloc in MC_(malloc) and exec it?
     > if not,can i use I_WRAP_SONAME_FNNAME_ZU function to hook malloc
    in my
     > valgrind tool's code?

    Yes if you want to wrap the function rather than replacing it
    then use the _WRAP_ macros and then your wrapper can get the
    original address and call it.


-- Tom Hughes ( <>)

Tom Hughes (

Check out the vibrant tech community on one of the world's most
engaging tech sites,!
Valgrind-users mailing list

Reply via email to