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