xref: /xnu-10063.141.1/tests/text_corruption.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions #include <unistd.h>
2*d8b80295SApple OSS Distributions #include <stdio.h>
3*d8b80295SApple OSS Distributions 
4*d8b80295SApple OSS Distributions #include <darwintest.h>
5*d8b80295SApple OSS Distributions #include <darwintest_utils.h>
6*d8b80295SApple OSS Distributions 
7*d8b80295SApple OSS Distributions T_GLOBAL_META(
8*d8b80295SApple OSS Distributions /*
9*d8b80295SApple OSS Distributions  * We're going to corrupt shared library text, so don't
10*d8b80295SApple OSS Distributions  * run with other tests.
11*d8b80295SApple OSS Distributions  */
12*d8b80295SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(false),
13*d8b80295SApple OSS Distributions 	T_META_REQUIRES_SYSCTL_NE("kern.page_protection_type", 2)
14*d8b80295SApple OSS Distributions 	);
15*d8b80295SApple OSS Distributions 
16*d8b80295SApple OSS Distributions /*
17*d8b80295SApple OSS Distributions  * No system(3c) on watchOS, so provide our own.
18*d8b80295SApple OSS Distributions  * returns -1 if fails to run
19*d8b80295SApple OSS Distributions  * returns 0 if process exits normally.
20*d8b80295SApple OSS Distributions  * returns +n if process exits due to signal N
21*d8b80295SApple OSS Distributions  */
22*d8b80295SApple OSS Distributions static int
my_system(const char * command)23*d8b80295SApple OSS Distributions my_system(const char *command)
24*d8b80295SApple OSS Distributions {
25*d8b80295SApple OSS Distributions 	pid_t pid;
26*d8b80295SApple OSS Distributions 	int status = 0;
27*d8b80295SApple OSS Distributions 	int signal = 0;
28*d8b80295SApple OSS Distributions 	int err;
29*d8b80295SApple OSS Distributions 	const char *argv[] = {
30*d8b80295SApple OSS Distributions 		"/bin/sh",
31*d8b80295SApple OSS Distributions 		"-c",
32*d8b80295SApple OSS Distributions 		command,
33*d8b80295SApple OSS Distributions 		NULL
34*d8b80295SApple OSS Distributions 	};
35*d8b80295SApple OSS Distributions 
36*d8b80295SApple OSS Distributions 	if (dt_launch_tool(&pid, (char **)(void *)argv, FALSE, NULL, NULL)) {
37*d8b80295SApple OSS Distributions 		return -1;
38*d8b80295SApple OSS Distributions 	}
39*d8b80295SApple OSS Distributions 
40*d8b80295SApple OSS Distributions 	err = dt_waitpid(pid, &status, &signal, 30);
41*d8b80295SApple OSS Distributions 	if (err) {
42*d8b80295SApple OSS Distributions 		return 0;
43*d8b80295SApple OSS Distributions 	}
44*d8b80295SApple OSS Distributions 
45*d8b80295SApple OSS Distributions 	return signal;
46*d8b80295SApple OSS Distributions }
47*d8b80295SApple OSS Distributions 
48*d8b80295SApple OSS Distributions 
49*d8b80295SApple OSS Distributions /*
50*d8b80295SApple OSS Distributions  * The tests are run in the following order:
51*d8b80295SApple OSS Distributions  *
52*d8b80295SApple OSS Distributions  * - call foo
53*d8b80295SApple OSS Distributions  * - corrupt foo, then call foo
54*d8b80295SApple OSS Distributions  * - call foo
55*d8b80295SApple OSS Distributions  *
56*d8b80295SApple OSS Distributions  * - call atan
57*d8b80295SApple OSS Distributions  * - corrupt atan, then call atan
58*d8b80295SApple OSS Distributions  * - call atan
59*d8b80295SApple OSS Distributions  *
60*d8b80295SApple OSS Distributions  * The first and last of each should exit normally. The middle one should exit with SIGILL.
61*d8b80295SApple OSS Distributions  *
62*d8b80295SApple OSS Distributions  * atan() was picked as a shared region function that isn't likely used by any normal daemons.
63*d8b80295SApple OSS Distributions  */
64*d8b80295SApple OSS Distributions T_DECL(text_corruption_recovery, "test detection/recovery of text corruption",
65*d8b80295SApple OSS Distributions     T_META_IGNORECRASHES(".*text_corruption_helper.*"),
66*d8b80295SApple OSS Distributions     T_META_ASROOT(true))
67*d8b80295SApple OSS Distributions {
68*d8b80295SApple OSS Distributions 	int ret;
69*d8b80295SApple OSS Distributions 
70*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper foo");
71*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "First call of foo");
72*d8b80295SApple OSS Distributions 
73*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper Xfoo");
74*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, SIGILL, "Call of corrupted foo");
75*d8b80295SApple OSS Distributions 
76*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper foo");
77*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "Fixed call of foo");
78*d8b80295SApple OSS Distributions 
79*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper atan");
80*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "First call of atan");
81*d8b80295SApple OSS Distributions 
82*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper Xatan");
83*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, SIGILL, "Call of corrupted atan");
84*d8b80295SApple OSS Distributions 
85*d8b80295SApple OSS Distributions 	ret = my_system("./text_corruption_helper atan");
86*d8b80295SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(ret, 0, "Fixed call of atan");
87*d8b80295SApple OSS Distributions }
88