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