1 /* $NetBSD: test-mem.c,v 1.1.1.1 2011/04/13 18:15:43 elric Exp $ */
4 * Copyright (c) 1999 - 2004 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifdef HAVE_SYS_MMAN_H
44 #include <krb5/roken.h>
48 /* #undef HAVE_MMAP */
55 enum rk_test_mem_type type
;
61 struct sigaction sa
, osa
;
75 char msg
[] = "SIGSEGV i current test: ";
77 fd
= open("/dev/stdout", O_WRONLY
, 0600);
79 (void)write(fd
, msg
, sizeof(msg
) - 1);
80 (void)write(fd
, testname
, strlen(testname
));
81 (void)write(fd
, "\n", 1);
89 errx(1, "test %s run recursively on %s", name, testname); \
90 testname = strdup(name); \
91 if (testname == NULL) \
95 ROKEN_LIB_FUNCTION
void * ROKEN_LIB_CALL
96 rk_test_mem_alloc(enum rk_test_mem_type type
, const char *name
,
97 void *buf
, size_t size
)
104 p
= malloc(size
+ 2);
111 p
[map
.size
-1] = 0xff;
112 map
.data_start
= p
+ 1;
116 size_t pagesize
= getpagesize();
127 fd
= open ("/dev/zero", O_RDONLY
);
129 err (1, "open /dev/zero");
132 flags
|= MAP_PRIVATE
;
134 map
.size
= size
+ pagesize
- (size
% pagesize
) + pagesize
* 2;
136 p
= (unsigned char *)mmap(0, map
.size
, PROT_READ
| PROT_WRITE
,
138 if (p
== (unsigned char *)MAP_FAILED
)
143 ret
= mprotect ((void *)p
, pagesize
, 0);
147 ret
= mprotect (p
+ map
.size
- pagesize
, pagesize
, 0);
153 map
.data_start
= p
+ map
.size
- pagesize
- size
;
156 map
.data_start
= p
+ pagesize
;
162 #ifdef HAVE_SIGACTION
163 sigemptyset (&sa
.sa_mask
);
166 sa
.sa_flags
|= SA_RESETHAND
;
168 sa
.sa_handler
= segv_handler
;
169 sigaction (SIGSEGV
, &sa
, &osa
);
171 osigh
= signal(SIGSEGV
, segv_handler
);
174 map
.data_size
= size
;
176 memcpy(map
.data_start
, buf
, size
);
177 return map
.data_start
;
180 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
181 rk_test_mem_free(const char *map_name
)
184 unsigned char *p
= map
.start
;
186 if (testname
== NULL
)
187 errx(1, "test_mem_free call on no free");
190 errx(1, "%s: %s underrun %x\n", testname
, map_name
, p
[0]);
191 if (p
[map
.size
-1] != 0xff)
192 errx(1, "%s: %s overrun %x\n", testname
, map_name
, p
[map
.size
- 1]);
197 if (testname
== NULL
)
198 errx(1, "test_mem_free call on no free");
200 ret
= munmap (map
.start
, map
.size
);
209 #ifdef HAVE_SIGACTION
210 sigaction (SIGSEGV
, &osa
, NULL
);
212 signal (SIGSEGV
, osigh
);