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