1 /* 2 * Copyright (c) 2022 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 29 #include <darwintest.h> 30 #include <TargetConditionals.h> 31 32 #include <errno.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/mman.h> 37 38 #include <mach/mach_error.h> 39 #include <mach/mach_init.h> 40 #include <mach/mach_vm.h> 41 42 T_GLOBAL_META( 43 T_META_NAMESPACE("xnu.vm"), 44 T_META_RADAR_COMPONENT_NAME("xnu"), 45 T_META_RADAR_COMPONENT_VERSION("VM")); 46 47 T_DECL(wire_copy_share, 48 "test VM object wired, copied and shared", T_META_TAG_VM_PREFERRED) 49 { 50 kern_return_t kr; 51 mach_vm_address_t vmaddr1, vmaddr2, vmaddr3; 52 mach_vm_size_t vmsize; 53 char *cp; 54 int i; 55 vm_prot_t cur_prot, max_prot; 56 int ret; 57 58 /* allocate anonymous memory */ 59 vmaddr1 = 0; 60 vmsize = 32 * PAGE_SIZE; 61 kr = mach_vm_allocate( 62 mach_task_self(), 63 &vmaddr1, 64 vmsize, 65 VM_FLAGS_ANYWHERE); 66 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate()"); 67 68 /* populate it */ 69 cp = (char *)(uintptr_t)vmaddr1; 70 for (i = 0; i < vmsize; i += PAGE_SIZE) { 71 cp[i] = i; 72 } 73 74 /* wire one page */ 75 ret = mlock(cp, PAGE_SIZE); 76 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mlock()"); 77 78 /* create a range to receive a copy */ 79 vmaddr2 = 0; 80 kr = mach_vm_allocate( 81 mach_task_self(), 82 &vmaddr2, 83 vmsize - PAGE_SIZE, 84 VM_FLAGS_ANYWHERE); 85 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate() for copy"); 86 87 /* copy the rest of the original object */ 88 kr = mach_vm_copy( 89 mach_task_self(), 90 vmaddr1 + PAGE_SIZE, 91 vmsize - PAGE_SIZE, 92 vmaddr2); 93 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_copy()"); 94 95 /* share the whole thing */ 96 vmaddr3 = 0; 97 kr = mach_vm_remap( 98 mach_task_self(), 99 &vmaddr3, 100 vmsize, 101 0, /* mask */ 102 VM_FLAGS_ANYWHERE, 103 mach_task_self(), 104 vmaddr1, 105 FALSE, /* copy */ 106 &cur_prot, 107 &max_prot, 108 VM_INHERIT_DEFAULT); 109 T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_remap()"); 110 } 111