malloc(size_t n)
如果n是0,返回一个最小的chunk,在32位系统上是16字节,在64位系统上是24或32字节。
free(void *p)
如果p是null,则没有影响。如果p已经被释放,则可能有任意的影响,不要这样做。
malloc_trim(size_t pad)
将内存交还给系统,堆分配后,释放的内存只是还给堆,并没有还给系统,使用这个接口来显式地释放内存,参数是要保留的大小,0表示释放尽可能多的内存。返回1表示释放了内存,0表示没有释放任何内存。
malloc_usable_size(void *p)
返回实际可用的内存,实际分配的内存可能比请求的内存要大。
malloc_stats()
打印统计信息到stderr
M_TRIM_THRESHOLD
控制在使用malloc_trim释放前,最多未使用的内存。默认值是128K
获取内存分配信息
struct mallinfo mallinfo(void);
struct mallinfo {
int arena; /* Non-mmapped space allocated (bytes) */
int ordblks; /* Number of free chunks */
int smblks; /* Number of free fastbin blocks */
int hblks; /* Number of mmapped regions */
int hblkhd; /* Space allocated in mmapped regions (bytes) */
int usmblks; /* Maximum total allocated space (bytes) */
int fsmblks; /* Space in freed fastbin blocks (bytes) */
int uordblks; /* Total allocated space (bytes) */
int fordblks; /* Total free space (bytes) */
int keepcost; /* Top-most, releasable space (bytes) */
};
这个接口只查询main arena的信息。
参考
glibc malloc source code.
http://gee.cs.oswego.edu/dl/html/malloc.html
https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/
https://www.kernel.org/doc/html/latest/admin-guide/mm/pagemap.html
https://www.blackhat.com/presentations/bh-usa-07/Ferguson/Whitepaper/bh-usa-07-ferguson-WP.pdf
https://azeria-labs.com/heap-exploitation-part-1-understanding-the-glibc-heap-implementation/
https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/