xref: /xnu-11215.81.4/tests/mach_port_insert_right.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions #include <stdio.h>
2*d4514f0bSApple OSS Distributions #include <mach/mach.h>
3*d4514f0bSApple OSS Distributions #include <mach/message.h>
4*d4514f0bSApple OSS Distributions #include <darwintest.h>
5*d4514f0bSApple OSS Distributions 
6*d4514f0bSApple OSS Distributions T_GLOBAL_META(
7*d4514f0bSApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
8*d4514f0bSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
9*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
10*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
11*d4514f0bSApple OSS Distributions 	T_META_TAG_VM_PREFERRED);
12*d4514f0bSApple OSS Distributions 
13*d4514f0bSApple OSS Distributions static inline mach_port_type_t
get_port_type(mach_port_t mp)14*d4514f0bSApple OSS Distributions get_port_type(mach_port_t mp)
15*d4514f0bSApple OSS Distributions {
16*d4514f0bSApple OSS Distributions 	mach_port_type_t type;
17*d4514f0bSApple OSS Distributions 	T_QUIET;
18*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_type(mach_task_self(), mp, &type),
19*d4514f0bSApple OSS Distributions 	    "mach_port_type(mP)");
20*d4514f0bSApple OSS Distributions 	return type;
21*d4514f0bSApple OSS Distributions }
22*d4514f0bSApple OSS Distributions 
23*d4514f0bSApple OSS Distributions T_DECL(mach_port_insert_right, "insert send right for an existing right", T_META_CHECK_LEAKS(false))
24*d4514f0bSApple OSS Distributions {
25*d4514f0bSApple OSS Distributions 	mach_port_t port = MACH_PORT_NULL;
26*d4514f0bSApple OSS Distributions 	mach_port_t port2 = MACH_PORT_NULL;
27*d4514f0bSApple OSS Distributions 	kern_return_t retval;
28*d4514f0bSApple OSS Distributions 
29*d4514f0bSApple OSS Distributions 	mach_port_t task = mach_task_self();
30*d4514f0bSApple OSS Distributions 
31*d4514f0bSApple OSS Distributions 	retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port);
32*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port);
33*d4514f0bSApple OSS Distributions 
34*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE,
35*d4514f0bSApple OSS Distributions 	    "0x%x should be a receive right", port);
36*d4514f0bSApple OSS Distributions 
37*d4514f0bSApple OSS Distributions 	retval = mach_port_insert_right(task, port, port, MACH_MSG_TYPE_MAKE_SEND);
38*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "insert a send right for port=[%d] with name=[%d]", port, port);
39*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE | MACH_PORT_TYPE_SEND,
40*d4514f0bSApple OSS Distributions 	    "0x%x should be a send-receive right", port);
41*d4514f0bSApple OSS Distributions 
42*d4514f0bSApple OSS Distributions 	mach_port_name_t name = 123;
43*d4514f0bSApple OSS Distributions 	/* 0x7B, lower two bits are 0b11 which is valid gen number, but index is invalid (0) */
44*d4514f0bSApple OSS Distributions 	retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
45*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_FAILURE, "insert a send right for port=[%d] with name=[%d]", port, name);
46*d4514f0bSApple OSS Distributions 
47*d4514f0bSApple OSS Distributions 	name = 122;
48*d4514f0bSApple OSS Distributions 	/* invalid gen number, should return KERN_INVALID_VALUE */
49*d4514f0bSApple OSS Distributions 	retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
50*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_INVALID_VALUE, "insert a send right for port=[%d] with name=[%d]", port, name);
51*d4514f0bSApple OSS Distributions 
52*d4514f0bSApple OSS Distributions 	retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port2);
53*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port2);
54*d4514f0bSApple OSS Distributions 
55*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port2), MACH_PORT_TYPE_RECEIVE,
56*d4514f0bSApple OSS Distributions 	    "0x%x should be a receive right", port2);
57*d4514f0bSApple OSS Distributions 
58*d4514f0bSApple OSS Distributions 	name = port;
59*d4514f0bSApple OSS Distributions 	retval = mach_port_insert_right(task, name, port2, MACH_MSG_TYPE_MAKE_SEND);
60*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_RIGHT_EXISTS, "insert a send right for port=[%d] with name=[%d]", port2, name);
61*d4514f0bSApple OSS Distributions }
62