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