我调用munmap
没有错误,但映射仍然可见 /proc/<pid>/maps
当我尝试 mmap(address_overlapping_with_what_I_tried_to_munmap, ...)
我没有得到我请求的地址。
这是错误还是功能?我可以做些什么来确保未映射某些内容吗?
详细信息:32 位 Linux 4.1.18
编辑
最初, map 条目是
bfe50000-bfe71000 rw-p 00000000 00:00 0
然后我调用munmap
带参数 0xbfe50000, 0x21000
入口是
bfe50000-bfe50000 rw-p 00000000 00:00 0
最佳答案
when I tried to mmap(address_overlapping_with_what_I_tried_to_munmap, ...) I did not get the address I requested.
再次仔细阅读mmap(2):
If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping
及以后:
MAP_FIXED
Don't interpret addr as a hint: place the mapping at exactly that address. addr must be a multiple of the page size.
因此,如果您确实需要将地址作为有用地址传递给 mmap
,您应该使用 MAP_FIXED
(谨慎,因为它可以覆盖并“粉碎”一个非常有用的预先存在的内存映射。
换句话说,我知道 mmap
的地址最好是 0,除非你说 MAP_FIXED
。否则,它只是一个“提示”,我不知道这实际上意味着什么(尤其是 ASLR )。
AFAIU,/proc/self/maps
或 /proc/1234/maps
反射(reflect)瞬间内核对您进程 virtual address space 的感知。当然,您需要打开
那个伪文件,快速和顺序阅读
,然后尽快关闭
它(不要将文件描述符保存到open
-ed /proc/self/maps
几秒钟)。如果您的进程在 open
和/proc/*/maps
文件的 close
是恕我直言 undefined behavior - 或者至少是 unspecified behavior - 你不应该依赖它。
但是 mmap
和 munmap
在该系统调用返回后立即生效;更改虚拟地址空间没有“延迟”。虚拟内存机制可能会产生一些延迟(例如,当从某个远程网络获取某些页面时......)但应用程序无法访问该延迟(进程将处于 D
状态)。另请阅读有关 thrashing 的信息。
关于linux munmap 不工作(或者至少不能立即工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36508965/