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