gpt4 book ai didi

c - 在主线程的堆栈上调用mprotect总是失败

转载 作者:行者123 更新时间:2023-12-03 12:55:22 26 4
gpt4 key购买 nike

我试图在主线程的堆栈上调用mprotect,但是它总是失败。每次尝试时,都会产生ENOMEM错误。而且我已经在/proc/[pid]/maps中 checkin 了整个堆栈,并将它们映射到了地址空间中。但是,我可以从主堆栈成功地在子线程的堆栈上调用mprotect。主线程的堆栈必须有一些特殊之处,可以防止它被mprotect更改。但是我找不到任何文档。有人有主意吗?

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/mman.h>
#include <assert.h>
#include <pthread.h>
#include <stdio.h>

void * addr;
size_t size;

void * thread(void * tls) {
sleep(1);
if (mprotect(addr, size, PROT_NONE) == -1) perror(NULL);
return NULL;
}

int main(int argc, const char *argv[]) {
pthread_attr_t attr;
pthread_getattr_np(pthread_self(), &attr);
pthread_attr_getstack(&attr, &addr, &size);

pthread_t th;
pthread_create(&th, NULL, thread, NULL);
sleep(2);
pthread_join(th, NULL);
return 0;
}

最佳答案

您试图保护未映射的页面。如果检查pthread_attr_getstack的来源,您会发现没有mmap.Stack页面是按需映射的。 this might help you

关于c - 在主线程的堆栈上调用mprotect总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603166/

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