肥宅钓鱼网
当前位置: 首页 钓鱼百科

一文彻底搞懂虚拟内存模型和malloc内部原理(一文彻底搞懂虚拟内存模型和malloc内部原理)

时间:2023-08-21 作者: 小编 阅读量: 1 栏目名: 钓鱼百科

在深入研究虚拟内存前,有几个关键点:每个进程都有它自己的虚拟内存虚拟内存的大小取决于系统的体系结构不同操作管理有着不同的管理虚拟内存的方式,但大多数操作系统的虚拟内存结构如下图:上图并不是特别详细的内存管理图,高地址其实还有内核空间等等,但这不是这篇文章的主题。首先通过一个简单的C程序探究虚拟内存。

linux服务器开发相关视频解析:

90分钟了解Linux内存架构,numa的优势,slab的实现,vmalloc原理

epoll的网络模型,从redis,memcached到nginx,一起搞定

通过/proc文件系统探究虚拟内存

我们会通过/proc文件系统找到正在运行的进程的字符串所在的虚拟内存地址,并通过更改此内存地址的内容来更改字符串内容,使你更深入了解虚拟内存这个概念!这之前先介绍下虚拟内存的定义!

虚拟内存

虚拟内存是一种实现在计算机软硬件之间的内存管理技术,它将程序使用到的内存地址(虚拟地址)映射到计算机内存中的物理地址,虚拟内存使得应用程序从繁琐的管理内存空间任务中解放出来,提高了内存隔离带来的安全性,虚拟内存地址通常是连续的地址空间,由操作系统的内存管理模块控制,在触发缺页中断时利用分页技术将实际的物理内存分配给虚拟内存,而且64位机器虚拟内存的空间大小远超出实际物理内存的大小,使得进程可以使用比物理内存大小更多的内存空间。

在深入研究虚拟内存前,有几个关键点:

  • 每个进程都有它自己的虚拟内存
  • 虚拟内存的大小取决于系统的体系结构
  • 不同操作管理有着不同的管理虚拟内存的方式,但大多数操作系统的虚拟内存结构如下图:

上图并不是特别详细的内存管理图,高地址其实还有内核空间等等,但这不是这篇文章的主题。从图中可以看到高地址存储着命令行参数和环境变量,之后是栈空间、堆空间和可执行程序,其中栈空间向下延申,堆空间向上增长,堆空间需要使用malloc分配,是动态分配的内存的一部分。

首先通过一个简单的C程序探究虚拟内存。

#include <stdlib.h>#include <stdio.h>#include <string.h>/** * main - 使用strdup创建一个字符串的拷贝,strdup内部会使用malloc分配空间, * 返回新空间的地址,这段地址空间需要外部自行使用free释放 * * Return: EXIT_FAILURE if malloc failed. Otherwise EXIT_SUCCESS */int main(void){char *s;s = strdup("test_memory");if (s == NULL){fprintf(stderr, "Can't allocate mem with malloc\n");return (EXIT_FAILURE);}printf("%p\n", (void *)s);return (EXIT_SUCCESS);}编译运行:gcc -Wall -Wextra -pedantic -Werror main.c -o test; ./test输出:0x88f010

我的机器是64位机器,进程的虚拟内存高地址为0xffffffffffffffff, 低地址为0x0,而0x88f010远小于0xffffffffffffffff,因此大概可以推断出被复制的字符串的地址(堆地址)是在内存低地址附近,具体可以通过/proc文件系统验证.ls /proc目录可以看到好多文件,这里主要关注/proc/[pid]/mem和/proc/[pid]/maps

mem & maps

man proc/proc/[pid]/memThis file can be used to access the pages of a process's memory through open(2), read(2), and lseek(2)./proc/[pid]/mapsAfile containing the currently mapped memory regions and their access permissions.See mmap(2) for some further information about memory mappings.The format of the file is:address perms offsetdevinodepathname00400000-00452000 r-xp 00000000 08:02 173521/usr/bin/dbus-daemon00651000-00652000 r--p 00051000 08:02 173521/usr/bin/dbus-daemon00652000-00655000 rw-p 00052000 08:02 173521/usr/bin/dbus-daemon00e03000-00e24000 rw-p 00000000 00:00 0 [heap]00e24000-011f7000 rw-p 00000000 00:00 0 [heap]...35b1800000-35b1820000 r-xp 00000000 08:02 135522/usr/lib64/ld-2.15.so35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522/usr/lib64/ld-2.15.so35b1a20000-35b1a21000 rw-p 00020000 08:02 135522/usr/lib64/ld-2.15.so35b1a21000-35b1a22000 rw-p 00000000 00:00 035b1c00000-35b1dac000 r-xp 00000000 08:02 135870/usr/lib64/libc-2.15.so35b1dac000-35b1fac000 ---p 001ac000 08:02 135870/usr/lib64/libc-2.15.so35b1fac000-35b1fb0000 r--p 001ac000 08:02 135870/usr/lib64/libc-2.15.so35b1fb0000-35b1fb2000 rw-p 001b0000 08:02 135870/usr/lib64/libc-2.15.so...f2c6ff8c000-7f2c7078c000 rw-p 00000000 00:00 0[stack:986]...7fffb2c0d000-7fffb2c2e000 rw-p 00000000 00:00 0[stack]7fffb2d48000-7fffb2d49000 r-xp 00000000 00:00 0[vdso]The address field is the address space in the process that the mapping occupies.The perms field is a set of permissions:r = readw = writex = executes = sharedp = private (copy on write)The offset field is the offset into the file/whatever;dev is the device (major:minor); inode is the inode on that device.0indicatesthat no inode is associated with the memory region,as would be the case with BSS (uninitialized data).Thepathname field will usually be the file that is backing the mapping.For ELF files, you can easily coordinate with the offset fieldby looking at the Offset field in the ELF program headers (readelf -l).There are additional helpful pseudo-paths:[stack]The initial process's (also known as the main thread's) stack.[stack:<tid>] (since Linux 3.4)A thread's stack (where the <tid> is a thread ID).It corresponds to the /proc/[pid]/task/[tid]/ path.[vdso] The virtual dynamically linked shared object.[heap] The process's heap.If the pathname field is blank, this is an anonymous mapping as obtained via the mmap(2) function.There is no easywaytocoordinatethis back to a process's source, short of running it through gdb(1), strace(1), or similar.Under Linux 2.0 there is no field giving pathname.

通过mem文件可以访问和修改整个进程的内存页,通过maps可以看到进程当前已映射的内存区域,有地址和访问权限偏移量等,从maps中可以看到堆空间是在低地址而栈空间是在高地址. 从maps中可以看到heap的访问权限是rw,即可写,所以可以通过堆地址找到上个示例程序中字符串的地址,并通过修改mem文件对应地址的内容,就可以修改字符串的内容啦,程序:

#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>/*** main - uses strdup to create a new string, loops forever-ever ** Return: EXIT_FAILURE if malloc failed. Other never returns */int main(void){char *s;unsigned long int i;s = strdup("test_memory");if (s == NULL){fprintf(stderr, "Can't allocate mem with malloc\n");return (EXIT_FAILURE);}i = 0;while (s){printf("[%lu] %s (%p)\n", i, s, (void *)s);sleep(1);i;}return (EXIT_SUCCESS);}编译运行:gcc -Wall -Wextra -pedantic -Werror main.c -o loop; ./loop输出:[0] test_memory (0x21dc010)[1] test_memory (0x21dc010)[2] test_memory (0x21dc010)[3] test_memory (0x21dc010)[4] test_memory (0x21dc010)[5] test_memory (0x21dc010)[6] test_memory (0x21dc010)...

这里可以写一个脚本通过/proc文件系统找到字符串所在位置并修改其内容,相应的输出也会更改。首先找到进程的进程号

ps aux | grep ./loop | grep -v grepzjucad25420.00.04352636 pts/3S12:280:00 ./loop

2542即为loop程序的进程号,cat /proc/2542/maps得到

00400000-00401000 r-xp 00000000 08:01 811716/home/zjucad/wangzhiqiang/loop00600000-00601000 r--p 00000000 08:01 811716/home/zjucad/wangzhiqiang/loop00601000-00602000 rw-p 00001000 08:01 811716/home/zjucad/wangzhiqiang/loop021dc000-021fd000 rw-p 00000000 00:00 0[heap]7f2adae2a000-7f2adafea000 r-xp 00000000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f2adafea000-7f2adb1ea000 ---p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f2adb1ea000-7f2adb1ee000 r--p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f2adb1ee000-7f2adb1f0000 rw-p 001c4000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f2adb1f0000-7f2adb1f4000 rw-p 00000000 00:00 07f2adb1f4000-7f2adb21a000 r-xp 00000000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f2adb3fa000-7f2adb3fd000 rw-p 00000000 00:00 07f2adb419000-7f2adb41a000 r--p 00025000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f2adb41a000-7f2adb41b000 rw-p 00026000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f2adb41b000-7f2adb41c000 rw-p 00000000 00:00 07ffd51bb3000-7ffd51bd4000 rw-p 00000000 00:00 0[stack]7ffd51bdd000-7ffd51be0000 r--p 00000000 00:00 0[vvar]7ffd51be0000-7ffd51be2000 r-xp 00000000 00:00 0[vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0[vsyscall]

看见堆地址范围021dc000-021fd000,并且可读可写,而且021dc000<0x21dc010<021fd000,这就可以确认字符串的地址在堆中,在堆中的索引是0x10(至于为什么是0x10,后面会讲到),这时可以通过mem文件到0x21dc010地址修改内容,字符串输出的内容也会随之更改,这里通过python脚本实现此功能.

#!/usr/bin/env python3'''Locates and replaces the first occurrence of a string in the heapof a processUsage: ./read_write_heap.py PID search_string replace_by_stringWhere: - PID is the pid of the target process- search_string is the ASCII string you are looking to overwrite- replace_by_string is the ASCII string you want to replacesearch_string with'''import sysdef print_usage_and_exit():print('Usage: {} pid search write'.format(sys.argv[0]))sys.exit(1)# check usageif len(sys.argv) != 4:print_usage_and_exit()# get the pid from argspid = int(sys.argv[1])if pid <= 0:print_usage_and_exit()search_string = str(sys.argv[2])if search_string== "":print_usage_and_exit()write_string = str(sys.argv[3])if search_string== "":print_usage_and_exit()# open the maps and mem files of the processmaps_filename = "/proc/{}/maps".format(pid)print("[*] maps: {}".format(maps_filename))mem_filename = "/proc/{}/mem".format(pid)print("[*] mem: {}".format(mem_filename))# try opening the maps filetry:maps_file = open('/proc/{}/maps'.format(pid), 'r')except IOError as e:print("[ERROR] Can not open file {}:".format(maps_filename))print("I/O error({}): {}".format(e.errno, e.strerror))sys.exit(1)for line in maps_file:sline = line.split(' ')# check if we found the heapif sline[-1][:-1] != "[heap]":continueprint("[*] Found [heap]:")# parse lineaddr = sline[0]perm = sline[1]offset = sline[2]device = sline[3]inode = sline[4]pathname = sline[-1][:-1]print("\tpathname = {}".format(pathname))print("\taddresses = {}".format(addr))print("\tpermisions = {}".format(perm))print("\toffset = {}".format(offset))print("\tinode = {}".format(inode))# check if there is read and write permissionif perm[0] != 'r' or perm[1] != 'w':print("[*] {} does not have read/write permission".format(pathname))maps_file.close()exit(0)# get start and end of the heap in the virtual memoryaddr = addr.split("-")if len(addr) != 2: # never trust anyone, not even your OS :)print("[*] Wrong addr format")maps_file.close()exit(1)addr_start = int(addr[0], 16)addr_end = int(addr[1], 16)print("\tAddr start [{:x}] | end [{:x}]".format(addr_start, addr_end))# open and read memtry:mem_file = open(mem_filename, 'rb ')except IOError as e:print("[ERROR] Can not open file {}:".format(mem_filename))print("I/O error({}): {}".format(e.errno, e.strerror))maps_file.close()exit(1)# read heapmem_file.seek(addr_start)heap = mem_file.read(addr_end - addr_start)# find stringtry:i = heap.index(bytes(search_string, "ASCII"))except Exception:print("Can't find '{}'".format(search_string))maps_file.close()mem_file.close()exit(0)print("[*] Found '{}' at {:x}".format(search_string, i))# write the new stringprint("[*] Writing '{}' at {:x}".format(write_string, addr_starti))mem_file.seek(addr_starti)mem_file.write(bytes(write_string, "ASCII"))# close filesmaps_file.close()mem_file.close()# there is only one heap in our examplebreak

运行这个Python脚本

zjucad@zjucad-ONDA-H110-MINI-V3-01:~/wangzhiqiang$ sudo ./loop.py 2542 test_memory test_hello[*] maps: /proc/2542/maps[*] mem: /proc/2542/mem[*] Found [heap]:pathname = [heap]addresses = 021dc000-021fd000permisions = rw-poffset = 00000000inode = 0Addr start [21dc000] | end [21fd000][*] Found 'test_memory' at 10[*] Writing 'test_hello' at 21dc010

同时字符串输出的内容也已更改

[633] test_memory (0x21dc010)[634] test_memory (0x21dc010)[635] test_memory (0x21dc010)[636] test_memory (0x21dc010)[637] test_memory (0x21dc010)[638] test_memory (0x21dc010)[639] test_memory (0x21dc010)[640] test_helloy (0x21dc010)[641] test_helloy (0x21dc010)[642] test_helloy (0x21dc010)[643] test_helloy (0x21dc010)[644] test_helloy (0x21dc010)[645] test_helloy (0x21dc010)

实验成功.

通过实践画出虚拟内存空间分布图

再列出内存空间分布图

基本上每个人或多或少都了解虚拟内存的空间分布,那如何验证它呢,下面会提到.

【文章福利】需要C/CLinux服务器架构师学习资料加群812855908(资料包括C/C,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等)

堆栈空间

首先验证栈空间的位置,我们都知道C中局部变量是存储在栈空间的,malloc分配的内存是存储在堆空间,所以可以通过打印出局部变量地址和malloc的返回内存地址的方式来验证堆栈空间在整个虚拟空间中的位置.

#include <stdlib.h>#include <stdio.h>#include <string.h>/** * main - print locations of various elements * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){int a;void *p;printf("Address of a: %p\n", (void *)&a);p = malloc(98);if (p == NULL){fprintf(stderr, "Can't malloc\n");return (EXIT_FAILURE);}printf("Allocated space in the heap: %p\n", p);return (EXIT_SUCCESS);}编译运行:gcc -Wall -Wextra -pedantic -Werror main.c -o test; ./test输出:Address of a: 0x7ffedde9c7fcAllocated space in the heap: 0x55ca5b360670

通过结果可以看出堆地址空间在栈地址空间下面,整理如图:

可执行程序

可执行程序也在虚拟内存中,可以通过打印main函数的地址,并与堆栈地址相比较,即可知道可执行程序地址相对于堆栈地址的分布.

#include <stdlib.h>#include <stdio.h>#include <string.h>/** * main - print locations of various elements * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){int a;void *p;printf("Address of a: %p\n", (void *)&a);p = malloc(98);if (p == NULL){fprintf(stderr, "Can't malloc\n");return (EXIT_FAILURE);}printf("Allocated space in the heap: %p\n", p);printf("Address of function main: %p\n", (void *)main);return (EXIT_SUCCESS);}编译运行:gcc main.c -o test; ./test输出:Address of a: 0x7ffed846de2cAllocated space in the heap: 0x561b9ee8c670Address of function main: 0x561b9deb378a

由于main(0x561b9deb378a) < heap(0x561b9ee8c670) < (0x7ffed846de2c),可以画出分布图如下:

命令行参数和环境变量

程序入口main函数可以携带参数:

  • 第一个参数(argc): 命令行参数的个数
  • 第二个参数(argv): 指向命令行参数数组的指针
  • 第三个参数(env): 指向环境变量数组的指针

通过程序可以看见这些元素在虚拟内存中的位置:

#include <stdlib.h>#include <stdio.h>#include <string.h>/** * main - print locations of various elements * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(int ac, char **av, char **env){int a;void *p;int i;printf("Address of a: %p\n", (void *)&a);p = malloc(98);if (p == NULL){fprintf(stderr, "Can't malloc\n");return (EXIT_FAILURE);}printf("Allocated space in the heap: %p\n", p);printf("Address of function main: %p\n", (void *)main);printf("First bytes of the main function:\n\t");for (i = 0; i < 15; i){printf("x ", ((unsigned char *)main)[i]);}printf("\n");printf("Address of the array of arguments: %p\n", (void *)av);printf("Addresses of the arguments:\n\t");for (i = 0; i < ac; i){printf("[%s]:%p ", av[i], av[i]);}printf("\n");printf("Address of the array of environment variables: %p\n", (void *)env);printf("Address of the first environment variable: %p\n", (void *)(env[0]));return (EXIT_SUCCESS);}编译运行:gcc main.c -o test; ./test nihao hello输出:Address of a: 0x7ffcc154a748Allocated space in the heap: 0x559bd1bee670Address of function main: 0x559bd09807caFirst bytes of the main function:55 48 89 e5 48 83 ec 40 89 7d dc 48 89 75 d0Address of the array of arguments: 0x7ffcc154a848Addresses of the arguments:[./test]:0x7ffcc154b94f [nihao]:0x7ffcc154b956 [hello]:0x7ffcc154b95cAddress of the array of environment variables: 0x7ffcc154a868Address of the first environment variable: 0x7ffcc154b962

结果如下:main(0x559bd09807ca) < heap(0x559bd1bee670) < stack(0x7ffcc154a748) < argv(0x7ffcc154a848) < env(0x7ffcc154a868) < arguments(0x7ffcc154b94f->0x7ffcc154b95c6)(6为hello 1('\0')) < env first(0x7ffcc154b962)可以看出所有的命令行参数都是相邻的,并且紧接着就是环境变量.

argv和env数组地址是相邻的吗

上例中argv有4个元素,命令行中有三个参数,还有一个NULL指向标记数组的末尾,每个指针是8字节,8*4=32, argv(0x7ffcc154a848)32(0x20) = env(0x7ffcc154a868),所以argv和env数组指针是相邻的.

命令行参数地址紧随环境变量地址之后吗

首先需要获取环境变量数组的大小,环境变量数组是以NULL结束的,所以可以遍历env数组,检查是否为NULL,获取数组大小,代码如下:

#include <stdlib.h>#include <stdio.h>#include <string.h>/*** main - print locations of various elements ** Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS*/int main(int ac, char **av, char **env){int a;void *p;int i;int size;printf("Address of a: %p\n", (void *)&a);p = malloc(98);if (p == NULL){fprintf(stderr, "Can't malloc\n");return (EXIT_FAILURE);}printf("Allocated space in the heap: %p\n", p);printf("Address of function main: %p\n", (void *)main);printf("First bytes of the main function:\n\t");for (i = 0; i < 15; i){printf("x ", ((unsigned char *)main)[i]);}printf("\n");printf("Address of the array of arguments: %p\n", (void *)av);printf("Addresses of the arguments:\n\t");for (i = 0; i < ac; i){printf("[%s]:%p ", av[i], av[i]);}printf("\n");printf("Address of the array of environment variables: %p\n", (void *)env);printf("Address of the first environment variables:\n");for (i = 0; i < 3; i){printf("\t[%p]:\"%s\"\n", env[i], env[i]);}/* size of the env array */i = 0;while (env[i] != NULL){i;}i; /* the NULL pointer */size = i * sizeof(char *);printf("Size of the array env: %d elements -> %d bytes (0x%x)\n", i, size, size);return (EXIT_SUCCESS);}编译运行:gcc main.c -o test; ./test nihao hello输出:Address of a: 0x7ffd5ebadff4Allocated space in the heap: 0x562ba4e13670Address of function main: 0x562ba2f1881aFirst bytes of the main function:55 48 89 e5 48 83 ec 40 89 7d dc 48 89 75 d0Address of the array of arguments: 0x7ffd5ebae0f8Addresses of the arguments:[./test]:0x7ffd5ebae94f [nihao]:0x7ffd5ebae956 [hello]:0x7ffd5ebae95cAddress of the array of environment variables: 0x7ffd5ebae118Address of the first environment variables:[0x7ffd5ebae962]:"LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:"[0x7ffd5ebaef4e]:"HOSTNAME=3e8650948c0c"[0x7ffd5ebaef64]:"OLDPWD=/"Size of the array env: 11 elements -> 88 bytes (0x58)运算结果如下:root@3e8650948c0c:/ubuntu# bcbc 1.07.1Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'.obase=16ibase=1658 7ffd5ebae118(standard_in) 3: syntax error58 7FFD5EBAE1187FFD5EBAE170quit

通过结果可知7FFD5EBAE170 != 0x7ffd5ebae94f,所以命令行参数地址不是紧随环境变量地址之后。截至目前画出图表如下:

栈内存真的向下增长吗

可以通过调用函数来确认,如果真的是向下增长,那么调用函数的地址应该高于被调用函数地址, 代码如下:

#include <stdlib.h>#include <stdio.h>#include <string.h>void f(void){int a;int b;int c;a = 98;b = 1024;c = a * b;printf("[f] a = %d, b = %d, c = a * b = %d\n", a, b, c);printf("[f] Adresses of a: %p, b = %p, c = %p\n", (void *)&a, (void *)&b, (void *)&c);}int main(int ac, char **av, char **env){int a;void *p;int i;int size;printf("Address of a: %p\n", (void *)&a);p = malloc(98);if (p == NULL){fprintf(stderr, "Can't malloc\n");return (EXIT_FAILURE);}printf("Allocated space in the heap: %p\n", p);printf("Address of function main: %p\n", (void *)main);f();return (EXIT_SUCCESS);}编译运行:gcc main.c -o test; ./test输出:Address of a: 0x7ffefc75083cAllocated space in the heap: 0x564d46318670Address of function main: 0x564d45b9880e[f] a = 98, b = 1024, c = a * b = 100352[f] Adresses of a: 0x7ffefc7507ec, b = 0x7ffefc7507f0, c = 0x7ffefc7507f4

结果可知: f{a} 0x7ffefc7507ec < main{a} 0x7ffefc75083c可画图如下:

其实也可以写一个简单的代码,通过查看/proc文件系统中map内容来查看内存分布,这里就不举例啦.

堆内存(malloc)malloc

malloc是常用的动态分配内存的函数,malloc申请的内存分配在堆中,注意malloc是glibc函数,不是系统调用.man malloc:

[...] allocate dynamic memory[...]void *malloc(size_t size);[...]The malloc() function allocates size bytes and returns a pointer to the allocated memory.

不调用malloc,就不会有堆空间[heap]

看一段不调用malloc的代码

#include <stdlib.h>#include <stdio.h>/** * main - do nothing * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){getchar();return (EXIT_SUCCESS);}编译运行:gcc test.c -o 2; ./2step 1 : ps aux | grep \ \./2$输出:zjucad30230.00.04352788 pts/3S13:580:00 ./2step 2 : /proc/3023/maps输出:00400000-00401000 r-xp 00000000 08:01 811723/home/zjucad/wangzhiqiang/200600000-00601000 r--p 00000000 08:01 811723/home/zjucad/wangzhiqiang/200601000-00602000 rw-p 00001000 08:01 811723/home/zjucad/wangzhiqiang/2007a4000-007c5000 rw-p 00000000 00:00 0[heap]7f954ca02000-7f954cbc2000 r-xp 00000000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f954cbc2000-7f954cdc2000 ---p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f954cdc2000-7f954cdc6000 r--p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f954cdc6000-7f954cdc8000 rw-p 001c4000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7f954cdc8000-7f954cdcc000 rw-p 00000000 00:00 07f954cdcc000-7f954cdf2000 r-xp 00000000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f954cfd2000-7f954cfd5000 rw-p 00000000 00:00 07f954cff1000-7f954cff2000 r--p 00025000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f954cff2000-7f954cff3000 rw-p 00026000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7f954cff3000-7f954cff4000 rw-p 00000000 00:00 07ffed68a1000-7ffed68c2000 rw-p 00000000 00:00 0[stack]7ffed690e000-7ffed6911000 r--p 00000000 00:00 0[vvar]7ffed6911000-7ffed6913000 r-xp 00000000 00:00 0[vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0[vsyscall]

可以看到,如果不调用malloc,maps中就没有[heap]

下面运行一个带有malloc的程序

#include <stdio.h>#include <stdlib.h>/** * main - prints the malloc returned address * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;p = malloc(1);printf("%p\n", p);getchar();return (EXIT_SUCCESS);}编译运行:gcc test.c -o 3; ./3输出:0xcc7010验证步骤及输出:zjucad@zjucad-ONDA-H110-MINI-V3-01:~/wangzhiqiang$ ps aux | grep \ \./3$zjucad31130.00.04352644 pts/3S14:060:00 ./3zjucad@zjucad-ONDA-H110-MINI-V3-01:~/wangzhiqiang$ cat /proc/3113/maps00400000-00401000 r-xp 00000000 08:01 811726/home/zjucad/wangzhiqiang/300600000-00601000 r--p 00000000 08:01 811726/home/zjucad/wangzhiqiang/300601000-00602000 rw-p 00001000 08:01 811726/home/zjucad/wangzhiqiang/300cc7000-00ce8000 rw-p 00000000 00:00 0[heap]7fc7e9128000-7fc7e92e8000 r-xp 00000000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7fc7e92e8000-7fc7e94e8000 ---p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7fc7e94e8000-7fc7e94ec000 r--p 001c0000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7fc7e94ec000-7fc7e94ee000 rw-p 001c4000 08:01 8661324/lib/x86_64-linux-gnu/libc-2.23.so7fc7e94ee000-7fc7e94f2000 rw-p 00000000 00:00 07fc7e94f2000-7fc7e9518000 r-xp 00000000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7fc7e96f8000-7fc7e96fb000 rw-p 00000000 00:00 07fc7e9717000-7fc7e9718000 r--p 00025000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7fc7e9718000-7fc7e9719000 rw-p 00026000 08:01 8661310/lib/x86_64-linux-gnu/ld-2.23.so7fc7e9719000-7fc7e971a000 rw-p 00000000 00:00 07ffc91c18000-7ffc91c39000 rw-p 00000000 00:00 0[stack]7ffc91d5f000-7ffc91d62000 r--p 00000000 00:00 0[vvar]7ffc91d62000-7ffc91d64000 r-xp 00000000 00:00 0[vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0[vsyscall]

程序中带有malloc,那maps中就有[heap]段,并且malloc返回的地址在heap的地址段中,但是返回的地址却不再heap的最开始地址上,相差了0x10字节,为什么呢?看下面:

strace, brk, sbrk

malloc不是系统调用,它是一个正常函数,它必须调用某些系统调用才可以操作堆内存,通过使用strace工具可以追踪进程的系统调用和信号,为了确认系统调用是malloc产生的,所以在malloc前后添加write系统调用方便定位问题。

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/** * main - let's find out which syscall malloc is using * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;write(1, "BEFORE MALLOC\n", 14);p = malloc(1);write(1, "AFTER MALLOC\n", 13);printf("%p\n", p);getchar();return (EXIT_SUCCESS);}编译运行:gcc test.c -o 4zjucad@zjucad-ONDA-H110-MINI-V3-01:~/wangzhiqiang$ strace ./4execve("./4", ["./4"], [/* 34 vars */]) = 0brk(NULL) = 0x781000access("/etc/ld.so.nohwcap", F_OK)= -1 ENOENT (No such file or directory)access("/etc/ld.so.preload", R_OK)= -1 ENOENT (No such file or directory)open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3fstat(3, {st_mode=S_IFREG|0644, st_size=111450, ...}) = 0mmap(NULL, 111450, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f37720fa000close(3)= 0access("/etc/ld.so.nohwcap", F_OK)= -1 ENOENT (No such file or directory)open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f37720f9000mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f3771b27000mprotect(0x7f3771ce7000, 2097152, PROT_NONE) = 0mmap(0x7f3771ee7000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7f3771ee7000mmap(0x7f3771eed000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f3771eed000close(3)= 0mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f37720f8000mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f37720f7000arch_prctl(ARCH_SET_FS, 0x7f37720f8700) = 0mprotect(0x7f3771ee7000, 16384, PROT_READ) = 0mprotect(0x600000, 4096, PROT_READ)= 0mprotect(0x7f3772116000, 4096, PROT_READ) = 0munmap(0x7f37720fa000, 111450)= 0write(1, "BEFORE MALLOC\n", 14BEFORE MALLOC)= 14brk(NULL) = 0x781000brk(0x7a2000)= 0x7a2000write(1, "AFTER MALLOC\n", 13AFTER MALLOC)= 13fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0write(1, "0x781010\n", 90x781010)= 9fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0

最后几行的输出可知,malloc主要调用brk系统调用来操作堆内存.

man brk...int brk(void *addr);void *sbrk(intptr_t increment);...DESCRIPTIONbrk() and sbrk() change the location of the programbreak,whichdefinesthe end of the process's data segment (i.e., the program break is the firstlocation after the end of the uninitialized data segment).Increasingtheprogrambreak has the effect of allocating memory to the process; decreas‐ing the break deallocates memory.brk() sets the end of the data segment to the value specified by addr, whenthatvalueisreasonable,the system has enough memory, and the processdoes not exceed its maximum data size (see setrlimit(2)).sbrk() increments the program's data spacebyincrementbytes.Callingsbrk()withan increment

程序中断是虚拟内存中程序数据段结束后的第一个位置的地址,malloc通过调用brk或者sbrk,增加程序中断的值就可以创建新空间来动态分配内存,首次调用brk会返回当前程序中断的地址,第二次调用brk也会返回程序中断的地址,可以发现第二次brk返回地址大于第一次brk返回地址,brk就是通过增加程序中断地址的方式来分配内存,可以看出现在的堆地址范围是0x781000-0x7a2000,通过cat /proc/[pid]/maps也可以验证,此处就不贴上实际验证的结果啦。

多次malloc

如果多次malloc会出现什么现象呢,代码如下:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/** * main - many calls to malloc * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;write(1, "BEFORE MALLOC #0\n", 17);p = malloc(1024);write(1, "AFTER MALLOC #0\n", 16);printf("%p\n", p);write(1, "BEFORE MALLOC #1\n", 17);p = malloc(1024);write(1, "AFTER MALLOC #1\n", 16);printf("%p\n", p);write(1, "BEFORE MALLOC #2\n", 17);p = malloc(1024);write(1, "AFTER MALLOC #2\n", 16);printf("%p\n", p);write(1, "BEFORE MALLOC #3\n", 17);p = malloc(1024);write(1, "AFTER MALLOC #3\n", 16);printf("%p\n", p);getchar();return (EXIT_SUCCESS);}编译运行:gcc test.c -o 5; strace ./5摘要输出结果如下:write(1, "BEFORE MALLOC #0\n", 17BEFORE MALLOC #0)= 17brk(NULL) = 0x561605c7a000brk(0x561605c9b000) = 0x561605c9b000write(1, "AFTER MALLOC #0\n", 16AFTER MALLOC #0)= 16fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0write(1, "0x561605c7a260\n", 150x561605c7a260)= 15write(1, "BEFORE MALLOC #1\n", 17BEFORE MALLOC #1)= 17write(1, "AFTER MALLOC #1\n", 16AFTER MALLOC #1)= 16write(1, "0x561605c7aa80\n", 150x561605c7aa80)= 15write(1, "BEFORE MALLOC #2\n", 17BEFORE MALLOC #2)= 17write(1, "AFTER MALLOC #2\n", 16AFTER MALLOC #2)= 16write(1, "0x561605c7ae90\n", 150x561605c7ae90)= 15write(1, "BEFORE MALLOC #3\n", 17BEFORE MALLOC #3)= 17write(1, "AFTER MALLOC #3\n", 16AFTER MALLOC #3)= 16write(1, "0x561605c7b2a0\n", 150x561605c7b2a0)= 15fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0

可以发现并不是每次调用malloc都会触发brk系统调用,首次调用malloc,内部会通过brk系统调用更改程序中断地址,分配出一大块内存空间,后续再调用malloc,malloc内部会优先使用之前分配出来的内存空间,直到内部内存空间已经不够再次分配给外部时才会再次触发brk系统调用.

0x10 那丢失的16字节是什么

上面分析可以看见程序第一次调用malloc返回的地址并不是heap段的首地址,而是相差了0x10个字节,那这16个字节究竟是什么,可以通过程序打印出这前16个字节的内容.

编译运行:gcc test.c -o test;./test输出:0x5589436ce260bytes at 0x5589436ce250:00 00 00 00 00 00 00 00 11 04 00 00 00 00 00 000x5589436cea80bytes at 0x5589436cea70:00 00 00 00 00 00 00 00 11 08 00 00 00 00 00 000x5589436cf290bytes at 0x5589436cf280:00 00 00 00 00 00 00 00 11 0c 00 00 00 00 00 000x5589436cfea0bytes at 0x5589436cfe90:00 00 00 00 00 00 00 00 11 10 00 00 00 00 00 000x5589436d0eb0bytes at 0x5589436d0ea0:00 00 00 00 00 00 00 00 11 14 00 00 00 00 00 000x5589436d22c0bytes at 0x5589436d22b0:00 00 00 00 00 00 00 00 11 18 00 00 00 00 00 000x5589436d3ad0bytes at 0x5589436d3ac0:00 00 00 00 00 00 00 00 11 1c 00 00 00 00 00 000x5589436d56e0bytes at 0x5589436d56d0:00 00 00 00 00 00 00 00 11 20 00 00 00 00 00 000x5589436d76f0bytes at 0x5589436d76e0:00 00 00 00 00 00 00 00 11 24 00 00 00 00 00 000x5589436d9b00bytes at 0x5589436d9af0:00 00 00 00 00 00 00 00 11 28 00 00 00 00 00 00

可以看出规律:这16个字节相当于malloc出来的地址的头,包含一些信息,目前可以看出它包括已经分配的地址空间的大小,第一次malloc申请了0x400(1024)字节,可以发现11 04 00 00 00 00 00 00大于0x400(1024),这8个字节表示数字 0x 00 00 00 00 00 00 04 11 = 0x400(1024)0x10(头的大小16)1(后面会说明它的含义),可以发现每次调用malloc,这前8个字节代表的含义都是malloc字节数 16 1。可以猜测,malloc内部会把这前16个字节强转成某种数据结构,数据结构包含某些信息,最主要的是已经分配的字节数,尽管我们不了解具体结构,但是也可以通过代码操作这16个字节验证我们上面总结的规律是否正确,注意代码中不调用free释放内存.

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/*** pmem - print mem* @p: memory address to start printing from* @bytes: number of bytes to print** Return: nothing */void pmem(void *p, unsigned int bytes){unsigned char *ptr;unsigned int i;ptr = (unsigned char *)p;for (i = 0; i < bytes; i){if (i != 0){printf(" ");}printf("x", *(ptri));}printf("\n");}/** * main - confirm the source code * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;int i;size_t size_of_the_chunk;size_t size_of_the_previous_chunk;void *chunks[10];for (i = 0; i < 10; i){p = malloc(1024 * (i1));chunks[i] = (void *)((char *)p - 0x10);printf("%p\n", p);}free((char *)(chunks[3])0x10);free((char *)(chunks[7])0x10);for (i = 0; i < 10; i){p = chunks[i];printf("chunks[%d]: ", i);pmem(p, 0x10);size_of_the_chunk = *((size_t *)((char *)p8)) - 1;size_of_the_previous_chunk = *((size_t *)((char *)p));printf("chunks[%d]: %p, size = %li, prev = %li\n",i, p, size_of_the_chunk, size_of_the_previous_chunk);}return (EXIT_SUCCESS);}编译运行输出:root@3e8650948c0c:/ubuntu# gcc test.c -o testroot@3e8650948c0c:/ubuntu# ./test0x559721de42600x559721de4a800x559721de52900x559721de5ea00x559721de6eb00x559721de82c00x559721de9ad00x559721deb6e00x559721ded6f00x559721defb00chunks[0]: 00 00 00 00 00 00 00 00 11 04 00 00 00 00 00 00chunks[0]: 0x559721de4250, size = 1040, prev = 0chunks[1]: 00 00 00 00 00 00 00 00 11 08 00 00 00 00 00 00chunks[1]: 0x559721de4a70, size = 2064, prev = 0chunks[2]: 00 00 00 00 00 00 00 00 11 0c 00 00 00 00 00 00chunks[2]: 0x559721de5280, size = 3088, prev = 0chunks[3]: 00 00 00 00 00 00 00 00 11 10 00 00 00 00 00 00chunks[3]: 0x559721de5e90, size = 4112, prev = 0chunks[4]: 10 10 00 00 00 00 00 00 10 14 00 00 00 00 00 00chunks[4]: 0x559721de6ea0, size = 5135, prev = 4112chunks[5]: 00 00 00 00 00 00 00 00 11 18 00 00 00 00 00 00chunks[5]: 0x559721de82b0, size = 6160, prev = 0chunks[6]: 00 00 00 00 00 00 00 00 11 1c 00 00 00 00 00 00chunks[6]: 0x559721de9ac0, size = 7184, prev = 0chunks[7]: 00 00 00 00 00 00 00 00 11 20 00 00 00 00 00 00chunks[7]: 0x559721deb6d0, size = 8208, prev = 0chunks[8]: 10 20 00 00 00 00 00 00 10 24 00 00 00 00 00 00chunks[8]: 0x559721ded6e0, size = 9231, prev = 8208chunks[9]: 00 00 00 00 00 00 00 00 11 28 00 00 00 00 00 00chunks[9]: 0x559721defaf0, size = 10256, prev = 0

结果可以看出,malloc返回的地址往前的16个字节可以表示已经分配的内存大小, 如图:

注意上述是没有调用free释放内存的结果,然而malloc只用了8个字节表示已经分配的内存大小,那么另外8个字节被用来表示什么含义呢,看下malloc函数的注释:

1055 /*1056malloc_chunk details:10571058(The following includes lightly edited explanations by Colin Plumb.)10591060Chunks of memory are maintained using a `boundary tag' method as1061described in e.g., Knuth or Standish.(See the paper by Paul1062Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a1063survey of such techniques.)Sizes of free chunks are stored both1064in the front of each chunk and at the end.This makes1065consolidating fragmented chunks into bigger chunks very fast.The1066size fields also hold bits representing whether chunks are free or1067in use.10681069An allocated chunk looks like this:107010711072chunk->- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1073|Size of previous chunk, if unallocated (P clear)|1074- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1075|Size of chunk, in bytes |A|M|P|1076mem->- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1077|User data starts here....1078..1079.(malloc_usable_size() bytes).1080.|1081nextchunk->- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1082|(size of chunk, but used for application data)|1083- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1084|Size of next chunk, in bytes|A|0|1|1085- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 10861087Where "chunk" is the front of the chunk for the purpose of most of1088the malloc code, but "mem" is the pointer that is returned to the1089user."Nextchunk" is the beginning of the next contiguous chunk.

可以看出这16字节有两个含义,前8个字节表示之前的空间有多少没有被分配的字节大小,后8个字节表示当前malloc已经分配的字节大小,通过一段调用free的代码查看:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/*** pmem - print mem* @p: memory address to start printing from* @bytes: number of bytes to print ** Return: nothing*/void pmem(void *p, unsigned int bytes){unsigned char *ptr;unsigned int i;ptr = (unsigned char *)p;for (i = 0; i < bytes; i){if (i != 0){printf(" ");}printf("x", *(ptri));}printf("\n");}/** * main - confirm the source code * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;int i;size_t size_of_the_chunk;size_t size_of_the_previous_chunk;void *chunks[10];for (i = 0; i < 10; i){p = malloc(1024 * (i1));chunks[i] = (void *)((char *)p - 0x10);printf("%p\n", p);}free((char *)(chunks[3])0x10);free((char *)(chunks[7])0x10);for (i = 0; i < 10; i){p = chunks[i];printf("chunks[%d]: ", i);pmem(p, 0x10);size_of_the_chunk = *((size_t *)((char *)p8)) - 1;size_of_the_previous_chunk = *((size_t *)((char *)p));printf("chunks[%d]: %p, size = %li, prev = %li\n",i, p, size_of_the_chunk, size_of_the_previous_chunk);}return (EXIT_SUCCESS);}编译运行输出:root@3e8650948c0c:/ubuntu# gcc test.c -o testroot@3e8650948c0c:/ubuntu# ./test0x55fbebf202600x55fbebf20a800x55fbebf212900x55fbebf21ea00x55fbebf22eb00x55fbebf242c00x55fbebf25ad00x55fbebf276e00x55fbebf296f00x55fbebf2bb00chunks[0]: 00 00 00 00 00 00 00 00 11 04 00 00 00 00 00 00chunks[0]: 0x55fbebf20250, size = 1040, prev = 0chunks[1]: 00 00 00 00 00 00 00 00 11 08 00 00 00 00 00 00chunks[1]: 0x55fbebf20a70, size = 2064, prev = 0chunks[2]: 00 00 00 00 00 00 00 00 11 0c 00 00 00 00 00 00chunks[2]: 0x55fbebf21280, size = 3088, prev = 0chunks[3]: 00 00 00 00 00 00 00 00 11 10 00 00 00 00 00 00chunks[3]: 0x55fbebf21e90, size = 4112, prev = 0chunks[4]: 10 10 00 00 00 00 00 00 10 14 00 00 00 00 00 00chunks[4]: 0x55fbebf22ea0, size = 5135, prev = 4112chunks[5]: 00 00 00 00 00 00 00 00 11 18 00 00 00 00 00 00chunks[5]: 0x55fbebf242b0, size = 6160, prev = 0chunks[6]: 00 00 00 00 00 00 00 00 11 1c 00 00 00 00 00 00chunks[6]: 0x55fbebf25ac0, size = 7184, prev = 0chunks[7]: 00 00 00 00 00 00 00 00 11 20 00 00 00 00 00 00chunks[7]: 0x55fbebf276d0, size = 8208, prev = 0chunks[8]: 10 20 00 00 00 00 00 00 10 24 00 00 00 00 00 00chunks[8]: 0x55fbebf296e0, size = 9231, prev = 8208chunks[9]: 00 00 00 00 00 00 00 00 11 28 00 00 00 00 00 00chunks[9]: 0x55fbebf2baf0, size = 10256, prev = 0

程序代码通过free释放了3和7数据块的空间,所以4和8的前8个字节已经不全是0啦,和其它不同,它们表示之前数据块没有被分配的大小,也可以注意到4和8块的后8个字节不像其它块一样需要加1啦,可以得出结论,malloc通过是否加1来作为前一个数据块是否已经分配的标志,加1表示前一个数据块已经分配。所以之前的程序代码可以修改为如下形式:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/*** pmem - print mem* @p: memory address to start printing from* @bytes: number of bytes to print ** Return: nothing*/void pmem(void *p, unsigned int bytes){unsigned char *ptr;unsigned int i;ptr = (unsigned char *)p;for (i = 0; i < bytes; i){if (i != 0){printf(" ");}printf("x", *(ptri));}printf("\n");}/** * main - updating with correct checks * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;int i;size_t size_of_the_chunk;size_t size_of_the_previous_chunk;void *chunks[10];char prev_used;for (i = 0; i < 10; i){p = malloc(1024 * (i1));chunks[i] = (void *)((char *)p - 0x10);}free((char *)(chunks[3])0x10);free((char *)(chunks[7])0x10);for (i = 0; i < 10; i){p = chunks[i];printf("chunks[%d]: ", i);pmem(p, 0x10);size_of_the_chunk = *((size_t *)((char *)p8));prev_used = size_of_the_chunk & 1;size_of_the_chunk -= prev_used;size_of_the_previous_chunk = *((size_t *)((char *)p));printf("chunks[%d]: %p, size = %li, prev (%s) = %li\n",i, p, size_of_the_chunk,(prev_used? "allocated": "unallocated"), size_of_the_previous_chunk);}return (EXIT_SUCCESS);}编译运行输出:root@3e8650948c0c:/ubuntu# gcc test.c -o testroot@3e8650948c0c:/ubuntu# ./testchunks[0]: 00 00 00 00 00 00 00 00 11 04 00 00 00 00 00 00chunks[0]: 0x56254f888250, size = 1040, prev (allocated) = 0chunks[1]: 00 00 00 00 00 00 00 00 11 08 00 00 00 00 00 00chunks[1]: 0x56254f888660, size = 2064, prev (allocated) = 0chunks[2]: 00 00 00 00 00 00 00 00 11 0c 00 00 00 00 00 00chunks[2]: 0x56254f888e70, size = 3088, prev (allocated) = 0chunks[3]: 00 00 00 00 00 00 00 00 11 04 00 00 00 00 00 00chunks[3]: 0x56254f889a80, size = 1040, prev (allocated) = 0chunks[4]: 00 0c 00 00 00 00 00 00 10 14 00 00 00 00 00 00chunks[4]: 0x56254f88aa90, size = 5136, prev (unallocated) = 3072chunks[5]: 00 00 00 00 00 00 00 00 11 18 00 00 00 00 00 00chunks[5]: 0x56254f88bea0, size = 6160, prev (allocated) = 0chunks[6]: 00 00 00 00 00 00 00 00 11 1c 00 00 00 00 00 00chunks[6]: 0x56254f88d6b0, size = 7184, prev (allocated) = 0chunks[7]: 00 00 00 00 00 00 00 00 11 20 00 00 00 00 00 00chunks[7]: 0x56254f88f2c0, size = 8208, prev (allocated) = 0chunks[8]: 10 20 00 00 00 00 00 00 10 24 00 00 00 00 00 00chunks[8]: 0x56254f8912d0, size = 9232, prev (unallocated) = 8208chunks[9]: 00 00 00 00 00 00 00 00 11 28 00 00 00 00 00 00chunks[9]: 0x56254f8936e0, size = 10256, prev (allocated) = 0

堆空间是向上增长吗?

通过代码验证:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/** * main - moving the program break * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){int i;write(1, "START\n", 6);malloc(1);getchar();write(1, "LOOP\n", 5);for (i = 0; i < 0x25000 / 1024; i){malloc(1024);}write(1, "END\n", 4);getchar();return (EXIT_SUCCESS);}编译运行部分摘要输出:root@3e8650948c0c:/ubuntu# gcc test.c -o testroot@3e8650948c0c:/ubuntu# strace ./testexecve("./test", ["./test"], 0x7ffe0d7cbd80 /* 10 vars */) = 0brk(NULL) = 0x555a2428f000access("/etc/ld.so.nohwcap", F_OK)= -1 ENOENT (No such file or directory)access("/etc/ld.so.preload", R_OK)= -1 ENOENT (No such file or directory)openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3fstat(3, {st_mode=S_IFREG|0644, st_size=13722, ...}) = 0mmap(NULL, 13722, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6423455000close(3)= 0access("/etc/ld.so.nohwcap", F_OK)= -1 ENOENT (No such file or directory)openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6423453000mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f6422e41000mprotect(0x7f6423028000, 2097152, PROT_NONE) = 0mmap(0x7f6423228000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f6423228000mmap(0x7f642322e000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f642322e000close(3)= 0arch_prctl(ARCH_SET_FS, 0x7f64234544c0) = 0mprotect(0x7f6423228000, 16384, PROT_READ) = 0mprotect(0x555a22f5f000, 4096, PROT_READ) = 0mprotect(0x7f6423459000, 4096, PROT_READ) = 0munmap(0x7f6423455000, 13722) = 0write(1, "START\n", 6START)= 6brk(NULL) = 0x555a2428f000brk(0x555a242b0000) = 0x555a242b0000fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0read(0,"\n", 1024) = 1write(1, "LOOP\n", 5LOOP)= 5brk(0x555a242d1000) = 0x555a242d1000brk(0x555a242f2000) = 0x555a242f2000brk(0x555a24313000) = 0x555a24313000brk(0x555a24334000) = 0x555a24334000brk(0x555a24355000) = 0x555a24355000brk(0x555a24376000) = 0x555a24376000brk(0x555a24397000) = 0x555a24397000brk(0x555a243b8000) = 0x555a243b8000brk(0x555a243d9000) = 0x555a243d9000brk(0x555a243fa000) = 0x555a243fa000

可以看出堆空间是向上增长的.

随机化地址空间布局

从开始到现在运行了好多个进程,通过查看对应进程的maps,发现每个进程的heap的起始地址和可执行程序的结束地址都不紧邻,而且差距还每次都不相同.

[3718]: 01195000 – 00602000 = b93000[3834]: 024d6000 – 00602000 = 1ed4000[4014]: 00e70000 – 00602000 = 86e000[4172]: 01314000 – 00602000 = d12000[7972]: 00901000 – 00602000 = 2ff000

可以看出这个差值是随机的,查看fs/binfmt_elf.c源代码

if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {current->mm->brk = current->mm->start_brk =arch_randomize_brk(current->mm);#ifdef compat_brk_randomizedcurrent->brk_randomized = 1;#endif}// current->mm->brk是当前进程程序中断的地址

arch_randomize_brk函数在arch/x86/kernel/process.c中

unsigned long arch_randomize_brk(struct mm_struct *mm){unsigned long range_end = mm->brk0x02000000;return randomize_range(mm->brk, range_end, 0) ? : mm->brk;}

randomize_range函数在drivers/char/random.c中

/* * randomize_range() returns a start address such that * *[...... <range> .....] *startend * * a <range> with size "len" starting at the return value is inside in the * area defined by [start, end], but is otherwise randomized. */unsigned longrandomize_range(unsigned long start, unsigned long end, unsigned long len){unsigned long range = end - len - start;if (end <= startlen)return 0;return PAGE_ALIGN(get_random_int() % rangestart);}

可以看出上面所说的这个差值其实就是0-0x02000000中的一个随机数,这种技术称为ASLR(Address Space Layout Randomisation),是一种计算机安全技术,随机安排虚拟内存中堆栈空间的位置,可以有效防止黑客攻击。通过以上分析,可以画出内存分布图如下:

malloc(0)发生了什么?

当调用malloc(0)会发生什么,代码如下:

#include <stdio.h>#include <stdlib.h>#include <unistd.h>/*** pmem - print mem* @p: memory address to start printing from* @bytes: number of bytes to print ** Return: nothing*/void pmem(void *p, unsigned int bytes){unsigned char *ptr;unsigned int i;ptr = (unsigned char *)p;for (i = 0; i < bytes; i){if (i != 0){printf(" ");}printf("x", *(ptri));}printf("\n");}/** * main - moving the program break * * Return: EXIT_FAILURE if something failed. Otherwise EXIT_SUCCESS */int main(void){void *p;size_t size_of_the_chunk;char prev_used;p = malloc(0);printf("%p\n", p);pmem((char *)p - 0x10, 0x10);size_of_the_chunk = *((size_t *)((char *)p - 8));prev_used = size_of_the_chunk & 1;size_of_the_chunk -= prev_used;printf("chunk size = %li bytes\n", size_of_the_chunk);return (EXIT_SUCCESS);}编译运行输出如下:root@3e8650948c0c:/ubuntu# gcc test.c -o testroot@3e8650948c0c:/ubuntu# ./test0x564ece64b26000 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00chunk size = 32 bytes

可以看出malloc(0)实际使用了32个字节,其中包括我们之前说的16个字节头部,然而有时候malloc(0)可能会有不同的结果输出,也有可能会返回NULL.

manmallocNULLmayalsobereturnedbyasuccessfulcalltomalloc()withasizeofzero

操作环境

示例代码主要在两种环境下跑过:ubuntu16.04gcc(Ubuntu7.4.0-1ubuntu1~16.04~ppa1)7.4.0ubuntu18.04dockergcc(Ubuntu7.4.0-1ubuntu1~18.04.1)7.4.0

    推荐阅读
  • 学群建设教学(东小店学习第三步)

    微信群能够直接接触到用户,并能够高效沟通。因为微信有规定拉群好友不满足40人时,不需要对方同意,也就是说可以直接创建一个40人微信群。微信群的名称可以设置为:京东内购优惠、京东秒杀特价...等。这个群是为了分享京东购物优惠信息的,换句话说你是在帮助他人谋福利。开通智能助手:当你的微信群人数达到80人后,可以联系在线客服开通智能助手。所以一定要多花点时间在微信群上面!

  • 孙悟空的暴击吸血铭文(孙悟空的三个铭文)

    孙悟空的暴击吸血铭文暴击+吸血铭文这套铭文同样是将官方推荐的蓝色兽痕换掉,更改为夺萃,其它两色必要的铭文保持不变,铭文更改后,夺萃为猴子增加的总体属性为:16%的物理吸血效果。游戏中一件吸血装备末世的吸血效果也只有10%而已,给猴子增加了16%的物理吸血效果就意味着猴子在出装选择下不再需要选择出吸血装备,而可以有更多的选择,比如破军,这对于猴子来说,在伤害输出方面更上一个等级。

  • 致伤害我的人一句话(有什么致伤害我的人一句话)

    致伤害我的人一句话好好去爱,去生活。记住,每天的太阳都是新的,不要辜负了美好的晨光。花用尽全力,才在一年中盛放一次,美艳,而短暂,就像青春,不同的是,花有很多个一年,我们却只有一个一辈子。人生最大的勇敢之一,就是经历欺骗和伤害之后,还能保持信任和爱的能力。心累,就是常常徘徊在坚持和放弃之间,举棋不定。能给一个太平洋的永远是自己。人间事往往如此,当时提起痛不欲生,几年之后,也不过是一场回忆而已。

  • 2021重庆黔江生源地助学贷款怎么办理 黔西县生源地助学贷款2021

    每年12月21日根据最新LPR5Y调整一次。助学贷款的还款结算日为每月的20日,每月的15日前还款;每月的16日及以后还款,次月的20日结算。未到高校就读或因故确实不需要贷款的学生及时与黔江区学生资助管理中心联系取消贷款,凡当年10月30日前未录入受理证明回执校验码的视为自动放弃贷款。贷款学生有义务敦促高校及时将受理证明录入系统进行确认。违约学生将承担诉讼费、律师费及迫讨贷款产生的其他相关费用。

  • 广州公交复学线路(广州这些公交站点及线路有临时调整)

    广州市2022年初中学业水平考试将于6月20日至22日进行。为确保考试期间市内各考场的考试环境良好,广州市交通运输部门将在考试期间对考场附近部分公交站点及线路进行临时调整,具体如下:一、6月22日8:30至11:00,临时撤销公共汽车西华路站(双向),原途经线路取消停靠以上站点。

  • 航空公司网上值机选座位(轻松搞定英文网上值机)

    当我们乘坐长途航班出国,很可能会乘坐国外航空公司的班机,网上值机多半是英文页面。有些航司则要等到该航班起飞前24小时,开放网上值机的时候,才能选座位。起飞前一天登录官网找到网上值机的入口。确认行程摘要进入JourneySummary行程摘要页,出现乘客名单及航班信息。显示电子登记牌,完成网上值机联程航班,2张登机牌最后出现带二维码的电子登机牌,网上值机、选座位都完成了。有些航班,在网上值机时不能选择座位。

  • gta如何设置画面(gta5怎么调画质)

    gta如何设置画面?我们一起去了解并探讨一下这个问题吧!gta如何设置画面《GTA5》这款游戏需要的配置要求并不高,而且优化非常好,不需要太高的配置就可以高画质流畅运行,而配置比较低的话就需要一些调整画质减轻模糊程度的方法了,首先是把画质全部调最高,更多如下。如果配置不太行的话建议调低分辨率,这样基本上就能玩了

  • 融资是机构吗 融资公司是金融机构吗

    政府机构债券是介于公司债券和国债之间的融资形式,它虽然不像国债有政府法律担保,但是政府不会放任违约事件的发生,因此它具有事实上的担保性质。政府机构债券一般流通性较弱,因此利率高于流通性很强的国债作为补偿。

  • 古代日本人的三大习俗(日本人奇特的光荣)

    日本人奇特的光荣一起闻屁味儿的才是朋友笔者常常对各国的奇特风俗和文化现象感到好奇,所以只要碰到相关书籍,无论是专业书还是通俗读物、杂志,都贪读不厌我在此谈及我广泛的读书兴趣,不是想夸耀我的阅读力,而是因为我从杂读中发。

  • 剥白果的快速方法(白果怎么去壳比较快)

    可以之后放在热水中煮熟,它便会自行脱落。但可以把果仁保护的最好,用钳子夹可能会对果仁外观造成破坏。将整颗白果数枚放入容器,和盘放入微波炉中,开大稍等十分钟,待听到噼噼啪啪的声音,表示白果皮已经悉数破碎,这时候就可以取出备用。准备好白果,用左手稳拿银杏果大头一端,小尖头一端往下按住,右手拿刀,用刀背敲击小尖头部位,力度要轻柔,敲击小尖头至碎。