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