xref: /xnu-10002.41.9/tests/posix_spawn_alt_rosetta_helper.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 #include <stdio.h>
2 #include <mach-o/dyld.h>
3 #include <mach-o/dyld_images.h>
4 #include <libproc.h>
5 
6 /*
7  * Returns 1 if the standard Rosetta runtime is loaded, 2 if the alternative
8  * runtime is loaded, and 0 if no runtime was detected.
9  */
10 int
main(int argc,const char * argv[])11 main(int argc, const char * argv[])
12 {
13 	unsigned depth = 1;
14 	vm_size_t size = 0;
15 	vm_address_t address = 0;
16 	vm_address_t end_address;
17 	kern_return_t err = KERN_SUCCESS;
18 	mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
19 	struct vm_region_submap_info_64 info;
20 	char path_buffer[MAXPATHLEN + 1] = {0};
21 
22 	while (1) {
23 		err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth, (vm_region_info_t)&info, &count);
24 		if (err != KERN_SUCCESS) {
25 			break;
26 		}
27 
28 		end_address = address + size;
29 		err = proc_regionfilename(getpid(), address, path_buffer, MAXPATHLEN);
30 		if (err == KERN_SUCCESS) {
31 			if (strcmp(path_buffer, "/usr/libexec/rosetta/runtime") == 0) {
32 				printf("0x%016lx-0x%016lx %s\n", address, end_address, path_buffer);
33 				return 1;
34 			} else if (strcmp(path_buffer, "/usr/local/libexec/rosetta/runtime_internal") == 0) {
35 				printf("0x%016lx-0x%016lx %s\n", address, end_address, path_buffer);
36 				return 2;
37 			}
38 		}
39 
40 		address = end_address;
41 	}
42 	return 0;
43 }
44