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