1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <signal.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include <memory>
22 #include <vector>
23
24 #include <unwindstack/Regs.h>
25 #include <unwindstack/RegsGetLocal.h>
26
27 #include "backtrace_testlib.h"
28
test_loop_forever()29 void test_loop_forever() {
30 while (1)
31 ;
32 }
33
test_signal_handler(int)34 void test_signal_handler(int) { test_loop_forever(); }
35
test_signal_action(int,siginfo_t *,void *)36 void test_signal_action(int, siginfo_t*, void*) { test_loop_forever(); }
37
test_level_four(int one,int two,int three,int four,void (* callback_func)(void *),void * data)38 int test_level_four(int one, int two, int three, int four, void (*callback_func)(void*),
39 void* data) {
40 if (callback_func != NULL) {
41 callback_func(data);
42 } else {
43 while (1)
44 ;
45 }
46 return one + two + three + four;
47 }
48
test_level_three(int one,int two,int three,int four,void (* callback_func)(void *),void * data)49 int test_level_three(int one, int two, int three, int four, void (*callback_func)(void*),
50 void* data) {
51 return test_level_four(one + 3, two + 6, three + 9, four + 12, callback_func, data) + 3;
52 }
53
test_level_two(int one,int two,int three,int four,void (* callback_func)(void *),void * data)54 int test_level_two(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
55 return test_level_three(one + 2, two + 4, three + 6, four + 8, callback_func, data) + 2;
56 }
57
test_level_one(int one,int two,int three,int four,void (* callback_func)(void *),void * data)58 int test_level_one(int one, int two, int three, int four, void (*callback_func)(void*), void* data) {
59 return test_level_two(one + 1, two + 2, three + 3, four + 4, callback_func, data) + 1;
60 }
61
test_recursive_call(int level,void (* callback_func)(void *),void * data)62 int test_recursive_call(int level, void (*callback_func)(void*), void* data) {
63 if (level > 0) {
64 return test_recursive_call(level - 1, callback_func, data) + level;
65 } else if (callback_func != NULL) {
66 callback_func(data);
67 } else {
68 while (1) {
69 }
70 }
71 return 0;
72 }
73
74 typedef struct {
75 std::vector<uint8_t>* ucontext;
76 volatile int* exit_flag;
77 } GetContextArg;
78
GetContextAndExit(void * data)79 static void GetContextAndExit(void* data) {
80 GetContextArg* arg = reinterpret_cast<GetContextArg*>(data);
81
82 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
83 unwindstack::RegsGetLocal(regs.get());
84
85 ucontext_t ucontext;
86 memset(&ucontext, 0, sizeof(ucontext));
87 #if defined(__arm__)
88 memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint32_t) * 16);
89 #elif defined(__aarch64__)
90 memcpy(&ucontext.uc_mcontext, regs->RawData(), sizeof(uint64_t) * 33);
91 #elif defined(__i386__)
92 uint32_t* reg_data = reinterpret_cast<uint32_t*>(regs->RawData());
93 ucontext.uc_mcontext.gregs[0] = reg_data[15];
94 ucontext.uc_mcontext.gregs[1] = reg_data[14];
95 ucontext.uc_mcontext.gregs[2] = reg_data[13];
96 ucontext.uc_mcontext.gregs[3] = reg_data[12];
97 ucontext.uc_mcontext.gregs[4] = reg_data[7];
98 ucontext.uc_mcontext.gregs[5] = reg_data[6];
99 ucontext.uc_mcontext.gregs[6] = reg_data[5];
100 ucontext.uc_mcontext.gregs[7] = reg_data[4];
101 ucontext.uc_mcontext.gregs[8] = reg_data[3];
102 ucontext.uc_mcontext.gregs[9] = reg_data[2];
103 ucontext.uc_mcontext.gregs[10] = reg_data[1];
104 ucontext.uc_mcontext.gregs[11] = reg_data[0];
105 ucontext.uc_mcontext.gregs[14] = reg_data[8];
106 ucontext.uc_mcontext.gregs[15] = reg_data[10];
107 #elif defined(__x86_64__)
108 uint64_t* reg_data = reinterpret_cast<uint64_t*>(regs->RawData());
109 ucontext.uc_mcontext.gregs[0] = reg_data[8];
110 ucontext.uc_mcontext.gregs[1] = reg_data[9];
111 ucontext.uc_mcontext.gregs[2] = reg_data[10];
112 ucontext.uc_mcontext.gregs[3] = reg_data[11];
113 ucontext.uc_mcontext.gregs[4] = reg_data[12];
114 ucontext.uc_mcontext.gregs[5] = reg_data[13];
115 ucontext.uc_mcontext.gregs[6] = reg_data[14];
116 ucontext.uc_mcontext.gregs[7] = reg_data[15];
117 ucontext.uc_mcontext.gregs[8] = reg_data[5];
118 ucontext.uc_mcontext.gregs[9] = reg_data[4];
119 ucontext.uc_mcontext.gregs[10] = reg_data[6];
120 ucontext.uc_mcontext.gregs[11] = reg_data[3];
121 ucontext.uc_mcontext.gregs[12] = reg_data[1];
122 ucontext.uc_mcontext.gregs[13] = reg_data[0];
123 ucontext.uc_mcontext.gregs[14] = reg_data[2];
124 ucontext.uc_mcontext.gregs[15] = reg_data[7];
125 ucontext.uc_mcontext.gregs[16] = reg_data[16];
126 #endif
127
128 arg->ucontext->resize(sizeof(ucontext));
129 memcpy(arg->ucontext->data(), &ucontext, sizeof(ucontext));
130
131 // Don't touch the stack anymore.
132 while (*arg->exit_flag == 0) {
133 }
134 }
135
test_get_context_and_wait(void * ucontext,volatile int * exit_flag)136 void test_get_context_and_wait(void* ucontext, volatile int* exit_flag) {
137 GetContextArg arg;
138 arg.ucontext = reinterpret_cast<std::vector<uint8_t>*>(ucontext);
139 arg.exit_flag = exit_flag;
140 test_level_one(1, 2, 3, 4, GetContextAndExit, &arg);
141 }
142