blob: 3e80733bf459cd1d95f0939b321e62c713347034 [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 Ferrisa2ec50b2018-02-21 15:39:07 -080097 ASSERT_EQ(4U, arm64.GetPcAdjustment(0x10, elf_.get()));
98 ASSERT_EQ(4U, arm64.GetPcAdjustment(0x4, elf_.get()));
99 ASSERT_EQ(0U, arm64.GetPcAdjustment(0x3, elf_.get()));
100 ASSERT_EQ(0U, arm64.GetPcAdjustment(0x2, elf_.get()));
101 ASSERT_EQ(0U, arm64.GetPcAdjustment(0x1, elf_.get()));
102 ASSERT_EQ(0U, arm64.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800103
104 RegsX86 x86;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800105 ASSERT_EQ(1U, x86.GetPcAdjustment(0x100, elf_.get()));
106 ASSERT_EQ(1U, x86.GetPcAdjustment(0x2, elf_.get()));
107 ASSERT_EQ(1U, x86.GetPcAdjustment(0x1, elf_.get()));
108 ASSERT_EQ(0U, x86.GetPcAdjustment(0x0, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800109
110 RegsX86_64 x86_64;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800111 ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x100, elf_.get()));
112 ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x2, elf_.get()));
113 ASSERT_EQ(1U, x86_64.GetPcAdjustment(0x1, elf_.get()));
114 ASSERT_EQ(0U, x86_64.GetPcAdjustment(0x0, elf_.get()));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100115
116 RegsMips mips;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800117 ASSERT_EQ(8U, mips.GetPcAdjustment(0x10, elf_.get()));
118 ASSERT_EQ(8U, mips.GetPcAdjustment(0x8, elf_.get()));
119 ASSERT_EQ(0U, mips.GetPcAdjustment(0x7, elf_.get()));
120 ASSERT_EQ(0U, mips.GetPcAdjustment(0x6, elf_.get()));
121 ASSERT_EQ(0U, mips.GetPcAdjustment(0x5, elf_.get()));
122 ASSERT_EQ(0U, mips.GetPcAdjustment(0x4, elf_.get()));
123 ASSERT_EQ(0U, mips.GetPcAdjustment(0x3, elf_.get()));
124 ASSERT_EQ(0U, mips.GetPcAdjustment(0x2, elf_.get()));
125 ASSERT_EQ(0U, mips.GetPcAdjustment(0x1, elf_.get()));
126 ASSERT_EQ(0U, mips.GetPcAdjustment(0x0, elf_.get()));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100127
128 RegsMips64 mips64;
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800129 ASSERT_EQ(8U, mips64.GetPcAdjustment(0x10, elf_.get()));
130 ASSERT_EQ(8U, mips64.GetPcAdjustment(0x8, elf_.get()));
131 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x7, elf_.get()));
132 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x6, elf_.get()));
133 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x5, elf_.get()));
134 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x4, elf_.get()));
135 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x3, elf_.get()));
136 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x2, elf_.get()));
137 ASSERT_EQ(0U, mips64.GetPcAdjustment(0x1, elf_.get()));
138 ASSERT_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 Ferrisa2ec50b2018-02-21 15:39:07 -0800146 ASSERT_EQ(2U, arm.GetPcAdjustment(0x5, elf_.get()));
147 ASSERT_EQ(0U, arm.GetPcAdjustment(0x4, elf_.get()));
148 ASSERT_EQ(0U, arm.GetPcAdjustment(0x3, elf_.get()));
149 ASSERT_EQ(0U, arm.GetPcAdjustment(0x2, elf_.get()));
150 ASSERT_EQ(0U, arm.GetPcAdjustment(0x1, elf_.get()));
151 ASSERT_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 Ferrisa2ec50b2018-02-21 15:39:07 -0800154 ASSERT_EQ(0U, arm.GetPcAdjustment(0xff, elf_.get()));
155 ASSERT_EQ(2U, arm.GetPcAdjustment(0x105, elf_.get()));
156 ASSERT_EQ(0U, arm.GetPcAdjustment(0x104, elf_.get()));
157 ASSERT_EQ(0U, arm.GetPcAdjustment(0x103, elf_.get()));
158 ASSERT_EQ(0U, arm.GetPcAdjustment(0x102, elf_.get()));
159 ASSERT_EQ(0U, arm.GetPcAdjustment(0x101, elf_.get()));
160 ASSERT_EQ(0U, arm.GetPcAdjustment(0x100, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800161
162 // Check thumb instructions handling.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700163 elf_->FakeSetLoadBias(0);
Christopher Ferris3958f802017-02-01 15:44:40 -0800164 memory_->SetData32(0x2000, 0);
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800165 ASSERT_EQ(2U, arm.GetPcAdjustment(0x2005, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800166 memory_->SetData32(0x2000, 0xe000f000);
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800167 ASSERT_EQ(4U, arm.GetPcAdjustment(0x2005, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800168
Christopher Ferrise69f4702017-10-19 16:08:58 -0700169 elf_->FakeSetLoadBias(0x400);
Christopher Ferris3958f802017-02-01 15:44:40 -0800170 memory_->SetData32(0x2100, 0);
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800171 ASSERT_EQ(2U, arm.GetPcAdjustment(0x2505, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800172 memory_->SetData32(0x2100, 0xf111f111);
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800173 ASSERT_EQ(4U, arm.GetPcAdjustment(0x2505, elf_.get()));
Christopher Ferris3958f802017-02-01 15:44:40 -0800174}
175
176TEST_F(RegsTest, elf_invalid) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800177 RegsArm regs_arm;
178 RegsArm64 regs_arm64;
179 RegsX86 regs_x86;
180 RegsX86_64 regs_x86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100181 RegsMips regs_mips;
182 RegsMips64 regs_mips64;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800183 MapInfo map_info(0x1000, 0x2000);
184 Elf* invalid_elf = new Elf(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800185 map_info.elf.reset(invalid_elf);
Christopher Ferris3958f802017-02-01 15:44:40 -0800186
187 regs_arm.set_pc(0x1500);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800188 EXPECT_EQ(0x500U, invalid_elf->GetRelPc(regs_arm.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800189 EXPECT_EQ(4U, regs_arm.GetPcAdjustment(0x500U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800190
191 regs_arm64.set_pc(0x1600);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800192 EXPECT_EQ(0x600U, invalid_elf->GetRelPc(regs_arm64.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800193 EXPECT_EQ(0U, regs_arm64.GetPcAdjustment(0x600U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800194
195 regs_x86.set_pc(0x1700);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800196 EXPECT_EQ(0x700U, invalid_elf->GetRelPc(regs_x86.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800197 EXPECT_EQ(0U, regs_x86.GetPcAdjustment(0x700U, invalid_elf));
Christopher Ferris3958f802017-02-01 15:44:40 -0800198
199 regs_x86_64.set_pc(0x1800);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800200 EXPECT_EQ(0x800U, invalid_elf->GetRelPc(regs_x86_64.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800201 EXPECT_EQ(0U, regs_x86_64.GetPcAdjustment(0x800U, invalid_elf));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100202
203 regs_mips.set_pc(0x1900);
204 EXPECT_EQ(0x900U, invalid_elf->GetRelPc(regs_mips.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800205 EXPECT_EQ(0U, regs_mips.GetPcAdjustment(0x900U, invalid_elf));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100206
207 regs_mips64.set_pc(0x1a00);
208 EXPECT_EQ(0xa00U, invalid_elf->GetRelPc(regs_mips64.pc(), &map_info));
Christopher Ferrisa2ec50b2018-02-21 15:39:07 -0800209 EXPECT_EQ(0U, regs_mips64.GetPcAdjustment(0xa00U, invalid_elf));
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800210}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700211
Yabin Cui11e96fe2018-03-14 18:16:22 -0700212TEST_F(RegsTest, arm_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700213 RegsArm arm;
214 uint32_t* regs = reinterpret_cast<uint32_t*>(arm.RawData());
215 regs[13] = 0x100;
216 regs[15] = 0x200;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700217 EXPECT_EQ(0x100U, arm.sp());
218 EXPECT_EQ(0x200U, arm.pc());
219}
220
Yabin Cui11e96fe2018-03-14 18:16:22 -0700221TEST_F(RegsTest, arm64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700222 RegsArm64 arm64;
223 uint64_t* regs = reinterpret_cast<uint64_t*>(arm64.RawData());
224 regs[31] = 0xb100000000ULL;
225 regs[32] = 0xc200000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700226 EXPECT_EQ(0xb100000000U, arm64.sp());
227 EXPECT_EQ(0xc200000000U, arm64.pc());
228}
229
Yabin Cui11e96fe2018-03-14 18:16:22 -0700230TEST_F(RegsTest, x86_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700231 RegsX86 x86;
232 uint32_t* regs = reinterpret_cast<uint32_t*>(x86.RawData());
233 regs[4] = 0x23450000;
234 regs[8] = 0xabcd0000;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700235 EXPECT_EQ(0x23450000U, x86.sp());
236 EXPECT_EQ(0xabcd0000U, x86.pc());
237}
238
Yabin Cui11e96fe2018-03-14 18:16:22 -0700239TEST_F(RegsTest, x86_64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700240 RegsX86_64 x86_64;
241 uint64_t* regs = reinterpret_cast<uint64_t*>(x86_64.RawData());
242 regs[7] = 0x1200000000ULL;
243 regs[16] = 0x4900000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700244 EXPECT_EQ(0x1200000000U, x86_64.sp());
245 EXPECT_EQ(0x4900000000U, x86_64.pc());
246}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700247
Yabin Cui11e96fe2018-03-14 18:16:22 -0700248TEST_F(RegsTest, mips_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100249 RegsMips mips;
250 uint32_t* regs = reinterpret_cast<uint32_t*>(mips.RawData());
251 regs[29] = 0x100;
252 regs[32] = 0x200;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100253 EXPECT_EQ(0x100U, mips.sp());
254 EXPECT_EQ(0x200U, mips.pc());
255}
256
Yabin Cui11e96fe2018-03-14 18:16:22 -0700257TEST_F(RegsTest, mips64_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100258 RegsMips64 mips64;
259 uint64_t* regs = reinterpret_cast<uint64_t*>(mips64.RawData());
260 regs[29] = 0xb100000000ULL;
261 regs[32] = 0xc200000000ULL;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100262 EXPECT_EQ(0xb100000000U, mips64.sp());
263 EXPECT_EQ(0xc200000000U, mips64.pc());
264}
265
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800266TEST_F(RegsTest, machine_type) {
267 RegsArm arm_regs;
268 EXPECT_EQ(ARCH_ARM, arm_regs.Arch());
269
270 RegsArm64 arm64_regs;
271 EXPECT_EQ(ARCH_ARM64, arm64_regs.Arch());
272
273 RegsX86 x86_regs;
274 EXPECT_EQ(ARCH_X86, x86_regs.Arch());
275
276 RegsX86_64 x86_64_regs;
277 EXPECT_EQ(ARCH_X86_64, x86_64_regs.Arch());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100278
279 RegsMips mips_regs;
280 EXPECT_EQ(ARCH_MIPS, mips_regs.Arch());
281
282 RegsMips64 mips64_regs;
283 EXPECT_EQ(ARCH_MIPS64, mips64_regs.Arch());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800284}
285
Christopher Ferrisd226a512017-07-14 10:37:19 -0700286} // namespace unwindstack