sigstackalign.c revision 1.2.32.1 1 1.2.32.1 yamt /* $NetBSD: sigstackalign.c,v 1.2.32.1 2008/05/18 12:30:47 yamt 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.2.32.1 yamt __RCSID("$NetBSD: sigstackalign.c,v 1.2.32.1 2008/05/18 12:30:47 yamt 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.1 bjh21
37 1.1 bjh21 #define RANGE 16
38 1.1 bjh21 #define STACKALIGN 8
39 1.1 bjh21 #define BLOCKSIZE (MINSIGSTKSZ + RANGE)
40 1.1 bjh21
41 1.1 bjh21 extern void getstackptr(int);
42 1.1 bjh21
43 1.1 bjh21 void *stackptr;
44 1.1 bjh21
45 1.1 bjh21 int
46 1.1 bjh21 main(int argc, char **argv)
47 1.1 bjh21 {
48 1.1 bjh21 char *stackblock;
49 1.1 bjh21 int i, ret;
50 1.1 bjh21 struct sigaction sa;
51 1.1 bjh21 stack_t ss;
52 1.1 bjh21
53 1.1 bjh21 ret = 0;
54 1.1 bjh21
55 1.1 bjh21 sa.sa_handler = getstackptr;
56 1.1 bjh21 sigemptyset(&sa.sa_mask);
57 1.1 bjh21 sa.sa_flags = SA_ONSTACK;
58 1.1 bjh21 if (sigaction(SIGUSR1, &sa, NULL) != 0)
59 1.1 bjh21 err(1, "sigaction");
60 1.1 bjh21
61 1.1 bjh21 stackblock = malloc(BLOCKSIZE);
62 1.1 bjh21 for (i = 0; i < RANGE; i++) {
63 1.1 bjh21 ss.ss_sp = stackblock;
64 1.1 bjh21 ss.ss_size = MINSIGSTKSZ + i;
65 1.1 bjh21 ss.ss_flags = 0;
66 1.1 bjh21 if (sigaltstack(&ss, NULL) != 0)
67 1.1 bjh21 err(1, "sigaltstack");
68 1.1 bjh21 kill(getpid(), SIGUSR1);
69 1.1 bjh21 if ((u_int)stackptr % STACKALIGN != 0) {
70 1.1 bjh21 fprintf(stderr, "Bad stack pointer %p\n", stackptr);
71 1.1 bjh21 ret = 1;
72 1.1 bjh21 }
73 1.1 bjh21 #if 0
74 1.1 bjh21 printf("i = %d, stackptr = %p\n", i, stackptr);
75 1.1 bjh21 #endif
76 1.1 bjh21 }
77 1.1 bjh21
78 1.1 bjh21 return ret;
79 1.1 bjh21 }
80 1.1 bjh21
81