blob: 85192d5d812dfbd6f2e94d5d39000fb85571e5f8 [file] [log] [blame]
Christopher Ferriseb4a6db2017-07-19 12:37:45 -07001/*
2 * Copyright (C) 2017 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 <stdint.h>
18
19#include <gtest/gtest.h>
20
21#include <unwindstack/Elf.h>
22#include <unwindstack/Regs.h>
23
24#include "Machine.h"
25
26#include "MemoryFake.h"
27
28namespace unwindstack {
29
30class RegsStepIfSignalHandlerTest : public ::testing::Test {
31 protected:
32 void SetUp() override {
33 elf_memory_ = new MemoryFake;
34 elf_.reset(new Elf(elf_memory_));
35 }
36
37 void ArmStepIfSignalHandlerNonRt(uint32_t pc_data);
38 void ArmStepIfSignalHandlerRt(uint32_t pc_data);
39
40 MemoryFake* elf_memory_;
41 MemoryFake process_memory_;
42 std::unique_ptr<Elf> elf_;
43};
44
45void RegsStepIfSignalHandlerTest::ArmStepIfSignalHandlerNonRt(uint32_t pc_data) {
46 uint64_t addr = 0x1000;
47 RegsArm regs;
48 regs[ARM_REG_PC] = 0x5000;
49 regs[ARM_REG_SP] = addr;
50 regs.SetFromRaw();
51
52 elf_memory_->SetData32(0x5000, pc_data);
53
54 for (uint64_t index = 0; index <= 30; index++) {
55 process_memory_.SetData32(addr + index * 4, index * 0x10);
56 }
57
58 ASSERT_TRUE(regs.StepIfSignalHandler(0x5000, elf_.get(), &process_memory_));
59 EXPECT_EQ(0x100U, regs[ARM_REG_SP]);
60 EXPECT_EQ(0x120U, regs[ARM_REG_PC]);
61 EXPECT_EQ(0x100U, regs.sp());
62 EXPECT_EQ(0x120U, regs.pc());
63}
64
65TEST_F(RegsStepIfSignalHandlerTest, arm_step_if_signal_handler_non_rt) {
66 // Form 1
67 ArmStepIfSignalHandlerNonRt(0xe3a07077);
68
69 // Form 2
70 ArmStepIfSignalHandlerNonRt(0xef900077);
71
72 // Form 3
73 ArmStepIfSignalHandlerNonRt(0xdf002777);
74}
75
76void RegsStepIfSignalHandlerTest::ArmStepIfSignalHandlerRt(uint32_t pc_data) {
77 uint64_t addr = 0x1000;
78 RegsArm regs;
79 regs[ARM_REG_PC] = 0x5000;
80 regs[ARM_REG_SP] = addr;
81 regs.SetFromRaw();
82
83 elf_memory_->SetData32(0x5000, pc_data);
84
85 for (uint64_t index = 0; index <= 100; index++) {
86 process_memory_.SetData32(addr + index * 4, index * 0x10);
87 }
88
89 ASSERT_TRUE(regs.StepIfSignalHandler(0x5000, elf_.get(), &process_memory_));
90 EXPECT_EQ(0x350U, regs[ARM_REG_SP]);
91 EXPECT_EQ(0x370U, regs[ARM_REG_PC]);
92 EXPECT_EQ(0x350U, regs.sp());
93 EXPECT_EQ(0x370U, regs.pc());
94}
95
96TEST_F(RegsStepIfSignalHandlerTest, arm_step_if_signal_handler_rt) {
97 // Form 1
98 ArmStepIfSignalHandlerRt(0xe3a070ad);
99
100 // Form 2
101 ArmStepIfSignalHandlerRt(0xef9000ad);
102
103 // Form 3
104 ArmStepIfSignalHandlerRt(0xdf0027ad);
105}
106
107TEST_F(RegsStepIfSignalHandlerTest, arm64_step_if_signal_handler) {
108 uint64_t addr = 0x1000;
109 RegsArm64 regs;
110 regs[ARM64_REG_PC] = 0x8000;
111 regs[ARM64_REG_SP] = addr;
112 regs.SetFromRaw();
113
114 elf_memory_->SetData64(0x8000, 0xd4000001d2801168ULL);
115
116 for (uint64_t index = 0; index <= 100; index++) {
117 process_memory_.SetData64(addr + index * 8, index * 0x10);
118 }
119
120 ASSERT_TRUE(regs.StepIfSignalHandler(0x8000, elf_.get(), &process_memory_));
121 EXPECT_EQ(0x460U, regs[ARM64_REG_SP]);
122 EXPECT_EQ(0x470U, regs[ARM64_REG_PC]);
123 EXPECT_EQ(0x460U, regs.sp());
124 EXPECT_EQ(0x470U, regs.pc());
125}
126
127TEST_F(RegsStepIfSignalHandlerTest, x86_step_if_signal_handler_no_siginfo) {
128 uint64_t addr = 0xa00;
129 RegsX86 regs;
130 regs[X86_REG_EIP] = 0x4100;
131 regs[X86_REG_ESP] = addr;
132 regs.SetFromRaw();
133
134 elf_memory_->SetData64(0x4100, 0x80cd00000077b858ULL);
135 for (uint64_t index = 0; index <= 25; index++) {
136 process_memory_.SetData32(addr + index * 4, index * 0x10);
137 }
138
139 ASSERT_TRUE(regs.StepIfSignalHandler(0x4100, elf_.get(), &process_memory_));
140 EXPECT_EQ(0x70U, regs[X86_REG_EBP]);
141 EXPECT_EQ(0x80U, regs[X86_REG_ESP]);
142 EXPECT_EQ(0x90U, regs[X86_REG_EBX]);
143 EXPECT_EQ(0xa0U, regs[X86_REG_EDX]);
144 EXPECT_EQ(0xb0U, regs[X86_REG_ECX]);
145 EXPECT_EQ(0xc0U, regs[X86_REG_EAX]);
146 EXPECT_EQ(0xf0U, regs[X86_REG_EIP]);
147 EXPECT_EQ(0x80U, regs.sp());
148 EXPECT_EQ(0xf0U, regs.pc());
149}
150
151TEST_F(RegsStepIfSignalHandlerTest, x86_step_if_signal_handler_siginfo) {
152 uint64_t addr = 0xa00;
153 RegsX86 regs;
154 regs[X86_REG_EIP] = 0x4100;
155 regs[X86_REG_ESP] = addr;
156 regs.SetFromRaw();
157
158 elf_memory_->SetData64(0x4100, 0x0080cd000000adb8ULL);
159 addr += 8;
160 // Pointer to ucontext data.
161 process_memory_.SetData32(addr, 0x8100);
162
163 addr = 0x8100;
164 for (uint64_t index = 0; index <= 30; index++) {
165 process_memory_.SetData32(addr + index * 4, index * 0x10);
166 }
167
168 ASSERT_TRUE(regs.StepIfSignalHandler(0x4100, elf_.get(), &process_memory_));
169 EXPECT_EQ(0xb0U, regs[X86_REG_EBP]);
170 EXPECT_EQ(0xc0U, regs[X86_REG_ESP]);
171 EXPECT_EQ(0xd0U, regs[X86_REG_EBX]);
172 EXPECT_EQ(0xe0U, regs[X86_REG_EDX]);
173 EXPECT_EQ(0xf0U, regs[X86_REG_ECX]);
174 EXPECT_EQ(0x100U, regs[X86_REG_EAX]);
175 EXPECT_EQ(0x130U, regs[X86_REG_EIP]);
176 EXPECT_EQ(0xc0U, regs.sp());
177 EXPECT_EQ(0x130U, regs.pc());
178}
179
180TEST_F(RegsStepIfSignalHandlerTest, x86_64_step_if_signal_handler) {
181 uint64_t addr = 0x500;
182 RegsX86_64 regs;
183 regs[X86_64_REG_RIP] = 0x7000;
184 regs[X86_64_REG_RSP] = addr;
185 regs.SetFromRaw();
186
187 elf_memory_->SetData64(0x7000, 0x0f0000000fc0c748);
188 elf_memory_->SetData16(0x7008, 0x0f05);
189
190 for (uint64_t index = 0; index <= 30; index++) {
191 process_memory_.SetData64(addr + index * 8, index * 0x10);
192 }
193
194 ASSERT_TRUE(regs.StepIfSignalHandler(0x7000, elf_.get(), &process_memory_));
195 EXPECT_EQ(0x140U, regs[X86_64_REG_RSP]);
196 EXPECT_EQ(0x150U, regs[X86_64_REG_RIP]);
197 EXPECT_EQ(0x140U, regs.sp());
198 EXPECT_EQ(0x150U, regs.pc());
199}
200
201} // namespace unwindstack