动态扩展Linux堆栈

时间:2019-02-06 23:44:36

标签: c linux memory page-fault

我注意到Linux堆栈开始时很小,并且由于递归/推送/ vlas导致的页面错误而扩展,大小达到getrlimit(RLIMIT_STACK,...),给出或接受(系统上的默认值为8MiB)。

但是奇怪的是,如果我通过在限制范围内直接寻址字节而导致页面错误,Linux只会定期进行segfault而不会扩展页面映射(但是如果我在拥有例如alloca之后执行此操作,则不会出现segfault会导致堆栈扩展)。

示例程序:

#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#define CMD "grep stack /proc/XXXXXXXXXXXXXXXX/maps"
#define CMDP "grep stack /proc/%ld/maps"
void vla(size_t Sz)
{
    char b[Sz];
    b[0]='y';
    b[1]='\0';
    puts(b);
}
#define OFFSET (sizeof(char)<<12)
int main(int C, char **V)
{
    char cmd[sizeof CMD]; sprintf(cmd,CMDP,(long)getpid());
    if(system(cmd)) return 1;
    for(int i=0; ; i++){
        printf("%d\n", i);
        char *ptr = (char*)(((uintptr_t)&ptr)-i*OFFSET);
        if(C>1) vla(i*OFFSET); //pass an argument to the executable to turn this on
        ptr[0] = 'x';
        ptr[1] = '\0';
        if(system(cmd)) return 1;
        puts(ptr);
    }
}

什么内核代码正在执行此操作?它如何区分自然堆栈增长和我在地址空间中闲逛?

1 个答案:

答案 0 :(得分:3)

Linux内核将堆栈指针的内容作为限制(在合理范围内)。访问堆栈指针下方的堆栈指针减去65536,并且32位无符号长型的大小导致分段冲突。因此,如果您要沿着堆栈访问内存,则必须确保堆栈指针以某种方式随访问的减少而减小,以使Linux内核扩大该段。请参见/arch/x86/mm/fault.c中的以下代码段:

if (sw_error_code & X86_PF_USER) {
    /*
     * Accessing the stack below %sp is always a bug.
     * The large cushion allows instructions like enter
     * and pusha to work. ("enter $65535, $31" pushes
     * 32 pointers and then decrements %sp by 65535.)
     */
    if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
        bad_area(regs, sw_error_code, address);
        return;
    }
}

这里的堆栈指针寄存器的值是关键!