xref: /xnu-11215.41.3/tests/mach_port_mod_refs.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1*33de042dSApple OSS Distributions #include <darwintest.h>
2*33de042dSApple OSS Distributions #include <mach/mach.h>
3*33de042dSApple OSS Distributions #include <stdlib.h>
4*33de042dSApple OSS Distributions #include <stdio.h>
5*33de042dSApple OSS Distributions 
6*33de042dSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true),
7*33de042dSApple OSS Distributions     T_META_NAMESPACE("xnu.ipc"),
8*33de042dSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
9*33de042dSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("IPC"));
10*33de042dSApple OSS Distributions 
11*33de042dSApple OSS Distributions T_DECL(mach_port_mod_refs, "mach_port_mod_refs", T_META_TAG_VM_PREFERRED){
12*33de042dSApple OSS Distributions 	mach_port_t port_set;
13*33de042dSApple OSS Distributions 	mach_port_t port;
14*33de042dSApple OSS Distributions 	task_exc_guard_behavior_t old, new;
15*33de042dSApple OSS Distributions 	int ret;
16*33de042dSApple OSS Distributions 
17*33de042dSApple OSS Distributions 	ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &port_set);
18*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_port_allocate MACH_PORT_RIGHT_PORT_SET");
19*33de042dSApple OSS Distributions 
20*33de042dSApple OSS Distributions 	ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
21*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_port_allocate MACH_PORT_RIGHT_RECEIVE");
22*33de042dSApple OSS Distributions 
23*33de042dSApple OSS Distributions 	/*
24*33de042dSApple OSS Distributions 	 * Disable [optional] Mach port guard exceptions to avoid fatal crash
25*33de042dSApple OSS Distributions 	 */
26*33de042dSApple OSS Distributions 	ret = task_get_exc_guard_behavior(mach_task_self(), &old);
27*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "task_get_exc_guard_behavior");
28*33de042dSApple OSS Distributions 	new = (old & ~TASK_EXC_GUARD_MP_DELIVER);
29*33de042dSApple OSS Distributions 	ret = task_set_exc_guard_behavior(mach_task_self(), new);
30*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "task_set_exc_guard_behavior new");
31*33de042dSApple OSS Distributions 
32*33de042dSApple OSS Distributions 	/*
33*33de042dSApple OSS Distributions 	 * Test all known variants of port rights on each type of port
34*33de042dSApple OSS Distributions 	 */
35*33de042dSApple OSS Distributions 
36*33de042dSApple OSS Distributions 	/* can't subtract a send right if it doesn't exist */
37*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND, -1);
38*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND: -1 on a RECV right");
39*33de042dSApple OSS Distributions 
40*33de042dSApple OSS Distributions 	/* can't subtract a send once right if it doesn't exist */
41*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_SEND_ONCE, -1);
42*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND_ONCE: -1 on a RECV right");
43*33de042dSApple OSS Distributions 
44*33de042dSApple OSS Distributions 	/* can't subtract a PORT SET right if it's not a port set */
45*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_PORT_SET, -1);
46*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs PORT_SET: -1 on a RECV right");
47*33de042dSApple OSS Distributions 
48*33de042dSApple OSS Distributions 	/* can't subtract a dead name right if it doesn't exist */
49*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_DEAD_NAME, -1);
50*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs DEAD_NAME: -1 on a RECV right");
51*33de042dSApple OSS Distributions 
52*33de042dSApple OSS Distributions 	/* can't subtract a LABELH right if it doesn't exist */
53*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_LABELH, -1);
54*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs LABELH: -1 on a RECV right");
55*33de042dSApple OSS Distributions 
56*33de042dSApple OSS Distributions 	/* can't subtract an invalid right-type */
57*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_NUMBER, -1);
58*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER: -1 on a RECV right");
59*33de042dSApple OSS Distributions 
60*33de042dSApple OSS Distributions 	/* can't subtract an invalid right-type */
61*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_NUMBER + 1, -1);
62*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER+1: -1 on a RECV right");
63*33de042dSApple OSS Distributions 
64*33de042dSApple OSS Distributions 
65*33de042dSApple OSS Distributions 	/* can't subtract a send right if it doesn't exist */
66*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_SEND, -1);
67*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND: -1 on a PORT_SET right");
68*33de042dSApple OSS Distributions 
69*33de042dSApple OSS Distributions 	/* can't subtract a send once right if it doesn't exist */
70*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_SEND_ONCE, -1);
71*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs SEND_ONCE: -1 on a PORT_SET right");
72*33de042dSApple OSS Distributions 
73*33de042dSApple OSS Distributions 	/* can't subtract a receive right if it's a port set */
74*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_RECEIVE, -1);
75*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs RECV: -1 on a PORT_SET right");
76*33de042dSApple OSS Distributions 
77*33de042dSApple OSS Distributions 	/* can't subtract a dead name right if it doesn't exist */
78*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_DEAD_NAME, -1);
79*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs DEAD_NAME: -1 on a PORT_SET right");
80*33de042dSApple OSS Distributions 
81*33de042dSApple OSS Distributions 	/* can't subtract a LABELH right if it doesn't exist */
82*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_LABELH, -1);
83*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_RIGHT, "mach_port_mod_refs LABELH: -1 on a PORT_SET right");
84*33de042dSApple OSS Distributions 
85*33de042dSApple OSS Distributions 	/* can't subtract an invalid right-type */
86*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_NUMBER, -1);
87*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER: -1 on a PORT_SET right");
88*33de042dSApple OSS Distributions 
89*33de042dSApple OSS Distributions 	/* can't subtract an invalid right-type */
90*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_NUMBER + 1, -1);
91*33de042dSApple OSS Distributions 	T_ASSERT_EQ(ret, KERN_INVALID_VALUE, "mach_port_mod_refs NUMBER+1: -1 on a PORT_SET right");
92*33de042dSApple OSS Distributions 
93*33de042dSApple OSS Distributions 	/* restore the old guard behavior */
94*33de042dSApple OSS Distributions 	ret = task_set_exc_guard_behavior(mach_task_self(), old);
95*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "task_set_exc_guard_behavior old");
96*33de042dSApple OSS Distributions 
97*33de042dSApple OSS Distributions 	/*
98*33de042dSApple OSS Distributions 	 * deallocate the ports/sets
99*33de042dSApple OSS Distributions 	 */
100*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port_set, MACH_PORT_RIGHT_PORT_SET, -1);
101*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_port_mod_refs(PORT_SET, -1)");
102*33de042dSApple OSS Distributions 
103*33de042dSApple OSS Distributions 	ret = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
104*33de042dSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(ret, "mach_port_mod_refs(RECV_RIGHT, -1)");
105*33de042dSApple OSS Distributions }
106