gpt4 book ai didi

ios - mprotect errno 22 iOS

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:43 43 4
gpt4 key购买 nike

我正在 iOS 上开发越狱应用程序,调用时出现 errno 22

mprotect(p, 1024, PROT_READ | PROT_EXEC)

errno 22 表示参数无效,但我不知道出了什么问题。我已经将 p 对齐为页面大小的倍数,并且在调用 mprotect 之前我已经分配了内存。

这是我的代码和示例输出

#define PAGESIZE 4096

FILE * pFile;
pFile = fopen ("log.txt","w");

uint32_t code[] = {
0xe2800001, // add r0, r0, #1
0xe12fff1e, // bx lr
};

fprintf(pFile, "Before Execution\n");

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 %p\n", p);

p = (uint32_t *)(((uintptr_t)p + PAGESIZE-1) & ~(PAGESIZE-1));

fprintf(pFile, "Moved pointer to %p\n", p);

fprintf(pFile, "Before Compiling\n");

// copy instructions to function
p[0] = code[0];
p[1] = code[1];

fprintf(pFile, "After Compiling\n");

if (mprotect(p, 1024, PROT_READ | PROT_EXEC)) {
int err = errno;
fprintf(pFile, "Couldn't mprotect2: %i\n", 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() 解决了这个问题。原来我没有将指针正确对齐到页面大小

关于ios - mprotect errno 22 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148345/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com