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