Skip to content

Commit 7c052fe

Browse files
committed
find proper request struct
1 parent 736706e commit 7c052fe

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

Crashlytics/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Unrelased
2-
- [fixed] Conformed to Mach IPC security restrictions. Note: This change would potentially change mach exception types we receive from kernel which might affect issue clustering result. (#15393)
1+
# Unreleased
2+
- [fixed] Conformed to Mach IPC security restrictions. (#15393)
33

44
# 12.4.0
55
- [fixed] Make set development platform APIs to chain on Crashlytics context init promise.

Crashlytics/Crashlytics/Handlers/FIRCLSMachException.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static kern_return_t FIRCLSMachExceptionDispatchMessage(FIRCLSMachExceptionReadC
215215
// inherited.
216216
mach_port_t actual_port;
217217
kern_return_t kr;
218-
task_id_token_t token = message->task_id.name;
218+
task_id_token_t token = message->task_id_token_t.name;
219219
kr = task_identity_token_get_task_port(token, TASK_FLAVOR_CONTROL, &actual_port);
220220

221221
if (kr || actual_port != mach_task_self()) {
@@ -525,7 +525,7 @@ static bool FIRCLSMachExceptionRecord(FIRCLSMachExceptionReadContext* context,
525525

526526
FIRCLSFileWriteSectionEnd(&file);
527527

528-
thread_t crashedThread;
528+
thread_t crashedThread = THREAD_NULL;
529529
FIRCLSCrashedThreadLookup(message, &crashedThread);
530530
FIRCLSSDKLog("Crashed threads: %d\n", crashedThread);
531531
FIRCLSHandler(&file, crashedThread, NULL, true);
@@ -539,35 +539,32 @@ static void FIRCLSCrashedThreadLookup(MachExceptionProtectedMessage* message, th
539539
thread_act_array_t threadList;
540540
mach_msg_type_number_t threadCount;
541541

542-
// last 64 bits include thread id info
543-
MachExceptionProtectedThreadInfo protected_thread_info = *(MachExceptionProtectedThreadInfo *) &message->thread_id;
544542
kern_return_t kr = task_threads(mach_task_self(), &threadList, &threadCount);
545-
546543
if (kr != KERN_SUCCESS) {
547544
FIRCLSSDKLogError("Failed to get threads: %d\n", kr);
548545
return;
549546
}
550-
for (int i = 0; i < threadCount; i++) {
551-
thread_t thread = threadList[i];
552-
553-
thread_basic_info_data_t basicInfo;
554-
thread_identifier_info_data_t identifierInfo;
555-
mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT;
556547

557-
kr = thread_info(thread, THREAD_IDENTIFIER_INFO, (thread_info_t)&identifierInfo, &infoCount);
548+
// Find the crashed thread.
549+
for (int i = 0; i < threadCount; i++) {
550+
thread_identifier_info_data_t identifierInfo;
551+
mach_msg_type_number_t infoCount = THREAD_IDENTIFIER_INFO_COUNT;
558552

559-
if (kr == KERN_SUCCESS) {
560-
FIRCLSSDKLog("Thread %d: Thread port: %d, thread id: %llx\n", i, thread, identifierInfo.thread_id);
553+
kr = thread_info(threadList[i], THREAD_IDENTIFIER_INFO, (thread_info_t)&identifierInfo, &infoCount);
561554

562-
if (protected_thread_info.thread_id == identifierInfo.thread_id) {
563-
FIRCLSSDKLog("Find crashed thread: %d\n", thread);
564-
*crashedThread = thread;
565-
}
555+
if (kr == KERN_SUCCESS) {
556+
FIRCLSSDKLog("Thread %d: Thread port: %d, thread id: %llx\n", i, threadList[i], identifierInfo.thread_id);
557+
if (message->thread_id == identifierInfo.thread_id) {
558+
FIRCLSSDKLog("Find crashed thread: %d\n", threadList[i]);
559+
*crashedThread = threadList[i];
560+
break;
566561
}
567-
568-
// Note: You must deallocate the send right for each thread port
569-
// to prevent port leaks, as task_threads increments the ref count.
570-
mach_port_deallocate(mach_task_self(), thread);
562+
}
563+
}
564+
for (int i = 0; i < threadCount; i++) {
565+
if (threadList[i] != *crashedThread) {
566+
mach_port_deallocate(mach_task_self(), threadList[i]);
567+
}
571568
}
572569
vm_deallocate(mach_task_self(), (vm_address_t)threadList, threadCount * sizeof(thread_t));
573570
}

Crashlytics/Crashlytics/Handlers/FIRCLSMachException.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@
2727

2828
#pragma mark Structures
2929
#pragma pack(push, 4)
30+
// run `mig -DMACH_EXC_SERVER_TASKIDTOKEN_STATE=1 mach/mach_exc.defs`
31+
// check mach_exc.h
3032
typedef struct {
3133
mach_msg_header_t head;
3234
/* start of the kernel processed data */
3335
mach_msg_body_t msgh_body;
34-
mach_msg_port_descriptor_t task_id;
35-
mach_msg_port_descriptor_t thread_id;
36+
mach_msg_port_descriptor_t task_id_token_t;
3637
/* end of the kernel processed data */
3738
NDR_record_t NDR;
39+
uint64_t thread_id;
3840
exception_type_t exception;
3941
mach_msg_type_number_t codeCnt;
4042
mach_exception_data_type_t code[EXCEPTION_CODE_MAX];
4143
mach_msg_trailer_t trailer;
4244
} MachExceptionProtectedMessage;
4345

44-
typedef struct {
45-
uint64_t pad1;
46-
uint64_t thread_id;
47-
} MachExceptionProtectedThreadInfo;
48-
4946
typedef struct {
5047
mach_msg_header_t head;
5148
NDR_record_t NDR;

0 commit comments

Comments
 (0)