xref: /xnu-8792.41.9/tests/exception_ports_info.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #include <darwintest.h>
2*5c2921b0SApple OSS Distributions #include <mach/mach.h>
3*5c2921b0SApple OSS Distributions #include <mach/mach_types.h>
4*5c2921b0SApple OSS Distributions #include <mach/task.h>
5*5c2921b0SApple OSS Distributions #include <mach/thread_act.h>
6*5c2921b0SApple OSS Distributions #include <mach_debug/ipc_info.h>
7*5c2921b0SApple OSS Distributions 
8*5c2921b0SApple OSS Distributions T_GLOBAL_META(
9*5c2921b0SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
10*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
11*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
12*5c2921b0SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
13*5c2921b0SApple OSS Distributions 
14*5c2921b0SApple OSS Distributions T_DECL(exception_ports_info, "Test {task, thread}_get_exception_ports_info")
15*5c2921b0SApple OSS Distributions {
16*5c2921b0SApple OSS Distributions 	kern_return_t kr;
17*5c2921b0SApple OSS Distributions 	mach_port_t exc_port1, exc_port2, exc_port3;
18*5c2921b0SApple OSS Distributions 
19*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t count = EXC_TYPES_COUNT;
20*5c2921b0SApple OSS Distributions 	exception_mask_t masks[EXC_TYPES_COUNT];
21*5c2921b0SApple OSS Distributions 	ipc_info_port_t ports_info[EXC_TYPES_COUNT];
22*5c2921b0SApple OSS Distributions 	exception_behavior_t behaviors[EXC_TYPES_COUNT];
23*5c2921b0SApple OSS Distributions 	thread_state_flavor_t flavors[EXC_TYPES_COUNT];
24*5c2921b0SApple OSS Distributions 
25*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t count2 = EXC_TYPES_COUNT;
26*5c2921b0SApple OSS Distributions 	exception_mask_t masks2[EXC_TYPES_COUNT];
27*5c2921b0SApple OSS Distributions 	mach_port_t ports[EXC_TYPES_COUNT];
28*5c2921b0SApple OSS Distributions 	exception_behavior_t behaviors2[EXC_TYPES_COUNT];
29*5c2921b0SApple OSS Distributions 	thread_state_flavor_t flavors2[EXC_TYPES_COUNT];
30*5c2921b0SApple OSS Distributions 
31*5c2921b0SApple OSS Distributions 	unsigned int exc_port1_kotype = 0, exc_port1_kaddr = 0;
32*5c2921b0SApple OSS Distributions 	unsigned int exc_port2_kotype = 0, exc_port2_kaddr = 0;
33*5c2921b0SApple OSS Distributions 	unsigned int kotype = 0, kobject = 0, exc_port3_kotype = 0, exc_port3_kaddr = 0;
34*5c2921b0SApple OSS Distributions 	boolean_t found_exc_port1 = false;
35*5c2921b0SApple OSS Distributions 	boolean_t found_exc_port2 = false;
36*5c2921b0SApple OSS Distributions 	boolean_t found_exc_port3 = false;
37*5c2921b0SApple OSS Distributions 
38*5c2921b0SApple OSS Distributions 	ipc_info_space_t info_space;
39*5c2921b0SApple OSS Distributions 	ipc_info_name_array_t table;
40*5c2921b0SApple OSS Distributions 	ipc_info_tree_name_array_t tree;
41*5c2921b0SApple OSS Distributions 	mach_msg_type_number_t tblcnt = 0, treecnt = 0;
42*5c2921b0SApple OSS Distributions 
43*5c2921b0SApple OSS Distributions 	/* Create the mach port the exception messages will be sent to. */
44*5c2921b0SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port1);
45*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
46*5c2921b0SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port2);
47*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
48*5c2921b0SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port3);
49*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port");
50*5c2921b0SApple OSS Distributions 
51*5c2921b0SApple OSS Distributions 	/*
52*5c2921b0SApple OSS Distributions 	 * Insert a send right into the exception port that the kernel will use to
53*5c2921b0SApple OSS Distributions 	 * send the exception thread the exception messages.
54*5c2921b0SApple OSS Distributions 	 */
55*5c2921b0SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), exc_port1, exc_port1, MACH_MSG_TYPE_MAKE_SEND);
56*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
57*5c2921b0SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), exc_port2, exc_port2, MACH_MSG_TYPE_MAKE_SEND);
58*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
59*5c2921b0SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), exc_port3, exc_port3, MACH_MSG_TYPE_MAKE_SEND);
60*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port");
61*5c2921b0SApple OSS Distributions 
62*5c2921b0SApple OSS Distributions 	T_LOG("exc_port1: 0x%x", exc_port1);
63*5c2921b0SApple OSS Distributions 	T_LOG("exc_port2: 0x%x", exc_port2);
64*5c2921b0SApple OSS Distributions 	T_LOG("exc_port3: 0x%x", exc_port3);
65*5c2921b0SApple OSS Distributions 
66*5c2921b0SApple OSS Distributions 	/* Tell the kernel what port to send exceptions to. */
67*5c2921b0SApple OSS Distributions 	kr = task_set_exception_ports(
68*5c2921b0SApple OSS Distributions 		mach_task_self(),
69*5c2921b0SApple OSS Distributions 		EXC_MASK_GUARD,
70*5c2921b0SApple OSS Distributions 		exc_port1,
71*5c2921b0SApple OSS Distributions 		(exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES),
72*5c2921b0SApple OSS Distributions 		THREAD_STATE_NONE);
73*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler");
74*5c2921b0SApple OSS Distributions 
75*5c2921b0SApple OSS Distributions 	kr = task_set_exception_ports(
76*5c2921b0SApple OSS Distributions 		mach_task_self(),
77*5c2921b0SApple OSS Distributions 		EXC_MASK_RPC_ALERT,  /* why can't be EXC_CRASH or EXC_MASK_CORPSE_NOTIFY ? */
78*5c2921b0SApple OSS Distributions 		exc_port2,
79*5c2921b0SApple OSS Distributions 		(exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES),
80*5c2921b0SApple OSS Distributions 		THREAD_STATE_NONE);
81*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler");
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions 	kr = task_set_exception_ports(
84*5c2921b0SApple OSS Distributions 		mach_task_self(),
85*5c2921b0SApple OSS Distributions 		EXC_MASK_RESOURCE | EXC_MASK_BREAKPOINT | EXC_MASK_SYSCALL,
86*5c2921b0SApple OSS Distributions 		exc_port3,
87*5c2921b0SApple OSS Distributions 		(exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES),
88*5c2921b0SApple OSS Distributions 		THREAD_STATE_NONE);
89*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler");
90*5c2921b0SApple OSS Distributions 
91*5c2921b0SApple OSS Distributions 	/* now, get exception ports info */
92*5c2921b0SApple OSS Distributions 	kr = thread_get_exception_ports(mach_thread_self(), EXC_MASK_ALL, masks2, &count2, ports, behaviors2, flavors2);
93*5c2921b0SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "thread_get_exception_ports(): 0x%x", kr);
94*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(count2, 0, "should have 0 exception ports");
95*5c2921b0SApple OSS Distributions 
96*5c2921b0SApple OSS Distributions 	kr = thread_get_exception_ports_info(mach_thread_self(), EXC_MASK_ALL, masks, &count, ports_info, behaviors, flavors);
97*5c2921b0SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "thread_get_exception_ports_info(): 0x%x", kr);
98*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(count, 0, "should have 0 exception ports");
99*5c2921b0SApple OSS Distributions 
100*5c2921b0SApple OSS Distributions 	count = EXC_TYPES_COUNT;
101*5c2921b0SApple OSS Distributions 	count2 = EXC_TYPES_COUNT;
102*5c2921b0SApple OSS Distributions 
103*5c2921b0SApple OSS Distributions 	kr = task_get_exception_ports_info(mach_task_self(), EXC_MASK_ALL, masks, &count, ports_info, behaviors, flavors);
104*5c2921b0SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports_info(): 0x%x", kr);
105*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(count, 4, "should have 4 masks"); /* Returns 3 if one exc_port registers for EXC_CRASH */
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions 	/* get exception ports */
108*5c2921b0SApple OSS Distributions 	kr = task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, masks2, &count2, ports, behaviors2, flavors2);
109*5c2921b0SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports(): 0x%x", kr);
110*5c2921b0SApple OSS Distributions 
111*5c2921b0SApple OSS Distributions 	for (int i = 0; i < count2; i++) {
112*5c2921b0SApple OSS Distributions 		T_LOG("exception port name: 0x%x", ports[i]);
113*5c2921b0SApple OSS Distributions 	}
114*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(count, count2, "should return same mask count");
115*5c2921b0SApple OSS Distributions 
116*5c2921b0SApple OSS Distributions 	kr = memcmp(masks, masks2, count * sizeof(exception_mask_t));
117*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(kr, 0, "masks should be the same");
118*5c2921b0SApple OSS Distributions 
119*5c2921b0SApple OSS Distributions 	kr = memcmp(behaviors, behaviors2, count * sizeof(exception_behavior_t));
120*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(kr, 0, "behaviors should be the same");
121*5c2921b0SApple OSS Distributions 
122*5c2921b0SApple OSS Distributions 	kr = memcmp(flavors, flavors, count * sizeof(thread_state_flavor_t));
123*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(kr, 0, "flavors should be the same");
124*5c2921b0SApple OSS Distributions 
125*5c2921b0SApple OSS Distributions 	kr = mach_port_kernel_object(mach_task_self(), mach_task_self(), &kotype, &kobject);
126*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_kernel_object(): 0x%x", kr);
127*5c2921b0SApple OSS Distributions 	T_LOG("task_self kobject: 0x%x", kobject);
128*5c2921b0SApple OSS Distributions 
129*5c2921b0SApple OSS Distributions 	T_QUIET; T_EXPECT_MACH_SUCCESS(mach_port_space_info(mach_task_self(), &info_space, &table,
130*5c2921b0SApple OSS Distributions 	    &tblcnt, &tree, &treecnt), "mach_port_space_info(): 0x%x", kr);
131*5c2921b0SApple OSS Distributions 
132*5c2921b0SApple OSS Distributions 	for (int i = 0; i < tblcnt; i++) {
133*5c2921b0SApple OSS Distributions 		if (table[i].iin_name == exc_port1) {
134*5c2921b0SApple OSS Distributions 			exc_port1_kaddr = table[i].iin_object;
135*5c2921b0SApple OSS Distributions 		}
136*5c2921b0SApple OSS Distributions 		if (table[i].iin_name == exc_port2) {
137*5c2921b0SApple OSS Distributions 			exc_port2_kaddr = table[i].iin_object;
138*5c2921b0SApple OSS Distributions 		}
139*5c2921b0SApple OSS Distributions 		if (table[i].iin_name == exc_port3) {
140*5c2921b0SApple OSS Distributions 			exc_port3_kaddr = table[i].iin_object;
141*5c2921b0SApple OSS Distributions 		}
142*5c2921b0SApple OSS Distributions 	}
143*5c2921b0SApple OSS Distributions 
144*5c2921b0SApple OSS Distributions 	T_LOG("exc_port_1_kaddr: 0x%x", exc_port1_kaddr);
145*5c2921b0SApple OSS Distributions 	T_LOG("exc_port_2_kaddr: 0x%x", exc_port2_kaddr);
146*5c2921b0SApple OSS Distributions 	T_LOG("exc_port_3_kaddr: 0x%x", exc_port3_kaddr);
147*5c2921b0SApple OSS Distributions 
148*5c2921b0SApple OSS Distributions 	for (int i = 0; i < count; i++) {
149*5c2921b0SApple OSS Distributions 		T_LOG("ports_info[%d].iip_port_object: 0x%x", i, ports_info[i].iip_port_object);
150*5c2921b0SApple OSS Distributions 
151*5c2921b0SApple OSS Distributions 		if (ports_info[i].iip_port_object == exc_port1_kaddr) {
152*5c2921b0SApple OSS Distributions 			T_EXPECT_NE(ports_info[i].iip_port_object, 0,
153*5c2921b0SApple OSS Distributions 			    "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object);
154*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject,
155*5c2921b0SApple OSS Distributions 			    "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object);
156*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(masks[i], EXC_MASK_GUARD, "check if mask for exc_port1 is correct");
157*5c2921b0SApple OSS Distributions 			found_exc_port1 = true;
158*5c2921b0SApple OSS Distributions 		}
159*5c2921b0SApple OSS Distributions 		if (ports_info[i].iip_port_object == exc_port2_kaddr) {
160*5c2921b0SApple OSS Distributions 			T_EXPECT_NE(ports_info[i].iip_port_object, 0,
161*5c2921b0SApple OSS Distributions 			    "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object);
162*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject,
163*5c2921b0SApple OSS Distributions 			    "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object);
164*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(masks[i], EXC_MASK_RPC_ALERT, "check if mask for exc_port2 is correct");
165*5c2921b0SApple OSS Distributions 			found_exc_port2 = true;
166*5c2921b0SApple OSS Distributions 		}
167*5c2921b0SApple OSS Distributions 		if (ports_info[i].iip_port_object == exc_port3_kaddr) {
168*5c2921b0SApple OSS Distributions 			T_EXPECT_NE(ports_info[i].iip_port_object, 0,
169*5c2921b0SApple OSS Distributions 			    "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object);
170*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject,
171*5c2921b0SApple OSS Distributions 			    "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object);
172*5c2921b0SApple OSS Distributions 			T_EXPECT_EQ(masks[i], EXC_MASK_RESOURCE | EXC_MASK_BREAKPOINT | EXC_MASK_SYSCALL, "check if mask for exc_port3 is correct");
173*5c2921b0SApple OSS Distributions 			found_exc_port3 = true;
174*5c2921b0SApple OSS Distributions 		}
175*5c2921b0SApple OSS Distributions 	}
176*5c2921b0SApple OSS Distributions 
177*5c2921b0SApple OSS Distributions 	T_EXPECT_TRUE(found_exc_port1, "should find exc_port1");
178*5c2921b0SApple OSS Distributions 	T_EXPECT_TRUE(found_exc_port2, "should find exc_port2");
179*5c2921b0SApple OSS Distributions 	T_EXPECT_TRUE(found_exc_port3, "should find exc_port3");
180*5c2921b0SApple OSS Distributions }
181