blob: e4fc6f0706016ffa7c7ac17b59430fb96b77a59e [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) {
Peter Collingbourne5ac39272020-03-19 17:27:58 -070096 EXPECT_EQ(4U, GetPcAdjustment(0x10, elf_.get(), ARCH_ARM64));
97 EXPECT_EQ(4U, GetPcAdjustment(0x4, elf_.get(), ARCH_ARM64));
98 EXPECT_EQ(0U, GetPcAdjustment(0x3, elf_.get(), ARCH_ARM64));
99 EXPECT_EQ(0U, GetPcAdjustment(0x2, elf_.get(), ARCH_ARM64));
100 EXPECT_EQ(0U, GetPcAdjustment(0x1, elf_.get(), ARCH_ARM64));
101 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_ARM64));
Christopher Ferris3958f802017-02-01 15:44:40 -0800102
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700103 EXPECT_EQ(1U, GetPcAdjustment(0x100, elf_.get(), ARCH_X86));
104 EXPECT_EQ(1U, GetPcAdjustment(0x2, elf_.get(), ARCH_X86));
105 EXPECT_EQ(1U, GetPcAdjustment(0x1, elf_.get(), ARCH_X86));
106 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_X86));
Christopher Ferris3958f802017-02-01 15:44:40 -0800107
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700108 EXPECT_EQ(1U, GetPcAdjustment(0x100, elf_.get(), ARCH_X86_64));
109 EXPECT_EQ(1U, GetPcAdjustment(0x2, elf_.get(), ARCH_X86_64));
110 EXPECT_EQ(1U, GetPcAdjustment(0x1, elf_.get(), ARCH_X86_64));
111 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_X86_64));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100112
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700113 EXPECT_EQ(8U, GetPcAdjustment(0x10, elf_.get(), ARCH_MIPS));
114 EXPECT_EQ(8U, GetPcAdjustment(0x8, elf_.get(), ARCH_MIPS));
115 EXPECT_EQ(0U, GetPcAdjustment(0x7, elf_.get(), ARCH_MIPS));
116 EXPECT_EQ(0U, GetPcAdjustment(0x6, elf_.get(), ARCH_MIPS));
117 EXPECT_EQ(0U, GetPcAdjustment(0x5, elf_.get(), ARCH_MIPS));
118 EXPECT_EQ(0U, GetPcAdjustment(0x4, elf_.get(), ARCH_MIPS));
119 EXPECT_EQ(0U, GetPcAdjustment(0x3, elf_.get(), ARCH_MIPS));
120 EXPECT_EQ(0U, GetPcAdjustment(0x2, elf_.get(), ARCH_MIPS));
121 EXPECT_EQ(0U, GetPcAdjustment(0x1, elf_.get(), ARCH_MIPS));
122 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_MIPS));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100123
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700124 EXPECT_EQ(8U, GetPcAdjustment(0x10, elf_.get(), ARCH_MIPS64));
125 EXPECT_EQ(8U, GetPcAdjustment(0x8, elf_.get(), ARCH_MIPS64));
126 EXPECT_EQ(0U, GetPcAdjustment(0x7, elf_.get(), ARCH_MIPS64));
127 EXPECT_EQ(0U, GetPcAdjustment(0x6, elf_.get(), ARCH_MIPS64));
128 EXPECT_EQ(0U, GetPcAdjustment(0x5, elf_.get(), ARCH_MIPS64));
129 EXPECT_EQ(0U, GetPcAdjustment(0x4, elf_.get(), ARCH_MIPS64));
130 EXPECT_EQ(0U, GetPcAdjustment(0x3, elf_.get(), ARCH_MIPS64));
131 EXPECT_EQ(0U, GetPcAdjustment(0x2, elf_.get(), ARCH_MIPS64));
132 EXPECT_EQ(0U, GetPcAdjustment(0x1, elf_.get(), ARCH_MIPS64));
133 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_MIPS64));
Christopher Ferris3958f802017-02-01 15:44:40 -0800134}
135
136TEST_F(RegsTest, rel_pc_arm) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800137 // Check fence posts.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700138 elf_->FakeSetLoadBias(0);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700139 EXPECT_EQ(2U, GetPcAdjustment(0x5, elf_.get(), ARCH_ARM));
140 EXPECT_EQ(2U, GetPcAdjustment(0x4, elf_.get(), ARCH_ARM));
141 EXPECT_EQ(2U, GetPcAdjustment(0x3, elf_.get(), ARCH_ARM));
142 EXPECT_EQ(2U, GetPcAdjustment(0x2, elf_.get(), ARCH_ARM));
143 EXPECT_EQ(0U, GetPcAdjustment(0x1, elf_.get(), ARCH_ARM));
144 EXPECT_EQ(0U, GetPcAdjustment(0x0, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800145
Christopher Ferrise69f4702017-10-19 16:08:58 -0700146 elf_->FakeSetLoadBias(0x100);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700147 EXPECT_EQ(0U, GetPcAdjustment(0x1, elf_.get(), ARCH_ARM));
148 EXPECT_EQ(2U, GetPcAdjustment(0x2, elf_.get(), ARCH_ARM));
149 EXPECT_EQ(2U, GetPcAdjustment(0xff, elf_.get(), ARCH_ARM));
150 EXPECT_EQ(2U, GetPcAdjustment(0x105, elf_.get(), ARCH_ARM));
151 EXPECT_EQ(2U, GetPcAdjustment(0x104, elf_.get(), ARCH_ARM));
152 EXPECT_EQ(2U, GetPcAdjustment(0x103, elf_.get(), ARCH_ARM));
153 EXPECT_EQ(2U, GetPcAdjustment(0x102, elf_.get(), ARCH_ARM));
154 EXPECT_EQ(0U, GetPcAdjustment(0x101, elf_.get(), ARCH_ARM));
155 EXPECT_EQ(0U, GetPcAdjustment(0x100, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800156
157 // Check thumb instructions handling.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700158 elf_->FakeSetLoadBias(0);
Christopher Ferris3958f802017-02-01 15:44:40 -0800159 memory_->SetData32(0x2000, 0);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700160 EXPECT_EQ(2U, GetPcAdjustment(0x2005, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800161 memory_->SetData32(0x2000, 0xe000f000);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700162 EXPECT_EQ(4U, GetPcAdjustment(0x2005, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800163
Christopher Ferrise69f4702017-10-19 16:08:58 -0700164 elf_->FakeSetLoadBias(0x400);
Christopher Ferris3958f802017-02-01 15:44:40 -0800165 memory_->SetData32(0x2100, 0);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700166 EXPECT_EQ(2U, GetPcAdjustment(0x2505, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800167 memory_->SetData32(0x2100, 0xf111f111);
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700168 EXPECT_EQ(4U, GetPcAdjustment(0x2505, elf_.get(), ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800169}
170
171TEST_F(RegsTest, elf_invalid) {
Christopher Ferris0f40a052020-01-22 12:17:06 -0800172 MapInfo map_info(nullptr, nullptr, 0x1000, 0x2000, 0, 0, "");
Christopher Ferris6dbc28e2018-03-28 15:12:49 -0700173 Elf* invalid_elf = new Elf(nullptr);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800174 map_info.elf.reset(invalid_elf);
Christopher Ferris3958f802017-02-01 15:44:40 -0800175
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700176 EXPECT_EQ(0x500U, invalid_elf->GetRelPc(0x1500, &map_info));
177 EXPECT_EQ(2U, GetPcAdjustment(0x500U, invalid_elf, ARCH_ARM));
178 EXPECT_EQ(2U, GetPcAdjustment(0x511U, invalid_elf, ARCH_ARM));
Christopher Ferris3958f802017-02-01 15:44:40 -0800179
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700180 EXPECT_EQ(0x600U, invalid_elf->GetRelPc(0x1600, &map_info));
181 EXPECT_EQ(4U, GetPcAdjustment(0x600U, invalid_elf, ARCH_ARM64));
Christopher Ferris3958f802017-02-01 15:44:40 -0800182
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700183 EXPECT_EQ(0x700U, invalid_elf->GetRelPc(0x1700, &map_info));
184 EXPECT_EQ(1U, GetPcAdjustment(0x700U, invalid_elf, ARCH_X86));
Christopher Ferris3958f802017-02-01 15:44:40 -0800185
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700186 EXPECT_EQ(0x800U, invalid_elf->GetRelPc(0x1800, &map_info));
187 EXPECT_EQ(1U, GetPcAdjustment(0x800U, invalid_elf, ARCH_X86_64));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100188
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700189 EXPECT_EQ(0x900U, invalid_elf->GetRelPc(0x1900, &map_info));
190 EXPECT_EQ(8U, GetPcAdjustment(0x900U, invalid_elf, ARCH_MIPS));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100191
Peter Collingbourne5ac39272020-03-19 17:27:58 -0700192 EXPECT_EQ(0xa00U, invalid_elf->GetRelPc(0x1a00, &map_info));
193 EXPECT_EQ(8U, GetPcAdjustment(0xa00U, invalid_elf, ARCH_MIPS64));
Christopher Ferris723cf9b2017-01-19 20:08:48 -0800194}
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700195
Yabin Cui11e96fe2018-03-14 18:16:22 -0700196TEST_F(RegsTest, arm_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700197 RegsArm arm;
198 uint32_t* regs = reinterpret_cast<uint32_t*>(arm.RawData());
199 regs[13] = 0x100;
200 regs[15] = 0x200;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700201 EXPECT_EQ(0x100U, arm.sp());
202 EXPECT_EQ(0x200U, arm.pc());
203}
204
Yabin Cui11e96fe2018-03-14 18:16:22 -0700205TEST_F(RegsTest, arm64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700206 RegsArm64 arm64;
207 uint64_t* regs = reinterpret_cast<uint64_t*>(arm64.RawData());
208 regs[31] = 0xb100000000ULL;
209 regs[32] = 0xc200000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700210 EXPECT_EQ(0xb100000000U, arm64.sp());
211 EXPECT_EQ(0xc200000000U, arm64.pc());
212}
213
Yabin Cui11e96fe2018-03-14 18:16:22 -0700214TEST_F(RegsTest, x86_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700215 RegsX86 x86;
216 uint32_t* regs = reinterpret_cast<uint32_t*>(x86.RawData());
217 regs[4] = 0x23450000;
218 regs[8] = 0xabcd0000;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700219 EXPECT_EQ(0x23450000U, x86.sp());
220 EXPECT_EQ(0xabcd0000U, x86.pc());
221}
222
Yabin Cui11e96fe2018-03-14 18:16:22 -0700223TEST_F(RegsTest, x86_64_verify_sp_pc) {
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700224 RegsX86_64 x86_64;
225 uint64_t* regs = reinterpret_cast<uint64_t*>(x86_64.RawData());
226 regs[7] = 0x1200000000ULL;
227 regs[16] = 0x4900000000ULL;
Christopher Ferris2a25c4a2017-07-07 16:35:48 -0700228 EXPECT_EQ(0x1200000000U, x86_64.sp());
229 EXPECT_EQ(0x4900000000U, x86_64.pc());
230}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700231
Yabin Cui11e96fe2018-03-14 18:16:22 -0700232TEST_F(RegsTest, mips_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100233 RegsMips mips;
234 uint32_t* regs = reinterpret_cast<uint32_t*>(mips.RawData());
235 regs[29] = 0x100;
236 regs[32] = 0x200;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100237 EXPECT_EQ(0x100U, mips.sp());
238 EXPECT_EQ(0x200U, mips.pc());
239}
240
Yabin Cui11e96fe2018-03-14 18:16:22 -0700241TEST_F(RegsTest, mips64_verify_sp_pc) {
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100242 RegsMips64 mips64;
243 uint64_t* regs = reinterpret_cast<uint64_t*>(mips64.RawData());
244 regs[29] = 0xb100000000ULL;
245 regs[32] = 0xc200000000ULL;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100246 EXPECT_EQ(0xb100000000U, mips64.sp());
247 EXPECT_EQ(0xc200000000U, mips64.pc());
248}
249
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800250TEST_F(RegsTest, machine_type) {
251 RegsArm arm_regs;
252 EXPECT_EQ(ARCH_ARM, arm_regs.Arch());
253
254 RegsArm64 arm64_regs;
255 EXPECT_EQ(ARCH_ARM64, arm64_regs.Arch());
256
257 RegsX86 x86_regs;
258 EXPECT_EQ(ARCH_X86, x86_regs.Arch());
259
260 RegsX86_64 x86_64_regs;
261 EXPECT_EQ(ARCH_X86_64, x86_64_regs.Arch());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100262
263 RegsMips mips_regs;
264 EXPECT_EQ(ARCH_MIPS, mips_regs.Arch());
265
266 RegsMips64 mips64_regs;
267 EXPECT_EQ(ARCH_MIPS64, mips64_regs.Arch());
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800268}
269
Josh Gao2f37a152018-04-20 11:51:14 -0700270template <typename RegisterType>
271void clone_test(Regs* regs) {
272 RegisterType* register_values = reinterpret_cast<RegisterType*>(regs->RawData());
273 int num_regs = regs->total_regs();
274 for (int i = 0; i < num_regs; ++i) {
275 register_values[i] = i;
276 }
277
278 std::unique_ptr<Regs> clone(regs->Clone());
279 ASSERT_EQ(regs->total_regs(), clone->total_regs());
280 RegisterType* clone_values = reinterpret_cast<RegisterType*>(clone->RawData());
281 for (int i = 0; i < num_regs; ++i) {
282 EXPECT_EQ(register_values[i], clone_values[i]);
283 EXPECT_NE(&register_values[i], &clone_values[i]);
284 }
285}
286
287TEST_F(RegsTest, clone) {
288 std::vector<std::unique_ptr<Regs>> regs;
289 regs.emplace_back(new RegsArm());
290 regs.emplace_back(new RegsArm64());
291 regs.emplace_back(new RegsX86());
292 regs.emplace_back(new RegsX86_64());
293 regs.emplace_back(new RegsMips());
294 regs.emplace_back(new RegsMips64());
295
296 for (auto& r : regs) {
297 if (r->Is32Bit()) {
298 clone_test<uint32_t>(r.get());
299 } else {
300 clone_test<uint64_t>(r.get());
301 }
302 }
303}
304
Christopher Ferrisd226a512017-07-14 10:37:19 -0700305} // namespace unwindstack