xref: /xnu-12377.81.4/tests/ipc/port_api.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 
4 #include <mach/mach.h>
5 #include <mach/mach_types.h>
6 #include <mach/mach_vm.h>
7 #include <mach/message.h>
8 #include <mach/mach_error.h>
9 #include <mach/task.h>
10 
11 #include <pthread.h>
12 #include <pthread/workqueue_private.h>
13 
14 T_GLOBAL_META(
15 	T_META_NAMESPACE("xnu.ipc"),
16 	T_META_RUN_CONCURRENTLY(TRUE),
17 	T_META_RADAR_COMPONENT_NAME("xnu"),
18 	T_META_RADAR_COMPONENT_VERSION("IPC"));
19 
20 T_DECL(mach_port_insert_right_123724977, "regression test for 123724977")
21 {
22 	mach_port_name_t pset;
23 	kern_return_t kr;
24 
25 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &pset);
26 	T_ASSERT_MACH_SUCCESS(kr, "creating port set");
27 
28 	kr = mach_port_insert_right(mach_task_self(), pset, pset,
29 	    MACH_MSG_TYPE_MAKE_SEND);
30 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_RIGHT, "insert right fails");
31 }
32 
33 T_DECL(mach_port_name_rules, "make sure port names work correctly")
34 {
35 	mach_port_type_t ty;
36 	kern_return_t kr;
37 	mach_port_t mp, mp2;
38 
39 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
40 	T_ASSERT_MACH_SUCCESS(kr, "creating port");
41 	T_ASSERT_EQ(mp & 0x3u, 0x3, "low bits are 0x3");
42 
43 	kr = mach_port_type(mach_task_self(), mp, &ty);
44 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_type");
45 	T_ASSERT_TRUE(ty & MACH_PORT_TYPE_RECEIVE, "mp is a receive right");
46 
47 	kr = mach_port_type(mach_task_self(), mp & ~0x3u, &ty);
48 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_NAME,
49 	    "lookup is sensitive to the low bits");
50 
51 	kr = mach_port_destruct(mach_task_self(), mp, 0, 0);
52 	T_ASSERT_MACH_SUCCESS(kr, "destroying port");
53 
54 	kr = mach_port_type(mach_task_self(), mp, &ty);
55 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_NAME, "port is destroyed");
56 
57 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp2);
58 	T_ASSERT_MACH_SUCCESS(kr, "creating port");
59 	T_ASSERT_EQ(mp2 & 0x3, 0x3, "low bits are 0x3");
60 	T_ASSERT_NE(mp, mp2, "port name will change");
61 	T_ASSERT_EQ(mp & ~0xffu, mp2 & ~0xffu,
62 	    "the index was reused with a generation delta of %d",
63 	    (mp2 - mp) >> 2);
64 
65 	kr = mach_port_destruct(mach_task_self(), mp2, 0, 0);
66 	T_ASSERT_MACH_SUCCESS(kr, "destroying port");
67 }
68