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