+void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
+ mach_exception_data_type_t code, mach_exception_data_type_t subcode,
+ uint64_t *udata_buffer, int num_udata, void *reason)
+{
+ struct rusage_superset rup;
+
+ gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
+ rup.ri.ri_phys_footprint = 0;
+ populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
+ udata_buffer, num_udata, reason);
+}
+
+static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
+{
+ mach_exception_data_type_t code_update = *code;
+ mach_exception_data_type_t subcode_update = *subcode;
+ if (p->p_exit_reason == OS_REASON_NULL) {
+ return;
+ }
+
+ switch (p->p_exit_reason->osr_namespace) {
+ case OS_REASON_JETSAM:
+ if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
+ /* Update the code with EXC_RESOURCE code for high memory watermark */
+ EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
+ EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
+ EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(p->task)) >> 20));
+ subcode_update = 0;
+ break;
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ *code = code_update;
+ *subcode = subcode_update;
+ return;
+}
+
+mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p)
+{
+ uint64_t subcode = 0;
+
+ if (p->p_exit_reason == OS_REASON_NULL) {
+ return 0;
+ }
+
+ /* Embed first 32 bits of osr_namespace and osr_code in exception code */
+ ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
+ ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
+ return (mach_exception_data_type_t)subcode;
+}
+
+static void
+populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
+ mach_exception_data_type_t code, mach_exception_data_type_t subcode,
+ uint64_t *udata_buffer, int num_udata, os_reason_t reason)
+{
+ mach_vm_address_t uaddr = 0;
+ mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
+ exc_codes[0] = code;
+ exc_codes[1] = subcode;
+ cpu_type_t cputype;
+ struct proc_uniqidentifierinfo p_uniqidinfo;
+ struct proc_workqueueinfo pwqinfo;
+ int retval = 0;
+ uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
+ unsigned int pflags = 0;
+ uint64_t max_footprint_mb;
+ uint64_t max_footprint;
+ void *crash_info_ptr = task_get_corpseinfo(corpse_task);
+
+#if CONFIG_MEMORYSTATUS
+ int memstat_dirty_flags = 0;
+#endif
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(p->p_pid), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_pid, sizeof(p->p_pid));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
+ }
+
+ /* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
+ if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
+ }
+ }
+
+ if (KERN_SUCCESS ==
+ kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
+ proc_piduniqidentifierinfo(p, &p_uniqidinfo);
+ kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(p->p_csflags), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_csflags, sizeof(p->p_csflags));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
+ struct timeval64 t64;
+ t64.tv_sec = (int64_t)p->p_start.tv_sec;
+ t64.tv_usec = (int64_t)p->p_start.tv_usec;
+ kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
+ char *buf = (char *) kalloc(MAXPATHLEN);
+ if (buf != NULL) {
+ bzero(buf, MAXPATHLEN);
+ proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
+ kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
+ kfree(buf, MAXPATHLEN);
+ }
+ }
+
+ pflags = p->p_flag & (P_LP64 | P_SUGID);
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
+ }
+
+ cputype = cpu_type() & ~CPU_ARCH_MASK;
+ if (IS_64BIT_PROCESS(p))
+ cputype |= CPU_ARCH_ABI64;
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
+ max_footprint = get_task_phys_footprint_limit(p->task);
+ max_footprint_mb = max_footprint >> 20;
+ kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
+ }
+
+ bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
+ retval = fill_procworkqueue(p, &pwqinfo);
+ if (retval == 0) {
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
+ }
+ }
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
+ }
+
+#if CONFIG_COALITIONS
+ if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
+ uint64_t coalition_ids[COALITION_NUM_TYPES];
+ task_coalition_ids(p->task, coalition_ids);
+ kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
+ }
+#endif /* CONFIG_COALITIONS */
+
+#if CONFIG_MEMORYSTATUS
+ memstat_dirty_flags = memorystatus_dirty_get(p);
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
+ }
+#endif
+
+ if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
+ reason = p->p_exit_reason;
+ }
+ if (reason != OS_REASON_NULL) {
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
+ struct exit_reason_snapshot ers = {
+ .ers_namespace = reason->osr_namespace,
+ .ers_code = reason->osr_code,
+ .ers_flags = reason->osr_flags
+ };
+
+ kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
+ }
+
+ if (reason->osr_kcd_buf != 0) {
+ uint32_t reason_buf_size = kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
+ assert(reason_buf_size != 0);
+
+ if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
+ }
+ }
+ }
+
+ if (num_udata > 0) {
+ if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
+ sizeof(uint64_t), num_udata, &uaddr)) {
+ kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
+ }
+ }
+}
+
+/*
+ * We only parse exit reason kcdata blobs for launchd when it dies
+ * and we're going to panic.
+ *
+ * Meant to be called immediately before panicking.
+ */
+char *
+launchd_exit_reason_get_string_desc(os_reason_t exit_reason)
+{
+ kcdata_iter_t iter;
+
+ if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
+ exit_reason->osr_bufsize == 0) {
+ return NULL;
+ }
+
+ iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
+ if (!kcdata_iter_valid(iter)) {
+#if DEBUG || DEVELOPMENT
+ printf("launchd exit reason has invalid exit reason buffer\n");
+#endif
+ return NULL;
+ }
+
+ if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
+#if DEBUG || DEVELOPMENT
+ printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
+ KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
+#endif
+ return NULL;
+ }
+
+ iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
+ if (!kcdata_iter_valid(iter)) {
+ return NULL;
+ }
+
+ return (char *)kcdata_iter_payload(iter);
+}
+
+static __attribute__((noinline)) void
+launchd_crashed_panic(proc_t p, int rv)
+{
+ char *launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(p->p_exit_reason);
+
+ if (p->p_exit_reason == OS_REASON_NULL) {
+ printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
+ WTERMSIG(rv), WEXITSTATUS(rv));
+ } else {
+ printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
+ p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, launchd_exit_reason_desc ?
+ launchd_exit_reason_desc : "none");
+ }
+
+ const char *launchd_crashed_prefix_str;
+
+ if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
+ launchd_crashed_prefix_str = "LTE preinit process exited";
+ } else {
+ launchd_crashed_prefix_str = "initproc exited";
+ }
+
+#if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
+ /*
+ * For debugging purposes, generate a core file of initproc before
+ * panicking. Leave at least 300 MB free on the root volume, and ignore
+ * the process's corefile ulimit. fsync() the file to ensure it lands on disk
+ * before the panic hits.
+ */
+
+ int err;
+ uint64_t coredump_start = mach_absolute_time();
+ uint64_t coredump_end;
+ clock_sec_t tv_sec;
+ clock_usec_t tv_usec;
+ uint32_t tv_msec;
+
+
+ err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
+
+ coredump_end = mach_absolute_time();
+
+ absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
+
+ tv_msec = tv_usec / 1000;
+
+ if (err != 0) {
+ printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
+ err, (uint32_t)tv_sec, tv_msec);
+ } else {
+ printf("Generated initproc core file in %d.%03d seconds\n",
+ (uint32_t)tv_sec, tv_msec);
+ }
+#endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
+
+ sync(p, (void *)NULL, (int *)NULL);
+
+ if (p->p_exit_reason == OS_REASON_NULL) {
+ panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
+ launchd_crashed_prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""));
+ } else {
+ panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
+ ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""),
+ launchd_crashed_prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
+ launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
+ }
+}
+
+#define OS_REASON_IFLAG_USER_FAULT 0x1
+
+#define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
+
+static int
+abort_with_payload_internal(proc_t p,
+ uint32_t reason_namespace, uint64_t reason_code,
+ user_addr_t payload, uint32_t payload_size,
+ user_addr_t reason_string, uint64_t reason_flags,
+ uint32_t internal_flags)
+{
+ os_reason_t exit_reason = OS_REASON_NULL;
+ kern_return_t kr = KERN_SUCCESS;
+
+ if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
+ uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
+ memory_order_relaxed);
+ for (;;) {
+ if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
+ return EQFULL;
+ }
+ // this reloads the value in old_value
+ if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
+ &old_value, old_value + 1, memory_order_relaxed,
+ memory_order_relaxed)) {
+ break;
+ }
+ }
+ }
+
+ KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
+ p->p_pid, reason_namespace,
+ reason_code, 0, 0);
+
+ exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
+ payload, payload_size, reason_string, reason_flags);
+
+ if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
+ mach_exception_code_t code = 0;
+
+ EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
+ EXC_GUARD_ENCODE_FLAVOR(code, 0);
+ EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
+
+ if (exit_reason == OS_REASON_NULL) {
+ kr = KERN_RESOURCE_SHORTAGE;
+ } else {
+ kr = task_violated_guard(code, reason_code, exit_reason);
+ }
+ os_reason_free(exit_reason);
+ } else {
+ /*
+ * We use SIGABRT (rather than calling exit directly from here) so that
+ * the debugger can catch abort_with_{reason,payload} calls.
+ */
+ psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
+ }
+
+ switch (kr) {
+ case KERN_SUCCESS:
+ return 0;
+ case KERN_NOT_SUPPORTED:
+ return ENOTSUP;
+ case KERN_INVALID_ARGUMENT:
+ return EINVAL;
+ case KERN_RESOURCE_SHORTAGE:
+ default:
+ return EBUSY;
+ }
+}
+
+int
+abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
+ __unused void *retval)
+{
+ abort_with_payload_internal(cur_proc, args->reason_namespace,
+ args->reason_code, args->payload, args->payload_size,
+ args->reason_string, args->reason_flags, 0);
+
+ return 0;
+}
+
+int
+os_fault_with_payload(struct proc *cur_proc,
+ struct os_fault_with_payload_args *args, __unused int *retval)
+{
+ return abort_with_payload_internal(cur_proc, args->reason_namespace,
+ args->reason_code, args->payload, args->payload_size,
+ args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
+}
+
+