【发布时间】:2019-07-01 00:50:00
【问题描述】:
我注意到 Linux 堆栈开始时很小,并随着递归/推送/vlas 导致的页面错误扩展至大小 getrlimit(RLIMIT_STACK,...),给予或接受(在我的系统上默认为 8MiB)。
但奇怪的是,如果我通过直接寻址字节导致页面错误,在限制范围内,Linux 只会定期出现段错误而不扩展页面映射(虽然没有段错误,如果我在获得例如 alloca 之后这样做,会导致堆栈扩展)。
示例程序:
#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);
}
}
什么内核代码在做这个?它如何区分自然堆栈增长和我在地址空间中四处寻找?
【问题讨论】:
标签: c linux memory page-fault