xref: /xnu-11215.81.4/tests/voucher_entry_18826844.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions /*
2*d4514f0bSApple OSS Distributions  * Test that sending a message to a voucher with the same voucher as the voucher port
3*d4514f0bSApple OSS Distributions  * with only one send right count with move send before the copy send doesn't panic.
4*d4514f0bSApple OSS Distributions  *
5*d4514f0bSApple OSS Distributions  * clang -o voucherentry voucherentry.c -ldarwintest -Weverything -Wno-gnu-flexible-array-initializer
6*d4514f0bSApple OSS Distributions  *
7*d4514f0bSApple OSS Distributions  * <rdar://problem/18826844>
8*d4514f0bSApple OSS Distributions  */
9*d4514f0bSApple OSS Distributions 
10*d4514f0bSApple OSS Distributions #include <mach/mach.h>
11*d4514f0bSApple OSS Distributions #include <darwintest.h>
12*d4514f0bSApple OSS Distributions 
13*d4514f0bSApple OSS Distributions T_GLOBAL_META(
14*d4514f0bSApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
15*d4514f0bSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
16*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
17*d4514f0bSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"));
18*d4514f0bSApple OSS Distributions 
19*d4514f0bSApple OSS Distributions T_DECL(voucher_entry, "voucher_entry", T_META_CHECK_LEAKS(false), T_META_ALL_VALID_ARCHS(true), T_META_TAG_VM_PREFERRED)
20*d4514f0bSApple OSS Distributions {
21*d4514f0bSApple OSS Distributions 	kern_return_t kr        = KERN_SUCCESS;
22*d4514f0bSApple OSS Distributions 	mach_voucher_t voucher  = MACH_VOUCHER_NULL;
23*d4514f0bSApple OSS Distributions 	mach_port_name_t reply;
24*d4514f0bSApple OSS Distributions 	mach_port_options_t opts = {
25*d4514f0bSApple OSS Distributions 		.flags = MPO_REPLY_PORT,
26*d4514f0bSApple OSS Distributions 	};
27*d4514f0bSApple OSS Distributions 
28*d4514f0bSApple OSS Distributions 	/*
29*d4514f0bSApple OSS Distributions 	 * The bank voucher already exists in this process, so using it doesn't
30*d4514f0bSApple OSS Distributions 	 * actually test the problem. Use an importance voucher instead.
31*d4514f0bSApple OSS Distributions 	 */
32*d4514f0bSApple OSS Distributions 	mach_voucher_attr_recipe_data_t recipe = {
33*d4514f0bSApple OSS Distributions 		.key                = MACH_VOUCHER_ATTR_KEY_IMPORTANCE,
34*d4514f0bSApple OSS Distributions 		.command            = MACH_VOUCHER_ATTR_IMPORTANCE_SELF,
35*d4514f0bSApple OSS Distributions 		.previous_voucher   = MACH_VOUCHER_NULL,
36*d4514f0bSApple OSS Distributions 		.content_size       = 0,
37*d4514f0bSApple OSS Distributions 	};
38*d4514f0bSApple OSS Distributions 
39*d4514f0bSApple OSS Distributions 	kr = host_create_mach_voucher(mach_host_self(),
40*d4514f0bSApple OSS Distributions 	    (mach_voucher_attr_raw_recipe_array_t)&recipe,
41*d4514f0bSApple OSS Distributions 	    sizeof(recipe), &voucher);
42*d4514f0bSApple OSS Distributions 
43*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "host_create_mach_voucher");
44*d4514f0bSApple OSS Distributions 
45*d4514f0bSApple OSS Distributions 	T_ASSERT_NOTNULL(voucher, "voucher must not be null");
46*d4514f0bSApple OSS Distributions 
47*d4514f0bSApple OSS Distributions 	mach_port_urefs_t refs = 0;
48*d4514f0bSApple OSS Distributions 
49*d4514f0bSApple OSS Distributions 	kr = mach_port_get_refs(mach_task_self(), voucher, MACH_PORT_RIGHT_SEND, &refs);
50*d4514f0bSApple OSS Distributions 
51*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_get_refs");
52*d4514f0bSApple OSS Distributions 
53*d4514f0bSApple OSS Distributions 	T_ASSERT_EQ(refs, (mach_port_urefs_t)1, "voucher must have only one ref");
54*d4514f0bSApple OSS Distributions 
55*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_construct(mach_task_self(),
56*d4514f0bSApple OSS Distributions 	    &opts, 0, &reply), "make reply port");
57*d4514f0bSApple OSS Distributions 
58*d4514f0bSApple OSS Distributions 	/* First, try with two moves (must fail because there's only one ref) */
59*d4514f0bSApple OSS Distributions 	mach_msg_header_t request_msg_1 = {
60*d4514f0bSApple OSS Distributions 		.msgh_remote_port   = voucher,
61*d4514f0bSApple OSS Distributions 		.msgh_local_port    = reply,
62*d4514f0bSApple OSS Distributions 		.msgh_voucher_port  = voucher,
63*d4514f0bSApple OSS Distributions 		.msgh_bits          = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND,
64*d4514f0bSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND_ONCE, MACH_MSG_TYPE_MOVE_SEND, 0),
65*d4514f0bSApple OSS Distributions 		.msgh_id            = 0xDEAD,
66*d4514f0bSApple OSS Distributions 		.msgh_size          = sizeof(request_msg_1),
67*d4514f0bSApple OSS Distributions 	};
68*d4514f0bSApple OSS Distributions 
69*d4514f0bSApple OSS Distributions 	kr = mach_msg2(&request_msg_1, MACH64_SEND_MSG | MACH64_RCV_MSG | MACH64_SEND_KOBJECT_CALL,
70*d4514f0bSApple OSS Distributions 	    request_msg_1, request_msg_1.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, 0);
71*d4514f0bSApple OSS Distributions 
72*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_ERROR(MACH_SEND_INVALID_DEST, kr, "send with two moves should fail with invalid dest");
73*d4514f0bSApple OSS Distributions 
74*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_destruct(mach_task_self(), reply, 0, 0),
75*d4514f0bSApple OSS Distributions 	    "destroy reply port");
76*d4514f0bSApple OSS Distributions 
77*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_construct(mach_task_self(),
78*d4514f0bSApple OSS Distributions 	    &opts, 0, &reply), "make reply port");
79*d4514f0bSApple OSS Distributions 
80*d4514f0bSApple OSS Distributions 	/* Next, try with a move and a copy (will succeed and destroy the last ref) */
81*d4514f0bSApple OSS Distributions 	union {
82*d4514f0bSApple OSS Distributions 		mach_msg_header_t hdr;
83*d4514f0bSApple OSS Distributions 		struct {
84*d4514f0bSApple OSS Distributions 			mig_reply_error_t err;
85*d4514f0bSApple OSS Distributions 			mach_msg_trailer_t trailer;
86*d4514f0bSApple OSS Distributions 		};
87*d4514f0bSApple OSS Distributions 	} request_msg_2 = {
88*d4514f0bSApple OSS Distributions 		.hdr = {
89*d4514f0bSApple OSS Distributions 			.msgh_remote_port   = voucher,
90*d4514f0bSApple OSS Distributions 			.msgh_local_port    = reply,
91*d4514f0bSApple OSS Distributions 			.msgh_voucher_port  = voucher,
92*d4514f0bSApple OSS Distributions 			.msgh_bits          = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND,
93*d4514f0bSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND_ONCE, MACH_MSG_TYPE_COPY_SEND, 0),
94*d4514f0bSApple OSS Distributions 			.msgh_id            = 0xDEAD,
95*d4514f0bSApple OSS Distributions 			.msgh_size          = sizeof(request_msg_2),
96*d4514f0bSApple OSS Distributions 		}
97*d4514f0bSApple OSS Distributions 	};
98*d4514f0bSApple OSS Distributions 
99*d4514f0bSApple OSS Distributions 	/* panic happens here */
100*d4514f0bSApple OSS Distributions 	kr = mach_msg2(&request_msg_2, MACH64_SEND_MSG | MACH64_RCV_MSG | MACH64_SEND_KOBJECT_CALL,
101*d4514f0bSApple OSS Distributions 	    request_msg_2.hdr, request_msg_2.hdr.msgh_size, sizeof(request_msg_2),
102*d4514f0bSApple OSS Distributions 	    reply, MACH_MSG_TIMEOUT_NONE, 0);
103*d4514f0bSApple OSS Distributions 
104*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "send with move and copy succeeds");
105*d4514f0bSApple OSS Distributions 
106*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_destruct(mach_task_self(), reply, 0, 0),
107*d4514f0bSApple OSS Distributions 	    "destroy reply port");
108*d4514f0bSApple OSS Distributions 
109*d4514f0bSApple OSS Distributions 	kr = mach_port_get_refs(mach_task_self(), voucher, MACH_PORT_RIGHT_SEND, &refs);
110*d4514f0bSApple OSS Distributions 
111*d4514f0bSApple OSS Distributions 	T_ASSERT_MACH_ERROR(KERN_INVALID_NAME, kr, "voucher should now be invalid name");
112*d4514f0bSApple OSS Distributions }
113