xref: /freebsd/tests/sys/vm/mlock_test.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/mman.h>
30 #include <sys/ptrace.h>
31 #include <sys/wait.h>
32 
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include <atf-c.h>
40 
41 static void
42 test_wired_copy_on_write(void *addr, size_t len)
43 {
44 	int status, val;
45 	pid_t pid;
46 
47 	pid = fork();
48 	if (pid == -1)
49 		atf_tc_fail("fork() failed: %s", strerror(errno));
50 	if (pid == 0) {
51 		if (mlock(addr, len) != 0)
52 			_exit(1);
53 		if (ptrace(PT_TRACE_ME, 0, NULL, 0) != 0)
54 			_exit(2);
55 		if (raise(SIGSTOP) != 0)
56 			_exit(3);
57 		if (munlock(addr, len) != 0)
58 			_exit(4);
59 		_exit(0);
60 	}
61 
62 	ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
63 	ATF_REQUIRE_MSG(!WIFEXITED(status),
64 	    "child exited with status %d", WEXITSTATUS(status));
65 	ATF_REQUIRE(WIFSTOPPED(status));
66 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
67 
68 	errno = 0;
69 	val = ptrace(PT_READ_D, pid, addr, 0);
70 	ATF_REQUIRE(errno == 0);
71 	ATF_REQUIRE(ptrace(PT_WRITE_D, pid, addr, val) == 0);
72 	ATF_REQUIRE(ptrace(PT_CONTINUE, pid, (caddr_t)1, 0) == 0);
73 	ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
74 	ATF_REQUIRE(WIFEXITED(status));
75 	ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
76 	    "child exited with status %d", WSTOPSIG(status));
77 }
78 
79 /*
80  * Use ptrace(2) to trigger a copy-on-write fault of anonymous memory.
81  */
82 ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_anon);
83 ATF_TC_BODY(mlock__copy_on_write_anon, tc)
84 {
85 	char *addr;
86 	int len;
87 
88 	len = getpagesize();
89 	addr = mmap(NULL, len, PROT_READ, MAP_ANON, -1, 0);
90 	ATF_REQUIRE(addr != MAP_FAILED);
91 
92 	test_wired_copy_on_write(addr, len);
93 }
94 
95 /*
96  * Use ptrace(2) to trigger a copy-on-write fault of a read-only text page.
97  */
98 ATF_TC_WITHOUT_HEAD(mlock__copy_on_write_vnode);
99 ATF_TC_BODY(mlock__copy_on_write_vnode, tc)
100 {
101 	void *addr;
102 	int len;
103 
104 	len = getpagesize();
105 	addr = (void *)((uintptr_t)test_wired_copy_on_write & ~(len - 1));
106 
107 	test_wired_copy_on_write(addr, len);
108 }
109 
110 /*
111  * Try truncating and then resizing an mlock()ed mapping.
112  */
113 ATF_TC_WITHOUT_HEAD(mlock__truncate_and_resize);
114 ATF_TC_BODY(mlock__truncate_and_resize, tc)
115 {
116 	char filename[16];
117 	char *addr;
118 	int fd, i, len;
119 
120 	snprintf(filename, sizeof(filename), "tmp.XXXXXX");
121 	fd = mkstemp(filename);
122 	ATF_REQUIRE(fd >= 0);
123 	ATF_REQUIRE(unlink(filename) == 0);
124 
125 	len = getpagesize();
126 	ATF_REQUIRE(ftruncate(fd, len) == 0);
127 
128 	addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
129 	ATF_REQUIRE(addr != MAP_FAILED);
130 	ATF_REQUIRE(mlock(addr, len) == 0);
131 	memset(addr, 1, len);
132 	ATF_REQUIRE(ftruncate(fd, 0) == 0);
133 	ATF_REQUIRE(ftruncate(fd, len) == 0);
134 	for (i = 0; i < len; i++)
135 		ATF_REQUIRE(addr[i] == 0);
136 	ATF_REQUIRE(munlock(addr, len) == 0);
137 }
138 
139 /*
140  * Make sure that we can munlock() a truncated mapping.
141  */
142 ATF_TC_WITHOUT_HEAD(mlock__truncate_and_unlock);
143 ATF_TC_BODY(mlock__truncate_and_unlock, tc)
144 {
145 	char filename[16];
146 	void *addr;
147 	int fd, len;
148 
149 	snprintf(filename, sizeof(filename), "tmp.XXXXXX");
150 	fd = mkstemp(filename);
151 	ATF_REQUIRE(fd >= 0);
152 	ATF_REQUIRE(unlink(filename) == 0);
153 
154 	len = getpagesize();
155 	ATF_REQUIRE(ftruncate(fd, len) == 0);
156 
157 	addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
158 	ATF_REQUIRE(addr != MAP_FAILED);
159 	ATF_REQUIRE(mlock(addr, len) == 0);
160 	ATF_REQUIRE(ftruncate(fd, 0) == 0);
161 	ATF_REQUIRE(munlock(addr, len) == 0);
162 }
163 
164 ATF_TP_ADD_TCS(tp)
165 {
166 	ATF_TP_ADD_TC(tp, mlock__copy_on_write_anon);
167 	ATF_TP_ADD_TC(tp, mlock__copy_on_write_vnode);
168 	ATF_TP_ADD_TC(tp, mlock__truncate_and_resize);
169 	ATF_TP_ADD_TC(tp, mlock__truncate_and_unlock);
170 
171 	return (atf_no_error());
172 }
173