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