简体   繁体   English

如何分配具有执行权限的内存?

[英]How to allocate a memory with execute permissions?

I want to allocate a memory with execute permissions. 我想分配一个具有执行权限的内存。 So I use mprotect to change the permissions.. To get a page aligned memory I use a valloc function. 所以我使用mprotect来更改权限。为了获得页面对齐的内存,我使用了一个valloc函数。

void * temp = (void *) valloc(x);

and then 然后

if( mprotect(temp, BLOCK_SIZE, (PROT_READ | PROT_WRITE |PROT_EXEC))) {
   exit(-1);
}

Now I want to add more memory to this allocated block. 现在我想为这个分配的块添加更多内存。 Hence I use a realloc function. 因此我使用realloc函数。

void * new_temp = (void *) realloc(temp, 1024);

Will this reallocate automatically change the permissions of the allocated memory to the ones I had set earlier ?? 这个重新分配会自动将分配的内存的权限更改为我之前设置的内存吗? In case realloc moves the entire block to a different location, what be the permissions of allocated memory earlier and the newly allocated memory? 如果realloc将整个块移动到另一个位置,那么先前分配的内存的权限和新分配的内存是什么?

Should mprotect be used again to get execute permissions memory. 应该再次使用mprotect来获取执行权限内存。 And is there a API to realloc on page size boundary like valloc . 而且有一个API realloc像页面大小边界上valloc ?

Try allocating a new region with another valloc , and copying the old contents across. 尝试使用另一个valloc分配新区域,并复制旧内容。 Better yet, stop using the deprecated valloc , and replace it with either posix_memalign calls, or directly mmap for very large allocations. 更好的是,停止使用已弃用的valloc ,并将其替换为posix_memalign调用,或直接使用mmap进行非常大的分配。 Using mremap you could effectively realloc page-aligned memory regions. 使用mremap你可以有效realloc页对齐的内存区域。

Should mprotect be used again to get execute permissions memory. 应该再次使用mprotect来获取执行权限内存。

Virtual memory is organized in pages. 虚拟内存按页面组织。 mprotect() changes the flags on all pages in the given block of virtual memory. mprotect()更改给定虚拟内存块中所有页面上的标志。 It is independent from the actual memory allocation. 它独立于实际的内存分配。 IOW, you have to call mprotect() again after realloc to reapply the permissions. IOW,你必须在realloc之后再次调用mprotect()来重新应用权限。 And you have to call it again for the whole region, since realloc() can instead of extending the existing block return pointer to a new one. 而且你必须再次为整个区域调用它,因为realloc()可以代替将现有的块返回指针扩展到新的。

Thinking about it now, I think one might need to call mprotect() before the realloc() to remove the exec permissions from the old memory region. 现在考虑一下,我认为可能需要在realloc()之前调用mprotect() realloc()来从旧内存区域中删除exec权限。 malloc() / realloc() are libc functions for management of memory inside the application's virtual memory, while mprotect() is a syscall operating independently on the application's virtual memory itself. malloc() / realloc()是用于管理应用程序虚拟内存中内存的libc函数,而mprotect()是一个独立于应用程序虚拟内存本身运行的系统调用。

And is there a API to realloc on page size boundary like valloc. 是否有一个API来重新分配页面大小边界,如valloc。 ?

Very much doubt it. 非常怀疑它。

In memory allocation intensive application, realloc() is rarely capable of extending the existing block and often ends up allocating new block + memcpy() + free old block. 在内存分配密集型应用程序中, realloc()很少能够扩展现有的块,并且通常最终会分配新的块+ memcpy()+ free旧块。 If realloc() performance was acceptable before, than its hand-coded version (taking stricter alignment into account) should be fine too. 如果之前realloc()性能是可接受的,那么它的手工编码版本(考虑更严格的对齐)也应该没问题。

BTW, POSIXv6 has a new function called posix_memalign() . BTW,POSIXv6有一个名为posix_memalign()的新函数。 valloc's man page is an interesting read, mostly why one shouldn't use the valloc() in the first place. valloc的手册页是一个有趣的读物,主要是为什么不应该首先使用valloc()。

PS Also you can always use standard POSIX function to find the page size sysconf(_SC_PAGESIZE); PS你也可以随时使用标准POSIX函数来查找页面大小sysconf(_SC_PAGESIZE); and align memory buffer yourself. 并自己对齐内存缓冲区。 Obviously you have to allocate new_size+(sysconf(_SC_PAGESIZE)-1) bytes to have sufficient memory to realign the pointer. 显然你必须分配new_size+(sysconf(_SC_PAGESIZE)-1)字节才能有足够的内存来重新对齐指针。

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

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