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