blob: 2892ebbe9169aef3c8bfdffcc204a43d9ee85fb9 [file] [log] [blame]
sewardj8eb8bab2015-07-21 14:44:28 +00001#include <inttypes.h>
sewardj2342c972002-05-22 23:34:20 +00002#include <stdio.h>
mueller535cc6e2004-01-03 14:18:02 +00003#include <stdlib.h>
sewardj2342c972002-05-22 23:34:20 +00004#include <signal.h>
njn83b62cb2009-04-15 03:12:43 +00005#include "tests/sys_mman.h"
sewardj2342c972002-05-22 23:34:20 +00006
7void sig_handler(int sig){
8 int var;
sewardj8eb8bab2015-07-21 14:44:28 +00009 fprintf(stderr, "caught signal, local var is on %#" PRIxPTR "\n",
10 (uintptr_t)&var);
sewardj2342c972002-05-22 23:34:20 +000011}
12
sewardj2342c972002-05-22 23:34:20 +000013int main(int argv, char** argc) {
14 int res, i;
15 stack_t sigstk;
16 struct sigaction act;
florian017d8f52015-03-23 17:13:04 +000017 static const int size = SIGSTKSZ*2;
njn09ef4082005-11-17 19:38:09 +000018 // We give EXEC permissions because this won't work on ppc32 unless you
njnb67c6ce2005-11-17 19:47:37 +000019 // ask for an alt stack with EXEC permissions,
sewardj7c68e982005-11-17 19:44:27 +000020 // since signal returning requires execution of code on the stack.
sewardj89551b32005-11-17 13:04:46 +000021 char *stk = (char *)mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC,
njn83b62cb2009-04-15 03:12:43 +000022 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
fitzhardingef0f911c2003-11-09 09:51:33 +000023 sigstk.ss_sp = stk;
sewardj2342c972002-05-22 23:34:20 +000024
fitzhardingef0f911c2003-11-09 09:51:33 +000025 sigstk.ss_size = size;
sewardj2342c972002-05-22 23:34:20 +000026 sigstk.ss_flags = 0;
sewardj8eb8bab2015-07-21 14:44:28 +000027 fprintf(stderr, "calling sigaltstack, stack base is %#" PRIxPTR "\n",
28 (uintptr_t)sigstk.ss_sp);
sewardj2342c972002-05-22 23:34:20 +000029 if (sigaltstack(&sigstk,0)<0) perror("sigaltstack");
30
31 fprintf(stderr,"setting sigaction\n");
32 act.sa_flags=SA_ONSTACK;
33 act.sa_handler=&sig_handler;
fitzhardinge98abfc72003-12-16 02:05:15 +000034 sigemptyset(&act.sa_mask);
sewardj2342c972002-05-22 23:34:20 +000035 res = sigaction(SIGUSR1,&act,0);
36 fprintf(stderr, "res = %d\n", res);
37 fprintf(stderr, "raising the signal\n");
38 raise(SIGUSR1);
39
40 /* Loop long enough so valgrind has a forced context switch and
41 actually delivers the signal before the thread exits. */
42 for (i = 0; i < 1000000; i++) ;
43
44 fprintf(stderr, "done\n");
45 return 0;
46}