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