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