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