xref: /freebsd/tests/sys/posixshm/posixshm_test.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/stat.h>
35 #include <sys/syscall.h>
36 #include <sys/sysctl.h>
37 #include <sys/wait.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include <atf-c.h>
49 
50 #define	TEST_PATH_LEN	256
51 static char test_path[TEST_PATH_LEN];
52 static char test_path2[TEST_PATH_LEN];
53 static unsigned int test_path_idx = 0;
54 
55 static void
56 gen_a_test_path(char *path)
57 {
58 	snprintf(path, TEST_PATH_LEN, "/%s/tmp.XXXXXX%d",
59 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"),
60 	    test_path_idx);
61 
62 	test_path_idx++;
63 
64 	ATF_REQUIRE_MSG(mkstemp(path) != -1,
65 	    "mkstemp failed; errno=%d", errno);
66 	ATF_REQUIRE_MSG(unlink(path) == 0,
67 	    "unlink failed; errno=%d", errno);
68 }
69 
70 static void
71 gen_test_path(void)
72 {
73 	gen_a_test_path(test_path);
74 }
75 
76 static void
77 gen_test_path2(void)
78 {
79 	gen_a_test_path(test_path2);
80 }
81 
82 /*
83  * Attempt a shm_open() that should fail with an expected error of 'error'.
84  */
85 static void
86 shm_open_should_fail(const char *path, int flags, mode_t mode, int error)
87 {
88 	int fd;
89 
90 	fd = shm_open(path, flags, mode);
91 	ATF_CHECK_MSG(fd == -1, "shm_open didn't fail");
92 	ATF_CHECK_MSG(error == errno,
93 	    "shm_open didn't fail with expected errno; errno=%d; expected "
94 	    "errno=%d", errno, error);
95 }
96 
97 /*
98  * Attempt a shm_unlink() that should fail with an expected error of 'error'.
99  */
100 static void
101 shm_unlink_should_fail(const char *path, int error)
102 {
103 
104 	ATF_CHECK_MSG(shm_unlink(path) == -1, "shm_unlink didn't fail");
105 	ATF_CHECK_MSG(error == errno,
106 	    "shm_unlink didn't fail with expected errno; errno=%d; expected "
107 	    "errno=%d", errno, error);
108 }
109 
110 /*
111  * Open the test object and write a value to the first byte.  Returns valid fd
112  * on success and -1 on failure.
113  */
114 static int
115 scribble_object(const char *path, char value)
116 {
117 	char *page;
118 	int fd, pagesize;
119 
120 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
121 
122 	fd = shm_open(path, O_CREAT|O_EXCL|O_RDWR, 0777);
123 	if (fd < 0 && errno == EEXIST) {
124 		if (shm_unlink(test_path) < 0)
125 			atf_tc_fail("shm_unlink");
126 		fd = shm_open(test_path, O_CREAT | O_EXCL | O_RDWR, 0777);
127 	}
128 	if (fd < 0)
129 		atf_tc_fail("shm_open failed; errno=%d", errno);
130 	if (ftruncate(fd, pagesize) < 0)
131 		atf_tc_fail("ftruncate failed; errno=%d", errno);
132 
133 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
134 	if (page == MAP_FAILED)
135 		atf_tc_fail("mmap failed; errno=%d", errno);
136 
137 	page[0] = value;
138 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
139 	    errno);
140 
141 	return (fd);
142 }
143 
144 /*
145  * Fail the test case if the 'path' does not refer to an shm whose first byte
146  * is equal to expected_value
147  */
148 static void
149 verify_object(const char *path, char expected_value)
150 {
151 	int fd;
152 	int pagesize;
153 	char *page;
154 
155 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
156 
157 	fd = shm_open(path, O_RDONLY, 0777);
158 	if (fd < 0)
159 		atf_tc_fail("shm_open failed in verify_object; errno=%d, path=%s",
160 		    errno, path);
161 
162 	page = mmap(0, pagesize, PROT_READ, MAP_SHARED, fd, 0);
163 	if (page == MAP_FAILED)
164 		atf_tc_fail("mmap(1)");
165 	if (page[0] != expected_value)
166 		atf_tc_fail("Renamed object has incorrect value; has"
167 		    "%d (0x%x, '%c'), expected %d (0x%x, '%c')\n",
168 		    page[0], page[0], isprint(page[0]) ? page[0] : ' ',
169 		    expected_value, expected_value,
170 		    isprint(expected_value) ? expected_value : ' ');
171 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
172 	    errno);
173 	close(fd);
174 }
175 
176 ATF_TC_WITHOUT_HEAD(remap_object);
177 ATF_TC_BODY(remap_object, tc)
178 {
179 	char *page;
180 	int fd, pagesize;
181 
182 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
183 
184 	gen_test_path();
185 	fd = scribble_object(test_path, '1');
186 
187 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
188 	if (page == MAP_FAILED)
189 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
190 
191 	if (page[0] != '1')
192 		atf_tc_fail("missing data ('%c' != '1')", page[0]);
193 
194 	close(fd);
195 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
196 	    errno);
197 
198 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
199 	    "shm_unlink failed; errno=%d", errno);
200 }
201 
202 ATF_TC_WITHOUT_HEAD(rename_from_anon);
203 ATF_TC_BODY(rename_from_anon, tc)
204 {
205 	int rc;
206 
207 	gen_test_path();
208 	rc = shm_rename(SHM_ANON, test_path, 0);
209 	if (rc != -1)
210 		atf_tc_fail("shm_rename from SHM_ANON succeeded unexpectedly");
211 }
212 
213 ATF_TC_WITHOUT_HEAD(rename_bad_path_pointer);
214 ATF_TC_BODY(rename_bad_path_pointer, tc)
215 {
216 	const char *bad_path;
217 	int rc;
218 
219 	bad_path = (const char *)0x1;
220 
221 	gen_test_path();
222 	rc = shm_rename(test_path, bad_path, 0);
223 	if (rc != -1)
224 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
225 
226 	rc = shm_rename(bad_path, test_path, 0);
227 	if (rc != -1)
228 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
229 }
230 
231 ATF_TC_WITHOUT_HEAD(rename_from_nonexisting);
232 ATF_TC_BODY(rename_from_nonexisting, tc)
233 {
234 	int rc;
235 
236 	gen_test_path();
237 	gen_test_path2();
238 	rc = shm_rename(test_path, test_path2, 0);
239 	if (rc != -1)
240 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
241 
242 	if (errno != ENOENT)
243 		atf_tc_fail("Expected ENOENT to rename of nonexistent shm; got %d",
244 		    errno);
245 }
246 
247 ATF_TC_WITHOUT_HEAD(rename_to_anon);
248 ATF_TC_BODY(rename_to_anon, tc)
249 {
250 	int rc;
251 
252 	gen_test_path();
253 	rc = shm_rename(test_path, SHM_ANON, 0);
254 	if (rc != -1)
255 		atf_tc_fail("shm_rename to SHM_ANON succeeded unexpectedly");
256 }
257 
258 ATF_TC_WITHOUT_HEAD(rename_to_replace);
259 ATF_TC_BODY(rename_to_replace, tc)
260 {
261 	char expected_value;
262 	int fd;
263 	int fd2;
264 
265 	// Some contents we can verify later
266 	expected_value = 'g';
267 
268 	gen_test_path();
269 	fd = scribble_object(test_path, expected_value);
270 	close(fd);
271 
272 	// Give the other some different value so we can detect success
273 	gen_test_path2();
274 	fd2 = scribble_object(test_path2, 'h');
275 	close(fd2);
276 
277 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2, 0) == 0,
278 	    "shm_rename failed; errno=%d", errno);
279 
280 	// Read back renamed; verify contents
281 	verify_object(test_path2, expected_value);
282 }
283 
284 ATF_TC_WITHOUT_HEAD(rename_to_noreplace);
285 ATF_TC_BODY(rename_to_noreplace, tc)
286 {
287 	char expected_value_from;
288 	char expected_value_to;
289 	int fd_from;
290 	int fd_to;
291 	int rc;
292 
293 	// Some contents we can verify later
294 	expected_value_from = 'g';
295 	gen_test_path();
296 	fd_from = scribble_object(test_path, expected_value_from);
297 	close(fd_from);
298 
299 	// Give the other some different value so we can detect success
300 	expected_value_to = 'h';
301 	gen_test_path2();
302 	fd_to = scribble_object(test_path2, expected_value_to);
303 	close(fd_to);
304 
305 	rc = shm_rename(test_path, test_path2, SHM_RENAME_NOREPLACE);
306 	ATF_REQUIRE_MSG((rc == -1) && (errno == EEXIST),
307 	    "shm_rename didn't fail as expected; errno: %d; return: %d", errno,
308 	    rc);
309 
310 	// Read back renamed; verify contents
311 	verify_object(test_path2, expected_value_to);
312 }
313 
314 ATF_TC_WITHOUT_HEAD(rename_to_exchange);
315 ATF_TC_BODY(rename_to_exchange, tc)
316 {
317 	char expected_value_from;
318 	char expected_value_to;
319 	int fd_from;
320 	int fd_to;
321 
322 	// Some contents we can verify later
323 	expected_value_from = 'g';
324 	gen_test_path();
325 	fd_from = scribble_object(test_path, expected_value_from);
326 	close(fd_from);
327 
328 	// Give the other some different value so we can detect success
329 	expected_value_to = 'h';
330 	gen_test_path2();
331 	fd_to = scribble_object(test_path2, expected_value_to);
332 	close(fd_to);
333 
334 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
335 	    SHM_RENAME_EXCHANGE) == 0,
336 	    "shm_rename failed; errno=%d", errno);
337 
338 	// Read back renamed; verify contents
339 	verify_object(test_path, expected_value_to);
340 	verify_object(test_path2, expected_value_from);
341 }
342 
343 ATF_TC_WITHOUT_HEAD(rename_to_exchange_nonexisting);
344 ATF_TC_BODY(rename_to_exchange_nonexisting, tc)
345 {
346 	char expected_value_from;
347 	int fd_from;
348 
349 	// Some contents we can verify later
350 	expected_value_from = 'g';
351 	gen_test_path();
352 	fd_from = scribble_object(test_path, expected_value_from);
353 	close(fd_from);
354 
355 	gen_test_path2();
356 
357 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
358 	    SHM_RENAME_EXCHANGE) == 0,
359 	    "shm_rename failed; errno=%d", errno);
360 
361 	// Read back renamed; verify contents
362 	verify_object(test_path2, expected_value_from);
363 }
364 
365 ATF_TC_WITHOUT_HEAD(rename_to_self);
366 ATF_TC_BODY(rename_to_self, tc)
367 {
368 	int fd;
369 	char expected_value;
370 
371 	expected_value = 't';
372 
373 	gen_test_path();
374 	fd = scribble_object(test_path, expected_value);
375 	close(fd);
376 
377 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path, 0) == 0,
378 	    "shm_rename failed; errno=%d", errno);
379 
380 	verify_object(test_path, expected_value);
381 }
382 
383 ATF_TC_WITHOUT_HEAD(rename_bad_flag);
384 ATF_TC_BODY(rename_bad_flag, tc)
385 {
386 	int fd;
387 	int rc;
388 
389 	/* Make sure we don't fail out due to ENOENT */
390 	gen_test_path();
391 	gen_test_path2();
392 	fd = scribble_object(test_path, 'd');
393 	close(fd);
394 	fd = scribble_object(test_path2, 'd');
395 	close(fd);
396 
397 	/*
398 	 * Note: if we end up with enough flags that we use all the bits,
399 	 * then remove this test completely.
400 	 */
401 	rc = shm_rename(test_path, test_path2, INT_MIN);
402 	ATF_REQUIRE_MSG((rc == -1) && (errno == EINVAL),
403 	    "shm_rename should have failed with EINVAL; got: return=%d, "
404 	    "errno=%d", rc, errno);
405 }
406 
407 ATF_TC_WITHOUT_HEAD(reopen_object);
408 ATF_TC_BODY(reopen_object, tc)
409 {
410 	char *page;
411 	int fd, pagesize;
412 
413 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
414 
415 	gen_test_path();
416 	fd = scribble_object(test_path, '1');
417 	close(fd);
418 
419 	fd = shm_open(test_path, O_RDONLY, 0777);
420 	if (fd < 0)
421 		atf_tc_fail("shm_open(2) failed; errno=%d", errno);
422 
423 	page = mmap(0, pagesize, PROT_READ, MAP_SHARED, fd, 0);
424 	if (page == MAP_FAILED)
425 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
426 
427 	if (page[0] != '1')
428 		atf_tc_fail("missing data ('%c' != '1')", page[0]);
429 
430 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
431 	    errno);
432 	close(fd);
433 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
434 	    "shm_unlink failed; errno=%d", errno);
435 }
436 
437 ATF_TC_WITHOUT_HEAD(readonly_mmap_write);
438 ATF_TC_BODY(readonly_mmap_write, tc)
439 {
440 	char *page;
441 	int fd, pagesize;
442 
443 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
444 
445 	gen_test_path();
446 
447 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
448 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
449 
450 	/* PROT_WRITE should fail with EACCES. */
451 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
452 	if (page != MAP_FAILED)
453 		atf_tc_fail("mmap(PROT_WRITE) succeeded unexpectedly");
454 
455 	if (errno != EACCES)
456 		atf_tc_fail("mmap(PROT_WRITE) didn't fail with EACCES; "
457 		    "errno=%d", errno);
458 
459 	close(fd);
460 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
461 	    "shm_unlink failed; errno=%d", errno);
462 }
463 
464 ATF_TC_WITHOUT_HEAD(open_after_link);
465 ATF_TC_BODY(open_after_link, tc)
466 {
467 	int fd;
468 
469 	gen_test_path();
470 
471 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
472 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
473 	close(fd);
474 
475 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1, "shm_unlink failed: %d",
476 	    errno);
477 
478 	shm_open_should_fail(test_path, O_RDONLY, 0777, ENOENT);
479 }
480 
481 ATF_TC_WITHOUT_HEAD(open_invalid_path);
482 ATF_TC_BODY(open_invalid_path, tc)
483 {
484 
485 	shm_open_should_fail("blah", O_RDONLY, 0777, EINVAL);
486 }
487 
488 ATF_TC_WITHOUT_HEAD(open_write_only);
489 ATF_TC_BODY(open_write_only, tc)
490 {
491 
492 	gen_test_path();
493 
494 	shm_open_should_fail(test_path, O_WRONLY, 0777, EINVAL);
495 }
496 
497 ATF_TC_WITHOUT_HEAD(open_extra_flags);
498 ATF_TC_BODY(open_extra_flags, tc)
499 {
500 
501 	gen_test_path();
502 
503 	shm_open_should_fail(test_path, O_RDONLY | O_DIRECT, 0777, EINVAL);
504 }
505 
506 ATF_TC_WITHOUT_HEAD(open_anon);
507 ATF_TC_BODY(open_anon, tc)
508 {
509 	int fd;
510 
511 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
512 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
513 	close(fd);
514 }
515 
516 ATF_TC_WITHOUT_HEAD(open_anon_readonly);
517 ATF_TC_BODY(open_anon_readonly, tc)
518 {
519 
520 	shm_open_should_fail(SHM_ANON, O_RDONLY, 0777, EINVAL);
521 }
522 
523 ATF_TC_WITHOUT_HEAD(open_bad_path_pointer);
524 ATF_TC_BODY(open_bad_path_pointer, tc)
525 {
526 
527 	shm_open_should_fail((char *)1024, O_RDONLY, 0777, EFAULT);
528 }
529 
530 ATF_TC_WITHOUT_HEAD(open_path_too_long);
531 ATF_TC_BODY(open_path_too_long, tc)
532 {
533 	char *page;
534 
535 	page = malloc(MAXPATHLEN + 1);
536 	memset(page, 'a', MAXPATHLEN);
537 	page[MAXPATHLEN] = '\0';
538 	shm_open_should_fail(page, O_RDONLY, 0777, ENAMETOOLONG);
539 	free(page);
540 }
541 
542 ATF_TC_WITHOUT_HEAD(open_nonexisting_object);
543 ATF_TC_BODY(open_nonexisting_object, tc)
544 {
545 
546 	shm_open_should_fail("/notreallythere", O_RDONLY, 0777, ENOENT);
547 }
548 
549 ATF_TC_WITHOUT_HEAD(open_create_existing_object);
550 ATF_TC_BODY(open_create_existing_object, tc)
551 {
552 	int fd;
553 
554 	gen_test_path();
555 
556 	fd = shm_open(test_path, O_RDONLY|O_CREAT, 0777);
557 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
558 	close(fd);
559 
560 	shm_open_should_fail(test_path, O_RDONLY|O_CREAT|O_EXCL,
561 	    0777, EEXIST);
562 
563 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
564 	    "shm_unlink failed; errno=%d", errno);
565 }
566 
567 ATF_TC_WITHOUT_HEAD(trunc_resets_object);
568 ATF_TC_BODY(trunc_resets_object, tc)
569 {
570 	struct stat sb;
571 	int fd;
572 
573 	gen_test_path();
574 
575 	/* Create object and set size to 1024. */
576 	fd = shm_open(test_path, O_RDWR | O_CREAT, 0777);
577 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
578 	ATF_REQUIRE_MSG(ftruncate(fd, 1024) != -1,
579 	    "ftruncate failed; errno=%d", errno);
580 	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
581 	    "fstat(1) failed; errno=%d", errno);
582 	ATF_REQUIRE_MSG(sb.st_size == 1024, "size %d != 1024", (int)sb.st_size);
583 	close(fd);
584 
585 	/* Open with O_TRUNC which should reset size to 0. */
586 	fd = shm_open(test_path, O_RDWR | O_TRUNC, 0777);
587 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(2) failed; errno=%d", errno);
588 	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
589 	    "fstat(2) failed; errno=%d", errno);
590 	ATF_REQUIRE_MSG(sb.st_size == 0,
591 	    "size was not 0 after truncation: %d", (int)sb.st_size);
592 	close(fd);
593 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
594 	    "shm_unlink failed; errno=%d", errno);
595 }
596 
597 ATF_TC_WITHOUT_HEAD(unlink_bad_path_pointer);
598 ATF_TC_BODY(unlink_bad_path_pointer, tc)
599 {
600 
601 	shm_unlink_should_fail((char *)1024, EFAULT);
602 }
603 
604 ATF_TC_WITHOUT_HEAD(unlink_path_too_long);
605 ATF_TC_BODY(unlink_path_too_long, tc)
606 {
607 	char *page;
608 
609 	page = malloc(MAXPATHLEN + 1);
610 	memset(page, 'a', MAXPATHLEN);
611 	page[MAXPATHLEN] = '\0';
612 	shm_unlink_should_fail(page, ENAMETOOLONG);
613 	free(page);
614 }
615 
616 ATF_TC_WITHOUT_HEAD(object_resize);
617 ATF_TC_BODY(object_resize, tc)
618 {
619 	pid_t pid;
620 	struct stat sb;
621 	char *page;
622 	int fd, pagesize, status;
623 
624 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
625 
626 	/* Start off with a size of a single page. */
627 	fd = shm_open(SHM_ANON, O_CREAT|O_RDWR, 0777);
628 	if (fd < 0)
629 		atf_tc_fail("shm_open failed; errno=%d", errno);
630 
631 	if (ftruncate(fd, pagesize) < 0)
632 		atf_tc_fail("ftruncate(1) failed; errno=%d", errno);
633 
634 	if (fstat(fd, &sb) < 0)
635 		atf_tc_fail("fstat(1) failed; errno=%d", errno);
636 
637 	if (sb.st_size != pagesize)
638 		atf_tc_fail("first resize failed (%d != %d)",
639 		    (int)sb.st_size, pagesize);
640 
641 	/* Write a '1' to the first byte. */
642 	page = mmap(0, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
643 	if (page == MAP_FAILED)
644 		atf_tc_fail("mmap(1)");
645 
646 	page[0] = '1';
647 
648 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
649 	    errno);
650 
651 	/* Grow the object to 2 pages. */
652 	if (ftruncate(fd, pagesize * 2) < 0)
653 		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
654 
655 	if (fstat(fd, &sb) < 0)
656 		atf_tc_fail("fstat(2) failed; errno=%d", errno);
657 
658 	if (sb.st_size != pagesize * 2)
659 		atf_tc_fail("second resize failed (%d != %d)",
660 		    (int)sb.st_size, pagesize * 2);
661 
662 	/* Check for '1' at the first byte. */
663 	page = mmap(0, pagesize * 2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
664 	if (page == MAP_FAILED)
665 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
666 
667 	if (page[0] != '1')
668 		atf_tc_fail("'%c' != '1'", page[0]);
669 
670 	/* Write a '2' at the start of the second page. */
671 	page[pagesize] = '2';
672 
673 	/* Shrink the object back to 1 page. */
674 	if (ftruncate(fd, pagesize) < 0)
675 		atf_tc_fail("ftruncate(3) failed; errno=%d", errno);
676 
677 	if (fstat(fd, &sb) < 0)
678 		atf_tc_fail("fstat(3) failed; errno=%d", errno);
679 
680 	if (sb.st_size != pagesize)
681 		atf_tc_fail("third resize failed (%d != %d)",
682 		    (int)sb.st_size, pagesize);
683 
684 	/*
685 	 * Fork a child process to make sure the second page is no
686 	 * longer valid.
687 	 */
688 	pid = fork();
689 	if (pid == -1)
690 		atf_tc_fail("fork failed; errno=%d", errno);
691 
692 	if (pid == 0) {
693 		struct rlimit lim;
694 		char c;
695 
696 		/* Don't generate a core dump. */
697 		ATF_REQUIRE(getrlimit(RLIMIT_CORE, &lim) == 0);
698 		lim.rlim_cur = 0;
699 		ATF_REQUIRE(setrlimit(RLIMIT_CORE, &lim) == 0);
700 
701 		/*
702 		 * The previous ftruncate(2) shrunk the backing object
703 		 * so that this address is no longer valid, so reading
704 		 * from it should trigger a SIGBUS.
705 		 */
706 		c = page[pagesize];
707 		fprintf(stderr, "child: page 1: '%c'\n", c);
708 		exit(0);
709 	}
710 
711 	if (wait(&status) < 0)
712 		atf_tc_fail("wait failed; errno=%d", errno);
713 
714 	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGBUS)
715 		atf_tc_fail("child terminated with status %x", status);
716 
717 	/* Grow the object back to 2 pages. */
718 	if (ftruncate(fd, pagesize * 2) < 0)
719 		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
720 
721 	if (fstat(fd, &sb) < 0)
722 		atf_tc_fail("fstat(2) failed; errno=%d", errno);
723 
724 	if (sb.st_size != pagesize * 2)
725 		atf_tc_fail("fourth resize failed (%d != %d)",
726 		    (int)sb.st_size, pagesize);
727 
728 	/*
729 	 * Note that the mapping at 'page' for the second page is
730 	 * still valid, and now that the shm object has been grown
731 	 * back up to 2 pages, there is now memory backing this page
732 	 * so the read will work.  However, the data should be zero
733 	 * rather than '2' as the old data was thrown away when the
734 	 * object was shrunk and the new pages when an object are
735 	 * grown are zero-filled.
736 	 */
737 	if (page[pagesize] != 0)
738 		atf_tc_fail("invalid data at %d: %x != 0",
739 		    pagesize, (int)page[pagesize]);
740 
741 	close(fd);
742 }
743 
744 /* Signal handler which does nothing. */
745 static void
746 ignoreit(int sig __unused)
747 {
748 	;
749 }
750 
751 ATF_TC_WITHOUT_HEAD(shm_functionality_across_fork);
752 ATF_TC_BODY(shm_functionality_across_fork, tc)
753 {
754 	char *cp, c;
755 	int error, desc, rv;
756 	long scval;
757 	sigset_t ss;
758 	struct sigaction sa;
759 	void *region;
760 	size_t i, psize;
761 
762 #ifndef _POSIX_SHARED_MEMORY_OBJECTS
763 	printf("_POSIX_SHARED_MEMORY_OBJECTS is undefined\n");
764 #else
765 	printf("_POSIX_SHARED_MEMORY_OBJECTS is defined as %ld\n",
766 	       (long)_POSIX_SHARED_MEMORY_OBJECTS - 0);
767 	if (_POSIX_SHARED_MEMORY_OBJECTS - 0 == -1)
768 		printf("***Indicates this feature may be unsupported!\n");
769 #endif
770 	errno = 0;
771 	scval = sysconf(_SC_SHARED_MEMORY_OBJECTS);
772 	if (scval == -1 && errno != 0) {
773 		atf_tc_fail("sysconf(_SC_SHARED_MEMORY_OBJECTS) failed; "
774 		    "errno=%d", errno);
775 	} else {
776 		printf("sysconf(_SC_SHARED_MEMORY_OBJECTS) returns %ld\n",
777 		       scval);
778 		if (scval == -1)
779 			printf("***Indicates this feature is unsupported!\n");
780 	}
781 
782 	errno = 0;
783 	scval = sysconf(_SC_PAGESIZE);
784 	if (scval == -1 && errno != 0) {
785 		atf_tc_fail("sysconf(_SC_PAGESIZE) failed; errno=%d", errno);
786 	} else if (scval <= 0) {
787 		fprintf(stderr, "bogus return from sysconf(_SC_PAGESIZE): %ld",
788 		    scval);
789 		psize = 4096;
790 	} else {
791 		printf("sysconf(_SC_PAGESIZE) returns %ld\n", scval);
792 		psize = scval;
793 	}
794 
795 	gen_test_path();
796 	desc = shm_open(test_path, O_EXCL | O_CREAT | O_RDWR, 0600);
797 
798 	ATF_REQUIRE_MSG(desc >= 0, "shm_open failed; errno=%d", errno);
799 	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
800 	    "shm_unlink failed; errno=%d", errno);
801 	ATF_REQUIRE_MSG(ftruncate(desc, (off_t)psize) != -1,
802 	    "ftruncate failed; errno=%d", errno);
803 
804 	region = mmap(NULL, psize, PROT_READ | PROT_WRITE, MAP_SHARED, desc, 0);
805 	ATF_REQUIRE_MSG(region != MAP_FAILED, "mmap failed; errno=%d", errno);
806 	memset(region, '\377', psize);
807 
808 	sa.sa_flags = 0;
809 	sa.sa_handler = ignoreit;
810 	sigemptyset(&sa.sa_mask);
811 	ATF_REQUIRE_MSG(sigaction(SIGUSR1, &sa, (struct sigaction *)0) == 0,
812 	    "sigaction failed; errno=%d", errno);
813 
814 	sigemptyset(&ss);
815 	sigaddset(&ss, SIGUSR1);
816 	ATF_REQUIRE_MSG(sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0) == 0,
817 	    "sigprocmask failed; errno=%d", errno);
818 
819 	rv = fork();
820 	ATF_REQUIRE_MSG(rv != -1, "fork failed; errno=%d", errno);
821 	if (rv == 0) {
822 		sigemptyset(&ss);
823 		sigsuspend(&ss);
824 
825 		for (cp = region; cp < (char *)region + psize; cp++) {
826 			if (*cp != '\151')
827 				_exit(1);
828 		}
829 		if (lseek(desc, 0, SEEK_SET) == -1)
830 			_exit(1);
831 		for (i = 0; i < psize; i++) {
832 			error = read(desc, &c, 1);
833 			if (c != '\151')
834 				_exit(1);
835 		}
836 		_exit(0);
837 	} else {
838 		int status;
839 
840 		memset(region, '\151', psize - 2);
841 		error = pwrite(desc, region, 2, psize - 2);
842 		if (error != 2) {
843 			if (error >= 0)
844 				atf_tc_fail("short write; %d bytes written",
845 				    error);
846 			else
847 				atf_tc_fail("shmfd write");
848 		}
849 		kill(rv, SIGUSR1);
850 		waitpid(rv, &status, 0);
851 
852 		if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
853 			printf("Functionality test successful\n");
854 		} else if (WIFEXITED(status)) {
855 			atf_tc_fail("Child process exited with status %d",
856 			    WEXITSTATUS(status));
857 		} else {
858 			atf_tc_fail("Child process terminated with %s",
859 			    strsignal(WTERMSIG(status)));
860 		}
861 	}
862 
863 	ATF_REQUIRE_MSG(munmap(region, psize) == 0, "munmap failed; errno=%d",
864 	    errno);
865 	shm_unlink(test_path);
866 }
867 
868 ATF_TC_WITHOUT_HEAD(cloexec);
869 ATF_TC_BODY(cloexec, tc)
870 {
871 	int fd;
872 
873 	gen_test_path();
874 
875 	/* shm_open(2) is required to set FD_CLOEXEC */
876 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
877 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
878 	ATF_REQUIRE((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0);
879 	close(fd);
880 
881 	/* Also make sure that named shm is correct */
882 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0600);
883 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
884 	ATF_REQUIRE((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0);
885 	close(fd);
886 }
887 
888 ATF_TC_WITHOUT_HEAD(mode);
889 ATF_TC_BODY(mode, tc)
890 {
891 	struct stat st;
892 	int fd;
893 	mode_t restore_mask;
894 
895 	gen_test_path();
896 
897 	/* Remove inhibitions from umask */
898 	restore_mask = umask(0);
899 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0600);
900 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
901 	ATF_REQUIRE(fstat(fd, &st) == 0);
902 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0600);
903 	close(fd);
904 	ATF_REQUIRE(shm_unlink(test_path) == 0);
905 
906 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0660);
907 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
908 	ATF_REQUIRE(fstat(fd, &st) == 0);
909 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0660);
910 	close(fd);
911 	ATF_REQUIRE(shm_unlink(test_path) == 0);
912 
913 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0666);
914 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
915 	ATF_REQUIRE(fstat(fd, &st) == 0);
916 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0666);
917 	close(fd);
918 	ATF_REQUIRE(shm_unlink(test_path) == 0);
919 
920 	umask(restore_mask);
921 }
922 
923 ATF_TC_WITHOUT_HEAD(fallocate);
924 ATF_TC_BODY(fallocate, tc)
925 {
926 	struct stat st;
927 	int error, fd, sz;
928 
929 	/*
930 	 * Primitive test case for posix_fallocate with shmd.  Effectively
931 	 * expected to work like a smarter ftruncate that will grow the region
932 	 * as needed in a race-free way.
933 	 */
934 	fd = shm_open(SHM_ANON, O_RDWR, 0666);
935 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
936 	/* Set the initial size. */
937 	sz = 32;
938 	ATF_REQUIRE(ftruncate(fd, sz) == 0);
939 
940 	/* Now grow it. */
941 	error = 0;
942 	sz *= 2;
943 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz)) == 0,
944 	    "posix_fallocate failed; error=%d", error);
945 	ATF_REQUIRE(fstat(fd, &st) == 0);
946 	ATF_REQUIRE(st.st_size == sz);
947 	/* Attempt to shrink it; should succeed, but not change the size. */
948 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz / 2)) == 0,
949 	    "posix_fallocate failed; error=%d", error);
950 	ATF_REQUIRE(fstat(fd, &st) == 0);
951 	ATF_REQUIRE(st.st_size == sz);
952 	/* Grow it using an offset of sz and len of sz. */
953 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, sz, sz)) == 0,
954 	    "posix_fallocate failed; error=%d", error);
955 	ATF_REQUIRE(fstat(fd, &st) == 0);
956 	ATF_REQUIRE(st.st_size == (sz * 2));
957 
958 	close(fd);
959 }
960 
961 static int
962 shm_open_large(int psind, int policy, size_t sz)
963 {
964 	int error, fd;
965 
966 	fd = shm_create_largepage(SHM_ANON, O_CREAT | O_RDWR, psind, policy, 0);
967 	if (fd < 0 && errno == ENOTTY)
968 		atf_tc_skip("no large page support");
969 	ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; errno=%d", errno);
970 
971 	error = ftruncate(fd, sz);
972 	if (error != 0 && errno == ENOMEM)
973 		/*
974 		 * The test system might not have enough memory to accommodate
975 		 * the request.
976 		 */
977 		atf_tc_skip("failed to allocate %zu-byte superpage", sz);
978 	ATF_REQUIRE_MSG(error == 0, "ftruncate failed; errno=%d", errno);
979 
980 	return (fd);
981 }
982 
983 static int
984 pagesizes(size_t ps[MAXPAGESIZES])
985 {
986 	int pscnt;
987 
988 	pscnt = getpagesizes(ps, MAXPAGESIZES);
989 	ATF_REQUIRE_MSG(pscnt != -1, "getpagesizes failed; errno=%d", errno);
990 	ATF_REQUIRE_MSG(ps[0] == PAGE_SIZE, "psind 0 is %zu", ps[0]);
991 	ATF_REQUIRE_MSG(pscnt <= MAXPAGESIZES, "invalid pscnt %d", pscnt);
992 	if (pscnt == 1)
993 		atf_tc_skip("no large page support");
994 	return (pscnt);
995 }
996 
997 ATF_TC_WITHOUT_HEAD(largepage_basic);
998 ATF_TC_BODY(largepage_basic, tc)
999 {
1000 	char zeroes[PAGE_SIZE];
1001 	char *addr, *vec;
1002 	size_t ps[MAXPAGESIZES];
1003 	int error, fd, pscnt;
1004 
1005 	memset(zeroes, 0, PAGE_SIZE);
1006 
1007 	pscnt = pagesizes(ps);
1008 	for (int i = 1; i < pscnt; i++) {
1009 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1010 
1011 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1012 		    0);
1013 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1014 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1015 		ATF_REQUIRE_MSG(((uintptr_t)addr & (ps[i] - 1)) == 0,
1016 		    "mmap(%zu bytes) returned unaligned mapping; addr=%p",
1017 		    ps[i], addr);
1018 
1019 		/* Force a page fault. */
1020 		*(volatile char *)addr = 0;
1021 
1022 		vec = malloc(ps[i] / PAGE_SIZE);
1023 		ATF_REQUIRE(vec != NULL);
1024 		error = mincore(addr, ps[i], vec);
1025 		ATF_REQUIRE_MSG(error == 0, "mincore failed; errno=%d", errno);
1026 
1027 		/* Verify that all pages in the run are mapped. */
1028 		for (size_t p = 0; p < ps[i] / PAGE_SIZE; p++) {
1029 			ATF_REQUIRE_MSG((vec[p] & MINCORE_INCORE) != 0,
1030 			    "page %zu is not mapped", p);
1031 			ATF_REQUIRE_MSG((vec[p] & MINCORE_PSIND(i)) != 0,
1032 			    "page %zu is not in a %zu-byte superpage",
1033 			    p, ps[i]);
1034 		}
1035 
1036 		/* Validate zeroing. */
1037 		for (size_t p = 0; p < ps[i] / PAGE_SIZE; p++) {
1038 			ATF_REQUIRE_MSG(memcmp(addr + p * PAGE_SIZE, zeroes,
1039 			    PAGE_SIZE) == 0, "page %zu miscompare", p);
1040 		}
1041 
1042 		free(vec);
1043 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1044 		ATF_REQUIRE(close(fd) == 0);
1045 	}
1046 }
1047 
1048 extern int __sys_shm_open2(const char *, int, mode_t, int, const char *);
1049 
1050 ATF_TC_WITHOUT_HEAD(largepage_config);
1051 ATF_TC_BODY(largepage_config, tc)
1052 {
1053 	struct shm_largepage_conf lpc;
1054 	char *addr, *buf;
1055 	size_t ps[MAXPAGESIZES + 1]; /* silence warnings if MAXPAGESIZES == 1 */
1056 	int error, fd, pscnt;
1057 
1058 	pscnt = pagesizes(ps);
1059 
1060 	fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0);
1061 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
1062 
1063 	/*
1064 	 * Configure a large page policy for an object created without
1065 	 * SHM_LARGEPAGE.
1066 	 */
1067 	lpc.psind = 1;
1068 	lpc.alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT;
1069 	error = ioctl(fd, FIOSSHMLPGCNF, &lpc);
1070 	ATF_REQUIRE(error != 0);
1071 	ATF_REQUIRE_MSG(errno == ENOTTY, "ioctl(FIOSSHMLPGCNF) returned %d",
1072 	    errno);
1073 	ATF_REQUIRE(close(fd) == 0);
1074 
1075 	/*
1076 	 * Create a largepage object and try to use it without actually
1077 	 * configuring anything.
1078 	 */
1079 	fd = __sys_shm_open2(SHM_ANON, O_CREAT | O_RDWR, 0, SHM_LARGEPAGE,
1080 	    NULL);
1081 	if (fd < 0 && errno == ENOTTY)
1082 		atf_tc_skip("no large page support");
1083 	ATF_REQUIRE_MSG(fd >= 0, "shm_open2 failed; error=%d", errno);
1084 
1085 	error = ftruncate(fd, ps[1]);
1086 	ATF_REQUIRE(error != 0);
1087 	ATF_REQUIRE_MSG(errno == EINVAL, "ftruncate returned %d", errno);
1088 
1089 	addr = mmap(NULL, ps[1], PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1090 	ATF_REQUIRE(addr == MAP_FAILED);
1091 	ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1092 	addr = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1093 	ATF_REQUIRE(addr == MAP_FAILED);
1094 	ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1095 
1096 	buf = calloc(1, PAGE_SIZE);
1097 	ATF_REQUIRE(buf != NULL);
1098 	ATF_REQUIRE(write(fd, buf, PAGE_SIZE) == -1);
1099 	ATF_REQUIRE_MSG(errno == EINVAL, "write returned %d", errno);
1100 	free(buf);
1101 	buf = calloc(1, ps[1]);
1102 	ATF_REQUIRE(buf != NULL);
1103 	ATF_REQUIRE(write(fd, buf, ps[1]) == -1);
1104 	ATF_REQUIRE_MSG(errno == EINVAL, "write returned %d", errno);
1105 	free(buf);
1106 
1107 	error = posix_fallocate(fd, 0, PAGE_SIZE);
1108 	ATF_REQUIRE_MSG(error == EINVAL, "posix_fallocate returned %d", error);
1109 
1110 	ATF_REQUIRE(close(fd) == 0);
1111 }
1112 
1113 ATF_TC_WITHOUT_HEAD(largepage_mmap);
1114 ATF_TC_BODY(largepage_mmap, tc)
1115 {
1116 	char *addr, *addr1, *vec;
1117 	size_t ps[MAXPAGESIZES];
1118 	int fd, pscnt;
1119 
1120 	pscnt = pagesizes(ps);
1121 	for (int i = 1; i < pscnt; i++) {
1122 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1123 
1124 		/* For mincore(). */
1125 		vec = malloc(ps[i]);
1126 		ATF_REQUIRE(vec != NULL);
1127 
1128 		/*
1129 		 * Wrong mapping size.
1130 		 */
1131 		addr = mmap(NULL, ps[i - 1], PROT_READ | PROT_WRITE, MAP_SHARED,
1132 		    fd, 0);
1133 		ATF_REQUIRE_MSG(addr == MAP_FAILED,
1134 		    "mmap(%zu bytes) succeeded", ps[i - 1]);
1135 		ATF_REQUIRE_MSG(errno == EINVAL,
1136 		    "mmap(%zu bytes) failed; error=%d", ps[i - 1], errno);
1137 
1138 		/*
1139 		 * Fixed mappings.
1140 		 */
1141 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1142 		    0);
1143 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1144 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1145 		ATF_REQUIRE_MSG(((uintptr_t)addr & (ps[i] - 1)) == 0,
1146 		    "mmap(%zu bytes) returned unaligned mapping; addr=%p",
1147 		    ps[i], addr);
1148 
1149 		/* Try mapping a small page with anonymous memory. */
1150 		addr1 = mmap(addr, ps[i - 1], PROT_READ | PROT_WRITE,
1151 		    MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
1152 		ATF_REQUIRE_MSG(addr1 == MAP_FAILED,
1153 		    "anon mmap(%zu bytes) succeeded", ps[i - 1]);
1154 		ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1155 
1156 		/* Check MAP_EXCL when creating a second largepage mapping. */
1157 		addr1 = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
1158 		    MAP_SHARED | MAP_FIXED | MAP_EXCL, fd, 0);
1159 		ATF_REQUIRE_MSG(addr1 == MAP_FAILED,
1160 		    "mmap(%zu bytes) succeeded", ps[i]);
1161 		/* XXX wrong errno */
1162 		ATF_REQUIRE_MSG(errno == ENOSPC, "mmap returned %d", errno);
1163 
1164 		/* Overwrite a largepage mapping with a lagepage mapping. */
1165 		addr1 = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
1166 		    MAP_SHARED | MAP_FIXED, fd, 0);
1167 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1168 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1169 		ATF_REQUIRE_MSG(addr == addr1,
1170 		    "mmap(%zu bytes) moved from %p to %p", ps[i], addr, addr1);
1171 
1172 		ATF_REQUIRE(munmap(addr, ps[i] == 0));
1173 
1174 		/* Clobber an anonymous mapping with a superpage. */
1175 		addr1 = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE,
1176 		    MAP_ANON | MAP_PRIVATE | MAP_ALIGNED(ffsl(ps[i]) - 1), -1,
1177 		    0);
1178 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1179 		    "mmap failed; error=%d", errno);
1180 		*(volatile char *)addr1 = '\0';
1181 		addr = mmap(addr1, ps[i], PROT_READ | PROT_WRITE,
1182 		    MAP_SHARED | MAP_FIXED, fd, 0);
1183 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1184 		    "mmap failed; error=%d", errno);
1185 		ATF_REQUIRE_MSG(addr == addr1,
1186 		    "mmap disobeyed MAP_FIXED, %p %p", addr, addr1);
1187 		*(volatile char *)addr = 0; /* fault */
1188 		ATF_REQUIRE(mincore(addr, ps[i], vec) == 0);
1189 		for (size_t p = 0; p < ps[i] / PAGE_SIZE; p++) {
1190 			ATF_REQUIRE_MSG((vec[p] & MINCORE_INCORE) != 0,
1191 			    "page %zu is not resident", p);
1192 			ATF_REQUIRE_MSG((vec[p] & MINCORE_PSIND(i)) != 0,
1193 			    "page %zu is not resident", p);
1194 		}
1195 
1196 		/*
1197 		 * Copy-on-write mappings are not permitted.
1198 		 */
1199 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_PRIVATE,
1200 		    fd, 0);
1201 		ATF_REQUIRE_MSG(addr == MAP_FAILED,
1202 		    "mmap(%zu bytes) succeeded", ps[i]);
1203 
1204 		ATF_REQUIRE(close(fd) == 0);
1205 	}
1206 }
1207 
1208 ATF_TC_WITHOUT_HEAD(largepage_munmap);
1209 ATF_TC_BODY(largepage_munmap, tc)
1210 {
1211 	char *addr;
1212 	size_t ps[MAXPAGESIZES], ps1;
1213 	int fd, pscnt;
1214 
1215 	pscnt = pagesizes(ps);
1216 	for (int i = 1; i < pscnt; i++) {
1217 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1218 		ps1 = ps[i - 1];
1219 
1220 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1221 		    0);
1222 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1223 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1224 
1225 		/* Try several unaligned munmap() requests. */
1226 		ATF_REQUIRE(munmap(addr, ps1) != 0);
1227 		ATF_REQUIRE_MSG(errno == EINVAL,
1228 		    "unexpected error %d from munmap", errno);
1229 		ATF_REQUIRE(munmap(addr, ps[i] - ps1));
1230 		ATF_REQUIRE_MSG(errno == EINVAL,
1231 		    "unexpected error %d from munmap", errno);
1232 		ATF_REQUIRE(munmap(addr + ps1, ps1) != 0);
1233 		ATF_REQUIRE_MSG(errno == EINVAL,
1234 		    "unexpected error %d from munmap", errno);
1235 		ATF_REQUIRE(munmap(addr, 0));
1236 		ATF_REQUIRE_MSG(errno == EINVAL,
1237 		    "unexpected error %d from munmap", errno);
1238 
1239 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1240 		ATF_REQUIRE(close(fd) == 0);
1241 	}
1242 }
1243 
1244 static void
1245 largepage_madvise(char *addr, size_t sz, int advice, int error)
1246 {
1247 	if (error == 0) {
1248 		ATF_REQUIRE_MSG(madvise(addr, sz, advice) == 0,
1249 		    "madvise(%zu, %d) failed; error=%d", sz, advice, errno);
1250 	} else {
1251 		ATF_REQUIRE_MSG(madvise(addr, sz, advice) != 0,
1252 		    "madvise(%zu, %d) succeeded", sz, advice);
1253 		ATF_REQUIRE_MSG(errno == error,
1254 		    "unexpected error %d from madvise(%zu, %d)",
1255 		    errno, sz, advice);
1256 	}
1257 }
1258 
1259 ATF_TC_WITHOUT_HEAD(largepage_madvise);
1260 ATF_TC_BODY(largepage_madvise, tc)
1261 {
1262 	char *addr;
1263 	size_t ps[MAXPAGESIZES];
1264 	int fd, pscnt;
1265 
1266 	pscnt = pagesizes(ps);
1267 	for (int i = 1; i < pscnt; i++) {
1268 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1269 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1270 		    0);
1271 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1272 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1273 
1274 		/* Advice that requires clipping. */
1275 		largepage_madvise(addr, PAGE_SIZE, MADV_NORMAL, EINVAL);
1276 		largepage_madvise(addr, ps[i], MADV_NORMAL, 0);
1277 		largepage_madvise(addr, PAGE_SIZE, MADV_RANDOM, EINVAL);
1278 		largepage_madvise(addr, ps[i], MADV_RANDOM, 0);
1279 		largepage_madvise(addr, PAGE_SIZE, MADV_SEQUENTIAL, EINVAL);
1280 		largepage_madvise(addr, ps[i], MADV_SEQUENTIAL, 0);
1281 		largepage_madvise(addr, PAGE_SIZE, MADV_NOSYNC, EINVAL);
1282 		largepage_madvise(addr, ps[i], MADV_NOSYNC, 0);
1283 		largepage_madvise(addr, PAGE_SIZE, MADV_AUTOSYNC, EINVAL);
1284 		largepage_madvise(addr, ps[i], MADV_AUTOSYNC, 0);
1285 		largepage_madvise(addr, PAGE_SIZE, MADV_CORE, EINVAL);
1286 		largepage_madvise(addr, ps[i], MADV_CORE, 0);
1287 		largepage_madvise(addr, PAGE_SIZE, MADV_NOCORE, EINVAL);
1288 		largepage_madvise(addr, ps[i], MADV_NOCORE, 0);
1289 
1290 		/* Advice that does not result in clipping. */
1291 		largepage_madvise(addr, PAGE_SIZE, MADV_DONTNEED, 0);
1292 		largepage_madvise(addr, ps[i], MADV_DONTNEED, 0);
1293 		largepage_madvise(addr, PAGE_SIZE, MADV_WILLNEED, 0);
1294 		largepage_madvise(addr, ps[i], MADV_WILLNEED, 0);
1295 		largepage_madvise(addr, PAGE_SIZE, MADV_FREE, 0);
1296 		largepage_madvise(addr, ps[i], MADV_FREE, 0);
1297 
1298 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1299 		ATF_REQUIRE(close(fd) == 0);
1300 	}
1301 }
1302 
1303 ATF_TC(largepage_mlock);
1304 ATF_TC_HEAD(largepage_mlock, tc)
1305 {
1306 	/* Needed to set rlimit. */
1307 	atf_tc_set_md_var(tc, "require.user", "root");
1308 }
1309 ATF_TC_BODY(largepage_mlock, tc)
1310 {
1311 	struct rlimit rl;
1312 	char *addr;
1313 	size_t ps[MAXPAGESIZES], sz;
1314 	u_long max_wired, wired;
1315 	int fd, error, pscnt;
1316 
1317 	rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
1318 	ATF_REQUIRE_MSG(setrlimit(RLIMIT_MEMLOCK, &rl) == 0,
1319 	    "setrlimit failed; error=%d", errno);
1320 
1321 	sz = sizeof(max_wired);
1322 	error = sysctlbyname("vm.max_user_wired", &max_wired, &sz, NULL, 0);
1323 	ATF_REQUIRE_MSG(error == 0,
1324 	    "sysctlbyname(vm.max_user_wired) failed; error=%d", errno);
1325 
1326 	sz = sizeof(wired);
1327 	error = sysctlbyname("vm.stats.vm.v_user_wire_count", &wired, &sz, NULL,
1328 	    0);
1329 	ATF_REQUIRE_MSG(error == 0,
1330 	    "sysctlbyname(vm.stats.vm.v_user_wire_count) failed; error=%d",
1331 	    errno);
1332 
1333 	pscnt = pagesizes(ps);
1334 	for (int i = 1; i < pscnt; i++) {
1335 		if (ps[i] / PAGE_SIZE > max_wired - wired) {
1336 			/* Cannot wire past the limit. */
1337 			atf_tc_skip("test would exceed wiring limit");
1338 		}
1339 
1340 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1341 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1342 		    0);
1343 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1344 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1345 
1346 		ATF_REQUIRE(mlock(addr, PAGE_SIZE) != 0);
1347 		ATF_REQUIRE_MSG(errno == EINVAL,
1348 		    "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1349 		ATF_REQUIRE(mlock(addr, ps[i] - PAGE_SIZE) != 0);
1350 		ATF_REQUIRE_MSG(errno == EINVAL,
1351 		    "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1352 
1353 		ATF_REQUIRE_MSG(mlock(addr, ps[i]) == 0,
1354 		    "mlock failed; error=%d", errno);
1355 
1356 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1357 
1358 		ATF_REQUIRE(mlockall(MCL_FUTURE) == 0);
1359 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1360 		    0);
1361 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1362 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1363 
1364 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1365 		ATF_REQUIRE(close(fd) == 0);
1366 	}
1367 }
1368 
1369 ATF_TC_WITHOUT_HEAD(largepage_msync);
1370 ATF_TC_BODY(largepage_msync, tc)
1371 {
1372 	char *addr;
1373 	size_t ps[MAXPAGESIZES];
1374 	int fd, pscnt;
1375 
1376 	pscnt = pagesizes(ps);
1377 	for (int i = 1; i < pscnt; i++) {
1378 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1379 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1380 		    0);
1381 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1382 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1383 
1384 		memset(addr, 0, ps[i]);
1385 
1386 		/*
1387 		 * "Sync" requests are no-ops for SHM objects, so small
1388 		 * PAGE_SIZE-sized requests succeed.
1389 		 */
1390 		ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_ASYNC) == 0,
1391 		    "msync(MS_ASYNC) failed; error=%d", errno);
1392 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_ASYNC) == 0,
1393 		    "msync(MS_ASYNC) failed; error=%d", errno);
1394 		ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_SYNC) == 0,
1395 		    "msync(MS_SYNC) failed; error=%d", errno);
1396 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_SYNC) == 0,
1397 		    "msync(MS_SYNC) failed; error=%d", errno);
1398 
1399 		ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_INVALIDATE) != 0,
1400 		    "msync(MS_INVALIDATE) succeeded");
1401 		/* XXX wrong errno */
1402 		ATF_REQUIRE_MSG(errno == EBUSY,
1403 		    "unexpected error %d from msync(MS_INVALIDATE)", errno);
1404 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_INVALIDATE) == 0,
1405 		    "msync(MS_INVALIDATE) failed; error=%d", errno);
1406 		memset(addr, 0, ps[i]);
1407 
1408 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1409 		ATF_REQUIRE(close(fd) == 0);
1410 	}
1411 }
1412 
1413 static void
1414 largepage_protect(char *addr, size_t sz, int prot, int error)
1415 {
1416 	if (error == 0) {
1417 		ATF_REQUIRE_MSG(mprotect(addr, sz, prot) == 0,
1418 		    "mprotect(%zu, %x) failed; error=%d", sz, prot, errno);
1419 	} else {
1420 		ATF_REQUIRE_MSG(mprotect(addr, sz, prot) != 0,
1421 		    "mprotect(%zu, %x) succeeded", sz, prot);
1422 		ATF_REQUIRE_MSG(errno == error,
1423 		    "unexpected error %d from mprotect(%zu, %x)",
1424 		    errno, sz, prot);
1425 	}
1426 }
1427 
1428 ATF_TC_WITHOUT_HEAD(largepage_mprotect);
1429 ATF_TC_BODY(largepage_mprotect, tc)
1430 {
1431 	char *addr, *addr1;
1432 	size_t ps[MAXPAGESIZES];
1433 	int fd, pscnt;
1434 
1435 	pscnt = pagesizes(ps);
1436 	for (int i = 1; i < pscnt; i++) {
1437 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1438 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1439 		    0);
1440 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1441 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1442 
1443 		/*
1444 		 * These should be no-ops from the pmap perspective since the
1445 		 * page is not yet entered into the pmap.
1446 		 */
1447 		largepage_protect(addr, PAGE_SIZE, PROT_READ, EINVAL);
1448 		largepage_protect(addr, ps[i], PROT_READ, 0);
1449 		largepage_protect(addr, PAGE_SIZE, PROT_NONE, EINVAL);
1450 		largepage_protect(addr, ps[i], PROT_NONE, 0);
1451 		largepage_protect(addr, PAGE_SIZE,
1452 		    PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1453 		largepage_protect(addr, ps[i],
1454 		    PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1455 
1456 		/* Trigger creation of a mapping and try again. */
1457 		*(volatile char *)addr = 0;
1458 		largepage_protect(addr, PAGE_SIZE, PROT_READ, EINVAL);
1459 		largepage_protect(addr, ps[i], PROT_READ, 0);
1460 		largepage_protect(addr, PAGE_SIZE, PROT_NONE, EINVAL);
1461 		largepage_protect(addr, ps[i], PROT_NONE, 0);
1462 		largepage_protect(addr, PAGE_SIZE,
1463 		    PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1464 		largepage_protect(addr, ps[i],
1465 		    PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1466 
1467 		memset(addr, 0, ps[i]);
1468 
1469 		/* Map two contiguous large pages and merge map entries. */
1470 		addr1 = mmap(addr + ps[i], ps[i], PROT_READ | PROT_WRITE,
1471 		    MAP_SHARED | MAP_FIXED | MAP_EXCL, fd, 0);
1472 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1473 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1474 
1475 		largepage_protect(addr1 - PAGE_SIZE, PAGE_SIZE * 2,
1476 		    PROT_READ | PROT_WRITE, EINVAL);
1477 		largepage_protect(addr, ps[i] * 2, PROT_READ | PROT_WRITE, 0);
1478 
1479 		memset(addr, 0, ps[i] * 2);
1480 
1481 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1482 		ATF_REQUIRE(munmap(addr1, ps[i]) == 0);
1483 		ATF_REQUIRE(close(fd) == 0);
1484 	}
1485 }
1486 
1487 ATF_TC_WITHOUT_HEAD(largepage_minherit);
1488 ATF_TC_BODY(largepage_minherit, tc)
1489 {
1490 	char *addr;
1491 	size_t ps[MAXPAGESIZES];
1492 	pid_t child;
1493 	int fd, pscnt, status;
1494 
1495 	pscnt = pagesizes(ps);
1496 	for (int i = 1; i < pscnt; i++) {
1497 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1498 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1499 		    0);
1500 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1501 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1502 
1503 		ATF_REQUIRE(minherit(addr, PAGE_SIZE, INHERIT_SHARE) != 0);
1504 
1505 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_SHARE) == 0,
1506 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1507 		child = fork();
1508 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1509 		if (child == 0) {
1510 			char v;
1511 
1512 			*(volatile char *)addr = 0;
1513 			if (mincore(addr, PAGE_SIZE, &v) != 0)
1514 				_exit(1);
1515 			if ((v & MINCORE_PSIND(i)) == 0)
1516 				_exit(2);
1517 			_exit(0);
1518 		}
1519 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1520 		    "waitpid failed; error=%d", errno);
1521 		ATF_REQUIRE_MSG(WIFEXITED(status),
1522 		    "child was killed by signal %d", WTERMSIG(status));
1523 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1524 		    "child exited with status %d", WEXITSTATUS(status));
1525 
1526 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_NONE) == 0,
1527 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1528 		child = fork();
1529 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1530 		if (child == 0) {
1531 			char v;
1532 
1533 			if (mincore(addr, PAGE_SIZE, &v) == 0)
1534 				_exit(1);
1535 			_exit(0);
1536 		}
1537 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1538 		    "waitpid failed; error=%d", errno);
1539 		ATF_REQUIRE_MSG(WIFEXITED(status),
1540 		    "child was killed by signal %d", WTERMSIG(status));
1541 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1542 		    "child exited with status %d", WEXITSTATUS(status));
1543 
1544 		/* Copy-on-write is not supported for static large pages. */
1545 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_COPY) != 0,
1546 		    "minherit(%zu bytes) succeeded", ps[i]);
1547 
1548 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_ZERO) == 0,
1549 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1550 		child = fork();
1551 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1552 		if (child == 0) {
1553 			char v;
1554 
1555 			*(volatile char *)addr = 0;
1556 			if (mincore(addr, PAGE_SIZE, &v) != 0)
1557 				_exit(1);
1558 			if ((v & MINCORE_SUPER) != 0)
1559 				_exit(2);
1560 			_exit(0);
1561 		}
1562 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1563 		    "waitpid failed; error=%d", errno);
1564 		ATF_REQUIRE_MSG(WIFEXITED(status),
1565 		    "child was killed by signal %d", WTERMSIG(status));
1566 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1567 		    "child exited with status %d", WEXITSTATUS(status));
1568 
1569 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1570 		ATF_REQUIRE(close(fd) == 0);
1571 	}
1572 }
1573 
1574 ATF_TC_WITHOUT_HEAD(largepage_pipe);
1575 ATF_TC_BODY(largepage_pipe, tc)
1576 {
1577 	size_t ps[MAXPAGESIZES];
1578 	char *addr;
1579 	ssize_t len;
1580 	int fd, pfd[2], pscnt, status;
1581 	pid_t child;
1582 
1583 	pscnt = pagesizes(ps);
1584 
1585 	for (int i = 1; i < pscnt; i++) {
1586 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1587 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1588 		    0);
1589 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1590 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1591 
1592 		/* Trigger creation of a mapping. */
1593 		*(volatile char *)addr = '\0';
1594 
1595 		ATF_REQUIRE(pipe(pfd) == 0);
1596 		child = fork();
1597 		ATF_REQUIRE_MSG(child != -1, "fork() failed; error=%d", errno);
1598 		if (child == 0) {
1599 			char buf[BUFSIZ];
1600 			ssize_t resid;
1601 
1602 			(void)close(pfd[0]);
1603 			for (resid = (size_t)ps[i]; resid > 0; resid -= len) {
1604 				len = read(pfd[1], buf, sizeof(buf));
1605 				if (len < 0)
1606 					_exit(1);
1607 			}
1608 			_exit(0);
1609 		}
1610 		ATF_REQUIRE(close(pfd[1]) == 0);
1611 		len = write(pfd[0], addr, ps[i]);
1612 		ATF_REQUIRE_MSG(len >= 0, "write() failed; error=%d", errno);
1613 		ATF_REQUIRE_MSG(len == (ssize_t)ps[i],
1614 		    "short write; len=%zd", len);
1615 		ATF_REQUIRE(close(pfd[0]) == 0);
1616 
1617 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1618 		    "waitpid() failed; error=%d", errno);
1619 		ATF_REQUIRE_MSG(WIFEXITED(status),
1620 		    "child was killed by signal %d", WTERMSIG(status));
1621 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1622 		    "child exited with status %d", WEXITSTATUS(status));
1623 
1624 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1625 		ATF_REQUIRE(close(fd) == 0);
1626 	}
1627 }
1628 
1629 ATF_TC_WITHOUT_HEAD(largepage_reopen);
1630 ATF_TC_BODY(largepage_reopen, tc)
1631 {
1632 	char *addr, *vec;
1633 	size_t ps[MAXPAGESIZES];
1634 	int fd, psind;
1635 
1636 	(void)pagesizes(ps);
1637 	psind = 1;
1638 
1639 	gen_test_path();
1640 	fd = shm_create_largepage(test_path, O_CREAT | O_RDWR, psind,
1641 	    SHM_LARGEPAGE_ALLOC_DEFAULT, 0600);
1642 	if (fd < 0 && errno == ENOTTY)
1643 		atf_tc_skip("no large page support");
1644 	ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; error=%d", errno);
1645 
1646 	ATF_REQUIRE_MSG(ftruncate(fd, ps[psind]) == 0,
1647 	    "ftruncate failed; error=%d", errno);
1648 
1649 	ATF_REQUIRE_MSG(close(fd) == 0, "close failed; error=%d", errno);
1650 
1651 	fd = shm_open(test_path, O_RDWR, 0);
1652 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
1653 
1654 	addr = mmap(NULL, ps[psind], PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1655 	ATF_REQUIRE_MSG(addr != MAP_FAILED, "mmap failed; error=%d", errno);
1656 
1657 	/* Trigger a fault and mapping creation. */
1658 	*(volatile char *)addr = 0;
1659 
1660 	vec = malloc(ps[psind] / PAGE_SIZE);
1661 	ATF_REQUIRE(vec != NULL);
1662 	ATF_REQUIRE_MSG(mincore(addr, ps[psind], vec) == 0,
1663 	    "mincore failed; error=%d", errno);
1664 	ATF_REQUIRE_MSG((vec[0] & MINCORE_PSIND(psind)) != 0,
1665 	    "page not mapped into a %zu-byte superpage", ps[psind]);
1666 
1667 	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
1668 	    "shm_unlink failed; errno=%d", errno);
1669 	ATF_REQUIRE_MSG(close(fd) == 0,
1670 	    "close failed; errno=%d", errno);
1671 }
1672 
1673 ATF_TP_ADD_TCS(tp)
1674 {
1675 
1676 	ATF_TP_ADD_TC(tp, remap_object);
1677 	ATF_TP_ADD_TC(tp, rename_from_anon);
1678 	ATF_TP_ADD_TC(tp, rename_bad_path_pointer);
1679 	ATF_TP_ADD_TC(tp, rename_from_nonexisting);
1680 	ATF_TP_ADD_TC(tp, rename_to_anon);
1681 	ATF_TP_ADD_TC(tp, rename_to_replace);
1682 	ATF_TP_ADD_TC(tp, rename_to_noreplace);
1683 	ATF_TP_ADD_TC(tp, rename_to_exchange);
1684 	ATF_TP_ADD_TC(tp, rename_to_exchange_nonexisting);
1685 	ATF_TP_ADD_TC(tp, rename_to_self);
1686 	ATF_TP_ADD_TC(tp, rename_bad_flag);
1687 	ATF_TP_ADD_TC(tp, reopen_object);
1688 	ATF_TP_ADD_TC(tp, readonly_mmap_write);
1689 	ATF_TP_ADD_TC(tp, open_after_link);
1690 	ATF_TP_ADD_TC(tp, open_invalid_path);
1691 	ATF_TP_ADD_TC(tp, open_write_only);
1692 	ATF_TP_ADD_TC(tp, open_extra_flags);
1693 	ATF_TP_ADD_TC(tp, open_anon);
1694 	ATF_TP_ADD_TC(tp, open_anon_readonly);
1695 	ATF_TP_ADD_TC(tp, open_bad_path_pointer);
1696 	ATF_TP_ADD_TC(tp, open_path_too_long);
1697 	ATF_TP_ADD_TC(tp, open_nonexisting_object);
1698 	ATF_TP_ADD_TC(tp, open_create_existing_object);
1699 	ATF_TP_ADD_TC(tp, shm_functionality_across_fork);
1700 	ATF_TP_ADD_TC(tp, trunc_resets_object);
1701 	ATF_TP_ADD_TC(tp, unlink_bad_path_pointer);
1702 	ATF_TP_ADD_TC(tp, unlink_path_too_long);
1703 	ATF_TP_ADD_TC(tp, object_resize);
1704 	ATF_TP_ADD_TC(tp, cloexec);
1705 	ATF_TP_ADD_TC(tp, mode);
1706 	ATF_TP_ADD_TC(tp, fallocate);
1707 	ATF_TP_ADD_TC(tp, largepage_basic);
1708 	ATF_TP_ADD_TC(tp, largepage_config);
1709 	ATF_TP_ADD_TC(tp, largepage_mmap);
1710 	ATF_TP_ADD_TC(tp, largepage_munmap);
1711 	ATF_TP_ADD_TC(tp, largepage_madvise);
1712 	ATF_TP_ADD_TC(tp, largepage_mlock);
1713 	ATF_TP_ADD_TC(tp, largepage_msync);
1714 	ATF_TP_ADD_TC(tp, largepage_mprotect);
1715 	ATF_TP_ADD_TC(tp, largepage_minherit);
1716 	ATF_TP_ADD_TC(tp, largepage_pipe);
1717 	ATF_TP_ADD_TC(tp, largepage_reopen);
1718 
1719 	return (atf_no_error());
1720 }
1721