xref: /xnu-12377.81.4/tests/exception_tests.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <darwintest.h>
2*043036a2SApple OSS Distributions #include <pthread/private.h>
3*043036a2SApple OSS Distributions #include <sys/sysctl.h>
4*043036a2SApple OSS Distributions #include <mach/task.h>
5*043036a2SApple OSS Distributions #include "exc_helpers.h"
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions #define EXCEPTION_IDENTITY_PROTECTED 4
8*043036a2SApple OSS Distributions 
9*043036a2SApple OSS Distributions T_GLOBAL_META(
10*043036a2SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
11*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
12*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
13*043036a2SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
14*043036a2SApple OSS Distributions 
15*043036a2SApple 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)16*043036a2SApple OSS Distributions exc_handler_identity_protected(
17*043036a2SApple OSS Distributions 	task_id_token_t token,
18*043036a2SApple OSS Distributions 	uint64_t thread_id,
19*043036a2SApple OSS Distributions 	exception_type_t type,
20*043036a2SApple OSS Distributions 	__unused exception_data_t codes)
21*043036a2SApple OSS Distributions {
22*043036a2SApple OSS Distributions 	mach_port_t port1, port2;
23*043036a2SApple OSS Distributions 	kern_return_t kr;
24*043036a2SApple OSS Distributions 
25*043036a2SApple OSS Distributions 	T_LOG("Got protected exception!");
26*043036a2SApple OSS Distributions 
27*043036a2SApple OSS Distributions 	port1 = mach_task_self();
28*043036a2SApple OSS Distributions 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_CONTROL, &port2); /* Immovable control port for self */
29*043036a2SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - CONTROL");
30*043036a2SApple OSS Distributions 	T_EXPECT_EQ(port1, port2, "Control port matches!");
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions 	T_END;
33*043036a2SApple OSS Distributions }
34*043036a2SApple OSS Distributions 
35*043036a2SApple OSS Distributions T_DECL(exc_raise_identity_protected, "Test identity-protected exception delivery behavior",
36*043036a2SApple OSS Distributions     T_META_TAG_VM_NOT_PREFERRED)
37*043036a2SApple OSS Distributions {
38*043036a2SApple OSS Distributions 	mach_port_t exc_port = create_exception_port_behavior64(EXC_MASK_BAD_ACCESS, EXCEPTION_IDENTITY_PROTECTED);
39*043036a2SApple OSS Distributions 
40*043036a2SApple OSS Distributions 	run_exception_handler_behavior64(exc_port, NULL, exc_handler_identity_protected, EXCEPTION_IDENTITY_PROTECTED, true);
41*043036a2SApple OSS Distributions 	*(void *volatile*)0 = 0;
42*043036a2SApple OSS Distributions }
43