• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test of correct simulation for active stack. */
2 
3 #include <assert.h>
4 #include <signal.h>
5 #include <stdio.h>
6 #include <ucontext.h>
7 
8 static char altstack_map[8096];
9 static volatile stack_t *sp;
10 
sighandler(int sig)11 static void sighandler(int sig)
12 {
13    /* Check that the alternate stack is active. */
14    assert(sp->ss_sp == altstack_map);
15    assert(sp->ss_size == sizeof(altstack_map));
16    assert(sp->ss_flags == SS_ONSTACK);
17 }
18 
main(void)19 int main(void)
20 {
21    stack_t mainstack;
22    stack_t altstack;
23    struct sigaction sa;
24    /* Obtain an address inside the stack using a dirty trick. */
25    void *local = &sa;
26 
27    /* Get an address for stack definition. */
28    if (getustack((stack_t**)&sp)) {
29       perror("getustack");
30       return 1;
31    }
32 
33    /* Check the current stack. */
34    assert(sp->ss_sp <= local);
35    assert(local < (void*)((char*)sp->ss_sp + sp->ss_size));
36    assert(sp->ss_flags == 0);
37 
38    /* Backup the current stack. */
39    mainstack = *sp;
40 
41    /* Setup a signal handler. */
42    sa.sa_handler = sighandler;
43    sa.sa_flags = SA_ONSTACK;
44    if (sigfillset(&sa.sa_mask)) {
45       perror("sigfillset");
46       return 1;
47    }
48    if (sigaction(SIGUSR1, &sa, NULL)) {
49       perror("sigaction");
50       return 1;
51    }
52 
53    /* Setup an alternate stack. */
54    altstack.ss_sp = altstack_map;
55    altstack.ss_size = sizeof(altstack_map);
56    altstack.ss_flags = 0;
57    if (sigaltstack(&altstack, NULL)) {
58       perror("sigaltstack");
59       return 1;
60    }
61 
62    /* Raise a signal. */
63    raise(SIGUSR1);
64 
65    /* Check the current stack. */
66    assert(mainstack.ss_sp == sp->ss_sp);
67    assert(mainstack.ss_size == sp->ss_size);
68    assert(mainstack.ss_flags == sp->ss_flags);
69 
70    return 0;
71 }
72 
73