1*043036a2SApple OSS Distributions #include <darwintest.h>
2*043036a2SApple OSS Distributions #include <darwintest_utils.h>
3*043036a2SApple OSS Distributions
4*043036a2SApple OSS Distributions #include <sys/types.h>
5*043036a2SApple OSS Distributions #include <sys/sysctl.h>
6*043036a2SApple OSS Distributions #include <mach/mach.h>
7*043036a2SApple OSS Distributions #include <mach/mach_vm.h>
8*043036a2SApple OSS Distributions #include <mach/task_info.h>
9*043036a2SApple OSS Distributions #include <mach/vm_param.h>
10*043036a2SApple OSS Distributions #include <mach/vm_types.h>
11*043036a2SApple OSS Distributions #include <sys/mman.h>
12*043036a2SApple OSS Distributions #include <unistd.h>
13*043036a2SApple OSS Distributions #include <TargetConditionals.h>
14*043036a2SApple OSS Distributions
15*043036a2SApple OSS Distributions enum {
16*043036a2SApple OSS Distributions DEFAULT = 0,
17*043036a2SApple OSS Distributions HEAP,
18*043036a2SApple OSS Distributions LARGE_FILE
19*043036a2SApple OSS Distributions };
20*043036a2SApple OSS Distributions
21*043036a2SApple OSS Distributions static char _filepath[MAXPATHLEN];
22*043036a2SApple OSS Distributions static struct mach_vm_range parent_default;
23*043036a2SApple OSS Distributions static struct mach_vm_range parent_heap;
24*043036a2SApple OSS Distributions
25*043036a2SApple OSS Distributions #define CHILD_PROCESS_COUNT (20)
26*043036a2SApple OSS Distributions #undef KiB
27*043036a2SApple OSS Distributions #undef MiB
28*043036a2SApple OSS Distributions #undef GiB
29*043036a2SApple OSS Distributions #define KiB(x) ((uint64_t)(x) << 10)
30*043036a2SApple OSS Distributions #define MiB(x) ((uint64_t)(x) << 20)
31*043036a2SApple OSS Distributions #define GiB(x) ((uint64_t)(x) << 30)
32*043036a2SApple OSS Distributions
33*043036a2SApple OSS Distributions #define ALLOCATION_SIZE (PAGE_SIZE)
34*043036a2SApple OSS Distributions #define LARGE_ALLOCATION_SIZE (GiB(1))
35*043036a2SApple OSS Distributions #define PER_ALLOC_AMT_GB (GiB(256))
36*043036a2SApple OSS Distributions #define N_ALLOC 5
37*043036a2SApple OSS Distributions
38*043036a2SApple OSS Distributions
39*043036a2SApple OSS Distributions /*
40*043036a2SApple OSS Distributions * Choose an arbitrary memory tag which applies to each of default/heap range
41*043036a2SApple OSS Distributions * for testing placement of allocations.
42*043036a2SApple OSS Distributions */
43*043036a2SApple OSS Distributions #define VM_MEMORY_RANGE_DEFAULT (VM_MAKE_TAG(VM_MEMORY_STACK))
44*043036a2SApple OSS Distributions #define VM_MEMORY_RANGE_HEAP (VM_MAKE_TAG(VM_MEMORY_MALLOC_SMALL))
45*043036a2SApple OSS Distributions
46*043036a2SApple OSS Distributions #define RANGE_DEFAULT_FLAGS (VM_FLAGS_ANYWHERE | VM_MEMORY_RANGE_DEFAULT)
47*043036a2SApple OSS Distributions #define RANGE_HEAP_FLAGS (VM_FLAGS_ANYWHERE | VM_MEMORY_RANGE_HEAP)
48*043036a2SApple OSS Distributions
49*043036a2SApple OSS Distributions T_GLOBAL_META(
50*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.vm"),
51*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
52*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"),
53*043036a2SApple OSS Distributions T_META_ENABLED(!TARGET_OS_OSX),
54*043036a2SApple OSS Distributions T_META_OWNER("mmorran")
55*043036a2SApple OSS Distributions );
56*043036a2SApple OSS Distributions
57*043036a2SApple OSS Distributions static bool
ranges_enabled(void)58*043036a2SApple OSS Distributions ranges_enabled(void)
59*043036a2SApple OSS Distributions {
60*043036a2SApple OSS Distributions struct mach_vm_range range;
61*043036a2SApple OSS Distributions size_t range_sz = sizeof(range);
62*043036a2SApple OSS Distributions
63*043036a2SApple OSS Distributions bzero(&range, sizeof(range));
64*043036a2SApple OSS Distributions
65*043036a2SApple OSS Distributions /*
66*043036a2SApple OSS Distributions * We will fail with ENOENT or EINVAL if ranges are either not supported
67*043036a2SApple OSS Distributions * or not enabled on our process.
68*043036a2SApple OSS Distributions */
69*043036a2SApple OSS Distributions return sysctlbyname("vm.vm_map_user_range_default",
70*043036a2SApple OSS Distributions &range, &range_sz, NULL, 0) == 0;
71*043036a2SApple OSS Distributions }
72*043036a2SApple OSS Distributions
73*043036a2SApple OSS Distributions #define CHECK_RANGES_ENABLED() \
74*043036a2SApple OSS Distributions if (!ranges_enabled()) { \
75*043036a2SApple OSS Distributions T_SKIP("VM map ranges not enabled"); \
76*043036a2SApple OSS Distributions }
77*043036a2SApple OSS Distributions
78*043036a2SApple OSS Distributions static struct mach_vm_range
get_range(int target_range)79*043036a2SApple OSS Distributions get_range(int target_range)
80*043036a2SApple OSS Distributions {
81*043036a2SApple OSS Distributions int ret = EINVAL;
82*043036a2SApple OSS Distributions struct mach_vm_range range;
83*043036a2SApple OSS Distributions size_t range_sz = sizeof(range);
84*043036a2SApple OSS Distributions
85*043036a2SApple OSS Distributions bzero(&range, sizeof(range));
86*043036a2SApple OSS Distributions
87*043036a2SApple OSS Distributions switch (target_range) {
88*043036a2SApple OSS Distributions case DEFAULT:
89*043036a2SApple OSS Distributions ret = sysctlbyname("vm.vm_map_user_range_default", &range, &range_sz, NULL, 0);
90*043036a2SApple OSS Distributions T_QUIET;
91*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "successfully retrieved user default range");
92*043036a2SApple OSS Distributions break;
93*043036a2SApple OSS Distributions
94*043036a2SApple OSS Distributions case HEAP:
95*043036a2SApple OSS Distributions ret = sysctlbyname("vm.vm_map_user_range_heap", &range, &range_sz, NULL, 0);
96*043036a2SApple OSS Distributions T_QUIET;
97*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "successfully retrieved user heap range");
98*043036a2SApple OSS Distributions break;
99*043036a2SApple OSS Distributions
100*043036a2SApple OSS Distributions case LARGE_FILE:
101*043036a2SApple OSS Distributions ret = sysctlbyname("vm.vm_map_user_range_large_file", &range, &range_sz, NULL, 0);
102*043036a2SApple OSS Distributions T_QUIET;
103*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "successfully retrieved user large file range");
104*043036a2SApple OSS Distributions break;
105*043036a2SApple OSS Distributions
106*043036a2SApple OSS Distributions default:
107*043036a2SApple OSS Distributions /* Fall through with EINVAL */
108*043036a2SApple OSS Distributions break;
109*043036a2SApple OSS Distributions }
110*043036a2SApple OSS Distributions
111*043036a2SApple OSS Distributions return range;
112*043036a2SApple OSS Distributions }
113*043036a2SApple OSS Distributions
114*043036a2SApple OSS Distributions static task_vm_info_data_t
get_vm_task_info(void)115*043036a2SApple OSS Distributions get_vm_task_info(void)
116*043036a2SApple OSS Distributions {
117*043036a2SApple OSS Distributions task_vm_info_data_t ti;
118*043036a2SApple OSS Distributions
119*043036a2SApple OSS Distributions mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
120*043036a2SApple OSS Distributions kern_return_t const kr = task_info(mach_task_self(),
121*043036a2SApple OSS Distributions TASK_VM_INFO,
122*043036a2SApple OSS Distributions (task_info_t) &ti,
123*043036a2SApple OSS Distributions &count);
124*043036a2SApple OSS Distributions T_QUIET;
125*043036a2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(kr, "get task_info()");
126*043036a2SApple OSS Distributions return ti;
127*043036a2SApple OSS Distributions }
128*043036a2SApple OSS Distributions
129*043036a2SApple OSS Distributions static mach_vm_address_t
assert_allocate(mach_vm_address_t dst,int vm_flags)130*043036a2SApple OSS Distributions assert_allocate(mach_vm_address_t dst, int vm_flags)
131*043036a2SApple OSS Distributions {
132*043036a2SApple OSS Distributions int ret = mach_vm_allocate(mach_task_self(), &dst, ALLOCATION_SIZE, vm_flags);
133*043036a2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "vm_allocate");
134*043036a2SApple OSS Distributions return dst;
135*043036a2SApple OSS Distributions }
136*043036a2SApple OSS Distributions
137*043036a2SApple OSS Distributions static void
assert_in_range(struct mach_vm_range range,mach_vm_offset_t addr)138*043036a2SApple OSS Distributions assert_in_range(struct mach_vm_range range, mach_vm_offset_t addr)
139*043036a2SApple OSS Distributions {
140*043036a2SApple OSS Distributions T_LOG("checking if %llx <= %llx <= %llx", range.min_address, addr,
141*043036a2SApple OSS Distributions range.max_address);
142*043036a2SApple OSS Distributions T_EXPECT_GE(addr, range.min_address, "allocation above min address");
143*043036a2SApple OSS Distributions T_EXPECT_LE(addr, range.max_address, "allocation below max address");
144*043036a2SApple OSS Distributions }
145*043036a2SApple OSS Distributions
146*043036a2SApple OSS Distributions static void
assert_in_heap_range(mach_vm_offset_t addr)147*043036a2SApple OSS Distributions assert_in_heap_range(mach_vm_offset_t addr)
148*043036a2SApple OSS Distributions {
149*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
150*043036a2SApple OSS Distributions
151*043036a2SApple OSS Distributions assert_in_range(range, addr);
152*043036a2SApple OSS Distributions }
153*043036a2SApple OSS Distributions
154*043036a2SApple OSS Distributions static void *
assert_mmap(void * addr,size_t sz,int fd,int flags)155*043036a2SApple OSS Distributions assert_mmap(void *addr, size_t sz, int fd, int flags)
156*043036a2SApple OSS Distributions {
157*043036a2SApple OSS Distributions void *ret = mmap(addr, sz, VM_PROT_READ | VM_PROT_WRITE,
158*043036a2SApple OSS Distributions flags, fd, 0);
159*043036a2SApple OSS Distributions T_EXPECT_NE(ret, MAP_FAILED, "mmap should not have MAP_FAILED");
160*043036a2SApple OSS Distributions T_EXPECT_NE(ret, NULL, "mmap should have returned a valid pointer");
161*043036a2SApple OSS Distributions return ret;
162*043036a2SApple OSS Distributions }
163*043036a2SApple OSS Distributions
164*043036a2SApple OSS Distributions static void
assert_allocate_eq(mach_vm_address_t dst,int vm_flags)165*043036a2SApple OSS Distributions assert_allocate_eq(mach_vm_address_t dst, int vm_flags)
166*043036a2SApple OSS Distributions {
167*043036a2SApple OSS Distributions mach_vm_address_t target = dst;
168*043036a2SApple OSS Distributions
169*043036a2SApple OSS Distributions T_ASSERT_MACH_SUCCESS(mach_vm_allocate(mach_task_self(), &target,
170*043036a2SApple OSS Distributions ALLOCATION_SIZE, vm_flags), "vm_allocate");
171*043036a2SApple OSS Distributions
172*043036a2SApple OSS Distributions T_EXPECT_EQ(target, dst, "target/dst differ");
173*043036a2SApple OSS Distributions }
174*043036a2SApple OSS Distributions
175*043036a2SApple OSS Distributions static mach_vm_address_t
assert_allocate_in_range(int target_range,mach_vm_address_t dst,int vm_flags)176*043036a2SApple OSS Distributions assert_allocate_in_range(int target_range, mach_vm_address_t dst, int vm_flags)
177*043036a2SApple OSS Distributions {
178*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(target_range);
179*043036a2SApple OSS Distributions dst = assert_allocate(dst, vm_flags);
180*043036a2SApple OSS Distributions
181*043036a2SApple OSS Distributions assert_in_range(range, (mach_vm_offset_t)dst);
182*043036a2SApple OSS Distributions
183*043036a2SApple OSS Distributions return dst;
184*043036a2SApple OSS Distributions }
185*043036a2SApple OSS Distributions
186*043036a2SApple OSS Distributions static void *
assert_mmap_in_range(void * addr,int target_range,size_t sz,int fd,int flags)187*043036a2SApple OSS Distributions assert_mmap_in_range(void *addr, int target_range, size_t sz, int fd, int flags)
188*043036a2SApple OSS Distributions {
189*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(target_range);
190*043036a2SApple OSS Distributions void *dst = assert_mmap(addr, sz, fd, flags);
191*043036a2SApple OSS Distributions
192*043036a2SApple OSS Distributions assert_in_range(range, (mach_vm_offset_t)dst);
193*043036a2SApple OSS Distributions
194*043036a2SApple OSS Distributions return dst;
195*043036a2SApple OSS Distributions }
196*043036a2SApple OSS Distributions
197*043036a2SApple OSS Distributions static void
ensure_mmap_fails(void * addr,size_t sz,int flags,int fd)198*043036a2SApple OSS Distributions ensure_mmap_fails(void *addr, size_t sz, int flags, int fd)
199*043036a2SApple OSS Distributions {
200*043036a2SApple OSS Distributions void *ret = mmap(addr, sz, VM_PROT_READ | VM_PROT_WRITE, flags, fd, 0);
201*043036a2SApple OSS Distributions T_QUIET;
202*043036a2SApple OSS Distributions T_EXPECT_EQ_PTR(ret, MAP_FAILED, "mmap should fail");
203*043036a2SApple OSS Distributions }
204*043036a2SApple OSS Distributions
205*043036a2SApple OSS Distributions __attribute__((overloadable))
206*043036a2SApple OSS Distributions static void
207*043036a2SApple OSS Distributions fork_child_test(void (^child_test)(void))
208*043036a2SApple OSS Distributions {
209*043036a2SApple OSS Distributions pid_t child_pid;
210*043036a2SApple OSS Distributions int err;
211*043036a2SApple OSS Distributions
212*043036a2SApple OSS Distributions child_pid = fork();
213*043036a2SApple OSS Distributions
214*043036a2SApple OSS Distributions if (child_pid == 0) {
215*043036a2SApple OSS Distributions /* child process */
216*043036a2SApple OSS Distributions T_LOG("in child");
217*043036a2SApple OSS Distributions child_test();
218*043036a2SApple OSS Distributions exit(0);
219*043036a2SApple OSS Distributions } else {
220*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(child_pid, "fork process");
221*043036a2SApple OSS Distributions
222*043036a2SApple OSS Distributions /* wait for child process to exit */
223*043036a2SApple OSS Distributions if (dt_waitpid(child_pid, &err, NULL, 30) == false) {
224*043036a2SApple OSS Distributions T_FAIL("dt_waitpid() failed on child pid %d", child_pid);
225*043036a2SApple OSS Distributions }
226*043036a2SApple OSS Distributions }
227*043036a2SApple OSS Distributions }
228*043036a2SApple OSS Distributions
229*043036a2SApple OSS Distributions __attribute__((overloadable))
230*043036a2SApple OSS Distributions static void
fork_child_test(void (* child_test)(void))231*043036a2SApple OSS Distributions fork_child_test(void (*child_test)(void))
232*043036a2SApple OSS Distributions {
233*043036a2SApple OSS Distributions fork_child_test(^{
234*043036a2SApple OSS Distributions child_test();
235*043036a2SApple OSS Distributions });
236*043036a2SApple OSS Distributions }
237*043036a2SApple OSS Distributions
238*043036a2SApple OSS Distributions static void
cleanup_file(void)239*043036a2SApple OSS Distributions cleanup_file(void)
240*043036a2SApple OSS Distributions {
241*043036a2SApple OSS Distributions unlink(_filepath);
242*043036a2SApple OSS Distributions bzero(_filepath, MAXPATHLEN);
243*043036a2SApple OSS Distributions }
244*043036a2SApple OSS Distributions
245*043036a2SApple OSS Distributions T_DECL(range_allocate_heap,
246*043036a2SApple OSS Distributions "ensure malloc tagged memory is allocated within the heap range", T_META_TAG_VM_PREFERRED)
247*043036a2SApple OSS Distributions {
248*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
249*043036a2SApple OSS Distributions
250*043036a2SApple OSS Distributions assert_allocate_in_range(HEAP, 0, RANGE_HEAP_FLAGS);
251*043036a2SApple OSS Distributions }
252*043036a2SApple OSS Distributions
253*043036a2SApple OSS Distributions T_DECL(range_allocate_anywhere,
254*043036a2SApple OSS Distributions "ensure allocation is within target range when hint is outwith range", T_META_TAG_VM_PREFERRED)
255*043036a2SApple OSS Distributions {
256*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
257*043036a2SApple OSS Distributions
258*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
259*043036a2SApple OSS Distributions
260*043036a2SApple OSS Distributions assert_allocate_in_range(HEAP, range.min_address - ALLOCATION_SIZE, RANGE_HEAP_FLAGS);
261*043036a2SApple OSS Distributions }
262*043036a2SApple OSS Distributions
263*043036a2SApple OSS Distributions T_DECL(range_allocate_stack,
264*043036a2SApple OSS Distributions "ensure a stack allocation is in the default range", T_META_TAG_VM_PREFERRED)
265*043036a2SApple OSS Distributions {
266*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
267*043036a2SApple OSS Distributions
268*043036a2SApple OSS Distributions assert_allocate_in_range(DEFAULT, 0, RANGE_DEFAULT_FLAGS);
269*043036a2SApple OSS Distributions }
270*043036a2SApple OSS Distributions
271*043036a2SApple OSS Distributions static void
ensure_fixed_mappings_succeed_cross(int heap)272*043036a2SApple OSS Distributions ensure_fixed_mappings_succeed_cross(int heap)
273*043036a2SApple OSS Distributions {
274*043036a2SApple OSS Distributions vm_map_address_t addr;
275*043036a2SApple OSS Distributions
276*043036a2SApple OSS Distributions addr = assert_allocate(0, VM_FLAGS_ANYWHERE | heap);
277*043036a2SApple OSS Distributions vm_deallocate(mach_task_self(), addr, ALLOCATION_SIZE);
278*043036a2SApple OSS Distributions
279*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_MEMORY_RANGE_DEFAULT);
280*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | VM_MEMORY_RANGE_DEFAULT);
281*043036a2SApple OSS Distributions vm_deallocate(mach_task_self(), addr, ALLOCATION_SIZE);
282*043036a2SApple OSS Distributions
283*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_MEMORY_RANGE_HEAP);
284*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE | VM_MEMORY_RANGE_HEAP);
285*043036a2SApple OSS Distributions vm_deallocate(mach_task_self(), addr, ALLOCATION_SIZE);
286*043036a2SApple OSS Distributions }
287*043036a2SApple OSS Distributions
288*043036a2SApple OSS Distributions static void
ensure_rogue_fixed_fails(void)289*043036a2SApple OSS Distributions ensure_rogue_fixed_fails(void)
290*043036a2SApple OSS Distributions {
291*043036a2SApple OSS Distributions struct mach_vm_range def = get_range(DEFAULT);
292*043036a2SApple OSS Distributions struct mach_vm_range heap = get_range(HEAP);
293*043036a2SApple OSS Distributions mach_vm_address_t addr;
294*043036a2SApple OSS Distributions kern_return_t kr;
295*043036a2SApple OSS Distributions
296*043036a2SApple OSS Distributions if (def.max_address + 3 * ALLOCATION_SIZE <= heap.min_address) {
297*043036a2SApple OSS Distributions addr = heap.min_address - 2 * ALLOCATION_SIZE;
298*043036a2SApple OSS Distributions } else {
299*043036a2SApple OSS Distributions /*
300*043036a2SApple OSS Distributions * in the unlikely event when there's no space
301*043036a2SApple OSS Distributions * between default and heap, then there must be
302*043036a2SApple OSS Distributions * a hole after heap.
303*043036a2SApple OSS Distributions */
304*043036a2SApple OSS Distributions addr = heap.max_address + ALLOCATION_SIZE;
305*043036a2SApple OSS Distributions }
306*043036a2SApple OSS Distributions
307*043036a2SApple OSS Distributions kr = mach_vm_allocate(mach_task_self(), &addr,
308*043036a2SApple OSS Distributions ALLOCATION_SIZE, VM_FLAGS_FIXED);
309*043036a2SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "should fail");
310*043036a2SApple OSS Distributions }
311*043036a2SApple OSS Distributions
312*043036a2SApple OSS Distributions static void
ensure_fixed_mapping(void)313*043036a2SApple OSS Distributions ensure_fixed_mapping(void)
314*043036a2SApple OSS Distributions {
315*043036a2SApple OSS Distributions ensure_fixed_mappings_succeed_cross(VM_MEMORY_RANGE_DEFAULT);
316*043036a2SApple OSS Distributions ensure_fixed_mappings_succeed_cross(VM_MEMORY_RANGE_HEAP);
317*043036a2SApple OSS Distributions
318*043036a2SApple OSS Distributions ensure_rogue_fixed_fails();
319*043036a2SApple OSS Distributions }
320*043036a2SApple OSS Distributions
321*043036a2SApple OSS Distributions T_DECL(range_allocate_fixed, "ensure fixed target is honored (even with an incorrect tag)", T_META_TAG_VM_PREFERRED)
322*043036a2SApple OSS Distributions {
323*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
324*043036a2SApple OSS Distributions
325*043036a2SApple OSS Distributions ensure_fixed_mapping();
326*043036a2SApple OSS Distributions fork_child_test(ensure_fixed_mapping);
327*043036a2SApple OSS Distributions }
328*043036a2SApple OSS Distributions
329*043036a2SApple OSS Distributions T_DECL(range_mmap_anon, "ensure anon mapping within HEAP range", T_META_TAG_VM_PREFERRED)
330*043036a2SApple OSS Distributions {
331*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
332*043036a2SApple OSS Distributions
333*043036a2SApple OSS Distributions assert_mmap_in_range(NULL, HEAP, ALLOCATION_SIZE, -1, MAP_ANON | MAP_PRIVATE);
334*043036a2SApple OSS Distributions }
335*043036a2SApple OSS Distributions
336*043036a2SApple OSS Distributions T_DECL(range_mmap_file, "ensure file is mapped within HEAP range", T_META_TAG_VM_PREFERRED)
337*043036a2SApple OSS Distributions {
338*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
339*043036a2SApple OSS Distributions
340*043036a2SApple OSS Distributions int fd = -1;
341*043036a2SApple OSS Distributions
342*043036a2SApple OSS Distributions /* prepare temp file */
343*043036a2SApple OSS Distributions strncpy(_filepath, "/tmp/mapfile.XXXXXX", MAXPATHLEN);
344*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd = mkstemp(_filepath), NULL);
345*043036a2SApple OSS Distributions atexit(cleanup_file);
346*043036a2SApple OSS Distributions
347*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ftruncate(fd, (off_t)ALLOCATION_SIZE), NULL);
348*043036a2SApple OSS Distributions
349*043036a2SApple OSS Distributions /* map it in to the heap rage */
350*043036a2SApple OSS Distributions #if TARGET_OS_OSX
351*043036a2SApple OSS Distributions T_LOG("mapping file in DEFAULT range");
352*043036a2SApple OSS Distributions assert_mmap_in_range(NULL, DEFAULT, ALLOCATION_SIZE, fd, MAP_FILE | MAP_SHARED);
353*043036a2SApple OSS Distributions #else
354*043036a2SApple OSS Distributions T_LOG("mapping file in HEAP range");
355*043036a2SApple OSS Distributions assert_mmap_in_range(NULL, HEAP, ALLOCATION_SIZE, fd, MAP_FILE | MAP_SHARED);
356*043036a2SApple OSS Distributions #endif
357*043036a2SApple OSS Distributions }
358*043036a2SApple OSS Distributions
359*043036a2SApple OSS Distributions
360*043036a2SApple OSS Distributions T_DECL(range_mmap_alias_tag, "ensure anon mapping with tag is honored", T_META_TAG_VM_PREFERRED)
361*043036a2SApple OSS Distributions {
362*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
363*043036a2SApple OSS Distributions
364*043036a2SApple OSS Distributions assert_mmap_in_range(NULL, DEFAULT, ALLOCATION_SIZE, VM_MEMORY_RANGE_DEFAULT, MAP_ANON | MAP_PRIVATE);
365*043036a2SApple OSS Distributions }
366*043036a2SApple OSS Distributions
367*043036a2SApple OSS Distributions T_DECL(range_mmap_with_low_hint,
368*043036a2SApple OSS Distributions "ensure allocation is within target range when hint is below range", T_META_TAG_VM_PREFERRED)
369*043036a2SApple OSS Distributions {
370*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
371*043036a2SApple OSS Distributions
372*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
373*043036a2SApple OSS Distributions mach_vm_address_t target = range.min_address - ALLOCATION_SIZE;
374*043036a2SApple OSS Distributions
375*043036a2SApple OSS Distributions assert_mmap_in_range((void *)target, HEAP, ALLOCATION_SIZE, -1, MAP_ANON | MAP_PRIVATE);
376*043036a2SApple OSS Distributions }
377*043036a2SApple OSS Distributions
378*043036a2SApple OSS Distributions T_DECL(range_mmap_with_high_hint,
379*043036a2SApple OSS Distributions "ensure allocation is within target range when hint is within range", T_META_TAG_VM_PREFERRED)
380*043036a2SApple OSS Distributions {
381*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
382*043036a2SApple OSS Distributions
383*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
384*043036a2SApple OSS Distributions mach_vm_address_t target = range.max_address - 100 * ALLOCATION_SIZE;
385*043036a2SApple OSS Distributions
386*043036a2SApple OSS Distributions void *dst = assert_mmap_in_range((void *)target, HEAP, ALLOCATION_SIZE, -1, MAP_ANON | MAP_PRIVATE);
387*043036a2SApple OSS Distributions
388*043036a2SApple OSS Distributions T_EXPECT_EQ((mach_vm_address_t)dst, target, "unexpected allocation address");
389*043036a2SApple OSS Distributions }
390*043036a2SApple OSS Distributions
391*043036a2SApple OSS Distributions T_DECL(range_mmap_with_bad_hint,
392*043036a2SApple OSS Distributions "ensure allocation fails when hint is above range", T_META_TAG_VM_PREFERRED)
393*043036a2SApple OSS Distributions {
394*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
395*043036a2SApple OSS Distributions
396*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
397*043036a2SApple OSS Distributions mach_vm_address_t target = range.max_address + 0x100000000;
398*043036a2SApple OSS Distributions
399*043036a2SApple OSS Distributions /* mmap should retry with 0 base on initial KERN_NO_SPACE failure */
400*043036a2SApple OSS Distributions assert_mmap_in_range((void *)target, HEAP, ALLOCATION_SIZE, -1, MAP_ANON | MAP_PRIVATE);
401*043036a2SApple OSS Distributions }
402*043036a2SApple OSS Distributions
403*043036a2SApple OSS Distributions T_DECL(range_mach_vm_map_with_bad_hint,
404*043036a2SApple OSS Distributions "ensure mach_vm_map fails when hint is above range", T_META_TAG_VM_PREFERRED)
405*043036a2SApple OSS Distributions {
406*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
407*043036a2SApple OSS Distributions
408*043036a2SApple OSS Distributions struct mach_vm_range range = get_range(HEAP);
409*043036a2SApple OSS Distributions mach_vm_address_t addr = range.max_address + 0x100000000;
410*043036a2SApple OSS Distributions
411*043036a2SApple OSS Distributions /*
412*043036a2SApple OSS Distributions * unlike mmap & vm_allocate, mach_vm_map should fail when given a hint
413*043036a2SApple OSS Distributions * out with the target range.
414*043036a2SApple OSS Distributions */
415*043036a2SApple OSS Distributions int ret = mach_vm_map(mach_task_self(), &addr, ALLOCATION_SIZE,
416*043036a2SApple OSS Distributions (mach_vm_offset_t)0, VM_FLAGS_ANYWHERE, MACH_PORT_NULL,
417*043036a2SApple OSS Distributions (memory_object_offset_t)0, FALSE, VM_PROT_DEFAULT, VM_PROT_ALL,
418*043036a2SApple OSS Distributions VM_INHERIT_DEFAULT);
419*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(ret, KERN_NO_SPACE, "expected KERN_NO_SPACE");
420*043036a2SApple OSS Distributions }
421*043036a2SApple OSS Distributions
422*043036a2SApple OSS Distributions T_DECL(range_mach_vm_remap_default,
423*043036a2SApple OSS Distributions "ensure mach_vm_remap is successful in default range", T_META_TAG_VM_PREFERRED)
424*043036a2SApple OSS Distributions {
425*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
426*043036a2SApple OSS Distributions
427*043036a2SApple OSS Distributions vm_prot_t curprot;
428*043036a2SApple OSS Distributions vm_prot_t maxprot;
429*043036a2SApple OSS Distributions
430*043036a2SApple OSS Distributions mach_vm_address_t addr = assert_allocate_in_range(DEFAULT, 0, RANGE_DEFAULT_FLAGS);
431*043036a2SApple OSS Distributions mach_vm_address_t target = addr + ALLOCATION_SIZE;
432*043036a2SApple OSS Distributions
433*043036a2SApple OSS Distributions int ret = mach_vm_remap(mach_task_self(), &target, ALLOCATION_SIZE,
434*043036a2SApple OSS Distributions (mach_vm_offset_t)0, VM_FLAGS_ANYWHERE, mach_task_self(),
435*043036a2SApple OSS Distributions addr, FALSE, &curprot, &maxprot, VM_INHERIT_NONE);
436*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(ret, KERN_SUCCESS, "expected KERN_SUCCESS");
437*043036a2SApple OSS Distributions }
438*043036a2SApple OSS Distributions
439*043036a2SApple OSS Distributions T_DECL(range_mach_vm_remap_heap_with_hint,
440*043036a2SApple OSS Distributions "ensure mach_vm_remap is successful in heap range", T_META_TAG_VM_PREFERRED)
441*043036a2SApple OSS Distributions {
442*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
443*043036a2SApple OSS Distributions
444*043036a2SApple OSS Distributions vm_prot_t curprot;
445*043036a2SApple OSS Distributions vm_prot_t maxprot;
446*043036a2SApple OSS Distributions
447*043036a2SApple OSS Distributions mach_vm_address_t addr = assert_allocate_in_range(HEAP, 0, RANGE_HEAP_FLAGS);
448*043036a2SApple OSS Distributions mach_vm_address_t target = addr + ALLOCATION_SIZE;
449*043036a2SApple OSS Distributions
450*043036a2SApple OSS Distributions int ret = mach_vm_remap(mach_task_self(), &target, ALLOCATION_SIZE,
451*043036a2SApple OSS Distributions (mach_vm_offset_t)0, VM_FLAGS_ANYWHERE, mach_task_self(),
452*043036a2SApple OSS Distributions addr, FALSE, &curprot, &maxprot, VM_INHERIT_NONE);
453*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(ret, KERN_SUCCESS, "expected KERN_SUCCESS");
454*043036a2SApple OSS Distributions assert_in_heap_range(target);
455*043036a2SApple OSS Distributions }
456*043036a2SApple OSS Distributions
457*043036a2SApple OSS Distributions T_DECL(range_mach_vm_remap_heap,
458*043036a2SApple OSS Distributions "ensure mach_vm_remap remains in same range", T_META_TAG_VM_PREFERRED)
459*043036a2SApple OSS Distributions {
460*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
461*043036a2SApple OSS Distributions
462*043036a2SApple OSS Distributions vm_prot_t curprot;
463*043036a2SApple OSS Distributions vm_prot_t maxprot;
464*043036a2SApple OSS Distributions
465*043036a2SApple OSS Distributions mach_vm_address_t addr = assert_allocate_in_range(HEAP, 0, RANGE_HEAP_FLAGS);
466*043036a2SApple OSS Distributions mach_vm_address_t target = 0;
467*043036a2SApple OSS Distributions
468*043036a2SApple OSS Distributions int ret = mach_vm_remap(mach_task_self(), &target, ALLOCATION_SIZE,
469*043036a2SApple OSS Distributions (mach_vm_offset_t)0, VM_FLAGS_ANYWHERE, mach_task_self(),
470*043036a2SApple OSS Distributions addr, FALSE, &curprot, &maxprot, VM_INHERIT_NONE);
471*043036a2SApple OSS Distributions T_EXPECT_EQ(ret, KERN_SUCCESS, "expected KERN_SUCCESS");
472*043036a2SApple OSS Distributions assert_in_heap_range(target);
473*043036a2SApple OSS Distributions }
474*043036a2SApple OSS Distributions
475*043036a2SApple OSS Distributions static void
ensure_range(void)476*043036a2SApple OSS Distributions ensure_range(void)
477*043036a2SApple OSS Distributions {
478*043036a2SApple OSS Distributions struct mach_vm_range def = get_range(DEFAULT);
479*043036a2SApple OSS Distributions struct mach_vm_range heap = get_range(HEAP);
480*043036a2SApple OSS Distributions
481*043036a2SApple OSS Distributions T_EXPECT_GT(heap.min_address, def.max_address,
482*043036a2SApple OSS Distributions "ranges should not overlap");
483*043036a2SApple OSS Distributions T_EXPECT_LE(heap.max_address, MACH_VM_MAX_ADDRESS,
484*043036a2SApple OSS Distributions "expected max <= %llx", MACH_VM_MAX_ADDRESS);
485*043036a2SApple OSS Distributions T_EXPECT_EQ(heap.min_address,
486*043036a2SApple OSS Distributions heap.min_address & (unsigned long)~0x1FFFFF,
487*043036a2SApple OSS Distributions "expected alignment on 2MB TT boundary");
488*043036a2SApple OSS Distributions }
489*043036a2SApple OSS Distributions
490*043036a2SApple OSS Distributions static void
ensure_child_range(void)491*043036a2SApple OSS Distributions ensure_child_range(void)
492*043036a2SApple OSS Distributions {
493*043036a2SApple OSS Distributions struct mach_vm_range def = get_range(DEFAULT);
494*043036a2SApple OSS Distributions struct mach_vm_range heap = get_range(HEAP);
495*043036a2SApple OSS Distributions
496*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(def.min_address, parent_default.min_address,
497*043036a2SApple OSS Distributions "expected forked default min to be equal");
498*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(def.max_address, parent_default.max_address,
499*043036a2SApple OSS Distributions "expected forked default max to be equal");
500*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(heap.min_address, parent_heap.min_address,
501*043036a2SApple OSS Distributions "expected forked heap min to be equal");
502*043036a2SApple OSS Distributions T_QUIET; T_EXPECT_EQ(heap.max_address, parent_heap.max_address,
503*043036a2SApple OSS Distributions "expected forked heap max to be equal");
504*043036a2SApple OSS Distributions }
505*043036a2SApple OSS Distributions
506*043036a2SApple OSS Distributions T_DECL(range_ensure_bounds, "ensure ranges respect map bounds", T_META_TAG_VM_PREFERRED)
507*043036a2SApple OSS Distributions {
508*043036a2SApple OSS Distributions CHECK_RANGES_ENABLED();
509*043036a2SApple OSS Distributions
510*043036a2SApple OSS Distributions parent_default = get_range(DEFAULT);
511*043036a2SApple OSS Distributions parent_heap = get_range(HEAP);
512*043036a2SApple OSS Distributions
513*043036a2SApple OSS Distributions ensure_range();
514*043036a2SApple OSS Distributions
515*043036a2SApple OSS Distributions for (uint32_t i = 0; i < CHILD_PROCESS_COUNT; i++) {
516*043036a2SApple OSS Distributions fork_child_test(ensure_child_range);
517*043036a2SApple OSS Distributions }
518*043036a2SApple OSS Distributions }
519*043036a2SApple OSS Distributions
520*043036a2SApple OSS Distributions static bool
parse_void_ranges(struct mach_vm_range * void1,struct mach_vm_range * void2)521*043036a2SApple OSS Distributions parse_void_ranges(struct mach_vm_range *void1, struct mach_vm_range *void2)
522*043036a2SApple OSS Distributions {
523*043036a2SApple OSS Distributions char buf[256];
524*043036a2SApple OSS Distributions size_t bsz = sizeof(buf) - 1;
525*043036a2SApple OSS Distributions char *s;
526*043036a2SApple OSS Distributions
527*043036a2SApple OSS Distributions if (sysctlbyname("vm.malloc_ranges", buf, &bsz, NULL, 0) == -1) {
528*043036a2SApple OSS Distributions if (errno == ENOENT) {
529*043036a2SApple OSS Distributions return false;
530*043036a2SApple OSS Distributions }
531*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(-1, "sysctlbyname(vm.malloc_ranges)");
532*043036a2SApple OSS Distributions }
533*043036a2SApple OSS Distributions buf[bsz] = '\0';
534*043036a2SApple OSS Distributions
535*043036a2SApple OSS Distributions s = buf;
536*043036a2SApple OSS Distributions
537*043036a2SApple OSS Distributions void1->min_address = strtoull(s, &s, 16);
538*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*s, ':', "should have a ':'");
539*043036a2SApple OSS Distributions s++;
540*043036a2SApple OSS Distributions
541*043036a2SApple OSS Distributions void1->max_address = strtoull(s, &s, 16);
542*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*s, ' ', "should have a ' '");
543*043036a2SApple OSS Distributions s++;
544*043036a2SApple OSS Distributions
545*043036a2SApple OSS Distributions void2->min_address = strtoull(s, &s, 16);
546*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*s, ':', "should have a ':'");
547*043036a2SApple OSS Distributions s++;
548*043036a2SApple OSS Distributions
549*043036a2SApple OSS Distributions void2->max_address = strtoull(s, &s, 16);
550*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_EQ(*s, '\0', "should be done");
551*043036a2SApple OSS Distributions
552*043036a2SApple OSS Distributions return true;
553*043036a2SApple OSS Distributions }
554*043036a2SApple OSS Distributions
555*043036a2SApple OSS Distributions T_DECL(create_range, "ensure create ranges kinda works", T_META_TAG_VM_PREFERRED)
556*043036a2SApple OSS Distributions {
557*043036a2SApple OSS Distributions struct mach_vm_range void1, void2, *r;
558*043036a2SApple OSS Distributions
559*043036a2SApple OSS Distributions mach_vm_range_recipe_v1_t array[10];
560*043036a2SApple OSS Distributions uint32_t nranges = 0;
561*043036a2SApple OSS Distributions
562*043036a2SApple OSS Distributions if (!parse_void_ranges(&void1, &void2)) {
563*043036a2SApple OSS Distributions T_SKIP("malloc_ranges not supported");
564*043036a2SApple OSS Distributions }
565*043036a2SApple OSS Distributions
566*043036a2SApple OSS Distributions T_LOG("Ranges are %#llx:%#llx %#llx:%#llx",
567*043036a2SApple OSS Distributions void1.min_address, void1.max_address,
568*043036a2SApple OSS Distributions void2.min_address, void2.max_address);
569*043036a2SApple OSS Distributions
570*043036a2SApple OSS Distributions #define reset() \
571*043036a2SApple OSS Distributions nranges = 0
572*043036a2SApple OSS Distributions #define add_range(l, r) \
573*043036a2SApple OSS Distributions array[nranges++] = (mach_vm_range_recipe_v1_t){ \
574*043036a2SApple OSS Distributions .range = { l, r }, .range_tag = MACH_VM_RANGE_FIXED, \
575*043036a2SApple OSS Distributions }
576*043036a2SApple OSS Distributions #define create_ranges() \
577*043036a2SApple OSS Distributions mach_vm_range_create(mach_task_self(), MACH_VM_RANGE_FLAVOR_V1, \
578*043036a2SApple OSS Distributions (mach_vm_range_recipes_raw_t)array, sizeof(array[0]) * nranges)
579*043036a2SApple OSS Distributions
580*043036a2SApple OSS Distributions if (void1.min_address + MiB(128) > void1.max_address) {
581*043036a2SApple OSS Distributions r = &void2;
582*043036a2SApple OSS Distributions } else {
583*043036a2SApple OSS Distributions r = &void1;
584*043036a2SApple OSS Distributions }
585*043036a2SApple OSS Distributions
586*043036a2SApple OSS Distributions reset();
587*043036a2SApple OSS Distributions add_range(void1.min_address - MiB(10), void1.min_address);
588*043036a2SApple OSS Distributions T_EXPECT_MACH_ERROR(create_ranges(), KERN_INVALID_ARGUMENT,
589*043036a2SApple OSS Distributions "should fail: range outside of voids");
590*043036a2SApple OSS Distributions
591*043036a2SApple OSS Distributions reset();
592*043036a2SApple OSS Distributions add_range(r->min_address + MiB(1), r->min_address + MiB(3));
593*043036a2SApple OSS Distributions add_range(r->min_address, r->min_address + MiB(2));
594*043036a2SApple OSS Distributions T_EXPECT_MACH_ERROR(create_ranges(), KERN_INVALID_ARGUMENT,
595*043036a2SApple OSS Distributions "should fail: overlapping ranges");
596*043036a2SApple OSS Distributions
597*043036a2SApple OSS Distributions reset();
598*043036a2SApple OSS Distributions add_range(r->min_address, r->min_address + MiB(1));
599*043036a2SApple OSS Distributions add_range(r->min_address + MiB(2), r->min_address + MiB(3));
600*043036a2SApple OSS Distributions T_EXPECT_MACH_SUCCESS(create_ranges(), "should succeed");
601*043036a2SApple OSS Distributions
602*043036a2SApple OSS Distributions reset();
603*043036a2SApple OSS Distributions add_range(r->min_address, r->min_address + MiB(1));
604*043036a2SApple OSS Distributions add_range(r->min_address + MiB(2), r->min_address + MiB(3));
605*043036a2SApple OSS Distributions T_EXPECT_MACH_ERROR(create_ranges(), KERN_MEMORY_PRESENT,
606*043036a2SApple OSS Distributions "should fail: already allocated");
607*043036a2SApple OSS Distributions
608*043036a2SApple OSS Distributions reset();
609*043036a2SApple OSS Distributions add_range(r->min_address + MiB(4), r->min_address + MiB(5));
610*043036a2SApple OSS Distributions add_range(r->min_address + MiB(6), r->min_address + MiB(7));
611*043036a2SApple OSS Distributions T_EXPECT_MACH_SUCCESS(create_ranges(), "should succeed");
612*043036a2SApple OSS Distributions
613*043036a2SApple OSS Distributions __block vm_offset_t offs = 0;
614*043036a2SApple OSS Distributions
615*043036a2SApple OSS Distributions void (^check_works)(void) = ^{
616*043036a2SApple OSS Distributions mach_vm_address_t addr;
617*043036a2SApple OSS Distributions kern_return_t kr;
618*043036a2SApple OSS Distributions
619*043036a2SApple OSS Distributions offs += PAGE_SIZE;
620*043036a2SApple OSS Distributions addr = r->min_address + offs;
621*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
622*043036a2SApple OSS Distributions
623*043036a2SApple OSS Distributions addr = r->min_address + MiB(2) + offs;
624*043036a2SApple OSS Distributions assert_allocate_eq(addr, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
625*043036a2SApple OSS Distributions
626*043036a2SApple OSS Distributions addr = r->min_address + MiB(1);
627*043036a2SApple OSS Distributions kr = mach_vm_allocate(mach_task_self(), &addr, ALLOCATION_SIZE,
628*043036a2SApple OSS Distributions VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
629*043036a2SApple OSS Distributions T_EXPECT_MACH_ERROR(kr, KERN_INVALID_ADDRESS, "should fail");
630*043036a2SApple OSS Distributions };
631*043036a2SApple OSS Distributions
632*043036a2SApple OSS Distributions check_works();
633*043036a2SApple OSS Distributions fork_child_test(check_works);
634*043036a2SApple OSS Distributions
635*043036a2SApple OSS Distributions #undef create_ranges
636*043036a2SApple OSS Distributions #undef add_range
637*043036a2SApple OSS Distributions #undef reset
638*043036a2SApple OSS Distributions }
639*043036a2SApple OSS Distributions
640*043036a2SApple OSS Distributions T_DECL(range_mmap_too_large, "ensure mmap fails when allocation is too large", T_META_TAG_VM_PREFERRED)
641*043036a2SApple OSS Distributions {
642*043036a2SApple OSS Distributions // Get VM map min_offset and max_offset
643*043036a2SApple OSS Distributions task_vm_info_data_t const ti = get_vm_task_info();
644*043036a2SApple OSS Distributions T_LOG("task_info range: 0x%llx-0x%llx, covering %llu bytes of memory",
645*043036a2SApple OSS Distributions ti.min_address, ti.max_address, ti.max_address - ti.min_address);
646*043036a2SApple OSS Distributions
647*043036a2SApple OSS Distributions // Try to mmap more memory than the address space can handle
648*043036a2SApple OSS Distributions size_t const sz_too_large = ti.max_address - ti.min_address + 1;
649*043036a2SApple OSS Distributions T_LOG("Trying to allocate %zu bytes", sz_too_large);
650*043036a2SApple OSS Distributions ensure_mmap_fails(NULL, sz_too_large, MAP_ANON | MAP_PRIVATE, -1);
651*043036a2SApple OSS Distributions }
652*043036a2SApple OSS Distributions
653*043036a2SApple OSS Distributions T_DECL(range_mmap_outside_map_range_fixed,
654*043036a2SApple OSS Distributions "ensure mmap fails when making a fixed allocation beyond VM map max address", T_META_TAG_VM_PREFERRED)
655*043036a2SApple OSS Distributions {
656*043036a2SApple OSS Distributions // Get VM map min_offset and max_offset
657*043036a2SApple OSS Distributions task_vm_info_data_t const ti = get_vm_task_info();
658*043036a2SApple OSS Distributions T_LOG("task_info range: 0x%llx-0x%llx", ti.min_address, ti.max_address);
659*043036a2SApple OSS Distributions
660*043036a2SApple OSS Distributions // Try to allocate a page between VM map max_offset and MACH_VM_MAX_ADDRESS
661*043036a2SApple OSS Distributions mach_vm_address_t const target = ti.max_address + PAGE_SIZE;
662*043036a2SApple OSS Distributions T_LOG("Trying to allocate memory at 0x%llx", target);
663*043036a2SApple OSS Distributions ensure_mmap_fails((void *)target, ALLOCATION_SIZE, MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1);
664*043036a2SApple OSS Distributions }
665*043036a2SApple OSS Distributions
666*043036a2SApple OSS Distributions static bool
large_file_range_enabled(void)667*043036a2SApple OSS Distributions large_file_range_enabled(void)
668*043036a2SApple OSS Distributions {
669*043036a2SApple OSS Distributions struct mach_vm_range range;
670*043036a2SApple OSS Distributions size_t range_sz = sizeof(range);
671*043036a2SApple OSS Distributions
672*043036a2SApple OSS Distributions bzero(&range, sizeof(range));
673*043036a2SApple OSS Distributions
674*043036a2SApple OSS Distributions /*
675*043036a2SApple OSS Distributions * We will fail with ENOENT or EINVAL if ranges are either not supported
676*043036a2SApple OSS Distributions * or not enabled on our process.
677*043036a2SApple OSS Distributions */
678*043036a2SApple OSS Distributions int const ret = sysctlbyname("vm.vm_map_user_range_large_file",
679*043036a2SApple OSS Distributions &range, &range_sz, NULL, 0);
680*043036a2SApple OSS Distributions if (ret) {
681*043036a2SApple OSS Distributions T_LOG("vm.vm_map_user_range_large_file errno: %d", errno);
682*043036a2SApple OSS Distributions } else {
683*043036a2SApple OSS Distributions T_LOG("large file range: (%llx, %llx)",
684*043036a2SApple OSS Distributions range.min_address, range.max_address);
685*043036a2SApple OSS Distributions }
686*043036a2SApple OSS Distributions return ret == 0;
687*043036a2SApple OSS Distributions }
688*043036a2SApple OSS Distributions
689*043036a2SApple OSS Distributions T_DECL(range_mmap_large_file,
690*043036a2SApple OSS Distributions "ensure large file is mapped within LARGE_FILE range", T_META_TAG_VM_PREFERRED)
691*043036a2SApple OSS Distributions {
692*043036a2SApple OSS Distributions if (!large_file_range_enabled()) {
693*043036a2SApple OSS Distributions T_SKIP("large file range not enabled");
694*043036a2SApple OSS Distributions }
695*043036a2SApple OSS Distributions
696*043036a2SApple OSS Distributions void *ptrs[N_ALLOC];
697*043036a2SApple OSS Distributions uint32_t i;
698*043036a2SApple OSS Distributions
699*043036a2SApple OSS Distributions int fd = -1;
700*043036a2SApple OSS Distributions /* prepare temp file */
701*043036a2SApple OSS Distributions char const * tmp_dir = dt_tmpdir();
702*043036a2SApple OSS Distributions snprintf(_filepath, MAXPATHLEN, "%s/maplargefile.XXXXXX", tmp_dir);
703*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd = mkstemp(_filepath), NULL);
704*043036a2SApple OSS Distributions atexit(cleanup_file);
705*043036a2SApple OSS Distributions
706*043036a2SApple OSS Distributions T_LOG("Attempting to allocate VA space in %llu GB chunks.",
707*043036a2SApple OSS Distributions LARGE_ALLOCATION_SIZE);
708*043036a2SApple OSS Distributions for (i = 0; i < N_ALLOC; ++i) {
709*043036a2SApple OSS Distributions void *p = assert_mmap_in_range(NULL, LARGE_FILE, LARGE_ALLOCATION_SIZE,
710*043036a2SApple OSS Distributions fd, MAP_SHARED | MAP_FILE);
711*043036a2SApple OSS Distributions if (p == MAP_FAILED) {
712*043036a2SApple OSS Distributions if (errno != ENOMEM) {
713*043036a2SApple OSS Distributions T_WITH_ERRNO;
714*043036a2SApple OSS Distributions T_LOG("mmap failed: stopped at %u of %d/%llu GB chunks",
715*043036a2SApple OSS Distributions i + 1, N_ALLOC, LARGE_ALLOCATION_SIZE);
716*043036a2SApple OSS Distributions }
717*043036a2SApple OSS Distributions break;
718*043036a2SApple OSS Distributions } else {
719*043036a2SApple OSS Distributions T_LOG("allocation %u: %p", i + 1, p);
720*043036a2SApple OSS Distributions }
721*043036a2SApple OSS Distributions
722*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(p, "mmap");
723*043036a2SApple OSS Distributions ptrs[i] = p;
724*043036a2SApple OSS Distributions }
725*043036a2SApple OSS Distributions
726*043036a2SApple OSS Distributions T_EXPECT_GE_UINT(i, N_ALLOC, "Allocate at least %u/%d %llu-GB chunks of VA space",
727*043036a2SApple OSS Distributions i, N_ALLOC, (LARGE_ALLOCATION_SIZE / GiB(1)));
728*043036a2SApple OSS Distributions
729*043036a2SApple OSS Distributions T_LOG("Unmapping memory");
730*043036a2SApple OSS Distributions for (uint32_t j = 0; j < i; ++j) {
731*043036a2SApple OSS Distributions int const res = munmap(ptrs[j], LARGE_ALLOCATION_SIZE);
732*043036a2SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(res, 0, "munmap");
733*043036a2SApple OSS Distributions }
734*043036a2SApple OSS Distributions }
735