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