C SIGSEGV Handler &保护 [英] C SIGSEGV Handler & Mprotect

查看:17
本文介绍了C SIGSEGV Handler &保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个程序,它使用 mprotect() 来限制访问一块内存.当请求内存时,会抛出一个 SIGSEGV,我使用 signal() 调用来监听它.

I'm constructing a program which uses mprotect() to restrict a block of memory from accessing. When the memory is requested, a SIGSEGV is thrown which I listen for using a signal() call.

一旦检测到 SIGSEGV,我需要以某种方式访问​​指向所请求内存(引发故障)的指针以及所请求段的大小.这可能吗?

Once the SIGSEGV has been detected, I need to somehow access the pointer to the memory that was requested (that threw the fault) and the size of the segment requested. Is this possible?

void fifoSigHandler(){

    // Needs to only remove protection from requested block of virtual memory
    mprotect(fifoVm,(size_t)fifoVm_size,PROT_WRITE);
    printf("Caught Seg Fault");
}

void fifo_init(void* vm, int vm_size, int n_frames, int page_size)
{
    fifoVm = vm;
    fifoVm_size = vm_size;
    fifoFrames = n_frames;
    fifoPageSize = page_size;

    mprotect(fifoVm,(size_t)fifoVm_size,PROT_NONE);

    signal(SIGSEGV, fifoSigHandler);
}

另外,有没有办法确定当前分配的内存块的 mprotect() 级别(PROT_NONE、PROT_READ 等)?

Additionally, is there a way to determine the level of mprotect() a block of memory is currently assigned (PROT_NONE,PROT_READ, etc..)?

推荐答案

你必须使用 sigactionSA_SIGINFO 而不是 signal 来建立您的处理程序,然后您将在 siginfo_t 中收到有用信息的回调,包括 si_addr.

You have to use sigaction with SA_SIGINFO instead of signal to establish your handler, and then you will get called back with useful information in a siginfo_t, including si_addr.

si_addr,如 sigaction(2) 中所述,将包含地址.至于长度,除非你愿意解析指令,否则你就不走运了.你能做的最好的就是对 si_addr 中报告的页面采取行动,然后如果这还不够,你很快就会得到另一个信号.至少,我们在 ObjectStore 中是这样处理的.

si_addr, as explained in sigaction(2), will contain the address. As for the length, well, you're out of luck unless you're willing to parse instructions. Best you can do is take action for the page reported in si_addr, and then if that's not enough, you'll get another signal soon enough. At least, that's how we did things in ObjectStore.

这篇关于C SIGSEGV Handler &保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆