5

我知道程序中断是Linux操作系统为进程分配的最高虚拟内存地址,因此标记了堆的最高地址。您可以通过调用sbrk(0)来获取程序中断的地址。

当我创建以下琐碎的程序时,每次运行时都会得到不同的结果:

#define _BSD_SOURCE
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf( "system break: %p\n", sbrk( 0 ) );
    return 0;
}

例如,在我的电脑上:

$ ./sbrk
system break: 0x81fc000
$ ./sbrk
system break: 0x9bce000
$ ./sbrk
system break: 0x97a6000

我的理解是堆被分配在虚拟内存中 BSS 部分的正上方——我想我希望它对于像这样的普通程序总是具有相同的初始值。程序中断最初定位的位置是否有一些随机化或某些东西?如果不是,为什么每次运行程序都不同?

4

2 回答 2

6

By default the kernel will randomise the initial point, though this feature can be disabled. This is the code that is run (for x86, in arch/x86/kernel/process.c):

unsigned long arch_randomize_brk(struct mm_struct *mm)
{
        unsigned long range_end = mm->brk + 0x02000000;
        return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
}

Additionally, in this function from the ELF binary loader (fs/binfmt_elf.c), you can see the function called:

if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
                current->mm->brk = current->mm->start_brk =
                        arch_randomize_brk(current->mm);
#ifdef CONFIG_COMPAT_BRK
                current->brk_randomized = 1;
#endif
}
于 2015-03-31T01:49:24.627 回答
4

是的,有随机性。称为地址空间布局随机化 (ASLR)。http://en.wikipedia.org/wiki/Address_space_layout_randomization

于 2015-03-31T01:10:43.667 回答