#include #include #include #include #include #include #include #include #include #define TRIES 2 void * get_stack_addr(); void print_layout(pid_t pid) { char maps_file[128]; char buf[BUFSIZ]; int fd; int bytes; snprintf(maps_file, sizeof(maps_file)-1, "/proc/%d/maps", pid); fd = open(maps_file, O_RDONLY); printf("%u MEMORY layout\n", pid); while((bytes = read(fd, buf, sizeof(buf)))>0) { buf[bytes]='\0'; printf("%s", buf); } printf("END\n"); } main() { size_t len; int i; void *v1, *v, *vold; void *stacktop; void *mina, *maxa; struct rlimit stack_limit; pid_t pid=getpid(); // len = 17179869184; // len = 34359738368; // did not infringe // len = 100000L * 4096; len = 20000L * 4096 * 1024; stacktop = get_stack_addr(); getrlimit(RLIMIT_STACK, &stack_limit); //printf("stacktop is %lx\n",(long) stacktop); mina = (void*)0xFFFFFFFFFFFFFFFFL; maxa = 0; vold = 0; for (i=0; i maxa) maxa = v + len; vold = v; } unsigned long delta = (unsigned long)stacktop - (unsigned long)maxa; if (delta/stat sprintf(fname,"/proc/%d/stat",getpid()); if (!(fp = fopen(fname, "r"))) { printf("Error opening %s\n",fname); abort(); } // create format string. The stack starting addr is param #28 // we don't care about the first 27 parameters. strcpy(fmt_str,"%*d %*s %*c"); for (i=4;i<28;i++) { strcat(fmt_str," %*d"); } strcat(fmt_str," %lu"); fscanf(fp,fmt_str,&val); fclose(fp); return (void *)val; }