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