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