File: sigaltstack.c

package info (click to toggle)
valgrind 1%3A3.14.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 156,980 kB
  • sloc: ansic: 728,128; exp: 26,134; xml: 22,268; cpp: 7,638; asm: 7,312; makefile: 6,102; perl: 5,910; sh: 5,717
file content (46 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "tests/sys_mman.h"

void sig_handler(int sig){
  int var;
  fprintf(stderr, "caught signal, local var is on %#" PRIxPTR "\n",
          (uintptr_t)&var);
}

int main(int argv, char** argc) {
  int res, i;
  stack_t sigstk;
  struct sigaction act;
  static const int size = SIGSTKSZ*2;
  // We give EXEC permissions because this won't work on ppc32 unless you
  // ask for an alt stack with EXEC permissions,
  // since signal returning requires execution of code on the stack.      
  char *stk = (char *)mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, 
                                    MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  sigstk.ss_sp = stk;

  sigstk.ss_size = size;
  sigstk.ss_flags = 0;
  fprintf(stderr, "calling sigaltstack, stack base is %#" PRIxPTR "\n",
          (uintptr_t)sigstk.ss_sp);
  if (sigaltstack(&sigstk,0)<0) perror("sigaltstack");

  fprintf(stderr,"setting sigaction\n");
  act.sa_flags=SA_ONSTACK;
  act.sa_handler=&sig_handler;
  sigemptyset(&act.sa_mask);
  res = sigaction(SIGUSR1,&act,0);
  fprintf(stderr, "res = %d\n", res);
  fprintf(stderr, "raising the signal\n");
  raise(SIGUSR1);
  
  /* Loop long enough so valgrind has a forced context switch and
     actually delivers the signal before the thread exits. */
  for (i = 0; i < 1000000; i++) ;

  fprintf(stderr, "done\n");
  return 0;
}