0

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

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

这个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”。任何帮助/提示将不胜感激!

4

1 回答 1

0

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

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

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

于 2013-01-24T23:37:12.863 回答