Segfault仅在GDB中出现

css*_*233 5 c linux x86 assembly segmentation-fault

为什么以下程序在执行时不会崩溃,但在GDB中崩溃时会出现段错误?在32位x86(Athlon 64,如果它应该重要)上使用GCC 4.5.2编译.

#include <stdio.h>
#include <string.h>

int modify(void)
{
        __asm__("mov $0x41414141, %edx"); // Stray value.
        __asm__("mov $0xbffff2d4, %eax"); // Addr. of ret pointer for function().
        __asm__("mov %edx, (%eax)");
}

int function(void)
{
        modify();

        return 0;
}

int main(int argc, char **argv)
{
        function();

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

mov $ 0xbffff2d4,%eax是使用GDB确定的,用于查找为"function"函数存储返回指针的地址.在不同的系统上,这可能会有所不同.ASLR因此而被禁用.

当我执行程序时,没有任何反应.dmesg也没有关于崩溃的报告.但是当我在GDB中执行相同的程序时:

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
=> 0x41414141:  Cannot access memory at address 0x41414141
Run Code Online (Sandbox Code Playgroud)

这是我期望在我正常执行程序时应该发生的事情.当其他程序崩溃时,我的确会像往常一样得到段错误,而且我可以轻松编写一个崩溃的小程序,其中有一个很好的段错误.但为什么这个特定的程序不会因为段错误而崩溃?

Jes*_*ter 2

即使完全禁用 ASLR,您仍然可以获得随机堆栈和堆。您可以使用内核引导参数全局关闭它norandmaps,或者通过设置/proc/sys/kernel/randomize_va_space为零在运行时关闭它。它也是流程个性的一部分。

在 GDB 中,您可以使用以下disable-randomization设置进行调整:

(gdb) help set disable-randomization
Set disabling of debuggee's virtual address space randomization.
When this mode is on (which is the default), randomization of the virtual
address space is disabled.  Standalone programs run with the randomization
enabled by default on some platforms.
Run Code Online (Sandbox Code Playgroud)

作为一个小测试程序来说明这一点,您可以打印局部变量的地址,例如:

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%p\n", &argc);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)