【问题标题】:Catching SIGSEGV using signals and sigsegjmp/siglongjmp使用信号和 sigsegjmp/siglongjmp 捕获 SIGSEGV
【发布时间】:2013-01-25 07:44:52
【问题描述】:

我正在编写一个小程序来扫描内存的每一页上的单个内存添加,以查看它是否可读/或可读写/或没有。

注意:我没有显示部分代码,因为它是导致错误的信号处理。

这个while循环用于遍历addys

char * currAddy;
currAddy = (char*)0x00000000;

while(1){

    int readWrite = read_write(currAddy);

    printf("Chunk val returned %i\n", readWrite);

    if(currAddy == (char*)0xfffff000)
        break;

    currAddy += pageSize;

}

其余的用于处理信号

int read_write (char * currAddy)
{

myRead = 0;
myWrite = 0;

/*
 myRead = 0 & myWrite = 0 -> NOT A CHUNK -> RW = -1
 myRead = 0 & myWrite = 1 -> NOT POSSIBLE
 myRead = 1 & myWrite = 0 -> RW = 0
 myRead = 1 & myWrite = 1 -> RW = 1

 */

if (sigsetjmp(jumpbuf, 1) == 0){

    //try and read
    char test = *currAddy;
    myRead = 1;

    //try and write
    *currAddy = 'a';
    myWrite = 1;


}else{

    //SIGSEGV while reading
    if (myRead == 0)
        return -1;

    //SIGSEGV while writing
    if (myRead == 1 && myWrite == 0)
        return 0;

    printf("Inside setjmp\n");

}

printf("Below the sigjmp\n");
sleep(1);

//return 1 because we can both read and write to this position

//doesnt appear to run though -- HELP HERE?
return 1;

}

这里是处理信号的地方

void handler (int sig)
{
    siglongjmp(jumpbuf, 1);
}

现在,当我运行它时,输出如下所示:

Chunk val returned -1
Chunk val returned -1
Chunk val returned -1
Chunk val returned 0
Chunk val returned 0
Below the sigjmp
Inside setjmp
Below the sigjmp
Below the sigjmp
Inside setjmp
Below the sigjmp
Below the sigjmp
Inside setjmp
.........

已编辑:我似乎无法弄清楚为什么它永远不会返回 1?同样奇怪的是它为什么会打印两次“Below the sigjmp”。任何帮助/提示将不胜感激!

【问题讨论】:

  • printf("Below the sigjmp\n"); 会在读写都成功时打印出来。
  • 是的,这是真的,但是如果它真的返回到 while 循环,我们还会在每个“Below the sigjmp”之后看到“Chunk val returned 1”?

标签: c signals segmentation-fault


【解决方案1】:

根据 POSIX,您不能在信号处理程序中使用 (sig)longjump - 如果这样做,您会得到未定义的行为。但是,至少某些 Unix 确实允许这样做(通常是 BSD 派生的),因此您将在此处看到的内容取决于您运行的操作系统。

看起来您在 sleep 调用中遇到了崩溃(可能是由于早期 siglongjmp 中损坏的信号/堆栈状态)导致它循环回 sigsetjmp

也有可能成功的*currAddy = 'a'; 正在破坏一些重要的东西,导致后来的崩溃循环回到sigsetjmp

【讨论】:

    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2019-08-27
    • 2017-08-02
    相关资源
    最近更新 更多