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