简体   繁体   English

如何使用munmap自定义malloc

[英]How to use munmap custom malloc

I'm trying to recode malloc and free functions but i got a problem with munmap.. 我正在尝试重新编码malloc和免费功能,但我遇到了munmap的问题..

With my custom malloc, I create a pool of memory with mmap (Ex : 4Kb), so I can return an index of this pool as an address when my malloc is called. 使用我的自定义malloc,我使用mmap创建一个内存池(例如:4Kb),因此我可以在调用malloc时将此池的索引作为地址返回。

This work fine, but when I use my custom free (Call to munmap) then I want to allocate something else with my custom malloc, I got a segfault, like if my ENTIRE pool was disallocated by munmap.. 这工作正常,但是当我使用我的自定义免费(调用munmap)然后我想用我的自定义malloc分配其他东西时,我得到了一个段错误,就像我的整个池被munmap取消分配一样。

Example: 例:

Ask 1024 bytes to my malloc
-> First call so malloc allocate a pool of 4 * pagesize() (So 16 000     
bytes)
-> Return to me an addr than I use. (addr[0] = 42)
-> Free with munmap this address (munmap(addr, 1024))
-> re ask to my malloc 1024 bytes
-> Try to fill it with something and segfault.

I don't really understand what is happening. 我真的不明白发生了什么。 Does munmap delete all my pool ? munmap是否删除了我的所有池?

Sorry for the poor english.. 对不起英语很差..

You are unmapping the entire page. 您正在取消整个页面的取消映射。

The address addr must be a multiple of the page size. 地址addr必须是页面大小的倍数。 All pages containing a part of the indicated range are unmapped, and subsequent references to these pages will generate SIGSEGV. 包含指定范围的一部分的所有页面都是未映射的,随后对这些页面的引用将生成SIGSEGV。 It is not an error if the indicated range does not contain any mapped pages. 如果指示的范围不包含任何映射页面,则不是错误。

munmap(2) munmap(2)

So when you munmap your first allocation, you unmapped the entire first page. 因此,当您对第一次分配进行munmap时,您将取消映射整个第一页。 You should wait to unmap only when the entire page is deallocated.. Or just not unmap at all - Just be sure two processes don't get memory from the same page, so there are no security vulnerabilities. 您应该等待仅在整个页面被取消分配时取消映射..或者根本不取消映射 - 只需确保两个进程不从同一页面获取内存,因此没有安全漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM