xref: /xnu-11417.101.15/tests/vm/corpse_owned_vmobjects.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*e3723e1fSApple OSS Distributions  *
6*e3723e1fSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*e3723e1fSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*e3723e1fSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*e3723e1fSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*e3723e1fSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*e3723e1fSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*e3723e1fSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*e3723e1fSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*e3723e1fSApple OSS Distributions  *
15*e3723e1fSApple OSS Distributions  * Please obtain a copy of the License at
16*e3723e1fSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*e3723e1fSApple OSS Distributions  *
18*e3723e1fSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*e3723e1fSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*e3723e1fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*e3723e1fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*e3723e1fSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*e3723e1fSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*e3723e1fSApple OSS Distributions  * limitations under the License.
25*e3723e1fSApple OSS Distributions  *
26*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*e3723e1fSApple OSS Distributions  */
28*e3723e1fSApple OSS Distributions 
29*e3723e1fSApple OSS Distributions #include <signal.h>
30*e3723e1fSApple OSS Distributions #include <spawn.h>
31*e3723e1fSApple OSS Distributions #include <stdlib.h>
32*e3723e1fSApple OSS Distributions #include <sys/sysctl.h>
33*e3723e1fSApple OSS Distributions #include <sys/ptrace.h>
34*e3723e1fSApple OSS Distributions #include <sys/types.h>
35*e3723e1fSApple OSS Distributions #include <mach/mach.h>
36*e3723e1fSApple OSS Distributions #include <excserver.h>
37*e3723e1fSApple OSS Distributions #include <sys/mman.h>
38*e3723e1fSApple OSS Distributions #include <kern/exc_resource.h>
39*e3723e1fSApple OSS Distributions #include <TargetConditionals.h>
40*e3723e1fSApple OSS Distributions #include <mach/vm_page_size.h>
41*e3723e1fSApple OSS Distributions #include <sys/spawn_internal.h>
42*e3723e1fSApple OSS Distributions #include <mach/mach_vm.h>
43*e3723e1fSApple OSS Distributions 
44*e3723e1fSApple OSS Distributions #include <darwintest.h>
45*e3723e1fSApple OSS Distributions #include <dispatch/dispatch.h>
46*e3723e1fSApple OSS Distributions #include <mach-o/dyld.h>
47*e3723e1fSApple OSS Distributions 
48*e3723e1fSApple OSS Distributions /* internal */
49*e3723e1fSApple OSS Distributions #include <spawn_private.h>
50*e3723e1fSApple OSS Distributions #include <sys/kern_memorystatus.h>
51*e3723e1fSApple OSS Distributions 
52*e3723e1fSApple OSS Distributions #define TEST_MEMLIMIT_MB 10
53*e3723e1fSApple OSS Distributions #define SEM_TIMEOUT dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC)
54*e3723e1fSApple OSS Distributions #define OWNED_VMOBJECTS_SYSCTL "vm.get_owned_vmobjects"
55*e3723e1fSApple OSS Distributions 
56*e3723e1fSApple OSS Distributions T_GLOBAL_META(
57*e3723e1fSApple OSS Distributions 	T_META_NAMESPACE("xnu.memorystatus"),
58*e3723e1fSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
59*e3723e1fSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"));
60*e3723e1fSApple OSS Distributions 
61*e3723e1fSApple OSS Distributions /* Globals */
62*e3723e1fSApple OSS Distributions static dispatch_semaphore_t sync_sema;
63*e3723e1fSApple OSS Distributions static pid_t child_pid;
64*e3723e1fSApple OSS Distributions static bool caught_crash = false, caught_corpse = false;
65*e3723e1fSApple OSS Distributions mach_port_t exception_port;
66*e3723e1fSApple OSS Distributions 
67*e3723e1fSApple OSS Distributions /* Exception  */
68*e3723e1fSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state(mach_port_t exception_port,exception_type_t exception,const mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,const thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)69*e3723e1fSApple OSS Distributions catch_mach_exception_raise_state(mach_port_t exception_port,
70*e3723e1fSApple OSS Distributions     exception_type_t exception,
71*e3723e1fSApple OSS Distributions     const mach_exception_data_t code,
72*e3723e1fSApple OSS Distributions     mach_msg_type_number_t code_count,
73*e3723e1fSApple OSS Distributions     int * flavor,
74*e3723e1fSApple OSS Distributions     const thread_state_t old_state,
75*e3723e1fSApple OSS Distributions     mach_msg_type_number_t old_state_count,
76*e3723e1fSApple OSS Distributions     thread_state_t new_state,
77*e3723e1fSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
78*e3723e1fSApple OSS Distributions {
79*e3723e1fSApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
80*e3723e1fSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state");
81*e3723e1fSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
82*e3723e1fSApple OSS Distributions }
83*e3723e1fSApple OSS Distributions 
84*e3723e1fSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)85*e3723e1fSApple OSS Distributions catch_mach_exception_raise_state_identity(mach_port_t exception_port,
86*e3723e1fSApple OSS Distributions     mach_port_t thread,
87*e3723e1fSApple OSS Distributions     mach_port_t task,
88*e3723e1fSApple OSS Distributions     exception_type_t exception,
89*e3723e1fSApple OSS Distributions     mach_exception_data_t code,
90*e3723e1fSApple OSS Distributions     mach_msg_type_number_t code_count,
91*e3723e1fSApple OSS Distributions     int * flavor,
92*e3723e1fSApple OSS Distributions     thread_state_t old_state,
93*e3723e1fSApple OSS Distributions     mach_msg_type_number_t old_state_count,
94*e3723e1fSApple OSS Distributions     thread_state_t new_state,
95*e3723e1fSApple OSS Distributions     mach_msg_type_number_t * new_state_count)
96*e3723e1fSApple OSS Distributions {
97*e3723e1fSApple OSS Distributions #pragma unused(exception_port, exception, code, code_count, flavor, old_state, old_state_count, new_state, new_state_count)
98*e3723e1fSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
99*e3723e1fSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
100*e3723e1fSApple OSS Distributions }
101*e3723e1fSApple OSS Distributions 
102*e3723e1fSApple OSS Distributions kern_return_t
catch_mach_exception_raise_state_identity_protected(mach_port_t exception_port,uint64_t thread_id,mach_port_t task_id_token,exception_type_t exception,mach_exception_data_t codes,mach_msg_type_number_t codeCnt,int * flavor,thread_state_t old_state,mach_msg_type_number_t old_state_count,thread_state_t new_state,mach_msg_type_number_t * new_state_count)103*e3723e1fSApple OSS Distributions catch_mach_exception_raise_state_identity_protected(
104*e3723e1fSApple OSS Distributions 	mach_port_t exception_port,
105*e3723e1fSApple OSS Distributions 	uint64_t thread_id,
106*e3723e1fSApple OSS Distributions 	mach_port_t task_id_token,
107*e3723e1fSApple OSS Distributions 	exception_type_t exception,
108*e3723e1fSApple OSS Distributions 	mach_exception_data_t codes,
109*e3723e1fSApple OSS Distributions 	mach_msg_type_number_t codeCnt,
110*e3723e1fSApple OSS Distributions 	int * flavor,
111*e3723e1fSApple OSS Distributions 	thread_state_t old_state,
112*e3723e1fSApple OSS Distributions 	mach_msg_type_number_t old_state_count,
113*e3723e1fSApple OSS Distributions 	thread_state_t new_state,
114*e3723e1fSApple OSS Distributions 	mach_msg_type_number_t * new_state_count)
115*e3723e1fSApple OSS Distributions {
116*e3723e1fSApple OSS Distributions #pragma unused(exception_port, thread_id, task_id_token, exception, codes, codeCnt, flavor, old_state, old_state_count, new_state, new_state_count)
117*e3723e1fSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
118*e3723e1fSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
119*e3723e1fSApple OSS Distributions }
120*e3723e1fSApple OSS Distributions 
121*e3723e1fSApple OSS Distributions kern_return_t
catch_mach_exception_raise_identity_protected(mach_port_t exception_port,uint64_t thread_id,mach_port_t task_id_token,exception_type_t exception,mach_exception_data_t codes,mach_msg_type_number_t codeCnt)122*e3723e1fSApple OSS Distributions catch_mach_exception_raise_identity_protected(
123*e3723e1fSApple OSS Distributions 	mach_port_t               exception_port,
124*e3723e1fSApple OSS Distributions 	uint64_t                  thread_id,
125*e3723e1fSApple OSS Distributions 	mach_port_t               task_id_token,
126*e3723e1fSApple OSS Distributions 	exception_type_t          exception,
127*e3723e1fSApple OSS Distributions 	mach_exception_data_t     codes,
128*e3723e1fSApple OSS Distributions 	mach_msg_type_number_t    codeCnt)
129*e3723e1fSApple OSS Distributions {
130*e3723e1fSApple OSS Distributions #pragma unused(exception_port, thread_id, task_id_token, exception, codes, codeCnt)
131*e3723e1fSApple OSS Distributions 	T_FAIL("Unsupported catch_mach_exception_raise_state_identity");
132*e3723e1fSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
133*e3723e1fSApple OSS Distributions }
134*e3723e1fSApple OSS Distributions 
135*e3723e1fSApple OSS Distributions void
verify_owned_vmobjects(task_t task)136*e3723e1fSApple OSS Distributions verify_owned_vmobjects(task_t task)
137*e3723e1fSApple OSS Distributions {
138*e3723e1fSApple OSS Distributions 	int ret;
139*e3723e1fSApple OSS Distributions 	size_t owned_vmobjects_len;
140*e3723e1fSApple OSS Distributions 
141*e3723e1fSApple OSS Distributions 	ret = sysctlbyname(OWNED_VMOBJECTS_SYSCTL, NULL, &owned_vmobjects_len, &task, sizeof(task));
142*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl " OWNED_VMOBJECTS_SYSCTL);
143*e3723e1fSApple OSS Distributions 	T_EXPECT_GT((int) owned_vmobjects_len, 0, "owned vmobjects list is populated on %s", (task == mach_task_self()) ? "self" : "corpse");
144*e3723e1fSApple OSS Distributions }
145*e3723e1fSApple OSS Distributions 
146*e3723e1fSApple OSS Distributions kern_return_t
catch_mach_exception_raise(mach_port_t exception_port,mach_port_t thread,mach_port_t task,exception_type_t exception,mach_exception_data_t code,mach_msg_type_number_t code_count)147*e3723e1fSApple OSS Distributions catch_mach_exception_raise(mach_port_t exception_port,
148*e3723e1fSApple OSS Distributions     mach_port_t thread,
149*e3723e1fSApple OSS Distributions     mach_port_t task,
150*e3723e1fSApple OSS Distributions     exception_type_t exception,
151*e3723e1fSApple OSS Distributions     mach_exception_data_t code,
152*e3723e1fSApple OSS Distributions     mach_msg_type_number_t code_count)
153*e3723e1fSApple OSS Distributions {
154*e3723e1fSApple OSS Distributions #pragma unused(thread, task, code, code_count)
155*e3723e1fSApple OSS Distributions 	T_QUIET; T_EXPECT_TRUE((exception == EXC_CRASH) || (exception == EXC_CORPSE_NOTIFY), "catch_mach_exception_raise() catches EXC_CRASH or EXC_CORPSE_NOTIFY");
156*e3723e1fSApple OSS Distributions 	if (exception == EXC_CRASH) {
157*e3723e1fSApple OSS Distributions 		caught_crash = true;
158*e3723e1fSApple OSS Distributions 		return KERN_SUCCESS;
159*e3723e1fSApple OSS Distributions 	} else if (exception == EXC_CORPSE_NOTIFY) {
160*e3723e1fSApple OSS Distributions 		caught_corpse = true;
161*e3723e1fSApple OSS Distributions 		verify_owned_vmobjects(task);
162*e3723e1fSApple OSS Distributions 		dispatch_semaphore_signal(sync_sema);
163*e3723e1fSApple OSS Distributions 		return KERN_SUCCESS;
164*e3723e1fSApple OSS Distributions 	}
165*e3723e1fSApple OSS Distributions 	return KERN_NOT_SUPPORTED;
166*e3723e1fSApple OSS Distributions }
167*e3723e1fSApple OSS Distributions 
168*e3723e1fSApple OSS Distributions /*
169*e3723e1fSApple OSS Distributions  * Background process that will allocate enough memory to push
170*e3723e1fSApple OSS Distributions  * itself over the threshold, hopefully triggering EXC_RESOURCE.
171*e3723e1fSApple OSS Distributions  */
172*e3723e1fSApple OSS Distributions T_HELPER_DECL(i_eat_memory_for_breakfast, "") {
173*e3723e1fSApple OSS Distributions 	int ret, j, num_pages = 0;
174*e3723e1fSApple OSS Distributions 	unsigned char *buf;
175*e3723e1fSApple OSS Distributions 
176*e3723e1fSApple OSS Distributions 	if (argc == 1) {
177*e3723e1fSApple OSS Distributions 		num_pages = atoi(argv[0]);
178*e3723e1fSApple OSS Distributions 	} else {
179*e3723e1fSApple OSS Distributions 		T_FAIL("No arguments passed to memory eater");
180*e3723e1fSApple OSS Distributions 	}
181*e3723e1fSApple OSS Distributions 
182*e3723e1fSApple OSS Distributions 	/* Allocate a purgeable buffer that will show up in owned vmobjects */
183*e3723e1fSApple OSS Distributions 	mach_vm_address_t addr = 0;
184*e3723e1fSApple OSS Distributions 	ret = mach_vm_allocate(mach_task_self(), &addr, vm_page_size, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);
185*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "allocate purgeable buffer");
186*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_NE((int) addr, 0, "purgeable buffer not null");
187*e3723e1fSApple OSS Distributions 	verify_owned_vmobjects(mach_task_self());
188*e3723e1fSApple OSS Distributions 
189*e3723e1fSApple OSS Distributions 	/* Allocate and touch all our pages */
190*e3723e1fSApple OSS Distributions 	T_LOG("Allocating %d pages...", num_pages);
191*e3723e1fSApple OSS Distributions 	buf = mmap(NULL, vm_page_size * num_pages, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
192*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(buf, "mmap");
193*e3723e1fSApple OSS Distributions 	for (j = 0; j < num_pages; j++) {
194*e3723e1fSApple OSS Distributions 		((volatile unsigned char *)buf)[j * vm_page_size] = 1;
195*e3723e1fSApple OSS Distributions 	}
196*e3723e1fSApple OSS Distributions 
197*e3723e1fSApple OSS Distributions 	exit(0);
198*e3723e1fSApple OSS Distributions }
199*e3723e1fSApple OSS Distributions 
200*e3723e1fSApple OSS Distributions static void
kill_child(void)201*e3723e1fSApple OSS Distributions kill_child(void)
202*e3723e1fSApple OSS Distributions {
203*e3723e1fSApple OSS Distributions 	int ret = kill(child_pid, SIGKILL);
204*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "kill");
205*e3723e1fSApple OSS Distributions }
206*e3723e1fSApple OSS Distributions 
207*e3723e1fSApple OSS Distributions static pid_t
launch_child(int num_pages)208*e3723e1fSApple OSS Distributions launch_child(int num_pages)
209*e3723e1fSApple OSS Distributions {
210*e3723e1fSApple OSS Distributions 	extern char **environ;
211*e3723e1fSApple OSS Distributions 	int ret;
212*e3723e1fSApple OSS Distributions 	char testpath[PATH_MAX];
213*e3723e1fSApple OSS Distributions 	posix_spawnattr_t spawn_attrs;
214*e3723e1fSApple OSS Distributions 
215*e3723e1fSApple OSS Distributions 	uint32_t testpath_buf_size = PATH_MAX;
216*e3723e1fSApple OSS Distributions 	char num_pages_str[32] = {0};
217*e3723e1fSApple OSS Distributions 	char *argv[5] = {testpath, "-n", "i_eat_memory_for_breakfast", num_pages_str, NULL};
218*e3723e1fSApple OSS Distributions 
219*e3723e1fSApple OSS Distributions 	T_LOG("Spawning child process...");
220*e3723e1fSApple OSS Distributions 
221*e3723e1fSApple OSS Distributions 	/* Fork so we can keep the exception port. */
222*e3723e1fSApple OSS Distributions 	if ((child_pid = fork()) == 0) {
223*e3723e1fSApple OSS Distributions 		ret = posix_spawnattr_init(&spawn_attrs);
224*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
225*e3723e1fSApple OSS Distributions 		ret = posix_spawnattr_setjetsam_ext(&spawn_attrs, POSIX_SPAWN_JETSAM_MEMLIMIT_FATAL, JETSAM_PRIORITY_FOREGROUND, TEST_MEMLIMIT_MB, TEST_MEMLIMIT_MB);
226*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setjetsam_ext");
227*e3723e1fSApple OSS Distributions 		ret = posix_spawnattr_setflags(&spawn_attrs, POSIX_SPAWN_SETEXEC);
228*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_setflags");
229*e3723e1fSApple OSS Distributions 		ret = snprintf(num_pages_str, sizeof(num_pages_str), "%d", num_pages);
230*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_LE((size_t) ret, sizeof(num_pages_str), "Don't allocate too many pages.");
231*e3723e1fSApple OSS Distributions 		ret = _NSGetExecutablePath(testpath, &testpath_buf_size);
232*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(ret, 0, "_NSGetExecutablePath");
233*e3723e1fSApple OSS Distributions 		ret = posix_spawn(&child_pid, argv[0], NULL, &spawn_attrs, argv, environ);
234*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
235*e3723e1fSApple OSS Distributions 	}
236*e3723e1fSApple OSS Distributions 
237*e3723e1fSApple OSS Distributions 	T_ATEND(kill_child);
238*e3723e1fSApple OSS Distributions 
239*e3723e1fSApple OSS Distributions 	return child_pid;
240*e3723e1fSApple OSS Distributions }
241*e3723e1fSApple OSS Distributions 
242*e3723e1fSApple OSS Distributions void*
exc_thread(void * arg)243*e3723e1fSApple OSS Distributions exc_thread(void *arg)
244*e3723e1fSApple OSS Distributions {
245*e3723e1fSApple OSS Distributions #pragma unused(arg)
246*e3723e1fSApple OSS Distributions 	kern_return_t kr;
247*e3723e1fSApple OSS Distributions 
248*e3723e1fSApple OSS Distributions 	while (1) {
249*e3723e1fSApple OSS Distributions 		kr = mach_msg_server(mach_exc_server, MACH_MSG_SIZE_RELIABLE, exception_port, 0);
250*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_server");
251*e3723e1fSApple OSS Distributions 	}
252*e3723e1fSApple OSS Distributions }
253*e3723e1fSApple OSS Distributions 
254*e3723e1fSApple OSS Distributions T_DECL(corpse_owned_vmobjects, "vm.get_owned_vmobjects sysctl on corpses",
255*e3723e1fSApple OSS Distributions     T_META_ASROOT(true),
256*e3723e1fSApple OSS Distributions     T_META_TAG_VM_PREFERRED
257*e3723e1fSApple OSS Distributions     )
258*e3723e1fSApple OSS Distributions {
259*e3723e1fSApple OSS Distributions 	int ret;
260*e3723e1fSApple OSS Distributions 	pthread_t handle_thread;
261*e3723e1fSApple OSS Distributions 	task_t task;
262*e3723e1fSApple OSS Distributions 
263*e3723e1fSApple OSS Distributions 	T_SETUPBEGIN;
264*e3723e1fSApple OSS Distributions 
265*e3723e1fSApple OSS Distributions 	sync_sema = dispatch_semaphore_create(0);
266*e3723e1fSApple OSS Distributions 
267*e3723e1fSApple OSS Distributions 	task = mach_task_self();
268*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_NE(task, MACH_PORT_NULL, "mach_task_self");
269*e3723e1fSApple OSS Distributions 
270*e3723e1fSApple OSS Distributions 	/* Allocate a port for receiving EXC_CRASH and EXC_CORPSE_NOTIFY */
271*e3723e1fSApple OSS Distributions 	ret = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &exception_port);
272*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "mach_port_allocate");
273*e3723e1fSApple OSS Distributions 	ret = mach_port_insert_right(task, exception_port, exception_port, MACH_MSG_TYPE_MAKE_SEND);
274*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "mach_port_insert_right");
275*e3723e1fSApple OSS Distributions 	ret = task_set_exception_ports(task, EXC_MASK_CRASH | EXC_MASK_CORPSE_NOTIFY, exception_port, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, 0);
276*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "task_set_exception_ports");
277*e3723e1fSApple OSS Distributions 
278*e3723e1fSApple OSS Distributions 	T_SETUPEND;
279*e3723e1fSApple OSS Distributions 
280*e3723e1fSApple OSS Distributions 	/* Spawn exception handling thread */
281*e3723e1fSApple OSS Distributions 	ret = pthread_create(&handle_thread, NULL, exc_thread, 0);
282*e3723e1fSApple OSS Distributions 
283*e3723e1fSApple OSS Distributions 	/* Spawn child to eat memory and trigger EXC_RESOURCE */
284*e3723e1fSApple OSS Distributions 	launch_child((TEST_MEMLIMIT_MB * (1 << 20)) / vm_page_size);
285*e3723e1fSApple OSS Distributions 
286*e3723e1fSApple OSS Distributions 	/* We should receive an exception */
287*e3723e1fSApple OSS Distributions 	dispatch_semaphore_wait(sync_sema, dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC));
288*e3723e1fSApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(caught_crash, true, "Caught EXC_CRASH");
289*e3723e1fSApple OSS Distributions 	T_QUIET; T_EXPECT_EQ(caught_corpse, true, "Caught EXC_CORPSE_NOTIFY");
290*e3723e1fSApple OSS Distributions }
291