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