2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
33 #include <sys/resource.h>
35 #include <sys/syscall.h>
36 #include <sys/sysctl.h>
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;
56 gen_a_test_path(char *path)
58 snprintf(path, TEST_PATH_LEN, "/%s/tmp.XXXXXX%d",
59 getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"),
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);
73 gen_a_test_path(test_path);
79 gen_a_test_path(test_path2);
83 * Attempt a shm_open() that should fail with an expected error of 'error'.
86 shm_open_should_fail(const char *path, int flags, mode_t mode, int error)
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);
98 * Attempt a shm_unlink() that should fail with an expected error of 'error'.
101 shm_unlink_should_fail(const char *path, int error)
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);
111 * Open the test object and write a value to the first byte. Returns valid fd
112 * on success and -1 on failure.
115 scribble_object(const char *path, char value)
120 ATF_REQUIRE(0 < (pagesize = getpagesize()));
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);
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);
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);
138 ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
145 * Fail the test case if the 'path' does not refer to an shm whose first byte
146 * is equal to expected_value
149 verify_object(const char *path, char expected_value)
155 ATF_REQUIRE(0 < (pagesize = getpagesize()));
157 fd = shm_open(path, O_RDONLY, 0777);
159 atf_tc_fail("shm_open failed in verify_object; errno=%d, path=%s",
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",
176 ATF_TC_WITHOUT_HEAD(remap_object);
177 ATF_TC_BODY(remap_object, tc)
182 ATF_REQUIRE(0 < (pagesize = getpagesize()));
185 fd = scribble_object(test_path, '1');
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);
192 atf_tc_fail("missing data ('%c' != '1')", page[0]);
195 ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
198 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
199 "shm_unlink failed; errno=%d", errno);
202 ATF_TC_WITHOUT_HEAD(rename_from_anon);
203 ATF_TC_BODY(rename_from_anon, tc)
208 rc = shm_rename(SHM_ANON, test_path, 0);
210 atf_tc_fail("shm_rename from SHM_ANON succeeded unexpectedly");
213 ATF_TC_WITHOUT_HEAD(rename_bad_path_pointer);
214 ATF_TC_BODY(rename_bad_path_pointer, tc)
216 const char *bad_path;
219 bad_path = (const char *)0x1;
222 rc = shm_rename(test_path, bad_path, 0);
224 atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
226 rc = shm_rename(bad_path, test_path, 0);
228 atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
231 ATF_TC_WITHOUT_HEAD(rename_from_nonexisting);
232 ATF_TC_BODY(rename_from_nonexisting, tc)
238 rc = shm_rename(test_path, test_path2, 0);
240 atf_tc_fail("shm_rename of nonexisting shm succeeded unexpectedly");
243 atf_tc_fail("Expected ENOENT to rename of nonexistent shm; got %d",
247 ATF_TC_WITHOUT_HEAD(rename_to_anon);
248 ATF_TC_BODY(rename_to_anon, tc)
253 rc = shm_rename(test_path, SHM_ANON, 0);
255 atf_tc_fail("shm_rename to SHM_ANON succeeded unexpectedly");
258 ATF_TC_WITHOUT_HEAD(rename_to_replace);
259 ATF_TC_BODY(rename_to_replace, tc)
265 // Some contents we can verify later
266 expected_value = 'g';
269 fd = scribble_object(test_path, expected_value);
272 // Give the other some different value so we can detect success
274 fd2 = scribble_object(test_path2, 'h');
277 ATF_REQUIRE_MSG(shm_rename(test_path, test_path2, 0) == 0,
278 "shm_rename failed; errno=%d", errno);
280 // Read back renamed; verify contents
281 verify_object(test_path2, expected_value);
284 ATF_TC_WITHOUT_HEAD(rename_to_noreplace);
285 ATF_TC_BODY(rename_to_noreplace, tc)
287 char expected_value_from;
288 char expected_value_to;
293 // Some contents we can verify later
294 expected_value_from = 'g';
296 fd_from = scribble_object(test_path, expected_value_from);
299 // Give the other some different value so we can detect success
300 expected_value_to = 'h';
302 fd_to = scribble_object(test_path2, expected_value_to);
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,
310 // Read back renamed; verify contents
311 verify_object(test_path2, expected_value_to);
314 ATF_TC_WITHOUT_HEAD(rename_to_exchange);
315 ATF_TC_BODY(rename_to_exchange, tc)
317 char expected_value_from;
318 char expected_value_to;
322 // Some contents we can verify later
323 expected_value_from = 'g';
325 fd_from = scribble_object(test_path, expected_value_from);
328 // Give the other some different value so we can detect success
329 expected_value_to = 'h';
331 fd_to = scribble_object(test_path2, expected_value_to);
334 ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
335 SHM_RENAME_EXCHANGE) == 0,
336 "shm_rename failed; errno=%d", errno);
338 // Read back renamed; verify contents
339 verify_object(test_path, expected_value_to);
340 verify_object(test_path2, expected_value_from);
343 ATF_TC_WITHOUT_HEAD(rename_to_exchange_nonexisting);
344 ATF_TC_BODY(rename_to_exchange_nonexisting, tc)
346 char expected_value_from;
349 // Some contents we can verify later
350 expected_value_from = 'g';
352 fd_from = scribble_object(test_path, expected_value_from);
357 ATF_REQUIRE_MSG(shm_rename(test_path, test_path2,
358 SHM_RENAME_EXCHANGE) == 0,
359 "shm_rename failed; errno=%d", errno);
361 // Read back renamed; verify contents
362 verify_object(test_path2, expected_value_from);
365 ATF_TC_WITHOUT_HEAD(rename_to_self);
366 ATF_TC_BODY(rename_to_self, tc)
371 expected_value = 't';
374 fd = scribble_object(test_path, expected_value);
377 ATF_REQUIRE_MSG(shm_rename(test_path, test_path, 0) == 0,
378 "shm_rename failed; errno=%d", errno);
380 verify_object(test_path, expected_value);
383 ATF_TC_WITHOUT_HEAD(rename_bad_flag);
384 ATF_TC_BODY(rename_bad_flag, tc)
389 /* Make sure we don't fail out due to ENOENT */
392 fd = scribble_object(test_path, 'd');
394 fd = scribble_object(test_path2, 'd');
398 * Note: if we end up with enough flags that we use all the bits,
399 * then remove this test completely.
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);
407 ATF_TC_WITHOUT_HEAD(reopen_object);
408 ATF_TC_BODY(reopen_object, tc)
413 ATF_REQUIRE(0 < (pagesize = getpagesize()));
416 fd = scribble_object(test_path, '1');
419 fd = shm_open(test_path, O_RDONLY, 0777);
421 atf_tc_fail("shm_open(2) failed; errno=%d", errno);
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);
428 atf_tc_fail("missing data ('%c' != '1')", page[0]);
430 ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
433 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
434 "shm_unlink failed; errno=%d", errno);
437 ATF_TC_WITHOUT_HEAD(readonly_mmap_write);
438 ATF_TC_BODY(readonly_mmap_write, tc)
443 ATF_REQUIRE(0 < (pagesize = getpagesize()));
447 fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
448 ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
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");
456 atf_tc_fail("mmap(PROT_WRITE) didn't fail with EACCES; "
460 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
461 "shm_unlink failed; errno=%d", errno);
464 ATF_TC_WITHOUT_HEAD(open_after_link);
465 ATF_TC_BODY(open_after_link, tc)
471 fd = shm_open(test_path, O_RDONLY | O_CREAT, 0777);
472 ATF_REQUIRE_MSG(fd >= 0, "shm_open(1) failed; errno=%d", errno);
475 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1, "shm_unlink failed: %d",
478 shm_open_should_fail(test_path, O_RDONLY, 0777, ENOENT);
481 ATF_TC_WITHOUT_HEAD(open_invalid_path);
482 ATF_TC_BODY(open_invalid_path, tc)
485 shm_open_should_fail("blah", O_RDONLY, 0777, EINVAL);
488 ATF_TC_WITHOUT_HEAD(open_write_only);
489 ATF_TC_BODY(open_write_only, tc)
494 shm_open_should_fail(test_path, O_WRONLY, 0777, EINVAL);
497 ATF_TC_WITHOUT_HEAD(open_extra_flags);
498 ATF_TC_BODY(open_extra_flags, tc)
503 shm_open_should_fail(test_path, O_RDONLY | O_DIRECT, 0777, EINVAL);
506 ATF_TC_WITHOUT_HEAD(open_anon);
507 ATF_TC_BODY(open_anon, tc)
511 fd = shm_open(SHM_ANON, O_RDWR, 0777);
512 ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
516 ATF_TC_WITHOUT_HEAD(open_anon_readonly);
517 ATF_TC_BODY(open_anon_readonly, tc)
520 shm_open_should_fail(SHM_ANON, O_RDONLY, 0777, EINVAL);
523 ATF_TC_WITHOUT_HEAD(open_bad_path_pointer);
524 ATF_TC_BODY(open_bad_path_pointer, tc)
527 shm_open_should_fail((char *)1024, O_RDONLY, 0777, EFAULT);
530 ATF_TC_WITHOUT_HEAD(open_path_too_long);
531 ATF_TC_BODY(open_path_too_long, tc)
535 page = malloc(MAXPATHLEN + 1);
536 memset(page, 'a', MAXPATHLEN);
537 page[MAXPATHLEN] = '\0';
538 shm_open_should_fail(page, O_RDONLY, 0777, ENAMETOOLONG);
542 ATF_TC_WITHOUT_HEAD(open_nonexisting_object);
543 ATF_TC_BODY(open_nonexisting_object, tc)
546 shm_open_should_fail("/notreallythere", O_RDONLY, 0777, ENOENT);
549 ATF_TC_WITHOUT_HEAD(open_create_existing_object);
550 ATF_TC_BODY(open_create_existing_object, tc)
556 fd = shm_open(test_path, O_RDONLY|O_CREAT, 0777);
557 ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; errno=%d", errno);
560 shm_open_should_fail(test_path, O_RDONLY|O_CREAT|O_EXCL,
563 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
564 "shm_unlink failed; errno=%d", errno);
567 ATF_TC_WITHOUT_HEAD(trunc_resets_object);
568 ATF_TC_BODY(trunc_resets_object, tc)
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);
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);
593 ATF_REQUIRE_MSG(shm_unlink(test_path) != -1,
594 "shm_unlink failed; errno=%d", errno);
597 ATF_TC_WITHOUT_HEAD(unlink_bad_path_pointer);
598 ATF_TC_BODY(unlink_bad_path_pointer, tc)
601 shm_unlink_should_fail((char *)1024, EFAULT);
604 ATF_TC_WITHOUT_HEAD(unlink_path_too_long);
605 ATF_TC_BODY(unlink_path_too_long, tc)
609 page = malloc(MAXPATHLEN + 1);
610 memset(page, 'a', MAXPATHLEN);
611 page[MAXPATHLEN] = '\0';
612 shm_unlink_should_fail(page, ENAMETOOLONG);
616 ATF_TC_WITHOUT_HEAD(object_resize);
617 ATF_TC_BODY(object_resize, tc)
622 int fd, pagesize, status;
624 ATF_REQUIRE(0 < (pagesize = getpagesize()));
626 /* Start off with a size of a single page. */
627 fd = shm_open(SHM_ANON, O_CREAT|O_RDWR, 0777);
629 atf_tc_fail("shm_open failed; errno=%d", errno);
631 if (ftruncate(fd, pagesize) < 0)
632 atf_tc_fail("ftruncate(1) failed; errno=%d", errno);
634 if (fstat(fd, &sb) < 0)
635 atf_tc_fail("fstat(1) failed; errno=%d", errno);
637 if (sb.st_size != pagesize)
638 atf_tc_fail("first resize failed (%d != %d)",
639 (int)sb.st_size, pagesize);
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)");
648 ATF_REQUIRE_MSG(munmap(page, pagesize) == 0, "munmap failed; errno=%d",
651 /* Grow the object to 2 pages. */
652 if (ftruncate(fd, pagesize * 2) < 0)
653 atf_tc_fail("ftruncate(2) failed; errno=%d", errno);
655 if (fstat(fd, &sb) < 0)
656 atf_tc_fail("fstat(2) failed; errno=%d", errno);
658 if (sb.st_size != pagesize * 2)
659 atf_tc_fail("second resize failed (%d != %d)",
660 (int)sb.st_size, pagesize * 2);
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);
668 atf_tc_fail("'%c' != '1'", page[0]);
670 /* Write a '2' at the start of the second page. */
671 page[pagesize] = '2';
673 /* Shrink the object back to 1 page. */
674 if (ftruncate(fd, pagesize) < 0)
675 atf_tc_fail("ftruncate(3) failed; errno=%d", errno);
677 if (fstat(fd, &sb) < 0)
678 atf_tc_fail("fstat(3) failed; errno=%d", errno);
680 if (sb.st_size != pagesize)
681 atf_tc_fail("third resize failed (%d != %d)",
682 (int)sb.st_size, pagesize);
685 * Fork a child process to make sure the second page is no
690 atf_tc_fail("fork failed; errno=%d", errno);
696 /* Don't generate a core dump. */
697 ATF_REQUIRE(getrlimit(RLIMIT_CORE, &lim) == 0);
699 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &lim) == 0);
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.
707 fprintf(stderr, "child: page 1: '%c'\n", c);
711 if (wait(&status) < 0)
712 atf_tc_fail("wait failed; errno=%d", errno);
714 if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGBUS)
715 atf_tc_fail("child terminated with status %x", status);
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);
721 if (fstat(fd, &sb) < 0)
722 atf_tc_fail("fstat(2) failed; errno=%d", errno);
724 if (sb.st_size != pagesize * 2)
725 atf_tc_fail("fourth resize failed (%d != %d)",
726 (int)sb.st_size, pagesize);
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.
737 if (page[pagesize] != 0)
738 atf_tc_fail("invalid data at %d: %x != 0",
739 pagesize, (int)page[pagesize]);
744 /* Signal handler which does nothing. */
746 ignoreit(int sig __unused)
751 ATF_TC_WITHOUT_HEAD(shm_functionality_across_fork);
752 ATF_TC_BODY(shm_functionality_across_fork, tc)
762 #ifndef _POSIX_SHARED_MEMORY_OBJECTS
763 printf("_POSIX_SHARED_MEMORY_OBJECTS is undefined\n");
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");
771 scval = sysconf(_SC_SHARED_MEMORY_OBJECTS);
772 if (scval == -1 && errno != 0) {
773 atf_tc_fail("sysconf(_SC_SHARED_MEMORY_OBJECTS) failed; "
776 printf("sysconf(_SC_SHARED_MEMORY_OBJECTS) returns %ld\n",
779 printf("***Indicates this feature is unsupported!\n");
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",
791 printf("sysconf(_SC_PAGESIZE) returns %ld\n", scval);
796 desc = shm_open(test_path, O_EXCL | O_CREAT | O_RDWR, 0600);
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);
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);
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);
815 sigaddset(&ss, SIGUSR1);
816 ATF_REQUIRE_MSG(sigprocmask(SIG_BLOCK, &ss, (sigset_t *)0) == 0,
817 "sigprocmask failed; errno=%d", errno);
820 ATF_REQUIRE_MSG(rv != -1, "fork failed; errno=%d", errno);
825 for (cp = region; cp < (char *)region + psize; cp++) {
829 if (lseek(desc, 0, SEEK_SET) == -1)
831 for (i = 0; i < psize; i++) {
832 error = read(desc, &c, 1);
840 memset(region, '\151', psize - 2);
841 error = pwrite(desc, region, 2, psize - 2);
844 atf_tc_fail("short write; %d bytes written",
847 atf_tc_fail("shmfd write");
850 waitpid(rv, &status, 0);
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));
858 atf_tc_fail("Child process terminated with %s",
859 strsignal(WTERMSIG(status)));
863 ATF_REQUIRE_MSG(munmap(region, psize) == 0, "munmap failed; errno=%d",
865 shm_unlink(test_path);
868 ATF_TC_WITHOUT_HEAD(cloexec);
869 ATF_TC_BODY(cloexec, tc)
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);
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);
888 ATF_TC_WITHOUT_HEAD(mode);
889 ATF_TC_BODY(mode, tc)
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);
904 ATF_REQUIRE(shm_unlink(test_path) == 0);
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);
911 ATF_REQUIRE(shm_unlink(test_path) == 0);
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);
918 ATF_REQUIRE(shm_unlink(test_path) == 0);
923 ATF_TC_WITHOUT_HEAD(fallocate);
924 ATF_TC_BODY(fallocate, tc)
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.
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. */
938 ATF_REQUIRE(ftruncate(fd, sz) == 0);
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));
962 shm_open_large(int psind, int policy, size_t sz)
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);
971 error = ftruncate(fd, sz);
972 if (error != 0 && errno == ENOMEM)
974 * The test system might not have enough memory to accommodate
977 atf_tc_skip("failed to allocate %zu-byte superpage", sz);
978 ATF_REQUIRE_MSG(error == 0, "ftruncate failed; errno=%d", errno);
984 pagesizes(size_t ps[MAXPAGESIZES])
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);
993 atf_tc_skip("no large page support");
997 ATF_TC_WITHOUT_HEAD(largepage_basic);
998 ATF_TC_BODY(largepage_basic, tc)
1000 char zeroes[PAGE_SIZE];
1002 size_t ps[MAXPAGESIZES];
1003 int error, fd, pscnt;
1005 memset(zeroes, 0, PAGE_SIZE);
1007 pscnt = pagesizes(ps);
1008 for (int i = 1; i < pscnt; i++) {
1009 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1011 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
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",
1019 /* Force a page fault. */
1020 *(volatile char *)addr = 0;
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);
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",
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);
1043 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1044 ATF_REQUIRE(close(fd) == 0);
1048 extern int __sys_shm_open2(const char *, int, mode_t, int, const char *);
1050 ATF_TC_WITHOUT_HEAD(largepage_config);
1051 ATF_TC_BODY(largepage_config, tc)
1053 struct shm_largepage_conf lpc;
1055 size_t ps[MAXPAGESIZES + 1]; /* silence warnings if MAXPAGESIZES == 1 */
1056 int error, fd, pscnt;
1058 pscnt = pagesizes(ps);
1060 fd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0);
1061 ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
1064 * Configure a large page policy for an object created without
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",
1073 ATF_REQUIRE(close(fd) == 0);
1076 * Create a largepage object and try to use it without actually
1077 * configuring anything.
1079 fd = __sys_shm_open2(SHM_ANON, O_CREAT | O_RDWR, 0, SHM_LARGEPAGE,
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);
1085 error = ftruncate(fd, ps[1]);
1086 ATF_REQUIRE(error != 0);
1087 ATF_REQUIRE_MSG(errno == EINVAL, "ftruncate returned %d", errno);
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);
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);
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);
1107 error = posix_fallocate(fd, 0, PAGE_SIZE);
1108 ATF_REQUIRE_MSG(error == EINVAL, "posix_fallocate returned %d", error);
1110 ATF_REQUIRE(close(fd) == 0);
1113 ATF_TC_WITHOUT_HEAD(largepage_mmap);
1114 ATF_TC_BODY(largepage_mmap, tc)
1116 char *addr, *addr1, *vec;
1117 size_t ps[MAXPAGESIZES];
1120 pscnt = pagesizes(ps);
1121 for (int i = 1; i < pscnt; i++) {
1122 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1124 /* For mincore(). */
1125 vec = malloc(ps[i]);
1126 ATF_REQUIRE(vec != NULL);
1129 * Wrong mapping size.
1131 addr = mmap(NULL, ps[i - 1], PROT_READ | PROT_WRITE, MAP_SHARED,
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);
1141 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
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",
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);
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);
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);
1172 ATF_REQUIRE(munmap(addr, ps[i] == 0));
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,
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);
1197 * Copy-on-write mappings are not permitted.
1199 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_PRIVATE,
1201 ATF_REQUIRE_MSG(addr == MAP_FAILED,
1202 "mmap(%zu bytes) succeeded", ps[i]);
1204 ATF_REQUIRE(close(fd) == 0);
1208 ATF_TC_WITHOUT_HEAD(largepage_munmap);
1209 ATF_TC_BODY(largepage_munmap, tc)
1212 size_t ps[MAXPAGESIZES], ps1;
1215 pscnt = pagesizes(ps);
1216 for (int i = 1; i < pscnt; i++) {
1217 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1220 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1222 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1223 "mmap(%zu bytes) failed; errno=%d", ps[i], errno);
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);
1239 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1240 ATF_REQUIRE(close(fd) == 0);
1245 largepage_madvise(char *addr, size_t sz, int advice, int error)
1248 ATF_REQUIRE_MSG(madvise(addr, sz, advice) == 0,
1249 "madvise(%zu, %d) failed; error=%d", sz, advice, errno);
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)",
1259 ATF_TC_WITHOUT_HEAD(largepage_madvise);
1260 ATF_TC_BODY(largepage_madvise, tc)
1263 size_t ps[MAXPAGESIZES];
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,
1271 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1272 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
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);
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);
1298 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1299 ATF_REQUIRE(close(fd) == 0);
1303 ATF_TC(largepage_mlock);
1304 ATF_TC_HEAD(largepage_mlock, tc)
1306 /* Needed to set rlimit. */
1307 atf_tc_set_md_var(tc, "require.user", "root");
1309 ATF_TC_BODY(largepage_mlock, tc)
1313 size_t ps[MAXPAGESIZES], sz;
1314 u_long max_wired, wired;
1315 int fd, error, pscnt;
1317 rl.rlim_cur = rl.rlim_max = RLIM_INFINITY;
1318 ATF_REQUIRE_MSG(setrlimit(RLIMIT_MEMLOCK, &rl) == 0,
1319 "setrlimit failed; error=%d", errno);
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 ATF_REQUIRE(max_wired >= 0);
1328 error = sysctlbyname("vm.stats.vm.v_user_wire_count", &wired, &sz, NULL,
1330 ATF_REQUIRE_MSG(error == 0,
1331 "sysctlbyname(vm.stats.vm.v_user_wire_count) failed; error=%d",
1333 ATF_REQUIRE(wired >= 0);
1335 pscnt = pagesizes(ps);
1336 for (int i = 1; i < pscnt; i++) {
1337 if (ps[i] / PAGE_SIZE > max_wired - wired) {
1338 /* Cannot wire past the limit. */
1339 atf_tc_skip("test would exceed wiring limit");
1342 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1343 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1345 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1346 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1348 ATF_REQUIRE(mlock(addr, PAGE_SIZE) != 0);
1349 ATF_REQUIRE_MSG(errno == EINVAL,
1350 "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1351 ATF_REQUIRE(mlock(addr, ps[i] - PAGE_SIZE) != 0);
1352 ATF_REQUIRE_MSG(errno == EINVAL,
1353 "unexpected error %d from mlock(%zu bytes)", errno, ps[i]);
1355 ATF_REQUIRE_MSG(mlock(addr, ps[i]) == 0,
1356 "mlock failed; error=%d", errno);
1358 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1360 ATF_REQUIRE(mlockall(MCL_FUTURE) == 0);
1361 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1363 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1364 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1366 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1367 ATF_REQUIRE(close(fd) == 0);
1371 ATF_TC_WITHOUT_HEAD(largepage_msync);
1372 ATF_TC_BODY(largepage_msync, tc)
1375 size_t ps[MAXPAGESIZES];
1378 pscnt = pagesizes(ps);
1379 for (int i = 1; i < pscnt; i++) {
1380 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1381 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1383 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1384 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1386 memset(addr, 0, ps[i]);
1389 * "Sync" requests are no-ops for SHM objects, so small
1390 * PAGE_SIZE-sized requests succeed.
1392 ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_ASYNC) == 0,
1393 "msync(MS_ASYNC) failed; error=%d", errno);
1394 ATF_REQUIRE_MSG(msync(addr, ps[i], MS_ASYNC) == 0,
1395 "msync(MS_ASYNC) failed; error=%d", errno);
1396 ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_SYNC) == 0,
1397 "msync(MS_SYNC) failed; error=%d", errno);
1398 ATF_REQUIRE_MSG(msync(addr, ps[i], MS_SYNC) == 0,
1399 "msync(MS_SYNC) failed; error=%d", errno);
1401 ATF_REQUIRE_MSG(msync(addr, PAGE_SIZE, MS_INVALIDATE) != 0,
1402 "msync(MS_INVALIDATE) succeeded");
1403 /* XXX wrong errno */
1404 ATF_REQUIRE_MSG(errno == EBUSY,
1405 "unexpected error %d from msync(MS_INVALIDATE)", errno);
1406 ATF_REQUIRE_MSG(msync(addr, ps[i], MS_INVALIDATE) == 0,
1407 "msync(MS_INVALIDATE) failed; error=%d", errno);
1408 memset(addr, 0, ps[i]);
1410 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1411 ATF_REQUIRE(close(fd) == 0);
1416 largepage_protect(char *addr, size_t sz, int prot, int error)
1419 ATF_REQUIRE_MSG(mprotect(addr, sz, prot) == 0,
1420 "mprotect(%zu, %x) failed; error=%d", sz, prot, errno);
1422 ATF_REQUIRE_MSG(mprotect(addr, sz, prot) != 0,
1423 "mprotect(%zu, %x) succeeded", sz, prot);
1424 ATF_REQUIRE_MSG(errno == error,
1425 "unexpected error %d from mprotect(%zu, %x)",
1430 ATF_TC_WITHOUT_HEAD(largepage_mprotect);
1431 ATF_TC_BODY(largepage_mprotect, tc)
1434 size_t ps[MAXPAGESIZES];
1437 pscnt = pagesizes(ps);
1438 for (int i = 1; i < pscnt; i++) {
1439 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1440 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1442 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1443 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1446 * These should be no-ops from the pmap perspective since the
1447 * page is not yet entered into the pmap.
1449 largepage_protect(addr, PAGE_SIZE, PROT_READ, EINVAL);
1450 largepage_protect(addr, ps[i], PROT_READ, 0);
1451 largepage_protect(addr, PAGE_SIZE, PROT_NONE, EINVAL);
1452 largepage_protect(addr, ps[i], PROT_NONE, 0);
1453 largepage_protect(addr, PAGE_SIZE,
1454 PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1455 largepage_protect(addr, ps[i],
1456 PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1458 /* Trigger creation of a mapping and try again. */
1459 *(volatile char *)addr = 0;
1460 largepage_protect(addr, PAGE_SIZE, PROT_READ, EINVAL);
1461 largepage_protect(addr, ps[i], PROT_READ, 0);
1462 largepage_protect(addr, PAGE_SIZE, PROT_NONE, EINVAL);
1463 largepage_protect(addr, ps[i], PROT_NONE, 0);
1464 largepage_protect(addr, PAGE_SIZE,
1465 PROT_READ | PROT_WRITE | PROT_EXEC, EINVAL);
1466 largepage_protect(addr, ps[i],
1467 PROT_READ | PROT_WRITE | PROT_EXEC, 0);
1469 memset(addr, 0, ps[i]);
1471 /* Map two contiguous large pages and merge map entries. */
1472 addr1 = mmap(addr + ps[i], ps[i], PROT_READ | PROT_WRITE,
1473 MAP_SHARED | MAP_FIXED | MAP_EXCL, fd, 0);
1474 ATF_REQUIRE_MSG(addr1 != MAP_FAILED,
1475 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1477 largepage_protect(addr1 - PAGE_SIZE, PAGE_SIZE * 2,
1478 PROT_READ | PROT_WRITE, EINVAL);
1479 largepage_protect(addr, ps[i] * 2, PROT_READ | PROT_WRITE, 0);
1481 memset(addr, 0, ps[i] * 2);
1483 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1484 ATF_REQUIRE(munmap(addr1, ps[i]) == 0);
1485 ATF_REQUIRE(close(fd) == 0);
1489 ATF_TC_WITHOUT_HEAD(largepage_minherit);
1490 ATF_TC_BODY(largepage_minherit, tc)
1493 size_t ps[MAXPAGESIZES];
1495 int fd, pscnt, status;
1497 pscnt = pagesizes(ps);
1498 for (int i = 1; i < pscnt; i++) {
1499 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1500 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1502 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1503 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1505 ATF_REQUIRE(minherit(addr, PAGE_SIZE, INHERIT_SHARE) != 0);
1507 ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_SHARE) == 0,
1508 "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1510 ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1514 *(volatile char *)addr = 0;
1515 if (mincore(addr, PAGE_SIZE, &v) != 0)
1517 if ((v & MINCORE_PSIND(i)) == 0)
1521 ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1522 "waitpid failed; error=%d", errno);
1523 ATF_REQUIRE_MSG(WIFEXITED(status),
1524 "child was killed by signal %d", WTERMSIG(status));
1525 ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1526 "child exited with status %d", WEXITSTATUS(status));
1528 ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_NONE) == 0,
1529 "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1531 ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1535 if (mincore(addr, PAGE_SIZE, &v) == 0)
1539 ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1540 "waitpid failed; error=%d", errno);
1541 ATF_REQUIRE_MSG(WIFEXITED(status),
1542 "child was killed by signal %d", WTERMSIG(status));
1543 ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1544 "child exited with status %d", WEXITSTATUS(status));
1546 /* Copy-on-write is not supported for static large pages. */
1547 ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_COPY) != 0,
1548 "minherit(%zu bytes) succeeded", ps[i]);
1550 ATF_REQUIRE_MSG(minherit(addr, ps[i], INHERIT_ZERO) == 0,
1551 "minherit(%zu bytes) failed; error=%d", ps[i], errno);
1553 ATF_REQUIRE_MSG(child != -1, "fork failed; error=%d", errno);
1557 *(volatile char *)addr = 0;
1558 if (mincore(addr, PAGE_SIZE, &v) != 0)
1560 if ((v & MINCORE_SUPER) != 0)
1564 ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1565 "waitpid failed; error=%d", errno);
1566 ATF_REQUIRE_MSG(WIFEXITED(status),
1567 "child was killed by signal %d", WTERMSIG(status));
1568 ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1569 "child exited with status %d", WEXITSTATUS(status));
1571 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1572 ATF_REQUIRE(close(fd) == 0);
1576 ATF_TC_WITHOUT_HEAD(largepage_pipe);
1577 ATF_TC_BODY(largepage_pipe, tc)
1579 size_t ps[MAXPAGESIZES];
1582 int fd, pfd[2], pscnt, status;
1585 pscnt = pagesizes(ps);
1587 for (int i = 1; i < pscnt; i++) {
1588 fd = shm_open_large(i, SHM_LARGEPAGE_ALLOC_DEFAULT, ps[i]);
1589 addr = mmap(NULL, ps[i], PROT_READ | PROT_WRITE, MAP_SHARED, fd,
1591 ATF_REQUIRE_MSG(addr != MAP_FAILED,
1592 "mmap(%zu bytes) failed; error=%d", ps[i], errno);
1594 /* Trigger creation of a mapping. */
1595 *(volatile char *)addr = '\0';
1597 ATF_REQUIRE(pipe(pfd) == 0);
1599 ATF_REQUIRE_MSG(child != -1, "fork() failed; error=%d", errno);
1604 (void)close(pfd[0]);
1605 for (resid = (size_t)ps[i]; resid > 0; resid -= len) {
1606 len = read(pfd[1], buf, sizeof(buf));
1612 ATF_REQUIRE(close(pfd[1]) == 0);
1613 len = write(pfd[0], addr, ps[i]);
1614 ATF_REQUIRE_MSG(len >= 0, "write() failed; error=%d", errno);
1615 ATF_REQUIRE_MSG(len == (ssize_t)ps[i],
1616 "short write; len=%zd", len);
1617 ATF_REQUIRE(close(pfd[0]) == 0);
1619 ATF_REQUIRE_MSG(waitpid(child, &status, 0) == child,
1620 "waitpid() failed; error=%d", errno);
1621 ATF_REQUIRE_MSG(WIFEXITED(status),
1622 "child was killed by signal %d", WTERMSIG(status));
1623 ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
1624 "child exited with status %d", WEXITSTATUS(status));
1626 ATF_REQUIRE(munmap(addr, ps[i]) == 0);
1627 ATF_REQUIRE(close(fd) == 0);
1631 ATF_TC_WITHOUT_HEAD(largepage_reopen);
1632 ATF_TC_BODY(largepage_reopen, tc)
1635 size_t ps[MAXPAGESIZES];
1638 (void)pagesizes(ps);
1642 fd = shm_create_largepage(test_path, O_CREAT | O_RDWR, psind,
1643 SHM_LARGEPAGE_ALLOC_DEFAULT, 0600);
1644 if (fd < 0 && errno == ENOTTY)
1645 atf_tc_skip("no large page support");
1646 ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; error=%d", errno);
1648 ATF_REQUIRE_MSG(ftruncate(fd, ps[psind]) == 0,
1649 "ftruncate failed; error=%d", errno);
1651 ATF_REQUIRE_MSG(close(fd) == 0, "close failed; error=%d", errno);
1653 fd = shm_open(test_path, O_RDWR, 0);
1654 ATF_REQUIRE_MSG(fd >= 0, "shm_open failed; error=%d", errno);
1656 addr = mmap(NULL, ps[psind], PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1657 ATF_REQUIRE_MSG(addr != MAP_FAILED, "mmap failed; error=%d", errno);
1659 /* Trigger a fault and mapping creation. */
1660 *(volatile char *)addr = 0;
1662 vec = malloc(ps[psind] / PAGE_SIZE);
1663 ATF_REQUIRE(vec != NULL);
1664 ATF_REQUIRE_MSG(mincore(addr, ps[psind], vec) == 0,
1665 "mincore failed; error=%d", errno);
1666 ATF_REQUIRE_MSG((vec[0] & MINCORE_PSIND(psind)) != 0,
1667 "page not mapped into a %zu-byte superpage", ps[psind]);
1669 ATF_REQUIRE_MSG(shm_unlink(test_path) == 0,
1670 "shm_unlink failed; errno=%d", errno);
1671 ATF_REQUIRE_MSG(close(fd) == 0,
1672 "close failed; errno=%d", errno);
1678 ATF_TP_ADD_TC(tp, remap_object);
1679 ATF_TP_ADD_TC(tp, rename_from_anon);
1680 ATF_TP_ADD_TC(tp, rename_bad_path_pointer);
1681 ATF_TP_ADD_TC(tp, rename_from_nonexisting);
1682 ATF_TP_ADD_TC(tp, rename_to_anon);
1683 ATF_TP_ADD_TC(tp, rename_to_replace);
1684 ATF_TP_ADD_TC(tp, rename_to_noreplace);
1685 ATF_TP_ADD_TC(tp, rename_to_exchange);
1686 ATF_TP_ADD_TC(tp, rename_to_exchange_nonexisting);
1687 ATF_TP_ADD_TC(tp, rename_to_self);
1688 ATF_TP_ADD_TC(tp, rename_bad_flag);
1689 ATF_TP_ADD_TC(tp, reopen_object);
1690 ATF_TP_ADD_TC(tp, readonly_mmap_write);
1691 ATF_TP_ADD_TC(tp, open_after_link);
1692 ATF_TP_ADD_TC(tp, open_invalid_path);
1693 ATF_TP_ADD_TC(tp, open_write_only);
1694 ATF_TP_ADD_TC(tp, open_extra_flags);
1695 ATF_TP_ADD_TC(tp, open_anon);
1696 ATF_TP_ADD_TC(tp, open_anon_readonly);
1697 ATF_TP_ADD_TC(tp, open_bad_path_pointer);
1698 ATF_TP_ADD_TC(tp, open_path_too_long);
1699 ATF_TP_ADD_TC(tp, open_nonexisting_object);
1700 ATF_TP_ADD_TC(tp, open_create_existing_object);
1701 ATF_TP_ADD_TC(tp, shm_functionality_across_fork);
1702 ATF_TP_ADD_TC(tp, trunc_resets_object);
1703 ATF_TP_ADD_TC(tp, unlink_bad_path_pointer);
1704 ATF_TP_ADD_TC(tp, unlink_path_too_long);
1705 ATF_TP_ADD_TC(tp, object_resize);
1706 ATF_TP_ADD_TC(tp, cloexec);
1707 ATF_TP_ADD_TC(tp, mode);
1708 ATF_TP_ADD_TC(tp, fallocate);
1709 ATF_TP_ADD_TC(tp, largepage_basic);
1710 ATF_TP_ADD_TC(tp, largepage_config);
1711 ATF_TP_ADD_TC(tp, largepage_mmap);
1712 ATF_TP_ADD_TC(tp, largepage_munmap);
1713 ATF_TP_ADD_TC(tp, largepage_madvise);
1714 ATF_TP_ADD_TC(tp, largepage_mlock);
1715 ATF_TP_ADD_TC(tp, largepage_msync);
1716 ATF_TP_ADD_TC(tp, largepage_mprotect);
1717 ATF_TP_ADD_TC(tp, largepage_minherit);
1718 ATF_TP_ADD_TC(tp, largepage_pipe);
1719 ATF_TP_ADD_TC(tp, largepage_reopen);
1721 return (atf_no_error());