Why is the heap section present when there are no malloc used

无人久伴 提交于 2020-02-07 07:23:07

问题


I am trying to find out whether heap exists in the address space of the process, if we don't call malloc.

#include <stdio.h>
int main()
{
    getchar();
    return 0;
}

Heap section is present in the maps even if i dont call malloc

cat /proc/73268/maps
55d0b405c000-55d0b4160000 r-xp 00000000 08:01 5505031                    /bin/bash
55d0b435f000-55d0b4363000 r--p 00103000 08:01 5505031                    /bin/bash
55d0b4363000-55d0b436c000 rw-p 00107000 08:01 5505031                    /bin/bash
55d0b436c000-55d0b4376000 rw-p 00000000 00:00 0 
55d0b567e000-55d0b5815000 rw-p 00000000 00:00 0                          [heap]
7f57533fd000-7f5753408000 r-xp 00000000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753408000-7f5753607000 ---p 0000b000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753607000-7f5753608000 r--p 0000a000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753608000-7f5753609000 rw-p 0000b000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753609000-7f575360f000 rw-p 00000000 00:00 0 
7f575360f000-7f5753626000 r-xp 00000000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753626000-7f5753825000 ---p 00017000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753825000-7f5753826000 r--p 00016000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753826000-7f5753827000 rw-p 00017000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753827000-7f5753829000 rw-p 00000000 00:00 0 
7f5753829000-7f5753834000 r-xp 00000000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753834000-7f5753a33000 ---p 0000b000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a33000-7f5753a34000 r--p 0000a000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a34000-7f5753a35000 rw-p 0000b000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a35000-7f5753a3d000 r-xp 00000000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753a3d000-7f5753c3d000 ---p 00008000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3d000-7f5753c3e000 r--p 00008000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3e000-7f5753c3f000 rw-p 00009000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3f000-7f575460e000 r--p 00000000 08:01 793285                     /usr/lib/locale/locale-archive
7f575460e000-7f57547f5000 r-xp 00000000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57547f5000-7f57549f5000 ---p 001e7000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549f5000-7f57549f9000 r--p 001e7000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549f9000-7f57549fb000 rw-p 001eb000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549fb000-7f57549ff000 rw-p 00000000 00:00 0 
7f57549ff000-7f5754a02000 r-xp 00000000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754a02000-7f5754c01000 ---p 00003000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c01000-7f5754c02000 r--p 00002000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c02000-7f5754c03000 rw-p 00003000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c03000-7f5754c28000 r-xp 00000000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754c28000-7f5754e28000 ---p 00025000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e28000-7f5754e2c000 r--p 00025000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e2c000-7f5754e2d000 rw-p 00029000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e2d000-7f5754e54000 r-xp 00000000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5754fb6000-7f575503c000 rw-p 00000000 00:00 0 
7f575504d000-7f5755054000 r--s 00000000 08:01 1050910                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f5755054000-7f5755055000 r--p 00027000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5755055000-7f5755056000 rw-p 00028000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5755056000-7f5755057000 rw-p 00000000 00:00 0 
7ffd41cc9000-7ffd41cea000 rw-p 00000000 00:00 0                          [stack]
7ffd41db7000-7ffd41dba000 r--p 00000000 00:00 0                          [vvar]
7ffd41dba000-7ffd41dbc000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

I tried this on Ubuntu 18.04 and gcc version is 7.4.0, can you guys help me understand why is the heap section created even if we dont have any malloc.


回答1:


try this

int main()
{
        for(;;);
}

And compile it with gcc test.c -nodefaultlibs -Wl,-e,main -o test -nostdlib -static




回答2:


Your program is using malloc(), at least when using the standard glibc from your system:

$ cat failmalloc.c
#include <unistd.h>
#define MSG     "out of memory!\n"
void *malloc(size_t z){
        write(2, MSG, sizeof MSG - 1); _exit(13);
}
$ cc -Wall -shared failmalloc.c -o failmalloc.so
$ LD_PRELOAD=./failmalloc.so ./your_program
out of memory!

Also, there's no guarantee that malloc() will be using the "heap". There are malloc() implementations based exclusively on mmap(2).



来源:https://stackoverflow.com/questions/57300908/why-is-the-heap-section-present-when-there-are-no-malloc-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!