1 /* $NetBSD: t_syscall.c,v 1.3 2018/05/28 07:55:56 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Martin Husemann. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * 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. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_syscall.c,v 1.3 2018/05/28 07:55:56 martin Exp $"); 33 34 35 #include <atf-c.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <fcntl.h> 39 #include <err.h> 40 #include <string.h> 41 #include <stdlib.h> 42 #include <sys/mman.h> 43 #include <sys/endian.h> 44 #include <sys/syscall.h> 45 46 #if !defined(_LP64) && BYTE_ORDER == _BIG_ENDIAN 47 #define __SYSCALL_TO_UINTPTR_T(V) ((uintptr_t)((V)>>32)) 48 #else 49 #define __SYSCALL_TO_UINTPTR_T(V) ((uintptr_t)(V)) 50 #endif 51 52 static const char secrect_data[1024] = { 53 "my secret key\n" 54 }; 55 56 #define FILE_NAME "dummy" 57 58 #ifndef _LP64 59 ATF_TC(mmap_syscall); 60 61 ATF_TC_HEAD(mmap_syscall, tc) 62 { 63 atf_tc_set_md_var(tc, "descr", "Tests mmap(2) via syscall(2)"); 64 } 65 66 ATF_TC_BODY(mmap_syscall, tc) 67 { 68 int fd; 69 const char *p; 70 71 fd = open(FILE_NAME, O_RDWR|O_CREAT|O_TRUNC, 0666); 72 ATF_REQUIRE(fd != -1); 73 74 write(fd, secrect_data, sizeof(secrect_data)); 75 76 p = (const char *)syscall(SYS_mmap, 77 0, sizeof(secrect_data), PROT_READ, MAP_PRIVATE, fd, 0, 0, 0); 78 ATF_REQUIRE(p != NULL); 79 80 ATF_REQUIRE(strcmp(p, secrect_data) == 0); 81 } 82 #endif 83 84 ATF_TC(mmap___syscall); 85 86 ATF_TC_HEAD(mmap___syscall, tc) 87 { 88 atf_tc_set_md_var(tc, "descr", "Tests mmap(2) via __syscall(2)"); 89 } 90 91 ATF_TC_BODY(mmap___syscall, tc) 92 { 93 int fd; 94 const char *p; 95 96 fd = open(FILE_NAME, O_RDWR|O_CREAT|O_TRUNC, 0666); 97 ATF_REQUIRE(fd != -1); 98 99 write(fd, secrect_data, sizeof(secrect_data)); 100 101 p = (const char *)__SYSCALL_TO_UINTPTR_T(__syscall(SYS_mmap, 102 0, sizeof(secrect_data), PROT_READ, MAP_PRIVATE, fd, 103 /* pad*/ 0, (off_t)0)); 104 ATF_REQUIRE(p != NULL); 105 106 ATF_REQUIRE(strcmp(p, secrect_data) == 0); 107 } 108 109 ATF_TP_ADD_TCS(tp) 110 { 111 112 #ifndef _LP64 113 ATF_TP_ADD_TC(tp, mmap_syscall); 114 #endif 115 ATF_TP_ADD_TC(tp, mmap___syscall); 116 117 return atf_no_error(); 118 } 119