xref: /xnu-11215.1.10/tests/text_corruption_helper.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <sys/sysctl.h>
5 #include <ptrauth.h>
6 #include <math.h>
7 #include <string.h>
8 
9 __attribute__((noinline))
10 static void
foo(void)11 foo(void)
12 {
13 	printf("In foo()\n");
14 	fflush(stdout);
15 }
16 
17 /*
18  * volatile to stop the compiler from optimizing away calls to atan()
19  */
20 volatile double zero = 0.0;
21 
22 int
main(int argc,char ** argv)23 main(int argc, char **argv)
24 {
25 	void *addr;
26 	size_t s = sizeof(addr);
27 	int err;
28 	int a;
29 
30 	/*
31 	 * needs to run as root for sysctl.
32 	 */
33 	if (geteuid() != 0) {
34 		printf("Test not running as root\n");
35 		exit(-1);
36 	}
37 
38 	if (strcmp(argv[argc - 1], "foo") == 0) {
39 		foo();
40 	} else if (strcmp(argv[argc - 1], "Xfoo") == 0) {
41 		printf("Warm up call to foo()\n");
42 		foo();
43 		addr = ptrauth_strip(&foo, ptrauth_key_function_pointer);
44 		err = sysctlbyname("vm.corrupt_text_addr", NULL, NULL, &addr, s);
45 		foo();
46 	} else if (strcmp(argv[argc - 1], "atan") == 0) {
47 		printf("atan(0) is %g\n", atan(zero));
48 	} else if (strcmp(argv[argc - 1], "Xatan") == 0) {
49 		printf("Warmup call to atan(0) is %g\n", atan(zero));
50 		addr = ptrauth_strip(&atan, ptrauth_key_function_pointer);
51 		err = sysctlbyname("vm.corrupt_text_addr", NULL, NULL, &addr, s);
52 		printf("atan(0) is %g\n", atan(zero));
53 	} else {
54 		exit(-1);
55 	}
56 }
57