xref: /xnu-10002.81.5/tests/exception_tests.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <darwintest.h>
2*5e3eaea3SApple OSS Distributions #include <pthread/private.h>
3*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
4*5e3eaea3SApple OSS Distributions #include <mach/task.h>
5*5e3eaea3SApple OSS Distributions #include "exc_helpers.h"
6*5e3eaea3SApple OSS Distributions 
7*5e3eaea3SApple OSS Distributions #define EXCEPTION_IDENTITY_PROTECTED 4
8*5e3eaea3SApple OSS Distributions 
9*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
10*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
11*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
12*5e3eaea3SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
13*5e3eaea3SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
14*5e3eaea3SApple OSS Distributions 
15*5e3eaea3SApple OSS Distributions static size_t
exc_immovable_handler(mach_port_t task,mach_port_t thread,__unused exception_type_t type,__unused mach_exception_data_t codes)16*5e3eaea3SApple OSS Distributions exc_immovable_handler(
17*5e3eaea3SApple OSS Distributions 	mach_port_t task,
18*5e3eaea3SApple OSS Distributions 	mach_port_t thread,
19*5e3eaea3SApple OSS Distributions 	__unused exception_type_t type,
20*5e3eaea3SApple OSS Distributions 	__unused mach_exception_data_t codes)
21*5e3eaea3SApple OSS Distributions {
22*5e3eaea3SApple OSS Distributions 	T_EXPECT_EQ(task, mach_task_self(), "Received immovable task port");
23*5e3eaea3SApple OSS Distributions 	T_EXPECT_EQ(thread, pthread_mach_thread_np(pthread_main_thread_np()),
24*5e3eaea3SApple OSS Distributions 	    "Received immovable thread port");
25*5e3eaea3SApple OSS Distributions 	T_END;
26*5e3eaea3SApple OSS Distributions }
27*5e3eaea3SApple OSS Distributions 
28*5e3eaea3SApple OSS Distributions static size_t
exc_handler_identity_protected(task_id_token_t token,uint64_t thread_id,exception_type_t type,__unused exception_data_t codes)29*5e3eaea3SApple OSS Distributions exc_handler_identity_protected(
30*5e3eaea3SApple OSS Distributions 	task_id_token_t token,
31*5e3eaea3SApple OSS Distributions 	uint64_t thread_id,
32*5e3eaea3SApple OSS Distributions 	exception_type_t type,
33*5e3eaea3SApple OSS Distributions 	__unused exception_data_t codes)
34*5e3eaea3SApple OSS Distributions {
35*5e3eaea3SApple OSS Distributions 	mach_port_t port1, port2;
36*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions 	T_LOG("Got protected exception!");
39*5e3eaea3SApple OSS Distributions 
40*5e3eaea3SApple OSS Distributions 	port1 = mach_task_self();
41*5e3eaea3SApple OSS Distributions 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_CONTROL, &port2); /* Immovable control port for self */
42*5e3eaea3SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - CONTROL");
43*5e3eaea3SApple OSS Distributions 	T_EXPECT_EQ(port1, port2, "Control port matches!");
44*5e3eaea3SApple OSS Distributions 
45*5e3eaea3SApple OSS Distributions 	T_END;
46*5e3eaea3SApple OSS Distributions }
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions T_DECL(exc_immovable, "Test that exceptions receive immovable ports")
49*5e3eaea3SApple OSS Distributions {
50*5e3eaea3SApple OSS Distributions 	mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS);
51*5e3eaea3SApple OSS Distributions 	uint32_t opts = 0;
52*5e3eaea3SApple OSS Distributions 	size_t size = sizeof(&opts);
53*5e3eaea3SApple OSS Distributions 	mach_port_t mp;
54*5e3eaea3SApple OSS Distributions 	kern_return_t kr;
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions 	T_LOG("Check if task_exc_guard exception has been enabled\n");
57*5e3eaea3SApple OSS Distributions 	int ret = sysctlbyname("kern.ipc_control_port_options", &opts, &size, NULL, 0);
58*5e3eaea3SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(ret, "sysctlbyname(kern.ipc_control_port_options)");
59*5e3eaea3SApple OSS Distributions 
60*5e3eaea3SApple OSS Distributions 	if ((opts & 0x30) == 0) {
61*5e3eaea3SApple OSS Distributions 		T_SKIP("immovable rights aren't enabled");
62*5e3eaea3SApple OSS Distributions 	}
63*5e3eaea3SApple OSS Distributions 
64*5e3eaea3SApple OSS Distributions 	kr = task_get_special_port(mach_task_self(), TASK_KERNEL_PORT, &mp);
65*5e3eaea3SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "task_get_special_port");
66*5e3eaea3SApple OSS Distributions 	T_EXPECT_NE(mp, mach_task_self(), "should receive movable port");
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions 	/*
69*5e3eaea3SApple OSS Distributions 	 * do not deallocate the port we received on purpose to check
70*5e3eaea3SApple OSS Distributions 	 * that the exception will not coalesce with the movable port
71*5e3eaea3SApple OSS Distributions 	 * we have in our space now
72*5e3eaea3SApple OSS Distributions 	 */
73*5e3eaea3SApple OSS Distributions 
74*5e3eaea3SApple OSS Distributions 	run_exception_handler(exc_port, exc_immovable_handler);
75*5e3eaea3SApple OSS Distributions 	*(void *volatile*)0 = 0;
76*5e3eaea3SApple OSS Distributions }
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions T_DECL(exc_raise_identity_protected, "Test identity-protected exception delivery behavior")
79*5e3eaea3SApple OSS Distributions {
80*5e3eaea3SApple OSS Distributions 	mach_port_t exc_port = create_exception_port_behavior64(EXC_MASK_BAD_ACCESS, EXCEPTION_IDENTITY_PROTECTED);
81*5e3eaea3SApple OSS Distributions 
82*5e3eaea3SApple OSS Distributions 	run_exception_handler_behavior64(exc_port, NULL, exc_handler_identity_protected, EXCEPTION_IDENTITY_PROTECTED);
83*5e3eaea3SApple OSS Distributions 	*(void *volatile*)0 = 0;
84*5e3eaea3SApple OSS Distributions }
85