Where is heap_info mapped into memory?

Viewed 295

I'm trying to determine how internals of the heap work. Specially now the function and common location of the heap_info struct.

I understand the basic function and how to locate main_arena. I understand that there is a heap_info struct that might hold pointers to main_arenas?

As seen here in glibc-2.23 the new_heap function uses it to create new heaps:

 535 static heap_info *
 536 internal_function
 537 new_heap (size_t size, size_t top_pad)
 538 {
 539   size_t pagesize = GLRO (dl_pagesize);
 540   char *p1, *p2;
 541   unsigned long ul;
 542   heap_info *h;
...

99   if (__mprotect (p2, size, PROT_READ | PROT_WRITE) != 0)
 600     {
 601       __munmap (p2, HEAP_MAX_SIZE);
 602       return 0;
 603     }
 604   h = (heap_info *) p2;
 605   h->size = size;
 606   h->mprotect_size = size;
 607   LIBC_PROBE (memory_heap_new, 2, h, h->size);
 608   return h;

It also sees some action in other parts of the glibc code for example:

> grep * -Rnie heap_info
ChangeLog.16:1043:  * malloc/arena.c (heap_info): Adjust the padding size if
ChangeLog.17:11564: * malloc/arena.c (heap_info): Add mprotect_size field, adjust pad.
Binary file malloc/.arena.c.swo matches
malloc/malloc.c:2403:      heap_info *old_heap, *heap;
malloc/malloc.c:3225:     heap_info *heap = heap_for_ptr (oldtop);
malloc/malloc.c:4087:   heap_info *heap = heap_for_ptr(top(av));
malloc/malloc.c:5177:     heap_info *heap = heap_for_ptr (top (ar_ptr));
Binary file malloc/.arena.c.swn matches
malloc/arena.c:48:typedef struct _heap_info
malloc/arena.c:51:  struct _heap_info *prev; /* Previous heap. */
malloc/arena.c:56:     that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of
malloc/arena.c:59:} heap_info;
malloc/arena.c:61:/* Get a compile-time error if the heap_info padding is not correct
malloc/arena.c:63:extern int sanity_check_heap_info_alignment[(sizeof (heap_info)
malloc/arena.c:126:  ((heap_info *) ((unsigned long) (ptr) & ~(HEAP_MAX_SIZE - 1)))
malloc/arena.c:493:dump_heap (heap_info *heap)
malloc/arena.c:535:static heap_info *
malloc/arena.c:542:  heap_info *h;
malloc/arena.c:604:  h = (heap_info *) p2;
malloc/arena.c:615:grow_heap (heap_info *h, long diff)
malloc/arena.c:643:shrink_heap (heap_info *h, long diff)
malloc/arena.c:681:heap_trim (heap_info *heap, size_t pad)
malloc/arena.c:686:  heap_info *prev_heap;
malloc/arena.c:776:  heap_info *h;

I also know that the macros heap_for_ptr(ptr) and top(ar_ptr) are used to locate this in code as follows:

42 #define top(ar_ptr) ((ar_ptr)->top)
...
 123 /* find the heap and corresponding arena for a given ptr */
 124 
 125 #define heap_for_ptr(ptr) \
 126   ((heap_info *) ((unsigned long) (ptr) & ~(HEAP_MAX_SIZE - 1)))
 127 #define arena_for_chunk(ptr) \
 128   (chunk_non_main_arena (ptr) ? heap_for_ptr (ptr)->ar_ptr : &main_arena)
 129 
 130 

But I'm having trouble finding this actual struct in memory during runtime. Does anyone know how to locate the heap_info structs in memory? Preferably using open source and widely available tools like (gdb).

Appreciate the help!

0 Answers
Related