1103423Snectar/* 2233294Sstas * Copyright (c) 2002 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 5103423Snectar * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 9103423Snectar * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 12103423Snectar * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 16103423Snectar * 17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 20103423Snectar * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 32103423Snectar */ 33103423Snectar 34103423Snectar#include <config.h> 35103423Snectar 36103423Snectar#include <sys/types.h> 37103423Snectar#ifdef HAVE_SYS_MMAN_H 38103423Snectar#include <sys/mman.h> 39103423Snectar#endif 40103423Snectar#include <fcntl.h> 41103423Snectar 42103423Snectar#include "roken.h" 43103423Snectar#include "resolve.h" 44103423Snectar 45103423Snectarstruct dns_reply* 46103423Snectarparse_reply(const unsigned char *, size_t); 47103423Snectar 48103423Snectarenum { MAX_BUF = 36}; 49103423Snectar 50103423Snectarstatic struct testcase { 51103423Snectar unsigned char buf[MAX_BUF]; 52103423Snectar size_t buf_len; 53103423Snectar} tests[] = { 54103423Snectar {{0x12, 0x67, 0x84, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 55103423Snectar 0x03, 'f', 'o', 'o', 0x00, 56103423Snectar 0x00, 0x10, 0x00, 0x01, 57103423Snectar 0x03, 'f', 'o', 'o', 0x00, 58103423Snectar 0x00, 0x10, 0x00, 0x01, 59103423Snectar 0x00, 0x00, 0x12, 0x67, 0xff, 0xff}, 36} 60103423Snectar}; 61103423Snectar 62103423Snectar#ifndef MAP_FAILED 63103423Snectar#define MAP_FAILED (-1) 64103423Snectar#endif 65103423Snectar 66103423Snectarstatic sig_atomic_t val = 0; 67103423Snectar 68103423Snectarstatic RETSIGTYPE 69103423Snectarsegv_handler(int sig) 70103423Snectar{ 71103423Snectar val = 1; 72103423Snectar} 73103423Snectar 74103423Snectarint 75103423Snectarmain(int argc, char **argv) 76103423Snectar{ 77103423Snectar#ifndef HAVE_MMAP 78103423Snectar return 77; /* signal to automake that this test 79103423Snectar cannot be run */ 80103423Snectar#else /* HAVE_MMAP */ 81103423Snectar int ret; 82103423Snectar int i; 83103423Snectar struct sigaction sa; 84103423Snectar 85103423Snectar sigemptyset (&sa.sa_mask); 86103423Snectar sa.sa_flags = 0; 87103423Snectar sa.sa_handler = segv_handler; 88103423Snectar sigaction (SIGSEGV, &sa, NULL); 89103423Snectar 90103423Snectar for (i = 0; val == 0 && i < sizeof(tests)/sizeof(tests[0]); ++i) { 91103423Snectar const struct testcase *t = &tests[i]; 92103423Snectar unsigned char *p1, *p2; 93103423Snectar int flags; 94103423Snectar int fd; 95103423Snectar size_t pagesize = getpagesize(); 96103423Snectar unsigned char *buf; 97103423Snectar 98103423Snectar#ifdef MAP_ANON 99103423Snectar flags = MAP_ANON; 100103423Snectar fd = -1; 101103423Snectar#else 102103423Snectar flags = 0; 103103423Snectar fd = open ("/dev/zero", O_RDONLY); 104103423Snectar if(fd < 0) 105103423Snectar err (1, "open /dev/zero"); 106103423Snectar#endif 107103423Snectar flags |= MAP_PRIVATE; 108103423Snectar 109178825Sdfr p1 = (unsigned char *)mmap(0, 2 * pagesize, PROT_READ | PROT_WRITE, 110103423Snectar flags, fd, 0); 111103423Snectar if (p1 == (unsigned char *)MAP_FAILED) 112103423Snectar err (1, "mmap"); 113103423Snectar p2 = p1 + pagesize; 114178825Sdfr ret = mprotect ((void *)p2, pagesize, 0); 115103423Snectar if (ret < 0) 116103423Snectar err (1, "mprotect"); 117103423Snectar buf = p2 - t->buf_len; 118103423Snectar memcpy (buf, t->buf, t->buf_len); 119103423Snectar parse_reply (buf, t->buf_len); 120178825Sdfr ret = munmap ((void *)p1, 2 * pagesize); 121103423Snectar if (ret < 0) 122103423Snectar err (1, "munmap"); 123103423Snectar } 124103423Snectar return val; 125103423Snectar#endif /* HAVE_MMAP */ 126103423Snectar} 127