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