xref: /xnu-12377.41.6/tests/ipc/mach_port_description.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2024 Apple Computer, 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 <darwintest.h>
29 #include <darwintest_mach.h>
30 #include <darwintest_posix.h>
31 #include <mach/kern_return.h>
32 #include <mach/mach.h>
33 #include <mach/mach_port.h>
34 #include <mach/mach_vm.h>
35 #include <mach/vm_page_size.h>
36 #include <mach/vm_param.h>
37 #include <sys/sysctl.h>
38 
39 T_GLOBAL_META(
40 	T_META_NAMESPACE("xnu.mach.port_description"),
41 	T_META_RADAR_COMPONENT_NAME("xnu"),
42 	T_META_RADAR_COMPONENT_VERSION("ipc"));
43 
44 // kern/ipc_kobject.h
45 #define IKOT_NAMED_ENTRY 28
46 
47 T_DECL(vm_named_entry,
48     "test mach_port_kobject_description() on a named memory entry")
49 {
50 	kern_return_t kr;
51 	mach_vm_size_t size = vm_page_size;
52 	mach_port_t named_entry = MACH_PORT_NULL;
53 	natural_t object_type;
54 	mach_vm_address_t object_addr;
55 	kobject_description_t object_description;
56 	boolean_t dev_kern;
57 	size_t dev_kern_size = sizeof(dev_kern);
58 	int ret;
59 
60 	ret = sysctlbyname("kern.development", &dev_kern, &dev_kern_size, NULL, 0);
61 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(kern.development)");
62 
63 	// Create a memory entry
64 	kr = mach_make_memory_entry_64(mach_task_self(), &size, 0ull,
65 	    MAP_MEM_NAMED_CREATE | VM_PROT_DEFAULT, &named_entry, MACH_PORT_NULL);
66 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_make_memory_entry_64()");
67 
68 	// Describe it
69 	kr = mach_port_kobject_description(mach_task_self(), named_entry,
70 	    &object_type, &object_addr, object_description);
71 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_kobject_description()");
72 
73 	T_LOG("Object Type: %d", object_type);
74 	T_EXPECT_EQ(object_type, IKOT_NAMED_ENTRY, "object has type IKOT_NAMED_ENTRY");
75 
76 	T_LOG("Object Address: %llu", object_addr);
77 	if (dev_kern) {
78 		T_EXPECT_NE(object_addr, 0ull, "object address is populated on development kernel");
79 	} else {
80 		T_EXPECT_EQ(object_addr, 0ull, "object address is zero on release kernel");
81 	}
82 
83 	T_LOG("Object Description: %s", object_description);
84 	T_EXPECT_NE_STR(object_description, "", "object description is populated");
85 
86 	mach_port_deallocate(mach_task_self(), named_entry);
87 }
88