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