没有合适的资源?快使用搜索试试~ 我知道了~
10张图22段代码万字长文带你搞懂虚拟内存模型和malloc内部原理.docx
需积分: 0 0 下载量 118 浏览量
2024-02-17
22:55:05
上传
评论
收藏 139KB DOCX 举报
温馨提示
试读
41页
malloc
资源推荐
资源详情
资源评论
10 张图 22 段代码,万字长文带你搞懂虚拟内
存模型和 malloc 内部原理
通过/proc 文件系统探究虚拟内存
我们会通过/proc 文件系统找到正在运行的进程的字符串所在的虚拟内存地址,并通过更改
此内存地址的内容来更改字符串内容,使你更深入了解虚拟内存这个概念!这之前先介绍下
虚拟内存的定义!
虚拟内存
虚拟内存是一种实现在计算机软硬件之间的内存管理技术,它将程序使用到的内存地址(虚
拟地址)映射到计算机内存中的物理地址,虚拟内存使得应用程序从繁琐的管理内存空间任
务中解放出来,提高了内存隔离带来的安全性,虚拟内存地址通常是连续的地址空间,由操
作系统的内存管理模块控制,在触发缺页中断时利用分页技术将实际的物理内存分配给虚拟
内存,而且 64 位机器虚拟内存的空间大小远超出实际物理内存的大小,使得进程可以使用
比物理内存大小更多的内存空间。
在深入研究虚拟内存前,有几个关键点:
· 每个进程都有它自己的虚拟内存
· 虚拟内存的大小取决于系统的体系结构
· 不同操作管理有着不同的管理虚拟内存的方式,但大多数操作系统的虚拟内存结构如
下图:
virtual_memory.png
上图并不是特别详细的内存管理图,高地址其实还有内核空间等等,但这不是这篇文章的主
题。从图中可以看到高地址存储着命令行参数和环境变量,之后是栈空间、堆空间和可执行
程序,其中栈空间向下延申,堆空间向上增长,堆空间需要使用 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]/mem
This file can be used to access the pages of a process's memory through open(2), read(2),
and lseek(2).
/proc/[pid]/maps
A file 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 offset dev inode pathname
00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
00651000-00652000 r--p 00051000 08:02 173521 /usr/bin/dbus-daemon
00652000-00655000 rw-p 00052000 08:02 173521 /usr/bin/dbus-daemon
00e03000-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.so
35b1a1f000-35b1a20000 r--p 0001f000 08:02 135522 /usr/lib64/ld-2.15.so
35b1a20000-35b1a21000 rw-p 00020000 08:02 135522 /usr/lib64/ld-2.15.so
35b1a21000-35b1a22000 rw-p 00000000 00:00 0
35b1c00000-35b1dac000 r-xp 00000000 08:02 135870 /usr/lib64/libc-2.15.so
35b1dac000-35b1fac000 ---p 001ac000 08:02 135870 /usr/lib64/libc-2.15.so
35b1fac000-35b1fb0000 r--p 001ac000 08:02 135870 /usr/lib64/libc-2.15.so
35b1fb0000-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 = read
w = write
x = execute
s = shared
p = 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. 0 indicates
that no inode is associated with the memory region,
as would be the case with BSS (uninitialized data).
The pathname field will usually be the file that is backing the mapping.
For ELF files, you can easily coordinate with the offset field
by 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 easy way to coordinate
this 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)
剩余40页未读,继续阅读
资源评论
omygodvv
- 粉丝: 503
- 资源: 1798
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功