1 /* This test causes an error in 3.10.1 and earlier versions like so:
3 ==8336== Can't extend stack to 0x4033f98 during signal delivery for thread 2:
4 ==8336== no stack segment
6 The reason was that only AnonC segments were considered as stack
21 static volatile char *lowest_j
;
22 static jmp_buf goback
;
24 static void sigsegv_handler(int signr
)
29 static void bad_things_till_guard_page(void)
31 fprintf(stderr
, "... doing bad things till guard page\n");
42 static void say_something(void)
44 fprintf(stderr
, "plugh\n");
47 static void* child_func ( void* arg
)
52 bad_things_till_guard_page();
57 int main(int argc
, const char** argv
)
61 /* We will discover the thread guard page using SEGV.
62 So, prepare an handler. */
64 sa
.sa_handler
= sigsegv_handler
;
65 sigemptyset(&sa
.sa_mask
);
67 if (sigaction (SIGSEGV
, &sa
, NULL
) != 0)
72 /* Create a file that will be used as stack for a pthread. */
73 const size_t file_size
= 1024 * 1024;
74 const char file_name
[] = "FILE";
75 fd
= open(file_name
, O_CREAT
|O_WRONLY
,
76 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
78 void *p
= malloc(file_size
);
80 memset(p
, 0, file_size
);
81 int written
= write(fd
, p
, file_size
);
82 assert(written
== file_size
);
85 /* Create a file-based stack for the child */
86 fd
= open(file_name
, O_CREAT
|O_RDWR
,
87 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
89 const size_t stack_size
= 256 * 1024;
90 assert(stack_size
< file_size
);
91 void *stack
= mmap(NULL
, stack_size
, PROT_READ
|PROT_WRITE
,
93 assert(stack
!= (void *)-1);
95 pthread_attr_init(&attr
);
96 r
= pthread_attr_setstack(&attr
, stack
, stack_size
);
99 /* Create child run */
100 r
= pthread_create(&child
, &attr
, child_func
, NULL
);
102 r
= pthread_join(child
, NULL
);