sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame^] | 1 | #include <inttypes.h> |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
mueller | 535cc6e | 2004-01-03 14:18:02 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 4 | #include <signal.h> |
njn | 83b62cb | 2009-04-15 03:12:43 +0000 | [diff] [blame] | 5 | #include "tests/sys_mman.h" |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 6 | |
| 7 | void sig_handler(int sig){ |
| 8 | int var; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame^] | 9 | fprintf(stderr, "caught signal, local var is on %#" PRIxPTR "\n", |
| 10 | (uintptr_t)&var); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 11 | } |
| 12 | |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 13 | int main(int argv, char** argc) { |
| 14 | int res, i; |
| 15 | stack_t sigstk; |
| 16 | struct sigaction act; |
florian | 017d8f5 | 2015-03-23 17:13:04 +0000 | [diff] [blame] | 17 | static const int size = SIGSTKSZ*2; |
njn | 09ef408 | 2005-11-17 19:38:09 +0000 | [diff] [blame] | 18 | // We give EXEC permissions because this won't work on ppc32 unless you |
njn | b67c6ce | 2005-11-17 19:47:37 +0000 | [diff] [blame] | 19 | // ask for an alt stack with EXEC permissions, |
sewardj | 7c68e98 | 2005-11-17 19:44:27 +0000 | [diff] [blame] | 20 | // since signal returning requires execution of code on the stack. |
sewardj | 89551b3 | 2005-11-17 13:04:46 +0000 | [diff] [blame] | 21 | char *stk = (char *)mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, |
njn | 83b62cb | 2009-04-15 03:12:43 +0000 | [diff] [blame] | 22 | MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); |
fitzhardinge | f0f911c | 2003-11-09 09:51:33 +0000 | [diff] [blame] | 23 | sigstk.ss_sp = stk; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 24 | |
fitzhardinge | f0f911c | 2003-11-09 09:51:33 +0000 | [diff] [blame] | 25 | sigstk.ss_size = size; |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 26 | sigstk.ss_flags = 0; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame^] | 27 | fprintf(stderr, "calling sigaltstack, stack base is %#" PRIxPTR "\n", |
| 28 | (uintptr_t)sigstk.ss_sp); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 29 | 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; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 34 | sigemptyset(&act.sa_mask); |
sewardj | 2342c97 | 2002-05-22 23:34:20 +0000 | [diff] [blame] | 35 | 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 | } |