xref: /xnu-8796.141.3/tests/get_shared_cache_address.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <sys/resource.h>
2*1b191cb5SApple OSS Distributions #include <unistd.h>
3*1b191cb5SApple OSS Distributions #include <stdlib.h>
4*1b191cb5SApple OSS Distributions #include <stdio.h>
5*1b191cb5SApple OSS Distributions #include <string.h>
6*1b191cb5SApple OSS Distributions #include <libproc.h>
7*1b191cb5SApple OSS Distributions #include <mach-o/dyld.h>
8*1b191cb5SApple OSS Distributions #include <mach-o/dyld_priv.h>
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions /*
11*1b191cb5SApple OSS Distributions  * Test helper to retrieve the address of the shared cache. The helper
12*1b191cb5SApple OSS Distributions  * also verifies that the process is correctly marked to have the shared
13*1b191cb5SApple OSS Distributions  * cache reslid when interrogated through proc_pid_rusage()
14*1b191cb5SApple OSS Distributions  */
15*1b191cb5SApple OSS Distributions int
main(int argc,char ** argv)16*1b191cb5SApple OSS Distributions main(int argc, char **argv)
17*1b191cb5SApple OSS Distributions {
18*1b191cb5SApple OSS Distributions 	size_t shared_cache_len = 0;
19*1b191cb5SApple OSS Distributions 	struct rusage_info_v5 ru = {};
20*1b191cb5SApple OSS Distributions 
21*1b191cb5SApple OSS Distributions 	if (argc != 2) {
22*1b191cb5SApple OSS Distributions 		fprintf(stderr, "Invalid helper invocation");
23*1b191cb5SApple OSS Distributions 		exit(1);
24*1b191cb5SApple OSS Distributions 	}
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions 	if (proc_pid_rusage(getpid(), RUSAGE_INFO_V5, (rusage_info_t *)&ru) != 0) {
27*1b191cb5SApple OSS Distributions 		perror("proc_pid_rusage() helper");
28*1b191cb5SApple OSS Distributions 		exit(1);
29*1b191cb5SApple OSS Distributions 	}
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions 	if (strcmp(argv[1], "check_rusage_flag") == 0) {
32*1b191cb5SApple OSS Distributions 		if (!(ru.ri_flags & RU_PROC_RUNS_RESLIDE)) {
33*1b191cb5SApple OSS Distributions 			fprintf(stderr, "Helper rusage flag check failed\n");
34*1b191cb5SApple OSS Distributions 			exit(1);
35*1b191cb5SApple OSS Distributions 		}
36*1b191cb5SApple OSS Distributions 	}
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	printf("%p\n", _dyld_get_shared_cache_range(&shared_cache_len));
39*1b191cb5SApple OSS Distributions 	exit(0);
40*1b191cb5SApple OSS Distributions }
41