simplify
[heimdal.git] / lib / roken / test-mem.c
blobef2a5ed7784c7947430892607eac94ad645b71c9
1 /*
2 * Copyright (c) 1999 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
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"
44 #include "test-mem.h"
46 /* #undef HAVE_MMAP */
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;
57 struct sigaction sa, osa;
59 char *testname;
61 static RETSIGTYPE
62 segv_handler(int sig)
64 int fd;
65 char msg[] = "SIGSEGV i current test: ";
67 fd = open("/dev/stdout", O_WRONLY, 0600);
68 if (fd >= 0) {
69 (void)write(fd, msg, sizeof(msg) - 1);
70 (void)write(fd, testname, strlen(testname));
71 (void)write(fd, "\n", 1);
72 close(fd);
74 _exit(1);
77 #define TESTREC() \
78 if (testname) \
79 errx(1, "test %s run recursively on %s", name, testname); \
80 testname = strdup(name); \
81 if (testname == NULL) \
82 errx(1, "malloc");
85 void * ROKEN_LIB_FUNCTION
86 rk_test_mem_alloc(enum rk_test_mem_type type, const char *name,
87 void *buf, size_t size)
89 #ifndef HAVE_MMAP
90 unsigned char *p;
92 TESTREC();
94 p = malloc(size + 2);
95 if (p == NULL)
96 errx(1, "malloc");
97 map.type = type;
98 map.start = p;
99 map.size = size + 2;
100 p[0] = 0xff;
101 p[map.size] = 0xff;
102 map.data_start = p + 1;
103 #else
104 unsigned char *p;
105 int flags, ret, fd;
106 size_t pagesize = getpagesize();
108 TESTREC();
110 map.type = type;
112 #ifdef MAP_ANON
113 flags = MAP_ANON;
114 fd = -1;
115 #else
116 flags = 0;
117 fd = open ("/dev/zero", O_RDONLY);
118 if(fd < 0)
119 err (1, "open /dev/zero");
120 #endif
121 map.fd = fd;
122 flags |= MAP_PRIVATE;
124 map.size = size + pagesize - (size % pagesize) + pagesize * 2;
126 p = (unsigned char *)mmap(0, map.size, PROT_READ | PROT_WRITE,
127 flags, fd, 0);
128 if (p == (unsigned char *)MAP_FAILED)
129 err (1, "mmap");
131 map.start = p;
133 ret = mprotect ((void *)p, pagesize, 0);
134 if (ret < 0)
135 err (1, "mprotect");
137 ret = mprotect (p + map.size - pagesize, pagesize, 0);
138 if (ret < 0)
139 err (1, "mprotect");
141 switch (type) {
142 case RK_TM_OVERRUN:
143 map.data_start = p + map.size - pagesize - size;
144 break;
145 case RK_TM_UNDERRUN:
146 map.data_start = p + pagesize;
147 break;
148 default:
149 abort();
151 #endif
152 sigemptyset (&sa.sa_mask);
153 sa.sa_flags = 0;
154 #ifdef SA_RESETHAND
155 sa.sa_flags |= SA_RESETHAND;
156 #endif
157 sa.sa_handler = segv_handler;
158 sigaction (SIGSEGV, &sa, &osa);
160 map.data_size = size;
161 if (buf)
162 memcpy(map.data_start, buf, size);
163 return map.data_start;
166 void ROKEN_LIB_FUNCTION
167 rk_test_mem_free(const char *map_name)
169 #ifndef HAVE_MMAP
170 unsigned char *p = map.start;
172 if (testname == NULL)
173 errx(1, "test_mem_free call on no free");
175 if (p[0] != 0xff)
176 errx(1, "%s: %s underrun %x\n", testname, map_name, p[0]);
177 if (p[map.size] != 0xff)
178 errx(1, "%s: %s overrun %x\n", testname, map_name, p[map.size - 1]);
179 free(map.start);
180 #else
181 int ret;
183 if (testname == NULL)
184 errx(1, "test_mem_free call on no free");
186 ret = munmap (map.start, map.size);
187 if (ret < 0)
188 err (1, "munmap");
189 if (map.fd > 0)
190 close(map.fd);
191 #endif
192 free(testname);
193 testname = NULL;
195 sigaction (SIGSEGV, &osa, NULL);