xref: /freebsd/tests/sys/posixshm/posixshm_test.c (revision 75c66218401c00f4728d5312e2b933b8d3aefde1)
1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Copyright (c) 2021 The FreeBSD Foundation
6  *
7  * Portions of this software were developed by Ka Ho Ng
8  * under sponsorship from the FreeBSD Foundation.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/mman.h>
35 #include <sys/resource.h>
36 #include <sys/stat.h>
37 #include <sys/syscall.h>
38 #include <sys/sysctl.h>
39 #include <sys/wait.h>
40 
41 #ifdef __amd64__
42 #include <machine/cpufunc.h>
43 #include <machine/specialreg.h>
44 #include <machine/sysarch.h>
45 #endif
46 
47 #include <ctype.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <paths.h>
51 #include <setjmp.h>
52 #include <signal.h>
53 #include <stdatomic.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include <atf-c.h>
60 
61 #include "posixshm.h"
62 
63 #define	TEST_PATH_LEN	256
64 static char test_path[TEST_PATH_LEN];
65 static char test_path2[TEST_PATH_LEN];
66 static unsigned int test_path_idx = 0;
67 
68 static void
gen_a_test_path(char * path)69 gen_a_test_path(char *path)
70 {
71 	snprintf(path, TEST_PATH_LEN, "/%s/tmp.XXXXXX%d",
72 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"),
73 	    test_path_idx);
74 
75 	test_path_idx++;
76 
77 	ATF_REQUIRE_MSG(mkstemp(path) != -1,
78 	    "mkstemp failed; errno=%d", errno);
79 	ATF_REQUIRE_MSG(unlink(path) == 0,
80 	    "unlink failed; errno=%d", errno);
81 }
82 
83 static void
gen_test_path(void)84 gen_test_path(void)
85 {
86 	gen_a_test_path(test_path);
87 }
88 
89 static void
gen_test_path2(void)90 gen_test_path2(void)
91 {
92 	gen_a_test_path(test_path2);
93 }
94 
95 /*
96  * Attempt a shm_open() that should fail with an expected error of 'error'.
97  */
98 static void
shm_open_should_fail(const char * path,int flags,mode_t mode,int error)99 shm_open_should_fail(const char *path, int flags, mode_t mode, int error)
100 {
101 	int fd;
102 
103 	fd = shm_open(path, flags, mode);
104 	ATF_CHECK_MSG(fd == -1, "shm_open didn't fail");
105 	ATF_CHECK_MSG(error == errno,
106 	    "shm_open didn't fail with expected errno; errno=%d; expected "
107 	    "errno=%d", errno, error);
108 }
109 
110 /*
111  * Attempt a shm_unlink() that should fail with an expected error of 'error'.
112  */
113 static void
shm_unlink_should_fail(const char * path,int error)114 shm_unlink_should_fail(const char *path, int error)
115 {
116 
117 	ATF_CHECK_MSG(shm_unlink(path) == -1, "shm_unlink didn't fail");
118 	ATF_CHECK_MSG(error == errno,
119 	    "shm_unlink didn't fail with expected errno; errno=%d; expected "
120 	    "errno=%d", errno, error);
121 }
122 
123 /*
124  * Open the test object and write a value to the first byte.  Returns valid fd
125  * on success and -1 on failure.
126  */
127 static int
scribble_object(const char * path,char value)128 scribble_object(const char *path, char value)
129 {
130 	char *page;
131 	int fd, pagesize;
132 
133 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
134 
135 	fd = shm_open(path, O_CREAT|O_EXCL|O_RDWR, 0777);
136 	if (fd < 0 && errno == EEXIST) {
137 		if (shm_unlink(test_path) < 0)
138 			atf_tc_fail("shm_unlink");
139 		fd = shm_open(test_path, O_CREAT | O_EXCL | O_RDWR, 0777);
140 	}
141 	if (fd < 0)
142 		atf_tc_fail("shm_open failed; errno=%d", errno);
143 	if (ftruncate(fd, pagesize) < 0)
144 		atf_tc_fail("ftruncate failed; errno=%d", errno);
145 
146 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
147 	if (page == MAP_FAILED)
148 		atf_tc_fail("mmap failed; errno=%d", errno);
149 
150 	page[0] = value;
151 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
152 	    errno);
153 
154 	return (fd);
155 }
156 
157 /*
158  * Fail the test case if the 'path' does not refer to an shm whose first byte
159  * is equal to expected_value
160  */
161 static void
verify_object(const char * path,char expected_value)162 verify_object(const char *path, char expected_value)
163 {
164 	int fd;
165 	int pagesize;
166 	char *page;
167 
168 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
169 
170 	fd = shm_open(path, O_RDONLY, 0777);
171 	if (fd < 0)
172 		atf_tc_fail("shm_open failed in verify_object; errno=%d, path=%s",
173 		    errno, path);
174 
175 	page = mmap(0, pagesize, PROT_READ, MAP_SHARED, fd, 0);
176 	if (page == MAP_FAILED)
177 		atf_tc_fail("mmap(1)");
178 	if (page[0] != expected_value)
179 		atf_tc_fail("Renamed object has incorrect value; has"
180 		    "%d (0x%x, '%c'), expected %d (0x%x, '%c')\n",
181 		    page[0], page[0], isprint(page[0]) ? page[0] : ' ',
182 		    expected_value, expected_value,
183 		    isprint(expected_value) ? expected_value : ' ');
184 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
185 	    errno);
186 	close(fd);
187 }
188 
189 static off_t shm_max_pages = 32;
190 static const char byte_to_fill = 0x5f;
191 
192 static int
shm_fill(int fd,off_t offset,off_t len)193 shm_fill(int fd, off_t offset, off_t len)
194 {
195 	int error;
196 	size_t blen, page_size;
197 	char *buf;
198 
199 	error = 0;
200 	page_size = getpagesize();
201 	buf = malloc(page_size);
202 	if (buf == NULL)
203 		return (1);
204 
205 	while (len > 0) {
206 		blen = len < (off_t)page_size ? (size_t)len : page_size;
207 		memset(buf, byte_to_fill, blen);
208 		if (pwrite(fd, buf, blen, offset) != (ssize_t)blen) {
209 			error = 1;
210 			break;
211 		}
212 		len -= blen;
213 		offset += blen;
214 	}
215 
216 	free(buf);
217 	return (error);
218 }
219 
220 static int
check_content_dealloc(int fd,off_t hole_start,off_t hole_len,off_t shm_sz)221 check_content_dealloc(int fd, off_t hole_start, off_t hole_len, off_t shm_sz)
222 {
223 	int error;
224 	size_t blen, page_size;
225 	off_t offset, resid;
226 	struct stat statbuf;
227 	char *buf, *sblk;
228 
229 	error = 0;
230 	page_size = getpagesize();
231 	buf = malloc(page_size * 2);
232 	if (buf == NULL)
233 		return (1);
234 	sblk = buf + page_size;
235 
236 	memset(sblk, 0, page_size);
237 
238 	if ((uint64_t)hole_start + hole_len > (uint64_t)shm_sz)
239 		hole_len = shm_sz - hole_start;
240 
241 	/*
242 	 * Check hole is zeroed.
243 	 */
244 	offset = hole_start;
245 	resid = hole_len;
246 	while (resid > 0) {
247 		blen = resid < (off_t)page_size ? (size_t)resid : page_size;
248 		if (pread(fd, buf, blen, offset) != (ssize_t)blen) {
249 			error = 1;
250 			break;
251 		}
252 		if (memcmp(buf, sblk, blen) != 0) {
253 			error = 1;
254 			break;
255 		}
256 		resid -= blen;
257 		offset += blen;
258 	}
259 
260 	memset(sblk, byte_to_fill, page_size);
261 
262 	/*
263 	 * Check file region before hole is zeroed.
264 	 */
265 	offset = 0;
266 	resid = hole_start;
267 	while (resid > 0) {
268 		blen = resid < (off_t)page_size ? (size_t)resid : page_size;
269 		if (pread(fd, buf, blen, offset) != (ssize_t)blen) {
270 			error = 1;
271 			break;
272 		}
273 		if (memcmp(buf, sblk, blen) != 0) {
274 			error = 1;
275 			break;
276 		}
277 		resid -= blen;
278 		offset += blen;
279 	}
280 
281 	/*
282 	 * Check file region after hole is zeroed.
283 	 */
284 	offset = hole_start + hole_len;
285 	resid = shm_sz - offset;
286 	while (resid > 0) {
287 		blen = resid < (off_t)page_size ? (size_t)resid : page_size;
288 		if (pread(fd, buf, blen, offset) != (ssize_t)blen) {
289 			error = 1;
290 			break;
291 		}
292 		if (memcmp(buf, sblk, blen) != 0) {
293 			error = 1;
294 			break;
295 		}
296 		resid -= blen;
297 		offset += blen;
298 	}
299 
300 	/*
301 	 * Check file size matches with expected file size.
302 	 */
303 	if (fstat(fd, &statbuf) == -1)
304 		error = -1;
305 	if (statbuf.st_size != shm_sz)
306 		error = -1;
307 
308 	free(buf);
309 	return (error);
310 }
311 
312 ATF_TC_WITHOUT_HEAD(remap_object);
ATF_TC_BODY(remap_object,tc)313 ATF_TC_BODY(remap_object, tc)
314 {
315 	char *page;
316 	int fd, pagesize;
317 
318 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
319 
320 	gen_test_path();
321 	fd = scribble_object(test_path, '1');
322 
323 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
324 	if (page == MAP_FAILED)
325 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
326 
327 	if (page[0] != '1')
328 		atf_tc_fail("missing data ('%c' != '1')", page[0]);
329 
330 	close(fd);
331 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
332 	    errno);
333 
334 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
335 	    "shm_unlink failed; errno=%d", errno);
336 }
337 
338 ATF_TC_WITHOUT_HEAD(rename_from_anon);
ATF_TC_BODY(rename_from_anon,tc)339 ATF_TC_BODY(rename_from_anon, tc)
340 {
341 	int rc;
342 
343 	gen_test_path();
344 	rc = shm_rename(SHM_ANON, test_path, 0);
345 	if (rc != -1)
346 		atf_tc_fail("shm_rename from SHM_ANON succeeded unexpectedly");
347 }
348 
349 ATF_TC_WITHOUT_HEAD(rename_bad_path_pointer);
ATF_TC_BODY(rename_bad_path_pointer,tc)350 ATF_TC_BODY(rename_bad_path_pointer, tc)
351 {
352 	const char *bad_path;
353 	int rc;
354 
355 	bad_path = (const char *)0x1;
356 
357 	gen_test_path();
358 	rc = shm_rename(test_path, bad_path, 0);
359 	if (rc != -1)
360 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
361 
362 	rc = shm_rename(bad_path, test_path, 0);
363 	if (rc != -1)
364 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
365 }
366 
367 ATF_TC_WITHOUT_HEAD(rename_from_nonexisting);
ATF_TC_BODY(rename_from_nonexisting,tc)368 ATF_TC_BODY(rename_from_nonexisting, tc)
369 {
370 	int rc;
371 
372 	gen_test_path();
373 	gen_test_path2();
374 	rc = shm_rename(test_path, test_path2, 0);
375 	if (rc != -1)
376 		atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
377 
378 	if (errno != ENOENT)
379 		atf_tc_fail("Expected ENOENT to rename of nonexistent shm; got %d",
380 		    errno);
381 }
382 
383 ATF_TC_WITHOUT_HEAD(rename_to_anon);
ATF_TC_BODY(rename_to_anon,tc)384 ATF_TC_BODY(rename_to_anon, tc)
385 {
386 	int rc;
387 
388 	gen_test_path();
389 	rc = shm_rename(test_path, SHM_ANON, 0);
390 	if (rc != -1)
391 		atf_tc_fail("shm_rename to SHM_ANON succeeded unexpectedly");
392 }
393 
394 ATF_TC_WITHOUT_HEAD(rename_to_replace);
ATF_TC_BODY(rename_to_replace,tc)395 ATF_TC_BODY(rename_to_replace, tc)
396 {
397 	char expected_value;
398 	int fd;
399 	int fd2;
400 
401 	// Some contents we can verify later
402 	expected_value = 'g';
403 
404 	gen_test_path();
405 	fd = scribble_object(test_path, expected_value);
406 	close(fd);
407 
408 	// Give the other some different value so we can detect success
409 	gen_test_path2();
410 	fd2 = scribble_object(test_path2, 'h');
411 	close(fd2);
412 
413 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2, 0) == 0,
414 	    "shm_rename failed; errno=%d", errno);
415 
416 	// Read back renamed; verify contents
417 	verify_object(test_path2, expected_value);
418 }
419 
420 ATF_TC_WITHOUT_HEAD(rename_to_noreplace);
ATF_TC_BODY(rename_to_noreplace,tc)421 ATF_TC_BODY(rename_to_noreplace, tc)
422 {
423 	char expected_value_from;
424 	char expected_value_to;
425 	int fd_from;
426 	int fd_to;
427 	int rc;
428 
429 	// Some contents we can verify later
430 	expected_value_from = 'g';
431 	gen_test_path();
432 	fd_from = scribble_object(test_path, expected_value_from);
433 	close(fd_from);
434 
435 	// Give the other some different value so we can detect success
436 	expected_value_to = 'h';
437 	gen_test_path2();
438 	fd_to = scribble_object(test_path2, expected_value_to);
439 	close(fd_to);
440 
441 	rc = shm_rename(test_path, test_path2, SHM_RENAME_NOREPLACE);
442 	ATF_REQUIRE_MSG((rc == -1) && (errno == EEXIST),
443 	    "shm_rename didn't fail as expected; errno: %d; return: %d", errno,
444 	    rc);
445 
446 	// Read back renamed; verify contents
447 	verify_object(test_path2, expected_value_to);
448 }
449 
450 ATF_TC_WITHOUT_HEAD(rename_to_exchange);
ATF_TC_BODY(rename_to_exchange,tc)451 ATF_TC_BODY(rename_to_exchange, tc)
452 {
453 	char expected_value_from;
454 	char expected_value_to;
455 	int fd_from;
456 	int fd_to;
457 
458 	// Some contents we can verify later
459 	expected_value_from = 'g';
460 	gen_test_path();
461 	fd_from = scribble_object(test_path, expected_value_from);
462 	close(fd_from);
463 
464 	// Give the other some different value so we can detect success
465 	expected_value_to = 'h';
466 	gen_test_path2();
467 	fd_to = scribble_object(test_path2, expected_value_to);
468 	close(fd_to);
469 
470 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
471 	    SHM_RENAME_EXCHANGE) == 0,
472 	    "shm_rename failed; errno=%d", errno);
473 
474 	// Read back renamed; verify contents
475 	verify_object(test_path, expected_value_to);
476 	verify_object(test_path2, expected_value_from);
477 }
478 
479 ATF_TC_WITHOUT_HEAD(rename_to_exchange_nonexisting);
ATF_TC_BODY(rename_to_exchange_nonexisting,tc)480 ATF_TC_BODY(rename_to_exchange_nonexisting, tc)
481 {
482 	char expected_value_from;
483 	int fd_from;
484 
485 	// Some contents we can verify later
486 	expected_value_from = 'g';
487 	gen_test_path();
488 	fd_from = scribble_object(test_path, expected_value_from);
489 	close(fd_from);
490 
491 	gen_test_path2();
492 
493 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
494 	    SHM_RENAME_EXCHANGE) == 0,
495 	    "shm_rename failed; errno=%d", errno);
496 
497 	// Read back renamed; verify contents
498 	verify_object(test_path2, expected_value_from);
499 }
500 
501 ATF_TC_WITHOUT_HEAD(rename_to_self);
ATF_TC_BODY(rename_to_self,tc)502 ATF_TC_BODY(rename_to_self, tc)
503 {
504 	int fd;
505 	char expected_value;
506 
507 	expected_value = 't';
508 
509 	gen_test_path();
510 	fd = scribble_object(test_path, expected_value);
511 	close(fd);
512 
513 	ATF_REQUIRE_MSG(shm_rename(test_path, test_path, 0) == 0,
514 	    "shm_rename failed; errno=%d", errno);
515 
516 	verify_object(test_path, expected_value);
517 }
518 
519 ATF_TC_WITHOUT_HEAD(rename_bad_flag);
ATF_TC_BODY(rename_bad_flag,tc)520 ATF_TC_BODY(rename_bad_flag, tc)
521 {
522 	int fd;
523 	int rc;
524 
525 	/* Make sure we don't fail out due to ENOENT */
526 	gen_test_path();
527 	gen_test_path2();
528 	fd = scribble_object(test_path, 'd');
529 	close(fd);
530 	fd = scribble_object(test_path2, 'd');
531 	close(fd);
532 
533 	/*
534 	 * Note: if we end up with enough flags that we use all the bits,
535 	 * then remove this test completely.
536 	 */
537 	rc = shm_rename(test_path, test_path2, INT_MIN);
538 	ATF_REQUIRE_MSG((rc == -1) && (errno == EINVAL),
539 	    "shm_rename should have failed with EINVAL; got: return=%d, "
540 	    "errno=%d", rc, errno);
541 }
542 
543 ATF_TC_WITHOUT_HEAD(reopen_object);
ATF_TC_BODY(reopen_object,tc)544 ATF_TC_BODY(reopen_object, tc)
545 {
546 	char *page;
547 	int fd, pagesize;
548 
549 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
550 
551 	gen_test_path();
552 	fd = scribble_object(test_path, '1');
553 	close(fd);
554 
555 	fd = shm_open(test_path, O_RDONLY, 0777);
556 	if (fd < 0)
557 		atf_tc_fail("shm_open(2) failed; errno=%d", errno);
558 
559 	page = mmap(0, pagesize, PROT_READ, MAP_SHARED, fd, 0);
560 	if (page == MAP_FAILED)
561 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
562 
563 	if (page[0] != '1')
564 		atf_tc_fail("missing data ('%c' != '1')", page[0]);
565 
566 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
567 	    errno);
568 	close(fd);
569 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
570 	    "shm_unlink failed; errno=%d", errno);
571 }
572 
573 ATF_TC_WITHOUT_HEAD(readonly_mmap_write);
ATF_TC_BODY(readonly_mmap_write,tc)574 ATF_TC_BODY(readonly_mmap_write, tc)
575 {
576 	char *page;
577 	int fd, pagesize;
578 
579 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
580 
581 	gen_test_path();
582 
583 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
584 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
585 
586 	/* PROT_WRITE should fail with EACCES. */
587 	page = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
588 	if (page != MAP_FAILED)
589 		atf_tc_fail("mmap(PROT_WRITE) succeeded unexpectedly");
590 
591 	if (errno != EACCES)
592 		atf_tc_fail("mmap(PROT_WRITE) didn't fail with EACCES; "
593 		    "errno=%d", errno);
594 
595 	close(fd);
596 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
597 	    "shm_unlink failed; errno=%d", errno);
598 }
599 
600 ATF_TC_WITHOUT_HEAD(open_after_link);
ATF_TC_BODY(open_after_link,tc)601 ATF_TC_BODY(open_after_link, tc)
602 {
603 	int fd;
604 
605 	gen_test_path();
606 
607 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
608 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
609 	close(fd);
610 
611 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1, "shm_unlink failed: %d",
612 	    errno);
613 
614 	shm_open_should_fail(test_path, O_RDONLY, 0777, ENOENT);
615 }
616 
617 ATF_TC_WITHOUT_HEAD(open_invalid_path);
ATF_TC_BODY(open_invalid_path,tc)618 ATF_TC_BODY(open_invalid_path, tc)
619 {
620 
621 	shm_open_should_fail("blah", O_RDONLY, 0777, EINVAL);
622 }
623 
624 ATF_TC_WITHOUT_HEAD(open_write_only);
ATF_TC_BODY(open_write_only,tc)625 ATF_TC_BODY(open_write_only, tc)
626 {
627 
628 	gen_test_path();
629 
630 	shm_open_should_fail(test_path, O_WRONLY, 0777, EINVAL);
631 }
632 
633 ATF_TC_WITHOUT_HEAD(open_extra_flags);
ATF_TC_BODY(open_extra_flags,tc)634 ATF_TC_BODY(open_extra_flags, tc)
635 {
636 
637 	gen_test_path();
638 
639 	shm_open_should_fail(test_path, O_RDONLY | O_DIRECT, 0777, EINVAL);
640 }
641 
642 ATF_TC_WITHOUT_HEAD(open_anon);
ATF_TC_BODY(open_anon,tc)643 ATF_TC_BODY(open_anon, tc)
644 {
645 	int fd;
646 
647 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
648 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
649 	close(fd);
650 }
651 
652 ATF_TC_WITHOUT_HEAD(open_anon_readonly);
ATF_TC_BODY(open_anon_readonly,tc)653 ATF_TC_BODY(open_anon_readonly, tc)
654 {
655 
656 	shm_open_should_fail(SHM_ANON, O_RDONLY, 0777, EINVAL);
657 }
658 
659 ATF_TC_WITHOUT_HEAD(open_bad_path_pointer);
ATF_TC_BODY(open_bad_path_pointer,tc)660 ATF_TC_BODY(open_bad_path_pointer, tc)
661 {
662 
663 	shm_open_should_fail((char *)1024, O_RDONLY, 0777, EFAULT);
664 }
665 
666 ATF_TC_WITHOUT_HEAD(open_path_too_long);
ATF_TC_BODY(open_path_too_long,tc)667 ATF_TC_BODY(open_path_too_long, tc)
668 {
669 	char *page;
670 
671 	page = malloc(MAXPATHLEN + 1);
672 	memset(page, 'a', MAXPATHLEN);
673 	page[MAXPATHLEN] = '\0';
674 	shm_open_should_fail(page, O_RDONLY, 0777, ENAMETOOLONG);
675 	free(page);
676 }
677 
678 ATF_TC_WITHOUT_HEAD(open_nonexisting_object);
ATF_TC_BODY(open_nonexisting_object,tc)679 ATF_TC_BODY(open_nonexisting_object, tc)
680 {
681 
682 	shm_open_should_fail("/notreallythere", O_RDONLY, 0777, ENOENT);
683 }
684 
685 ATF_TC_WITHOUT_HEAD(open_create_existing_object);
ATF_TC_BODY(open_create_existing_object,tc)686 ATF_TC_BODY(open_create_existing_object, tc)
687 {
688 	int fd;
689 
690 	gen_test_path();
691 
692 	fd = shm_open(test_path, O_RDONLY|O_CREAT, 0777);
693 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
694 	close(fd);
695 
696 	shm_open_should_fail(test_path, O_RDONLY|O_CREAT|O_EXCL,
697 	    0777, EEXIST);
698 
699 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
700 	    "shm_unlink failed; errno=%d", errno);
701 }
702 
703 ATF_TC_WITHOUT_HEAD(trunc_resets_object);
ATF_TC_BODY(trunc_resets_object,tc)704 ATF_TC_BODY(trunc_resets_object, tc)
705 {
706 	struct stat sb;
707 	int fd;
708 
709 	gen_test_path();
710 
711 	/* Create object and set size to 1024. */
712 	fd = shm_open(test_path, O_RDWR | O_CREAT, 0777);
713 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
714 	ATF_REQUIRE_MSG(ftruncate(fd, 1024) != -1,
715 	    "ftruncate failed; errno=%d", errno);
716 	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
717 	    "fstat(1) failed; errno=%d", errno);
718 	ATF_REQUIRE_MSG(sb.st_size == 1024, "size %d != 1024", (int)sb.st_size);
719 	close(fd);
720 
721 	/* Open with O_TRUNC which should reset size to 0. */
722 	fd = shm_open(test_path, O_RDWR | O_TRUNC, 0777);
723 	ATF_REQUIRE_MSG(fd >= 0, "shm_open(2) failed; errno=%d", errno);
724 	ATF_REQUIRE_MSG(fstat(fd, &sb) != -1,
725 	    "fstat(2) failed; errno=%d", errno);
726 	ATF_REQUIRE_MSG(sb.st_size == 0,
727 	    "size was not 0 after truncation: %d", (int)sb.st_size);
728 	close(fd);
729 	ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
730 	    "shm_unlink failed; errno=%d", errno);
731 }
732 
733 ATF_TC_WITHOUT_HEAD(unlink_bad_path_pointer);
ATF_TC_BODY(unlink_bad_path_pointer,tc)734 ATF_TC_BODY(unlink_bad_path_pointer, tc)
735 {
736 
737 	shm_unlink_should_fail((char *)1024, EFAULT);
738 }
739 
740 ATF_TC_WITHOUT_HEAD(unlink_path_too_long);
ATF_TC_BODY(unlink_path_too_long,tc)741 ATF_TC_BODY(unlink_path_too_long, tc)
742 {
743 	char *page;
744 
745 	page = malloc(MAXPATHLEN + 1);
746 	memset(page, 'a', MAXPATHLEN);
747 	page[MAXPATHLEN] = '\0';
748 	shm_unlink_should_fail(page, ENAMETOOLONG);
749 	free(page);
750 }
751 
752 ATF_TC_WITHOUT_HEAD(object_resize);
ATF_TC_BODY(object_resize,tc)753 ATF_TC_BODY(object_resize, tc)
754 {
755 	pid_t pid;
756 	struct stat sb;
757 	char *page;
758 	int fd, pagesize, status;
759 
760 	ATF_REQUIRE(0 < (pagesize = getpagesize()));
761 
762 	/* Start off with a size of a single page. */
763 	fd = shm_open(SHM_ANON, O_CREAT|O_RDWR, 0777);
764 	if (fd < 0)
765 		atf_tc_fail("shm_open failed; errno=%d", errno);
766 
767 	if (ftruncate(fd, pagesize) < 0)
768 		atf_tc_fail("ftruncate(1) failed; errno=%d", errno);
769 
770 	if (fstat(fd, &sb) < 0)
771 		atf_tc_fail("fstat(1) failed; errno=%d", errno);
772 
773 	if (sb.st_size != pagesize)
774 		atf_tc_fail("first resize failed (%d != %d)",
775 		    (int)sb.st_size, pagesize);
776 
777 	/* Write a '1' to the first byte. */
778 	page = mmap(0, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
779 	if (page == MAP_FAILED)
780 		atf_tc_fail("mmap(1)");
781 
782 	page[0] = '1';
783 
784 	ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
785 	    errno);
786 
787 	/* Grow the object to 2 pages. */
788 	if (ftruncate(fd, pagesize * 2) < 0)
789 		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
790 
791 	if (fstat(fd, &sb) < 0)
792 		atf_tc_fail("fstat(2) failed; errno=%d", errno);
793 
794 	if (sb.st_size != pagesize * 2)
795 		atf_tc_fail("second resize failed (%d != %d)",
796 		    (int)sb.st_size, pagesize * 2);
797 
798 	/* Check for '1' at the first byte. */
799 	page = mmap(0, pagesize * 2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
800 	if (page == MAP_FAILED)
801 		atf_tc_fail("mmap(2) failed; errno=%d", errno);
802 
803 	if (page[0] != '1')
804 		atf_tc_fail("'%c' != '1'", page[0]);
805 
806 	/* Write a '2' at the start of the second page. */
807 	page[pagesize] = '2';
808 
809 	/* Shrink the object back to 1 page. */
810 	if (ftruncate(fd, pagesize) < 0)
811 		atf_tc_fail("ftruncate(3) failed; errno=%d", errno);
812 
813 	if (fstat(fd, &sb) < 0)
814 		atf_tc_fail("fstat(3) failed; errno=%d", errno);
815 
816 	if (sb.st_size != pagesize)
817 		atf_tc_fail("third resize failed (%d != %d)",
818 		    (int)sb.st_size, pagesize);
819 
820 	/*
821 	 * Fork a child process to make sure the second page is no
822 	 * longer valid.
823 	 */
824 	pid = fork();
825 	if (pid == -1)
826 		atf_tc_fail("fork failed; errno=%d", errno);
827 
828 	if (pid == 0) {
829 		struct rlimit lim;
830 		char c;
831 
832 		/* Don't generate a core dump. */
833 		ATF_REQUIRE(getrlimit(RLIMIT_CORE, &lim) == 0);
834 		lim.rlim_cur = 0;
835 		ATF_REQUIRE(setrlimit(RLIMIT_CORE, &lim) == 0);
836 
837 		/*
838 		 * The previous ftruncate(2) shrunk the backing object
839 		 * so that this address is no longer valid, so reading
840 		 * from it should trigger a SIGBUS.
841 		 */
842 		c = page[pagesize];
843 		fprintf(stderr, "child: page 1: '%c'\n", c);
844 		exit(0);
845 	}
846 
847 	if (wait(&status) < 0)
848 		atf_tc_fail("wait failed; errno=%d", errno);
849 
850 	if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGBUS)
851 		atf_tc_fail("child terminated with status %x", status);
852 
853 	/* Grow the object back to 2 pages. */
854 	if (ftruncate(fd, pagesize * 2) < 0)
855 		atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
856 
857 	if (fstat(fd, &sb) < 0)
858 		atf_tc_fail("fstat(2) failed; errno=%d", errno);
859 
860 	if (sb.st_size != pagesize * 2)
861 		atf_tc_fail("fourth resize failed (%d != %d)",
862 		    (int)sb.st_size, pagesize);
863 
864 	/*
865 	 * Note that the mapping at 'page' for the second page is
866 	 * still valid, and now that the shm object has been grown
867 	 * back up to 2 pages, there is now memory backing this page
868 	 * so the read will work.  However, the data should be zero
869 	 * rather than '2' as the old data was thrown away when the
870 	 * object was shrunk and the new pages when an object are
871 	 * grown are zero-filled.
872 	 */
873 	if (page[pagesize] != 0)
874 		atf_tc_fail("invalid data at %d: %x != 0",
875 		    pagesize, (int)page[pagesize]);
876 
877 	close(fd);
878 }
879 
880 /* Signal handler which does nothing. */
881 static void
ignoreit(int sig __unused)882 ignoreit(int sig __unused)
883 {
884 	;
885 }
886 
887 ATF_TC_WITHOUT_HEAD(shm_functionality_across_fork);
ATF_TC_BODY(shm_functionality_across_fork,tc)888 ATF_TC_BODY(shm_functionality_across_fork, tc)
889 {
890 	char *cp, c;
891 	int error, desc, rv;
892 	long scval;
893 	sigset_t ss;
894 	struct sigaction sa;
895 	void *region;
896 	size_t i, psize;
897 
898 #ifndef _POSIX_SHARED_MEMORY_OBJECTS
899 	printf("_POSIX_SHARED_MEMORY_OBJECTS is undefined\n");
900 #else
901 	printf("_POSIX_SHARED_MEMORY_OBJECTS is defined as %ld\n",
902 	       (long)_POSIX_SHARED_MEMORY_OBJECTS - 0);
903 	if (_POSIX_SHARED_MEMORY_OBJECTS - 0 == -1)
904 		printf("***Indicates this feature may be unsupported!\n");
905 #endif
906 	errno = 0;
907 	scval = sysconf(_SC_SHARED_MEMORY_OBJECTS);
908 	if (scval == -1 && errno != 0) {
909 		atf_tc_fail("sysconf(_SC_SHARED_MEMORY_OBJECTS) failed; "
910 		    "errno=%d", errno);
911 	} else {
912 		printf("sysconf(_SC_SHARED_MEMORY_OBJECTS) returns %ld\n",
913 		       scval);
914 		if (scval == -1)
915 			printf("***Indicates this feature is unsupported!\n");
916 	}
917 
918 	errno = 0;
919 	scval = sysconf(_SC_PAGESIZE);
920 	if (scval == -1 && errno != 0) {
921 		atf_tc_fail("sysconf(_SC_PAGESIZE) failed; errno=%d", errno);
922 	} else if (scval <= 0) {
923 		fprintf(stderr, "bogus return from sysconf(_SC_PAGESIZE): %ld",
924 		    scval);
925 		psize = 4096;
926 	} else {
927 		printf("sysconf(_SC_PAGESIZE) returns %ld\n", scval);
928 		psize = scval;
929 	}
930 
931 	gen_test_path();
932 	desc = shm_open(test_path, O_EXCL | O_CREAT | O_RDWR, 0600);
933 
934 	ATF_REQUIRE_MSG(desc >= 0, "shm_open failed; errno=%d", errno);
935 	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
936 	    "shm_unlink failed; errno=%d", errno);
937 	ATF_REQUIRE_MSG(ftruncate(desc, (off_t)psize) != -1,
938 	    "ftruncate failed; errno=%d", errno);
939 
940 	region = mmap(NULL, psize, PROT_READ | PROT_WRITE, MAP_SHARED, desc, 0);
941 	ATF_REQUIRE_MSG(region != MAP_FAILED, "mmap failed; errno=%d", errno);
942 	memset(region, '\377', psize);
943 
944 	sa.sa_flags = 0;
945 	sa.sa_handler = ignoreit;
946 	sigemptyset(&sa.sa_mask);
947 	ATF_REQUIRE_MSG(sigaction(SIGUSR1, &sa, (struct sigaction *)0) == 0,
948 	    "sigaction failed; errno=%d", errno);
949 
950 	sigemptyset(&ss);
951 	sigaddset(&ss, SIGUSR1);
952 	ATF_REQUIRE_MSG(sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0) == 0,
953 	    "sigprocmask failed; errno=%d", errno);
954 
955 	rv = fork();
956 	ATF_REQUIRE_MSG(rv != -1, "fork failed; errno=%d", errno);
957 	if (rv == 0) {
958 		sigemptyset(&ss);
959 		sigsuspend(&ss);
960 
961 		for (cp = region; cp < (char *)region + psize; cp++) {
962 			if (*cp != '\151')
963 				_exit(1);
964 		}
965 		if (lseek(desc, 0, SEEK_SET) == -1)
966 			_exit(1);
967 		for (i = 0; i < psize; i++) {
968 			error = read(desc, &c, 1);
969 			if (c != '\151')
970 				_exit(1);
971 		}
972 		_exit(0);
973 	} else {
974 		int status;
975 
976 		memset(region, '\151', psize - 2);
977 		error = pwrite(desc, region, 2, psize - 2);
978 		if (error != 2) {
979 			if (error >= 0)
980 				atf_tc_fail("short write; %d bytes written",
981 				    error);
982 			else
983 				atf_tc_fail("shmfd write");
984 		}
985 		kill(rv, SIGUSR1);
986 		waitpid(rv, &status, 0);
987 
988 		if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
989 			printf("Functionality test successful\n");
990 		} else if (WIFEXITED(status)) {
991 			atf_tc_fail("Child process exited with status %d",
992 			    WEXITSTATUS(status));
993 		} else {
994 			atf_tc_fail("Child process terminated with %s",
995 			    strsignal(WTERMSIG(status)));
996 		}
997 	}
998 
999 	ATF_REQUIRE_MSG(munmap(region, psize) == 0, "munmap failed; errno=%d",
1000 	    errno);
1001 	shm_unlink(test_path);
1002 }
1003 
1004 ATF_TC_WITHOUT_HEAD(cloexec);
ATF_TC_BODY(cloexec,tc)1005 ATF_TC_BODY(cloexec, tc)
1006 {
1007 	int fd;
1008 
1009 	gen_test_path();
1010 
1011 	/* shm_open(2) is required to set FD_CLOEXEC */
1012 	fd = shm_open(SHM_ANON, O_RDWR, 0777);
1013 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1014 	ATF_REQUIRE((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0);
1015 	close(fd);
1016 
1017 	/* Also make sure that named shm is correct */
1018 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0600);
1019 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1020 	ATF_REQUIRE((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0);
1021 	close(fd);
1022 }
1023 
1024 ATF_TC_WITHOUT_HEAD(mode);
ATF_TC_BODY(mode,tc)1025 ATF_TC_BODY(mode, tc)
1026 {
1027 	struct stat st;
1028 	int fd;
1029 	mode_t restore_mask;
1030 
1031 	gen_test_path();
1032 
1033 	/* Remove inhibitions from umask */
1034 	restore_mask = umask(0);
1035 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0600);
1036 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1037 	ATF_REQUIRE(fstat(fd, &st) == 0);
1038 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0600);
1039 	close(fd);
1040 	ATF_REQUIRE(shm_unlink(test_path) == 0);
1041 
1042 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0660);
1043 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1044 	ATF_REQUIRE(fstat(fd, &st) == 0);
1045 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0660);
1046 	close(fd);
1047 	ATF_REQUIRE(shm_unlink(test_path) == 0);
1048 
1049 	fd = shm_open(test_path, O_CREAT | O_RDWR, 0666);
1050 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1051 	ATF_REQUIRE(fstat(fd, &st) == 0);
1052 	ATF_REQUIRE((st.st_mode & ACCESSPERMS) == 0666);
1053 	close(fd);
1054 	ATF_REQUIRE(shm_unlink(test_path) == 0);
1055 
1056 	umask(restore_mask);
1057 }
1058 
1059 ATF_TC_WITHOUT_HEAD(fallocate);
ATF_TC_BODY(fallocate,tc)1060 ATF_TC_BODY(fallocate, tc)
1061 {
1062 	struct stat st;
1063 	int error, fd, sz;
1064 
1065 	/*
1066 	 * Primitive test case for posix_fallocate with shmd.  Effectively
1067 	 * expected to work like a smarter ftruncate that will grow the region
1068 	 * as needed in a race-free way.
1069 	 */
1070 	fd = shm_open(SHM_ANON, O_RDWR, 0666);
1071 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
1072 	/* Set the initial size. */
1073 	sz = 32;
1074 	ATF_REQUIRE(ftruncate(fd, sz) == 0);
1075 
1076 	/* Now grow it. */
1077 	error = 0;
1078 	sz *= 2;
1079 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz)) == 0,
1080 	    "posix_fallocate failed; error=%d", error);
1081 	ATF_REQUIRE(fstat(fd, &st) == 0);
1082 	ATF_REQUIRE(st.st_size == sz);
1083 	/* Attempt to shrink it; should succeed, but not change the size. */
1084 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, sz / 2)) == 0,
1085 	    "posix_fallocate failed; error=%d", error);
1086 	ATF_REQUIRE(fstat(fd, &st) == 0);
1087 	ATF_REQUIRE(st.st_size == sz);
1088 	/* Grow it using an offset of sz and len of sz. */
1089 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, sz, sz)) == 0,
1090 	    "posix_fallocate failed; error=%d", error);
1091 	ATF_REQUIRE(fstat(fd, &st) == 0);
1092 	ATF_REQUIRE(st.st_size == sz * 2);
1093 
1094 	close(fd);
1095 }
1096 
1097 ATF_TC_WITHOUT_HEAD(fspacectl);
ATF_TC_BODY(fspacectl,tc)1098 ATF_TC_BODY(fspacectl, tc)
1099 {
1100 	struct spacectl_range range;
1101 	off_t offset, length, shm_sz;
1102 	size_t page_size;
1103 	int fd, error;
1104 
1105 	page_size = getpagesize();
1106 	shm_sz = shm_max_pages * page_size;
1107 
1108 	fd = shm_open("/testtest", O_RDWR | O_CREAT, 0666);
1109 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno:%d", errno);
1110 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, shm_sz)) == 0,
1111 	    "posix_fallocate failed; error=%d", error);
1112 
1113 	/* Aligned fspacectl(fd, SPACECTL_DEALLOC, ...) */
1114 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1115 	range.r_offset = offset = page_size;
1116 	range.r_len = length = ((shm_max_pages - 1) * page_size) -
1117 	    range.r_offset;
1118 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1119 	    "Aligned fspacectl failed; errno=%d", errno);
1120 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1121 	    "Aligned fspacectl content checking failed");
1122 
1123 	/* Unaligned fspacectl(fd, SPACECTL_DEALLOC, ...) */
1124 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1125 	range.r_offset = offset = page_size / 2;
1126 	range.r_len = length = (shm_max_pages - 1) * page_size +
1127 	    (page_size / 2) - offset;
1128 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1129 	    "Unaligned fspacectl failed; errno=%d", errno);
1130 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1131 	    "Unaligned fspacectl content checking failed");
1132 
1133 	/* Aligned fspacectl(fd, SPACECTL_DEALLOC, ...) to OFF_MAX */
1134 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1135 	range.r_offset = offset = page_size;
1136 	range.r_len = length = OFF_MAX - offset;
1137 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1138 	    "Aligned fspacectl to OFF_MAX failed; errno=%d", errno);
1139 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1140 	    "Aligned fspacectl to OFF_MAX content checking failed");
1141 
1142 	/* Unaligned fspacectl(fd, SPACECTL_DEALLOC, ...) to OFF_MAX */
1143 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1144 	range.r_offset = offset = page_size / 2;
1145 	range.r_len = length = OFF_MAX - offset;
1146 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1147 	    "Unaligned fspacectl to OFF_MAX failed; errno=%d", errno);
1148 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1149 	    "Unaligned fspacectl to OFF_MAX content checking failed");
1150 
1151 	/* Aligned fspacectl(fd, SPACECTL_DEALLOC, ...) past shm_sz */
1152 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1153 	range.r_offset = offset = page_size;
1154 	range.r_len = length = (shm_max_pages + 1) * page_size - offset;
1155 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1156 	    "Aligned fspacectl past shm_sz failed; errno=%d", errno);
1157 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1158 	    "Aligned fspacectl past shm_sz content checking failed");
1159 
1160 	/* Unaligned fspacectl(fd, SPACECTL_DEALLOC, ...) past shm_sz */
1161 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1162 	range.r_offset = offset = page_size / 2;
1163 	range.r_len = length = (shm_max_pages + 1) * page_size - offset;
1164 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1165 	    "Unaligned fspacectl past shm_sz failed; errno=%d", errno);
1166 	ATF_CHECK_MSG(check_content_dealloc(fd, offset, length, shm_sz) == 0,
1167 	    "Unaligned fspacectl past shm_sz content checking failed");
1168 
1169 	ATF_REQUIRE(close(fd) == 0);
1170 }
1171 
1172 ATF_TC_WITHOUT_HEAD(accounting);
ATF_TC_BODY(accounting,tc)1173 ATF_TC_BODY(accounting, tc)
1174 {
1175 	struct spacectl_range range;
1176 	struct stat st;
1177 	off_t shm_sz, len;
1178 	size_t page_size;
1179 	int fd, error;
1180 
1181 	page_size = getpagesize();
1182 	shm_sz = shm_max_pages * page_size;
1183 
1184 	fd = shm_open("/testtest1", O_RDWR | O_CREAT, 0666);
1185 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno:%d", errno);
1186 	ATF_REQUIRE_MSG((error = posix_fallocate(fd, 0, shm_sz)) == 0,
1187 	    "posix_fallocate failed; error=%d", error);
1188 
1189 	ATF_REQUIRE(shm_fill(fd, 0, shm_sz) == 0);
1190 	ATF_REQUIRE(fstat(fd, &st) == 0);
1191 	ATF_REQUIRE(st.st_blksize * st.st_blocks == (blkcnt_t)shm_sz);
1192 
1193 	range.r_offset = page_size;
1194 	range.r_len = len = (shm_max_pages - 1) * page_size -
1195 	    range.r_offset;
1196 	ATF_CHECK_MSG(fspacectl(fd, SPACECTL_DEALLOC, &range, 0, &range) == 0,
1197 	    "SPACECTL_DEALLOC failed; errno=%d", errno);
1198 	ATF_REQUIRE(fstat(fd, &st) == 0);
1199 	ATF_REQUIRE(st.st_blksize * st.st_blocks == (blkcnt_t)(shm_sz - len));
1200 
1201 	ATF_REQUIRE(close(fd) == 0);
1202 }
1203 
1204 ATF_TC_WITHOUT_HEAD(mmap_prot);
ATF_TC_BODY(mmap_prot,tc)1205 ATF_TC_BODY(mmap_prot, tc)
1206 {
1207 	void *p;
1208 	int fd, pagesize;
1209 
1210 	ATF_REQUIRE((pagesize = getpagesize()) > 0);
1211 
1212 	gen_test_path();
1213 	fd = shm_open(test_path, O_RDONLY | O_CREAT, 0644);
1214 	ATF_REQUIRE(fd >= 0);
1215 
1216 	p = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
1217 	ATF_REQUIRE(p != MAP_FAILED);
1218 	ATF_REQUIRE(munmap(p, pagesize) == 0);
1219 	p = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1220 	ATF_REQUIRE_ERRNO(EACCES, p == MAP_FAILED);
1221 	p = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
1222 	ATF_REQUIRE(p != MAP_FAILED);
1223 	ATF_REQUIRE(munmap(p, pagesize) == 0);
1224 
1225 	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
1226 	    "shm_unlink failed; errno=%d", errno);
1227 	ATF_REQUIRE_MSG(close(fd) == 0,
1228 	    "close failed; errno=%d", errno);
1229 }
1230 
1231 static int
shm_open_large(int psind,int policy,size_t sz)1232 shm_open_large(int psind, int policy, size_t sz)
1233 {
1234 	int error, fd;
1235 
1236 	fd = shm_create_largepage(SHM_ANON, O_CREAT | O_RDWR, psind, policy, 0);
1237 	if (fd < 0 && errno == ENOTTY)
1238 		atf_tc_skip("no large page support");
1239 	ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; errno=%d", errno);
1240 
1241 	error = ftruncate(fd, sz);
1242 	if (error != 0 && errno == ENOMEM)
1243 		/*
1244 		 * The test system might not have enough memory to accommodate
1245 		 * the request.
1246 		 */
1247 		atf_tc_skip("failed to allocate %zu-byte superpage", sz);
1248 	ATF_REQUIRE_MSG(error == 0, "ftruncate failed; errno=%d", errno);
1249 
1250 	return (fd);
1251 }
1252 
1253 ATF_TC_WITHOUT_HEAD(largepage_basic);
ATF_TC_BODY(largepage_basic,tc)1254 ATF_TC_BODY(largepage_basic, tc)
1255 {
1256 	char *zeroes;
1257 	char *addr, *vec;
1258 	size_t ps[MAXPAGESIZES];
1259 	int error, fd, pscnt;
1260 
1261 	pscnt = pagesizes(ps, true);
1262 	zeroes = calloc(1, ps[0]);
1263 	ATF_REQUIRE(zeroes != NULL);
1264 	for (int i = 1; i < pscnt; i++) {
1265 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1266 
1267 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1268 		    0);
1269 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1270 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1271 		ATF_REQUIRE_MSG(((uintptr_t)addr & (ps[i] - 1)) == 0,
1272 		    "mmap(%zu bytes) returned unaligned mapping; addr=%p",
1273 		    ps[i], addr);
1274 
1275 		/* Force a page fault. */
1276 		*(volatile char *)addr = 0;
1277 
1278 		vec = malloc(ps[i] / ps[0]);
1279 		ATF_REQUIRE(vec != NULL);
1280 		error = mincore(addr, ps[i], vec);
1281 		ATF_REQUIRE_MSG(error == 0, "mincore failed; errno=%d", errno);
1282 
1283 		/* Verify that all pages in the run are mapped. */
1284 		for (size_t p = 0; p < ps[i] / ps[0]; p++) {
1285 			ATF_REQUIRE_MSG((vec[p] & MINCORE_INCORE) != 0,
1286 			    "page %zu is not mapped", p);
1287 			ATF_REQUIRE_MSG((vec[p] & MINCORE_SUPER) ==
1288 			    MINCORE_PSIND(i),
1289 			    "page %zu is not in a %zu-byte superpage",
1290 			    p, ps[i]);
1291 		}
1292 
1293 		/* Validate zeroing. */
1294 		for (size_t p = 0; p < ps[i] / ps[0]; p++) {
1295 			ATF_REQUIRE_MSG(memcmp(addr + p * ps[0], zeroes,
1296 			    ps[0]) == 0, "page %zu miscompare", p);
1297 		}
1298 
1299 		free(vec);
1300 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1301 		ATF_REQUIRE(close(fd) == 0);
1302 	}
1303 
1304 	free(zeroes);
1305 }
1306 
1307 extern int __sys_shm_open2(const char *, int, mode_t, int, const char *);
1308 
1309 ATF_TC_WITHOUT_HEAD(largepage_config);
ATF_TC_BODY(largepage_config,tc)1310 ATF_TC_BODY(largepage_config, tc)
1311 {
1312 	struct shm_largepage_conf lpc;
1313 	char *addr, *buf;
1314 	size_t ps[MAXPAGESIZES + 1]; /* silence warnings if MAXPAGESIZES == 1 */
1315 	int error, fd;
1316 
1317 	(void)pagesizes(ps, true);
1318 
1319 	fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0);
1320 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
1321 
1322 	/*
1323 	 * Configure a large page policy for an object created without
1324 	 * SHM_LARGEPAGE.
1325 	 */
1326 	lpc.psind = 1;
1327 	lpc.alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT;
1328 	error = ioctl(fd, FIOSSHMLPGCNF, &lpc);
1329 	ATF_REQUIRE(error != 0);
1330 	ATF_REQUIRE_MSG(errno == ENOTTY, "ioctl(FIOSSHMLPGCNF) returned %d",
1331 	    errno);
1332 	ATF_REQUIRE(close(fd) == 0);
1333 
1334 	/*
1335 	 * Create a largepage object and try to use it without actually
1336 	 * configuring anything.
1337 	 */
1338 	fd = __sys_shm_open2(SHM_ANON, O_CREAT | O_RDWR, 0, SHM_LARGEPAGE,
1339 	    NULL);
1340 	if (fd < 0 && errno == ENOTTY)
1341 		atf_tc_skip("no large page support");
1342 	ATF_REQUIRE_MSG(fd >= 0, "shm_open2 failed; error=%d", errno);
1343 
1344 	error = ftruncate(fd, ps[1]);
1345 	ATF_REQUIRE(error != 0);
1346 	ATF_REQUIRE_MSG(errno == EINVAL, "ftruncate returned %d", errno);
1347 
1348 	addr = mmap(NULL, ps[1], PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1349 	ATF_REQUIRE(addr == MAP_FAILED);
1350 	ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1351 	addr = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1352 	ATF_REQUIRE(addr == MAP_FAILED);
1353 	ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1354 
1355 	buf = calloc(1, ps[0]);
1356 	ATF_REQUIRE(buf != NULL);
1357 	ATF_REQUIRE(write(fd, buf, ps[0]) == -1);
1358 	ATF_REQUIRE_MSG(errno == EINVAL, "write returned %d", errno);
1359 	free(buf);
1360 	buf = calloc(1, ps[1]);
1361 	ATF_REQUIRE(buf != NULL);
1362 	ATF_REQUIRE(write(fd, buf, ps[1]) == -1);
1363 	ATF_REQUIRE_MSG(errno == EINVAL, "write returned %d", errno);
1364 	free(buf);
1365 
1366 	error = posix_fallocate(fd, 0, ps[0]);
1367 	ATF_REQUIRE_MSG(error == EINVAL, "posix_fallocate returned %d", error);
1368 
1369 	ATF_REQUIRE(close(fd) == 0);
1370 }
1371 
1372 ATF_TC_WITHOUT_HEAD(largepage_mmap);
ATF_TC_BODY(largepage_mmap,tc)1373 ATF_TC_BODY(largepage_mmap, tc)
1374 {
1375 	char *addr, *addr1, *vec;
1376 	size_t ps[MAXPAGESIZES];
1377 	int fd, pscnt;
1378 
1379 	pscnt = pagesizes(ps, true);
1380 	for (int i = 1; i < pscnt; i++) {
1381 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1382 
1383 		/* For mincore(). */
1384 		vec = malloc(ps[i]);
1385 		ATF_REQUIRE(vec != NULL);
1386 
1387 		/*
1388 		 * Wrong mapping size.
1389 		 */
1390 		addr = mmap(NULL, ps[i - 1], PROT_READ | PROT_WRITE, MAP_SHARED,
1391 		    fd, 0);
1392 		ATF_REQUIRE_MSG(addr == MAP_FAILED,
1393 		    "mmap(%zu bytes) succeeded", ps[i - 1]);
1394 		ATF_REQUIRE_MSG(errno == EINVAL,
1395 		    "mmap(%zu bytes) failed; error=%d", ps[i - 1], errno);
1396 
1397 		/*
1398 		 * Fixed mappings.
1399 		 */
1400 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1401 		    0);
1402 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1403 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1404 		ATF_REQUIRE_MSG(((uintptr_t)addr & (ps[i] - 1)) == 0,
1405 		    "mmap(%zu bytes) returned unaligned mapping; addr=%p",
1406 		    ps[i], addr);
1407 
1408 		/* Try mapping a small page with anonymous memory. */
1409 		addr1 = mmap(addr, ps[i - 1], PROT_READ | PROT_WRITE,
1410 		    MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
1411 		ATF_REQUIRE_MSG(addr1 == MAP_FAILED,
1412 		    "anon mmap(%zu bytes) succeeded", ps[i - 1]);
1413 		ATF_REQUIRE_MSG(errno == EINVAL, "mmap returned %d", errno);
1414 
1415 		/* Check MAP_EXCL when creating a second largepage mapping. */
1416 		addr1 = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
1417 		    MAP_SHARED | MAP_FIXED | MAP_EXCL, fd, 0);
1418 		ATF_REQUIRE_MSG(addr1 == MAP_FAILED,
1419 		    "mmap(%zu bytes) succeeded", ps[i]);
1420 		/* XXX wrong errno */
1421 		ATF_REQUIRE_MSG(errno == ENOSPC, "mmap returned %d", errno);
1422 
1423 		/* Overwrite a largepage mapping with a lagepage mapping. */
1424 		addr1 = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
1425 		    MAP_SHARED | MAP_FIXED, fd, 0);
1426 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1427 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1428 		ATF_REQUIRE_MSG(addr == addr1,
1429 		    "mmap(%zu bytes) moved from %p to %p", ps[i], addr, addr1);
1430 
1431 		ATF_REQUIRE(munmap(addr, ps[i] == 0));
1432 
1433 		/* Clobber an anonymous mapping with a superpage. */
1434 		addr1 = mmap(NULL, ps[i], PROT_READ | PROT_WRITE,
1435 		    MAP_ANON | MAP_PRIVATE | MAP_ALIGNED(ffsl(ps[i]) - 1), -1,
1436 		    0);
1437 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1438 		    "mmap failed; error=%d", errno);
1439 		*(volatile char *)addr1 = '\0';
1440 		addr = mmap(addr1, ps[i], PROT_READ | PROT_WRITE,
1441 		    MAP_SHARED | MAP_FIXED, fd, 0);
1442 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1443 		    "mmap failed; error=%d", errno);
1444 		ATF_REQUIRE_MSG(addr == addr1,
1445 		    "mmap disobeyed MAP_FIXED, %p %p", addr, addr1);
1446 		*(volatile char *)addr = 0; /* fault */
1447 		ATF_REQUIRE(mincore(addr, ps[i], vec) == 0);
1448 		for (size_t p = 0; p < ps[i] / ps[0]; p++) {
1449 			ATF_REQUIRE_MSG((vec[p] & MINCORE_INCORE) != 0,
1450 			    "page %zu is not resident", p);
1451 			ATF_REQUIRE_MSG((vec[p] & MINCORE_SUPER) ==
1452 			    MINCORE_PSIND(i),
1453 			    "page %zu is not resident", p);
1454 		}
1455 
1456 		/*
1457 		 * Copy-on-write mappings are not permitted.
1458 		 */
1459 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_PRIVATE,
1460 		    fd, 0);
1461 		ATF_REQUIRE_MSG(addr == MAP_FAILED,
1462 		    "mmap(%zu bytes) succeeded", ps[i]);
1463 
1464 		ATF_REQUIRE(close(fd) == 0);
1465 	}
1466 }
1467 
1468 ATF_TC_WITHOUT_HEAD(largepage_munmap);
ATF_TC_BODY(largepage_munmap,tc)1469 ATF_TC_BODY(largepage_munmap, tc)
1470 {
1471 	char *addr;
1472 	size_t ps[MAXPAGESIZES], ps1;
1473 	int fd, pscnt;
1474 
1475 	pscnt = pagesizes(ps, true);
1476 	for (int i = 1; i < pscnt; i++) {
1477 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1478 		ps1 = ps[i - 1];
1479 
1480 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1481 		    0);
1482 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1483 		    "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
1484 
1485 		/* Try several unaligned munmap() requests. */
1486 		ATF_REQUIRE(munmap(addr, ps1) != 0);
1487 		ATF_REQUIRE_MSG(errno == EINVAL,
1488 		    "unexpected error %d from munmap", errno);
1489 		ATF_REQUIRE(munmap(addr, ps[i] - ps1));
1490 		ATF_REQUIRE_MSG(errno == EINVAL,
1491 		    "unexpected error %d from munmap", errno);
1492 		ATF_REQUIRE(munmap(addr + ps1, ps1) != 0);
1493 		ATF_REQUIRE_MSG(errno == EINVAL,
1494 		    "unexpected error %d from munmap", errno);
1495 		ATF_REQUIRE(munmap(addr, 0));
1496 		ATF_REQUIRE_MSG(errno == EINVAL,
1497 		    "unexpected error %d from munmap", errno);
1498 
1499 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1500 		ATF_REQUIRE(close(fd) == 0);
1501 	}
1502 }
1503 
1504 static void
largepage_madvise(char * addr,size_t sz,int advice,int error)1505 largepage_madvise(char *addr, size_t sz, int advice, int error)
1506 {
1507 	if (error == 0) {
1508 		ATF_REQUIRE_MSG(madvise(addr, sz, advice) == 0,
1509 		    "madvise(%zu, %d) failed; error=%d", sz, advice, errno);
1510 	} else {
1511 		ATF_REQUIRE_MSG(madvise(addr, sz, advice) != 0,
1512 		    "madvise(%zu, %d) succeeded", sz, advice);
1513 		ATF_REQUIRE_MSG(errno == error,
1514 		    "unexpected error %d from madvise(%zu, %d)",
1515 		    errno, sz, advice);
1516 	}
1517 }
1518 
1519 ATF_TC_WITHOUT_HEAD(largepage_madvise);
ATF_TC_BODY(largepage_madvise,tc)1520 ATF_TC_BODY(largepage_madvise, tc)
1521 {
1522 	char *addr;
1523 	size_t ps[MAXPAGESIZES];
1524 	int fd, pscnt;
1525 
1526 	pscnt = pagesizes(ps, true);
1527 	for (int i = 1; i < pscnt; i++) {
1528 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1529 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1530 		    0);
1531 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1532 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1533 
1534 		memset(addr, 0, ps[i]);
1535 
1536 		/* Advice that requires clipping. */
1537 		largepage_madvise(addr, ps[0], MADV_NORMAL, EINVAL);
1538 		largepage_madvise(addr, ps[i], MADV_NORMAL, 0);
1539 		largepage_madvise(addr, ps[0], MADV_RANDOM, EINVAL);
1540 		largepage_madvise(addr, ps[i], MADV_RANDOM, 0);
1541 		largepage_madvise(addr, ps[0], MADV_SEQUENTIAL, EINVAL);
1542 		largepage_madvise(addr, ps[i], MADV_SEQUENTIAL, 0);
1543 		largepage_madvise(addr, ps[0], MADV_NOSYNC, EINVAL);
1544 		largepage_madvise(addr, ps[i], MADV_NOSYNC, 0);
1545 		largepage_madvise(addr, ps[0], MADV_AUTOSYNC, EINVAL);
1546 		largepage_madvise(addr, ps[i], MADV_AUTOSYNC, 0);
1547 		largepage_madvise(addr, ps[0], MADV_CORE, EINVAL);
1548 		largepage_madvise(addr, ps[i], MADV_CORE, 0);
1549 		largepage_madvise(addr, ps[0], MADV_NOCORE, EINVAL);
1550 		largepage_madvise(addr, ps[i], MADV_NOCORE, 0);
1551 
1552 		/* Advice that does not result in clipping. */
1553 		largepage_madvise(addr, ps[0], MADV_DONTNEED, 0);
1554 		largepage_madvise(addr, ps[i], MADV_DONTNEED, 0);
1555 		largepage_madvise(addr, ps[0], MADV_WILLNEED, 0);
1556 		largepage_madvise(addr, ps[i], MADV_WILLNEED, 0);
1557 		largepage_madvise(addr, ps[0], MADV_FREE, 0);
1558 		largepage_madvise(addr, ps[i], MADV_FREE, 0);
1559 
1560 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1561 		ATF_REQUIRE(close(fd) == 0);
1562 	}
1563 }
1564 
1565 ATF_TC(largepage_mlock);
ATF_TC_HEAD(largepage_mlock,tc)1566 ATF_TC_HEAD(largepage_mlock, tc)
1567 {
1568 	/* Needed to set rlimit. */
1569 	atf_tc_set_md_var(tc, "require.user", "root");
1570 }
ATF_TC_BODY(largepage_mlock,tc)1571 ATF_TC_BODY(largepage_mlock, tc)
1572 {
1573 	struct rlimit rl;
1574 	char *addr;
1575 	size_t ps[MAXPAGESIZES], sz;
1576 	u_long max_wired, wired;
1577 	int fd, error, pscnt;
1578 
1579 	rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
1580 	ATF_REQUIRE_MSG(setrlimit(RLIMIT_MEMLOCK, &rl) == 0,
1581 	    "setrlimit failed; error=%d", errno);
1582 
1583 	sz = sizeof(max_wired);
1584 	error = sysctlbyname("vm.max_user_wired", &max_wired, &sz, NULL, 0);
1585 	ATF_REQUIRE_MSG(error == 0,
1586 	    "sysctlbyname(vm.max_user_wired) failed; error=%d", errno);
1587 
1588 	sz = sizeof(wired);
1589 	error = sysctlbyname("vm.stats.vm.v_user_wire_count", &wired, &sz, NULL,
1590 	    0);
1591 	ATF_REQUIRE_MSG(error == 0,
1592 	    "sysctlbyname(vm.stats.vm.v_user_wire_count) failed; error=%d",
1593 	    errno);
1594 
1595 	pscnt = pagesizes(ps, true);
1596 	for (int i = 1; i < pscnt; i++) {
1597 		if (ps[i] / ps[0] > max_wired - wired) {
1598 			/* Cannot wire past the limit. */
1599 			atf_tc_skip("test would exceed wiring limit");
1600 		}
1601 
1602 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1603 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1604 		    0);
1605 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1606 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1607 
1608 		ATF_REQUIRE(mlock(addr, ps[0]) != 0);
1609 		ATF_REQUIRE_MSG(errno == EINVAL,
1610 		    "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1611 		ATF_REQUIRE(mlock(addr, ps[i] - ps[0]) != 0);
1612 		ATF_REQUIRE_MSG(errno == EINVAL,
1613 		    "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1614 
1615 		ATF_REQUIRE_MSG(mlock(addr, ps[i]) == 0,
1616 		    "mlock failed; error=%d", errno);
1617 
1618 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1619 
1620 		ATF_REQUIRE(mlockall(MCL_FUTURE) == 0);
1621 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1622 		    0);
1623 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1624 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1625 
1626 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1627 		ATF_REQUIRE(close(fd) == 0);
1628 	}
1629 }
1630 
1631 ATF_TC_WITHOUT_HEAD(largepage_msync);
ATF_TC_BODY(largepage_msync,tc)1632 ATF_TC_BODY(largepage_msync, tc)
1633 {
1634 	char *addr;
1635 	size_t ps[MAXPAGESIZES];
1636 	int fd, pscnt;
1637 
1638 	pscnt = pagesizes(ps, true);
1639 	for (int i = 1; i < pscnt; i++) {
1640 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1641 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1642 		    0);
1643 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1644 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1645 
1646 		memset(addr, 0, ps[i]);
1647 
1648 		/*
1649 		 * "Sync" requests are no-ops for SHM objects, so small
1650 		 * PAGE_SIZE-sized requests succeed.
1651 		 */
1652 		ATF_REQUIRE_MSG(msync(addr, ps[0], MS_ASYNC) == 0,
1653 		    "msync(MS_ASYNC) failed; error=%d", errno);
1654 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_ASYNC) == 0,
1655 		    "msync(MS_ASYNC) failed; error=%d", errno);
1656 		ATF_REQUIRE_MSG(msync(addr, ps[0], MS_SYNC) == 0,
1657 		    "msync(MS_SYNC) failed; error=%d", errno);
1658 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_SYNC) == 0,
1659 		    "msync(MS_SYNC) failed; error=%d", errno);
1660 
1661 		ATF_REQUIRE_MSG(msync(addr, ps[0], MS_INVALIDATE) != 0,
1662 		    "msync(MS_INVALIDATE) succeeded");
1663 		/* XXX wrong errno */
1664 		ATF_REQUIRE_MSG(errno == EBUSY,
1665 		    "unexpected error %d from msync(MS_INVALIDATE)", errno);
1666 		ATF_REQUIRE_MSG(msync(addr, ps[i], MS_INVALIDATE) == 0,
1667 		    "msync(MS_INVALIDATE) failed; error=%d", errno);
1668 		memset(addr, 0, ps[i]);
1669 
1670 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1671 		ATF_REQUIRE(close(fd) == 0);
1672 	}
1673 }
1674 
1675 static void
largepage_protect(char * addr,size_t sz,int prot,int error)1676 largepage_protect(char *addr, size_t sz, int prot, int error)
1677 {
1678 	if (error == 0) {
1679 		ATF_REQUIRE_MSG(mprotect(addr, sz, prot) == 0,
1680 		    "mprotect(%zu, %x) failed; error=%d", sz, prot, errno);
1681 	} else {
1682 		ATF_REQUIRE_MSG(mprotect(addr, sz, prot) != 0,
1683 		    "mprotect(%zu, %x) succeeded", sz, prot);
1684 		ATF_REQUIRE_MSG(errno == error,
1685 		    "unexpected error %d from mprotect(%zu, %x)",
1686 		    errno, sz, prot);
1687 	}
1688 }
1689 
1690 ATF_TC_WITHOUT_HEAD(largepage_mprotect);
ATF_TC_BODY(largepage_mprotect,tc)1691 ATF_TC_BODY(largepage_mprotect, tc)
1692 {
1693 	char *addr, *addr1;
1694 	size_t ps[MAXPAGESIZES];
1695 	int fd, pscnt;
1696 
1697 	pscnt = pagesizes(ps, true);
1698 	for (int i = 1; i < pscnt; i++) {
1699 		/*
1700 		 * Reserve a contiguous region in the address space to avoid
1701 		 * spurious failures in the face of ASLR.
1702 		 */
1703 		addr = mmap(NULL, ps[i] * 2, PROT_NONE,
1704 		    MAP_ANON | MAP_ALIGNED(ffsl(ps[i]) - 1), -1, 0);
1705 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1706 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1707 		ATF_REQUIRE(munmap(addr, ps[i] * 2) == 0);
1708 
1709 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1710 		addr = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
1711 		    MAP_SHARED | MAP_FIXED, fd, 0);
1712 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1713 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1714 
1715 		/*
1716 		 * These should be no-ops from the pmap perspective since the
1717 		 * page is not yet entered into the pmap.
1718 		 */
1719 		largepage_protect(addr, ps[0], PROT_READ, EINVAL);
1720 		largepage_protect(addr, ps[i], PROT_READ, 0);
1721 		largepage_protect(addr, ps[0], PROT_NONE, EINVAL);
1722 		largepage_protect(addr, ps[i], PROT_NONE, 0);
1723 		largepage_protect(addr, ps[0],
1724 		    PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1725 		largepage_protect(addr, ps[i],
1726 		    PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1727 
1728 		/* Trigger creation of a mapping and try again. */
1729 		*(volatile char *)addr = 0;
1730 		largepage_protect(addr, ps[0], PROT_READ, EINVAL);
1731 		largepage_protect(addr, ps[i], PROT_READ, 0);
1732 		largepage_protect(addr, ps[0], PROT_NONE, EINVAL);
1733 		largepage_protect(addr, ps[i], PROT_NONE, 0);
1734 		largepage_protect(addr, ps[0],
1735 		    PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1736 		largepage_protect(addr, ps[i],
1737 		    PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1738 
1739 		memset(addr, 0, ps[i]);
1740 
1741 		/* Map two contiguous large pages and merge map entries. */
1742 		addr1 = mmap(addr + ps[i], ps[i], PROT_READ | PROT_WRITE,
1743 		    MAP_SHARED | MAP_FIXED | MAP_EXCL, fd, 0);
1744 		ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1745 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1746 
1747 		largepage_protect(addr1 - ps[0], ps[0] * 2,
1748 		    PROT_READ | PROT_WRITE, EINVAL);
1749 		largepage_protect(addr, ps[i] * 2, PROT_READ | PROT_WRITE, 0);
1750 
1751 		memset(addr, 0, ps[i] * 2);
1752 
1753 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1754 		ATF_REQUIRE(munmap(addr1, ps[i]) == 0);
1755 		ATF_REQUIRE(close(fd) == 0);
1756 	}
1757 }
1758 
1759 ATF_TC_WITHOUT_HEAD(largepage_minherit);
ATF_TC_BODY(largepage_minherit,tc)1760 ATF_TC_BODY(largepage_minherit, tc)
1761 {
1762 	char *addr;
1763 	size_t ps[MAXPAGESIZES];
1764 	pid_t child;
1765 	int fd, pscnt, status;
1766 
1767 	pscnt = pagesizes(ps, true);
1768 	for (int i = 1; i < pscnt; i++) {
1769 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1770 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1771 		    0);
1772 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1773 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1774 
1775 		ATF_REQUIRE(minherit(addr, ps[0], INHERIT_SHARE) != 0);
1776 
1777 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_SHARE) == 0,
1778 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1779 		child = fork();
1780 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1781 		if (child == 0) {
1782 			char v;
1783 
1784 			*(volatile char *)addr = 0;
1785 			if (mincore(addr, ps[0], &v) != 0)
1786 				_exit(1);
1787 			if ((v & MINCORE_SUPER) == 0)
1788 				_exit(2);
1789 			_exit(0);
1790 		}
1791 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1792 		    "waitpid failed; error=%d", errno);
1793 		ATF_REQUIRE_MSG(WIFEXITED(status),
1794 		    "child was killed by signal %d", WTERMSIG(status));
1795 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1796 		    "child exited with status %d", WEXITSTATUS(status));
1797 
1798 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_NONE) == 0,
1799 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1800 		child = fork();
1801 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1802 		if (child == 0) {
1803 			char v;
1804 
1805 			if (mincore(addr, ps[0], &v) == 0)
1806 				_exit(1);
1807 			_exit(0);
1808 		}
1809 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1810 		    "waitpid failed; error=%d", errno);
1811 		ATF_REQUIRE_MSG(WIFEXITED(status),
1812 		    "child was killed by signal %d", WTERMSIG(status));
1813 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1814 		    "child exited with status %d", WEXITSTATUS(status));
1815 
1816 		/* Copy-on-write is not supported for static large pages. */
1817 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_COPY) != 0,
1818 		    "minherit(%zu bytes) succeeded", ps[i]);
1819 
1820 		ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_ZERO) == 0,
1821 		    "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1822 		child = fork();
1823 		ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1824 		if (child == 0) {
1825 			char v;
1826 
1827 			*(volatile char *)addr = 0;
1828 			if (mincore(addr, ps[0], &v) != 0)
1829 				_exit(1);
1830 			if ((v & MINCORE_SUPER) != 0)
1831 				_exit(2);
1832 			_exit(0);
1833 		}
1834 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1835 		    "waitpid failed; error=%d", errno);
1836 		ATF_REQUIRE_MSG(WIFEXITED(status),
1837 		    "child was killed by signal %d", WTERMSIG(status));
1838 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1839 		    "child exited with status %d", WEXITSTATUS(status));
1840 
1841 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1842 		ATF_REQUIRE(close(fd) == 0);
1843 	}
1844 }
1845 
1846 ATF_TC_WITHOUT_HEAD(largepage_pipe);
ATF_TC_BODY(largepage_pipe,tc)1847 ATF_TC_BODY(largepage_pipe, tc)
1848 {
1849 	size_t ps[MAXPAGESIZES];
1850 	char *addr;
1851 	ssize_t len;
1852 	int fd, pfd[2], pscnt, status;
1853 	pid_t child;
1854 
1855 	pscnt = pagesizes(ps, true);
1856 
1857 	for (int i = 1; i < pscnt; i++) {
1858 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1859 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1860 		    0);
1861 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1862 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1863 
1864 		/* Trigger creation of a mapping. */
1865 		*(volatile char *)addr = '\0';
1866 
1867 		ATF_REQUIRE(pipe(pfd) == 0);
1868 		child = fork();
1869 		ATF_REQUIRE_MSG(child != -1, "fork() failed; error=%d", errno);
1870 		if (child == 0) {
1871 			char buf[BUFSIZ];
1872 			ssize_t resid;
1873 
1874 			(void)close(pfd[0]);
1875 			for (resid = (size_t)ps[i]; resid > 0; resid -= len) {
1876 				len = read(pfd[1], buf, sizeof(buf));
1877 				if (len < 0)
1878 					_exit(1);
1879 			}
1880 			_exit(0);
1881 		}
1882 		ATF_REQUIRE(close(pfd[1]) == 0);
1883 		len = write(pfd[0], addr, ps[i]);
1884 		ATF_REQUIRE_MSG(len >= 0, "write() failed; error=%d", errno);
1885 		ATF_REQUIRE_MSG(len == (ssize_t)ps[i],
1886 		    "short write; len=%zd", len);
1887 		ATF_REQUIRE(close(pfd[0]) == 0);
1888 
1889 		ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1890 		    "waitpid() failed; error=%d", errno);
1891 		ATF_REQUIRE_MSG(WIFEXITED(status),
1892 		    "child was killed by signal %d", WTERMSIG(status));
1893 		ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1894 		    "child exited with status %d", WEXITSTATUS(status));
1895 
1896 		ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1897 		ATF_REQUIRE(close(fd) == 0);
1898 	}
1899 }
1900 
1901 #ifdef __amd64__
1902 static sigjmp_buf jmpbuf;
1903 static _Atomic(void *) faultaddr;
1904 static _Atomic(int) faultsig;
1905 
1906 #define	KEY_RW	1
1907 #define	KEY_RO	2
1908 #define KEY_WO	3
1909 #define KEY_NO	4
1910 #define	VAL	0xdeadfacec0debeef
1911 static void
set_keys(void)1912 set_keys(void)
1913 {
1914 	int error;
1915 
1916 	error = x86_pkru_set_perm(KEY_RW, 1, 1);
1917 	ATF_REQUIRE(error == 0);
1918 	error = x86_pkru_set_perm(KEY_RO, 1, 0);
1919 	ATF_REQUIRE(error == 0);
1920 	error = x86_pkru_set_perm(KEY_WO, 0, 1);
1921 	ATF_REQUIRE(error == 0);
1922 	error = x86_pkru_set_perm(KEY_NO, 0, 0);
1923 	ATF_REQUIRE(error == 0);
1924 }
1925 
1926 static void
sigsegv(int sig,siginfo_t * si,void * uc __unused)1927 sigsegv(int sig, siginfo_t *si, void *uc __unused)
1928 {
1929 	faultsig = sig;
1930 	faultaddr = si->si_addr;
1931 	siglongjmp(jmpbuf, 1);
1932 }
1933 
1934 static bool
try_read(volatile uint64_t * p,uint64_t * outp)1935 try_read(volatile uint64_t *p, uint64_t *outp)
1936 {
1937 	if (sigsetjmp(jmpbuf, 1) == 0) {
1938 		*outp = *p;
1939 		return (true);
1940 	} else {
1941 		atomic_signal_fence(memory_order_relaxed);
1942 		ATF_REQUIRE(faultsig == SIGSEGV);
1943 		ATF_REQUIRE(faultaddr == p);
1944 		set_keys(); /* PKRU is not restored by siglongjmp? */
1945 		return (false);
1946 	}
1947 }
1948 
1949 static bool
try_write(volatile uint64_t * p,uint64_t val)1950 try_write(volatile uint64_t *p, uint64_t val)
1951 {
1952 	if (sigsetjmp(jmpbuf, 1) == 0) {
1953 		*p = val;
1954 		return (true);
1955 	} else {
1956 		atomic_signal_fence(memory_order_relaxed);
1957 		ATF_REQUIRE(faultsig == SIGSEGV);
1958 		ATF_REQUIRE(faultaddr == p);
1959 		set_keys(); /* PKRU is not restored by siglongjmp? */
1960 		return (false);
1961 	}
1962 }
1963 
1964 ATF_TC_WITHOUT_HEAD(largepage_pkru);
ATF_TC_BODY(largepage_pkru,tc)1965 ATF_TC_BODY(largepage_pkru, tc)
1966 {
1967 	size_t ps[MAXPAGESIZES];
1968 	struct sigaction sa;
1969 	char *addr, *addr1;
1970 	int error, fd, pscnt;
1971 	u_int regs[4];
1972 
1973 	do_cpuid(0, regs);
1974 	if (regs[0] < 7)
1975 		atf_tc_skip("PKU not supported");
1976 	cpuid_count(7, 0, regs);
1977 	if ((regs[2] & CPUID_STDEXT2_PKU) == 0)
1978 		atf_tc_skip("PKU not supported");
1979 
1980 	memset(&sa, 0, sizeof(sa));
1981 	sa.sa_sigaction = sigsegv;
1982 	sa.sa_flags = SA_SIGINFO;
1983 	sigemptyset(&sa.sa_mask);
1984 	error = sigaction(SIGSEGV, &sa, NULL);
1985 	ATF_REQUIRE(error == 0);
1986 
1987 	pscnt = pagesizes(ps, true);
1988 
1989 	for (int i = 1; i < pscnt; i++) {
1990 		uint64_t val;
1991 
1992 		fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1993 		addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1994 		    0);
1995 		ATF_REQUIRE_MSG(addr != MAP_FAILED,
1996 		    "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1997 
1998 		/*
1999 		 * Ensure that the page is faulted into the pmap.
2000 		 */
2001 		memset(addr, 0, ps[i]);
2002 
2003 		set_keys();
2004 
2005 		/*
2006 		 * Make sure we can't partially cover a largepage mapping.
2007 		 */
2008 		error = x86_pkru_protect_range(addr, PAGE_SIZE, KEY_RW, 0);
2009 		ATF_REQUIRE_ERRNO(EINVAL, error != 0);
2010 		error = x86_pkru_protect_range(addr, ps[i] - PAGE_SIZE, KEY_RW,
2011 		    0);
2012 		ATF_REQUIRE_ERRNO(EINVAL, error != 0);
2013 		error = x86_pkru_protect_range(addr + PAGE_SIZE, ps[i] - PAGE_SIZE,
2014 		    KEY_RW, 0);
2015 		ATF_REQUIRE_ERRNO(EINVAL, error != 0);
2016 		error = x86_pkru_protect_range(addr + 1, ps[i], KEY_RW, 0);
2017 		ATF_REQUIRE_ERRNO(EINVAL, error != 0);
2018 
2019 		/*
2020 		 * Make sure that protections are honoured.
2021 		 */
2022 		for (int j = 1; j <= 4; j++) {
2023 			volatile uint64_t *addr64;
2024 
2025 			error = x86_pkru_protect_range(addr, ps[i], 0, 0);
2026 			ATF_REQUIRE(error == 0);
2027 
2028 			addr64 = (volatile uint64_t *)(void *)addr;
2029 			*addr64 = VAL;
2030 
2031 			error = x86_pkru_protect_range(addr, ps[i], j, 0);
2032 			ATF_REQUIRE(error == 0);
2033 			switch (j) {
2034 			case KEY_RW:
2035 				ATF_REQUIRE(try_write(addr64, VAL));
2036 				ATF_REQUIRE(try_read(addr64, &val));
2037 				ATF_REQUIRE(val == VAL);
2038 				break;
2039 			case KEY_RO:
2040 				ATF_REQUIRE(try_read(addr64, &val));
2041 				ATF_REQUIRE(val == VAL);
2042 				ATF_REQUIRE(!try_write(addr64, VAL));
2043 				break;
2044 			case KEY_WO:
2045 				/* !access implies !modify */
2046 			case KEY_NO:
2047 				ATF_REQUIRE(!try_read(addr64, &val));
2048 				ATF_REQUIRE(!try_write(addr64, VAL));
2049 				break;
2050 			default:
2051 				__unreachable();
2052 			}
2053 		}
2054 		error = munmap(addr, ps[i]);
2055 		ATF_CHECK(error == 0);
2056 
2057 		/*
2058 		 * Try mapping a large page in a region partially covered by a
2059 		 * key.
2060 		 *
2061 		 * Rather than detecting the mismatch when the logical mapping
2062 		 * is created, we currently only fail once pmap_enter() is
2063 		 * called from the fault handler.  This is not ideal and might
2064 		 * be improved in the future.
2065 		 */
2066 		error = x86_pkru_protect_range(addr, ps[i], 0, 0);
2067 		ATF_REQUIRE(error == 0);
2068 		error = x86_pkru_protect_range(addr + PAGE_SIZE,
2069 		    ps[i] - PAGE_SIZE, KEY_RW, 0);
2070 		ATF_REQUIRE(error == 0);
2071 
2072 		addr1 = mmap(addr, ps[i], PROT_READ | PROT_WRITE,
2073 		    MAP_SHARED | MAP_FIXED, fd, 0);
2074 		ATF_REQUIRE(addr1 != MAP_FAILED);
2075 		ATF_REQUIRE(addr == addr1);
2076 		ATF_REQUIRE(!try_read((volatile uint64_t *)(void *)addr, &val));
2077 		ATF_REQUIRE(!try_write((volatile uint64_t *)(void *)addr, VAL));
2078 	}
2079 }
2080 #undef KEY_RW
2081 #undef KEY_RO
2082 #undef KEY_WO
2083 #undef KEY_NO
2084 #endif
2085 
2086 ATF_TC_WITHOUT_HEAD(largepage_reopen);
ATF_TC_BODY(largepage_reopen,tc)2087 ATF_TC_BODY(largepage_reopen, tc)
2088 {
2089 	char *addr, *vec;
2090 	size_t ps[MAXPAGESIZES];
2091 	int fd, psind;
2092 
2093 	(void)pagesizes(ps, true);
2094 	psind = 1;
2095 
2096 	gen_test_path();
2097 	fd = shm_create_largepage(test_path, O_CREAT | O_RDWR, psind,
2098 	    SHM_LARGEPAGE_ALLOC_DEFAULT, 0600);
2099 	if (fd < 0 && errno == ENOTTY)
2100 		atf_tc_skip("no large page support");
2101 	ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; error=%d", errno);
2102 
2103 	ATF_REQUIRE_MSG(ftruncate(fd, ps[psind]) == 0,
2104 	    "ftruncate failed; error=%d", errno);
2105 
2106 	ATF_REQUIRE_MSG(close(fd) == 0, "close failed; error=%d", errno);
2107 
2108 	fd = shm_open(test_path, O_RDWR, 0);
2109 	ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
2110 
2111 	addr = mmap(NULL, ps[psind], PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2112 	ATF_REQUIRE_MSG(addr != MAP_FAILED, "mmap failed; error=%d", errno);
2113 
2114 	/* Trigger a fault and mapping creation. */
2115 	*(volatile char *)addr = 0;
2116 
2117 	vec = malloc(ps[psind] / ps[0]);
2118 	ATF_REQUIRE(vec != NULL);
2119 	ATF_REQUIRE_MSG(mincore(addr, ps[psind], vec) == 0,
2120 	    "mincore failed; error=%d", errno);
2121 	ATF_REQUIRE_MSG((vec[0] & MINCORE_SUPER) == MINCORE_PSIND(psind),
2122 	    "page not mapped into a %zu-byte superpage", ps[psind]);
2123 
2124 	ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
2125 	    "shm_unlink failed; errno=%d", errno);
2126 	ATF_REQUIRE_MSG(close(fd) == 0,
2127 	    "close failed; errno=%d", errno);
2128 }
2129 
ATF_TP_ADD_TCS(tp)2130 ATF_TP_ADD_TCS(tp)
2131 {
2132 	ATF_TP_ADD_TC(tp, remap_object);
2133 	ATF_TP_ADD_TC(tp, rename_from_anon);
2134 	ATF_TP_ADD_TC(tp, rename_bad_path_pointer);
2135 	ATF_TP_ADD_TC(tp, rename_from_nonexisting);
2136 	ATF_TP_ADD_TC(tp, rename_to_anon);
2137 	ATF_TP_ADD_TC(tp, rename_to_replace);
2138 	ATF_TP_ADD_TC(tp, rename_to_noreplace);
2139 	ATF_TP_ADD_TC(tp, rename_to_exchange);
2140 	ATF_TP_ADD_TC(tp, rename_to_exchange_nonexisting);
2141 	ATF_TP_ADD_TC(tp, rename_to_self);
2142 	ATF_TP_ADD_TC(tp, rename_bad_flag);
2143 	ATF_TP_ADD_TC(tp, reopen_object);
2144 	ATF_TP_ADD_TC(tp, readonly_mmap_write);
2145 	ATF_TP_ADD_TC(tp, open_after_link);
2146 	ATF_TP_ADD_TC(tp, open_invalid_path);
2147 	ATF_TP_ADD_TC(tp, open_write_only);
2148 	ATF_TP_ADD_TC(tp, open_extra_flags);
2149 	ATF_TP_ADD_TC(tp, open_anon);
2150 	ATF_TP_ADD_TC(tp, open_anon_readonly);
2151 	ATF_TP_ADD_TC(tp, open_bad_path_pointer);
2152 	ATF_TP_ADD_TC(tp, open_path_too_long);
2153 	ATF_TP_ADD_TC(tp, open_nonexisting_object);
2154 	ATF_TP_ADD_TC(tp, open_create_existing_object);
2155 	ATF_TP_ADD_TC(tp, shm_functionality_across_fork);
2156 	ATF_TP_ADD_TC(tp, trunc_resets_object);
2157 	ATF_TP_ADD_TC(tp, unlink_bad_path_pointer);
2158 	ATF_TP_ADD_TC(tp, unlink_path_too_long);
2159 	ATF_TP_ADD_TC(tp, object_resize);
2160 	ATF_TP_ADD_TC(tp, cloexec);
2161 	ATF_TP_ADD_TC(tp, mode);
2162 	ATF_TP_ADD_TC(tp, fallocate);
2163 	ATF_TP_ADD_TC(tp, fspacectl);
2164 	ATF_TP_ADD_TC(tp, accounting);
2165 	ATF_TP_ADD_TC(tp, mmap_prot);
2166 	ATF_TP_ADD_TC(tp, largepage_basic);
2167 	ATF_TP_ADD_TC(tp, largepage_config);
2168 	ATF_TP_ADD_TC(tp, largepage_mmap);
2169 	ATF_TP_ADD_TC(tp, largepage_munmap);
2170 	ATF_TP_ADD_TC(tp, largepage_madvise);
2171 	ATF_TP_ADD_TC(tp, largepage_mlock);
2172 	ATF_TP_ADD_TC(tp, largepage_msync);
2173 	ATF_TP_ADD_TC(tp, largepage_mprotect);
2174 	ATF_TP_ADD_TC(tp, largepage_minherit);
2175 	ATF_TP_ADD_TC(tp, largepage_pipe);
2176 #ifdef __amd64__
2177 	ATF_TP_ADD_TC(tp, largepage_pkru);
2178 #endif
2179 	ATF_TP_ADD_TC(tp, largepage_reopen);
2180 
2181 	return (atf_no_error());
2182 }
2183