xref: /xnu-11417.140.69/tests/vm_memory_tests_src/common.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include "mach_vm_tests.h"
2*43a90889SApple OSS Distributions #include "unistd.h"
3*43a90889SApple OSS Distributions 
4*43a90889SApple OSS Distributions #define TEST_TXT_FILE "/tmp/xnu.vm.sharing.test.txt"
5*43a90889SApple OSS Distributions 
6*43a90889SApple OSS Distributions static const char * lorem_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \
7*43a90889SApple OSS Distributions Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate\
8*43a90889SApple OSS Distributions velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
9*43a90889SApple OSS Distributions 
10*43a90889SApple OSS Distributions 
11*43a90889SApple OSS Distributions static int fd = 0;
12*43a90889SApple OSS Distributions static struct stat sb;
13*43a90889SApple OSS Distributions mach_port_t persistentReplyPort;
14*43a90889SApple OSS Distributions 
15*43a90889SApple OSS Distributions int
mach_server_data_setup(void ** buffer)16*43a90889SApple OSS Distributions mach_server_data_setup(void **buffer)
17*43a90889SApple OSS Distributions {
18*43a90889SApple OSS Distributions 	if (0 != access(TEST_TXT_FILE, F_OK)) {
19*43a90889SApple OSS Distributions 		/* create a test file */
20*43a90889SApple OSS Distributions 		const size_t lorem_text_length = strlen(lorem_text);
21*43a90889SApple OSS Distributions 		int w_fd = open(TEST_TXT_FILE, O_WRONLY | O_CREAT | O_TRUNC, (mode_t)0666);
22*43a90889SApple OSS Distributions 		size_t required_length = 450783;
23*43a90889SApple OSS Distributions 		assert(w_fd >= 0);
24*43a90889SApple OSS Distributions 		size_t bytes_written = 0;
25*43a90889SApple OSS Distributions 		while (bytes_written < required_length) {
26*43a90889SApple OSS Distributions 			bytes_written += (size_t)write(w_fd, &lorem_text[0], (size_t)(lorem_text_length - 1));
27*43a90889SApple OSS Distributions 			if ((bytes_written + lorem_text_length) > required_length) {
28*43a90889SApple OSS Distributions 				bytes_written += (size_t)write(w_fd, &lorem_text[0], (size_t)(required_length - bytes_written));
29*43a90889SApple OSS Distributions 				break;
30*43a90889SApple OSS Distributions 			}
31*43a90889SApple OSS Distributions 		}
32*43a90889SApple OSS Distributions 		close(w_fd);
33*43a90889SApple OSS Distributions 	}
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions 	/* Sample data set needs to be mapped in our space */
36*43a90889SApple OSS Distributions 	fd = open(TEST_TXT_FILE, O_RDONLY | O_EXCL, 0666);
37*43a90889SApple OSS Distributions 
38*43a90889SApple OSS Distributions 	if (fd < 0) {
39*43a90889SApple OSS Distributions 		printf("mach_server_data_setup: cannot open file %s - %d (%s).\n", TEST_TXT_FILE, errno, strerror(errno));
40*43a90889SApple OSS Distributions 		return errno;
41*43a90889SApple OSS Distributions 	}
42*43a90889SApple OSS Distributions 
43*43a90889SApple OSS Distributions 	if (fstat(fd, &sb) < 0) {
44*43a90889SApple OSS Distributions 		printf("mach_server_data_setup: cannot stat file %s - %d (%s).\n", TEST_TXT_FILE, errno, strerror(errno));
45*43a90889SApple OSS Distributions 		return errno;
46*43a90889SApple OSS Distributions 	}
47*43a90889SApple OSS Distributions 
48*43a90889SApple OSS Distributions #if MMAP_PATH
49*43a90889SApple OSS Distributions 	*buffer = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
50*43a90889SApple OSS Distributions 
51*43a90889SApple OSS Distributions 	if (*buffer == MAP_FAILED) {
52*43a90889SApple OSS Distributions 		printf("mach_server_remap: mmap failed - %d (%s).\n", errno, strerror(errno));
53*43a90889SApple OSS Distributions 		*buffer = NULL;
54*43a90889SApple OSS Distributions 	}
55*43a90889SApple OSS Distributions #else
56*43a90889SApple OSS Distributions 	kern_return_t kr = KERN_SUCCESS;
57*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), (mach_vm_address_t *)buffer, (mach_vm_size_t)sb.st_size, VM_FLAGS_ANYWHERE);
58*43a90889SApple OSS Distributions 	assert(kr == KERN_SUCCESS);
59*43a90889SApple OSS Distributions #endif
60*43a90889SApple OSS Distributions 	return 0;
61*43a90889SApple OSS Distributions }
62*43a90889SApple OSS Distributions 
63*43a90889SApple OSS Distributions void
mach_server_data_cleanup(void * buffer,mach_vm_address_t src,mach_vm_size_t size)64*43a90889SApple OSS Distributions mach_server_data_cleanup(void *buffer, mach_vm_address_t src, mach_vm_size_t size)
65*43a90889SApple OSS Distributions {
66*43a90889SApple OSS Distributions #if MMAP_PATH
67*43a90889SApple OSS Distributions 	if (buffer) {
68*43a90889SApple OSS Distributions 		munmap(buffer, sb.st_size);
69*43a90889SApple OSS Distributions 	}
70*43a90889SApple OSS Distributions #else
71*43a90889SApple OSS Distributions 	mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)buffer, (mach_vm_size_t)sb.st_size);
72*43a90889SApple OSS Distributions #endif
73*43a90889SApple OSS Distributions 
74*43a90889SApple OSS Distributions 	if (src) {
75*43a90889SApple OSS Distributions 		mach_vm_deallocate(mach_task_self(), src, size);
76*43a90889SApple OSS Distributions 	}
77*43a90889SApple OSS Distributions 
78*43a90889SApple OSS Distributions 	if (fd > 2) {
79*43a90889SApple OSS Distributions 		close(fd);
80*43a90889SApple OSS Distributions 	}
81*43a90889SApple OSS Distributions }
82*43a90889SApple OSS Distributions 
83*43a90889SApple OSS Distributions void
mach_server_construct_header(ipc_message_t * message,mach_port_t replyPort)84*43a90889SApple OSS Distributions mach_server_construct_header(ipc_message_t *message, mach_port_t replyPort)
85*43a90889SApple OSS Distributions {
86*43a90889SApple OSS Distributions 	bzero(message, sizeof(*message));
87*43a90889SApple OSS Distributions 	message->header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) | MACH_MSGH_BITS_COMPLEX;
88*43a90889SApple OSS Distributions 	message->header.msgh_remote_port = persistentReplyPort;//serverPort;
89*43a90889SApple OSS Distributions 	message->header.msgh_local_port = replyPort;
90*43a90889SApple OSS Distributions 	message->header.msgh_size = sizeof(*message);
91*43a90889SApple OSS Distributions 	message->header.msgh_id = 1;
92*43a90889SApple OSS Distributions }
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions void
mach_server_contruct_payload(ipc_message_t * message,mach_vm_address_t src,mach_port_t port,mach_vm_size_t size,mach_vm_offset_t misoffset,boolean_t copy,int vm_op)95*43a90889SApple OSS Distributions mach_server_contruct_payload(ipc_message_t         *message,
96*43a90889SApple OSS Distributions     mach_vm_address_t       src,
97*43a90889SApple OSS Distributions     mach_port_t                     port,
98*43a90889SApple OSS Distributions     mach_vm_size_t          size,
99*43a90889SApple OSS Distributions     mach_vm_offset_t        misoffset,
100*43a90889SApple OSS Distributions     boolean_t                       copy,
101*43a90889SApple OSS Distributions     int                                     vm_op)
102*43a90889SApple OSS Distributions {
103*43a90889SApple OSS Distributions 	if (port == MACH_PORT_NULL) {
104*43a90889SApple OSS Distributions 		message->address = src;//LD TODO: (src + 8193);
105*43a90889SApple OSS Distributions 	} else {
106*43a90889SApple OSS Distributions 		message->body.msgh_descriptor_count = 1;
107*43a90889SApple OSS Distributions 		message->port_descriptor.name = port;
108*43a90889SApple OSS Distributions 		message->port_descriptor.disposition = MACH_MSG_TYPE_COPY_SEND;
109*43a90889SApple OSS Distributions 		message->port_descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
110*43a90889SApple OSS Distributions 	}
111*43a90889SApple OSS Distributions 
112*43a90889SApple OSS Distributions 	message->pid = (uint64_t)getpid();
113*43a90889SApple OSS Distributions 	message->size = size;
114*43a90889SApple OSS Distributions 	message->vm_op = vm_op;
115*43a90889SApple OSS Distributions 	message->copy = copy;
116*43a90889SApple OSS Distributions 	message->misoffset = misoffset;
117*43a90889SApple OSS Distributions }
118*43a90889SApple OSS Distributions 
119*43a90889SApple OSS Distributions void
mach_server_create_allocation(mach_vm_address_t * src,mach_vm_size_t size,void * buffer)120*43a90889SApple OSS Distributions mach_server_create_allocation(mach_vm_address_t *src, mach_vm_size_t size, void *buffer)
121*43a90889SApple OSS Distributions {
122*43a90889SApple OSS Distributions 	kern_return_t       kr = KERN_SUCCESS;
123*43a90889SApple OSS Distributions 	mach_vm_size_t      chunk_size = 0;
124*43a90889SApple OSS Distributions 	unsigned int        chunk_count = 0;
125*43a90889SApple OSS Distributions 	mach_vm_address_t   localsrc = 0;
126*43a90889SApple OSS Distributions 
127*43a90889SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), &localsrc, size, VM_FLAGS_ANYWHERE);
128*43a90889SApple OSS Distributions 	assert(KERN_SUCCESS == kr);
129*43a90889SApple OSS Distributions 
130*43a90889SApple OSS Distributions 	chunk_size = MIN(size, (mach_vm_size_t)sb.st_size);
131*43a90889SApple OSS Distributions 
132*43a90889SApple OSS Distributions 	if (chunk_size == 0) {
133*43a90889SApple OSS Distributions 		printf("mach_server_remap: Input size is 0\n");
134*43a90889SApple OSS Distributions 		exit(0);
135*43a90889SApple OSS Distributions 	}
136*43a90889SApple OSS Distributions 
137*43a90889SApple OSS Distributions 	chunk_count = (unsigned int)(size / (mach_vm_size_t)sb.st_size);
138*43a90889SApple OSS Distributions 
139*43a90889SApple OSS Distributions 	if (debug && 0) {
140*43a90889SApple OSS Distributions 		printf("Chunks of size: 0x%llx and count: %d\n", chunk_size, chunk_count);
141*43a90889SApple OSS Distributions 	}
142*43a90889SApple OSS Distributions 
143*43a90889SApple OSS Distributions 	for (unsigned int i = 0; i < chunk_count; i++) {
144*43a90889SApple OSS Distributions 		memcpy((void*)(localsrc + (i * chunk_size)), buffer, chunk_size);
145*43a90889SApple OSS Distributions 	}
146*43a90889SApple OSS Distributions 
147*43a90889SApple OSS Distributions 	*src = localsrc;
148*43a90889SApple OSS Distributions }
149*43a90889SApple OSS Distributions 
150*43a90889SApple OSS Distributions void
server_error_out(mach_port_t port)151*43a90889SApple OSS Distributions server_error_out(mach_port_t port)
152*43a90889SApple OSS Distributions {
153*43a90889SApple OSS Distributions 	/* All done here...*/
154*43a90889SApple OSS Distributions 	kern_return_t ret;
155*43a90889SApple OSS Distributions 
156*43a90889SApple OSS Distributions 	mach_msg_size_t messageSize = sizeof(ipc_message_t) + sizeof(mach_msg_trailer_t) + 64;
157*43a90889SApple OSS Distributions 	ipc_message_t *message = (ipc_message_t *)calloc(1, messageSize);
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions 	message->header.msgh_bits = MACH_MSGH_BITS_ZERO;
160*43a90889SApple OSS Distributions 	message->header.msgh_size = messageSize;
161*43a90889SApple OSS Distributions 	message->header.msgh_remote_port = MACH_PORT_NULL;
162*43a90889SApple OSS Distributions 	message->header.msgh_local_port = port;
163*43a90889SApple OSS Distributions 
164*43a90889SApple OSS Distributions 	mach_server_construct_header(message, port);
165*43a90889SApple OSS Distributions 	message->vm_op = VM_OP_EXIT_ERROR;
166*43a90889SApple OSS Distributions 	ret = mach_msg(&message->header, MACH_SEND_MSG, message->header.msgh_size, 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
167*43a90889SApple OSS Distributions 	if (ret != KERN_SUCCESS) {
168*43a90889SApple OSS Distributions 		T_LOG("ERROR: Failed to send message to client: (%d) %s\n", ret, mach_error_string(ret));
169*43a90889SApple OSS Distributions 		exit(1);
170*43a90889SApple OSS Distributions 	}
171*43a90889SApple OSS Distributions 	T_LOG("server_error_out. abort()\n");
172*43a90889SApple OSS Distributions 	abort();
173*43a90889SApple OSS Distributions }
174