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