sigstackalign.c revision 1.4 1 1.4 mellon /* $NetBSD: sigstackalign.c,v 1.4 2011/05/18 19:45:05 mellon Exp $ */
2 1.2 itojun
3 1.2 itojun /*-
4 1.2 itojun * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.2 itojun * All rights reserved.
6 1.2 itojun *
7 1.2 itojun * Redistribution and use in source and binary forms, with or without
8 1.2 itojun * modification, are permitted provided that the following conditions
9 1.2 itojun * are met:
10 1.2 itojun * 1. Redistributions of source code must retain the above copyright
11 1.2 itojun * notice, this list of conditions and the following disclaimer.
12 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 itojun * notice, this list of conditions and the following disclaimer in the
14 1.2 itojun * documentation and/or other materials provided with the distribution.
15 1.2 itojun *
16 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2 itojun * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2 itojun * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2 itojun * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2 itojun * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2 itojun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2 itojun * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2 itojun * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2 itojun * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2 itojun * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2 itojun * POSSIBILITY OF SUCH DAMAGE.
27 1.2 itojun */
28 1.1 bjh21
29 1.1 bjh21 #include <sys/types.h>
30 1.1 bjh21
31 1.4 mellon __RCSID("$NetBSD: sigstackalign.c,v 1.4 2011/05/18 19:45:05 mellon Exp $");
32 1.1 bjh21
33 1.1 bjh21 #include <signal.h>
34 1.1 bjh21 #include <stdio.h>
35 1.1 bjh21 #include <stdlib.h>
36 1.4 mellon #include <err.h>
37 1.4 mellon #include <unistd.h>
38 1.1 bjh21
39 1.1 bjh21 #define RANGE 16
40 1.1 bjh21 #define STACKALIGN 8
41 1.1 bjh21 #define BLOCKSIZE (MINSIGSTKSZ + RANGE)
42 1.1 bjh21
43 1.1 bjh21 extern void getstackptr(int);
44 1.1 bjh21
45 1.1 bjh21 void *stackptr;
46 1.1 bjh21
47 1.1 bjh21 int
48 1.1 bjh21 main(int argc, char **argv)
49 1.1 bjh21 {
50 1.1 bjh21 char *stackblock;
51 1.1 bjh21 int i, ret;
52 1.1 bjh21 struct sigaction sa;
53 1.1 bjh21 stack_t ss;
54 1.1 bjh21
55 1.1 bjh21 ret = 0;
56 1.1 bjh21
57 1.1 bjh21 sa.sa_handler = getstackptr;
58 1.1 bjh21 sigemptyset(&sa.sa_mask);
59 1.1 bjh21 sa.sa_flags = SA_ONSTACK;
60 1.1 bjh21 if (sigaction(SIGUSR1, &sa, NULL) != 0)
61 1.1 bjh21 err(1, "sigaction");
62 1.1 bjh21
63 1.1 bjh21 stackblock = malloc(BLOCKSIZE);
64 1.1 bjh21 for (i = 0; i < RANGE; i++) {
65 1.1 bjh21 ss.ss_sp = stackblock;
66 1.1 bjh21 ss.ss_size = MINSIGSTKSZ + i;
67 1.1 bjh21 ss.ss_flags = 0;
68 1.1 bjh21 if (sigaltstack(&ss, NULL) != 0)
69 1.1 bjh21 err(1, "sigaltstack");
70 1.1 bjh21 kill(getpid(), SIGUSR1);
71 1.1 bjh21 if ((u_int)stackptr % STACKALIGN != 0) {
72 1.1 bjh21 fprintf(stderr, "Bad stack pointer %p\n", stackptr);
73 1.1 bjh21 ret = 1;
74 1.1 bjh21 }
75 1.1 bjh21 #if 0
76 1.1 bjh21 printf("i = %d, stackptr = %p\n", i, stackptr);
77 1.1 bjh21 #endif
78 1.1 bjh21 }
79 1.1 bjh21
80 1.1 bjh21 return ret;
81 1.1 bjh21 }
82 1.1 bjh21
83