Hi I have Written a memory allocator, and works perfectly. I use sbrk/brk for page allocation and deallocation. But it all breaks the moment i start printing information using printfs. Googling shows that - printf internally does use sbrk also. So, another glibc function (printf) making use of sbrk modified heap segment unexpectedly - corrupting the bookkeeping the memory allocator is doing.
Reference : sbrk(0) value getting increased after calling printf basically, any other glibc function using sbrk would break my memory allocator. Can you suggest what could be the possible solution to it ?
Pasting the below backtrace showing that printf eventually calls sbrk. Even after printing is done, I see, break pointer never restores to its original point. Shouldnt printf must have restored the break pointer where it was originally in heap segment ? Any alternative to printf in this regard ?
(gdb) bt
0 __GI___sbrk (increment=135168) at sbrk.c:40
1 0x00007ffff7e68a99 in __GI___default_morecore (increment=<optimized out>) at morecore.c:47
2 0x00007ffff7e64297 in sysmalloc (nb=nb@entry=592, av=av@entry=0x7ffff7fb2c40 <main_arena>) at malloc.c:2480
3 0x00007ffff7e657b3 in _int_malloc (av=av@entry=0x7ffff7fb2c40 <main_arena>, bytes=bytes@entry=576) at malloc.c:4149
4 0x00007ffff7e65f25 in tcache_init () at malloc.c:2995
5 0x00007ffff7e66ba6 in tcache_init () at malloc.c:3050
6 __GI___libc_malloc (bytes=1024) at malloc.c:3050
7 0x00007ffff7e4f85c in __GI__IO_file_doallocate (fp=0x7ffff7fb3760 <_IO_2_1_stdout_>) at filedoalloc.c:101
8 0x00007ffff7e5f0b2 in __GI__IO_doallocbuf (fp=fp@entry=0x7ffff7fb3760 <_IO_2_1_stdout_>) at libioP.h:904
9 0x00007ffff7e5e198 in _IO_new_file_overflow (f=0x7ffff7fb3760 <_IO_2_1_stdout_>, ch=-1) at fileops.c:752
10 0x00007ffff7e5cbd5 in _IO_new_file_xsputn (n=13, data=<optimized out>, f=0x7ffff7fb3760 <_IO_2_1_stdout_>) at libioP.h:904
11 _IO_new_file_xsputn (f=0x7ffff7fb3760 <_IO_2_1_stdout_>, data=<optimized out>, n=13) at fileops.c:1204
12 0x00007ffff7e44e10 in __vfprintf_internal (s=0x7ffff7fb3760 <_IO_2_1_stdout_>, format=0x555555557334 "\nPage Size = %zu Bytes\n", ap=ap@entry=0x7fffffffdcc0,
mode_flags=mode_flags@entry=0) at ../libio/libioP.h:904
13 0x00007ffff7e308d8 in __printf (format=<optimized out>) at printf.c:33
14 0x000055555555677d in mm_print_memory_usage () at mm.c:613
15 0x00005555555552a9 in main (argc=1, argv=0x7fffffffdf18) at testapp.c:64