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