1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2019 Mark Johnston <markj@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/mman.h> 32 #include <sys/ptrace.h> 33 #include <sys/wait.h> 34 35 #include <errno.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #include <atf-c.h> 42 43 static void 44 test_wired_copy_on_write(void *addr, size_t len) 45 { 46 int status, val; 47 pid_t pid; 48 49 pid = fork(); 50 if (pid == -1) 51 atf_tc_fail("fork() failed: %s", strerror(errno)); 52 if (pid == 0) { 53 if (mlock(addr, len) != 0) 54 _exit(1); 55 if (ptrace(PT_TRACE_ME, 0, NULL, 0) != 0) 56 _exit(2); 57 if (raise(SIGSTOP) != 0) 58 _exit(3); 59 if (munlock(addr, len) != 0) 60 _exit(4); 61 _exit(0); 62 } 63 64 ATF_REQUIRE(waitpid(pid, &status, 0) == pid); 65 ATF_REQUIRE_MSG(!WIFEXITED(status), 66 "child exited with status %d", WEXITSTATUS(status)); 67 ATF_REQUIRE(WIFSTOPPED(status)); 68 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP); 69 70 errno = 0; 71 val = ptrace(PT_READ_D, pid, addr, 0); 72 ATF_REQUIRE(errno == 0); 73 ATF_REQUIRE(ptrace(PT_WRITE_D, pid, addr, val) == 0); 74 ATF_REQUIRE(ptrace(PT_CONTINUE, pid, (caddr_t)1, 0) == 0); 75 ATF_REQUIRE(waitpid(pid, &status, 0) == pid); 76 ATF_REQUIRE(WIFEXITED(status)); 77 ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0, 78 "child exited with status %d", WSTOPSIG(status)); 79 } 80 81 /* 82 * Use ptrace(2) to trigger a copy-on-write fault of anonymous memory. 83 */ 84 ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_anon); 85 ATF_TC_BODY(mlock__copy_on_write_anon, tc) 86 { 87 char *addr; 88 int len; 89 90 len = getpagesize(); 91 addr = mmap(NULL, len, PROT_READ, MAP_ANON, -1, 0); 92 ATF_REQUIRE(addr != MAP_FAILED); 93 94 test_wired_copy_on_write(addr, len); 95 } 96 97 /* 98 * Use ptrace(2) to trigger a copy-on-write fault of a read-only text page. 99 */ 100 ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_vnode); 101 ATF_TC_BODY(mlock__copy_on_write_vnode, tc) 102 { 103 void *addr; 104 int len; 105 106 len = getpagesize(); 107 addr = (void *)((uintptr_t)test_wired_copy_on_write & ~(len - 1)); 108 109 test_wired_copy_on_write(addr, len); 110 } 111 112 /* 113 * Try truncating and then resizing an mlock()ed mapping. 114 */ 115 ATF_TC_WITHOUT_HEAD(mlock__truncate_and_resize); 116 ATF_TC_BODY(mlock__truncate_and_resize, tc) 117 { 118 char filename[16]; 119 char *addr; 120 int fd, i, len; 121 122 snprintf(filename, sizeof(filename), "tmp.XXXXXX"); 123 fd = mkstemp(filename); 124 ATF_REQUIRE(fd >= 0); 125 ATF_REQUIRE(unlink(filename) == 0); 126 127 len = getpagesize(); 128 ATF_REQUIRE(ftruncate(fd, len) == 0); 129 130 addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 131 ATF_REQUIRE(addr != MAP_FAILED); 132 ATF_REQUIRE(mlock(addr, len) == 0); 133 memset(addr, 1, len); 134 ATF_REQUIRE(ftruncate(fd, 0) == 0); 135 ATF_REQUIRE(ftruncate(fd, len) == 0); 136 for (i = 0; i < len; i++) 137 ATF_REQUIRE(addr[i] == 0); 138 ATF_REQUIRE(munlock(addr, len) == 0); 139 } 140 141 /* 142 * Make sure that we can munlock() a truncated mapping. 143 */ 144 ATF_TC_WITHOUT_HEAD(mlock__truncate_and_unlock); 145 ATF_TC_BODY(mlock__truncate_and_unlock, tc) 146 { 147 char filename[16]; 148 void *addr; 149 int fd, len; 150 151 snprintf(filename, sizeof(filename), "tmp.XXXXXX"); 152 fd = mkstemp(filename); 153 ATF_REQUIRE(fd >= 0); 154 ATF_REQUIRE(unlink(filename) == 0); 155 156 len = getpagesize(); 157 ATF_REQUIRE(ftruncate(fd, len) == 0); 158 159 addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 160 ATF_REQUIRE(addr != MAP_FAILED); 161 ATF_REQUIRE(mlock(addr, len) == 0); 162 ATF_REQUIRE(ftruncate(fd, 0) == 0); 163 ATF_REQUIRE(munlock(addr, len) == 0); 164 } 165 166 ATF_TP_ADD_TCS(tp) 167 { 168 ATF_TP_ADD_TC(tp, mlock__copy_on_write_anon); 169 ATF_TP_ADD_TC(tp, mlock__copy_on_write_vnode); 170 ATF_TP_ADD_TC(tp, mlock__truncate_and_resize); 171 ATF_TP_ADD_TC(tp, mlock__truncate_and_unlock); 172 173 return (atf_no_error()); 174 } 175