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