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