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