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