xref: /freebsd/crypto/heimdal/lib/roken/test-mem.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*
2  * Copyright (c) 1999 - 2004 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <config.h>
35 
36 #ifdef HAVE_SYS_MMAN_H
37 #include <sys/mman.h>
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <err.h>
42 #include "roken.h"
43 
44 #include "test-mem.h"
45 
46 /* #undef HAVE_MMAP */
47 
48 struct {
49     void *start;
50     size_t size;
51     void *data_start;
52     size_t data_size;
53     enum rk_test_mem_type type;
54     int fd;
55 } map;
56 
57 #ifdef HAVE_SIGACTION
58 
59 struct sigaction sa, osa;
60 
61 #else
62 
63 void (* osigh)(int);
64 
65 #endif
66 
67 char *testname;
68 
69 static RETSIGTYPE
70 segv_handler(int sig)
71 {
72     int fd;
73     char msg[] = "SIGSEGV i current test: ";
74 
75     fd = open("/dev/stdout", O_WRONLY, 0600);
76     if (fd >= 0) {
77 	(void)write(fd, msg, sizeof(msg) - 1);
78 	(void)write(fd, testname, strlen(testname));
79 	(void)write(fd, "\n", 1);
80 	close(fd);
81     }
82     _exit(1);
83 }
84 
85 #define TESTREC()							\
86     if (testname)							\
87 	errx(1, "test %s run recursively on %s", name, testname);	\
88     testname = strdup(name);						\
89     if (testname == NULL)						\
90 	errx(1, "malloc");
91 
92 
93 ROKEN_LIB_FUNCTION void * ROKEN_LIB_CALL
94 rk_test_mem_alloc(enum rk_test_mem_type type, const char *name,
95 		  void *buf, size_t size)
96 {
97 #ifndef HAVE_MMAP
98     unsigned char *p;
99 
100     TESTREC();
101 
102     p = malloc(size + 2);
103     if (p == NULL)
104 	errx(1, "malloc");
105     map.type = type;
106     map.start = p;
107     map.size = size + 2;
108     p[0] = 0xff;
109     p[map.size-1] = 0xff;
110     map.data_start = p + 1;
111 #else
112     unsigned char *p;
113     int flags, ret, fd;
114     size_t pagesize = getpagesize();
115 
116     TESTREC();
117 
118     map.type = type;
119 
120 #ifdef MAP_ANON
121     flags = MAP_ANON;
122     fd = -1;
123 #else
124     flags = 0;
125     fd = open ("/dev/zero", O_RDONLY);
126     if(fd < 0)
127 	err (1, "open /dev/zero");
128 #endif
129     map.fd = fd;
130     flags |= MAP_PRIVATE;
131 
132     map.size = size + pagesize - (size % pagesize) + pagesize * 2;
133 
134     p = (unsigned char *)mmap(0, map.size, PROT_READ | PROT_WRITE,
135 			      flags, fd, 0);
136     if (p == (unsigned char *)MAP_FAILED)
137 	err (1, "mmap");
138 
139     map.start = p;
140 
141     ret = mprotect ((void *)p, pagesize, 0);
142     if (ret < 0)
143 	err (1, "mprotect");
144 
145     ret = mprotect (p + map.size - pagesize, pagesize, 0);
146     if (ret < 0)
147 	err (1, "mprotect");
148 
149     switch (type) {
150     case RK_TM_OVERRUN:
151 	map.data_start = p + map.size - pagesize - size;
152 	break;
153     case RK_TM_UNDERRUN:
154 	map.data_start = p + pagesize;
155 	break;
156     default:
157 	abort();
158     }
159 #endif
160 #ifdef HAVE_SIGACTION
161     sigemptyset (&sa.sa_mask);
162     sa.sa_flags = 0;
163 #ifdef SA_RESETHAND
164     sa.sa_flags |= SA_RESETHAND;
165 #endif
166     sa.sa_handler = segv_handler;
167     sigaction (SIGSEGV, &sa, &osa);
168 #else
169     osigh = signal(SIGSEGV, segv_handler);
170 #endif
171 
172     map.data_size = size;
173     if (buf)
174 	memcpy(map.data_start, buf, size);
175     return map.data_start;
176 }
177 
178 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
179 rk_test_mem_free(const char *map_name)
180 {
181 #ifndef HAVE_MMAP
182     unsigned char *p = map.start;
183 
184     if (testname == NULL)
185 	errx(1, "test_mem_free call on no free");
186 
187     if (p[0] != 0xff)
188 	errx(1, "%s: %s underrun %x\n", testname, map_name, p[0]);
189     if (p[map.size-1] != 0xff)
190 	errx(1, "%s: %s overrun %x\n", testname, map_name, p[map.size - 1]);
191     free(map.start);
192 #else
193     int ret;
194 
195     if (testname == NULL)
196 	errx(1, "test_mem_free call on no free");
197 
198     ret = munmap (map.start, map.size);
199     if (ret < 0)
200 	err (1, "munmap");
201     if (map.fd > 0)
202 	close(map.fd);
203 #endif
204     free(testname);
205     testname = NULL;
206 
207 #ifdef HAVE_SIGACTION
208     sigaction (SIGSEGV, &osa, NULL);
209 #else
210     signal (SIGSEGV, osigh);
211 #endif
212 }
213