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