1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // A binary that exits via different modes: crashes, causes violation, exits
16 // normally or times out, to test the stack tracing symbolizer.
17
18 #include <syscall.h>
19 #include <unistd.h>
20
21 #include <cstdlib>
22
23 #include "absl/base/attributes.h"
24 #include "absl/strings/numbers.h"
25 #include "sandboxed_api/sandbox2/testcases/symbolize_lib.h"
26 #include "sandboxed_api/util/raw_logging.h"
27
28 // Sometimes we don't have debug info to properly unwind through libc (a frame
29 // is skipped).
30 // Workaround by putting another frame on the call stack.
31 template <typename F>
IndirectLibcCall(F func)32 ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL void IndirectLibcCall(
33 F func) {
34 func();
35 }
36
37 ABSL_ATTRIBUTE_NOINLINE
CrashMe(char x=0)38 void CrashMe(char x = 0) {
39 volatile char* null = nullptr;
40 *null = x;
41 }
42
43 ABSL_ATTRIBUTE_NOINLINE
44 ABSL_ATTRIBUTE_NO_TAIL_CALL
ViolatePolicy(int x=0)45 void ViolatePolicy(int x = 0) {
46 IndirectLibcCall([x]() { syscall(__NR_ptrace, x); });
47 }
48
49 ABSL_ATTRIBUTE_NOINLINE
50 ABSL_ATTRIBUTE_NO_TAIL_CALL
ExitNormally(int x=0)51 void ExitNormally(int x = 0) {
52 IndirectLibcCall([x]() {
53 // _exit is marked noreturn, which makes stack traces a bit trickier -
54 // work around by using a volatile read
55 if (volatile int y = 1) {
56 _exit(x);
57 }
58 });
59 }
60
61 ABSL_ATTRIBUTE_NOINLINE
62 ABSL_ATTRIBUTE_NO_TAIL_CALL
SleepForXSeconds(int x=0)63 void SleepForXSeconds(int x = 0) {
64 IndirectLibcCall([x]() { sleep(x); });
65 }
66
67 ABSL_ATTRIBUTE_NOINLINE
68 ABSL_ATTRIBUTE_NO_TAIL_CALL
RunTest(int testno)69 void RunTest(int testno) {
70 switch (testno) {
71 case 1:
72 CrashMe();
73 break;
74 case 2:
75 ViolatePolicy();
76 break;
77 case 3:
78 ExitNormally();
79 break;
80 case 4:
81 SleepForXSeconds(10);
82 break;
83 default:
84 SAPI_RAW_LOG(FATAL, "Unknown test case: %d", testno);
85 }
86 }
87
88 ABSL_ATTRIBUTE_NOINLINE
89 ABSL_ATTRIBUTE_NO_TAIL_CALL
90 void RecurseA(int testno, int n);
91
92 ABSL_ATTRIBUTE_NOINLINE
93 ABSL_ATTRIBUTE_NO_TAIL_CALL
RecurseB(int testno,int n)94 void RecurseB(int testno, int n) {
95 if (n > 1) {
96 return RecurseA(testno, n - 1);
97 }
98 return RunTest(testno);
99 }
100
RecurseA(int testno,int n)101 void RecurseA(int testno, int n) {
102 if (n > 1) {
103 return RecurseB(testno, n - 1);
104 }
105 return RunTest(testno);
106 }
107
main(int argc,char * argv[])108 int main(int argc, char* argv[]) {
109 SAPI_RAW_CHECK(argc >= 3, "Not enough arguments");
110 int testno;
111 int testmode;
112 SAPI_RAW_CHECK(absl::SimpleAtoi(argv[1], &testno), "testno not a number");
113 SAPI_RAW_CHECK(absl::SimpleAtoi(argv[2], &testmode), "testmode not a number");
114 switch (testmode) {
115 case 1:
116 RunTest(testno);
117 break;
118 case 2:
119 RecurseA(testno, 10);
120 break;
121 case 3:
122 LibRecurse(&RunTest, testno, 10);
123 break;
124 default:
125 SAPI_RAW_LOG(FATAL, "Unknown test mode: %d", testmode);
126 }
127 return EXIT_SUCCESS;
128 }
129