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_REQUIRES_SYSCTL_NE("kern.kasan.available", 1),
48 T_META_BOOTARGS_SET("zlog1=" ZLOG_ZONE))
49 {
50 char *cmd[] = { "/usr/local/bin/zlog", "-l", "-z", ZLOG_ZONE, NULL };
51 dispatch_semaphore_t sema = dispatch_semaphore_create(0);
52 int status = 0;
53 pid_t pid;
54
55 pid = dt_launch_tool_pipe(cmd, false, NULL,
56 ^bool (char *d, size_t s, dt_pipe_data_handler_context_t *ctx) {
57 (void)ctx;
58 if (strstr(d, "active refs") && strstr(d, "operation type: ")) {
59 T_PASS("found line [%.*s]", (int)(s - 1), d);
60 dispatch_semaphore_signal(sema);
61 }
62 return false;
63 }, ^bool (char *d, size_t s, dt_pipe_data_handler_context_t *ctx) {
64 /* Forward errors to stderror for debugging */
65 (void)ctx;
66 fwrite(d, 1, s, stderr);
67 return false;
68 }, BUFFER_PATTERN_LINE, NULL);
69
70 dt_waitpid(pid, &status, NULL, 0);
71 if (WIFEXITED(status)) {
72 T_LOG("waitpid for %d returned with status %d",
73 pid, WEXITSTATUS(status));
74 } else {
75 int sig = WTERMSIG(status);
76 T_LOG("waitpid for %d killed by signal %d/%s",
77 pid, sig, sys_signame[sig]);
78 }
79 T_ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0,
80 "zlog exited cleanly");
81
82 /* work around rdar://84948713 */
83 T_ASSERT_EQ(dispatch_semaphore_wait(sema,
84 dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC)), 0L,
85 "found the line we wanted");
86 dispatch_release(sema);
87 }
88