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