xref: /xnu-8796.121.2/tests/vm_memory_tests_src/vm_tests.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions //
2*c54f35caSApple OSS Distributions //  vmremaptest.c
3*c54f35caSApple OSS Distributions //
4*c54f35caSApple OSS Distributions //  Created by Lionel Desai on 9/16/19.
5*c54f35caSApple OSS Distributions //  Copyright © 2019 Apple. All rights reserved.
6*c54f35caSApple OSS Distributions //
7*c54f35caSApple OSS Distributions 
8*c54f35caSApple OSS Distributions #include "mach_vm_tests.h"
9*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
10*c54f35caSApple OSS Distributions 
11*c54f35caSApple OSS Distributions 
12*c54f35caSApple OSS Distributions #define TESTSZ (140 * 1024 * 1024ULL)
13*c54f35caSApple OSS Distributions 
14*c54f35caSApple OSS Distributions void
mach_vm_client(mach_port_t port)15*c54f35caSApple OSS Distributions mach_vm_client(mach_port_t port)
16*c54f35caSApple OSS Distributions {
17*c54f35caSApple OSS Distributions 	mach_port_t memport = MACH_PORT_NULL;
18*c54f35caSApple OSS Distributions 	mach_vm_address_t       src = 0, dest = 0, tmp = 0;
19*c54f35caSApple OSS Distributions 	mach_vm_size_t          size = 0;
20*c54f35caSApple OSS Distributions 	vm_prot_t cur_prot, max_prot;
21*c54f35caSApple OSS Distributions 	mach_port_name_t        lport = 0;
22*c54f35caSApple OSS Distributions 	kern_return_t           ret = 0;
23*c54f35caSApple OSS Distributions 	boolean_t                       copy = FALSE;
24*c54f35caSApple OSS Distributions 	mach_vm_offset_t misoffset = 0;
25*c54f35caSApple OSS Distributions 
26*c54f35caSApple OSS Distributions 	mach_msg_type_number_t countp;
27*c54f35caSApple OSS Distributions 	mach_msg_size_t messageSize = 0;
28*c54f35caSApple OSS Distributions 	ipc_message_t *message = NULL;
29*c54f35caSApple OSS Distributions 
30*c54f35caSApple OSS Distributions 	char buffer[PATH_MAX];
31*c54f35caSApple OSS Distributions 	ret = proc_pidpath(getpid(), buffer, sizeof(buffer));
32*c54f35caSApple OSS Distributions 	assert(ret != -1);
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions 	messageSize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
35*c54f35caSApple OSS Distributions 	message = (ipc_message_t *)calloc(1, messageSize);
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions 	message->header.msgh_bits = MACH_MSGH_BITS_ZERO;
38*c54f35caSApple OSS Distributions 	message->header.msgh_size = messageSize;
39*c54f35caSApple OSS Distributions 	message->header.msgh_remote_port = MACH_PORT_NULL;
40*c54f35caSApple OSS Distributions 	message->header.msgh_local_port = port;
41*c54f35caSApple OSS Distributions 
42*c54f35caSApple OSS Distributions 	while (1) {
43*c54f35caSApple OSS Distributions 		/* Awaiting the pid/src. addr/size from the server so we know what to remap from where */
44*c54f35caSApple OSS Distributions 		ret = mach_msg(&message->header, MACH_RCV_MSG | MACH_RCV_LARGE, 0, messageSize, port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
45*c54f35caSApple OSS Distributions 		if (ret == KERN_SUCCESS) {
46*c54f35caSApple OSS Distributions 			if (debug) {
47*c54f35caSApple OSS Distributions 				T_LOG("CLIENT: received info from server... 0x%llx, %lld, 0x%llx, %d - %d\n", message->address, message->size, message->misoffset, message->vm_op, message->copy);
48*c54f35caSApple OSS Distributions 			}
49*c54f35caSApple OSS Distributions 
50*c54f35caSApple OSS Distributions 			switch (message->vm_op) {
51*c54f35caSApple OSS Distributions 			case VM_OP_REMAP:
52*c54f35caSApple OSS Distributions 				ret = task_for_pid(mach_task_self(), (pid_t) message->pid, &lport);
53*c54f35caSApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "task_for_pid");
54*c54f35caSApple OSS Distributions 
55*c54f35caSApple OSS Distributions 				copy = message->copy;
56*c54f35caSApple OSS Distributions 				size = message->size;
57*c54f35caSApple OSS Distributions 				src = message->address;
58*c54f35caSApple OSS Distributions 				misoffset = 0;
59*c54f35caSApple OSS Distributions 
60*c54f35caSApple OSS Distributions 				ret = mach_vm_allocate(mach_task_self(), &tmp, size + 16384, VM_FLAGS_ANYWHERE);
61*c54f35caSApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "mach_vm_allocate");
62*c54f35caSApple OSS Distributions 				mach_vm_deallocate(mach_task_self(), tmp, size + 16384);
63*c54f35caSApple OSS Distributions 
64*c54f35caSApple OSS Distributions 				dest = tmp + 4096;
65*c54f35caSApple OSS Distributions 
66*c54f35caSApple OSS Distributions 				ret = mach_vm_remap(mach_task_self(), &dest, size, 0, VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR,
67*c54f35caSApple OSS Distributions 				    lport, src, copy,
68*c54f35caSApple OSS Distributions 				    &cur_prot,
69*c54f35caSApple OSS Distributions 				    &max_prot,
70*c54f35caSApple OSS Distributions 				    VM_INHERIT_NONE);
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions 				if (ret) {
73*c54f35caSApple OSS Distributions 					char dstval[64];
74*c54f35caSApple OSS Distributions 					memcpy(dstval, (void*) dest, 64);
75*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_remap FAILED: %s -- src 0x%llx, dest 0x%llx (%s)\n", mach_error_string(ret), src, dest, dstval);
76*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_remap FAILED");
77*c54f35caSApple OSS Distributions 				}
78*c54f35caSApple OSS Distributions 
79*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*)dest, 64);
80*c54f35caSApple OSS Distributions 				break;
81*c54f35caSApple OSS Distributions 
82*c54f35caSApple OSS Distributions 			case VM_OP_READ_OVERWRITE:
83*c54f35caSApple OSS Distributions 				ret = task_for_pid(mach_task_self(), (pid_t) message->pid, &lport);
84*c54f35caSApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "task_for_pid");
85*c54f35caSApple OSS Distributions 
86*c54f35caSApple OSS Distributions 				size = message->size;
87*c54f35caSApple OSS Distributions 				src = message->address;
88*c54f35caSApple OSS Distributions 				misoffset = 0;
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions 				mach_vm_size_t  dest_size = 0;
91*c54f35caSApple OSS Distributions 				ret = mach_vm_allocate(mach_task_self(), &tmp, size + 16384, VM_FLAGS_ANYWHERE);
92*c54f35caSApple OSS Distributions 				assert(KERN_SUCCESS == ret);
93*c54f35caSApple OSS Distributions 
94*c54f35caSApple OSS Distributions 				dest = tmp + 4096;
95*c54f35caSApple OSS Distributions 
96*c54f35caSApple OSS Distributions 				ret = mach_vm_read_overwrite(lport, src, size, dest, &dest_size);
97*c54f35caSApple OSS Distributions 
98*c54f35caSApple OSS Distributions 				if (ret) {
99*c54f35caSApple OSS Distributions 					char dstval[64];
100*c54f35caSApple OSS Distributions 					memcpy(dstval, (void*) dest, 64);
101*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_read_overwrite FAILED: %s -- src 0x%llx, dest 0x%llx (%s)\n", mach_error_string(ret), src, dest, dstval);
102*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_read_overwrite FAILED");
103*c54f35caSApple OSS Distributions 				}
104*c54f35caSApple OSS Distributions 
105*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*)dest, 64);
106*c54f35caSApple OSS Distributions 				break;
107*c54f35caSApple OSS Distributions 
108*c54f35caSApple OSS Distributions 			case VM_OP_READ:
109*c54f35caSApple OSS Distributions 				ret = task_for_pid(mach_task_self(), (pid_t) message->pid, &lport);
110*c54f35caSApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "task_for_pid");
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions 				size = message->size;
113*c54f35caSApple OSS Distributions 				src = message->address;
114*c54f35caSApple OSS Distributions 				misoffset = 0;
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions 				ret = mach_vm_read(lport, src, size, (vm_offset_t*)&dest, &countp);
117*c54f35caSApple OSS Distributions 				if (ret) {
118*c54f35caSApple OSS Distributions 					char dstval[64];
119*c54f35caSApple OSS Distributions 					memcpy(dstval, (void*) dest, 64);
120*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_read FAILED: %s -- src 0x%llx, dest 0x%llx (%s)\n", mach_error_string(ret), src, dest, dstval);
121*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_read FAILED");
122*c54f35caSApple OSS Distributions 					exit(1);
123*c54f35caSApple OSS Distributions 				}
124*c54f35caSApple OSS Distributions 
125*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*)dest, 64);
126*c54f35caSApple OSS Distributions 				break;
127*c54f35caSApple OSS Distributions 
128*c54f35caSApple OSS Distributions #if 0
129*c54f35caSApple OSS Distributions 			case VM_OP_WRITE:
130*c54f35caSApple OSS Distributions 				ret = task_for_pid(mach_task_self(), (pid_t) message->pid, &lport);
131*c54f35caSApple OSS Distributions 				T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "task_for_pid");
132*c54f35caSApple OSS Distributions 
133*c54f35caSApple OSS Distributions 				size = message->size;
134*c54f35caSApple OSS Distributions 				src = message->address;
135*c54f35caSApple OSS Distributions 				misoffset = 0;
136*c54f35caSApple OSS Distributions 
137*c54f35caSApple OSS Distributions 				ret = mach_vm_write(lport, src, dest, countp);
138*c54f35caSApple OSS Distributions 				if (ret) {
139*c54f35caSApple OSS Distributions 					char dstval[64];
140*c54f35caSApple OSS Distributions 					memcpy(dstval, (void*) dest, 64);
141*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_write FAILED: %s -- src 0x%llx, dest 0x%llx (%s)\n", mach_error_string(ret), src, dest, dstval);
142*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_write FAILED");
143*c54f35caSApple OSS Distributions 				}
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*)dest, 64);
146*c54f35caSApple OSS Distributions 				break;
147*c54f35caSApple OSS Distributions #endif
148*c54f35caSApple OSS Distributions 			case VM_OP_MEMENTRY:
149*c54f35caSApple OSS Distributions 				assert(message->body.msgh_descriptor_count == 1);
150*c54f35caSApple OSS Distributions 				dest = 0;
151*c54f35caSApple OSS Distributions 				size = message->size;
152*c54f35caSApple OSS Distributions 				memport = message->port_descriptor.name;
153*c54f35caSApple OSS Distributions 				copy = message->copy;
154*c54f35caSApple OSS Distributions 				if (copy) {
155*c54f35caSApple OSS Distributions 					misoffset = 0;
156*c54f35caSApple OSS Distributions 				} else {
157*c54f35caSApple OSS Distributions 					misoffset = message->misoffset;
158*c54f35caSApple OSS Distributions 				}
159*c54f35caSApple OSS Distributions 
160*c54f35caSApple OSS Distributions 				cur_prot = max_prot = VM_PROT_READ;
161*c54f35caSApple OSS Distributions #if 0
162*c54f35caSApple OSS Distributions 				/* This + VM_FLAGS_FIXED in mach_vm_map() will return KERN_INVALID_ARG expectedly */
163*c54f35caSApple OSS Distributions 				ret = mach_vm_allocate(mach_task_self(), &tmp, size + 16384, VM_FLAGS_ANYWHERE);
164*c54f35caSApple OSS Distributions 				dest = tmp + 4095;
165*c54f35caSApple OSS Distributions 				mach_vm_deallocate(mach_task_self(), tmp, size + 16384);
166*c54f35caSApple OSS Distributions #endif
167*c54f35caSApple OSS Distributions 				ret = mach_vm_map(mach_task_self(), &dest, size, 0 /*mask*/,
168*c54f35caSApple OSS Distributions 				    VM_FLAGS_ANYWHERE | VM_FLAGS_RETURN_DATA_ADDR,
169*c54f35caSApple OSS Distributions 				    memport, 0 /*offset*/, copy, cur_prot, max_prot, VM_INHERIT_NONE);
170*c54f35caSApple OSS Distributions 
171*c54f35caSApple OSS Distributions 				if (ret) {
172*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_map FAILED: %s -- port 0x%x\n", mach_error_string(ret), memport);
173*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_map FAILED");
174*c54f35caSApple OSS Distributions 				}
175*c54f35caSApple OSS Distributions 
176*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*)(dest + misoffset), 64);
177*c54f35caSApple OSS Distributions 				break;
178*c54f35caSApple OSS Distributions 
179*c54f35caSApple OSS Distributions 			case VM_OP_UNMAP:
180*c54f35caSApple OSS Distributions 				assert(dest);
181*c54f35caSApple OSS Distributions 				ret = mach_vm_deallocate(mach_task_self(), dest, size);
182*c54f35caSApple OSS Distributions 				if (ret) {
183*c54f35caSApple OSS Distributions 					T_LOG("CLIENT: mach_vm_deallocate FAILED: %s -- dest 0x%llx, size 0x%llx\n", mach_error_string(ret), dest, size);
184*c54f35caSApple OSS Distributions 					T_FAIL("CLIENT: mach_vm_deallocate FAILED");
185*c54f35caSApple OSS Distributions 				}
186*c54f35caSApple OSS Distributions 				/* No message to send here */
187*c54f35caSApple OSS Distributions 				continue;
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions 			case VM_OP_NONE:
190*c54f35caSApple OSS Distributions 				memcpy(message->value, (void*) (dest + misoffset), 64);
191*c54f35caSApple OSS Distributions 				break;
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions 			case VM_OP_EXIT:
194*c54f35caSApple OSS Distributions 				if (debug) {
195*c54f35caSApple OSS Distributions 					T_LOG("CLIENT EXITING ****** \n");
196*c54f35caSApple OSS Distributions 				}
197*c54f35caSApple OSS Distributions 				return;
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions 			case VM_OP_EXIT_ERROR:
200*c54f35caSApple OSS Distributions 				if (debug) {
201*c54f35caSApple OSS Distributions 					T_LOG("CLIENT EXITING WITH ERROR****** \n");
202*c54f35caSApple OSS Distributions 					T_FAIL("Revieved VM_OP_EXIT_ERROR");
203*c54f35caSApple OSS Distributions 				}
204*c54f35caSApple OSS Distributions 				return;
205*c54f35caSApple OSS Distributions 			default:
206*c54f35caSApple OSS Distributions 				break;
207*c54f35caSApple OSS Distributions 			}
208*c54f35caSApple OSS Distributions 
209*c54f35caSApple OSS Distributions 			char dstval[64];
210*c54f35caSApple OSS Distributions 			memcpy(dstval, (void*) message->value, 64);
211*c54f35caSApple OSS Distributions 			dstval[63] = '\0';
212*c54f35caSApple OSS Distributions 
213*c54f35caSApple OSS Distributions 			if (debug) {
214*c54f35caSApple OSS Distributions 				T_LOG("CLIENT: dest 0x%llx -> 0x%llx (0x%llx), *dest %s\n", dest, dest + misoffset, misoffset, dstval);
215*c54f35caSApple OSS Distributions 				/*memcpy(dstval, (void*) (dest + misoffset), 64);
216*c54f35caSApple OSS Distributions 				 *  dstval[63]='\0';
217*c54f35caSApple OSS Distributions 				 *  T_LOG("*dest %s\n", dstval);*/
218*c54f35caSApple OSS Distributions 			}
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions 			message->header.msgh_local_port = MACH_PORT_NULL;
221*c54f35caSApple OSS Distributions 
222*c54f35caSApple OSS Distributions 			ret = mach_msg(&message->header, MACH_SEND_MSG, message->header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
223*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "CLIENT: mach_msg_send FAILED");
224*c54f35caSApple OSS Distributions 		} else {
225*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "CLIENT: mach_msg_rcv FAILED");
226*c54f35caSApple OSS Distributions 		}
227*c54f35caSApple OSS Distributions 	}
228*c54f35caSApple OSS Distributions }
229*c54f35caSApple OSS Distributions 
230*c54f35caSApple OSS Distributions void
mach_server_make_memory_entry(mach_port_t replyPort)231*c54f35caSApple OSS Distributions mach_server_make_memory_entry(mach_port_t replyPort)
232*c54f35caSApple OSS Distributions {
233*c54f35caSApple OSS Distributions 	mach_vm_address_t       src = 0, lsrc = 0;
234*c54f35caSApple OSS Distributions 	mach_vm_size_t          size = TESTSZ;
235*c54f35caSApple OSS Distributions 	memory_object_size_t memsz = 0;
236*c54f35caSApple OSS Distributions 	kern_return_t           kr;
237*c54f35caSApple OSS Distributions 	boolean_t                       modified_in_server = FALSE, perm_changed = FALSE;
238*c54f35caSApple OSS Distributions 	ipc_message_t               message;
239*c54f35caSApple OSS Distributions 	ipc_message_t               *reply = NULL;
240*c54f35caSApple OSS Distributions 	char                            src_val[64], dst_val[64];
241*c54f35caSApple OSS Distributions 	mach_msg_size_t             replySize = 0;
242*c54f35caSApple OSS Distributions 	void                            *buffer = NULL;
243*c54f35caSApple OSS Distributions 	int                                     flags = 0;
244*c54f35caSApple OSS Distributions 	mach_port_t                     memport = 0;
245*c54f35caSApple OSS Distributions 	int                                     mementry_pass_idx = 0;
246*c54f35caSApple OSS Distributions 	mach_vm_offset_t        misoffset = 0;
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	if (debug) {
249*c54f35caSApple OSS Distributions 		T_LOG("\n*************** make_memory_entry_test START ***************\n");
250*c54f35caSApple OSS Distributions 	}
251*c54f35caSApple OSS Distributions 
252*c54f35caSApple OSS Distributions 	if (mach_server_data_setup(&buffer) != 0) {
253*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
254*c54f35caSApple OSS Distributions 	}
255*c54f35caSApple OSS Distributions 
256*c54f35caSApple OSS Distributions 	if (buffer == NULL) {
257*c54f35caSApple OSS Distributions 		mach_server_data_cleanup(NULL, 0, 0);
258*c54f35caSApple OSS Distributions 		exit(0);
259*c54f35caSApple OSS Distributions 	}
260*c54f35caSApple OSS Distributions 
261*c54f35caSApple OSS Distributions 	replySize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
262*c54f35caSApple OSS Distributions 	reply = calloc(1, replySize);
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions test_different_mementry_mode:
265*c54f35caSApple OSS Distributions 	/* create message to send over rights/address/pid/size */
266*c54f35caSApple OSS Distributions 	mach_server_construct_header(&message, replyPort);
267*c54f35caSApple OSS Distributions 
268*c54f35caSApple OSS Distributions 	/* allocation that we plan to remap in the client */
269*c54f35caSApple OSS Distributions 	mach_server_create_allocation(&src, size, buffer);
270*c54f35caSApple OSS Distributions 
271*c54f35caSApple OSS Distributions 	memsz = 8191;
272*c54f35caSApple OSS Distributions 	lsrc = src + 94095;
273*c54f35caSApple OSS Distributions 	int pgmask = (getpagesize() - 1);
274*c54f35caSApple OSS Distributions 	misoffset = 94095 - (94095 & ~pgmask);
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions 	if (mementry_pass_idx < 2) {
277*c54f35caSApple OSS Distributions 		if (mementry_pass_idx == 0) {
278*c54f35caSApple OSS Distributions 			flags = VM_PROT_DEFAULT | MAP_MEM_VM_COPY | MAP_MEM_USE_DATA_ADDR;
279*c54f35caSApple OSS Distributions 			T_LOG("mach_make_memory_entry VM_COPY | USE_DATA_ADDR test...");
280*c54f35caSApple OSS Distributions 		} else {
281*c54f35caSApple OSS Distributions 			flags = VM_PROT_READ | MAP_MEM_VM_SHARE;
282*c54f35caSApple OSS Distributions 			T_LOG("mach_make_memory_entry VM_SHARE test...");
283*c54f35caSApple OSS Distributions 		}
284*c54f35caSApple OSS Distributions 		kr = mach_vm_protect(mach_task_self(), (mach_vm_address_t) lsrc, (mach_vm_size_t)getpagesize(), FALSE, VM_PROT_READ);
285*c54f35caSApple OSS Distributions 		assert(kr == KERN_SUCCESS);
286*c54f35caSApple OSS Distributions 		perm_changed = TRUE;
287*c54f35caSApple OSS Distributions 	} else {
288*c54f35caSApple OSS Distributions 		flags = VM_PROT_DEFAULT;
289*c54f35caSApple OSS Distributions 		perm_changed = FALSE;
290*c54f35caSApple OSS Distributions 		T_LOG("mach_make_memory_entry DEFAULT test...");
291*c54f35caSApple OSS Distributions 	}
292*c54f35caSApple OSS Distributions 
293*c54f35caSApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(), &memsz, lsrc, flags, &memport, MACH_PORT_NULL);
294*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
295*c54f35caSApple OSS Distributions 		T_LOG("ERROR: mach_make_memory_entry_64 try (%d) failed in Client: (%d) %s\n",
296*c54f35caSApple OSS Distributions 		    mementry_pass_idx + 1, kr, mach_error_string(kr));
297*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
298*c54f35caSApple OSS Distributions 	}
299*c54f35caSApple OSS Distributions 
300*c54f35caSApple OSS Distributions 	mach_server_contruct_payload(&message, lsrc, memport, memsz, misoffset, ((flags & MAP_MEM_VM_COPY) == MAP_MEM_VM_COPY) /*copy*/, VM_OP_MEMENTRY);
301*c54f35caSApple OSS Distributions 
302*c54f35caSApple OSS Distributions 	memcpy(src_val, (void*) lsrc, 64);
303*c54f35caSApple OSS Distributions 	src_val[63] = '\0';
304*c54f35caSApple OSS Distributions 
305*c54f35caSApple OSS Distributions check_again:
306*c54f35caSApple OSS Distributions 	/* Sending over pid/src address/size */
307*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
310*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
311*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
312*c54f35caSApple OSS Distributions 	}
313*c54f35caSApple OSS Distributions 
314*c54f35caSApple OSS Distributions 	/* Ack from client that it worked */
315*c54f35caSApple OSS Distributions 
316*c54f35caSApple OSS Distributions 	bzero(reply, replySize);
317*c54f35caSApple OSS Distributions 
318*c54f35caSApple OSS Distributions 	kr = mach_msg(&reply->header, MACH_RCV_MSG, 0, replySize, replyPort, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
319*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
320*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to get reply from client: (%d) %s\n", kr, mach_error_string(kr));
321*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
322*c54f35caSApple OSS Distributions 	}
323*c54f35caSApple OSS Distributions 
324*c54f35caSApple OSS Distributions 	memcpy(dst_val, &reply->value, 64);
325*c54f35caSApple OSS Distributions 	dst_val[63] = '\0';
326*c54f35caSApple OSS Distributions 
327*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
328*c54f35caSApple OSS Distributions 		if (strncmp(src_val, dst_val, 64)) {
329*c54f35caSApple OSS Distributions 			T_LOG("FAILED\n");
330*c54f35caSApple OSS Distributions 			T_LOG("(%d) Pre modification mach_make_memory_entry() FAILED: copy(%d) src_val: %s  dest_val: %s\n", mementry_pass_idx + 1, message.copy, src_val, dst_val);
331*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
332*c54f35caSApple OSS Distributions 		}
333*c54f35caSApple OSS Distributions 	} else {
334*c54f35caSApple OSS Distributions 		if (message.copy == TRUE) {
335*c54f35caSApple OSS Distributions 			if (strncmp(src_val, dst_val, 64) == 0) {
336*c54f35caSApple OSS Distributions 				T_LOG("FAILED\n");
337*c54f35caSApple OSS Distributions 				T_LOG("(%d) Data mismatch with Copy: %d src_val: %s  dest_val: %s\n",
338*c54f35caSApple OSS Distributions 				    mementry_pass_idx + 1, message.copy, src_val, dst_val);
339*c54f35caSApple OSS Distributions 				server_error_out(replyPort);
340*c54f35caSApple OSS Distributions 			}
341*c54f35caSApple OSS Distributions 		} else {
342*c54f35caSApple OSS Distributions 			if (strncmp(src_val, dst_val, 64)) {
343*c54f35caSApple OSS Distributions 				T_LOG("FAILED\n");
344*c54f35caSApple OSS Distributions 				T_LOG("(%d) Data mismatch with Copy: %d src_val: %s  dest_val: %s\n",
345*c54f35caSApple OSS Distributions 				    mementry_pass_idx + 1, message.copy, src_val, dst_val);
346*c54f35caSApple OSS Distributions 				server_error_out(replyPort);
347*c54f35caSApple OSS Distributions 			}
348*c54f35caSApple OSS Distributions 		}
349*c54f35caSApple OSS Distributions 	}
350*c54f35caSApple OSS Distributions 
351*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
352*c54f35caSApple OSS Distributions 		/* Now we change our data that has been mapped elsewhere */
353*c54f35caSApple OSS Distributions 		if (perm_changed) {
354*c54f35caSApple OSS Distributions 			kr = mach_vm_protect(mach_task_self(), (mach_vm_address_t) lsrc, (mach_vm_size_t)getpagesize(), FALSE, VM_PROT_READ | VM_PROT_WRITE);
355*c54f35caSApple OSS Distributions 			assert(kr == KERN_SUCCESS);
356*c54f35caSApple OSS Distributions 		}
357*c54f35caSApple OSS Distributions 
358*c54f35caSApple OSS Distributions 		memcpy((void*) lsrc, "THIS IS DIFFERENT -- BUT WE DON'T know if that's expecTED", 64);
359*c54f35caSApple OSS Distributions 
360*c54f35caSApple OSS Distributions 		if (perm_changed) {
361*c54f35caSApple OSS Distributions 			kr = mach_vm_protect(mach_task_self(), (mach_vm_address_t) lsrc, (mach_vm_size_t)getpagesize(), FALSE, VM_PROT_READ);
362*c54f35caSApple OSS Distributions 			assert(kr == KERN_SUCCESS);
363*c54f35caSApple OSS Distributions 		}
364*c54f35caSApple OSS Distributions 
365*c54f35caSApple OSS Distributions 		memcpy(src_val, (void*) lsrc, 64);
366*c54f35caSApple OSS Distributions 		src_val[63] = '\0';
367*c54f35caSApple OSS Distributions 		modified_in_server = TRUE;
368*c54f35caSApple OSS Distributions 		message.vm_op = VM_OP_NONE;
369*c54f35caSApple OSS Distributions 
370*c54f35caSApple OSS Distributions 		/* Check to see if the data in the other process is as expected */
371*c54f35caSApple OSS Distributions 		goto check_again;
372*c54f35caSApple OSS Distributions 	}
373*c54f35caSApple OSS Distributions 
374*c54f35caSApple OSS Distributions 	if (mementry_pass_idx < 2) {
375*c54f35caSApple OSS Distributions 		/* Next remap mode...so ask the other process to unmap the older mapping. */
376*c54f35caSApple OSS Distributions 		message.vm_op = VM_OP_UNMAP;
377*c54f35caSApple OSS Distributions 		kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
378*c54f35caSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
379*c54f35caSApple OSS Distributions 			T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
380*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
381*c54f35caSApple OSS Distributions 		}
382*c54f35caSApple OSS Distributions 
383*c54f35caSApple OSS Distributions 		mach_port_deallocate(mach_task_self(), memport);
384*c54f35caSApple OSS Distributions 		memport = MACH_PORT_NULL;
385*c54f35caSApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), src, size);
386*c54f35caSApple OSS Distributions 
387*c54f35caSApple OSS Distributions 		T_LOG("PASSED\n");
388*c54f35caSApple OSS Distributions 
389*c54f35caSApple OSS Distributions 		mementry_pass_idx++;
390*c54f35caSApple OSS Distributions 		modified_in_server = FALSE;
391*c54f35caSApple OSS Distributions 
392*c54f35caSApple OSS Distributions 		goto test_different_mementry_mode;
393*c54f35caSApple OSS Distributions 	}
394*c54f35caSApple OSS Distributions 
395*c54f35caSApple OSS Distributions 	T_LOG("PASSED\n");
396*c54f35caSApple OSS Distributions 
397*c54f35caSApple OSS Distributions 	/* Unmap old mapping in the other process. */
398*c54f35caSApple OSS Distributions 	message.vm_op = VM_OP_UNMAP;
399*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
400*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
401*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
402*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
403*c54f35caSApple OSS Distributions 	}
404*c54f35caSApple OSS Distributions 
405*c54f35caSApple OSS Distributions 	free(reply);
406*c54f35caSApple OSS Distributions 	reply = NULL;
407*c54f35caSApple OSS Distributions 
408*c54f35caSApple OSS Distributions 	mach_port_deallocate(mach_task_self(), memport);
409*c54f35caSApple OSS Distributions 	memport = MACH_PORT_NULL;
410*c54f35caSApple OSS Distributions 
411*c54f35caSApple OSS Distributions 	mach_server_data_cleanup(buffer, src, size);
412*c54f35caSApple OSS Distributions 	buffer = NULL;
413*c54f35caSApple OSS Distributions 
414*c54f35caSApple OSS Distributions 	if (debug) {
415*c54f35caSApple OSS Distributions 		T_LOG("*************** mach_make_memory_entry_test END ***************\n");
416*c54f35caSApple OSS Distributions 	}
417*c54f35caSApple OSS Distributions }
418*c54f35caSApple OSS Distributions 
419*c54f35caSApple OSS Distributions void
mach_server_read(mach_port_t replyPort,int op)420*c54f35caSApple OSS Distributions mach_server_read(mach_port_t replyPort, int op)
421*c54f35caSApple OSS Distributions {
422*c54f35caSApple OSS Distributions 	mach_vm_address_t       src;
423*c54f35caSApple OSS Distributions 	mach_vm_size_t          size = TESTSZ;
424*c54f35caSApple OSS Distributions 	kern_return_t           kr;
425*c54f35caSApple OSS Distributions 	boolean_t                       modified_in_server = FALSE;
426*c54f35caSApple OSS Distributions 	ipc_message_t               message;
427*c54f35caSApple OSS Distributions 	char                            src_val[64], dst_val[64];
428*c54f35caSApple OSS Distributions 	mach_msg_size_t             replySize = 0;
429*c54f35caSApple OSS Distributions 	ipc_message_t               *reply = NULL;
430*c54f35caSApple OSS Distributions 	void                            *buffer = NULL;
431*c54f35caSApple OSS Distributions 
432*c54f35caSApple OSS Distributions 	if (debug) {
433*c54f35caSApple OSS Distributions 		T_LOG("\n*************** vm_read / write / overwrite_test START ***************\n");
434*c54f35caSApple OSS Distributions 	}
435*c54f35caSApple OSS Distributions 
436*c54f35caSApple OSS Distributions 	{
437*c54f35caSApple OSS Distributions 		char opname[16];
438*c54f35caSApple OSS Distributions 		if (op == VM_OP_READ) {
439*c54f35caSApple OSS Distributions 			strlcpy(opname, "read", 5);
440*c54f35caSApple OSS Distributions 		}
441*c54f35caSApple OSS Distributions 		if (op == VM_OP_WRITE) {
442*c54f35caSApple OSS Distributions 			strlcpy(opname, "write", 6);
443*c54f35caSApple OSS Distributions 		}
444*c54f35caSApple OSS Distributions 		if (op == VM_OP_READ_OVERWRITE) {
445*c54f35caSApple OSS Distributions 			strlcpy(opname, "read_overwrite", 15);
446*c54f35caSApple OSS Distributions 		}
447*c54f35caSApple OSS Distributions 
448*c54f35caSApple OSS Distributions 		T_LOG("vm_%s test...", opname);
449*c54f35caSApple OSS Distributions 	}
450*c54f35caSApple OSS Distributions 
451*c54f35caSApple OSS Distributions 	if (mach_server_data_setup(&buffer) != 0) {
452*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
453*c54f35caSApple OSS Distributions 	}
454*c54f35caSApple OSS Distributions 
455*c54f35caSApple OSS Distributions 	if (buffer == NULL) {
456*c54f35caSApple OSS Distributions 		mach_server_data_cleanup(NULL, 0, 0);
457*c54f35caSApple OSS Distributions 		exit(0);
458*c54f35caSApple OSS Distributions 	}
459*c54f35caSApple OSS Distributions 
460*c54f35caSApple OSS Distributions 	replySize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
461*c54f35caSApple OSS Distributions 	reply = calloc(1, replySize);
462*c54f35caSApple OSS Distributions 
463*c54f35caSApple OSS Distributions 	/* create message to send over rights/address/pid/size */
464*c54f35caSApple OSS Distributions 	mach_server_construct_header(&message, replyPort);
465*c54f35caSApple OSS Distributions 
466*c54f35caSApple OSS Distributions 	/* allocation that we plan to remap in the client */
467*c54f35caSApple OSS Distributions 	mach_server_create_allocation(&src, size, buffer);
468*c54f35caSApple OSS Distributions 
469*c54f35caSApple OSS Distributions 	mach_server_contruct_payload(&message, src, MACH_PORT_NULL /* port */, size, 0, TRUE /*copy*/, op);
470*c54f35caSApple OSS Distributions 	if (debug) {
471*c54f35caSApple OSS Distributions 		T_LOG("server COPY: Sending 0x%llx, %d, 0x%llx\n", message.address, getpid(), message.size);
472*c54f35caSApple OSS Distributions 	}
473*c54f35caSApple OSS Distributions 	memcpy(src_val, (void*)message.address, 64);
474*c54f35caSApple OSS Distributions 
475*c54f35caSApple OSS Distributions check_again:
476*c54f35caSApple OSS Distributions 	/* Sending over pid/src address/size */
477*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
478*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
479*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
480*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
481*c54f35caSApple OSS Distributions 	}
482*c54f35caSApple OSS Distributions 
483*c54f35caSApple OSS Distributions 	/* Ack from client that it worked */
484*c54f35caSApple OSS Distributions 
485*c54f35caSApple OSS Distributions 	bzero(reply, replySize);
486*c54f35caSApple OSS Distributions 
487*c54f35caSApple OSS Distributions 	kr = mach_msg(&reply->header, MACH_RCV_MSG, 0, replySize, replyPort, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
488*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
489*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to get reply from client: (%d) %s\n", kr, mach_error_string(kr));
490*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
491*c54f35caSApple OSS Distributions 	}
492*c54f35caSApple OSS Distributions 
493*c54f35caSApple OSS Distributions 	memcpy(dst_val, &reply->value, 64);
494*c54f35caSApple OSS Distributions 
495*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
496*c54f35caSApple OSS Distributions 		if (strncmp(src_val, dst_val, 64)) {
497*c54f35caSApple OSS Distributions 			T_LOG("Pre modification (op: %d) FAILED: src_val: %s  dest_val: %s\n", op, src_val, dst_val);
498*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
499*c54f35caSApple OSS Distributions 		}
500*c54f35caSApple OSS Distributions 	} else {
501*c54f35caSApple OSS Distributions 		if (strncmp(src_val, dst_val, 64) == 0) {
502*c54f35caSApple OSS Distributions 			T_LOG("Data mismatch (op:%d) with Copy: %d src_val: %s  dest_val: %s\n", op, message.copy, src_val, dst_val);
503*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
504*c54f35caSApple OSS Distributions 		}
505*c54f35caSApple OSS Distributions 	}
506*c54f35caSApple OSS Distributions 
507*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
508*c54f35caSApple OSS Distributions 		/* Now we change our data that has been mapped elsewhere */
509*c54f35caSApple OSS Distributions 		memcpy((void*)message.address, "THIS IS DIFFERENT -- BUT WE DON'T know if that's expecTED", 64);
510*c54f35caSApple OSS Distributions 		memcpy(src_val, (void*)message.address, 64);
511*c54f35caSApple OSS Distributions 		modified_in_server = TRUE;
512*c54f35caSApple OSS Distributions 		message.vm_op = VM_OP_NONE;
513*c54f35caSApple OSS Distributions 
514*c54f35caSApple OSS Distributions 		/* Check to see if the data in the other process is as expected */
515*c54f35caSApple OSS Distributions 		goto check_again;
516*c54f35caSApple OSS Distributions 	}
517*c54f35caSApple OSS Distributions 
518*c54f35caSApple OSS Distributions 	/* Unmap old mapping in the other process. */
519*c54f35caSApple OSS Distributions 	message.vm_op = VM_OP_UNMAP;
520*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
521*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
522*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
523*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
524*c54f35caSApple OSS Distributions 	}
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions 	free(reply);
527*c54f35caSApple OSS Distributions 	reply = NULL;
528*c54f35caSApple OSS Distributions 
529*c54f35caSApple OSS Distributions 	mach_server_data_cleanup(buffer, src, size);
530*c54f35caSApple OSS Distributions 	buffer = NULL;
531*c54f35caSApple OSS Distributions 
532*c54f35caSApple OSS Distributions 	if (debug) {
533*c54f35caSApple OSS Distributions 		T_LOG("*************** vm_read_test END ***************\n");
534*c54f35caSApple OSS Distributions 	}
535*c54f35caSApple OSS Distributions 
536*c54f35caSApple OSS Distributions 	T_LOG("PASSED\n");
537*c54f35caSApple OSS Distributions }
538*c54f35caSApple OSS Distributions 
539*c54f35caSApple OSS Distributions void
mach_server_remap(mach_port_t replyPort)540*c54f35caSApple OSS Distributions mach_server_remap(mach_port_t replyPort)
541*c54f35caSApple OSS Distributions {
542*c54f35caSApple OSS Distributions 	mach_vm_address_t       src = 0, lsrc = 0;
543*c54f35caSApple OSS Distributions 	mach_vm_size_t          size = TESTSZ;
544*c54f35caSApple OSS Distributions 	kern_return_t           kr;
545*c54f35caSApple OSS Distributions 	int                                     remap_copy_pass_idx = 0;
546*c54f35caSApple OSS Distributions 	boolean_t                       modified_in_server = FALSE;
547*c54f35caSApple OSS Distributions 	void                            *buffer;
548*c54f35caSApple OSS Distributions 	ipc_message_t               message;
549*c54f35caSApple OSS Distributions 	char                            src_val[64], dst_val[64];
550*c54f35caSApple OSS Distributions 	mach_msg_size_t             replySize = 0;
551*c54f35caSApple OSS Distributions 	ipc_message_t               *reply = NULL;
552*c54f35caSApple OSS Distributions 
553*c54f35caSApple OSS Distributions 	if (debug) {
554*c54f35caSApple OSS Distributions 		T_LOG("\n*************** vm_remap_test START ***************\n");
555*c54f35caSApple OSS Distributions 	}
556*c54f35caSApple OSS Distributions 
557*c54f35caSApple OSS Distributions 	if (mach_server_data_setup(&buffer) != 0) {
558*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
559*c54f35caSApple OSS Distributions 	}
560*c54f35caSApple OSS Distributions 
561*c54f35caSApple OSS Distributions 	if (buffer == NULL) {
562*c54f35caSApple OSS Distributions 		mach_server_data_cleanup(NULL, 0, 0);
563*c54f35caSApple OSS Distributions 		exit(0);
564*c54f35caSApple OSS Distributions 	}
565*c54f35caSApple OSS Distributions 
566*c54f35caSApple OSS Distributions 	replySize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
567*c54f35caSApple OSS Distributions 	reply = calloc(1, replySize);
568*c54f35caSApple OSS Distributions 
569*c54f35caSApple OSS Distributions remap_again:
570*c54f35caSApple OSS Distributions 
571*c54f35caSApple OSS Distributions 	T_LOG("vm_remap (copy = %s) test...", ((remap_copy_pass_idx == 0) ? "FALSE" : "TRUE"));
572*c54f35caSApple OSS Distributions 
573*c54f35caSApple OSS Distributions 	/* create message to send over rights/address/pid/size */
574*c54f35caSApple OSS Distributions 	mach_server_construct_header(&message, replyPort);
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions 	/* server allocation that we plan to remap in the client */
577*c54f35caSApple OSS Distributions 	mach_server_create_allocation(&src, size, buffer);
578*c54f35caSApple OSS Distributions 
579*c54f35caSApple OSS Distributions 	lsrc = src + 8193;
580*c54f35caSApple OSS Distributions 
581*c54f35caSApple OSS Distributions 	mach_server_contruct_payload(&message, lsrc, MACH_PORT_NULL /* port */, size - 9000, 0, remap_copy_pass_idx /*copy*/, VM_OP_REMAP);
582*c54f35caSApple OSS Distributions 	if (debug) {
583*c54f35caSApple OSS Distributions 		T_LOG("server COPY: Sending 0x%llx, %d, 0x%llx\n", message.address, getpid(), message.size);
584*c54f35caSApple OSS Distributions 	}
585*c54f35caSApple OSS Distributions 
586*c54f35caSApple OSS Distributions 	memcpy(src_val, (void*)lsrc, 64);
587*c54f35caSApple OSS Distributions 	src_val[63] = '\0';
588*c54f35caSApple OSS Distributions 
589*c54f35caSApple OSS Distributions check_again:
590*c54f35caSApple OSS Distributions 	/* Sending over pid/src address/size */
591*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
592*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
593*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
594*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
595*c54f35caSApple OSS Distributions 	}
596*c54f35caSApple OSS Distributions 
597*c54f35caSApple OSS Distributions 	/* Ack from client that it worked */
598*c54f35caSApple OSS Distributions 
599*c54f35caSApple OSS Distributions 	bzero(reply, replySize);
600*c54f35caSApple OSS Distributions 
601*c54f35caSApple OSS Distributions 	kr = mach_msg(&reply->header, MACH_RCV_MSG, 0, replySize, replyPort, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
602*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
603*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to get reply from client: (%d) %s\n", kr, mach_error_string(kr));
604*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
605*c54f35caSApple OSS Distributions 	}
606*c54f35caSApple OSS Distributions 
607*c54f35caSApple OSS Distributions 	memcpy(dst_val, &reply->value, 64);
608*c54f35caSApple OSS Distributions 	dst_val[63] = '\0';
609*c54f35caSApple OSS Distributions 
610*c54f35caSApple OSS Distributions 
611*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
612*c54f35caSApple OSS Distributions 		if (strncmp(src_val, dst_val, 64)) {
613*c54f35caSApple OSS Distributions 			T_LOG("Pre modification remap() FAILED: copy(%d) src_val: %s  dest_val: %s\n",
614*c54f35caSApple OSS Distributions 			    message.copy, src_val, dst_val);
615*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
616*c54f35caSApple OSS Distributions 		}
617*c54f35caSApple OSS Distributions 	} else {
618*c54f35caSApple OSS Distributions 		if (message.copy == TRUE) {
619*c54f35caSApple OSS Distributions 			if (strcmp(src_val, dst_val) == 0) {
620*c54f35caSApple OSS Distributions 				T_LOG("Data mismatch with Copy: %d src_val: %s  dest_val: %s\n",
621*c54f35caSApple OSS Distributions 				    message.copy, src_val, dst_val);
622*c54f35caSApple OSS Distributions 				server_error_out(replyPort);
623*c54f35caSApple OSS Distributions 			}
624*c54f35caSApple OSS Distributions 		} else {
625*c54f35caSApple OSS Distributions 			if (strcmp(src_val, dst_val)) {
626*c54f35caSApple OSS Distributions 				T_LOG("Data mismatch with Copy: %d src_val: %s  dest_val: %s\n",
627*c54f35caSApple OSS Distributions 				    message.copy, src_val, dst_val);
628*c54f35caSApple OSS Distributions 				server_error_out(replyPort);
629*c54f35caSApple OSS Distributions 			}
630*c54f35caSApple OSS Distributions 		}
631*c54f35caSApple OSS Distributions 	}
632*c54f35caSApple OSS Distributions 
633*c54f35caSApple OSS Distributions 	if (modified_in_server == FALSE) {
634*c54f35caSApple OSS Distributions 		/* Now we change our data that has been mapped elsewhere */
635*c54f35caSApple OSS Distributions 		memcpy((void*)message.address, "THIS IS DIFFERENT -- BUT WE DON'T know if that's expecTED", 64);
636*c54f35caSApple OSS Distributions 		memcpy(src_val, (void*)message.address, 64);
637*c54f35caSApple OSS Distributions 		src_val[63] = '\0';
638*c54f35caSApple OSS Distributions 
639*c54f35caSApple OSS Distributions 		modified_in_server = TRUE;
640*c54f35caSApple OSS Distributions 		message.vm_op = VM_OP_NONE;
641*c54f35caSApple OSS Distributions 
642*c54f35caSApple OSS Distributions 		/* Check to see if the data in the other process is as expected */
643*c54f35caSApple OSS Distributions 		goto check_again;
644*c54f35caSApple OSS Distributions 	}
645*c54f35caSApple OSS Distributions 
646*c54f35caSApple OSS Distributions 	if (remap_copy_pass_idx == 0) {
647*c54f35caSApple OSS Distributions 		/* Next remap mode...so ask the other process to unmap the older mapping. */
648*c54f35caSApple OSS Distributions 		message.vm_op = VM_OP_UNMAP;
649*c54f35caSApple OSS Distributions 		kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
650*c54f35caSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
651*c54f35caSApple OSS Distributions 			T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
652*c54f35caSApple OSS Distributions 			server_error_out(replyPort);
653*c54f35caSApple OSS Distributions 		}
654*c54f35caSApple OSS Distributions 
655*c54f35caSApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), src, size);
656*c54f35caSApple OSS Distributions 
657*c54f35caSApple OSS Distributions 		T_LOG("PASSED\n");
658*c54f35caSApple OSS Distributions 
659*c54f35caSApple OSS Distributions 		remap_copy_pass_idx++;
660*c54f35caSApple OSS Distributions 		modified_in_server = FALSE;
661*c54f35caSApple OSS Distributions 
662*c54f35caSApple OSS Distributions 		/* Next remap pass to test (copy == TRUE). Send data out again to the other process to remap. */
663*c54f35caSApple OSS Distributions 		goto remap_again;
664*c54f35caSApple OSS Distributions 	}
665*c54f35caSApple OSS Distributions 
666*c54f35caSApple OSS Distributions 	T_LOG("PASSED\n");
667*c54f35caSApple OSS Distributions 
668*c54f35caSApple OSS Distributions 	/* Unmap old mapping in the other process. */
669*c54f35caSApple OSS Distributions 	message.vm_op = VM_OP_UNMAP;
670*c54f35caSApple OSS Distributions 	kr = mach_msg(&message.header, MACH_SEND_MSG, message.header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
671*c54f35caSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
672*c54f35caSApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", kr, mach_error_string(kr));
673*c54f35caSApple OSS Distributions 		server_error_out(replyPort);
674*c54f35caSApple OSS Distributions 	}
675*c54f35caSApple OSS Distributions 
676*c54f35caSApple OSS Distributions 	free(reply);
677*c54f35caSApple OSS Distributions 	reply = NULL;
678*c54f35caSApple OSS Distributions 
679*c54f35caSApple OSS Distributions 	mach_server_data_cleanup(buffer, src, size);
680*c54f35caSApple OSS Distributions 	buffer = NULL;
681*c54f35caSApple OSS Distributions 
682*c54f35caSApple OSS Distributions 	if (debug) {
683*c54f35caSApple OSS Distributions 		T_LOG("*************** vm_remap_test END ***************\n");
684*c54f35caSApple OSS Distributions 	}
685*c54f35caSApple OSS Distributions }
686