xref: /xnu-8792.41.9/bsd/kern/kern_exit.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1 /*
2  * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29 /*
30  * Copyright (c) 1982, 1986, 1989, 1991, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  * (c) UNIX System Laboratories, Inc.
33  * All or some portions of this file are derived from material licensed
34  * to the University of California by American Telephone and Telegraph
35  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36  * the permission of UNIX System Laboratories, Inc.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)kern_exit.c	8.7 (Berkeley) 2/12/94
67  */
68 /*
69  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70  * support for mandatory and extensible security protections.  This notice
71  * is included in support of clause 2.2 (b) of the Apple Public License,
72  * Version 2.0.
73  */
74 
75 #include <machine/reg.h>
76 #include <machine/psl.h>
77 #include <stdatomic.h>
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/ioctl.h>
82 #include <sys/proc_internal.h>
83 #include <sys/proc.h>
84 #include <sys/kauth.h>
85 #include <sys/tty.h>
86 #include <sys/time.h>
87 #include <sys/resource.h>
88 #include <sys/kernel.h>
89 #include <sys/wait.h>
90 #include <sys/file_internal.h>
91 #include <sys/vnode_internal.h>
92 #include <sys/syslog.h>
93 #include <sys/malloc.h>
94 #include <sys/resourcevar.h>
95 #include <sys/ptrace.h>
96 #include <sys/proc_info.h>
97 #include <sys/reason.h>
98 #include <sys/_types/_timeval64.h>
99 #include <sys/user.h>
100 #include <sys/aio_kern.h>
101 #include <sys/sysproto.h>
102 #include <sys/signalvar.h>
103 #include <sys/kdebug.h>
104 #include <sys/kdebug_triage.h>
105 #include <sys/acct.h> /* acct_process */
106 #include <sys/codesign.h>
107 #include <sys/event.h> /* kevent_proc_copy_uptrs */
108 #include <sys/sdt.h>
109 #include <sys/bsdtask_info.h> /* bsd_getthreadname */
110 #include <sys/spawn.h>
111 
112 #include <security/audit/audit.h>
113 #include <bsm/audit_kevents.h>
114 
115 #include <mach/mach_types.h>
116 #include <mach/task.h>
117 #include <mach/thread_act.h>
118 
119 #include <kern/exc_resource.h>
120 #include <kern/kern_types.h>
121 #include <kern/kalloc.h>
122 #include <kern/task.h>
123 #include <corpses/task_corpse.h>
124 #include <kern/thread.h>
125 #include <kern/thread_call.h>
126 #include <kern/sched_prim.h>
127 #include <kern/assert.h>
128 #include <kern/locks.h>
129 #include <kern/policy_internal.h>
130 #include <kern/exc_guard.h>
131 #include <kern/backtrace.h>
132 
133 #include <vm/vm_protos.h>
134 #include <os/log.h>
135 #include <os/system_event_log.h>
136 
137 #include <pexpert/pexpert.h>
138 
139 #include <kdp/kdp_dyld.h>
140 
141 #if SYSV_SHM
142 #include <sys/shm_internal.h>   /* shmexit */
143 #endif /* SYSV_SHM */
144 #if CONFIG_PERSONAS
145 #include <sys/persona.h>
146 #endif /* CONFIG_PERSONAS */
147 #if CONFIG_MEMORYSTATUS
148 #include <sys/kern_memorystatus.h>
149 #endif /* CONFIG_MEMORYSTATUS */
150 #if CONFIG_DTRACE
151 /* Do not include dtrace.h, it redefines kmem_[alloc/free] */
152 void dtrace_proc_exit(proc_t p);
153 #include <sys/dtrace_ptss.h>
154 #endif /* CONFIG_DTRACE */
155 #if CONFIG_MACF
156 #include <security/mac_framework.h>
157 #include <security/mac_mach_internal.h>
158 #include <sys/syscall.h>
159 #endif /* CONFIG_MACF */
160 
161 #if CONFIG_MEMORYSTATUS
162 static void proc_memorystatus_remove(proc_t p);
163 #endif /* CONFIG_MEMORYSTATUS */
164 void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify);
165 void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
166     mach_exception_data_type_t code, mach_exception_data_type_t subcode,
167     uint64_t *udata_buffer, int num_udata, void *reason, exception_type_t etype);
168 mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p);
169 exception_type_t get_exception_from_corpse_crashinfo(kcdata_descriptor_t corpse_info);
170 __private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
171 __private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
172 static void populate_corpse_crashinfo(proc_t p, task_t corpse_task,
173     struct rusage_superset *rup, mach_exception_data_type_t code,
174     mach_exception_data_type_t subcode, uint64_t *udata_buffer,
175     int num_udata, os_reason_t reason, exception_type_t etype);
176 static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode);
177 extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, char *buffer, uint32_t buffersize, int32_t *retval);
178 extern void proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
179 extern void task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES]);
180 extern uint64_t get_task_phys_footprint_limit(task_t);
181 int proc_list_uptrs(void *p, uint64_t *udata_buffer, int size);
182 extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task);
183 
184 extern unsigned int exception_log_max_pid;
185 
186 extern void IOUserServerRecordExitReason(task_t task, os_reason_t reason);
187 
188 /*
189  * Flags for `reap_child_locked`.
190  */
191 __options_decl(reap_flags_t, uint32_t, {
192 	/*
193 	 * Parent is exiting, so the kernel is responsible for reaping children.
194 	 */
195 	REAP_DEAD_PARENT = 0x01,
196 	/*
197 	 * Childr process was re-parented to initproc.
198 	 */
199 	REAP_REPARENTED_TO_INIT = 0x02,
200 	/*
201 	 * `proc_list_lock` is held on entry.
202 	 */
203 	REAP_LOCKED = 0x04,
204 	/*
205 	 * Drop the `proc_list_lock` on return.  Note that the `proc_list_lock` will
206 	 * be dropped internally by the function regardless.
207 	 */
208 	REAP_DROP_LOCK = 0x08,
209 });
210 static void reap_child_locked(proc_t parent, proc_t child, reap_flags_t flags);
211 
212 static KALLOC_TYPE_DEFINE(zombie_zone, struct rusage_superset, KT_DEFAULT);
213 
214 /*
215  * Things which should have prototypes in headers, but don't
216  */
217 void    proc_exit(proc_t p);
218 int     wait1continue(int result);
219 int     waitidcontinue(int result);
220 kern_return_t sys_perf_notify(thread_t thread, int pid);
221 kern_return_t task_exception_notify(exception_type_t exception,
222     mach_exception_data_type_t code, mach_exception_data_type_t subcode);
223 void    delay(int);
224 
225 #if __has_feature(ptrauth_calls)
226 int exit_with_pac_exception(proc_t p, exception_type_t exception, mach_exception_code_t code,
227     mach_exception_subcode_t subcode);
228 #endif /* __has_feature(ptrauth_calls) */
229 
230 int exit_with_guard_exception(proc_t p, mach_exception_data_type_t code,
231     mach_exception_data_type_t subcode);
232 int exit_with_port_space_exception(proc_t p, mach_exception_data_type_t code,
233     mach_exception_data_type_t subcode);
234 static int exit_with_mach_exception(proc_t p, os_reason_t reason, exception_type_t exception,
235     mach_exception_code_t code, mach_exception_subcode_t subcode);
236 
237 #if DEVELOPMENT || DEBUG
238 static LCK_GRP_DECLARE(proc_exit_lpexit_spin_lock_grp, "proc_exit_lpexit_spin");
239 static LCK_MTX_DECLARE(proc_exit_lpexit_spin_lock, &proc_exit_lpexit_spin_lock_grp);
240 static pid_t proc_exit_lpexit_spin_pid = -1;            /* wakeup point */
241 static int proc_exit_lpexit_spin_pos = -1;              /* point to block */
242 static int proc_exit_lpexit_spinning = 0;
243 enum {
244 	PELS_POS_START = 0,             /* beginning of proc_exit */
245 	PELS_POS_PRE_TASK_DETACH,       /* before task/proc detach */
246 	PELS_POS_POST_TASK_DETACH,      /* after task/proc detach */
247 	PELS_POS_END,                   /* end of proc_exit */
248 	PELS_NPOS                       /* # valid values */
249 };
250 
251 /* Panic if matching processes (delimited by ',') exit on error. */
252 static TUNABLE_STR(panic_on_eexit_pcomms, 128, "panic_on_error_exit", "");
253 
254 static int
255 proc_exit_lpexit_spin_pid_sysctl SYSCTL_HANDLER_ARGS
256 {
257 #pragma unused(oidp, arg1, arg2)
258 	pid_t new_value;
259 	int changed;
260 	int error;
261 
262 	if (!PE_parse_boot_argn("enable_proc_exit_lpexit_spin", NULL, 0)) {
263 		return ENOENT;
264 	}
265 
266 	error = sysctl_io_number(req, proc_exit_lpexit_spin_pid,
267 	    sizeof(proc_exit_lpexit_spin_pid), &new_value, &changed);
268 	if (error == 0 && changed != 0) {
269 		if (new_value < -1) {
270 			return EINVAL;
271 		}
272 		lck_mtx_lock(&proc_exit_lpexit_spin_lock);
273 		proc_exit_lpexit_spin_pid = new_value;
274 		wakeup(&proc_exit_lpexit_spin_pid);
275 		proc_exit_lpexit_spinning = 0;
276 		lck_mtx_unlock(&proc_exit_lpexit_spin_lock);
277 	}
278 	return error;
279 }
280 
281 static int
282 proc_exit_lpexit_spin_pos_sysctl SYSCTL_HANDLER_ARGS
283 {
284 #pragma unused(oidp, arg1, arg2)
285 	int new_value;
286 	int changed;
287 	int error;
288 
289 	if (!PE_parse_boot_argn("enable_proc_exit_lpexit_spin", NULL, 0)) {
290 		return ENOENT;
291 	}
292 
293 	error = sysctl_io_number(req, proc_exit_lpexit_spin_pos,
294 	    sizeof(proc_exit_lpexit_spin_pos), &new_value, &changed);
295 	if (error == 0 && changed != 0) {
296 		if (new_value < -1 || new_value >= PELS_NPOS) {
297 			return EINVAL;
298 		}
299 		lck_mtx_lock(&proc_exit_lpexit_spin_lock);
300 		proc_exit_lpexit_spin_pos = new_value;
301 		wakeup(&proc_exit_lpexit_spin_pid);
302 		proc_exit_lpexit_spinning = 0;
303 		lck_mtx_unlock(&proc_exit_lpexit_spin_lock);
304 	}
305 	return error;
306 }
307 
308 static int
309 proc_exit_lpexit_spinning_sysctl SYSCTL_HANDLER_ARGS
310 {
311 #pragma unused(oidp, arg1, arg2)
312 	int new_value;
313 	int changed;
314 	int error;
315 
316 	if (!PE_parse_boot_argn("enable_proc_exit_lpexit_spin", NULL, 0)) {
317 		return ENOENT;
318 	}
319 
320 	error = sysctl_io_number(req, proc_exit_lpexit_spinning,
321 	    sizeof(proc_exit_lpexit_spinning), &new_value, &changed);
322 	if (error == 0 && changed != 0) {
323 		return EINVAL;
324 	}
325 	return error;
326 }
327 
328 SYSCTL_PROC(_debug, OID_AUTO, proc_exit_lpexit_spin_pid,
329     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
330     NULL, sizeof(pid_t),
331     proc_exit_lpexit_spin_pid_sysctl, "I", "PID to hold in proc_exit");
332 
333 SYSCTL_PROC(_debug, OID_AUTO, proc_exit_lpexit_spin_pos,
334     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
335     NULL, sizeof(int),
336     proc_exit_lpexit_spin_pos_sysctl, "I", "position to hold in proc_exit");
337 
338 SYSCTL_PROC(_debug, OID_AUTO, proc_exit_lpexit_spinning,
339     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
340     NULL, sizeof(int),
341     proc_exit_lpexit_spinning_sysctl, "I", "is a thread at requested pid/pos");
342 
343 static inline void
proc_exit_lpexit_check(pid_t pid,int pos)344 proc_exit_lpexit_check(pid_t pid, int pos)
345 {
346 	if (proc_exit_lpexit_spin_pid == pid) {
347 		bool slept = false;
348 		lck_mtx_lock(&proc_exit_lpexit_spin_lock);
349 		while (proc_exit_lpexit_spin_pid == pid &&
350 		    proc_exit_lpexit_spin_pos == pos) {
351 			if (!slept) {
352 				os_log(OS_LOG_DEFAULT,
353 				    "proc_exit_lpexit_check: Process[%d] waiting during proc_exit at pos %d as requested", pid, pos);
354 				slept = true;
355 			}
356 			proc_exit_lpexit_spinning = 1;
357 			msleep(&proc_exit_lpexit_spin_pid, &proc_exit_lpexit_spin_lock,
358 			    PWAIT, "proc_exit_lpexit_check", NULL);
359 			proc_exit_lpexit_spinning = 0;
360 		}
361 		lck_mtx_unlock(&proc_exit_lpexit_spin_lock);
362 		if (slept) {
363 			os_log(OS_LOG_DEFAULT,
364 			    "proc_exit_lpexit_check: Process[%d] driving on from pos %d", pid, pos);
365 		}
366 	}
367 }
368 #endif /* DEVELOPMENT || DEBUG */
369 
370 /*
371  * NOTE: Source and target may *NOT* overlap!
372  * XXX Should share code with bsd/dev/ppc/unix_signal.c
373  */
374 void
siginfo_user_to_user32(user_siginfo_t * in,user32_siginfo_t * out)375 siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
376 {
377 	out->si_signo   = in->si_signo;
378 	out->si_errno   = in->si_errno;
379 	out->si_code    = in->si_code;
380 	out->si_pid     = in->si_pid;
381 	out->si_uid     = in->si_uid;
382 	out->si_status  = in->si_status;
383 	out->si_addr    = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_addr);
384 	/* following cast works for sival_int because of padding */
385 	out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_value.sival_ptr);
386 	out->si_band    = (user32_long_t)in->si_band;                  /* range reduction */
387 }
388 
389 void
siginfo_user_to_user64(user_siginfo_t * in,user64_siginfo_t * out)390 siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
391 {
392 	out->si_signo   = in->si_signo;
393 	out->si_errno   = in->si_errno;
394 	out->si_code    = in->si_code;
395 	out->si_pid     = in->si_pid;
396 	out->si_uid     = in->si_uid;
397 	out->si_status  = in->si_status;
398 	out->si_addr    = in->si_addr;
399 	/* following cast works for sival_int because of padding */
400 	out->si_value.sival_ptr = in->si_value.sival_ptr;
401 	out->si_band    = in->si_band;                  /* range reduction */
402 }
403 
404 static int
copyoutsiginfo(user_siginfo_t * native,boolean_t is64,user_addr_t uaddr)405 copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr)
406 {
407 	if (is64) {
408 		user64_siginfo_t sinfo64;
409 
410 		bzero(&sinfo64, sizeof(sinfo64));
411 		siginfo_user_to_user64(native, &sinfo64);
412 		return copyout(&sinfo64, uaddr, sizeof(sinfo64));
413 	} else {
414 		user32_siginfo_t sinfo32;
415 
416 		bzero(&sinfo32, sizeof(sinfo32));
417 		siginfo_user_to_user32(native, &sinfo32);
418 		return copyout(&sinfo32, uaddr, sizeof(sinfo32));
419 	}
420 }
421 
422 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,exception_type_t etype)423 gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
424     mach_exception_data_type_t code, mach_exception_data_type_t subcode,
425     uint64_t *udata_buffer, int num_udata, void *reason, exception_type_t etype)
426 {
427 	struct rusage_superset rup;
428 
429 	gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
430 	rup.ri.ri_phys_footprint = 0;
431 	populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
432 	    udata_buffer, num_udata, reason, etype);
433 }
434 
435 static void
proc_update_corpse_exception_codes(proc_t p,mach_exception_data_type_t * code,mach_exception_data_type_t * subcode)436 proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
437 {
438 	mach_exception_data_type_t code_update = *code;
439 	mach_exception_data_type_t subcode_update = *subcode;
440 	if (p->p_exit_reason == OS_REASON_NULL) {
441 		return;
442 	}
443 
444 	switch (p->p_exit_reason->osr_namespace) {
445 	case OS_REASON_JETSAM:
446 		if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
447 			/* Update the code with EXC_RESOURCE code for high memory watermark */
448 			EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
449 			EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
450 			EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(proc_task(p))) >> 20));
451 			subcode_update = 0;
452 			break;
453 		}
454 
455 		break;
456 	default:
457 		break;
458 	}
459 
460 	*code = code_update;
461 	*subcode = subcode_update;
462 	return;
463 }
464 
465 mach_exception_data_type_t
proc_encode_exit_exception_code(proc_t p)466 proc_encode_exit_exception_code(proc_t p)
467 {
468 	uint64_t subcode = 0;
469 
470 	if (p->p_exit_reason == OS_REASON_NULL) {
471 		return 0;
472 	}
473 
474 	/* Embed first 32 bits of osr_namespace and osr_code in exception code */
475 	ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
476 	ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
477 	return (mach_exception_data_type_t)subcode;
478 }
479 
480 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,exception_type_t etype)481 populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
482     mach_exception_data_type_t code, mach_exception_data_type_t subcode,
483     uint64_t *udata_buffer, int num_udata, os_reason_t reason, exception_type_t etype)
484 {
485 	mach_vm_address_t uaddr = 0;
486 	mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
487 	exc_codes[0] = code;
488 	exc_codes[1] = subcode;
489 	cpu_type_t cputype;
490 	struct proc_uniqidentifierinfo p_uniqidinfo;
491 	struct proc_workqueueinfo pwqinfo;
492 	int retval = 0;
493 	uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
494 	bool is_corpse_fork;
495 	uint32_t csflags;
496 	unsigned int pflags = 0;
497 	uint64_t max_footprint_mb;
498 	uint64_t max_footprint;
499 
500 	uint64_t ledger_internal;
501 	uint64_t ledger_internal_compressed;
502 	uint64_t ledger_iokit_mapped;
503 	uint64_t ledger_alternate_accounting;
504 	uint64_t ledger_alternate_accounting_compressed;
505 	uint64_t ledger_purgeable_nonvolatile;
506 	uint64_t ledger_purgeable_nonvolatile_compressed;
507 	uint64_t ledger_page_table;
508 	uint64_t ledger_phys_footprint;
509 	uint64_t ledger_phys_footprint_lifetime_max;
510 	uint64_t ledger_network_nonvolatile;
511 	uint64_t ledger_network_nonvolatile_compressed;
512 	uint64_t ledger_wired_mem;
513 	uint64_t ledger_tagged_footprint;
514 	uint64_t ledger_tagged_footprint_compressed;
515 	uint64_t ledger_media_footprint;
516 	uint64_t ledger_media_footprint_compressed;
517 	uint64_t ledger_graphics_footprint;
518 	uint64_t ledger_graphics_footprint_compressed;
519 	uint64_t ledger_neural_footprint;
520 	uint64_t ledger_neural_footprint_compressed;
521 
522 	void *crash_info_ptr = task_get_corpseinfo(corpse_task);
523 
524 #if CONFIG_MEMORYSTATUS
525 	int memstat_dirty_flags = 0;
526 #endif
527 
528 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
529 		kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
530 	}
531 
532 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(pid_t), &uaddr)) {
533 		pid_t pid = proc_getpid(p);
534 		kcdata_memcpy(crash_info_ptr, uaddr, &pid, sizeof(pid));
535 	}
536 
537 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
538 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
539 	}
540 
541 	/* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
542 	if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
543 		if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
544 			kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
545 		}
546 	}
547 
548 	static_assert(sizeof(struct proc_uniqidentifierinfo) == sizeof(struct crashinfo_proc_uniqidentifierinfo));
549 	if (KERN_SUCCESS ==
550 	    kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
551 		proc_piduniqidentifierinfo(p, &p_uniqidinfo);
552 		kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
553 	}
554 
555 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
556 		kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
557 	}
558 
559 	csflags = (uint32_t)proc_getcsflags(p);
560 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(csflags), &uaddr)) {
561 		kcdata_memcpy(crash_info_ptr, uaddr, &csflags, sizeof(csflags));
562 	}
563 
564 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
565 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
566 	}
567 
568 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
569 		struct timeval64 t64;
570 		t64.tv_sec = (int64_t)p->p_start.tv_sec;
571 		t64.tv_usec = (int64_t)p->p_start.tv_usec;
572 		kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
573 	}
574 
575 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
576 		kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
577 	}
578 
579 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
580 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
581 	}
582 
583 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
584 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
585 	}
586 
587 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
588 		char *buf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
589 		proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
590 		kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
591 		zfree(ZV_NAMEI, buf);
592 	}
593 
594 	pflags = p->p_flag & (P_LP64 | P_SUGID | P_TRANSLATED);
595 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
596 		kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
597 	}
598 
599 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
600 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
601 	}
602 
603 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
604 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
605 	}
606 
607 	cputype = cpu_type() & ~CPU_ARCH_MASK;
608 	if (IS_64BIT_PROCESS(p)) {
609 		cputype |= CPU_ARCH_ABI64;
610 	} else if (proc_is64bit_data(p)) {
611 		cputype |= CPU_ARCH_ABI64_32;
612 	}
613 
614 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
615 		kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
616 	}
617 
618 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
619 		max_footprint = get_task_phys_footprint_limit(proc_task(p));
620 		max_footprint_mb = max_footprint >> 20;
621 		kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
622 	}
623 
624 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX, sizeof(ledger_phys_footprint_lifetime_max), &uaddr)) {
625 		ledger_phys_footprint_lifetime_max = get_task_phys_footprint_lifetime_max(proc_task(p));
626 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint_lifetime_max, sizeof(ledger_phys_footprint_lifetime_max));
627 	}
628 
629 	// In the forking case, the current ledger info is copied into the corpse while the original task is suspended for consistency
630 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL, sizeof(ledger_internal), &uaddr)) {
631 		ledger_internal = get_task_internal(corpse_task);
632 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal, sizeof(ledger_internal));
633 	}
634 
635 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED, sizeof(ledger_internal_compressed), &uaddr)) {
636 		ledger_internal_compressed = get_task_internal_compressed(corpse_task);
637 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal_compressed, sizeof(ledger_internal_compressed));
638 	}
639 
640 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_IOKIT_MAPPED, sizeof(ledger_iokit_mapped), &uaddr)) {
641 		ledger_iokit_mapped = get_task_iokit_mapped(corpse_task);
642 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_iokit_mapped, sizeof(ledger_iokit_mapped));
643 	}
644 
645 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING, sizeof(ledger_alternate_accounting), &uaddr)) {
646 		ledger_alternate_accounting = get_task_alternate_accounting(corpse_task);
647 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting, sizeof(ledger_alternate_accounting));
648 	}
649 
650 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED, sizeof(ledger_alternate_accounting_compressed), &uaddr)) {
651 		ledger_alternate_accounting_compressed = get_task_alternate_accounting_compressed(corpse_task);
652 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting_compressed, sizeof(ledger_alternate_accounting_compressed));
653 	}
654 
655 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE, sizeof(ledger_purgeable_nonvolatile), &uaddr)) {
656 		ledger_purgeable_nonvolatile = get_task_purgeable_nonvolatile(corpse_task);
657 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile, sizeof(ledger_purgeable_nonvolatile));
658 	}
659 
660 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED, sizeof(ledger_purgeable_nonvolatile_compressed), &uaddr)) {
661 		ledger_purgeable_nonvolatile_compressed = get_task_purgeable_nonvolatile_compressed(corpse_task);
662 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile_compressed, sizeof(ledger_purgeable_nonvolatile_compressed));
663 	}
664 
665 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PAGE_TABLE, sizeof(ledger_page_table), &uaddr)) {
666 		ledger_page_table = get_task_page_table(corpse_task);
667 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_page_table, sizeof(ledger_page_table));
668 	}
669 
670 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT, sizeof(ledger_phys_footprint), &uaddr)) {
671 		ledger_phys_footprint = get_task_phys_footprint(corpse_task);
672 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint, sizeof(ledger_phys_footprint));
673 	}
674 
675 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE, sizeof(ledger_network_nonvolatile), &uaddr)) {
676 		ledger_network_nonvolatile = get_task_network_nonvolatile(corpse_task);
677 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile, sizeof(ledger_network_nonvolatile));
678 	}
679 
680 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED, sizeof(ledger_network_nonvolatile_compressed), &uaddr)) {
681 		ledger_network_nonvolatile_compressed = get_task_network_nonvolatile_compressed(corpse_task);
682 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile_compressed, sizeof(ledger_network_nonvolatile_compressed));
683 	}
684 
685 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_WIRED_MEM, sizeof(ledger_wired_mem), &uaddr)) {
686 		ledger_wired_mem = get_task_wired_mem(corpse_task);
687 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_wired_mem, sizeof(ledger_wired_mem));
688 	}
689 
690 	bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
691 	retval = fill_procworkqueue(p, &pwqinfo);
692 	if (retval == 0) {
693 		if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
694 			kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
695 		}
696 	}
697 
698 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
699 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
700 	}
701 
702 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PERSONA_ID, sizeof(uid_t), &uaddr)) {
703 		uid_t persona_id = proc_persona_id(p);
704 		kcdata_memcpy(crash_info_ptr, uaddr, &persona_id, sizeof(persona_id));
705 	}
706 
707 #if CONFIG_COALITIONS
708 	if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
709 		uint64_t coalition_ids[COALITION_NUM_TYPES];
710 		task_coalition_ids(proc_task(p), coalition_ids);
711 		kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
712 	}
713 #endif /* CONFIG_COALITIONS */
714 
715 #if CONFIG_MEMORYSTATUS
716 	memstat_dirty_flags = memorystatus_dirty_get(p, FALSE);
717 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
718 		kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
719 	}
720 #endif
721 
722 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT_INCREASE, sizeof(p->p_memlimit_increase), &uaddr)) {
723 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memlimit_increase, sizeof(p->p_memlimit_increase));
724 	}
725 
726 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT, sizeof(ledger_tagged_footprint), &uaddr)) {
727 		ledger_tagged_footprint = get_task_tagged_footprint(corpse_task);
728 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint, sizeof(ledger_tagged_footprint));
729 	}
730 
731 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT_COMPRESSED, sizeof(ledger_tagged_footprint_compressed), &uaddr)) {
732 		ledger_tagged_footprint_compressed = get_task_tagged_footprint_compressed(corpse_task);
733 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint_compressed, sizeof(ledger_tagged_footprint_compressed));
734 	}
735 
736 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT, sizeof(ledger_media_footprint), &uaddr)) {
737 		ledger_media_footprint = get_task_media_footprint(corpse_task);
738 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint, sizeof(ledger_media_footprint));
739 	}
740 
741 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT_COMPRESSED, sizeof(ledger_media_footprint_compressed), &uaddr)) {
742 		ledger_media_footprint_compressed = get_task_media_footprint_compressed(corpse_task);
743 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint_compressed, sizeof(ledger_media_footprint_compressed));
744 	}
745 
746 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT, sizeof(ledger_graphics_footprint), &uaddr)) {
747 		ledger_graphics_footprint = get_task_graphics_footprint(corpse_task);
748 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint, sizeof(ledger_graphics_footprint));
749 	}
750 
751 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT_COMPRESSED, sizeof(ledger_graphics_footprint_compressed), &uaddr)) {
752 		ledger_graphics_footprint_compressed = get_task_graphics_footprint_compressed(corpse_task);
753 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint_compressed, sizeof(ledger_graphics_footprint_compressed));
754 	}
755 
756 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT, sizeof(ledger_neural_footprint), &uaddr)) {
757 		ledger_neural_footprint = get_task_neural_footprint(corpse_task);
758 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint, sizeof(ledger_neural_footprint));
759 	}
760 
761 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT_COMPRESSED, sizeof(ledger_neural_footprint_compressed), &uaddr)) {
762 		ledger_neural_footprint_compressed = get_task_neural_footprint_compressed(corpse_task);
763 		kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint_compressed, sizeof(ledger_neural_footprint_compressed));
764 	}
765 
766 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORYSTATUS_EFFECTIVE_PRIORITY, sizeof(p->p_memstat_effectivepriority), &uaddr)) {
767 		kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memstat_effectivepriority, sizeof(p->p_memstat_effectivepriority));
768 	}
769 
770 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_KERNEL_TRIAGE_INFO_V1, sizeof(struct kernel_triage_info_v1), &uaddr)) {
771 		char triage_strings[KDBG_TRIAGE_MAX_STRINGS][KDBG_TRIAGE_MAX_STRLEN];
772 		ktriage_extract(thread_tid(current_thread()), triage_strings, KDBG_TRIAGE_MAX_STRINGS * KDBG_TRIAGE_MAX_STRLEN);
773 		kcdata_memcpy(crash_info_ptr, uaddr, (void*) triage_strings, sizeof(struct kernel_triage_info_v1));
774 	}
775 
776 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_TASK_IS_CORPSE_FORK, sizeof(is_corpse_fork), &uaddr)) {
777 		is_corpse_fork = is_corpsefork(corpse_task);
778 		kcdata_memcpy(crash_info_ptr, uaddr, &is_corpse_fork, sizeof(is_corpse_fork));
779 	}
780 
781 	if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_TYPE, sizeof(etype), &uaddr)) {
782 		kcdata_memcpy(crash_info_ptr, uaddr, &etype, sizeof(etype));
783 	}
784 
785 	if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
786 		reason = p->p_exit_reason;
787 	}
788 	if (reason != OS_REASON_NULL) {
789 		if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
790 			struct exit_reason_snapshot ers = {
791 				.ers_namespace = reason->osr_namespace,
792 				.ers_code = reason->osr_code,
793 				.ers_flags = reason->osr_flags
794 			};
795 
796 			kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
797 		}
798 
799 		if (reason->osr_kcd_buf != 0) {
800 			uint32_t reason_buf_size = (uint32_t)kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
801 			assert(reason_buf_size != 0);
802 
803 			if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
804 				kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
805 			}
806 		}
807 	}
808 
809 	if (num_udata > 0) {
810 		if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
811 		    sizeof(uint64_t), num_udata, &uaddr)) {
812 			kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
813 		}
814 	}
815 }
816 
817 exception_type_t
get_exception_from_corpse_crashinfo(kcdata_descriptor_t corpse_info)818 get_exception_from_corpse_crashinfo(kcdata_descriptor_t corpse_info)
819 {
820 	kcdata_iter_t iter = kcdata_iter((void *)corpse_info->kcd_addr_begin,
821 	    corpse_info->kcd_length);
822 	__assert_only uint32_t type = kcdata_iter_type(iter);
823 	assert(type == KCDATA_BUFFER_BEGIN_CRASHINFO);
824 
825 	iter = kcdata_iter_find_type(iter, TASK_CRASHINFO_EXCEPTION_TYPE);
826 	exception_type_t *etype = kcdata_iter_payload(iter);
827 	return *etype;
828 }
829 
830 /*
831  * Collect information required for generating lightwight corpse for current
832  * task, which can be terminating.
833  */
834 kern_return_t
current_thread_collect_backtrace_info(kcdata_descriptor_t * new_desc,exception_type_t etype,mach_exception_data_t code,mach_msg_type_number_t codeCnt,void * reasonp)835 current_thread_collect_backtrace_info(
836 	kcdata_descriptor_t *new_desc,
837 	exception_type_t etype,
838 	mach_exception_data_t code,
839 	mach_msg_type_number_t codeCnt,
840 	void *reasonp)
841 {
842 	kcdata_descriptor_t kcdata;
843 	kern_return_t kr;
844 	int frame_count = 0, max_frames = 100;
845 	mach_vm_address_t uuid_info_addr = 0;
846 	uint32_t uuid_info_count         = 0;
847 	uint32_t btinfo_flag             = 0;
848 	mach_vm_address_t btinfo_flag_addr = 0, kaddr = 0;
849 	natural_t alloc_size = BTINFO_ALLOCATION_SIZE;
850 	mach_msg_type_number_t th_info_count = THREAD_IDENTIFIER_INFO_COUNT;
851 	thread_identifier_info_data_t th_info;
852 	char threadname[MAXTHREADNAMESIZE];
853 	void *btdata_kernel = NULL;
854 	typedef uintptr_t user_btframe_t __kernel_data_semantics;
855 	user_btframe_t *btframes = NULL;
856 	os_reason_t reason = (os_reason_t)reasonp;
857 	struct backtrace_user_info info = BTUINFO_INIT;
858 	struct rusage_superset rup;
859 	uint32_t platform;
860 
861 	task_t task = current_task();
862 	proc_t p = current_proc();
863 
864 	bool has_64bit_addr = task_get_64bit_addr(current_task());
865 	bool has_64bit_data = task_get_64bit_data(current_task());
866 
867 	if (new_desc == NULL) {
868 		return KERN_INVALID_ARGUMENT;
869 	}
870 
871 	/* First, collect backtrace frames */
872 	btframes = kalloc_data(max_frames * sizeof(btframes[0]), Z_WAITOK | Z_ZERO);
873 	if (!btframes) {
874 		return KERN_RESOURCE_SHORTAGE;
875 	}
876 
877 	frame_count = backtrace_user(btframes, max_frames, NULL, &info);
878 	if (info.btui_error || frame_count == 0) {
879 		kfree_data(btframes, max_frames * sizeof(btframes[0]));
880 		return KERN_FAILURE;
881 	}
882 
883 	if ((info.btui_info & BTI_TRUNCATED) != 0) {
884 		btinfo_flag |= TASK_BTINFO_FLAG_BT_TRUNCATED;
885 	}
886 
887 	/* Captured in kcdata descriptor below */
888 	btdata_kernel = kalloc_data(alloc_size, Z_WAITOK | Z_ZERO);
889 	if (!btdata_kernel) {
890 		kfree_data(btframes, max_frames * sizeof(btframes[0]));
891 		return KERN_RESOURCE_SHORTAGE;
892 	}
893 
894 	kcdata = task_btinfo_alloc_init((mach_vm_address_t)btdata_kernel, alloc_size);
895 	if (!kcdata) {
896 		kfree_data(btdata_kernel, alloc_size);
897 		kfree_data(btframes, max_frames * sizeof(btframes[0]));
898 		return KERN_RESOURCE_SHORTAGE;
899 	}
900 
901 	/* First reserve space in kcdata blob for the btinfo flag fields */
902 	if (KERN_SUCCESS != kcdata_get_memory_addr(kcdata, TASK_BTINFO_FLAGS,
903 	    sizeof(uint32_t), &btinfo_flag_addr)) {
904 		kfree_data(btdata_kernel, alloc_size);
905 		kfree_data(btframes, max_frames * sizeof(btframes[0]));
906 		kcdata_memory_destroy(kcdata);
907 		return KERN_RESOURCE_SHORTAGE;
908 	}
909 
910 	if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(kcdata,
911 	    (has_64bit_addr ? TASK_BTINFO_BACKTRACE64 : TASK_BTINFO_BACKTRACE),
912 	    sizeof(uintptr_t), frame_count, &kaddr)) {
913 		kcdata_memcpy(kcdata, kaddr, btframes, sizeof(uintptr_t) * frame_count);
914 	}
915 
916 #if __LP64__
917 	/* We only support async stacks on 64-bit kernels */
918 	frame_count = 0;
919 
920 	if (info.btui_async_frame_addr != 0) {
921 		if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_ASYNC_START_INDEX,
922 		    sizeof(uint32_t), &kaddr)) {
923 			uint32_t idx = info.btui_async_start_index;
924 			kcdata_memcpy(kcdata, kaddr, &idx, sizeof(uint32_t));
925 		}
926 		struct backtrace_control ctl = {
927 			.btc_frame_addr = info.btui_async_frame_addr,
928 			.btc_addr_offset = BTCTL_ASYNC_ADDR_OFFSET,
929 		};
930 
931 		info = BTUINFO_INIT;
932 		frame_count = backtrace_user(btframes, max_frames, &ctl, &info);
933 		if (info.btui_error == 0 && frame_count > 0) {
934 			if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(kcdata,
935 			    TASK_BTINFO_ASYNC_BACKTRACE64,
936 			    sizeof(uintptr_t), frame_count, &kaddr)) {
937 				kcdata_memcpy(kcdata, kaddr, btframes, sizeof(uintptr_t) * frame_count);
938 			}
939 		}
940 
941 		if ((info.btui_info & BTI_TRUNCATED) != 0) {
942 			btinfo_flag |= TASK_BTINFO_FLAG_ASYNC_BT_TRUNCATED;
943 		}
944 	}
945 #endif
946 
947 	/* Backtrace collection done, free the frames buffer */
948 	kfree_data(btframes, max_frames * sizeof(btframes[0]));
949 	btframes = NULL;
950 
951 	/* Next, suspend the task briefly and collect image load infos */
952 	task_suspend_internal(task);
953 
954 	/* all_image_info struct is ABI, in agreement with address width */
955 	if (has_64bit_addr) {
956 		struct user64_dyld_all_image_infos task_image_infos = {};
957 		struct btinfo_sc_load_info64 sc_info;
958 		(void)copyin((user_addr_t)task_get_all_image_info_addr(task), &task_image_infos,
959 		    sizeof(struct user64_dyld_all_image_infos));
960 		uuid_info_count = (uint32_t)task_image_infos.uuidArrayCount;
961 		uuid_info_addr = task_image_infos.uuidArray;
962 
963 		sc_info.sharedCacheSlide = task_image_infos.sharedCacheSlide;
964 		sc_info.sharedCacheBaseAddress = task_image_infos.sharedCacheBaseAddress;
965 		memcpy(&sc_info.sharedCacheUUID, &task_image_infos.sharedCacheUUID,
966 		    sizeof(task_image_infos.sharedCacheUUID));
967 
968 		if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata,
969 		    TASK_BTINFO_SC_LOADINFO64, sizeof(sc_info), &kaddr)) {
970 			kcdata_memcpy(kcdata, kaddr, &sc_info, sizeof(sc_info));
971 		}
972 	} else {
973 		struct user32_dyld_all_image_infos task_image_infos = {};
974 		struct btinfo_sc_load_info sc_info;
975 		(void)copyin((user_addr_t)task_get_all_image_info_addr(task), &task_image_infos,
976 		    sizeof(struct user32_dyld_all_image_infos));
977 		uuid_info_count = task_image_infos.uuidArrayCount;
978 		uuid_info_addr = task_image_infos.uuidArray;
979 
980 		sc_info.sharedCacheSlide = task_image_infos.sharedCacheSlide;
981 		sc_info.sharedCacheBaseAddress = task_image_infos.sharedCacheBaseAddress;
982 		memcpy(&sc_info.sharedCacheUUID, &task_image_infos.sharedCacheUUID,
983 		    sizeof(task_image_infos.sharedCacheUUID));
984 
985 		if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata,
986 		    TASK_BTINFO_SC_LOADINFO, sizeof(sc_info), &kaddr)) {
987 			kcdata_memcpy(kcdata, kaddr, &sc_info, sizeof(sc_info));
988 		}
989 	}
990 
991 	if (!uuid_info_addr) {
992 		/*
993 		 * Can happen when we catch dyld in the middle of updating
994 		 * this data structure, or copyin of all_image_info struct failed.
995 		 */
996 		task_resume_internal(task);
997 		kfree_data(btdata_kernel, alloc_size);
998 		kcdata_memory_destroy(kcdata);
999 		return KERN_MEMORY_ERROR;
1000 	}
1001 
1002 	if (uuid_info_count > 0) {
1003 		uint32_t uuid_info_size = (uint32_t)(has_64bit_addr ?
1004 		    sizeof(struct user64_dyld_uuid_info) : sizeof(struct user32_dyld_uuid_info));
1005 
1006 		if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(kcdata,
1007 		    (has_64bit_addr ? TASK_BTINFO_DYLD_LOADINFO64 : TASK_BTINFO_DYLD_LOADINFO),
1008 		    uuid_info_size, uuid_info_count, &kaddr)) {
1009 			if (copyin((user_addr_t)uuid_info_addr, (void *)kaddr, uuid_info_size * uuid_info_count)) {
1010 				task_resume_internal(task);
1011 				kfree_data(btdata_kernel, alloc_size);
1012 				kcdata_memory_destroy(kcdata);
1013 				return KERN_MEMORY_ERROR;
1014 			}
1015 		}
1016 	}
1017 
1018 	task_resume_internal(task);
1019 
1020 	/* Next, collect all other information */
1021 	thread_flavor_t tsflavor;
1022 	mach_msg_type_number_t tscount;
1023 
1024 #if defined(__x86_64__) || defined(__i386__)
1025 	tsflavor = x86_THREAD_STATE;      /* unified */
1026 	tscount  = x86_THREAD_STATE_COUNT;
1027 #else
1028 	tsflavor = ARM_THREAD_STATE;      /* unified */
1029 	tscount  = ARM_UNIFIED_THREAD_STATE_COUNT;
1030 #endif
1031 
1032 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_THREAD_STATE,
1033 	    sizeof(struct btinfo_thread_state_data_t) + sizeof(int) * tscount, &kaddr)) {
1034 		struct btinfo_thread_state_data_t *bt_thread_state = (struct btinfo_thread_state_data_t *)kaddr;
1035 		bt_thread_state->flavor = tsflavor;
1036 		bt_thread_state->count = tscount;
1037 		/* variable-sized tstate array follows */
1038 
1039 		kr = thread_getstatus_to_user(current_thread(), bt_thread_state->flavor,
1040 		    (thread_state_t)&bt_thread_state->tstate, &bt_thread_state->count, TSSF_FLAGS_NONE);
1041 		if (kr != KERN_SUCCESS) {
1042 			bzero((void *)kaddr, sizeof(struct btinfo_thread_state_data_t) + sizeof(int) * tscount);
1043 			if (kr == KERN_TERMINATED) {
1044 				btinfo_flag |= TASK_BTINFO_FLAG_TASK_TERMINATED;
1045 			}
1046 		}
1047 	}
1048 
1049 #if defined(__x86_64__) || defined(__i386__)
1050 	tsflavor = x86_EXCEPTION_STATE;       /* unified */
1051 	tscount  = x86_EXCEPTION_STATE_COUNT;
1052 #else
1053 #if defined(__arm64__)
1054 	if (has_64bit_data) {
1055 		tsflavor = ARM_EXCEPTION_STATE64;
1056 		tscount  = ARM_EXCEPTION_STATE64_COUNT;
1057 	} else
1058 #endif /* defined(__arm64__) */
1059 	{
1060 		tsflavor = ARM_EXCEPTION_STATE;
1061 		tscount  = ARM_EXCEPTION_STATE_COUNT;
1062 	}
1063 #endif /* defined(__x86_64__) || defined(__i386__) */
1064 
1065 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_THREAD_EXCEPTION_STATE,
1066 	    sizeof(struct btinfo_thread_state_data_t) + sizeof(int) * tscount, &kaddr)) {
1067 		struct btinfo_thread_state_data_t *bt_thread_state = (struct btinfo_thread_state_data_t *)kaddr;
1068 		bt_thread_state->flavor = tsflavor;
1069 		bt_thread_state->count = tscount;
1070 		/* variable-sized tstate array follows */
1071 
1072 		kr = thread_getstatus_to_user(current_thread(), bt_thread_state->flavor,
1073 		    (thread_state_t)&bt_thread_state->tstate, &bt_thread_state->count, TSSF_FLAGS_NONE);
1074 		if (kr != KERN_SUCCESS) {
1075 			bzero((void *)kaddr, sizeof(struct btinfo_thread_state_data_t) + sizeof(int) * tscount);
1076 			if (kr == KERN_TERMINATED) {
1077 				btinfo_flag |= TASK_BTINFO_FLAG_TASK_TERMINATED;
1078 			}
1079 		}
1080 	}
1081 
1082 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PID, sizeof(pid_t), &kaddr)) {
1083 		pid_t pid = proc_getpid(p);
1084 		kcdata_memcpy(kcdata, kaddr, &pid, sizeof(pid));
1085 	}
1086 
1087 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PPID, sizeof(p->p_ppid), &kaddr)) {
1088 		kcdata_memcpy(kcdata, kaddr, &p->p_ppid, sizeof(p->p_ppid));
1089 	}
1090 
1091 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PROC_NAME, sizeof(p->p_comm), &kaddr)) {
1092 		kcdata_memcpy(kcdata, kaddr, &p->p_comm, sizeof(p->p_comm));
1093 	}
1094 
1095 #if CONFIG_COALITIONS
1096 	if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(kcdata, TASK_BTINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &kaddr)) {
1097 		uint64_t coalition_ids[COALITION_NUM_TYPES];
1098 		task_coalition_ids(proc_task(p), coalition_ids);
1099 		kcdata_memcpy(kcdata, kaddr, coalition_ids, sizeof(coalition_ids));
1100 	}
1101 #endif /* CONFIG_COALITIONS */
1102 
1103 	/* V0 is sufficient for ReportCrash */
1104 	gather_rusage_info(current_proc(), &rup.ri, RUSAGE_INFO_V0);
1105 	rup.ri.ri_phys_footprint = 0;
1106 	/* Soft crash, proc did not exit */
1107 	rup.ri.ri_proc_exit_abstime = 0;
1108 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_RUSAGE_INFO, sizeof(struct rusage_info_v0), &kaddr)) {
1109 		kcdata_memcpy(kcdata, kaddr, &rup.ri, sizeof(struct rusage_info_v0));
1110 	}
1111 
1112 	platform = proc_platform(current_proc());
1113 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PLATFORM, sizeof(platform), &kaddr)) {
1114 		kcdata_memcpy(kcdata, kaddr, &platform, sizeof(platform));
1115 	}
1116 
1117 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PROC_PATH, MAXPATHLEN, &kaddr)) {
1118 		char *buf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
1119 		proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, NULL);
1120 		kcdata_memcpy(kcdata, kaddr, buf, MAXPATHLEN);
1121 		zfree(ZV_NAMEI, buf);
1122 	}
1123 
1124 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_UID, sizeof(p->p_uid), &kaddr)) {
1125 		kcdata_memcpy(kcdata, kaddr, &p->p_uid, sizeof(p->p_uid));
1126 	}
1127 
1128 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_GID, sizeof(p->p_gid), &kaddr)) {
1129 		kcdata_memcpy(kcdata, kaddr, &p->p_gid, sizeof(p->p_gid));
1130 	}
1131 
1132 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_PROC_FLAGS, sizeof(unsigned int), &kaddr)) {
1133 		unsigned int pflags = p->p_flag & (P_LP64 | P_SUGID | P_TRANSLATED);
1134 		kcdata_memcpy(kcdata, kaddr, &pflags, sizeof(pflags));
1135 	}
1136 
1137 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_CPUTYPE, sizeof(cpu_type_t), &kaddr)) {
1138 		cpu_type_t cputype = cpu_type() & ~CPU_ARCH_MASK;
1139 		if (has_64bit_addr) {
1140 			cputype |= CPU_ARCH_ABI64;
1141 		} else if (has_64bit_data) {
1142 			cputype |= CPU_ARCH_ABI64_32;
1143 		}
1144 		kcdata_memcpy(kcdata, kaddr, &cputype, sizeof(cpu_type_t));
1145 	}
1146 
1147 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_EXCEPTION_TYPE, sizeof(etype), &kaddr)) {
1148 		kcdata_memcpy(kcdata, kaddr, &etype, sizeof(etype));
1149 	}
1150 
1151 	assert(codeCnt <= EXCEPTION_CODE_MAX);
1152 
1153 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_EXCEPTION_CODES,
1154 	    sizeof(mach_exception_code_t) * codeCnt, &kaddr)) {
1155 		kcdata_memcpy(kcdata, kaddr, code, sizeof(mach_exception_code_t) * codeCnt);
1156 	}
1157 
1158 	if (reason != OS_REASON_NULL) {
1159 		if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &kaddr)) {
1160 			struct exit_reason_snapshot ers = {
1161 				.ers_namespace = reason->osr_namespace,
1162 				.ers_code = reason->osr_code,
1163 				.ers_flags = reason->osr_flags
1164 			};
1165 
1166 			kcdata_memcpy(kcdata, kaddr, &ers, sizeof(ers));
1167 		}
1168 
1169 		if (reason->osr_kcd_buf != 0) {
1170 			uint32_t reason_buf_size = (uint32_t)kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
1171 			assert(reason_buf_size != 0);
1172 
1173 			if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &kaddr)) {
1174 				kcdata_memcpy(kcdata, kaddr, reason->osr_kcd_buf, reason_buf_size);
1175 			}
1176 		}
1177 	}
1178 
1179 	threadname[0] = '\0';
1180 	if (KERN_SUCCESS == kcdata_get_memory_addr(kcdata, TASK_BTINFO_THREAD_NAME,
1181 	    sizeof(threadname), &kaddr)) {
1182 		bsd_getthreadname(get_bsdthread_info(current_thread()), threadname);
1183 		kcdata_memcpy(kcdata, kaddr, threadname, sizeof(threadname));
1184 	}
1185 
1186 	kr = thread_info(current_thread(), THREAD_IDENTIFIER_INFO, (thread_info_t)&th_info, &th_info_count);
1187 	if (kr == KERN_TERMINATED) {
1188 		btinfo_flag |= TASK_BTINFO_FLAG_TASK_TERMINATED;
1189 	}
1190 
1191 
1192 	kern_return_t last_kr = kcdata_get_memory_addr(kcdata, TASK_BTINFO_THREAD_ID,
1193 	    sizeof(uint64_t), &kaddr);
1194 
1195 	/*
1196 	 * If the last kcdata_get_memory_addr() failed (unlikely), signal to exception
1197 	 * handler (ReportCrash) that lw corpse collection ran out of space and the
1198 	 * result is incomplete.
1199 	 */
1200 	if (last_kr != KERN_SUCCESS) {
1201 		btinfo_flag |= TASK_BTINFO_FLAG_KCDATA_INCOMPLETE;
1202 	}
1203 
1204 	if (KERN_SUCCESS == kr && KERN_SUCCESS == last_kr) {
1205 		kcdata_memcpy(kcdata, kaddr, &th_info.thread_id, sizeof(uint64_t));
1206 	}
1207 
1208 	/* Lastly, copy the flags to the address we reserved at the beginning. */
1209 	kcdata_memcpy(kcdata, btinfo_flag_addr, &btinfo_flag, sizeof(uint32_t));
1210 
1211 	*new_desc = kcdata;
1212 
1213 	return KERN_SUCCESS;
1214 }
1215 
1216 /*
1217  * We only parse exit reason kcdata blobs for critical process before they die
1218  * and we're going to panic or for opt-in, limited diagnostic tools.
1219  *
1220  * Meant to be called immediately before panicking or limited diagnostic
1221  * scenarios.
1222  */
1223 char *
exit_reason_get_string_desc(os_reason_t exit_reason)1224 exit_reason_get_string_desc(os_reason_t exit_reason)
1225 {
1226 	kcdata_iter_t iter;
1227 
1228 	if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
1229 	    exit_reason->osr_bufsize == 0) {
1230 		return NULL;
1231 	}
1232 
1233 	iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
1234 	if (!kcdata_iter_valid(iter)) {
1235 #if DEBUG || DEVELOPMENT
1236 		printf("exit reason has invalid exit reason buffer\n");
1237 #endif
1238 		return NULL;
1239 	}
1240 
1241 	if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
1242 #if DEBUG || DEVELOPMENT
1243 		printf("exit reason buffer type mismatch, expected %d got %d\n",
1244 		    KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
1245 #endif
1246 		return NULL;
1247 	}
1248 
1249 	iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
1250 	if (!kcdata_iter_valid(iter)) {
1251 		return NULL;
1252 	}
1253 
1254 	return (char *)kcdata_iter_payload(iter);
1255 }
1256 
1257 static int initproc_spawned = 0;
1258 
1259 static int
sysctl_initproc_spawned(struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)1260 sysctl_initproc_spawned(struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
1261 {
1262 	if (req->newptr != 0 && (proc_getpid(req->p) != 1 || initproc_spawned != 0)) {
1263 		// Can only ever be set by launchd, and only once at boot
1264 		return EPERM;
1265 	}
1266 	return sysctl_handle_int(oidp, &initproc_spawned, 0, req);
1267 }
1268 
1269 SYSCTL_PROC(_kern, OID_AUTO, initproc_spawned,
1270     CTLFLAG_RW | CTLFLAG_KERN | CTLTYPE_INT | CTLFLAG_LOCKED, 0, 0,
1271     sysctl_initproc_spawned, "I", "Boolean indicator that launchd has reached main");
1272 
1273 #if DEVELOPMENT || DEBUG
1274 
1275 /* disable user faults */
1276 static TUNABLE(bool, bootarg_disable_user_faults, "-disable_user_faults", false);
1277 #endif /* DEVELOPMENT || DEBUG */
1278 
1279 #define OS_REASON_IFLAG_USER_FAULT 0x1
1280 
1281 #define OS_REASON_TOTAL_USER_FAULTS_PER_PROC  5
1282 
1283 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)1284 abort_with_payload_internal(proc_t p,
1285     uint32_t reason_namespace, uint64_t reason_code,
1286     user_addr_t payload, uint32_t payload_size,
1287     user_addr_t reason_string, uint64_t reason_flags,
1288     uint32_t internal_flags)
1289 {
1290 	os_reason_t exit_reason = OS_REASON_NULL;
1291 	kern_return_t kr = KERN_SUCCESS;
1292 
1293 	if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
1294 		uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
1295 		    memory_order_relaxed);
1296 
1297 #if DEVELOPMENT || DEBUG
1298 		if (bootarg_disable_user_faults) {
1299 			return EQFULL;
1300 		}
1301 #endif /* DEVELOPMENT || DEBUG */
1302 
1303 		for (;;) {
1304 			if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
1305 				return EQFULL;
1306 			}
1307 			// this reloads the value in old_value
1308 			if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
1309 			    &old_value, old_value + 1, memory_order_relaxed,
1310 			    memory_order_relaxed)) {
1311 				break;
1312 			}
1313 		}
1314 	}
1315 
1316 	KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
1317 	    proc_getpid(p), reason_namespace,
1318 	    reason_code, 0, 0);
1319 
1320 	exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
1321 	    payload, payload_size, reason_string, reason_flags | OS_REASON_FLAG_ABORT);
1322 
1323 	if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
1324 		mach_exception_code_t code = 0;
1325 
1326 		EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
1327 		EXC_GUARD_ENCODE_FLAVOR(code, 0);
1328 		EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
1329 
1330 		if (exit_reason == OS_REASON_NULL) {
1331 			kr = KERN_RESOURCE_SHORTAGE;
1332 		} else {
1333 			kr = task_violated_guard(code, reason_code, exit_reason, TRUE);
1334 		}
1335 		os_reason_free(exit_reason);
1336 	} else {
1337 		/*
1338 		 * We use SIGABRT (rather than calling exit directly from here) so that
1339 		 * the debugger can catch abort_with_{reason,payload} calls.
1340 		 */
1341 		psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
1342 	}
1343 
1344 	switch (kr) {
1345 	case KERN_SUCCESS:
1346 		return 0;
1347 	case KERN_NOT_SUPPORTED:
1348 		return ENOTSUP;
1349 	case KERN_INVALID_ARGUMENT:
1350 		return EINVAL;
1351 	case KERN_RESOURCE_SHORTAGE:
1352 	default:
1353 		return EBUSY;
1354 	}
1355 }
1356 
1357 int
abort_with_payload(struct proc * cur_proc,struct abort_with_payload_args * args,__unused void * retval)1358 abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
1359     __unused void *retval)
1360 {
1361 	abort_with_payload_internal(cur_proc, args->reason_namespace,
1362 	    args->reason_code, args->payload, args->payload_size,
1363 	    args->reason_string, args->reason_flags, 0);
1364 
1365 	return 0;
1366 }
1367 
1368 int
os_fault_with_payload(struct proc * cur_proc,struct os_fault_with_payload_args * args,__unused int * retval)1369 os_fault_with_payload(struct proc *cur_proc,
1370     struct os_fault_with_payload_args *args, __unused int *retval)
1371 {
1372 	return abort_with_payload_internal(cur_proc, args->reason_namespace,
1373 	           args->reason_code, args->payload, args->payload_size,
1374 	           args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
1375 }
1376 
1377 
1378 /*
1379  * exit --
1380  *	Death of process.
1381  */
1382 __attribute__((noreturn))
1383 void
exit(proc_t p,struct exit_args * uap,int * retval)1384 exit(proc_t p, struct exit_args *uap, int *retval)
1385 {
1386 	p->p_xhighbits = ((uint32_t)(uap->rval) & 0xFF000000) >> 24;
1387 	exit1(p, W_EXITCODE((uint32_t)uap->rval, 0), retval);
1388 
1389 	thread_exception_return();
1390 	/* NOTREACHED */
1391 	while (TRUE) {
1392 		thread_block(THREAD_CONTINUE_NULL);
1393 	}
1394 	/* NOTREACHED */
1395 }
1396 
1397 /*
1398  * Exit: deallocate address space and other resources, change proc state
1399  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
1400  * status and rusage for wait().  Check for child processes and orphan them.
1401  */
1402 int
exit1(proc_t p,int rv,int * retval)1403 exit1(proc_t p, int rv, int *retval)
1404 {
1405 	return exit1_internal(p, rv, retval, FALSE, TRUE, 0);
1406 }
1407 
1408 int
exit1_internal(proc_t p,int rv,int * retval,boolean_t thread_can_terminate,boolean_t perf_notify,int jetsam_flags)1409 exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
1410     int jetsam_flags)
1411 {
1412 	return exit_with_reason(p, rv, retval, thread_can_terminate, perf_notify, jetsam_flags, OS_REASON_NULL);
1413 }
1414 
1415 /*
1416  * NOTE: exit_with_reason drops a reference on the passed exit_reason
1417  */
1418 int
exit_with_reason(proc_t p,int rv,int * retval,boolean_t thread_can_terminate,boolean_t perf_notify,int jetsam_flags,struct os_reason * exit_reason)1419 exit_with_reason(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
1420     int jetsam_flags, struct os_reason *exit_reason)
1421 {
1422 	thread_t self = current_thread();
1423 	struct task *task = proc_task(p);
1424 	struct uthread *ut;
1425 	int error = 0;
1426 	bool proc_exiting = false;
1427 
1428 #if DEVELOPMENT || DEBUG
1429 	/*
1430 	 * Debug boot-arg: panic here if matching process is exiting with non-zero code.
1431 	 * Example usage: panic_on_error_exit=launchd,logd,watchdogd
1432 	 */
1433 	if (rv && strnstr(panic_on_eexit_pcomms, p->p_comm, sizeof(panic_on_eexit_pcomms))) {
1434 		panic("%s: Process %s with pid %d exited on error with code 0x%x.",
1435 		    __FUNCTION__, p->p_comm, proc_getpid(p), rv);
1436 	}
1437 #endif
1438 
1439 	/*
1440 	 * If a thread in this task has already
1441 	 * called exit(), then halt any others
1442 	 * right here.
1443 	 */
1444 
1445 	ut = get_bsdthread_info(self);
1446 	(void)retval;
1447 
1448 	/*
1449 	 * The parameter list of audit_syscall_exit() was augmented to
1450 	 * take the Darwin syscall number as the first parameter,
1451 	 * which is currently required by mac_audit_postselect().
1452 	 */
1453 
1454 	/*
1455 	 * The BSM token contains two components: an exit status as passed
1456 	 * to exit(), and a return value to indicate what sort of exit it
1457 	 * was.  The exit status is WEXITSTATUS(rv), but it's not clear
1458 	 * what the return value is.
1459 	 */
1460 	AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
1461 	/*
1462 	 * TODO: what to audit here when jetsam calls exit and the uthread,
1463 	 * 'ut' does not belong to the proc, 'p'.
1464 	 */
1465 	AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
1466 
1467 	DTRACE_PROC1(exit, int, CLD_EXITED);
1468 
1469 	/* mark process is going to exit and pull out of DBG/disk throttle */
1470 	/* TODO: This should be done after becoming exit thread */
1471 	proc_set_task_policy(proc_task(p), TASK_POLICY_ATTRIBUTE,
1472 	    TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
1473 
1474 	proc_lock(p);
1475 	error = proc_transstart(p, 1, (jetsam_flags ? 1 : 0));
1476 	if (error == EDEADLK) {
1477 		/*
1478 		 * If proc_transstart() returns EDEADLK, then another thread
1479 		 * is either exec'ing or exiting. Return an error and allow
1480 		 * the other thread to continue.
1481 		 */
1482 		proc_unlock(p);
1483 		os_reason_free(exit_reason);
1484 		if (current_proc() == p) {
1485 			if (p->exit_thread == self) {
1486 				panic("exit_thread failed to exit");
1487 			}
1488 
1489 			if (thread_can_terminate) {
1490 				thread_exception_return();
1491 			}
1492 		}
1493 
1494 		return error;
1495 	}
1496 
1497 	proc_exiting = !!(p->p_lflag & P_LEXIT);
1498 
1499 	while (proc_exiting || p->exit_thread != self) {
1500 		if (proc_exiting || sig_try_locked(p) <= 0) {
1501 			proc_transend(p, 1);
1502 			os_reason_free(exit_reason);
1503 
1504 			if (get_threadtask(self) != task) {
1505 				proc_unlock(p);
1506 				return 0;
1507 			}
1508 			proc_unlock(p);
1509 
1510 			thread_terminate(self);
1511 			if (!thread_can_terminate) {
1512 				return 0;
1513 			}
1514 
1515 			thread_exception_return();
1516 			/* NOTREACHED */
1517 		}
1518 		sig_lock_to_exit(p);
1519 	}
1520 
1521 	if (exit_reason != OS_REASON_NULL) {
1522 		KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_COMMIT) | DBG_FUNC_NONE,
1523 		    proc_getpid(p), exit_reason->osr_namespace,
1524 		    exit_reason->osr_code, 0, 0);
1525 	}
1526 
1527 	assert(p->p_exit_reason == OS_REASON_NULL);
1528 	p->p_exit_reason = exit_reason;
1529 
1530 	p->p_lflag |= P_LEXIT;
1531 	p->p_xstat = rv;
1532 	p->p_lflag |= jetsam_flags;
1533 
1534 	proc_transend(p, 1);
1535 	proc_unlock(p);
1536 
1537 	proc_prepareexit(p, rv, perf_notify);
1538 
1539 	/* Last thread to terminate will call proc_exit() */
1540 	task_terminate_internal(task);
1541 
1542 	return 0;
1543 }
1544 
1545 #if CONFIG_MEMORYSTATUS
1546 /*
1547  * Remove this process from jetsam bands for freezing or exiting. Note this will block, if the process
1548  * is currently being frozen.
1549  * The proc_list_lock is held by the caller.
1550  * NB: If the process should be ineligible for future freezing or jetsaming the caller should first set
1551  * the p_refcount P_REF_DEAD bit.
1552  */
1553 static void
proc_memorystatus_remove(proc_t p)1554 proc_memorystatus_remove(proc_t p)
1555 {
1556 	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
1557 	while (memorystatus_remove(p) == EAGAIN) {
1558 		os_log(OS_LOG_DEFAULT, "memorystatus_remove: Process[%d] tried to exit while being frozen. Blocking exit until freeze completes.", proc_getpid(p));
1559 		msleep(&p->p_memstat_state, &proc_list_mlock, PWAIT, "proc_memorystatus_remove", NULL);
1560 	}
1561 }
1562 #endif
1563 
1564 #if DEVELOPMENT
1565 boolean_t crash_behavior_test_mode = FALSE;
1566 boolean_t crash_behavior_test_would_panic = FALSE;
1567 SYSCTL_UINT(_kern, OID_AUTO, crash_behavior_test_mode, CTLFLAG_RW, &crash_behavior_test_mode, 0, "");
1568 SYSCTL_UINT(_kern, OID_AUTO, crash_behavior_test_would_panic, CTLFLAG_RW, &crash_behavior_test_would_panic, 0, "");
1569 #endif /* DEVELOPMENT */
1570 
1571 static bool
_proc_is_crashing_signal(int sig)1572 _proc_is_crashing_signal(int sig)
1573 {
1574 	bool result = false;
1575 	switch (sig) {
1576 	case SIGILL:
1577 	case SIGABRT:
1578 	case SIGFPE:
1579 	case SIGBUS:
1580 	case SIGSEGV:
1581 	case SIGSYS:
1582 	/*
1583 	 * If SIGTRAP is the terminating signal, then we can safely assume the
1584 	 * process crashed. (On iOS, SIGTRAP will be the terminating signal when
1585 	 * a process calls __builtin_trap(), which will abort.)
1586 	 */
1587 	case SIGTRAP:
1588 		result = true;
1589 	}
1590 
1591 	return result;
1592 }
1593 
1594 static bool
_proc_is_fatal_reason(os_reason_t reason)1595 _proc_is_fatal_reason(os_reason_t reason)
1596 {
1597 	if ((reason->osr_flags & OS_REASON_FLAG_ABORT) != 0) {
1598 		/* Abort is always fatal even if there is no crash report generated */
1599 		return true;
1600 	}
1601 	if ((reason->osr_flags & OS_REASON_FLAG_NO_CRASH_REPORT) != 0) {
1602 		/*
1603 		 * No crash report means this reason shouldn't be considered fatal
1604 		 * unless we are in test mode
1605 		 */
1606 #if DEVELOPMENT
1607 		if (crash_behavior_test_mode) {
1608 			return true;
1609 		}
1610 #endif /* DEVELOPMENT */
1611 		return false;
1612 	}
1613 	// By default all OS_REASON are fatal
1614 	return true;
1615 }
1616 
1617 static bool
proc_should_trigger_panic(proc_t p,int rv)1618 proc_should_trigger_panic(proc_t p, int rv)
1619 {
1620 	if (p == initproc || (p->p_crash_behavior & POSIX_SPAWN_PANIC_ON_EXIT) != 0) {
1621 		/* Always panic for launchd or equivalents */
1622 		return true;
1623 	}
1624 
1625 	if ((p->p_crash_behavior & POSIX_SPAWN_PANIC_ON_SPAWN_FAIL) != 0) {
1626 		return true;
1627 	}
1628 
1629 	if (p->p_posix_spawn_failed) {
1630 		/* posix_spawn failures normally don't qualify for panics */
1631 		return false;
1632 	}
1633 
1634 	bool deadline_expired = (mach_continuous_time() > p->p_crash_behavior_deadline);
1635 	if (p->p_crash_behavior_deadline != 0 && deadline_expired) {
1636 		return false;
1637 	}
1638 
1639 	if (WIFEXITED(rv)) {
1640 		int code = WEXITSTATUS(rv);
1641 
1642 		if ((p->p_crash_behavior & POSIX_SPAWN_PANIC_ON_NON_ZERO_EXIT) != 0) {
1643 			if (code == 0) {
1644 				/* No panic if we exit 0 */
1645 				return false;
1646 			} else {
1647 				/* Panic on non-zero exit */
1648 				return true;
1649 			}
1650 		} else {
1651 			/* No panic on normal exit if the process doesn't have the non-zero flag set */
1652 			return false;
1653 		}
1654 	} else if (WIFSIGNALED(rv)) {
1655 		int signal = WTERMSIG(rv);
1656 		/* This is a crash (non-normal exit) */
1657 		if ((p->p_crash_behavior & POSIX_SPAWN_PANIC_ON_CRASH) != 0) {
1658 			os_reason_t reason = p->p_exit_reason;
1659 			if (reason != OS_REASON_NULL) {
1660 				if (!_proc_is_fatal_reason(reason)) {
1661 					// Skip non-fatal terminate_with_reason
1662 					return false;
1663 				}
1664 				if (reason->osr_namespace == OS_REASON_SIGNAL) {
1665 					return _proc_is_crashing_signal(signal);
1666 				} else {
1667 					/*
1668 					 * This branch covers the case of terminate_with_reason which
1669 					 * delivers a SIGTERM which is still considered a crash even
1670 					 * thought the signal is not considered a crashing signal
1671 					 */
1672 					return true;
1673 				}
1674 			}
1675 			return _proc_is_crashing_signal(signal);
1676 		} else {
1677 			return false;
1678 		}
1679 	} else {
1680 		/*
1681 		 * This branch implies that we didn't exit normally nor did we receive
1682 		 * a signal. This should be unreachable.
1683 		 */
1684 		return true;
1685 	}
1686 }
1687 
1688 static void
proc_crash_coredump(proc_t p)1689 proc_crash_coredump(proc_t p)
1690 {
1691 	if (p != initproc) {
1692 		/* Core dumps are only enabled for launchd for now */
1693 		return;
1694 	}
1695 
1696 #if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
1697 	/*
1698 	 * For debugging purposes, generate a core file of initproc before
1699 	 * panicking. Leave at least 300 MB free on the root volume, and ignore
1700 	 * the process's corefile ulimit. fsync() the file to ensure it lands on disk
1701 	 * before the panic hits.
1702 	 */
1703 
1704 	int             err;
1705 	uint64_t        coredump_start = mach_absolute_time();
1706 	uint64_t        coredump_end;
1707 	clock_sec_t     tv_sec;
1708 	clock_usec_t    tv_usec;
1709 	uint32_t        tv_msec;
1710 
1711 
1712 	err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
1713 
1714 	coredump_end = mach_absolute_time();
1715 
1716 	absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
1717 
1718 	tv_msec = tv_usec / 1000;
1719 
1720 	if (err != 0) {
1721 		printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
1722 		    err, (uint32_t)tv_sec, tv_msec);
1723 	} else {
1724 		printf("Generated initproc core file in %d.%03d seconds\n",
1725 		    (uint32_t)tv_sec, tv_msec);
1726 	}
1727 #endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
1728 }
1729 
1730 static void
proc_handle_critical_exit(proc_t p,int rv)1731 proc_handle_critical_exit(proc_t p, int rv)
1732 {
1733 	if (!proc_should_trigger_panic(p, rv)) {
1734 		// No panic, bail out
1735 		return;
1736 	}
1737 
1738 #if DEVELOPMENT
1739 	if (crash_behavior_test_mode) {
1740 		crash_behavior_test_would_panic = TRUE;
1741 		// Force test mode off after hitting a panic
1742 		crash_behavior_test_mode = FALSE;
1743 		return;
1744 	}
1745 #endif /* DEVELOPMENT */
1746 
1747 	char *exit_reason_desc = exit_reason_get_string_desc(p->p_exit_reason);
1748 
1749 	if (p->p_exit_reason == OS_REASON_NULL) {
1750 		printf("pid %d exited -- no exit reason available -- (signal %d, exit %d)\n",
1751 		    proc_getpid(p), WTERMSIG(rv), WEXITSTATUS(rv));
1752 	} else {
1753 		printf("pid %d exited -- exit reason namespace %d subcode 0x%llx, description %s\n", proc_getpid(p),
1754 		    p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, exit_reason_desc ?
1755 		    exit_reason_desc : "none");
1756 	}
1757 
1758 	const char *prefix_str;
1759 	char prefix_str_buf[128];
1760 
1761 	if (p == initproc) {
1762 		if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
1763 			prefix_str = "LTE preinit process exited";
1764 		} else if (initproc_spawned) {
1765 			prefix_str = "initproc exited";
1766 		} else {
1767 			prefix_str = "initproc failed to start";
1768 		}
1769 	} else {
1770 		/* For processes that aren't launchd, just use the process name and pid */
1771 		snprintf(prefix_str_buf, sizeof(prefix_str_buf), "%s[%d] exited", p->p_name, proc_getpid(p));
1772 		prefix_str = prefix_str_buf;
1773 	}
1774 
1775 	proc_crash_coredump(p);
1776 
1777 	sync(p, (void *)NULL, (int *)NULL);
1778 
1779 	if (p->p_exit_reason == OS_REASON_NULL) {
1780 		panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
1781 		    prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((proc_getcsflags(p) & CS_KILLED) ? "CS_KILLED" : ""));
1782 	} else {
1783 		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",
1784 		    ((proc_getcsflags(p) & CS_KILLED) ? "CS_KILLED" : ""),
1785 		    prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
1786 		    exit_reason_desc ? exit_reason_desc : "none");
1787 	}
1788 }
1789 
1790 void
proc_prepareexit(proc_t p,int rv,boolean_t perf_notify)1791 proc_prepareexit(proc_t p, int rv, boolean_t perf_notify)
1792 {
1793 	mach_exception_data_type_t code = 0, subcode = 0;
1794 	exception_type_t etype;
1795 
1796 	struct uthread *ut;
1797 	thread_t self = current_thread();
1798 	ut = get_bsdthread_info(self);
1799 	struct rusage_superset *rup;
1800 	int kr = 0;
1801 	int create_corpse = FALSE;
1802 
1803 	if (p->p_crash_behavior != 0 || p == initproc) {
1804 		proc_handle_critical_exit(p, rv);
1805 	}
1806 
1807 	/*
1808 	 * Generate a corefile/crashlog if:
1809 	 *      The process doesn't have an exit reason that indicates no crash report should be created
1810 	 *      AND any of the following are true:
1811 	 *	- The process was terminated due to a fatal signal that generates a core
1812 	 *	- The process was killed due to a code signing violation
1813 	 *	- The process has an exit reason that indicates we should generate a crash report
1814 	 *
1815 	 * The first condition is necessary because abort_with_reason()/payload() use SIGABRT
1816 	 * (which normally triggers a core) but may indicate that no crash report should be created.
1817 	 */
1818 	if (!(PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) & OS_REASON_FLAG_NO_CRASH_REPORT)) &&
1819 	    (hassigprop(WTERMSIG(rv), SA_CORE) || ((proc_getcsflags(p) & CS_KILLED) != 0) ||
1820 	    (PROC_HAS_EXITREASON(p) && (PROC_EXITREASON_FLAGS(p) &
1821 	    OS_REASON_FLAG_GENERATE_CRASH_REPORT)))) {
1822 		/*
1823 		 * Workaround for processes checking up on PT_DENY_ATTACH:
1824 		 * should be backed out post-Leopard (details in 5431025).
1825 		 */
1826 		if ((SIGSEGV == WTERMSIG(rv)) &&
1827 		    (p->p_pptr->p_lflag & P_LNOATTACH)) {
1828 			goto skipcheck;
1829 		}
1830 
1831 		/*
1832 		 * Crash Reporter looks for the signal value, original exception
1833 		 * type, and low 20 bits of the original code in code[0]
1834 		 * (8, 4, and 20 bits respectively). code[1] is unmodified.
1835 		 */
1836 		code = ((WTERMSIG(rv) & 0xff) << 24) |
1837 		    ((ut->uu_exception & 0x0f) << 20) |
1838 		    ((int)ut->uu_code & 0xfffff);
1839 		subcode = ut->uu_subcode;
1840 		etype = ut->uu_exception;
1841 
1842 		/* Defualt to EXC_CRASH if the exception is not an EXC_RESOURCE or EXC_GUARD */
1843 		if (etype != EXC_RESOURCE || etype != EXC_GUARD) {
1844 			etype = EXC_CRASH;
1845 		}
1846 
1847 #if (DEVELOPMENT || DEBUG)
1848 		if (p->p_pid <= exception_log_max_pid) {
1849 			char *proc_name = proc_best_name(p);
1850 			if (PROC_HAS_EXITREASON(p)) {
1851 				record_system_event(SYSTEM_EVENT_TYPE_INFO, SYSTEM_EVENT_SUBSYSTEM_PROCESS, "process exit",
1852 				    "pid: %d -- process name: %s -- exit reason namespace: %d -- subcode: 0x%llx -- description: %s",
1853 				    proc_getpid(p), proc_name, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
1854 				    exit_reason_get_string_desc(p->p_exit_reason));
1855 			} else {
1856 				record_system_event(SYSTEM_EVENT_TYPE_INFO, SYSTEM_EVENT_SUBSYSTEM_PROCESS, "process exit",
1857 				    "pid: %d -- process name: %s -- exit status %d",
1858 				    proc_getpid(p), proc_name, WEXITSTATUS(rv));
1859 			}
1860 		}
1861 #endif
1862 
1863 		kr = task_exception_notify(EXC_CRASH, code, subcode);
1864 
1865 		/* Nobody handled EXC_CRASH?? remember to make corpse */
1866 		if (kr != 0 && p == current_proc()) {
1867 			/*
1868 			 * Do not create corpse when exit is called from jetsam thread.
1869 			 * Corpse creation code requires that proc_prepareexit is
1870 			 * called by the exiting proc and not the kernel_proc.
1871 			 */
1872 			create_corpse = TRUE;
1873 		}
1874 
1875 		/*
1876 		 * Revalidate the code signing of the text pages around current PC.
1877 		 * This is an attempt to detect and repair faults due to memory
1878 		 * corruption of text pages.
1879 		 *
1880 		 * The goal here is to fixup infrequent memory corruptions due to
1881 		 * things like aging RAM bit flips. So the approach is to only expect
1882 		 * to have to fixup one thing per crash. This also limits the amount
1883 		 * of extra work we cause in case this is a development kernel with an
1884 		 * active memory stomp happening.
1885 		 */
1886 		task_t task = proc_task(p);
1887 		uintptr_t bt[2];
1888 		struct backtrace_user_info btinfo = BTUINFO_INIT;
1889 		unsigned int frame_count = backtrace_user(bt, 2, NULL, &btinfo);
1890 		int bt_err = btinfo.btui_error;
1891 		if (bt_err == 0 && frame_count >= 1) {
1892 			/*
1893 			 * First check at the page containing the current PC.
1894 			 * This passes if the page code signs -or- if we can't figure out
1895 			 * what is at that address. The latter action is so we continue checking
1896 			 * previous pages which may be corrupt and caused a wild branch.
1897 			 */
1898 			kr = revalidate_text_page(task, bt[0]);
1899 
1900 			/* No corruption found, check the previous sequential page */
1901 			if (kr == KERN_SUCCESS) {
1902 				kr = revalidate_text_page(task, bt[0] - get_task_page_size(task));
1903 			}
1904 
1905 			/* Still no corruption found, check the current function's caller */
1906 			if (kr == KERN_SUCCESS) {
1907 				if (frame_count > 1 &&
1908 				    atop(bt[0]) != atop(bt[1]) &&           /* don't recheck PC page */
1909 				    atop(bt[0]) - 1 != atop(bt[1])) {       /* don't recheck page before */
1910 					kr = revalidate_text_page(task, (vm_map_offset_t)bt[1]);
1911 				}
1912 			}
1913 
1914 			/*
1915 			 * Log that we found a corruption.
1916 			 */
1917 			if (kr != KERN_SUCCESS) {
1918 				os_log(OS_LOG_DEFAULT,
1919 				    "Text page corruption detected in dying process %d\n", proc_getpid(p));
1920 			}
1921 		}
1922 	}
1923 
1924 skipcheck:
1925 	if (task_is_driver(proc_task(p)) && PROC_HAS_EXITREASON(p)) {
1926 		IOUserServerRecordExitReason(proc_task(p), p->p_exit_reason);
1927 	}
1928 
1929 	/* Notify the perf server? */
1930 	if (perf_notify) {
1931 		(void)sys_perf_notify(self, proc_getpid(p));
1932 	}
1933 
1934 
1935 	/* stash the usage into corpse data if making_corpse == true */
1936 	if (create_corpse == TRUE) {
1937 		kr = task_mark_corpse(proc_task(p));
1938 		if (kr != KERN_SUCCESS) {
1939 			if (kr == KERN_NO_SPACE) {
1940 				printf("Process[%d] has no vm space for corpse info.\n", proc_getpid(p));
1941 			} else if (kr == KERN_NOT_SUPPORTED) {
1942 				printf("Process[%d] was destined to be corpse. But corpse is disabled by config.\n", proc_getpid(p));
1943 			} else if (kr == KERN_TERMINATED) {
1944 				printf("Process[%d] has been terminated before it could be converted to a corpse.\n", proc_getpid(p));
1945 			} else {
1946 				printf("Process[%d] crashed: %s. Too many corpses being created.\n", proc_getpid(p), p->p_comm);
1947 			}
1948 			create_corpse = FALSE;
1949 		}
1950 	}
1951 
1952 	if (!proc_is_shadow(p)) {
1953 		/*
1954 		 * Before this process becomes a zombie, stash resource usage
1955 		 * stats in the proc for external observers to query
1956 		 * via proc_pid_rusage().
1957 		 *
1958 		 * If the zombie allocation fails, just punt the stats.
1959 		 */
1960 		rup = zalloc(zombie_zone);
1961 		gather_rusage_info(p, &rup->ri, RUSAGE_INFO_CURRENT);
1962 		rup->ri.ri_phys_footprint = 0;
1963 		rup->ri.ri_proc_exit_abstime = mach_absolute_time();
1964 		/*
1965 		 * Make the rusage_info visible to external observers
1966 		 * only after it has been completely filled in.
1967 		 */
1968 		p->p_ru = rup;
1969 	}
1970 
1971 	if (create_corpse) {
1972 		int est_knotes = 0, num_knotes = 0;
1973 		uint64_t *buffer = NULL;
1974 		uint32_t buf_size = 0;
1975 
1976 		/* Get all the udata pointers from kqueue */
1977 		est_knotes = kevent_proc_copy_uptrs(p, NULL, 0);
1978 		if (est_knotes > 0) {
1979 			buf_size = (uint32_t)((est_knotes + 32) * sizeof(uint64_t));
1980 			buffer = kalloc_data(buf_size, Z_WAITOK);
1981 			if (buffer) {
1982 				num_knotes = kevent_proc_copy_uptrs(p, buffer, buf_size);
1983 				if (num_knotes > est_knotes + 32) {
1984 					num_knotes = est_knotes + 32;
1985 				}
1986 			}
1987 		}
1988 
1989 		/* Update the code, subcode based on exit reason */
1990 		proc_update_corpse_exception_codes(p, &code, &subcode);
1991 		populate_corpse_crashinfo(p, proc_task(p), rup,
1992 		    code, subcode, buffer, num_knotes, NULL, etype);
1993 		kfree_data(buffer, buf_size);
1994 	}
1995 	/*
1996 	 * Remove proc from allproc queue and from pidhash chain.
1997 	 * Need to do this before we do anything that can block.
1998 	 * Not doing causes things like mount() find this on allproc
1999 	 * in partially cleaned state.
2000 	 */
2001 
2002 	proc_list_lock();
2003 
2004 #if CONFIG_MEMORYSTATUS
2005 	proc_memorystatus_remove(p);
2006 #endif
2007 
2008 	LIST_REMOVE(p, p_list);
2009 	LIST_INSERT_HEAD(&zombproc, p, p_list); /* Place onto zombproc. */
2010 	/* will not be visible via proc_find */
2011 	os_atomic_or(&p->p_refcount, P_REF_DEAD, relaxed);
2012 
2013 	proc_list_unlock();
2014 
2015 	/*
2016 	 * If parent is waiting for us to exit or exec,
2017 	 * P_LPPWAIT is set; we will wakeup the parent below.
2018 	 */
2019 	proc_lock(p);
2020 	p->p_lflag &= ~(P_LTRACED | P_LPPWAIT);
2021 	p->p_sigignore = ~(sigcantmask);
2022 
2023 	/*
2024 	 * If a thread is already waiting for us in proc_exit,
2025 	 * P_LTERM is set, wakeup the thread.
2026 	 */
2027 	if (p->p_lflag & P_LTERM) {
2028 		wakeup(&p->exit_thread);
2029 	} else {
2030 		p->p_lflag |= P_LTERM;
2031 	}
2032 
2033 	/* If current proc is exiting, ignore signals on the exit thread */
2034 	if (p == current_proc()) {
2035 		ut->uu_siglist = 0;
2036 	}
2037 	proc_unlock(p);
2038 }
2039 
2040 void
proc_exit(proc_t p)2041 proc_exit(proc_t p)
2042 {
2043 	proc_t q;
2044 	proc_t pp;
2045 	struct task *task = proc_task(p);
2046 	vnode_t tvp = NULLVP;
2047 	struct pgrp * pg;
2048 	struct session *sessp;
2049 	struct uthread * uth;
2050 	pid_t pid;
2051 	int exitval;
2052 	int knote_hint;
2053 
2054 	uth = current_uthread();
2055 
2056 	proc_lock(p);
2057 	proc_transstart(p, 1, 0);
2058 	if (!(p->p_lflag & P_LEXIT)) {
2059 		/*
2060 		 * This can happen if a thread_terminate() occurs
2061 		 * in a single-threaded process.
2062 		 */
2063 		p->p_lflag |= P_LEXIT;
2064 		proc_transend(p, 1);
2065 		proc_unlock(p);
2066 		proc_prepareexit(p, 0, TRUE);
2067 		(void) task_terminate_internal(task);
2068 		proc_lock(p);
2069 	} else if (!(p->p_lflag & P_LTERM)) {
2070 		proc_transend(p, 1);
2071 		/* Jetsam is in middle of calling proc_prepareexit, wait for it */
2072 		p->p_lflag |= P_LTERM;
2073 		msleep(&p->exit_thread, &p->p_mlock, PWAIT, "proc_prepareexit_wait", NULL);
2074 	} else {
2075 		proc_transend(p, 1);
2076 	}
2077 
2078 	p->p_lflag |= P_LPEXIT;
2079 
2080 	/*
2081 	 * Other kernel threads may be in the middle of signalling this process.
2082 	 * Wait for those threads to wrap it up before making the process
2083 	 * disappear on them.
2084 	 */
2085 	if ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 0)) {
2086 		p->p_sigwaitcnt++;
2087 		while ((p->p_lflag & P_LINSIGNAL) || (p->p_sigwaitcnt > 1)) {
2088 			msleep(&p->p_sigmask, &p->p_mlock, PWAIT, "proc_sigdrain", NULL);
2089 		}
2090 		p->p_sigwaitcnt--;
2091 	}
2092 
2093 	proc_unlock(p);
2094 	pid = proc_getpid(p);
2095 	exitval = p->p_xstat;
2096 	KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
2097 	    BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_START,
2098 	    pid, exitval, 0, 0, 0);
2099 
2100 #if DEVELOPMENT || DEBUG
2101 	proc_exit_lpexit_check(pid, PELS_POS_START);
2102 #endif
2103 
2104 #if CONFIG_DTRACE
2105 	dtrace_proc_exit(p);
2106 #endif
2107 
2108 	/*
2109 	 * need to cancel async IO requests that can be cancelled and wait for those
2110 	 * already active.  MAY BLOCK!
2111 	 */
2112 
2113 	proc_refdrain(p);
2114 
2115 	/* if any pending cpu limits action, clear it */
2116 	task_clear_cpuusage(proc_task(p), TRUE);
2117 
2118 	workq_mark_exiting(p);
2119 
2120 	_aio_exit( p );
2121 
2122 	/*
2123 	 * Close open files and release open-file table.
2124 	 * This may block!
2125 	 */
2126 	fdt_invalidate(p);
2127 
2128 	/*
2129 	 * Once all the knotes, kqueues & workloops are destroyed, get rid of the
2130 	 * workqueue.
2131 	 */
2132 	workq_exit(p);
2133 
2134 	if (uth->uu_lowpri_window) {
2135 		/*
2136 		 * task is marked as a low priority I/O type
2137 		 * and the I/O we issued while in flushing files on close
2138 		 * collided with normal I/O operations...
2139 		 * no need to throttle this thread since its going away
2140 		 * but we do need to update our bookeeping w/r to throttled threads
2141 		 */
2142 		throttle_lowpri_io(0);
2143 	}
2144 
2145 	if (p->p_lflag & P_LNSPACE_RESOLVER) {
2146 		/*
2147 		 * The namespace resolver is exiting; there may be
2148 		 * outstanding materialization requests to clean up.
2149 		 */
2150 		nspace_resolver_exited(p);
2151 	}
2152 
2153 #if SYSV_SHM
2154 	/* Close ref SYSV Shared memory*/
2155 	if (p->vm_shm) {
2156 		shmexit(p);
2157 	}
2158 #endif
2159 #if SYSV_SEM
2160 	/* Release SYSV semaphores */
2161 	semexit(p);
2162 #endif
2163 
2164 #if PSYNCH
2165 	pth_proc_hashdelete(p);
2166 #endif /* PSYNCH */
2167 
2168 	pg = proc_pgrp(p, &sessp);
2169 	if (SESS_LEADER(p, sessp)) {
2170 		if (sessp->s_ttyvp != NULLVP) {
2171 			struct vnode *ttyvp;
2172 			int ttyvid;
2173 			int cttyflag = 0;
2174 			struct vfs_context context;
2175 			struct tty *tp;
2176 			struct pgrp *tpgrp = PGRP_NULL;
2177 
2178 			/*
2179 			 * Controlling process.
2180 			 * Signal foreground pgrp,
2181 			 * drain controlling terminal
2182 			 * and revoke access to controlling terminal.
2183 			 */
2184 
2185 			proc_list_lock(); /* prevent any t_pgrp from changing */
2186 			session_lock(sessp);
2187 			if (sessp->s_ttyp && sessp->s_ttyp->t_session == sessp) {
2188 				tpgrp = tty_pgrp_locked(sessp->s_ttyp);
2189 			}
2190 			proc_list_unlock();
2191 
2192 			if (tpgrp != PGRP_NULL) {
2193 				session_unlock(sessp);
2194 				pgsignal(tpgrp, SIGHUP, 1);
2195 				pgrp_rele(tpgrp);
2196 				session_lock(sessp);
2197 			}
2198 
2199 			cttyflag = (os_atomic_andnot_orig(&sessp->s_refcount,
2200 			    S_CTTYREF, relaxed) & S_CTTYREF);
2201 			ttyvp = sessp->s_ttyvp;
2202 			ttyvid = sessp->s_ttyvid;
2203 			tp = session_clear_tty_locked(sessp);
2204 			if (ttyvp) {
2205 				vnode_hold(ttyvp);
2206 			}
2207 			session_unlock(sessp);
2208 
2209 			if ((ttyvp != NULLVP) && (vnode_getwithvid(ttyvp, ttyvid) == 0)) {
2210 				if (tp != TTY_NULL) {
2211 					tty_lock(tp);
2212 					(void) ttywait(tp);
2213 					tty_unlock(tp);
2214 				}
2215 
2216 				context.vc_thread = NULL;
2217 				context.vc_ucred = kauth_cred_proc_ref(p);
2218 				VNOP_REVOKE(ttyvp, REVOKEALL, &context);
2219 				if (cttyflag) {
2220 					/*
2221 					 * Release the extra usecount taken in cttyopen.
2222 					 * usecount should be released after VNOP_REVOKE is called.
2223 					 * This usecount was taken to ensure that
2224 					 * the VNOP_REVOKE results in a close to
2225 					 * the tty since cttyclose is a no-op.
2226 					 */
2227 					vnode_rele(ttyvp);
2228 				}
2229 				vnode_put(ttyvp);
2230 				kauth_cred_unref(&context.vc_ucred);
2231 				vnode_drop(ttyvp);
2232 				ttyvp = NULLVP;
2233 			}
2234 			if (ttyvp) {
2235 				vnode_drop(ttyvp);
2236 			}
2237 			if (tp) {
2238 				ttyfree(tp);
2239 			}
2240 		}
2241 		session_lock(sessp);
2242 		sessp->s_leader = NULL;
2243 		session_unlock(sessp);
2244 	}
2245 
2246 	if (!proc_is_shadow(p)) {
2247 		fixjobc(p, pg, 0);
2248 	}
2249 	pgrp_rele(pg);
2250 
2251 	/*
2252 	 * Change RLIMIT_FSIZE for accounting/debugging.
2253 	 */
2254 	proc_limitsetcur_fsize(p, RLIM_INFINITY);
2255 
2256 	(void)acct_process(p);
2257 
2258 	proc_list_lock();
2259 
2260 	if ((p->p_listflag & P_LIST_EXITCOUNT) == P_LIST_EXITCOUNT) {
2261 		p->p_listflag &= ~P_LIST_EXITCOUNT;
2262 		proc_shutdown_exitcount--;
2263 		if (proc_shutdown_exitcount == 0) {
2264 			wakeup(&proc_shutdown_exitcount);
2265 		}
2266 	}
2267 
2268 	/* wait till parentrefs are dropped and grant no more */
2269 	proc_childdrainstart(p);
2270 	while ((q = p->p_children.lh_first) != NULL) {
2271 		if (q->p_stat == SZOMB) {
2272 			if (p != q->p_pptr) {
2273 				panic("parent child linkage broken");
2274 			}
2275 			/* check for sysctl zomb lookup */
2276 			while ((q->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2277 				msleep(&q->p_stat, &proc_list_mlock, PWAIT, "waitcoll", 0);
2278 			}
2279 			q->p_listflag |= P_LIST_WAITING;
2280 			/*
2281 			 * This is a named reference and it is not granted
2282 			 * if the reap is already in progress. So we get
2283 			 * the reference here exclusively and their can be
2284 			 * no waiters. So there is no need for a wakeup
2285 			 * after we are done.  Also the reap frees the structure
2286 			 * and the proc struct cannot be used for wakeups as well.
2287 			 * It is safe to use q here as this is system reap
2288 			 */
2289 			reap_flags_t reparent_flags = (q->p_listflag & P_LIST_DEADPARENT) ?
2290 			    REAP_REPARENTED_TO_INIT : 0;
2291 			reap_child_locked(p, q,
2292 			    REAP_DEAD_PARENT | REAP_LOCKED | reparent_flags);
2293 		} else {
2294 			/*
2295 			 * Traced processes are killed
2296 			 * since their existence means someone is messing up.
2297 			 */
2298 			if (q->p_lflag & P_LTRACED) {
2299 				struct proc *opp;
2300 
2301 				/*
2302 				 * Take a reference on the child process to
2303 				 * ensure it doesn't exit and disappear between
2304 				 * the time we drop the list_lock and attempt
2305 				 * to acquire its proc_lock.
2306 				 */
2307 				if (proc_ref(q, true) != q) {
2308 					continue;
2309 				}
2310 
2311 				proc_list_unlock();
2312 
2313 				opp = proc_find(q->p_oppid);
2314 				if (opp != PROC_NULL) {
2315 					proc_list_lock();
2316 					q->p_oppid = 0;
2317 					proc_list_unlock();
2318 					proc_reparentlocked(q, opp, 0, 0);
2319 					proc_rele(opp);
2320 				} else {
2321 					/* original parent exited while traced */
2322 					proc_list_lock();
2323 					q->p_listflag |= P_LIST_DEADPARENT;
2324 					q->p_oppid = 0;
2325 					proc_list_unlock();
2326 					proc_reparentlocked(q, initproc, 0, 0);
2327 				}
2328 
2329 				proc_lock(q);
2330 				q->p_lflag &= ~P_LTRACED;
2331 
2332 				if (q->sigwait_thread) {
2333 					thread_t thread = q->sigwait_thread;
2334 
2335 					proc_unlock(q);
2336 					/*
2337 					 * The sigwait_thread could be stopped at a
2338 					 * breakpoint. Wake it up to kill.
2339 					 * Need to do this as it could be a thread which is not
2340 					 * the first thread in the task. So any attempts to kill
2341 					 * the process would result into a deadlock on q->sigwait.
2342 					 */
2343 					thread_resume(thread);
2344 					clear_wait(thread, THREAD_INTERRUPTED);
2345 					threadsignal(thread, SIGKILL, 0, TRUE);
2346 				} else {
2347 					proc_unlock(q);
2348 				}
2349 
2350 				psignal(q, SIGKILL);
2351 				proc_list_lock();
2352 				proc_rele(q);
2353 			} else {
2354 				q->p_listflag |= P_LIST_DEADPARENT;
2355 				proc_reparentlocked(q, initproc, 0, 1);
2356 			}
2357 		}
2358 	}
2359 
2360 	proc_childdrainend(p);
2361 	proc_list_unlock();
2362 
2363 #if CONFIG_MACF
2364 	if (!proc_is_shadow(p)) {
2365 		/*
2366 		 * Notify MAC policies that proc is dead.
2367 		 * This should be replaced with proper label management
2368 		 * (rdar://problem/32126399).
2369 		 */
2370 		mac_proc_notify_exit(p);
2371 	}
2372 #endif
2373 
2374 	/*
2375 	 * Release reference to text vnode
2376 	 */
2377 	tvp = p->p_textvp;
2378 	p->p_textvp = NULL;
2379 	if (tvp != NULLVP) {
2380 		vnode_rele(tvp);
2381 	}
2382 
2383 	/*
2384 	 * Save exit status and final rusage info, adding in child rusage
2385 	 * info and self times.  If we were unable to allocate a zombie
2386 	 * structure, this information is lost.
2387 	 */
2388 	if (p->p_ru != NULL) {
2389 		calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, NULL);
2390 		p->p_ru->ru = p->p_stats->p_ru;
2391 
2392 		ruadd(&(p->p_ru->ru), &p->p_stats->p_cru);
2393 	}
2394 
2395 	/*
2396 	 * Free up profiling buffers.
2397 	 */
2398 	{
2399 		struct uprof *p0 = &p->p_stats->p_prof, *p1, *pn;
2400 
2401 		p1 = p0->pr_next;
2402 		p0->pr_next = NULL;
2403 		p0->pr_scale = 0;
2404 
2405 		for (; p1 != NULL; p1 = pn) {
2406 			pn = p1->pr_next;
2407 			kfree_type(struct uprof, p1);
2408 		}
2409 	}
2410 
2411 	proc_free_realitimer(p);
2412 
2413 	/*
2414 	 * Other substructures are freed from wait().
2415 	 */
2416 	zfree(proc_stats_zone, p->p_stats);
2417 	p->p_stats = NULL;
2418 
2419 	zfree_ro(ZONE_ID_PROC_SIGACTS_RO, p->p_sigacts.ps_ro);
2420 
2421 	proc_limitdrop(p);
2422 
2423 #if DEVELOPMENT || DEBUG
2424 	proc_exit_lpexit_check(pid, PELS_POS_PRE_TASK_DETACH);
2425 #endif
2426 
2427 	/*
2428 	 * Finish up by terminating the task
2429 	 * and halt this thread (only if a
2430 	 * member of the task exiting).
2431 	 */
2432 	proc_set_task(p, TASK_NULL);
2433 	set_bsdtask_info(task, NULL);
2434 	clear_thread_ro_proc(get_machthread(uth));
2435 
2436 #if DEVELOPMENT || DEBUG
2437 	proc_exit_lpexit_check(pid, PELS_POS_POST_TASK_DETACH);
2438 #endif
2439 
2440 	knote_hint = NOTE_EXIT | (p->p_xstat & 0xffff);
2441 	proc_knote(p, knote_hint);
2442 
2443 	/* mark the thread as the one that is doing proc_exit
2444 	 * no need to hold proc lock in uthread_free
2445 	 */
2446 	uth->uu_flag |= UT_PROCEXIT;
2447 	/*
2448 	 * Notify parent that we're gone.
2449 	 */
2450 	pp = proc_parent(p);
2451 	if (proc_is_shadow(p)) {
2452 		/* kernel can reap this one, no need to move it to launchd */
2453 		proc_list_lock();
2454 		p->p_listflag |= P_LIST_DEADPARENT;
2455 		proc_list_unlock();
2456 	} else if (pp->p_flag & P_NOCLDWAIT) {
2457 		if (p->p_ru != NULL) {
2458 			proc_lock(pp);
2459 #if 3839178
2460 			/*
2461 			 * If the parent is ignoring SIGCHLD, then POSIX requires
2462 			 * us to not add the resource usage to the parent process -
2463 			 * we are only going to hand it off to init to get reaped.
2464 			 * We should contest the standard in this case on the basis
2465 			 * of RLIMIT_CPU.
2466 			 */
2467 #else   /* !3839178 */
2468 			/*
2469 			 * Add child resource usage to parent before giving
2470 			 * zombie to init.  If we were unable to allocate a
2471 			 * zombie structure, this information is lost.
2472 			 */
2473 			ruadd(&pp->p_stats->p_cru, &p->p_ru->ru);
2474 #endif  /* !3839178 */
2475 			update_rusage_info_child(&pp->p_stats->ri_child, &p->p_ru->ri);
2476 			proc_unlock(pp);
2477 		}
2478 
2479 		/* kernel can reap this one, no need to move it to launchd */
2480 		proc_list_lock();
2481 		p->p_listflag |= P_LIST_DEADPARENT;
2482 		proc_list_unlock();
2483 	}
2484 	if (!proc_is_shadow(p) &&
2485 	    ((p->p_listflag & P_LIST_DEADPARENT) == 0 || p->p_oppid)) {
2486 		if (pp != initproc) {
2487 			proc_lock(pp);
2488 			pp->si_pid = proc_getpid(p);
2489 			pp->p_xhighbits = p->p_xhighbits;
2490 			p->p_xhighbits = 0;
2491 			pp->si_status = p->p_xstat;
2492 			pp->si_code = CLD_EXITED;
2493 			/*
2494 			 * p_ucred usage is safe as it is an exiting process
2495 			 * and reference is dropped in reap
2496 			 */
2497 			pp->si_uid = kauth_cred_getruid(proc_ucred(p));
2498 			proc_unlock(pp);
2499 		}
2500 		/* mark as a zombie */
2501 		/* No need to take proc lock as all refs are drained and
2502 		 * no one except parent (reaping ) can look at this.
2503 		 * The write is to an int and is coherent. Also parent is
2504 		 *  keyed off of list lock for reaping
2505 		 */
2506 		DTRACE_PROC2(exited, proc_t, p, int, exitval);
2507 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
2508 		    BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
2509 		    pid, exitval, 0, 0, 0);
2510 		p->p_stat = SZOMB;
2511 		/*
2512 		 * The current process can be reaped so, no one
2513 		 * can depend on this
2514 		 */
2515 
2516 		psignal(pp, SIGCHLD);
2517 
2518 		/* and now wakeup the parent */
2519 		proc_list_lock();
2520 		wakeup((caddr_t)pp);
2521 		proc_list_unlock();
2522 	} else {
2523 		/* should be fine as parent proc would be initproc */
2524 		/* mark as a zombie */
2525 		/* No need to take proc lock as all refs are drained and
2526 		 * no one except parent (reaping ) can look at this.
2527 		 * The write is to an int and is coherent. Also parent is
2528 		 *  keyed off of list lock for reaping
2529 		 */
2530 		DTRACE_PROC2(exited, proc_t, p, int, exitval);
2531 		proc_list_lock();
2532 		KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
2533 		    BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXIT) | DBG_FUNC_END,
2534 		    pid, exitval, 0, 0, 0);
2535 		/* check for sysctl zomb lookup */
2536 		while ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
2537 			msleep(&p->p_stat, &proc_list_mlock, PWAIT, "waitcoll", 0);
2538 		}
2539 		/* safe to use p as this is a system reap */
2540 		p->p_stat = SZOMB;
2541 		p->p_listflag |= P_LIST_WAITING;
2542 
2543 		/*
2544 		 * This is a named reference and it is not granted
2545 		 * if the reap is already in progress. So we get
2546 		 * the reference here exclusively and their can be
2547 		 * no waiters. So there is no need for a wakeup
2548 		 * after we are done. AlsO  the reap frees the structure
2549 		 * and the proc struct cannot be used for wakeups as well.
2550 		 * It is safe to use p here as this is system reap
2551 		 */
2552 		reap_child_locked(pp, p,
2553 		    REAP_DEAD_PARENT | REAP_LOCKED | REAP_DROP_LOCK);
2554 	}
2555 	if (uth->uu_lowpri_window) {
2556 		/*
2557 		 * task is marked as a low priority I/O type and we've
2558 		 * somehow picked up another throttle during exit processing...
2559 		 * no need to throttle this thread since its going away
2560 		 * but we do need to update our bookeeping w/r to throttled threads
2561 		 */
2562 		throttle_lowpri_io(0);
2563 	}
2564 
2565 	proc_rele(pp);
2566 #if DEVELOPMENT || DEBUG
2567 	proc_exit_lpexit_check(pid, PELS_POS_END);
2568 #endif
2569 }
2570 
2571 
2572 /*
2573  * reap_child_locked
2574  *
2575  * Finalize a child exit once its status has been saved.
2576  *
2577  * If ptrace has attached, detach it and return it to its real parent.  Free any
2578  * remaining resources.
2579  *
2580  * Parameters:
2581  * - proc_t parent      Parent of process being reaped
2582  * - proc_t child       Process to reap
2583  * - reap_flags_t flags Control locking and re-parenting behavior
2584  */
2585 static void
reap_child_locked(proc_t parent,proc_t child,reap_flags_t flags)2586 reap_child_locked(proc_t parent, proc_t child, reap_flags_t flags)
2587 {
2588 	struct pgrp *pg;
2589 	kauth_cred_t cred;
2590 	boolean_t shadow_proc = proc_is_shadow(child);
2591 
2592 	if (flags & REAP_LOCKED) {
2593 		proc_list_unlock();
2594 	}
2595 
2596 	/*
2597 	 * Under ptrace, the child should now be re-parented back to its original
2598 	 * parent, unless that parent was initproc or it didn't come to initproc
2599 	 * through re-parenting.
2600 	 */
2601 	bool child_ptraced = child->p_oppid != 0;
2602 	if (!shadow_proc && child_ptraced) {
2603 		int knote_hint;
2604 		pid_t orig_ppid = 0;
2605 		proc_t orig_parent = PROC_NULL;
2606 
2607 		proc_lock(child);
2608 		orig_ppid = child->p_oppid;
2609 		child->p_oppid = 0;
2610 		knote_hint = NOTE_EXIT | (child->p_xstat & 0xffff);
2611 		proc_unlock(child);
2612 
2613 		orig_parent = proc_find(orig_ppid);
2614 		if (orig_parent) {
2615 			/*
2616 			 * Only re-parent the process if its original parent was not
2617 			 * initproc and it did not come to initproc from re-parenting.
2618 			 */
2619 			bool reparenting = orig_parent != initproc ||
2620 			    (flags & REAP_REPARENTED_TO_INIT) == 0;
2621 			if (reparenting) {
2622 				if (orig_parent != initproc) {
2623 					/*
2624 					 * Internal fields should be safe to access here because the
2625 					 * child is exited and not reaped or re-parented yet.
2626 					 */
2627 					proc_lock(orig_parent);
2628 					orig_parent->si_pid = proc_getpid(child);
2629 					orig_parent->si_status = child->p_xstat;
2630 					orig_parent->si_code = CLD_CONTINUED;
2631 					orig_parent->si_uid = kauth_cred_getruid(proc_ucred(child));
2632 					proc_unlock(orig_parent);
2633 				}
2634 				proc_reparentlocked(child, orig_parent, 1, 0);
2635 
2636 				/*
2637 				 * After re-parenting, re-send the child's NOTE_EXIT to the
2638 				 * original parent.
2639 				 */
2640 				proc_knote(child, knote_hint);
2641 				psignal(orig_parent, SIGCHLD);
2642 
2643 				proc_list_lock();
2644 				wakeup((caddr_t)orig_parent);
2645 				child->p_listflag &= ~P_LIST_WAITING;
2646 				wakeup(&child->p_stat);
2647 				proc_list_unlock();
2648 
2649 				proc_rele(orig_parent);
2650 				if ((flags & REAP_LOCKED) && !(flags & REAP_DROP_LOCK)) {
2651 					proc_list_lock();
2652 				}
2653 				return;
2654 			} else {
2655 				/*
2656 				 * Satisfy the knote lifecycle because ptraced processes don't
2657 				 * broadcast NOTE_EXIT during initial child termination.
2658 				 */
2659 				proc_knote(child, knote_hint);
2660 				proc_rele(orig_parent);
2661 			}
2662 		}
2663 	}
2664 
2665 #pragma clang diagnostic push
2666 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2667 	proc_knote(child, NOTE_REAP);
2668 #pragma clang diagnostic pop
2669 
2670 	proc_knote_drain(child);
2671 
2672 	child->p_xstat = 0;
2673 	if (!shadow_proc && child->p_ru) {
2674 		/*
2675 		 * Roll up the rusage statistics to the parent, unless the parent is
2676 		 * ignoring SIGCHLD.  POSIX requires the children's resources of such a
2677 		 * parent to not be included in the parent's usage (seems odd given
2678 		 * RLIMIT_CPU, though).
2679 		 */
2680 		proc_lock(parent);
2681 		bool rollup_child = (parent->p_flag & P_NOCLDWAIT) == 0;
2682 		if (rollup_child) {
2683 			ruadd(&parent->p_stats->p_cru, &child->p_ru->ru);
2684 		}
2685 		update_rusage_info_child(&parent->p_stats->ri_child, &child->p_ru->ri);
2686 		proc_unlock(parent);
2687 		zfree(zombie_zone, child->p_ru);
2688 		child->p_ru = NULL;
2689 	} else if (!shadow_proc) {
2690 		printf("Warning : lost p_ru for %s\n", child->p_comm);
2691 	} else {
2692 		assert(child->p_ru == NULL);
2693 	}
2694 
2695 	AUDIT_SESSION_PROCEXIT(child);
2696 
2697 #if CONFIG_PERSONAS
2698 	persona_proc_drop(child);
2699 #endif /* CONFIG_PERSONAS */
2700 	(void)chgproccnt(kauth_cred_getruid(proc_ucred(child)), -1);
2701 
2702 	os_reason_free(child->p_exit_reason);
2703 
2704 	proc_list_lock();
2705 
2706 	pg = pgrp_leave_locked(child);
2707 	LIST_REMOVE(child, p_list);
2708 	parent->p_childrencnt--;
2709 	LIST_REMOVE(child, p_sibling);
2710 	bool no_more_children = (flags & REAP_DEAD_PARENT) &&
2711 	    LIST_EMPTY(&parent->p_children);
2712 	if (no_more_children) {
2713 		wakeup((caddr_t)parent);
2714 	}
2715 	child->p_listflag &= ~P_LIST_WAITING;
2716 	wakeup(&child->p_stat);
2717 
2718 	/* Take it out of process hash */
2719 	if (!shadow_proc) {
2720 		phash_remove_locked(proc_getpid(child), child);
2721 	}
2722 	proc_checkdeadrefs(child);
2723 	nprocs--;
2724 	if (flags & REAP_DEAD_PARENT) {
2725 		child->p_listflag |= P_LIST_DEADPARENT;
2726 	}
2727 	cred = proc_ucred(child);
2728 	child->p_proc_ro = proc_ro_release_proc(child->p_proc_ro);
2729 
2730 	proc_list_unlock();
2731 
2732 	pgrp_rele(pg);
2733 	if (child->p_proc_ro != NULL) {
2734 		proc_ro_free(child->p_proc_ro);
2735 		child->p_proc_ro = NULL;
2736 	}
2737 	kauth_cred_set(&cred, NOCRED);
2738 	fdt_destroy(child);
2739 	lck_mtx_destroy(&child->p_mlock, &proc_mlock_grp);
2740 	lck_mtx_destroy(&child->p_ucred_mlock, &proc_ucred_mlock_grp);
2741 #if CONFIG_DTRACE
2742 	lck_mtx_destroy(&child->p_dtrace_sprlock, &proc_lck_grp);
2743 #endif
2744 	lck_spin_destroy(&child->p_slock, &proc_slock_grp);
2745 	proc_wait_release(child);
2746 
2747 	if ((flags & REAP_LOCKED) && (flags & REAP_DROP_LOCK) == 0) {
2748 		proc_list_lock();
2749 	}
2750 }
2751 
2752 int
wait1continue(int result)2753 wait1continue(int result)
2754 {
2755 	proc_t p;
2756 	thread_t thread;
2757 	uthread_t uth;
2758 	struct _wait4_data *wait4_data;
2759 	struct wait4_nocancel_args *uap;
2760 	int *retval;
2761 
2762 	if (result) {
2763 		return result;
2764 	}
2765 
2766 	p = current_proc();
2767 	thread = current_thread();
2768 	uth = (struct uthread *)get_bsdthread_info(thread);
2769 
2770 	wait4_data = &uth->uu_save.uus_wait4_data;
2771 	uap = wait4_data->args;
2772 	retval = wait4_data->retval;
2773 	return wait4_nocancel(p, uap, retval);
2774 }
2775 
2776 int
wait4(proc_t q,struct wait4_args * uap,int32_t * retval)2777 wait4(proc_t q, struct wait4_args *uap, int32_t *retval)
2778 {
2779 	__pthread_testcancel(1);
2780 	return wait4_nocancel(q, (struct wait4_nocancel_args *)uap, retval);
2781 }
2782 
2783 int
wait4_nocancel(proc_t q,struct wait4_nocancel_args * uap,int32_t * retval)2784 wait4_nocancel(proc_t q, struct wait4_nocancel_args *uap, int32_t *retval)
2785 {
2786 	int nfound;
2787 	int sibling_count;
2788 	proc_t p;
2789 	int status, error;
2790 	uthread_t uth;
2791 	struct _wait4_data *wait4_data;
2792 
2793 	AUDIT_ARG(pid, uap->pid);
2794 
2795 	if (uap->pid == 0) {
2796 		uap->pid = -q->p_pgrpid;
2797 	}
2798 
2799 	if (uap->pid == INT_MIN) {
2800 		return EINVAL;
2801 	}
2802 
2803 loop:
2804 	proc_list_lock();
2805 loop1:
2806 	nfound = 0;
2807 	sibling_count = 0;
2808 
2809 	PCHILDREN_FOREACH(q, p) {
2810 		if (p->p_sibling.le_next != 0) {
2811 			sibling_count++;
2812 		}
2813 		if (uap->pid != WAIT_ANY &&
2814 		    proc_getpid(p) != uap->pid &&
2815 		    p->p_pgrpid != -(uap->pid)) {
2816 			continue;
2817 		}
2818 
2819 		if (proc_is_shadow(p)) {
2820 			continue;
2821 		}
2822 
2823 		nfound++;
2824 
2825 		/* XXX This is racy because we don't get the lock!!!! */
2826 
2827 		if (p->p_listflag & P_LIST_WAITING) {
2828 			/* we're not using a continuation here but we still need to stash
2829 			 * the args for stackshot. */
2830 			uth = current_uthread();
2831 			wait4_data = &uth->uu_save.uus_wait4_data;
2832 			wait4_data->args = uap;
2833 			thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess);
2834 
2835 			(void)msleep(&p->p_stat, &proc_list_mlock, PWAIT, "waitcoll", 0);
2836 			goto loop1;
2837 		}
2838 		p->p_listflag |= P_LIST_WAITING;   /* only allow single thread to wait() */
2839 
2840 
2841 		if (p->p_stat == SZOMB) {
2842 			reap_flags_t reap_flags = (p->p_listflag & P_LIST_DEADPARENT) ?
2843 			    REAP_REPARENTED_TO_INIT : 0;
2844 
2845 			proc_list_unlock();
2846 #if CONFIG_MACF
2847 			if ((error = mac_proc_check_wait(q, p)) != 0) {
2848 				goto out;
2849 			}
2850 #endif
2851 			retval[0] = proc_getpid(p);
2852 			if (uap->status) {
2853 				/* Legacy apps expect only 8 bits of status */
2854 				status = 0xffff & p->p_xstat;   /* convert to int */
2855 				error = copyout((caddr_t)&status,
2856 				    uap->status,
2857 				    sizeof(status));
2858 				if (error) {
2859 					goto out;
2860 				}
2861 			}
2862 			if (uap->rusage) {
2863 				if (p->p_ru == NULL) {
2864 					error = ENOMEM;
2865 				} else {
2866 					if (IS_64BIT_PROCESS(q)) {
2867 						struct user64_rusage    my_rusage = {};
2868 						munge_user64_rusage(&p->p_ru->ru, &my_rusage);
2869 						error = copyout((caddr_t)&my_rusage,
2870 						    uap->rusage,
2871 						    sizeof(my_rusage));
2872 					} else {
2873 						struct user32_rusage    my_rusage = {};
2874 						munge_user32_rusage(&p->p_ru->ru, &my_rusage);
2875 						error = copyout((caddr_t)&my_rusage,
2876 						    uap->rusage,
2877 						    sizeof(my_rusage));
2878 					}
2879 				}
2880 				/* information unavailable? */
2881 				if (error) {
2882 					goto out;
2883 				}
2884 			}
2885 
2886 			/* Conformance change for 6577252.
2887 			 * When SIGCHLD is blocked and wait() returns because the status
2888 			 * of a child process is available and there are no other
2889 			 * children processes, then any pending SIGCHLD signal is cleared.
2890 			 */
2891 			if (sibling_count == 0) {
2892 				int mask = sigmask(SIGCHLD);
2893 				uth = current_uthread();
2894 
2895 				if ((uth->uu_sigmask & mask) != 0) {
2896 					/* we are blocking SIGCHLD signals.  clear any pending SIGCHLD.
2897 					 * This locking looks funny but it is protecting access to the
2898 					 * thread via p_uthlist.
2899 					 */
2900 					proc_lock(q);
2901 					uth->uu_siglist &= ~mask;       /* clear pending signal */
2902 					proc_unlock(q);
2903 				}
2904 			}
2905 
2906 			/* Clean up */
2907 			(void)reap_child_locked(q, p, reap_flags);
2908 
2909 			return 0;
2910 		}
2911 		if (p->p_stat == SSTOP && (p->p_lflag & P_LWAITED) == 0 &&
2912 		    (p->p_lflag & P_LTRACED || uap->options & WUNTRACED)) {
2913 			proc_list_unlock();
2914 #if CONFIG_MACF
2915 			if ((error = mac_proc_check_wait(q, p)) != 0) {
2916 				goto out;
2917 			}
2918 #endif
2919 			proc_lock(p);
2920 			p->p_lflag |= P_LWAITED;
2921 			proc_unlock(p);
2922 			retval[0] = proc_getpid(p);
2923 			if (uap->status) {
2924 				status = W_STOPCODE(p->p_xstat);
2925 				error = copyout((caddr_t)&status,
2926 				    uap->status,
2927 				    sizeof(status));
2928 			} else {
2929 				error = 0;
2930 			}
2931 			goto out;
2932 		}
2933 		/*
2934 		 * If we are waiting for continued processses, and this
2935 		 * process was continued
2936 		 */
2937 		if ((uap->options & WCONTINUED) &&
2938 		    (p->p_flag & P_CONTINUED)) {
2939 			proc_list_unlock();
2940 #if CONFIG_MACF
2941 			if ((error = mac_proc_check_wait(q, p)) != 0) {
2942 				goto out;
2943 			}
2944 #endif
2945 
2946 			/* Prevent other process for waiting for this event */
2947 			OSBitAndAtomic(~((uint32_t)P_CONTINUED), &p->p_flag);
2948 			retval[0] = proc_getpid(p);
2949 			if (uap->status) {
2950 				status = W_STOPCODE(SIGCONT);
2951 				error = copyout((caddr_t)&status,
2952 				    uap->status,
2953 				    sizeof(status));
2954 			} else {
2955 				error = 0;
2956 			}
2957 			goto out;
2958 		}
2959 		p->p_listflag &= ~P_LIST_WAITING;
2960 		wakeup(&p->p_stat);
2961 	}
2962 	/* list lock is held when we get here any which way */
2963 	if (nfound == 0) {
2964 		proc_list_unlock();
2965 		return ECHILD;
2966 	}
2967 
2968 	if (uap->options & WNOHANG) {
2969 		retval[0] = 0;
2970 		proc_list_unlock();
2971 		return 0;
2972 	}
2973 
2974 	/* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
2975 	uth = current_uthread();
2976 	wait4_data = &uth->uu_save.uus_wait4_data;
2977 	wait4_data->args = uap;
2978 	wait4_data->retval = retval;
2979 
2980 	thread_set_pending_block_hint(current_thread(), kThreadWaitOnProcess);
2981 	if ((error = msleep0((caddr_t)q, &proc_list_mlock, PWAIT | PCATCH | PDROP, "wait", 0, wait1continue))) {
2982 		return error;
2983 	}
2984 
2985 	goto loop;
2986 out:
2987 	proc_list_lock();
2988 	p->p_listflag &= ~P_LIST_WAITING;
2989 	wakeup(&p->p_stat);
2990 	proc_list_unlock();
2991 	return error;
2992 }
2993 
2994 #if DEBUG
2995 #define ASSERT_LCK_MTX_OWNED(lock)      \
2996 	                        lck_mtx_assert(lock, LCK_MTX_ASSERT_OWNED)
2997 #else
2998 #define ASSERT_LCK_MTX_OWNED(lock)      /* nothing */
2999 #endif
3000 
3001 int
waitidcontinue(int result)3002 waitidcontinue(int result)
3003 {
3004 	proc_t p;
3005 	thread_t thread;
3006 	uthread_t uth;
3007 	struct _waitid_data *waitid_data;
3008 	struct waitid_nocancel_args *uap;
3009 	int *retval;
3010 
3011 	if (result) {
3012 		return result;
3013 	}
3014 
3015 	p = current_proc();
3016 	thread = current_thread();
3017 	uth = (struct uthread *)get_bsdthread_info(thread);
3018 
3019 	waitid_data = &uth->uu_save.uus_waitid_data;
3020 	uap = waitid_data->args;
3021 	retval = waitid_data->retval;
3022 	return waitid_nocancel(p, uap, retval);
3023 }
3024 
3025 /*
3026  * Description:	Suspend the calling thread until one child of the process
3027  *		containing the calling thread changes state.
3028  *
3029  * Parameters:	uap->idtype		one of P_PID, P_PGID, P_ALL
3030  *		uap->id			pid_t or gid_t or ignored
3031  *		uap->infop		Address of siginfo_t struct in
3032  *					user space into which to return status
3033  *		uap->options		flag values
3034  *
3035  * Returns:	0			Success
3036  *		!0			Error returning status to user space
3037  */
3038 int
waitid(proc_t q,struct waitid_args * uap,int32_t * retval)3039 waitid(proc_t q, struct waitid_args *uap, int32_t *retval)
3040 {
3041 	__pthread_testcancel(1);
3042 	return waitid_nocancel(q, (struct waitid_nocancel_args *)uap, retval);
3043 }
3044 
3045 int
waitid_nocancel(proc_t q,struct waitid_nocancel_args * uap,__unused int32_t * retval)3046 waitid_nocancel(proc_t q, struct waitid_nocancel_args *uap,
3047     __unused int32_t *retval)
3048 {
3049 	user_siginfo_t  siginfo;        /* siginfo data to return to caller */
3050 	boolean_t caller64 = IS_64BIT_PROCESS(q);
3051 	int nfound;
3052 	proc_t p;
3053 	int error;
3054 	uthread_t uth;
3055 	struct _waitid_data *waitid_data;
3056 
3057 	if (uap->options == 0 ||
3058 	    (uap->options & ~(WNOHANG | WNOWAIT | WCONTINUED | WSTOPPED | WEXITED))) {
3059 		return EINVAL;        /* bits set that aren't recognized */
3060 	}
3061 	switch (uap->idtype) {
3062 	case P_PID:     /* child with process ID equal to... */
3063 	case P_PGID:    /* child with process group ID equal to... */
3064 		if (((int)uap->id) < 0) {
3065 			return EINVAL;
3066 		}
3067 		break;
3068 	case P_ALL:     /* any child */
3069 		break;
3070 	}
3071 
3072 loop:
3073 	proc_list_lock();
3074 loop1:
3075 	nfound = 0;
3076 
3077 	PCHILDREN_FOREACH(q, p) {
3078 		switch (uap->idtype) {
3079 		case P_PID:     /* child with process ID equal to... */
3080 			if (proc_getpid(p) != (pid_t)uap->id) {
3081 				continue;
3082 			}
3083 			break;
3084 		case P_PGID:    /* child with process group ID equal to... */
3085 			if (p->p_pgrpid != (pid_t)uap->id) {
3086 				continue;
3087 			}
3088 			break;
3089 		case P_ALL:     /* any child */
3090 			break;
3091 		}
3092 
3093 		if (proc_is_shadow(p)) {
3094 			continue;
3095 		}
3096 		/* XXX This is racy because we don't get the lock!!!! */
3097 
3098 		/*
3099 		 * Wait collision; go to sleep and restart; used to maintain
3100 		 * the single return for waited process guarantee.
3101 		 */
3102 		if (p->p_listflag & P_LIST_WAITING) {
3103 			(void) msleep(&p->p_stat, &proc_list_mlock,
3104 			    PWAIT, "waitidcoll", 0);
3105 			goto loop1;
3106 		}
3107 		p->p_listflag |= P_LIST_WAITING;                /* mark busy */
3108 
3109 		nfound++;
3110 
3111 		bzero(&siginfo, sizeof(siginfo));
3112 
3113 		switch (p->p_stat) {
3114 		case SZOMB:             /* Exited */
3115 			if (!(uap->options & WEXITED)) {
3116 				break;
3117 			}
3118 			proc_list_unlock();
3119 #if CONFIG_MACF
3120 			if ((error = mac_proc_check_wait(q, p)) != 0) {
3121 				goto out;
3122 			}
3123 #endif
3124 			siginfo.si_signo = SIGCHLD;
3125 			siginfo.si_pid = proc_getpid(p);
3126 
3127 			/* If the child terminated abnormally due to a signal, the signum
3128 			 * needs to be preserved in the exit status.
3129 			 */
3130 			if (WIFSIGNALED(p->p_xstat)) {
3131 				siginfo.si_code = WCOREDUMP(p->p_xstat) ?
3132 				    CLD_DUMPED : CLD_KILLED;
3133 				siginfo.si_status = WTERMSIG(p->p_xstat);
3134 			} else {
3135 				siginfo.si_code = CLD_EXITED;
3136 				siginfo.si_status = WEXITSTATUS(p->p_xstat) & 0x00FFFFFF;
3137 			}
3138 			siginfo.si_status |= (((uint32_t)(p->p_xhighbits) << 24) & 0xFF000000);
3139 			p->p_xhighbits = 0;
3140 
3141 			if ((error = copyoutsiginfo(&siginfo,
3142 			    caller64, uap->infop)) != 0) {
3143 				goto out;
3144 			}
3145 
3146 			/* Prevent other process for waiting for this event? */
3147 			if (!(uap->options & WNOWAIT)) {
3148 				reap_child_locked(q, p, 0);
3149 				return 0;
3150 			}
3151 			goto out;
3152 
3153 		case SSTOP:             /* Stopped */
3154 			/*
3155 			 * If we are not interested in stopped processes, then
3156 			 * ignore this one.
3157 			 */
3158 			if (!(uap->options & WSTOPPED)) {
3159 				break;
3160 			}
3161 
3162 			/*
3163 			 * If someone has already waited it, we lost a race
3164 			 * to be the one to return status.
3165 			 */
3166 			if ((p->p_lflag & P_LWAITED) != 0) {
3167 				break;
3168 			}
3169 			proc_list_unlock();
3170 #if CONFIG_MACF
3171 			if ((error = mac_proc_check_wait(q, p)) != 0) {
3172 				goto out;
3173 			}
3174 #endif
3175 			siginfo.si_signo = SIGCHLD;
3176 			siginfo.si_pid = proc_getpid(p);
3177 			siginfo.si_status = p->p_xstat; /* signal number */
3178 			siginfo.si_code = CLD_STOPPED;
3179 
3180 			if ((error = copyoutsiginfo(&siginfo,
3181 			    caller64, uap->infop)) != 0) {
3182 				goto out;
3183 			}
3184 
3185 			/* Prevent other process for waiting for this event? */
3186 			if (!(uap->options & WNOWAIT)) {
3187 				proc_lock(p);
3188 				p->p_lflag |= P_LWAITED;
3189 				proc_unlock(p);
3190 			}
3191 			goto out;
3192 
3193 		default:                /* All other states => Continued */
3194 			if (!(uap->options & WCONTINUED)) {
3195 				break;
3196 			}
3197 
3198 			/*
3199 			 * If the flag isn't set, then this process has not
3200 			 * been stopped and continued, or the status has
3201 			 * already been reaped by another caller of waitid().
3202 			 */
3203 			if ((p->p_flag & P_CONTINUED) == 0) {
3204 				break;
3205 			}
3206 			proc_list_unlock();
3207 #if CONFIG_MACF
3208 			if ((error = mac_proc_check_wait(q, p)) != 0) {
3209 				goto out;
3210 			}
3211 #endif
3212 			siginfo.si_signo = SIGCHLD;
3213 			siginfo.si_code = CLD_CONTINUED;
3214 			proc_lock(p);
3215 			siginfo.si_pid = p->p_contproc;
3216 			siginfo.si_status = p->p_xstat;
3217 			proc_unlock(p);
3218 
3219 			if ((error = copyoutsiginfo(&siginfo,
3220 			    caller64, uap->infop)) != 0) {
3221 				goto out;
3222 			}
3223 
3224 			/* Prevent other process for waiting for this event? */
3225 			if (!(uap->options & WNOWAIT)) {
3226 				OSBitAndAtomic(~((uint32_t)P_CONTINUED),
3227 				    &p->p_flag);
3228 			}
3229 			goto out;
3230 		}
3231 		ASSERT_LCK_MTX_OWNED(&proc_list_mlock);
3232 
3233 		/* Not a process we are interested in; go on to next child */
3234 
3235 		p->p_listflag &= ~P_LIST_WAITING;
3236 		wakeup(&p->p_stat);
3237 	}
3238 	ASSERT_LCK_MTX_OWNED(&proc_list_mlock);
3239 
3240 	/* No child processes that could possibly satisfy the request? */
3241 
3242 	if (nfound == 0) {
3243 		proc_list_unlock();
3244 		return ECHILD;
3245 	}
3246 
3247 	if (uap->options & WNOHANG) {
3248 		proc_list_unlock();
3249 #if CONFIG_MACF
3250 		if ((error = mac_proc_check_wait(q, p)) != 0) {
3251 			return error;
3252 		}
3253 #endif
3254 		/*
3255 		 * The state of the siginfo structure in this case
3256 		 * is undefined.  Some implementations bzero it, some
3257 		 * (like here) leave it untouched for efficiency.
3258 		 *
3259 		 * Thus the most portable check for "no matching pid with
3260 		 * WNOHANG" is to store a zero into si_pid before
3261 		 * invocation, then check for a non-zero value afterwards.
3262 		 */
3263 		return 0;
3264 	}
3265 
3266 	/* Save arguments for continuation. Backing storage is in uthread->uu_arg, and will not be deallocated */
3267 	uth = current_uthread();
3268 	waitid_data = &uth->uu_save.uus_waitid_data;
3269 	waitid_data->args = uap;
3270 	waitid_data->retval = retval;
3271 
3272 	if ((error = msleep0(q, &proc_list_mlock,
3273 	    PWAIT | PCATCH | PDROP, "waitid", 0, waitidcontinue)) != 0) {
3274 		return error;
3275 	}
3276 
3277 	goto loop;
3278 out:
3279 	proc_list_lock();
3280 	p->p_listflag &= ~P_LIST_WAITING;
3281 	wakeup(&p->p_stat);
3282 	proc_list_unlock();
3283 	return error;
3284 }
3285 
3286 /*
3287  * make process 'parent' the new parent of process 'child'.
3288  */
3289 void
proc_reparentlocked(proc_t child,proc_t parent,int signallable,int locked)3290 proc_reparentlocked(proc_t child, proc_t parent, int signallable, int locked)
3291 {
3292 	proc_t oldparent = PROC_NULL;
3293 
3294 	if (child->p_pptr == parent) {
3295 		return;
3296 	}
3297 
3298 	if (locked == 0) {
3299 		proc_list_lock();
3300 	}
3301 
3302 	oldparent = child->p_pptr;
3303 #if __PROC_INTERNAL_DEBUG
3304 	if (oldparent == PROC_NULL) {
3305 		panic("proc_reparent: process %p does not have a parent", child);
3306 	}
3307 #endif
3308 
3309 	LIST_REMOVE(child, p_sibling);
3310 #if __PROC_INTERNAL_DEBUG
3311 	if (oldparent->p_childrencnt == 0) {
3312 		panic("process children count already 0");
3313 	}
3314 #endif
3315 	oldparent->p_childrencnt--;
3316 #if __PROC_INTERNAL_DEBUG
3317 	if (oldparent->p_childrencnt < 0) {
3318 		panic("process children count -ve");
3319 	}
3320 #endif
3321 	LIST_INSERT_HEAD(&parent->p_children, child, p_sibling);
3322 	parent->p_childrencnt++;
3323 	child->p_pptr = parent;
3324 	child->p_ppid = proc_getpid(parent);
3325 
3326 	proc_list_unlock();
3327 
3328 	if ((signallable != 0) && (initproc == parent) && (child->p_stat == SZOMB)) {
3329 		psignal(initproc, SIGCHLD);
3330 	}
3331 	if (locked == 1) {
3332 		proc_list_lock();
3333 	}
3334 }
3335 
3336 /*
3337  * Exit: deallocate address space and other resources, change proc state
3338  * to zombie, and unlink proc from allproc and parent's lists.  Save exit
3339  * status and rusage for wait().  Check for child processes and orphan them.
3340  */
3341 
3342 
3343 /*
3344  * munge_rusage
3345  *	LP64 support - long is 64 bits if we are dealing with a 64 bit user
3346  *	process.  We munge the kernel version of rusage into the
3347  *	64 bit version.
3348  */
3349 __private_extern__  void
munge_user64_rusage(struct rusage * a_rusage_p,struct user64_rusage * a_user_rusage_p)3350 munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p)
3351 {
3352 	/* Zero-out struct so that padding is cleared */
3353 	bzero(a_user_rusage_p, sizeof(struct user64_rusage));
3354 
3355 	/* timeval changes size, so utime and stime need special handling */
3356 	a_user_rusage_p->ru_utime.tv_sec = a_rusage_p->ru_utime.tv_sec;
3357 	a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
3358 	a_user_rusage_p->ru_stime.tv_sec = a_rusage_p->ru_stime.tv_sec;
3359 	a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
3360 	/*
3361 	 * everything else can be a direct assign, since there is no loss
3362 	 * of precision implied boing 32->64.
3363 	 */
3364 	a_user_rusage_p->ru_maxrss = a_rusage_p->ru_maxrss;
3365 	a_user_rusage_p->ru_ixrss = a_rusage_p->ru_ixrss;
3366 	a_user_rusage_p->ru_idrss = a_rusage_p->ru_idrss;
3367 	a_user_rusage_p->ru_isrss = a_rusage_p->ru_isrss;
3368 	a_user_rusage_p->ru_minflt = a_rusage_p->ru_minflt;
3369 	a_user_rusage_p->ru_majflt = a_rusage_p->ru_majflt;
3370 	a_user_rusage_p->ru_nswap = a_rusage_p->ru_nswap;
3371 	a_user_rusage_p->ru_inblock = a_rusage_p->ru_inblock;
3372 	a_user_rusage_p->ru_oublock = a_rusage_p->ru_oublock;
3373 	a_user_rusage_p->ru_msgsnd = a_rusage_p->ru_msgsnd;
3374 	a_user_rusage_p->ru_msgrcv = a_rusage_p->ru_msgrcv;
3375 	a_user_rusage_p->ru_nsignals = a_rusage_p->ru_nsignals;
3376 	a_user_rusage_p->ru_nvcsw = a_rusage_p->ru_nvcsw;
3377 	a_user_rusage_p->ru_nivcsw = a_rusage_p->ru_nivcsw;
3378 }
3379 
3380 /* For a 64-bit kernel and 32-bit userspace, munging may be needed */
3381 __private_extern__  void
munge_user32_rusage(struct rusage * a_rusage_p,struct user32_rusage * a_user_rusage_p)3382 munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p)
3383 {
3384 	bzero(a_user_rusage_p, sizeof(struct user32_rusage));
3385 
3386 	/* timeval changes size, so utime and stime need special handling */
3387 	a_user_rusage_p->ru_utime.tv_sec = (user32_time_t)a_rusage_p->ru_utime.tv_sec;
3388 	a_user_rusage_p->ru_utime.tv_usec = a_rusage_p->ru_utime.tv_usec;
3389 	a_user_rusage_p->ru_stime.tv_sec = (user32_time_t)a_rusage_p->ru_stime.tv_sec;
3390 	a_user_rusage_p->ru_stime.tv_usec = a_rusage_p->ru_stime.tv_usec;
3391 	/*
3392 	 * everything else can be a direct assign. We currently ignore
3393 	 * the loss of precision
3394 	 */
3395 	a_user_rusage_p->ru_maxrss = (user32_long_t)a_rusage_p->ru_maxrss;
3396 	a_user_rusage_p->ru_ixrss = (user32_long_t)a_rusage_p->ru_ixrss;
3397 	a_user_rusage_p->ru_idrss = (user32_long_t)a_rusage_p->ru_idrss;
3398 	a_user_rusage_p->ru_isrss = (user32_long_t)a_rusage_p->ru_isrss;
3399 	a_user_rusage_p->ru_minflt = (user32_long_t)a_rusage_p->ru_minflt;
3400 	a_user_rusage_p->ru_majflt = (user32_long_t)a_rusage_p->ru_majflt;
3401 	a_user_rusage_p->ru_nswap = (user32_long_t)a_rusage_p->ru_nswap;
3402 	a_user_rusage_p->ru_inblock = (user32_long_t)a_rusage_p->ru_inblock;
3403 	a_user_rusage_p->ru_oublock = (user32_long_t)a_rusage_p->ru_oublock;
3404 	a_user_rusage_p->ru_msgsnd = (user32_long_t)a_rusage_p->ru_msgsnd;
3405 	a_user_rusage_p->ru_msgrcv = (user32_long_t)a_rusage_p->ru_msgrcv;
3406 	a_user_rusage_p->ru_nsignals = (user32_long_t)a_rusage_p->ru_nsignals;
3407 	a_user_rusage_p->ru_nvcsw = (user32_long_t)a_rusage_p->ru_nvcsw;
3408 	a_user_rusage_p->ru_nivcsw = (user32_long_t)a_rusage_p->ru_nivcsw;
3409 }
3410 
3411 void
kdp_wait4_find_process(thread_t thread,__unused event64_t wait_event,thread_waitinfo_t * waitinfo)3412 kdp_wait4_find_process(thread_t thread, __unused event64_t wait_event, thread_waitinfo_t *waitinfo)
3413 {
3414 	assert(thread != NULL);
3415 	assert(waitinfo != NULL);
3416 
3417 	struct uthread *ut = get_bsdthread_info(thread);
3418 	waitinfo->context = 0;
3419 	// ensure wmesg is consistent with a thread waiting in wait4
3420 	assert(!strcmp(ut->uu_wmesg, "waitcoll") || !strcmp(ut->uu_wmesg, "wait"));
3421 	struct wait4_nocancel_args *args = ut->uu_save.uus_wait4_data.args;
3422 	// May not actually contain a pid; this is just the argument to wait4.
3423 	// See man wait4 for other valid wait4 arguments.
3424 	waitinfo->owner = args->pid;
3425 }
3426 
3427 int
exit_with_guard_exception(proc_t p,mach_exception_data_type_t code,mach_exception_data_type_t subcode)3428 exit_with_guard_exception(
3429 	proc_t p,
3430 	mach_exception_data_type_t code,
3431 	mach_exception_data_type_t subcode)
3432 {
3433 	os_reason_t reason = os_reason_create(OS_REASON_GUARD, (uint64_t)code);
3434 	assert(reason != OS_REASON_NULL);
3435 
3436 	return exit_with_mach_exception(p, reason, EXC_GUARD, code, subcode);
3437 }
3438 
3439 #if __has_feature(ptrauth_calls)
3440 int
exit_with_pac_exception(proc_t p,exception_type_t exception,mach_exception_code_t code,mach_exception_subcode_t subcode)3441 exit_with_pac_exception(proc_t p, exception_type_t exception, mach_exception_code_t code,
3442     mach_exception_subcode_t subcode)
3443 {
3444 	os_reason_t reason = os_reason_create(OS_REASON_PAC_EXCEPTION, (uint64_t)code);
3445 	assert(reason != OS_REASON_NULL);
3446 
3447 	return exit_with_mach_exception(p, reason, exception, code, subcode);
3448 }
3449 #endif /* __has_feature(ptrauth_calls) */
3450 
3451 int
exit_with_port_space_exception(proc_t p,mach_exception_data_type_t code,mach_exception_data_type_t subcode)3452 exit_with_port_space_exception(proc_t p, mach_exception_data_type_t code,
3453     mach_exception_data_type_t subcode)
3454 {
3455 	os_reason_t reason = os_reason_create(OS_REASON_PORT_SPACE, (uint64_t)code);
3456 	assert(reason != OS_REASON_NULL);
3457 
3458 	return exit_with_mach_exception(p, reason, EXC_RESOURCE, code, subcode);
3459 }
3460 
3461 static int
exit_with_mach_exception(proc_t p,os_reason_t reason,exception_type_t exception,mach_exception_code_t code,mach_exception_subcode_t subcode)3462 exit_with_mach_exception(proc_t p, os_reason_t reason, exception_type_t exception, mach_exception_code_t code,
3463     mach_exception_subcode_t subcode)
3464 {
3465 	thread_t self = current_thread();
3466 	struct uthread *ut = get_bsdthread_info(self);
3467 
3468 	ut->uu_exception = exception;
3469 	ut->uu_code = code;
3470 	ut->uu_subcode = subcode;
3471 
3472 	reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
3473 	return exit_with_reason(p, W_EXITCODE(0, SIGKILL), NULL,
3474 	           TRUE, FALSE, 0, reason);
3475 }
3476