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