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