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