Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | /* * Copyright (c) 2024 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <darwintest.h> #include <darwintest_mach.h> #include <darwintest_posix.h> #include <mach/kern_return.h> #include <mach/mach.h> #include <mach/mach_port.h> #include <mach/mach_vm.h> #include <mach/vm_page_size.h> #include <mach/vm_param.h> #include <sys/sysctl.h> #include <semaphore.h> #include <stdlib.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.mach.port_description"), T_META_RADAR_COMPONENT_NAME("xnu"), T_META_RADAR_COMPONENT_VERSION("ipc")); // mach_debug/ipc_info.h #define IKOT_NAMED_ENTRY 28 /* IPC_OTYPE_NAMED_ENTRY */ #define IKOT_THREAD_RESUME 54 /* IPC_OTYPE_THREAD_RESUME */ T_DECL(vm_named_entry, "test mach_port_kobject_description() on a named memory entry") { kern_return_t kr; mach_vm_size_t size = vm_page_size; mach_port_t named_entry = MACH_PORT_NULL; natural_t object_type; mach_vm_address_t object_addr; kobject_description_t object_description; boolean_t dev_kern; size_t dev_kern_size = sizeof(dev_kern); int ret; ret = sysctlbyname("kern.development", &dev_kern, &dev_kern_size, NULL, 0); T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.development)"); // Create a memory entry kr = mach_make_memory_entry_64(mach_task_self(), &size, 0ull, MAP_MEM_NAMED_CREATE | VM_PROT_DEFAULT, &named_entry, MACH_PORT_NULL); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_make_memory_entry_64()"); // Describe it kr = mach_port_kobject_description(mach_task_self(), named_entry, &object_type, &object_addr, object_description); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_kobject_description()"); T_LOG("Object Type: %d", object_type); T_EXPECT_EQ(object_type, IKOT_NAMED_ENTRY, "object has type IKOT_NAMED_ENTRY"); T_LOG("Object Address: %llu", object_addr); if (dev_kern) { T_EXPECT_NE(object_addr, 0ull, "object address is populated on development kernel"); } else { T_EXPECT_EQ(object_addr, 0ull, "object address is zero on release kernel"); } T_LOG("Object Description: %s", object_description); T_EXPECT_NE_STR(object_description, "", "object description is populated"); mach_port_deallocate(mach_task_self(), named_entry); } static thread_t get_thread_for_pid(pid_t pid) { kern_return_t kr; task_t task = TASK_NULL; thread_act_array_t threads = NULL; mach_msg_type_number_t thread_count = 0; kr = task_for_pid(mach_task_self(), pid, &task); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_for_pid"); T_QUIET; T_ASSERT_NE(task, TASK_NULL, "task_for_pid task"); kr = task_threads(task, &threads, &thread_count); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_threads"); T_QUIET; T_ASSERT_NE(threads, NULL, "task_threads threads"); T_QUIET; T_ASSERT_NE(threads[0], THREAD_NULL, "threads[0]"); return threads[0]; } T_DECL(thread_resume, "test mach_port_kobject_description() on a thread resume port", T_META_ASROOT(true)) { int ret; kern_return_t kr; pid_t pid; sem_t *sync_sem; char sem_name[32]; thread_t thread; thread_suspension_token_t token; natural_t object_type; mach_vm_address_t object_addr; kobject_description_t object_description; /* Create a unique semaphore name */ snprintf(sem_name, sizeof(sem_name), "/thread_resume_desc_%d", getpid()); /* Create shared semaphore for parent-child synchronization */ sem_unlink(sem_name); /* Clean up any stale semaphore */ sync_sem = sem_open(sem_name, O_CREAT | O_EXCL, 0644, 0); T_QUIET; T_ASSERT_NE(sync_sem, SEM_FAILED, "sem_open"); pid = fork(); T_QUIET; T_ASSERT_NE(pid, -1, "fork"); if (pid == 0) { T_LOG("Child waiting for parent to signal readiness"); ret = sem_wait(sync_sem); T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sem_wait"); T_LOG("Child received signal from parent, exciting"); exit(0); } /* Obtain suspension token */ thread = get_thread_for_pid(pid); kr = thread_suspend2(thread, &token); T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "thread_suspend2"); T_QUIET; T_ASSERT_NE(token, THREAD_NULL, "thread_suspend2 token"); /* Describe it */ kr = mach_port_kobject_description(mach_task_self(), token, &object_type, &object_addr, object_description); T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_kobject_description()"); T_LOG("Object Type: %d", object_type); T_EXPECT_EQ(object_type, IKOT_THREAD_RESUME, "object has type IKOT_THREAD_RESUME"); T_LOG("Object Address: %llu", object_addr); T_EXPECT_NE(object_addr, 0ull, "object address is populated"); T_LOG("Object Description: %s", object_description); T_EXPECT_EQ(atoi(object_description), pid, "object description == pid"); /* Resume child */ kr = thread_resume2(token); T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "thread_resume2"); /* Signal child to exit */ ret = sem_post(sync_sem); T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sem_post"); wait(NULL); sem_close(sync_sem); sem_unlink(sem_name); } |