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