1*fdd8201dSApple OSS Distributions #include "mach_vm_tests.h"
2*fdd8201dSApple OSS Distributions
3*fdd8201dSApple OSS Distributions T_GLOBAL_META(
4*fdd8201dSApple OSS Distributions T_META_NAMESPACE("xnu.vm"),
5*fdd8201dSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
6*fdd8201dSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"));
7*fdd8201dSApple OSS Distributions
8*fdd8201dSApple OSS Distributions extern char **environ;
9*fdd8201dSApple OSS Distributions
10*fdd8201dSApple OSS Distributions static void
11*fdd8201dSApple OSS Distributions spawn_process(char *action, char *serviceName, char *extraArg,
12*fdd8201dSApple OSS Distributions mach_port_t *server_Port, pid_t *serverPid, boolean_t use4k);
13*fdd8201dSApple OSS Distributions
14*fdd8201dSApple OSS Distributions static void mach_client(void);
15*fdd8201dSApple OSS Distributions
16*fdd8201dSApple OSS Distributions mach_port_t serverPort;
17*fdd8201dSApple OSS Distributions static pid_t serverPid;
18*fdd8201dSApple OSS Distributions
19*fdd8201dSApple OSS Distributions boolean_t debug = TRUE;
20*fdd8201dSApple OSS Distributions
21*fdd8201dSApple OSS Distributions void
spawn_process(char * action,char * serviceName,char * extraArg,mach_port_t * server_Port,pid_t * server_Pid,boolean_t use4k)22*fdd8201dSApple OSS Distributions spawn_process(char *action, char *serviceName, char *extraArg,
23*fdd8201dSApple OSS Distributions mach_port_t *server_Port, pid_t *server_Pid, boolean_t use4k)
24*fdd8201dSApple OSS Distributions {
25*fdd8201dSApple OSS Distributions char buffer[PATH_MAX];
26*fdd8201dSApple OSS Distributions char *argv[10] = {0};
27*fdd8201dSApple OSS Distributions int arg_index = 0;
28*fdd8201dSApple OSS Distributions pid_t pid = -1;
29*fdd8201dSApple OSS Distributions int r = proc_pidpath(getpid(), buffer, sizeof(buffer));
30*fdd8201dSApple OSS Distributions T_ASSERT_NE(r, -1, "proc_pidpath");
31*fdd8201dSApple OSS Distributions r = (int)strlcat(buffer, "_server", sizeof(buffer));
32*fdd8201dSApple OSS Distributions T_ASSERT_LT(r, (int)sizeof(buffer), "strlcat");
33*fdd8201dSApple OSS Distributions
34*fdd8201dSApple OSS Distributions if (use4k) {
35*fdd8201dSApple OSS Distributions int supported = 0;
36*fdd8201dSApple OSS Distributions size_t supported_size = sizeof(supported);
37*fdd8201dSApple OSS Distributions
38*fdd8201dSApple OSS Distributions r = sysctlbyname("debug.vm_mixed_pagesize_supported", &supported, &supported_size, NULL, 0);
39*fdd8201dSApple OSS Distributions if (r == 0 && supported) {
40*fdd8201dSApple OSS Distributions T_LOG("Using %s to spawn process with 4k", VM_SPAWN_TOOL);
41*fdd8201dSApple OSS Distributions argv[arg_index++] = VM_SPAWN_TOOL;
42*fdd8201dSApple OSS Distributions } else {
43*fdd8201dSApple OSS Distributions /*
44*fdd8201dSApple OSS Distributions * We didnt find debug.vm.mixed_page.supported OR its set to 0.
45*fdd8201dSApple OSS Distributions * Skip the test.
46*fdd8201dSApple OSS Distributions */
47*fdd8201dSApple OSS Distributions T_SKIP("Hardware doesn't support 4K pages, skipping test...");
48*fdd8201dSApple OSS Distributions exit(0);
49*fdd8201dSApple OSS Distributions }
50*fdd8201dSApple OSS Distributions }
51*fdd8201dSApple OSS Distributions argv[arg_index++] = (char *)&buffer[0];
52*fdd8201dSApple OSS Distributions argv[arg_index++] = (char *)action;
53*fdd8201dSApple OSS Distributions argv[arg_index++] = (char *)serviceName;
54*fdd8201dSApple OSS Distributions argv[arg_index++] = (char *)extraArg;
55*fdd8201dSApple OSS Distributions argv[arg_index++] = NULL;
56*fdd8201dSApple OSS Distributions
57*fdd8201dSApple OSS Distributions printf("posix_spawn with argv: ");
58*fdd8201dSApple OSS Distributions for (r = 0; r <= arg_index; r++) {
59*fdd8201dSApple OSS Distributions printf("%s ", argv[r]);
60*fdd8201dSApple OSS Distributions }
61*fdd8201dSApple OSS Distributions printf("\n");
62*fdd8201dSApple OSS Distributions
63*fdd8201dSApple OSS Distributions T_LOG("Spawning %s process(%s) with service name %s at %s\n", action, buffer, serviceName, buffer);
64*fdd8201dSApple OSS Distributions
65*fdd8201dSApple OSS Distributions
66*fdd8201dSApple OSS Distributions posix_spawn_file_actions_t actions;
67*fdd8201dSApple OSS Distributions posix_spawn_file_actions_init(&actions);
68*fdd8201dSApple OSS Distributions
69*fdd8201dSApple OSS Distributions if (use4k) {
70*fdd8201dSApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn(&pid, VM_SPAWN_TOOL, &actions, NULL, argv, environ), "spawn %s", serviceName);
71*fdd8201dSApple OSS Distributions } else {
72*fdd8201dSApple OSS Distributions T_ASSERT_POSIX_ZERO(posix_spawn(&pid, buffer, &actions, NULL, argv, environ), "spawn %s", serviceName);
73*fdd8201dSApple OSS Distributions }
74*fdd8201dSApple OSS Distributions posix_spawn_file_actions_destroy(&actions);
75*fdd8201dSApple OSS Distributions
76*fdd8201dSApple OSS Distributions kern_return_t ret;
77*fdd8201dSApple OSS Distributions mach_port_t servicePort;
78*fdd8201dSApple OSS Distributions int attempts = 0;
79*fdd8201dSApple OSS Distributions const int kMaxAttempts = 10;
80*fdd8201dSApple OSS Distributions do {
81*fdd8201dSApple OSS Distributions sleep(1);
82*fdd8201dSApple OSS Distributions ret = bootstrap_look_up(bootstrap_port, serviceName, &servicePort);
83*fdd8201dSApple OSS Distributions attempts++;
84*fdd8201dSApple OSS Distributions } while (ret == BOOTSTRAP_UNKNOWN_SERVICE && attempts < kMaxAttempts);
85*fdd8201dSApple OSS Distributions
86*fdd8201dSApple OSS Distributions if (ret != KERN_SUCCESS) {
87*fdd8201dSApple OSS Distributions printf("ERROR: Failed bootstrap lookup for process with mach service name '%s': (%d) %s\n", serviceName, ret, mach_error_string(ret));
88*fdd8201dSApple OSS Distributions if (pid > 0) {
89*fdd8201dSApple OSS Distributions kill(pid, SIGKILL);
90*fdd8201dSApple OSS Distributions }
91*fdd8201dSApple OSS Distributions T_FAIL("Failed bootstrap lookup for process with mach service");
92*fdd8201dSApple OSS Distributions }
93*fdd8201dSApple OSS Distributions
94*fdd8201dSApple OSS Distributions *server_Port = servicePort;
95*fdd8201dSApple OSS Distributions *server_Pid = pid;
96*fdd8201dSApple OSS Distributions T_LOG("Server pid=%d port 0x%x", pid, servicePort);
97*fdd8201dSApple OSS Distributions }
98*fdd8201dSApple OSS Distributions
99*fdd8201dSApple OSS Distributions void
mach_client()100*fdd8201dSApple OSS Distributions mach_client()
101*fdd8201dSApple OSS Distributions {
102*fdd8201dSApple OSS Distributions mach_port_t replyPort;
103*fdd8201dSApple OSS Distributions T_ASSERT_POSIX_ZERO(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyPort), "create recieve port");
104*fdd8201dSApple OSS Distributions T_ASSERT_POSIX_ZERO(mach_port_insert_right(mach_task_self(), replyPort, replyPort, MACH_MSG_TYPE_MAKE_SEND), "insert send port");
105*fdd8201dSApple OSS Distributions
106*fdd8201dSApple OSS Distributions ipc_message_t message;
107*fdd8201dSApple OSS Distributions bzero(&message, sizeof(message));
108*fdd8201dSApple OSS Distributions message.header.msgh_id = 1;
109*fdd8201dSApple OSS Distributions
110*fdd8201dSApple OSS Distributions message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_COPY_SEND);
111*fdd8201dSApple OSS Distributions message.header.msgh_remote_port = serverPort;
112*fdd8201dSApple OSS Distributions message.header.msgh_local_port = replyPort;
113*fdd8201dSApple OSS Distributions message.header.msgh_size = sizeof(message);
114*fdd8201dSApple OSS Distributions
115*fdd8201dSApple OSS Distributions /* reply creation is not necessary in this case.
116*fdd8201dSApple OSS Distributions * mach_msg_size_t replySize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
117*fdd8201dSApple OSS Distributions * ipc_message_t *reply = calloc(1, replySize);
118*fdd8201dSApple OSS Distributions */
119*fdd8201dSApple OSS Distributions T_LOG("sending message to %d of size %u", message.header.msgh_remote_port, message.header.msgh_size);
120*fdd8201dSApple OSS Distributions kern_return_t ret = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
121*fdd8201dSApple OSS Distributions T_ASSERT_MACH_SUCCESS(ret, "mach_msg to serverProcess");
122*fdd8201dSApple OSS Distributions mach_vm_client(replyPort);
123*fdd8201dSApple OSS Distributions T_LOG("Sending SIGKILL to server(%d)", serverPid);
124*fdd8201dSApple OSS Distributions kill(serverPid, SIGKILL);
125*fdd8201dSApple OSS Distributions }
126*fdd8201dSApple OSS Distributions
127*fdd8201dSApple OSS Distributions T_DECL(memory_share_tests,
128*fdd8201dSApple OSS Distributions "test vm memory sharing between client and server process with different process PAGE_SIZE",
129*fdd8201dSApple OSS Distributions T_META_ASROOT(true))
130*fdd8201dSApple OSS Distributions {
131*fdd8201dSApple OSS Distributions boolean_t use4k = FALSE;
132*fdd8201dSApple OSS Distributions char serviceName[64];
133*fdd8201dSApple OSS Distributions
134*fdd8201dSApple OSS Distributions struct sigaction action = {
135*fdd8201dSApple OSS Distributions .sa_handler = SIG_IGN,
136*fdd8201dSApple OSS Distributions .sa_flags = SA_NOCLDWAIT
137*fdd8201dSApple OSS Distributions };
138*fdd8201dSApple OSS Distributions sigaction(SIGCHLD, &action, NULL);
139*fdd8201dSApple OSS Distributions
140*fdd8201dSApple OSS Distributions if (getenv("USE4K")) {
141*fdd8201dSApple OSS Distributions use4k = TRUE;
142*fdd8201dSApple OSS Distributions }
143*fdd8201dSApple OSS Distributions
144*fdd8201dSApple OSS Distributions if (getenv("QUIET")) {
145*fdd8201dSApple OSS Distributions debug = FALSE;
146*fdd8201dSApple OSS Distributions }
147*fdd8201dSApple OSS Distributions
148*fdd8201dSApple OSS Distributions T_LOG("running with use4k=%d debug=%d", use4k, (int)debug);
149*fdd8201dSApple OSS Distributions
150*fdd8201dSApple OSS Distributions strcpy(serviceName, MACH_VM_TEST_SERVICE_NAME);
151*fdd8201dSApple OSS Distributions
152*fdd8201dSApple OSS Distributions spawn_process("machserver", serviceName, NULL, &serverPort, &serverPid, use4k);
153*fdd8201dSApple OSS Distributions mach_client();
154*fdd8201dSApple OSS Distributions }
155*fdd8201dSApple OSS Distributions
156*fdd8201dSApple OSS Distributions /*
157*fdd8201dSApple OSS Distributions * This posix spawn attribute used in the 4K test should only be valid on apple
158*fdd8201dSApple OSS Distributions * silicon macs. But we've had issues in the past where it would accidently
159*fdd8201dSApple OSS Distributions * trigger incorrect behavior on other platforms.
160*fdd8201dSApple OSS Distributions * So we run the test on all platforms to catch issues like that.
161*fdd8201dSApple OSS Distributions * On all non apple silcon mac platforms, this test should be identical to
162*fdd8201dSApple OSS Distributions * running without the 4K flag.
163*fdd8201dSApple OSS Distributions */
164*fdd8201dSApple OSS Distributions T_DECL_REF(memory_share_tests_4k, memory_share_tests, "vm memory sharing with 4k processes",
165*fdd8201dSApple OSS Distributions T_META_ENVVAR("USE4K=YES"),
166*fdd8201dSApple OSS Distributions T_META_ASROOT(true)
167*fdd8201dSApple OSS Distributions );
168