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