xref: /xnu-8020.140.41/tests/vm/zalloc.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
2*27b03b36SApple OSS Distributions #include <signal.h>
3*27b03b36SApple OSS Distributions #include <darwintest.h>
4*27b03b36SApple OSS Distributions #include <darwintest_utils.h>
5*27b03b36SApple OSS Distributions 
6*27b03b36SApple OSS Distributions T_GLOBAL_META(
7*27b03b36SApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
8*27b03b36SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
9*27b03b36SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("zalloc"),
10*27b03b36SApple OSS Distributions 	T_META_ASROOT(YES));
11*27b03b36SApple OSS Distributions 
12*27b03b36SApple OSS Distributions static int64_t
run_sysctl_test(const char * t,int64_t value)13*27b03b36SApple OSS Distributions run_sysctl_test(const char *t, int64_t value)
14*27b03b36SApple OSS Distributions {
15*27b03b36SApple OSS Distributions 	char name[1024];
16*27b03b36SApple OSS Distributions 	int64_t result = 0;
17*27b03b36SApple OSS Distributions 	size_t s = sizeof(value);
18*27b03b36SApple OSS Distributions 	int rc;
19*27b03b36SApple OSS Distributions 
20*27b03b36SApple OSS Distributions 	snprintf(name, sizeof(name), "debug.test.%s", t);
21*27b03b36SApple OSS Distributions 	rc = sysctlbyname(name, &result, &s, &value, s);
22*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "sysctlbyname(%s)", t);
23*27b03b36SApple OSS Distributions 	return result;
24*27b03b36SApple OSS Distributions }
25*27b03b36SApple OSS Distributions 
26*27b03b36SApple OSS Distributions T_DECL(basic_zone_test, "General zalloc test",
27*27b03b36SApple OSS Distributions     T_META_CHECK_LEAKS(false))
28*27b03b36SApple OSS Distributions {
29*27b03b36SApple OSS Distributions 	T_EXPECT_EQ(1ull, run_sysctl_test("zone_basic_test", 0), "zone_basic_test");
30*27b03b36SApple OSS Distributions }
31*27b03b36SApple OSS Distributions 
32*27b03b36SApple OSS Distributions T_DECL(read_only_zone_test, "Read-only zalloc test",
33*27b03b36SApple OSS Distributions     T_META_CHECK_LEAKS(false))
34*27b03b36SApple OSS Distributions {
35*27b03b36SApple OSS Distributions 	T_EXPECT_EQ(1ull, run_sysctl_test("zone_ro_basic_test", 0), "zone_ro_basic_test");
36*27b03b36SApple OSS Distributions }
37*27b03b36SApple OSS Distributions 
38*27b03b36SApple OSS Distributions T_DECL(zone_stress_test, "Zone stress test of edge cases",
39*27b03b36SApple OSS Distributions     T_META_CHECK_LEAKS(false))
40*27b03b36SApple OSS Distributions {
41*27b03b36SApple OSS Distributions 	T_EXPECT_EQ(1ull, run_sysctl_test("zone_stress_test", 0), "zone_stress_test");
42*27b03b36SApple OSS Distributions }
43*27b03b36SApple OSS Distributions 
44*27b03b36SApple OSS Distributions #define ZLOG_ZONE "data.kalloc.128"
45*27b03b36SApple OSS Distributions 
46*27b03b36SApple OSS Distributions T_DECL(zlog_smoke_test, "check that zlog functions at all",
47*27b03b36SApple OSS Distributions     T_META_BOOTARGS_SET("zlog1=" ZLOG_ZONE))
48*27b03b36SApple OSS Distributions {
49*27b03b36SApple OSS Distributions 	char *cmd[] = { "/usr/local/bin/zlog", "-l", "-z", ZLOG_ZONE, NULL };
50*27b03b36SApple OSS Distributions 	dispatch_semaphore_t sema = dispatch_semaphore_create(0);
51*27b03b36SApple OSS Distributions 	int status = 0;
52*27b03b36SApple OSS Distributions 	pid_t pid;
53*27b03b36SApple OSS Distributions 
54*27b03b36SApple OSS Distributions 	pid = dt_launch_tool_pipe(cmd, false, NULL,
55*27b03b36SApple OSS Distributions 	    ^bool (char *d, size_t s, dt_pipe_data_handler_context_t *ctx) {
56*27b03b36SApple OSS Distributions 		(void)ctx;
57*27b03b36SApple OSS Distributions 		if (strstr(d, "active refs") && strstr(d, "operation type: ")) {
58*27b03b36SApple OSS Distributions 		        T_PASS("found line [%.*s]", (int)(s - 1), d);
59*27b03b36SApple OSS Distributions 		        dispatch_semaphore_signal(sema);
60*27b03b36SApple OSS Distributions 		}
61*27b03b36SApple OSS Distributions 		return false;
62*27b03b36SApple OSS Distributions 	}, ^bool (char *d, size_t s, dt_pipe_data_handler_context_t *ctx) {
63*27b03b36SApple OSS Distributions 		/* Forward errors to stderror for debugging */
64*27b03b36SApple OSS Distributions 		(void)ctx;
65*27b03b36SApple OSS Distributions 		fwrite(d, 1, s, stderr);
66*27b03b36SApple OSS Distributions 		return false;
67*27b03b36SApple OSS Distributions 	}, BUFFER_PATTERN_LINE, NULL);
68*27b03b36SApple OSS Distributions 
69*27b03b36SApple OSS Distributions 	dt_waitpid(pid, &status, NULL, 0);
70*27b03b36SApple OSS Distributions 	if (WIFEXITED(status)) {
71*27b03b36SApple OSS Distributions 		T_LOG("waitpid for %d returned with status %d",
72*27b03b36SApple OSS Distributions 		    pid, WEXITSTATUS(status));
73*27b03b36SApple OSS Distributions 	} else {
74*27b03b36SApple OSS Distributions 		int sig = WTERMSIG(status);
75*27b03b36SApple OSS Distributions 		T_LOG("waitpid for %d killed by signal %d/%s",
76*27b03b36SApple OSS Distributions 		    pid, sig, sys_signame[sig]);
77*27b03b36SApple OSS Distributions 	}
78*27b03b36SApple OSS Distributions 	T_ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0,
79*27b03b36SApple OSS Distributions 	    "zlog exited cleanly");
80*27b03b36SApple OSS Distributions 
81*27b03b36SApple OSS Distributions 	/* work around rdar://84948713 */
82*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(dispatch_semaphore_wait(sema,
83*27b03b36SApple OSS Distributions 	    dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC)), 0L,
84*27b03b36SApple OSS Distributions 	    "found the line we wanted");
85*27b03b36SApple OSS Distributions 	dispatch_release(sema);
86*27b03b36SApple OSS Distributions }
87