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