xref: /xnu-10002.41.9/tests/mktimer_kobject.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributions #include <stdint.h>
2*699cd480SApple OSS Distributions #include <stdio.h>
3*699cd480SApple OSS Distributions #include <unistd.h>
4*699cd480SApple OSS Distributions 
5*699cd480SApple OSS Distributions #include <mach/mach.h>
6*699cd480SApple OSS Distributions #include <mach/mk_timer.h>
7*699cd480SApple OSS Distributions 
8*699cd480SApple OSS Distributions #include <darwintest.h>
9*699cd480SApple OSS Distributions 
10*699cd480SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
11*699cd480SApple OSS Distributions     T_META_NAMESPACE("xnu.ipc"),
12*699cd480SApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
13*699cd480SApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("IPC"));
14*699cd480SApple OSS Distributions 
15*699cd480SApple OSS Distributions T_DECL(mktimer_kobject, "mktimer_kobject()", T_META_ALL_VALID_ARCHS(true), T_META_IGNORECRASHES(".*mktimer_kobject.*"))
16*699cd480SApple OSS Distributions {
17*699cd480SApple OSS Distributions 	mach_port_t timer_port = MACH_PORT_NULL;
18*699cd480SApple OSS Distributions 	mach_port_t notify_port = MACH_PORT_NULL;
19*699cd480SApple OSS Distributions 
20*699cd480SApple OSS Distributions 	kern_return_t kr = KERN_SUCCESS;
21*699cd480SApple OSS Distributions 	task_exc_guard_behavior_t old, new;
22*699cd480SApple OSS Distributions 
23*699cd480SApple OSS Distributions 	/*
24*699cd480SApple OSS Distributions 	 * Disable [optional] Mach port guard exceptions to avoid fatal crash
25*699cd480SApple OSS Distributions 	 */
26*699cd480SApple OSS Distributions 	kr = task_get_exc_guard_behavior(mach_task_self(), &old);
27*699cd480SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_get_exc_guard_behavior");
28*699cd480SApple OSS Distributions 	new = (old & ~TASK_EXC_GUARD_MP_DELIVER);
29*699cd480SApple OSS Distributions 	kr = task_set_exc_guard_behavior(mach_task_self(), new);
30*699cd480SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_set_exc_guard_behavior new");
31*699cd480SApple OSS Distributions 
32*699cd480SApple OSS Distributions 	/*
33*699cd480SApple OSS Distributions 	 * timer port
34*699cd480SApple OSS Distributions 	 * This is a receive right which is also a kobject
35*699cd480SApple OSS Distributions 	 */
36*699cd480SApple OSS Distributions 	timer_port = mk_timer_create();
37*699cd480SApple OSS Distributions 	T_ASSERT_NE(timer_port, (mach_port_t)MACH_PORT_NULL, "mk_timer_create: %s", mach_error_string(kr));
38*699cd480SApple OSS Distributions 
39*699cd480SApple OSS Distributions 	mach_port_set_context(mach_task_self(), timer_port, (mach_port_context_t) 0x1);
40*699cd480SApple OSS Distributions 	T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_set_context(timer_port): %s", mach_error_string(kr));
41*699cd480SApple OSS Distributions 
42*699cd480SApple OSS Distributions 	/* notification port for the mk_timer port to come back on */
43*699cd480SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &notify_port);
44*699cd480SApple OSS Distributions 	T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_allocate(notify_port): %s", mach_error_string(kr));
45*699cd480SApple OSS Distributions 
46*699cd480SApple OSS Distributions 	kr = mach_port_set_context(mach_task_self(), notify_port, (mach_port_context_t) 0x2);
47*699cd480SApple OSS Distributions 	T_ASSERT_EQ(kr, KERN_SUCCESS, "mach_port_set_context(notify_port): %s", mach_error_string(kr));
48*699cd480SApple OSS Distributions 
49*699cd480SApple OSS Distributions 	T_LOG("timer: 0x%x, notify: 0x%x", timer_port, notify_port);
50*699cd480SApple OSS Distributions 
51*699cd480SApple OSS Distributions 	/*
52*699cd480SApple OSS Distributions 	 * This code generates a mach port guard exception and should be tested with an exception catcher.
53*699cd480SApple OSS Distributions 	 * Will be updated in <rdar://problem/70971318>
54*699cd480SApple OSS Distributions 	 */
55*699cd480SApple OSS Distributions 	mach_port_t previous = MACH_PORT_NULL;
56*699cd480SApple OSS Distributions 
57*699cd480SApple OSS Distributions 	/* request a port-destroyed notification on the timer port */
58*699cd480SApple OSS Distributions 	kr = mach_port_request_notification(mach_task_self(), timer_port, MACH_NOTIFY_PORT_DESTROYED,
59*699cd480SApple OSS Distributions 	    0, notify_port, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
60*699cd480SApple OSS Distributions 	/* this will ordinarily fail with a guard exception! */
61*699cd480SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_RIGHT, "notifications should NOT work on mk_timer ports!");
62*699cd480SApple OSS Distributions 
63*699cd480SApple OSS Distributions 	/* restore the old guard behavior */
64*699cd480SApple OSS Distributions 	kr = task_set_exc_guard_behavior(mach_task_self(), old);
65*699cd480SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "task_set_exc_guard_behavior old");
66*699cd480SApple OSS Distributions 
67*699cd480SApple OSS Distributions 	T_LOG("done");
68*699cd480SApple OSS Distributions }
69