1*bbb1b6f9SApple OSS Distributions #include <darwintest.h> 2*bbb1b6f9SApple OSS Distributions #include <mach/mach.h> 3*bbb1b6f9SApple OSS Distributions #include <mach/mach_types.h> 4*bbb1b6f9SApple OSS Distributions #include <mach/task.h> 5*bbb1b6f9SApple OSS Distributions #include <mach/thread_act.h> 6*bbb1b6f9SApple OSS Distributions #include <mach_debug/ipc_info.h> 7*bbb1b6f9SApple OSS Distributions 8*bbb1b6f9SApple OSS Distributions T_GLOBAL_META( 9*bbb1b6f9SApple OSS Distributions T_META_NAMESPACE("xnu.ipc"), 10*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 11*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"), 12*bbb1b6f9SApple OSS Distributions T_META_RUN_CONCURRENTLY(true), 13*bbb1b6f9SApple OSS Distributions T_META_TAG_VM_PREFERRED); 14*bbb1b6f9SApple OSS Distributions 15*bbb1b6f9SApple OSS Distributions T_DECL(exception_ports_info, "Test {task, thread}_get_exception_ports_info") 16*bbb1b6f9SApple OSS Distributions { 17*bbb1b6f9SApple OSS Distributions kern_return_t kr; 18*bbb1b6f9SApple OSS Distributions mach_port_t exc_port1, exc_port2, exc_port3; 19*bbb1b6f9SApple OSS Distributions 20*bbb1b6f9SApple OSS Distributions mach_msg_type_number_t count = EXC_TYPES_COUNT; 21*bbb1b6f9SApple OSS Distributions exception_mask_t masks[EXC_TYPES_COUNT]; 22*bbb1b6f9SApple OSS Distributions ipc_info_port_t ports_info[EXC_TYPES_COUNT]; 23*bbb1b6f9SApple OSS Distributions exception_behavior_t behaviors[EXC_TYPES_COUNT]; 24*bbb1b6f9SApple OSS Distributions thread_state_flavor_t flavors[EXC_TYPES_COUNT]; 25*bbb1b6f9SApple OSS Distributions 26*bbb1b6f9SApple OSS Distributions mach_msg_type_number_t count2 = EXC_TYPES_COUNT; 27*bbb1b6f9SApple OSS Distributions exception_mask_t masks2[EXC_TYPES_COUNT]; 28*bbb1b6f9SApple OSS Distributions mach_port_t ports[EXC_TYPES_COUNT]; 29*bbb1b6f9SApple OSS Distributions exception_behavior_t behaviors2[EXC_TYPES_COUNT]; 30*bbb1b6f9SApple OSS Distributions thread_state_flavor_t flavors2[EXC_TYPES_COUNT]; 31*bbb1b6f9SApple OSS Distributions 32*bbb1b6f9SApple OSS Distributions unsigned int exc_port1_kotype = 0, exc_port1_kaddr = 0; 33*bbb1b6f9SApple OSS Distributions unsigned int exc_port2_kotype = 0, exc_port2_kaddr = 0; 34*bbb1b6f9SApple OSS Distributions unsigned int kotype = 0, kobject = 0, exc_port3_kotype = 0, exc_port3_kaddr = 0; 35*bbb1b6f9SApple OSS Distributions boolean_t found_exc_port1 = false; 36*bbb1b6f9SApple OSS Distributions boolean_t found_exc_port2 = false; 37*bbb1b6f9SApple OSS Distributions boolean_t found_exc_port3 = false; 38*bbb1b6f9SApple OSS Distributions 39*bbb1b6f9SApple OSS Distributions ipc_info_space_t info_space; 40*bbb1b6f9SApple OSS Distributions ipc_info_name_array_t table; 41*bbb1b6f9SApple OSS Distributions ipc_info_tree_name_array_t tree; 42*bbb1b6f9SApple OSS Distributions mach_msg_type_number_t tblcnt = 0, treecnt = 0; 43*bbb1b6f9SApple OSS Distributions 44*bbb1b6f9SApple OSS Distributions /* Create the mach port the exception messages will be sent to. */ 45*bbb1b6f9SApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port1); 46*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port"); 47*bbb1b6f9SApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port2); 48*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port"); 49*bbb1b6f9SApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exc_port3); 50*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Allocated mach exception port"); 51*bbb1b6f9SApple OSS Distributions 52*bbb1b6f9SApple OSS Distributions /* 53*bbb1b6f9SApple OSS Distributions * Insert a send right into the exception port that the kernel will use to 54*bbb1b6f9SApple OSS Distributions * send the exception thread the exception messages. 55*bbb1b6f9SApple OSS Distributions */ 56*bbb1b6f9SApple OSS Distributions kr = mach_port_insert_right(mach_task_self(), exc_port1, exc_port1, MACH_MSG_TYPE_MAKE_SEND); 57*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port"); 58*bbb1b6f9SApple OSS Distributions kr = mach_port_insert_right(mach_task_self(), exc_port2, exc_port2, MACH_MSG_TYPE_MAKE_SEND); 59*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port"); 60*bbb1b6f9SApple OSS Distributions kr = mach_port_insert_right(mach_task_self(), exc_port3, exc_port3, MACH_MSG_TYPE_MAKE_SEND); 61*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Inserted a SEND right into the exception port"); 62*bbb1b6f9SApple OSS Distributions 63*bbb1b6f9SApple OSS Distributions T_LOG("exc_port1: 0x%x", exc_port1); 64*bbb1b6f9SApple OSS Distributions T_LOG("exc_port2: 0x%x", exc_port2); 65*bbb1b6f9SApple OSS Distributions T_LOG("exc_port3: 0x%x", exc_port3); 66*bbb1b6f9SApple OSS Distributions 67*bbb1b6f9SApple OSS Distributions /* Tell the kernel what port to send exceptions to. */ 68*bbb1b6f9SApple OSS Distributions kr = task_set_exception_ports( 69*bbb1b6f9SApple OSS Distributions mach_task_self(), 70*bbb1b6f9SApple OSS Distributions EXC_MASK_GUARD, 71*bbb1b6f9SApple OSS Distributions exc_port1, 72*bbb1b6f9SApple OSS Distributions (exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES), 73*bbb1b6f9SApple OSS Distributions THREAD_STATE_NONE); 74*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler"); 75*bbb1b6f9SApple OSS Distributions 76*bbb1b6f9SApple OSS Distributions kr = task_set_exception_ports( 77*bbb1b6f9SApple OSS Distributions mach_task_self(), 78*bbb1b6f9SApple OSS Distributions EXC_MASK_RPC_ALERT, /* why can't be EXC_CRASH or EXC_MASK_CORPSE_NOTIFY ? */ 79*bbb1b6f9SApple OSS Distributions exc_port2, 80*bbb1b6f9SApple OSS Distributions (exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES), 81*bbb1b6f9SApple OSS Distributions THREAD_STATE_NONE); 82*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler"); 83*bbb1b6f9SApple OSS Distributions 84*bbb1b6f9SApple OSS Distributions kr = task_set_exception_ports( 85*bbb1b6f9SApple OSS Distributions mach_task_self(), 86*bbb1b6f9SApple OSS Distributions EXC_MASK_RESOURCE | EXC_MASK_BREAKPOINT | EXC_MASK_SYSCALL, 87*bbb1b6f9SApple OSS Distributions exc_port3, 88*bbb1b6f9SApple OSS Distributions (exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES), 89*bbb1b6f9SApple OSS Distributions THREAD_STATE_NONE); 90*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "Set the exception port to my custom handler"); 91*bbb1b6f9SApple OSS Distributions 92*bbb1b6f9SApple OSS Distributions /* now, get exception ports info */ 93*bbb1b6f9SApple OSS Distributions kr = thread_get_exception_ports(mach_thread_self(), EXC_MASK_ALL, masks2, &count2, ports, behaviors2, flavors2); 94*bbb1b6f9SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "thread_get_exception_ports(): 0x%x", kr); 95*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(count2, 0, "should have 0 exception ports"); 96*bbb1b6f9SApple OSS Distributions 97*bbb1b6f9SApple OSS Distributions kr = thread_get_exception_ports_info(mach_thread_self(), EXC_MASK_ALL, masks, &count, ports_info, behaviors, flavors); 98*bbb1b6f9SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "thread_get_exception_ports_info(): 0x%x", kr); 99*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(count, 0, "should have 0 exception ports"); 100*bbb1b6f9SApple OSS Distributions 101*bbb1b6f9SApple OSS Distributions count = EXC_TYPES_COUNT; 102*bbb1b6f9SApple OSS Distributions count2 = EXC_TYPES_COUNT; 103*bbb1b6f9SApple OSS Distributions 104*bbb1b6f9SApple OSS Distributions kr = task_get_exception_ports_info(mach_task_self(), EXC_MASK_ALL, masks, &count, ports_info, behaviors, flavors); 105*bbb1b6f9SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports_info(): 0x%x", kr); 106*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(count, 4, "should have 4 masks"); /* Returns 3 if one exc_port registers for EXC_CRASH */ 107*bbb1b6f9SApple OSS Distributions 108*bbb1b6f9SApple OSS Distributions /* get exception ports */ 109*bbb1b6f9SApple OSS Distributions kr = task_get_exception_ports(mach_task_self(), EXC_MASK_ALL, masks2, &count2, ports, behaviors2, flavors2); 110*bbb1b6f9SApple OSS Distributions T_EXPECT_MACH_SUCCESS(kr, "task_get_exception_ports(): 0x%x", kr); 111*bbb1b6f9SApple OSS Distributions 112*bbb1b6f9SApple OSS Distributions for (int i = 0; i < count2; i++) { 113*bbb1b6f9SApple OSS Distributions T_LOG("exception port name: 0x%x", ports[i]); 114*bbb1b6f9SApple OSS Distributions } 115*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(count, count2, "should return same mask count"); 116*bbb1b6f9SApple OSS Distributions 117*bbb1b6f9SApple OSS Distributions kr = memcmp(masks, masks2, count * sizeof(exception_mask_t)); 118*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(kr, 0, "masks should be the same"); 119*bbb1b6f9SApple OSS Distributions 120*bbb1b6f9SApple OSS Distributions kr = memcmp(behaviors, behaviors2, count * sizeof(exception_behavior_t)); 121*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(kr, 0, "behaviors should be the same"); 122*bbb1b6f9SApple OSS Distributions 123*bbb1b6f9SApple OSS Distributions kr = memcmp(flavors, flavors, count * sizeof(thread_state_flavor_t)); 124*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(kr, 0, "flavors should be the same"); 125*bbb1b6f9SApple OSS Distributions 126*bbb1b6f9SApple OSS Distributions kr = mach_port_kernel_object(mach_task_self(), mach_task_self(), &kotype, &kobject); 127*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_kernel_object(): 0x%x", kr); 128*bbb1b6f9SApple OSS Distributions T_LOG("task_self kobject: 0x%x", kobject); 129*bbb1b6f9SApple OSS Distributions 130*bbb1b6f9SApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(mach_port_space_info(mach_task_self(), &info_space, &table, 131*bbb1b6f9SApple OSS Distributions &tblcnt, &tree, &treecnt), "mach_port_space_info(): 0x%x", kr); 132*bbb1b6f9SApple OSS Distributions 133*bbb1b6f9SApple OSS Distributions for (int i = 0; i < tblcnt; i++) { 134*bbb1b6f9SApple OSS Distributions if (table[i].iin_name == exc_port1) { 135*bbb1b6f9SApple OSS Distributions exc_port1_kaddr = table[i].iin_object; 136*bbb1b6f9SApple OSS Distributions } 137*bbb1b6f9SApple OSS Distributions if (table[i].iin_name == exc_port2) { 138*bbb1b6f9SApple OSS Distributions exc_port2_kaddr = table[i].iin_object; 139*bbb1b6f9SApple OSS Distributions } 140*bbb1b6f9SApple OSS Distributions if (table[i].iin_name == exc_port3) { 141*bbb1b6f9SApple OSS Distributions exc_port3_kaddr = table[i].iin_object; 142*bbb1b6f9SApple OSS Distributions } 143*bbb1b6f9SApple OSS Distributions } 144*bbb1b6f9SApple OSS Distributions 145*bbb1b6f9SApple OSS Distributions T_LOG("exc_port_1_kaddr: 0x%x", exc_port1_kaddr); 146*bbb1b6f9SApple OSS Distributions T_LOG("exc_port_2_kaddr: 0x%x", exc_port2_kaddr); 147*bbb1b6f9SApple OSS Distributions T_LOG("exc_port_3_kaddr: 0x%x", exc_port3_kaddr); 148*bbb1b6f9SApple OSS Distributions 149*bbb1b6f9SApple OSS Distributions for (int i = 0; i < count; i++) { 150*bbb1b6f9SApple OSS Distributions T_LOG("ports_info[%d].iip_port_object: 0x%x", i, ports_info[i].iip_port_object); 151*bbb1b6f9SApple OSS Distributions 152*bbb1b6f9SApple OSS Distributions if (ports_info[i].iip_port_object == exc_port1_kaddr) { 153*bbb1b6f9SApple OSS Distributions T_EXPECT_NE(ports_info[i].iip_port_object, 0, 154*bbb1b6f9SApple OSS Distributions "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object); 155*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject, 156*bbb1b6f9SApple OSS Distributions "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object); 157*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(masks[i], EXC_MASK_GUARD, "check if mask for exc_port1 is correct"); 158*bbb1b6f9SApple OSS Distributions found_exc_port1 = true; 159*bbb1b6f9SApple OSS Distributions } 160*bbb1b6f9SApple OSS Distributions if (ports_info[i].iip_port_object == exc_port2_kaddr) { 161*bbb1b6f9SApple OSS Distributions T_EXPECT_NE(ports_info[i].iip_port_object, 0, 162*bbb1b6f9SApple OSS Distributions "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object); 163*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject, 164*bbb1b6f9SApple OSS Distributions "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object); 165*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(masks[i], EXC_MASK_RPC_ALERT, "check if mask for exc_port2 is correct"); 166*bbb1b6f9SApple OSS Distributions found_exc_port2 = true; 167*bbb1b6f9SApple OSS Distributions } 168*bbb1b6f9SApple OSS Distributions if (ports_info[i].iip_port_object == exc_port3_kaddr) { 169*bbb1b6f9SApple OSS Distributions T_EXPECT_NE(ports_info[i].iip_port_object, 0, 170*bbb1b6f9SApple OSS Distributions "on debug/kernel, port object should be non-zero: 0x%x", ports_info[i].iip_port_object); 171*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(ports_info[i].iip_receiver_object, kobject, 172*bbb1b6f9SApple OSS Distributions "receiver object should match task self kobject: 0x%x", ports_info[i].iip_receiver_object); 173*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ(masks[i], EXC_MASK_RESOURCE | EXC_MASK_BREAKPOINT | EXC_MASK_SYSCALL, "check if mask for exc_port3 is correct"); 174*bbb1b6f9SApple OSS Distributions found_exc_port3 = true; 175*bbb1b6f9SApple OSS Distributions } 176*bbb1b6f9SApple OSS Distributions } 177*bbb1b6f9SApple OSS Distributions 178*bbb1b6f9SApple OSS Distributions T_EXPECT_TRUE(found_exc_port1, "should find exc_port1"); 179*bbb1b6f9SApple OSS Distributions T_EXPECT_TRUE(found_exc_port2, "should find exc_port2"); 180*bbb1b6f9SApple OSS Distributions T_EXPECT_TRUE(found_exc_port3, "should find exc_port3"); 181*bbb1b6f9SApple OSS Distributions } 182