我正在尝试编写一个程序,在该程序中,我分配一个内存块,然后有选择地更改该块的页大小和页对齐的子集的保护。但是,当我尝试对内存块中超过8个页面的部分调用mprotect时,mprotect失败,并显示错误"Cannot allocate“。
下面是一个最小的、完整的、可验证的示例,它再现了这个问题:
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <cerrno>
#include <cstring>
#include <iostream>
int main() {
const int n_pages = 12;
const int page_size = sysconf(_SC_PAGE_SIZE); // 4096
const int block_size = n_pages * page_size; // 65536
char* block_addr = (char*)aligned_alloc(page_size, block_size);
char* chunks[n_pages];
char* pointer = block_addr;
for (int n = 0; n < n_pages; n++) {
pointer = pointer + (n * page_size);
chunks[n] = pointer;
}
std::cout << "Set chunks read-only.\n";
for (int n = 0; n < n_pages; n++) {
if (mprotect(chunks[n], page_size, PROT_READ) != 0) {
std::cerr << n+1 << '/' << n_pages << ' '
<< "mprotect failed: " << std::strerror(errno) << '\n';
}
}
std::cout << "Set chunks read/write.\n";
for (int n = 0; n < n_pages; n++) {
if (mprotect(chunks[n], page_size, PROT_READ|PROT_WRITE) != 0) {
std::cerr << n+1 << '/' << n_pages << ' '
<< "mprotect failed: " << std::strerror(errno) << '\n';
}
}
free(block_addr);
}对于chunks n>8,此操作始终失败,并给出以下消息:
Set chunks read-only.
9/12 mprotect failed: Cannot allocate memory
10/12 mprotect failed: Cannot allocate memory
11/12 mprotect failed: Cannot allocate memory
12/12 mprotect failed: Cannot allocate memory
Set chunks read/write.
9/12 mprotect failed: Cannot allocate memory
10/12 mprotect failed: Cannot allocate memory
11/12 mprotect failed: Cannot allocate memory
12/12 mprotect failed: Cannot allocate memory我发现了一个question,其中OP似乎得到了与我相同的错误,其中David Hammen提供了一些有用的提示来指出问题的根源,但我并不真正理解他在说什么。不幸的是,OP不提供他们的代码,所以我们不能确切知道他们在做什么,或者他们是如何修复它的。
所以基本上我的问题是:为什么mprotect会产生这个错误,我如何修复它?
发布于 2016-08-13 17:48:12
pointer = pointer + (n * page_size);这条线看起来很可疑。它可能应该是
pointer = block_addr + (n * page_size);或
chunks[n] = pointer; // need to assign to chunks[n] first
pointer = pointer + page_size;否则pointer将离开(步长为0,1 (= 0+ 1),3 (= 0+1+ 2),6 (= 0+1+2+ 3),...)进入不属于你的进程的内存,因为你没有分配它(即它是“未映射的”)。这就解释了为什么mprotect在最后几个块中失败了。
https://stackoverflow.com/questions/38931295
复制相似问题