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