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