1 /*
2 * Copyright (c) 2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <stdbool.h>
31
32 #include <mach/mach_init.h>
33 #include <mach/mach_vm.h>
34 #include <mach/task.h>
35 #include <mach/vm_map.h>
36 #include <machine/cpu_capabilities.h>
37
38 #include <sys/commpage.h>
39 #include <sys/mman.h>
40 #include <sys/syslimits.h>
41
42 #include <darwintest.h>
43 #include <darwintest_utils.h>
44
45 T_GLOBAL_META(
46 T_META_NAMESPACE("xnu.vm"),
47 T_META_RADAR_COMPONENT_NAME("xnu"),
48 T_META_RADAR_COMPONENT_VERSION("VM"),
49 T_META_RUN_CONCURRENTLY(true));
50
51
52 static const size_t kNumPages_4willneed = 16;
53 static vm_address_t vmaddr_4willneed = 0;
54 static void *fbaddr_4willneed = NULL;
55 static int fd_4willneed;
56
57 static void
willneed_tear_down(void)58 willneed_tear_down(void)
59 {
60 int ret;
61 kern_return_t kr;
62 vm_size_t vmsize_4willneed = PAGE_SIZE * kNumPages_4willneed;
63 kr = vm_deallocate(mach_task_self(), vmaddr_4willneed, vmsize_4willneed);
64 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate()");
65 vmaddr_4willneed = 0;
66 ret = munmap(fbaddr_4willneed, vmsize_4willneed);
67 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "munmap()");
68 ret = close(fd_4willneed);
69 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "close()");
70 }
71
72 T_DECL(madv_willneed, "test madvise(MADV_WILLNEED)")
73 {
74 char tmpf[PATH_MAX] = {0};
75 kern_return_t kr;
76 int ret;
77 vm_size_t vmsize_4willneed = PAGE_SIZE * kNumPages_4willneed;
78 uint8_t pattern[4] = {0xFEu, 0xDCu, 0xBAu, 0x98u};
79 char buf[vmsize_4willneed];
80 char vec[kNumPages_4willneed] = {0};
81
82 T_SETUPBEGIN;
83
84 T_LOG("Allocating %lu anonymous pages", kNumPages_4willneed);
85 kr = vm_allocate(mach_task_self(),
86 &vmaddr_4willneed,
87 vmsize_4willneed,
88 (VM_FLAGS_ANYWHERE |
89 VM_MAKE_TAG(VM_MEMORY_MALLOC)));
90 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate()");
91
92 strlcpy(tmpf, dt_tmpdir(), PATH_MAX);
93 strlcat(tmpf, "/willneed.txt", PATH_MAX);
94 T_QUIET; T_ASSERT_LT(strlen(tmpf), (unsigned long)PATH_MAX, "path exceeds PATH_MAX");
95
96 T_LOG("Opening file '%s'", tmpf);
97 fd_4willneed = open(tmpf, (O_RDWR | O_CREAT));
98 ret = fcntl(fd_4willneed, F_NOCACHE, TRUE);
99 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "fcntl(F_NOCACHE)");
100
101 memset_pattern4(buf, pattern, vmsize_4willneed);
102 write(fd_4willneed, buf, vmsize_4willneed);
103 fsync(fd_4willneed);
104
105 ret = fcntl(fd_4willneed, F_NOCACHE, FALSE);
106 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "fcntl(F_NOCACHE)");
107
108 fbaddr_4willneed = mmap(NULL, vmsize_4willneed, PROT_READ, (MAP_FILE | MAP_SHARED), fd_4willneed, 0);
109
110 T_ATEND(willneed_tear_down);
111
112 T_SETUPEND;
113
114 T_LOG("Checking anonymous residency (pre-madvise)");
115 ret = mincore((void *)vmaddr_4willneed, vmsize_4willneed, vec);
116 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore()");
117 for (size_t i = 0; i < kNumPages_4willneed; i++) {
118 T_QUIET; T_ASSERT_BITS_SET(vec[i], MINCORE_ANONYMOUS,
119 "pre-madvise pages should be anonymous");
120 T_QUIET; T_ASSERT_BITS_NOTSET(vec[i], MINCORE_INCORE,
121 "pre-madvise pages must not be resident");
122 T_QUIET; T_ASSERT_BITS_NOTSET(vec[i], MINCORE_REFERENCED,
123 "pre-madvise pages must not be referenced");
124 }
125
126 T_LOG("Advising anonymous memory...");
127 ret = madvise((void *)vmaddr_4willneed, vmsize_4willneed, MADV_WILLNEED);
128 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "madvise(MADV_WILLNEED)");
129
130 T_LOG("Checking anonymous residency");
131 ret = mincore((void *)vmaddr_4willneed, vmsize_4willneed, vec);
132 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore()");
133
134 for (size_t i = 0; i < kNumPages_4willneed; i++) {
135 T_EXPECT_BITS_SET(vec[i], MINCORE_INCORE,
136 "madvised page %lu should be resident", i);
137 }
138
139 T_LOG("Checking file-backed residency (pre-madvise)");
140 ret = mincore((void *)fbaddr_4willneed, vmsize_4willneed, vec);
141 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore()");
142 for (size_t i = 0; i < kNumPages_4willneed; i++) {
143 T_QUIET; T_ASSERT_BITS_NOTSET(vec[i], MINCORE_INCORE,
144 "pre-madvise pages must not be resident");
145 T_QUIET; T_ASSERT_BITS_NOTSET(vec[i], MINCORE_REFERENCED,
146 "pre-madvise pages must not be referenced");
147 T_QUIET; T_ASSERT_BITS_NOTSET(vec[i], MINCORE_MODIFIED,
148 "pre-madvise pages must not be modified");
149 }
150
151 T_LOG("Advising file-backed memory...");
152 ret = madvise(fbaddr_4willneed, vmsize_4willneed, MADV_WILLNEED);
153 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "madvise(MADV_WILLNEED)");
154
155 T_LOG("Checking file-backed residency (post-madvise)");
156 ret = mincore((void *)fbaddr_4willneed, vmsize_4willneed, vec);
157 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore()");
158
159 for (size_t i = 0; i < kNumPages_4willneed; i++) {
160 T_EXPECT_BITS_SET(vec[i], MINCORE_INCORE,
161 "madvised page %lu should be resident", i);
162 }
163 }
164