我正在iOS上开发一个越狱应用程序,调用时出现错误22
mprotect(p, 1024, PROT_READ | PROT_EXEC)
错误22的意思是无效的论点,但我不知道出了什么问题。我已经将p对齐为页面大小的倍数,并且在调用mprotect之前,我已经对内存进行了mallow处理。
这是我的代码和样本输出
#define PAGESIZE 4096
FILE * pFile;
pFile = fopen ("log.txt","w");
uint32_t code[] = {
0xe2800001, // add r0, r0, #1
0xe12fff1e, // bx lr
};
fprintf(pFile, "Before Executionn");
p = (uint32_t *)malloc(1024+PAGESIZE-1);
if (!p) {
fprintf(pFile, "Couldn't malloc(1024)");
perror("Couldn't malloc(1024)");
exit(errno);
}
fprintf(pFile, "Malloced to %pn", p);
p = (uint32_t *)(((uintptr_t)p + PAGESIZE-1) & ~(PAGESIZE-1));
fprintf(pFile, "Moved pointer to %pn", p);
fprintf(pFile, "Before Compilingn");
// copy instructions to function
p[0] = code[0];
p[1] = code[1];
fprintf(pFile, "After Compilingn");
if (mprotect(p, 1024, PROT_READ | PROT_EXEC)) {
int err = errno;
fprintf(pFile, "Couldn't mprotect2: %in", errno);
perror("Couldn't mprotect");
exit(errno);
}
输出:
Before Execution
Malloced to 0x13611ec00
Moved pointer 0x13611f000
Before Compiling
After Compiling
Couldn't mprotect2: 22
使用posix_memalign()修复了此问题。结果发现我没有正确地将指针与页面大小对齐