blob: 472d1cfa44aff3375fbe50510815ab1b65174032 [file] [log] [blame]
Christopher Ferris723cf9b2017-01-19 20:08:48 -08001/*
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
Christopher Ferrisd226a512017-07-14 10:37:19 -070021#include <unwindstack/Elf.h>
22#include <unwindstack/ElfInterface.h>
23#include <unwindstack/MapInfo.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080024#include <unwindstack/RegsArm.h>
25#include <unwindstack/RegsArm64.h>
26#include <unwindstack/RegsX86.h>
27#include <unwindstack/RegsX86_64.h>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010028#include <unwindstack/RegsMips.h>
29#include <unwindstack/RegsMips64.h>
Christopher Ferris723cf9b2017-01-19 20:08:48 -080030
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070031#include "ElfFake.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080032#include "MemoryFake.h"
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070033#include "RegsFake.h"
Christopher Ferris723cf9b2017-01-19 20:08:48 -080034
Christopher Ferrisd226a512017-07-14 10:37:19 -070035namespace unwindstack {
36
Christopher Ferris3958f802017-02-01 15:44:40 -080037class RegsTest : public ::testing::Test {
38 protected:
39 void SetUp() override {
40 memory_ = new MemoryFake;
41 elf_.reset(new ElfFake(memory_));
42 elf_interface_ = new ElfInterfaceFake(elf_->memory());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070043 elf_->FakeSetInterface(elf_interface_);
Christopher Ferris723cf9b2017-01-19 20:08:48 -080044 }
45
Christopher Ferris3958f802017-02-01 15:44:40 -080046 ElfInterfaceFake* elf_interface_;
47 MemoryFake* memory_;
48 std::unique_ptr<ElfFake> elf_;
49};
50
51TEST_F(RegsTest, regs32) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070052 RegsImplFake<uint32_t> regs32(50);
Christopher Ferris3958f802017-02-01 15:44:40 -080053 ASSERT_EQ(50U, regs32.total_regs());
Christopher Ferris3958f802017-02-01 15:44:40 -080054
55 uint32_t* raw = reinterpret_cast<uint32_t*>(regs32.RawData());
56 for (size_t i = 0; i < 50; i++) {
57 raw[i] = 0xf0000000 + i;
58 }
59 regs32.set_pc(0xf0120340);
60 regs32.set_sp(0xa0ab0cd0);
61
62 for (size_t i = 0; i < 50; i++) {
63 ASSERT_EQ(0xf0000000U + i, regs32[i]) << "Failed comparing register " << i;
64 }
65
66 ASSERT_EQ(0xf0120340U, regs32.pc());
67 ASSERT_EQ(0xa0ab0cd0U, regs32.sp());
68
69 regs32[32] = 10;
70 ASSERT_EQ(10U, regs32[32]);
Christopher Ferris723cf9b2017-01-19 20:08:48 -080071}
72
73TEST_F(RegsTest, regs64) {
Yabin Cui11e96fe2018-03-14 18:16:22 -070074 RegsImplFake<uint64_t> regs64(30);
Christopher Ferris723cf9b2017-01-19 20:08:48 -080075 ASSERT_EQ(30U, regs64.total_regs());
76
Christopher Ferris3958f802017-02-01 15:44:40 -080077 uint64_t* raw = reinterpret_cast<uint64_t*>(regs64.RawData());
Christopher Ferris723cf9b2017-01-19 20:08:48 -080078 for (size_t i = 0; i < 30; i++) {
79 raw[i] = 0xf123456780000000UL + i;
80 }
Christopher Ferris3958f802017-02-01 15:44:40 -080081 regs64.set_pc(0xf123456780102030UL);
82 regs64.set_sp(0xa123456780a0b0c0UL);
Christopher Ferris723cf9b2017-01-19 20:08:48 -080083
Christopher Ferris3958f802017-02-01 15:44:40 -080084 for (size_t i = 0; i < 30; i++) {
85 ASSERT_EQ(0xf123456780000000U + i, regs64[i]) << "Failed reading register " << i;
86 }
Christopher Ferris723cf9b2017-01-19 20:08:48 -080087
Christopher Ferris3958f802017-02-01 15:44:40 -080088 ASSERT_EQ(0xf123456780102030UL, regs64.pc());
89 ASSERT_EQ(0xa123456780a0b0c0UL, regs64.sp());
90
Christopher Ferris723cf9b2017-01-19 20:08:48 -080091 regs64[8] = 10;
92 ASSERT_EQ(10U, regs64[8]);
Christopher Ferris3958f802017-02-01 15:44:40 -080093}
Christopher Ferris723cf9b2017-01-19 20:08:48 -080094
Christopher Ferris3958f802017-02-01 15:44:40 -080095TEST_F(RegsTest, rel_pc) {
96 RegsArm64 arm64;
Christopher Ferris6dbc28e2018-03-28 15:12:49 -070097 EXPECT_EQ(4U, arm64.GetPcAdjustment(0x10, elf_.get()));
98 EXPECT_EQ(4U, arm64.GetPcAdjustment(0x4, elf_.get()));
99 EXPECT_EQ(0U, arm64.GetPcAdjustment(0x3, elf_.get()));
100 EXPECT_EQ(0U, arm64.GetPcAdjustment(0x2, elf_.get()));
101 EXPECT_EQ(0U, arm64.GetPcAdjustment(0x1, elf_.get()));
102 EXPECT_EQ(0U, arm64.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800103
104 RegsX86 x86;
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700105 EXPECT_EQ(1U, x86.GetPcAdjustment(0x100, elf_.get()));
106 EXPECT_EQ(1U, x86.GetPcAdjustment(0x2, elf_.get()));
107 EXPECT_EQ(1U, x86.GetPcAdjustment(0x1, elf_.get()));
108 EXPECT_EQ(0U, x86.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800109
110 RegsX86_64 x86_64;
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700111 EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x100, elf_.get()));
112 EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x2, elf_.get()));
113 EXPECT_EQ(1U, x86_64.GetPcAdjustment(0x1, elf_.get()));
114 EXPECT_EQ(0U, x86_64.GetPcAdjustment(0x0, elf_.get()));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100115
116 RegsMips mips;
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700117 EXPECT_EQ(8U, mips.GetPcAdjustment(0x10, elf_.get()));
118 EXPECT_EQ(8U, mips.GetPcAdjustment(0x8, elf_.get()));
119 EXPECT_EQ(0U, mips.GetPcAdjustment(0x7, elf_.get()));
120 EXPECT_EQ(0U, mips.GetPcAdjustment(0x6, elf_.get()));
121 EXPECT_EQ(0U, mips.GetPcAdjustment(0x5, elf_.get()));
122 EXPECT_EQ(0U, mips.GetPcAdjustment(0x4, elf_.get()));
123 EXPECT_EQ(0U, mips.GetPcAdjustment(0x3, elf_.get()));
124 EXPECT_EQ(0U, mips.GetPcAdjustment(0x2, elf_.get()));
125 EXPECT_EQ(0U, mips.GetPcAdjustment(0x1, elf_.get()));
126 EXPECT_EQ(0U, mips.GetPcAdjustment(0x0, elf_.get()));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100127
128 RegsMips64 mips64;
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700129 EXPECT_EQ(8U, mips64.GetPcAdjustment(0x10, elf_.get()));
130 EXPECT_EQ(8U, mips64.GetPcAdjustment(0x8, elf_.get()));
131 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x7, elf_.get()));
132 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x6, elf_.get()));
133 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x5, elf_.get()));
134 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x4, elf_.get()));
135 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x3, elf_.get()));
136 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x2, elf_.get()));
137 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x1, elf_.get()));
138 EXPECT_EQ(0U, mips64.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800139}
140
141TEST_F(RegsTest, rel_pc_arm) {
142 RegsArm arm;
143
144 // Check fence posts.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700145 elf_->FakeSetLoadBias(0);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700146 EXPECT_EQ(2U, arm.GetPcAdjustment(0x5, elf_.get()));
147 EXPECT_EQ(2U, arm.GetPcAdjustment(0x4, elf_.get()));
148 EXPECT_EQ(2U, arm.GetPcAdjustment(0x3, elf_.get()));
149 EXPECT_EQ(2U, arm.GetPcAdjustment(0x2, elf_.get()));
150 EXPECT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
151 EXPECT_EQ(0U, arm.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800152
Christopher Ferrise69f4702017-10-19 16:08:58 -0700153 elf_->FakeSetLoadBias(0x100);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700154 EXPECT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
155 EXPECT_EQ(2U, arm.GetPcAdjustment(0x2, elf_.get()));
156 EXPECT_EQ(2U, arm.GetPcAdjustment(0xff, elf_.get()));
157 EXPECT_EQ(2U, arm.GetPcAdjustment(0x105, elf_.get()));
158 EXPECT_EQ(2U, arm.GetPcAdjustment(0x104, elf_.get()));
159 EXPECT_EQ(2U, arm.GetPcAdjustment(0x103, elf_.get()));
160 EXPECT_EQ(2U, arm.GetPcAdjustment(0x102, elf_.get()));
161 EXPECT_EQ(0U, arm.GetPcAdjustment(0x101, elf_.get()));
162 EXPECT_EQ(0U, arm.GetPcAdjustment(0x100, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800163
164 // Check thumb instructions handling.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700165 elf_->FakeSetLoadBias(0);
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 memory_->SetData32(0x2000, 0);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700167 EXPECT_EQ(2U, arm.GetPcAdjustment(0x2005, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800168 memory_->SetData32(0x2000, 0xe000f000);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700169 EXPECT_EQ(4U, arm.GetPcAdjustment(0x2005, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800170
Christopher Ferrise69f4702017-10-19 16:08:58 -0700171 elf_->FakeSetLoadBias(0x400);
Christopher Ferris3958f802017-02-01 15:44:40 -0800172 memory_->SetData32(0x2100, 0);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700173 EXPECT_EQ(2U, arm.GetPcAdjustment(0x2505, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800174 memory_->SetData32(0x2100, 0xf111f111);
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700175 EXPECT_EQ(4U, arm.GetPcAdjustment(0x2505, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800176}
177
178TEST_F(RegsTest, elf_invalid) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800179 RegsArm regs_arm;
180 RegsArm64 regs_arm64;
181 RegsX86 regs_x86;
182 RegsX86_64 regs_x86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100183 RegsMips regs_mips;
184 RegsMips64 regs_mips64;
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800185 MapInfo map_info(nullptr, 0x1000, 0x2000, 0, 0, "");
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700186 Elf* invalid_elf = new Elf(nullptr);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800187 map_info.elf.reset(invalid_elf);
Christopher Ferris3958f802017-02-01 15:44:40 -0800188
189 regs_arm.set_pc(0x1500);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800190 EXPECT_EQ(0x500U, invalid_elf->GetRelPc(regs_arm.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700191 EXPECT_EQ(2U, regs_arm.GetPcAdjustment(0x500U, invalid_elf));
192 EXPECT_EQ(2U, regs_arm.GetPcAdjustment(0x511U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800193
194 regs_arm64.set_pc(0x1600);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800195 EXPECT_EQ(0x600U, invalid_elf->GetRelPc(regs_arm64.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700196 EXPECT_EQ(4U, regs_arm64.GetPcAdjustment(0x600U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800197
198 regs_x86.set_pc(0x1700);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800199 EXPECT_EQ(0x700U, invalid_elf->GetRelPc(regs_x86.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700200 EXPECT_EQ(1U, regs_x86.GetPcAdjustment(0x700U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800201
202 regs_x86_64.set_pc(0x1800);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800203 EXPECT_EQ(0x800U, invalid_elf->GetRelPc(regs_x86_64.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700204 EXPECT_EQ(1U, regs_x86_64.GetPcAdjustment(0x800U, invalid_elf));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100205
206 regs_mips.set_pc(0x1900);
207 EXPECT_EQ(0x900U, invalid_elf->GetRelPc(regs_mips.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700208 EXPECT_EQ(8U, regs_mips.GetPcAdjustment(0x900U, invalid_elf));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100209
210 regs_mips64.set_pc(0x1a00);
211 EXPECT_EQ(0xa00U, invalid_elf->GetRelPc(regs_mips64.pc(), &map_info));
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700212 EXPECT_EQ(8U, regs_mips64.GetPcAdjustment(0xa00U, invalid_elf));
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800213}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700214
Yabin Cui11e96fe2018-03-14 18:16:22 -0700215TEST_F(RegsTest, arm_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700216 RegsArm arm;
217 uint32_t* regs = reinterpret_cast<uint32_t*>(arm.RawData());
218 regs[13] = 0x100;
219 regs[15] = 0x200;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700220 EXPECT_EQ(0x100U, arm.sp());
221 EXPECT_EQ(0x200U, arm.pc());
222}
223
Yabin Cui11e96fe2018-03-14 18:16:22 -0700224TEST_F(RegsTest, arm64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700225 RegsArm64 arm64;
226 uint64_t* regs = reinterpret_cast<uint64_t*>(arm64.RawData());
227 regs[31] = 0xb100000000ULL;
228 regs[32] = 0xc200000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700229 EXPECT_EQ(0xb100000000U, arm64.sp());
230 EXPECT_EQ(0xc200000000U, arm64.pc());
231}
232
Yabin Cui11e96fe2018-03-14 18:16:22 -0700233TEST_F(RegsTest, x86_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700234 RegsX86 x86;
235 uint32_t* regs = reinterpret_cast<uint32_t*>(x86.RawData());
236 regs[4] = 0x23450000;
237 regs[8] = 0xabcd0000;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700238 EXPECT_EQ(0x23450000U, x86.sp());
239 EXPECT_EQ(0xabcd0000U, x86.pc());
240}
241
Yabin Cui11e96fe2018-03-14 18:16:22 -0700242TEST_F(RegsTest, x86_64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700243 RegsX86_64 x86_64;
244 uint64_t* regs = reinterpret_cast<uint64_t*>(x86_64.RawData());
245 regs[7] = 0x1200000000ULL;
246 regs[16] = 0x4900000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700247 EXPECT_EQ(0x1200000000U, x86_64.sp());
248 EXPECT_EQ(0x4900000000U, x86_64.pc());
249}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700250
Yabin Cui11e96fe2018-03-14 18:16:22 -0700251TEST_F(RegsTest, mips_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100252 RegsMips mips;
253 uint32_t* regs = reinterpret_cast<uint32_t*>(mips.RawData());
254 regs[29] = 0x100;
255 regs[32] = 0x200;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100256 EXPECT_EQ(0x100U, mips.sp());
257 EXPECT_EQ(0x200U, mips.pc());
258}
259
Yabin Cui11e96fe2018-03-14 18:16:22 -0700260TEST_F(RegsTest, mips64_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100261 RegsMips64 mips64;
262 uint64_t* regs = reinterpret_cast<uint64_t*>(mips64.RawData());
263 regs[29] = 0xb100000000ULL;
264 regs[32] = 0xc200000000ULL;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100265 EXPECT_EQ(0xb100000000U, mips64.sp());
266 EXPECT_EQ(0xc200000000U, mips64.pc());
267}
268
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800269TEST_F(RegsTest, machine_type) {
270 RegsArm arm_regs;
271 EXPECT_EQ(ARCH_ARM, arm_regs.Arch());
272
273 RegsArm64 arm64_regs;
274 EXPECT_EQ(ARCH_ARM64, arm64_regs.Arch());
275
276 RegsX86 x86_regs;
277 EXPECT_EQ(ARCH_X86, x86_regs.Arch());
278
279 RegsX86_64 x86_64_regs;
280 EXPECT_EQ(ARCH_X86_64, x86_64_regs.Arch());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100281
282 RegsMips mips_regs;
283 EXPECT_EQ(ARCH_MIPS, mips_regs.Arch());
284
285 RegsMips64 mips64_regs;
286 EXPECT_EQ(ARCH_MIPS64, mips64_regs.Arch());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800287}
288
Josh Gao2f37a152018-04-20 11:51:14 -0700289template <typename RegisterType>
290void clone_test(Regs* regs) {
291 RegisterType* register_values = reinterpret_cast<RegisterType*>(regs->RawData());
292 int num_regs = regs->total_regs();
293 for (int i = 0; i < num_regs; ++i) {
294 register_values[i] = i;
295 }
296
297 std::unique_ptr<Regs> clone(regs->Clone());
298 ASSERT_EQ(regs->total_regs(), clone->total_regs());
299 RegisterType* clone_values = reinterpret_cast<RegisterType*>(clone->RawData());
300 for (int i = 0; i < num_regs; ++i) {
301 EXPECT_EQ(register_values[i], clone_values[i]);
302 EXPECT_NE(&register_values[i], &clone_values[i]);
303 }
304}
305
306TEST_F(RegsTest, clone) {
307 std::vector<std::unique_ptr<Regs>> regs;
308 regs.emplace_back(new RegsArm());
309 regs.emplace_back(new RegsArm64());
310 regs.emplace_back(new RegsX86());
311 regs.emplace_back(new RegsX86_64());
312 regs.emplace_back(new RegsMips());
313 regs.emplace_back(new RegsMips64());
314
315 for (auto& r : regs) {
316 if (r->Is32Bit()) {
317 clone_test<uint32_t>(r.get());
318 } else {
319 clone_test<uint64_t>(r.get());
320 }
321 }
322}
323
Christopher Ferrisd226a512017-07-14 10:37:19 -0700324} // namespace unwindstack