3333static void * FIRCLSMachExceptionServer (void * argument );
3434static bool FIRCLSMachExceptionThreadStart (FIRCLSMachExceptionReadContext * context );
3535static bool FIRCLSMachExceptionReadMessage (FIRCLSMachExceptionReadContext * context ,
36- MachExceptionProtectedMessage * message );
36+ MachExceptionMessage * message );
3737static kern_return_t FIRCLSMachExceptionDispatchMessage (FIRCLSMachExceptionReadContext * context ,
38- MachExceptionProtectedMessage * message );
38+ MachExceptionMessage * message );
3939static bool FIRCLSMachExceptionReply (FIRCLSMachExceptionReadContext * context ,
40- MachExceptionProtectedMessage * message ,
40+ MachExceptionMessage * message ,
4141 kern_return_t result );
4242static bool FIRCLSMachExceptionRegister (FIRCLSMachExceptionReadContext * context );
4343static bool FIRCLSMachExceptionUnregister (FIRCLSMachExceptionOriginalPorts * originalPorts ,
4444 exception_mask_t mask );
4545static bool FIRCLSMachExceptionRecord (FIRCLSMachExceptionReadContext * context ,
46- MachExceptionProtectedMessage * message );
47- static void FIRCLSCrashedThreadLookup (MachExceptionProtectedMessage * message , thread_t * crashedThread );
46+ MachExceptionMessage * message );
47+ static void FIRCLSCrashedThreadLookup (MachExceptionMessage * message , thread_t * crashedThread );
4848#pragma mark - Initialization
4949void FIRCLSMachExceptionInit (FIRCLSMachExceptionReadContext * context ) {
5050 if (!FIRCLSUnlinkIfExists (context -> path )) {
@@ -166,7 +166,7 @@ static void* FIRCLSMachExceptionServer(void* argument) {
166166 pthread_setname_np ("com.google.firebase.crashlytics.MachExceptionServer" );
167167
168168 while (1 ) {
169- MachExceptionProtectedMessage message ;
169+ MachExceptionMessage message ;
170170
171171 // read the exception message
172172 if (!FIRCLSMachExceptionReadMessage (context , & message )) {
@@ -188,12 +188,12 @@ static void* FIRCLSMachExceptionServer(void* argument) {
188188}
189189
190190static bool FIRCLSMachExceptionReadMessage (FIRCLSMachExceptionReadContext * context ,
191- MachExceptionProtectedMessage * message ) {
191+ MachExceptionMessage * message ) {
192192 mach_msg_return_t r ;
193193
194- memset (message , 0 , sizeof (MachExceptionProtectedMessage ));
194+ memset (message , 0 , sizeof (MachExceptionMessage ));
195195
196- r = mach_msg (& message -> head , MACH_RCV_MSG | MACH_RCV_LARGE , 0 , sizeof (MachExceptionProtectedMessage ),
196+ r = mach_msg (& message -> head , MACH_RCV_MSG | MACH_RCV_LARGE , 0 , sizeof (MachExceptionMessage ),
197197 context -> port , MACH_MSG_TIMEOUT_NONE , MACH_PORT_NULL );
198198 if (r != MACH_MSG_SUCCESS ) {
199199 FIRCLSSDKLog ("Error receiving mach_msg (%d)\n" , r );
@@ -206,17 +206,22 @@ static bool FIRCLSMachExceptionReadMessage(FIRCLSMachExceptionReadContext* conte
206206}
207207
208208static kern_return_t FIRCLSMachExceptionDispatchMessage (FIRCLSMachExceptionReadContext * context ,
209- MachExceptionProtectedMessage * message ) {
209+ MachExceptionMessage * message ) {
210210 FIRCLSSDKLog ("Mach exception: 0x%x, count: %d, code: 0x%llx 0x%llx\n" , message -> exception ,
211211 message -> codeCnt , message -> codeCnt > 0 ? message -> code [0 ] : -1 ,
212212 message -> codeCnt > 1 ? message -> code [1 ] : -1 );
213213
214214 // This will happen if a child process raises an exception, as the exception ports are
215215 // inherited.
216216 mach_port_t actual_port ;
217- kern_return_t kr ;
218- task_id_token_t token = message -> task_id .name ;
219- kr = task_identity_token_get_task_port (token , TASK_FLAVOR_CONTROL , & actual_port );
217+ kern_return_t kr = KERN_SUCCESS ;
218+ if (context -> behavior == EXCEPTION_DEFAULT ){
219+ actual_port = message -> payload_2 .name ;
220+ } else {
221+ // EXCEPTION_IDENTITY_PROTECTED
222+ task_id_token_t token = message -> payload_1 .name ;
223+ kr = task_identity_token_get_task_port (token , TASK_FLAVOR_CONTROL , & actual_port );
224+ }
220225
221226 if (kr || actual_port != mach_task_self ()) {
222227 FIRCLSSDKLog ("Mach exception task mis-match, returning failure\n" );
@@ -245,7 +250,7 @@ static kern_return_t FIRCLSMachExceptionDispatchMessage(FIRCLSMachExceptionReadC
245250}
246251
247252static bool FIRCLSMachExceptionReply (FIRCLSMachExceptionReadContext * context ,
248- MachExceptionProtectedMessage * message ,
253+ MachExceptionMessage * message ,
249254 kern_return_t result ) {
250255 MachExceptionReply reply ;
251256 mach_msg_return_t r ;
@@ -301,7 +306,7 @@ static bool FIRCLSMachExceptionRegister(FIRCLSMachExceptionReadContext* context)
301306
302307 // ORing with MACH_EXCEPTION_CODES will produce 64-bit exception data
303308 kr = task_swap_exception_ports (task , context -> mask , context -> port ,
304- EXCEPTION_IDENTITY_PROTECTED | MACH_EXCEPTION_CODES , THREAD_STATE_NONE ,
309+ context -> behavior | MACH_EXCEPTION_CODES , THREAD_STATE_NONE ,
305310 context -> originalPorts .masks , & context -> originalPorts .count ,
306311 context -> originalPorts .ports , context -> originalPorts .behaviors ,
307312 context -> originalPorts .flavors );
@@ -477,7 +482,7 @@ void FIRCLSMachExceptionNameLookup(exception_type_t number,
477482}
478483
479484static bool FIRCLSMachExceptionRecord (FIRCLSMachExceptionReadContext * context ,
480- MachExceptionProtectedMessage * message ) {
485+ MachExceptionMessage * message ) {
481486 if (!context || !message ) {
482487 return false;
483488 }
@@ -526,7 +531,12 @@ static bool FIRCLSMachExceptionRecord(FIRCLSMachExceptionReadContext* context,
526531 FIRCLSFileWriteSectionEnd (& file );
527532
528533 thread_t crashedThread ;
529- FIRCLSCrashedThreadLookup (message , & crashedThread );
534+ if (context -> behavior == EXCEPTION_DEFAULT ) {
535+ crashedThread = message -> payload_1 .name ;
536+ } else {
537+ // EXCEPTION_IDENTITY_PROTECTED
538+ FIRCLSCrashedThreadLookup (message , & crashedThread );
539+ }
530540 FIRCLSSDKLog ("Crashed threads: %d\n" , crashedThread );
531541 FIRCLSHandler (& file , crashedThread , NULL , true);
532542
@@ -535,12 +545,12 @@ static bool FIRCLSMachExceptionRecord(FIRCLSMachExceptionReadContext* context,
535545 return true;
536546}
537547
538- static void FIRCLSCrashedThreadLookup (MachExceptionProtectedMessage * message , thread_t * crashedThread ) {
548+ static void FIRCLSCrashedThreadLookup (MachExceptionMessage * message , thread_t * crashedThread ) {
539549 thread_act_array_t threadList ;
540550 mach_msg_type_number_t threadCount ;
541551
542552 // last 64 bits include thread id info
543- MachExceptionProtectedThreadInfo protected_thread_info = * (MachExceptionProtectedThreadInfo * ) & message -> thread_id ;
553+ MachExceptionProtectedThreadInfo protected_thread_info = * (MachExceptionProtectedThreadInfo * ) & message -> payload_2 ;
544554 kern_return_t kr = task_threads (mach_task_self (), & threadList , & threadCount );
545555
546556 if (kr != KERN_SUCCESS ) {
0 commit comments