xref: /xnu-8792.41.9/tests/memorystatus_vm_map_fork.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #include <stdio.h>
2*5c2921b0SApple OSS Distributions #include <unistd.h>
3*5c2921b0SApple OSS Distributions #include <stdlib.h>
4*5c2921b0SApple OSS Distributions #include <errno.h>
5*5c2921b0SApple OSS Distributions #include <string.h>
6*5c2921b0SApple OSS Distributions #include <assert.h>
7*5c2921b0SApple OSS Distributions #include <signal.h>
8*5c2921b0SApple OSS Distributions #include <spawn.h>
9*5c2921b0SApple OSS Distributions #include <spawn_private.h>
10*5c2921b0SApple OSS Distributions #include <stdint.h>
11*5c2921b0SApple OSS Distributions #include <sys/sysctl.h>
12*5c2921b0SApple OSS Distributions #include <sys/spawn_internal.h>
13*5c2921b0SApple OSS Distributions #include <sys/kern_memorystatus.h>
14*5c2921b0SApple OSS Distributions #include <mach-o/dyld.h>
15*5c2921b0SApple OSS Distributions 
16*5c2921b0SApple OSS Distributions #include <darwintest.h>
17*5c2921b0SApple OSS Distributions #include <darwintest_utils.h>
18*5c2921b0SApple OSS Distributions 
19*5c2921b0SApple OSS Distributions #include "test_utils.h"
20*5c2921b0SApple OSS Distributions 
21*5c2921b0SApple OSS Distributions T_GLOBAL_META(
22*5c2921b0SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
23*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
24*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"),
25*5c2921b0SApple OSS Distributions 	T_META_CHECK_LEAKS(false)
26*5c2921b0SApple OSS Distributions 	);
27*5c2921b0SApple OSS Distributions 
28*5c2921b0SApple OSS Distributions extern char **environ;
29*5c2921b0SApple OSS Distributions 
30*5c2921b0SApple OSS Distributions /*
31*5c2921b0SApple OSS Distributions  * This test file contains two sub-tests which attempt to verify
32*5c2921b0SApple OSS Distributions  * the allowing or not allowing of a corpse for crashreporter when
33*5c2921b0SApple OSS Distributions  * a task exceeds its memory allocation limit. vm_map_fork() is the
34*5c2921b0SApple OSS Distributions  * kernel routine used to generate a corpse task.
35*5c2921b0SApple OSS Distributions  *
36*5c2921b0SApple OSS Distributions  * A corpse is allowed to be taken if a task's memory resource limit that
37*5c2921b0SApple OSS Distributions  * is exceeded is less than 1/4 of the system wide task limit.
38*5c2921b0SApple OSS Distributions  * If the amount exceeds 1/4 the sytem wide limit, then the corpse is disallowed.
39*5c2921b0SApple OSS Distributions  *
40*5c2921b0SApple OSS Distributions  * If the device under test is already under pressure, the test
41*5c2921b0SApple OSS Distributions  * could fail due to jetsam cutting in and killing the parent, child or
42*5c2921b0SApple OSS Distributions  * other necessary testing processes.
43*5c2921b0SApple OSS Distributions  */
44*5c2921b0SApple OSS Distributions 
45*5c2921b0SApple OSS Distributions /* Test variants */
46*5c2921b0SApple OSS Distributions #define TEST_ALLOWED     0x1
47*5c2921b0SApple OSS Distributions #define TEST_NOT_ALLOWED 0x2
48*5c2921b0SApple OSS Distributions 
49*5c2921b0SApple OSS Distributions /*
50*5c2921b0SApple OSS Distributions  * Values which the kernel OR's into the PID when a corpse
51*5c2921b0SApple OSS Distributions  * is either allowed or disallowed for the
52*5c2921b0SApple OSS Distributions  * kern.memorystatus_vm_map_fork_pidwatch sysctl.
53*5c2921b0SApple OSS Distributions  */
54*5c2921b0SApple OSS Distributions #define MEMORYSTATUS_VM_MAP_FORK_ALLOWED        0x100000000ul
55*5c2921b0SApple OSS Distributions #define MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED 0x200000000ul
56*5c2921b0SApple OSS Distributions 
57*5c2921b0SApple OSS Distributions /*
58*5c2921b0SApple OSS Distributions  * The memory allocation happens in a child process, this
59*5c2921b0SApple OSS Distributions  * is stuff to deal with creating and managing the child.
60*5c2921b0SApple OSS Distributions  * The child will only execute the T_HELPER_DECL.
61*5c2921b0SApple OSS Distributions  */
62*5c2921b0SApple OSS Distributions static char testpath[PATH_MAX];
63*5c2921b0SApple OSS Distributions static uint32_t testpath_size = sizeof(testpath);
64*5c2921b0SApple OSS Distributions #define LIMIT_DELTA_MB 5 /* an arbitrary limit delta */
65*5c2921b0SApple OSS Distributions #define MEGABYTE        (1024 * 1024)
66*5c2921b0SApple OSS Distributions 
67*5c2921b0SApple OSS Distributions /*
68*5c2921b0SApple OSS Distributions  * The child process communicates back to parent via an exit() code.
69*5c2921b0SApple OSS Distributions  */
70*5c2921b0SApple OSS Distributions enum child_exits {
71*5c2921b0SApple OSS Distributions 	NORMAL_EXIT = 0,
72*5c2921b0SApple OSS Distributions 	NO_MEMSIZE_ARG,
73*5c2921b0SApple OSS Distributions 	INVALID_MEMSIZE,
74*5c2921b0SApple OSS Distributions 	MALLOC_FAILED,
75*5c2921b0SApple OSS Distributions 	NUM_CHILD_EXIT
76*5c2921b0SApple OSS Distributions };
77*5c2921b0SApple OSS Distributions static char *child_exit_why[] = {
78*5c2921b0SApple OSS Distributions 	"normal exit",
79*5c2921b0SApple OSS Distributions 	"no memsize argument to child",
80*5c2921b0SApple OSS Distributions 	"invalid memsize argument to child",
81*5c2921b0SApple OSS Distributions 	"malloc() failed",
82*5c2921b0SApple OSS Distributions };
83*5c2921b0SApple OSS Distributions 
84*5c2921b0SApple OSS Distributions /*
85*5c2921b0SApple OSS Distributions  * Set/Get the sysctl used to determine if corpse collection occurs.
86*5c2921b0SApple OSS Distributions  * This is done by the kernel checking for a specific PID.
87*5c2921b0SApple OSS Distributions  */
88*5c2921b0SApple OSS Distributions static void
set_memorystatus_vm_map_fork_pidwatch(pid_t pid)89*5c2921b0SApple OSS Distributions set_memorystatus_vm_map_fork_pidwatch(pid_t pid)
90*5c2921b0SApple OSS Distributions {
91*5c2921b0SApple OSS Distributions 	uint64_t new_value = (uint64_t)pid;
92*5c2921b0SApple OSS Distributions 	size_t new_len = sizeof(new_value);
93*5c2921b0SApple OSS Distributions 	int err;
94*5c2921b0SApple OSS Distributions 
95*5c2921b0SApple OSS Distributions 	err = sysctlbyname("kern.memorystatus_vm_map_fork_pidwatch", NULL, NULL, &new_value, new_len);
96*5c2921b0SApple OSS Distributions 	T_QUIET;
97*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "set sysctlbyname(kern.memorystatus_vm_map_fork_pidwatch...) failed");
98*5c2921b0SApple OSS Distributions 	return;
99*5c2921b0SApple OSS Distributions }
100*5c2921b0SApple OSS Distributions 
101*5c2921b0SApple OSS Distributions static uint64_t
get_memorystatus_vm_map_fork_pidwatch()102*5c2921b0SApple OSS Distributions get_memorystatus_vm_map_fork_pidwatch()
103*5c2921b0SApple OSS Distributions {
104*5c2921b0SApple OSS Distributions 	uint64_t value = 0;
105*5c2921b0SApple OSS Distributions 	size_t val_len = sizeof(value);
106*5c2921b0SApple OSS Distributions 	int err;
107*5c2921b0SApple OSS Distributions 
108*5c2921b0SApple OSS Distributions 	err = sysctlbyname("kern.memorystatus_vm_map_fork_pidwatch", &value, &val_len, NULL, 0);
109*5c2921b0SApple OSS Distributions 	T_QUIET;
110*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(err, "get sysctlbyname(kern.memorystatus_vm_map_fork_pidwatch...) failed");
111*5c2921b0SApple OSS Distributions 
112*5c2921b0SApple OSS Distributions 	return value;
113*5c2921b0SApple OSS Distributions }
114*5c2921b0SApple OSS Distributions 
115*5c2921b0SApple OSS Distributions /*
116*5c2921b0SApple OSS Distributions  * We want to avoid jetsam giving us bad results, if possible. So check if there's
117*5c2921b0SApple OSS Distributions  * enough memory for the test to run, waiting briefly for some to free up.
118*5c2921b0SApple OSS Distributions  */
119*5c2921b0SApple OSS Distributions static void
wait_for_free_mem(int need_mb)120*5c2921b0SApple OSS Distributions wait_for_free_mem(int need_mb)
121*5c2921b0SApple OSS Distributions {
122*5c2921b0SApple OSS Distributions 	int64_t         memsize;
123*5c2921b0SApple OSS Distributions 	int             memorystatus_level;
124*5c2921b0SApple OSS Distributions 	size_t          size;
125*5c2921b0SApple OSS Distributions 	int64_t         avail;
126*5c2921b0SApple OSS Distributions 	int             err;
127*5c2921b0SApple OSS Distributions 	int             try;
128*5c2921b0SApple OSS Distributions 
129*5c2921b0SApple OSS Distributions 	/*
130*5c2921b0SApple OSS Distributions 	 * get amount of memory in the machine
131*5c2921b0SApple OSS Distributions 	 */
132*5c2921b0SApple OSS Distributions 	size = sizeof(memsize);
133*5c2921b0SApple OSS Distributions 	err = sysctlbyname("hw.memsize", &memsize, &size, NULL, 0);
134*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "sysctlbyname(hw.memsize...) failed");
135*5c2921b0SApple OSS Distributions 
136*5c2921b0SApple OSS Distributions 	/*
137*5c2921b0SApple OSS Distributions 	 * Use a loop to briefly sleep and recheck if short on memory.
138*5c2921b0SApple OSS Distributions 	 */
139*5c2921b0SApple OSS Distributions 	try = 1;
140*5c2921b0SApple OSS Distributions 	for (;;) {
141*5c2921b0SApple OSS Distributions 		/*
142*5c2921b0SApple OSS Distributions 		 * memorystatus_level is a percentage of memory available. For example 20 means 1/5 of memory.
143*5c2921b0SApple OSS Distributions 		 * It currently doesn't exist on macOS but neither does jetsam, so pass the test there.
144*5c2921b0SApple OSS Distributions 		 */
145*5c2921b0SApple OSS Distributions 		size = sizeof(memorystatus_level);
146*5c2921b0SApple OSS Distributions 		if (sysctlbyname("kern.memorystatus_level", &memorystatus_level, &size, NULL, 0) != 0) {
147*5c2921b0SApple OSS Distributions 			return;
148*5c2921b0SApple OSS Distributions 		}
149*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_LE(memorystatus_level, 100, "memorystatus_level too high");
150*5c2921b0SApple OSS Distributions 		T_QUIET; T_ASSERT_GT(memorystatus_level, 0, "memorystatus_level negative");
151*5c2921b0SApple OSS Distributions 
152*5c2921b0SApple OSS Distributions 		/*
153*5c2921b0SApple OSS Distributions 		 * jetsam kicks in at memory status level of 15%, so subtract that much out of what's available.
154*5c2921b0SApple OSS Distributions 		 */
155*5c2921b0SApple OSS Distributions 		avail = MAX(0, (memsize * (memorystatus_level - 15)) / 100);
156*5c2921b0SApple OSS Distributions 
157*5c2921b0SApple OSS Distributions 		/*
158*5c2921b0SApple OSS Distributions 		 * We're good to go if there's more than enough available.
159*5c2921b0SApple OSS Distributions 		 */
160*5c2921b0SApple OSS Distributions 		if ((int64_t)need_mb * MEGABYTE < avail) {
161*5c2921b0SApple OSS Distributions 			return;
162*5c2921b0SApple OSS Distributions 		}
163*5c2921b0SApple OSS Distributions 
164*5c2921b0SApple OSS Distributions 		/*
165*5c2921b0SApple OSS Distributions 		 * issue a message to log and sleep briefly to see if we can get more memory
166*5c2921b0SApple OSS Distributions 		 */
167*5c2921b0SApple OSS Distributions 		if (try-- == 0) {
168*5c2921b0SApple OSS Distributions 			break;
169*5c2921b0SApple OSS Distributions 		}
170*5c2921b0SApple OSS Distributions 		T_LOG("Need %d MB, only %d MB available. sleeping 5 seconds for more to free. memorystatus_level %d",
171*5c2921b0SApple OSS Distributions 		    need_mb, (int)(avail / MEGABYTE), memorystatus_level);
172*5c2921b0SApple OSS Distributions 		sleep(5);
173*5c2921b0SApple OSS Distributions 	}
174*5c2921b0SApple OSS Distributions 	T_SKIP("Needed %d MB, but only %d MB available. Skipping test to avoid jetsam issues.",
175*5c2921b0SApple OSS Distributions 	    need_mb, (int)(avail / MEGABYTE));
176*5c2921b0SApple OSS Distributions }
177*5c2921b0SApple OSS Distributions 
178*5c2921b0SApple OSS Distributions 
179*5c2921b0SApple OSS Distributions /*
180*5c2921b0SApple OSS Distributions  * The main test calls this to spawn child process which will run and
181*5c2921b0SApple OSS Distributions  * exceed some memory limit. The child is initially suspended so that
182*5c2921b0SApple OSS Distributions  * we can do the sysctl calls before it runs.
183*5c2921b0SApple OSS Distributions  * Since this is a libdarwintest, the "-n" names the T_HELPER_DECL() that
184*5c2921b0SApple OSS Distributions  * we want to run. The arguments specific to the test follow a "--".
185*5c2921b0SApple OSS Distributions  */
186*5c2921b0SApple OSS Distributions static pid_t
spawn_child_process(char * const executable,char * const memlimit,short flags,int priority,int active_limit_mb,int inactive_limit_mb)187*5c2921b0SApple OSS Distributions spawn_child_process(
188*5c2921b0SApple OSS Distributions 	char * const executable,
189*5c2921b0SApple OSS Distributions 	char * const memlimit,
190*5c2921b0SApple OSS Distributions 	short flags,
191*5c2921b0SApple OSS Distributions 	int priority,
192*5c2921b0SApple OSS Distributions 	int active_limit_mb,
193*5c2921b0SApple OSS Distributions 	int inactive_limit_mb)
194*5c2921b0SApple OSS Distributions {
195*5c2921b0SApple OSS Distributions 	posix_spawnattr_t spawn_attrs;
196*5c2921b0SApple OSS Distributions 	int err;
197*5c2921b0SApple OSS Distributions 	pid_t child_pid;
198*5c2921b0SApple OSS Distributions 	char * const argv_child[] = { executable, "-n", "child_process", "--", memlimit, NULL };
199*5c2921b0SApple OSS Distributions 
200*5c2921b0SApple OSS Distributions 	err = posix_spawnattr_init(&spawn_attrs);
201*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "  posix_spawnattr_init() failed");
202*5c2921b0SApple OSS Distributions 
203*5c2921b0SApple OSS Distributions 	err = posix_spawnattr_setflags(&spawn_attrs, POSIX_SPAWN_START_SUSPENDED);
204*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "  posix_spawnattr_setflags() failed");
205*5c2921b0SApple OSS Distributions 
206*5c2921b0SApple OSS Distributions 	err = posix_spawnattr_setjetsam_ext(&spawn_attrs, flags, priority, active_limit_mb, inactive_limit_mb);
207*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "  posix_spawnattr_setjetsam_ext() failed");
208*5c2921b0SApple OSS Distributions 
209*5c2921b0SApple OSS Distributions 	err = posix_spawn(&child_pid, executable, NULL, &spawn_attrs, argv_child, environ);
210*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "  posix_spawn() failed");
211*5c2921b0SApple OSS Distributions 
212*5c2921b0SApple OSS Distributions 	return child_pid;
213*5c2921b0SApple OSS Distributions }
214*5c2921b0SApple OSS Distributions 
215*5c2921b0SApple OSS Distributions 
216*5c2921b0SApple OSS Distributions /*
217*5c2921b0SApple OSS Distributions  * The parent calls this to continue the suspended child, then wait for its result.
218*5c2921b0SApple OSS Distributions  * We collect its resource usage to vefiry the expected amount allocated.
219*5c2921b0SApple OSS Distributions  */
220*5c2921b0SApple OSS Distributions static void
test_child_process(pid_t child_pid,int * status,struct rusage * ru)221*5c2921b0SApple OSS Distributions test_child_process(pid_t child_pid, int *status, struct rusage *ru)
222*5c2921b0SApple OSS Distributions {
223*5c2921b0SApple OSS Distributions 	int err = 0;
224*5c2921b0SApple OSS Distributions 	pid_t got_pid;
225*5c2921b0SApple OSS Distributions 
226*5c2921b0SApple OSS Distributions 	T_LOG("  continuing child[%d]\n", child_pid);
227*5c2921b0SApple OSS Distributions 
228*5c2921b0SApple OSS Distributions 	err = kill(child_pid, SIGCONT);
229*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(err, "  kill(%d, SIGCONT) failed", child_pid);
230*5c2921b0SApple OSS Distributions 
231*5c2921b0SApple OSS Distributions 	T_LOG("  waiting for child[%d] to exit", child_pid);
232*5c2921b0SApple OSS Distributions 
233*5c2921b0SApple OSS Distributions 	got_pid = wait4(child_pid, status, 0, ru);
234*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(child_pid, got_pid, "  wait4(%d, ...) returned %d", child_pid, got_pid);
235*5c2921b0SApple OSS Distributions }
236*5c2921b0SApple OSS Distributions 
237*5c2921b0SApple OSS Distributions /*
238*5c2921b0SApple OSS Distributions  * The child process executes this code. The easiest way, with given darwintest infrastructure,
239*5c2921b0SApple OSS Distributions  * it has to return information is via exit status.
240*5c2921b0SApple OSS Distributions  */
241*5c2921b0SApple OSS Distributions T_HELPER_DECL(child_process, "child allocates memory to failure")
242*5c2921b0SApple OSS Distributions {
243*5c2921b0SApple OSS Distributions #define BYTESPERALLOC   MEGABYTE
244*5c2921b0SApple OSS Distributions #define BYTESINEXCESS   (2 * MEGABYTE) /* 2 MB - arbitrary */
245*5c2921b0SApple OSS Distributions 	char *limit;
246*5c2921b0SApple OSS Distributions 	long limit_mb = 0;
247*5c2921b0SApple OSS Distributions 	long max_bytes_to_munch, bytes_remaining, bytes_this_munch;
248*5c2921b0SApple OSS Distributions 	void *mem = NULL;
249*5c2921b0SApple OSS Distributions 
250*5c2921b0SApple OSS Distributions 	/*
251*5c2921b0SApple OSS Distributions 	 * This helper is run in a child process. The helper sees one argument
252*5c2921b0SApple OSS Distributions 	 * as a string which is the amount of memory in megabytes to allocate.
253*5c2921b0SApple OSS Distributions 	 */
254*5c2921b0SApple OSS Distributions 	if (argc != 1) {
255*5c2921b0SApple OSS Distributions 		exit(NO_MEMSIZE_ARG);
256*5c2921b0SApple OSS Distributions 	}
257*5c2921b0SApple OSS Distributions 
258*5c2921b0SApple OSS Distributions 	limit = argv[0];
259*5c2921b0SApple OSS Distributions 	errno = 0;
260*5c2921b0SApple OSS Distributions 	limit_mb = strtol(limit, NULL, 10);
261*5c2921b0SApple OSS Distributions 	if (errno != 0 || limit_mb <= 0) {
262*5c2921b0SApple OSS Distributions 		exit(INVALID_MEMSIZE);
263*5c2921b0SApple OSS Distributions 	}
264*5c2921b0SApple OSS Distributions 
265*5c2921b0SApple OSS Distributions 	/* Compute in excess of assigned limit */
266*5c2921b0SApple OSS Distributions 	max_bytes_to_munch = limit_mb * MEGABYTE;
267*5c2921b0SApple OSS Distributions 	max_bytes_to_munch += BYTESINEXCESS;
268*5c2921b0SApple OSS Distributions 
269*5c2921b0SApple OSS Distributions 	for (bytes_remaining = max_bytes_to_munch; bytes_remaining > 0; bytes_remaining -= bytes_this_munch) {
270*5c2921b0SApple OSS Distributions 		bytes_this_munch = MIN(bytes_remaining, BYTESPERALLOC);
271*5c2921b0SApple OSS Distributions 
272*5c2921b0SApple OSS Distributions 		mem = malloc((size_t)bytes_this_munch);
273*5c2921b0SApple OSS Distributions 		if (mem == NULL) {
274*5c2921b0SApple OSS Distributions 			exit(MALLOC_FAILED);
275*5c2921b0SApple OSS Distributions 		}
276*5c2921b0SApple OSS Distributions 		arc4random_buf(mem, (size_t)bytes_this_munch);
277*5c2921b0SApple OSS Distributions 	}
278*5c2921b0SApple OSS Distributions 
279*5c2921b0SApple OSS Distributions 	/* We chewed up all the memory we were asked to. */
280*5c2921b0SApple OSS Distributions 	exit(NORMAL_EXIT);
281*5c2921b0SApple OSS Distributions }
282*5c2921b0SApple OSS Distributions 
283*5c2921b0SApple OSS Distributions 
284*5c2921b0SApple OSS Distributions /*
285*5c2921b0SApple OSS Distributions  * Actual test body.
286*5c2921b0SApple OSS Distributions  */
287*5c2921b0SApple OSS Distributions static void
memorystatus_vm_map_fork_parent(int test_variant)288*5c2921b0SApple OSS Distributions memorystatus_vm_map_fork_parent(int test_variant)
289*5c2921b0SApple OSS Distributions {
290*5c2921b0SApple OSS Distributions 	int             max_task_pmem = 0; /* MB */
291*5c2921b0SApple OSS Distributions 	size_t          size = 0;
292*5c2921b0SApple OSS Distributions 	int             active_limit_mb = 0;
293*5c2921b0SApple OSS Distributions 	int             inactive_limit_mb = 0;
294*5c2921b0SApple OSS Distributions 	short           flags = 0;
295*5c2921b0SApple OSS Distributions 	char            memlimit_str[16];
296*5c2921b0SApple OSS Distributions 	pid_t           child_pid;
297*5c2921b0SApple OSS Distributions 	int             child_status;
298*5c2921b0SApple OSS Distributions 	uint64_t        kernel_pidwatch_val;
299*5c2921b0SApple OSS Distributions 	uint64_t        expected_pidwatch_val;
300*5c2921b0SApple OSS Distributions 	int             ret;
301*5c2921b0SApple OSS Distributions 	struct rusage   ru;
302*5c2921b0SApple OSS Distributions 	enum child_exits exit_val;
303*5c2921b0SApple OSS Distributions 
304*5c2921b0SApple OSS Distributions 	/*
305*5c2921b0SApple OSS Distributions 	 * The code to set/get the pidwatch sysctl is only in
306*5c2921b0SApple OSS Distributions 	 * development kernels. Skip the test if not on one.
307*5c2921b0SApple OSS Distributions 	 */
308*5c2921b0SApple OSS Distributions 	if (!is_development_kernel()) {
309*5c2921b0SApple OSS Distributions 		T_SKIP("Can't test on release kernel");
310*5c2921b0SApple OSS Distributions 	}
311*5c2921b0SApple OSS Distributions 
312*5c2921b0SApple OSS Distributions 	/*
313*5c2921b0SApple OSS Distributions 	 * Determine a memory limit based on system having one or not.
314*5c2921b0SApple OSS Distributions 	 */
315*5c2921b0SApple OSS Distributions 	size = sizeof(max_task_pmem);
316*5c2921b0SApple OSS Distributions 	(void)sysctlbyname("kern.max_task_pmem", &max_task_pmem, &size, NULL, 0);
317*5c2921b0SApple OSS Distributions 	if (max_task_pmem <= 0) {
318*5c2921b0SApple OSS Distributions 		max_task_pmem = 0;
319*5c2921b0SApple OSS Distributions 	}
320*5c2921b0SApple OSS Distributions 
321*5c2921b0SApple OSS Distributions 	/* default limit is 1/4 of max task phys memory value */
322*5c2921b0SApple OSS Distributions 	active_limit_mb = max_task_pmem / 4;
323*5c2921b0SApple OSS Distributions 
324*5c2921b0SApple OSS Distributions #if TARGET_OS_WATCH
325*5c2921b0SApple OSS Distributions 
326*5c2921b0SApple OSS Distributions 	/*
327*5c2921b0SApple OSS Distributions 	 * Larger memory watches have a raised corpse size limit.
328*5c2921b0SApple OSS Distributions 	 * One coprse of 300Meg is allowed, others are 200M.
329*5c2921b0SApple OSS Distributions 	 * We pick 300 or 200 based on which test is being done.
330*5c2921b0SApple OSS Distributions 	 */
331*5c2921b0SApple OSS Distributions 	uint64_t hw_memsize = 0;
332*5c2921b0SApple OSS Distributions 	size = sizeof(hw_memsize);
333*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname("hw.memsize", &hw_memsize, &size, NULL, 0), "read hw.memsize");
334*5c2921b0SApple OSS Distributions 	if (hw_memsize > 1024 * 1024 * 1024) {
335*5c2921b0SApple OSS Distributions 		if (test_variant == TEST_ALLOWED) {
336*5c2921b0SApple OSS Distributions 			active_limit_mb = MAX(active_limit_mb, 200);
337*5c2921b0SApple OSS Distributions 		} else {
338*5c2921b0SApple OSS Distributions 			active_limit_mb = MAX(active_limit_mb, 300);
339*5c2921b0SApple OSS Distributions 		}
340*5c2921b0SApple OSS Distributions 	}
341*5c2921b0SApple OSS Distributions 
342*5c2921b0SApple OSS Distributions #endif /* TARGET_OS_WATCH */
343*5c2921b0SApple OSS Distributions 
344*5c2921b0SApple OSS Distributions 	if (test_variant == TEST_ALLOWED) {
345*5c2921b0SApple OSS Distributions 		/*
346*5c2921b0SApple OSS Distributions 		 * Tell the child to allocate less than 1/4 the system wide limit.
347*5c2921b0SApple OSS Distributions 		 */
348*5c2921b0SApple OSS Distributions 		if (active_limit_mb <= LIMIT_DELTA_MB) {
349*5c2921b0SApple OSS Distributions 			active_limit_mb = LIMIT_DELTA_MB;
350*5c2921b0SApple OSS Distributions 		} else {
351*5c2921b0SApple OSS Distributions 			active_limit_mb -= LIMIT_DELTA_MB;
352*5c2921b0SApple OSS Distributions 		}
353*5c2921b0SApple OSS Distributions 		expected_pidwatch_val = MEMORYSTATUS_VM_MAP_FORK_ALLOWED;
354*5c2921b0SApple OSS Distributions 	} else { /* TEST_NOT_ALLOWED */
355*5c2921b0SApple OSS Distributions 		/*
356*5c2921b0SApple OSS Distributions 		 * Tell the child to allocate more than 1/4 the system wide limit.
357*5c2921b0SApple OSS Distributions 		 */
358*5c2921b0SApple OSS Distributions 		active_limit_mb += LIMIT_DELTA_MB;
359*5c2921b0SApple OSS Distributions 		if (max_task_pmem == 0) {
360*5c2921b0SApple OSS Distributions 			expected_pidwatch_val = MEMORYSTATUS_VM_MAP_FORK_ALLOWED;
361*5c2921b0SApple OSS Distributions 		} else {
362*5c2921b0SApple OSS Distributions 			expected_pidwatch_val = MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED;
363*5c2921b0SApple OSS Distributions 		}
364*5c2921b0SApple OSS Distributions 	}
365*5c2921b0SApple OSS Distributions 	inactive_limit_mb = active_limit_mb;
366*5c2921b0SApple OSS Distributions 	T_LOG("using limit of %d Meg", active_limit_mb);
367*5c2921b0SApple OSS Distributions 
368*5c2921b0SApple OSS Distributions 	/*
369*5c2921b0SApple OSS Distributions 	 * When run as part of a larger suite, a previous test
370*5c2921b0SApple OSS Distributions 	 * may have left the system temporarily with too little
371*5c2921b0SApple OSS Distributions 	 * memory to run this test. We try to detect if there is
372*5c2921b0SApple OSS Distributions 	 * enough free memory to proceed, waiting a little bit
373*5c2921b0SApple OSS Distributions 	 * for memory to free up.
374*5c2921b0SApple OSS Distributions 	 */
375*5c2921b0SApple OSS Distributions 	wait_for_free_mem(active_limit_mb);
376*5c2921b0SApple OSS Distributions 
377*5c2921b0SApple OSS Distributions #if TARGET_OS_OSX
378*5c2921b0SApple OSS Distributions 	/*
379*5c2921b0SApple OSS Distributions 	 * vm_map_fork() is always allowed on desktop.
380*5c2921b0SApple OSS Distributions 	 */
381*5c2921b0SApple OSS Distributions 	expected_pidwatch_val = MEMORYSTATUS_VM_MAP_FORK_ALLOWED;
382*5c2921b0SApple OSS Distributions #endif
383*5c2921b0SApple OSS Distributions 
384*5c2921b0SApple OSS Distributions 	/*
385*5c2921b0SApple OSS Distributions 	 * Prepare the arguments needed to spawn the child process.
386*5c2921b0SApple OSS Distributions 	 */
387*5c2921b0SApple OSS Distributions 	memset(memlimit_str, 0, sizeof(memlimit_str));
388*5c2921b0SApple OSS Distributions 	(void)sprintf(memlimit_str, "%d", active_limit_mb);
389*5c2921b0SApple OSS Distributions 
390*5c2921b0SApple OSS Distributions 	ret = _NSGetExecutablePath(testpath, &testpath_size);
391*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "_NSGetExecutablePath(%s, ...)", testpath);
392*5c2921b0SApple OSS Distributions 
393*5c2921b0SApple OSS Distributions 	/*
394*5c2921b0SApple OSS Distributions 	 * We put the child process in FOREGROUND to try and keep jetsam's hands off it.
395*5c2921b0SApple OSS Distributions 	 */
396*5c2921b0SApple OSS Distributions 	child_pid = spawn_child_process(testpath, memlimit_str, flags,
397*5c2921b0SApple OSS Distributions 	    JETSAM_PRIORITY_FOREGROUND, active_limit_mb, inactive_limit_mb);
398*5c2921b0SApple OSS Distributions 
399*5c2921b0SApple OSS Distributions 	expected_pidwatch_val |= (uint64_t)child_pid;
400*5c2921b0SApple OSS Distributions 
401*5c2921b0SApple OSS Distributions 	/*
402*5c2921b0SApple OSS Distributions 	 * We only reach here if parent successfully spawned child process.
403*5c2921b0SApple OSS Distributions 	 */
404*5c2921b0SApple OSS Distributions 	T_LOG("  spawned child_pid[%d] with memlimit %s (%d)MB\n",
405*5c2921b0SApple OSS Distributions 	    child_pid, memlimit_str, active_limit_mb);
406*5c2921b0SApple OSS Distributions 
407*5c2921b0SApple OSS Distributions 	/*
408*5c2921b0SApple OSS Distributions 	 * Set the kernel's pidwatch to look for the child.
409*5c2921b0SApple OSS Distributions 	 */
410*5c2921b0SApple OSS Distributions 	(void)set_memorystatus_vm_map_fork_pidwatch((pid_t)0);
411*5c2921b0SApple OSS Distributions 	(void)set_memorystatus_vm_map_fork_pidwatch(child_pid);
412*5c2921b0SApple OSS Distributions 
413*5c2921b0SApple OSS Distributions 	/*
414*5c2921b0SApple OSS Distributions 	 * Let the child run and wait for it to finish.
415*5c2921b0SApple OSS Distributions 	 */
416*5c2921b0SApple OSS Distributions 	test_child_process(child_pid, &child_status, &ru);
417*5c2921b0SApple OSS Distributions 	T_LOG("Child exited with max_rss of %ld", ru.ru_maxrss);
418*5c2921b0SApple OSS Distributions 
419*5c2921b0SApple OSS Distributions 	/*
420*5c2921b0SApple OSS Distributions 	 * Retrieve the kernel's pidwatch value. This should now indicate
421*5c2921b0SApple OSS Distributions 	 * if the corpse was allowed or not.
422*5c2921b0SApple OSS Distributions 	 */
423*5c2921b0SApple OSS Distributions 	kernel_pidwatch_val = get_memorystatus_vm_map_fork_pidwatch();
424*5c2921b0SApple OSS Distributions 	(void)set_memorystatus_vm_map_fork_pidwatch((pid_t)0);
425*5c2921b0SApple OSS Distributions 
426*5c2921b0SApple OSS Distributions 	/*
427*5c2921b0SApple OSS Distributions 	 * If the child died abnormally, the test is invalid.
428*5c2921b0SApple OSS Distributions 	 */
429*5c2921b0SApple OSS Distributions 	if (!WIFEXITED(child_status)) {
430*5c2921b0SApple OSS Distributions 		if (WIFSIGNALED(child_status)) {
431*5c2921b0SApple OSS Distributions 			/* jetsam kills a process with SIGKILL */
432*5c2921b0SApple OSS Distributions 			if (WTERMSIG(child_status) == SIGKILL) {
433*5c2921b0SApple OSS Distributions 				T_LOG("Child appears to have been a jetsam victim");
434*5c2921b0SApple OSS Distributions 			}
435*5c2921b0SApple OSS Distributions 			T_SKIP("Child terminated by signal %d test result invalid", WTERMSIG(child_status));
436*5c2921b0SApple OSS Distributions 		}
437*5c2921b0SApple OSS Distributions 		T_SKIP("child did not exit normally (status=%d) test result invalid", child_status);
438*5c2921b0SApple OSS Distributions 	}
439*5c2921b0SApple OSS Distributions 
440*5c2921b0SApple OSS Distributions 	/*
441*5c2921b0SApple OSS Distributions 	 * We don't expect the child to exit for any other reason than success
442*5c2921b0SApple OSS Distributions 	 */
443*5c2921b0SApple OSS Distributions 	exit_val = (enum child_exits)WEXITSTATUS(child_status);
444*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(exit_val, NORMAL_EXIT, "child exit due to: %s",
445*5c2921b0SApple OSS Distributions 	    (0 < exit_val && exit_val < NUM_CHILD_EXIT) ? child_exit_why[exit_val] : "unknown");
446*5c2921b0SApple OSS Distributions 
447*5c2921b0SApple OSS Distributions 	/*
448*5c2921b0SApple OSS Distributions 	 * If the kernel aborted generating a corpse for other reasons, the test is invalid.
449*5c2921b0SApple OSS Distributions 	 */
450*5c2921b0SApple OSS Distributions 	if (kernel_pidwatch_val == -1ull) {
451*5c2921b0SApple OSS Distributions 		T_SKIP("corpse generation was aborted by kernel");
452*5c2921b0SApple OSS Distributions 	}
453*5c2921b0SApple OSS Distributions 
454*5c2921b0SApple OSS Distributions 	/*
455*5c2921b0SApple OSS Distributions 	 * We should always have made it through the vm_map_fork() checks in the kernel for this test.
456*5c2921b0SApple OSS Distributions 	 */
457*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_NE_ULLONG(kernel_pidwatch_val, (uint64_t)child_pid, "child didn't trigger corpse generation");
458*5c2921b0SApple OSS Distributions 
459*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(kernel_pidwatch_val, expected_pidwatch_val, "kernel value 0x%llx - expected 0x%llx",
460*5c2921b0SApple OSS Distributions 	    kernel_pidwatch_val, expected_pidwatch_val);
461*5c2921b0SApple OSS Distributions }
462*5c2921b0SApple OSS Distributions 
463*5c2921b0SApple OSS Distributions /*
464*5c2921b0SApple OSS Distributions  * The order of these 2 test functions is important. They will be executed by the test framwork in order.
465*5c2921b0SApple OSS Distributions  *
466*5c2921b0SApple OSS Distributions  * We test "not allowed first", then "allowed". If it were the other way around, the corpse from the "allowed"
467*5c2921b0SApple OSS Distributions  * test would likely cause memory pressure and jetsam would likely kill the "not allowed" test.
468*5c2921b0SApple OSS Distributions  */
469*5c2921b0SApple OSS Distributions T_DECL(memorystatus_vm_map_fork_test_not_allowed, "test that corpse generation was not allowed", T_META_ASROOT(true))
470*5c2921b0SApple OSS Distributions {
471*5c2921b0SApple OSS Distributions 	memorystatus_vm_map_fork_parent(TEST_NOT_ALLOWED);
472*5c2921b0SApple OSS Distributions }
473*5c2921b0SApple OSS Distributions 
474*5c2921b0SApple OSS Distributions T_DECL(memorystatus_vm_map_fork_test_allowed, "test corpse generation allowed", T_META_ASROOT(true))
475*5c2921b0SApple OSS Distributions {
476*5c2921b0SApple OSS Distributions 	memorystatus_vm_map_fork_parent(TEST_ALLOWED);
477*5c2921b0SApple OSS Distributions }
478