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