blob: d81edbff7736289ba02f37c32bad0334076c96e3 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 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 <elf.h>
Christopher Ferrisbae69f12017-06-28 14:51:54 -070018#include <fcntl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080022
Christopher Ferrise69f4702017-10-19 16:08:58 -070023#include <gmock/gmock.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080024#include <gtest/gtest.h>
25
Christopher Ferrisd226a512017-07-14 10:37:19 -070026#include <unwindstack/Elf.h>
27#include <unwindstack/MapInfo.h>
Christopher Ferrisd06001d2017-11-30 18:56:01 -080028#include <unwindstack/RegsArm.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080029
Christopher Ferrise69f4702017-10-19 16:08:58 -070030#include "ElfFake.h"
Christopher Ferris570b76f2017-06-30 17:18:16 -070031#include "ElfTestUtils.h"
Christopher Ferrisa0196652017-07-18 16:09:20 -070032#include "LogFake.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080033#include "MemoryFake.h"
34
35#if !defined(PT_ARM_EXIDX)
36#define PT_ARM_EXIDX 0x70000001
37#endif
38
Christopher Ferrisd226a512017-07-14 10:37:19 -070039namespace unwindstack {
40
Christopher Ferris3958f802017-02-01 15:44:40 -080041class ElfTest : public ::testing::Test {
42 protected:
43 void SetUp() override {
44 memory_ = new MemoryFake;
45 }
46
Christopher Ferris570b76f2017-06-30 17:18:16 -070047 void InitElf32(uint32_t machine_type) {
Christopher Ferris3958f802017-02-01 15:44:40 -080048 Elf32_Ehdr ehdr;
Christopher Ferris570b76f2017-06-30 17:18:16 -070049 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, machine_type);
Christopher Ferris3958f802017-02-01 15:44:40 -080050
Christopher Ferris3958f802017-02-01 15:44:40 -080051 ehdr.e_phoff = 0x100;
Christopher Ferris3958f802017-02-01 15:44:40 -080052 ehdr.e_ehsize = sizeof(ehdr);
53 ehdr.e_phentsize = sizeof(Elf32_Phdr);
54 ehdr.e_phnum = 1;
55 ehdr.e_shentsize = sizeof(Elf32_Shdr);
Christopher Ferris570b76f2017-06-30 17:18:16 -070056 if (machine_type == EM_ARM) {
Christopher Ferris3958f802017-02-01 15:44:40 -080057 ehdr.e_flags = 0x5000200;
58 ehdr.e_phnum = 2;
59 }
60 memory_->SetMemory(0, &ehdr, sizeof(ehdr));
61
62 Elf32_Phdr phdr;
63 memset(&phdr, 0, sizeof(phdr));
64 phdr.p_type = PT_LOAD;
Christopher Ferris3958f802017-02-01 15:44:40 -080065 phdr.p_filesz = 0x10000;
66 phdr.p_memsz = 0x10000;
67 phdr.p_flags = PF_R | PF_X;
68 phdr.p_align = 0x1000;
69 memory_->SetMemory(0x100, &phdr, sizeof(phdr));
70
Christopher Ferris570b76f2017-06-30 17:18:16 -070071 if (machine_type == EM_ARM) {
Christopher Ferris3958f802017-02-01 15:44:40 -080072 memset(&phdr, 0, sizeof(phdr));
73 phdr.p_type = PT_ARM_EXIDX;
74 phdr.p_offset = 0x30000;
75 phdr.p_vaddr = 0x30000;
76 phdr.p_paddr = 0x30000;
77 phdr.p_filesz = 16;
78 phdr.p_memsz = 16;
79 phdr.p_flags = PF_R;
80 phdr.p_align = 0x4;
81 memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
82 }
83 }
84
Christopher Ferris570b76f2017-06-30 17:18:16 -070085 void InitElf64(uint32_t machine_type) {
Christopher Ferris3958f802017-02-01 15:44:40 -080086 Elf64_Ehdr ehdr;
Christopher Ferris570b76f2017-06-30 17:18:16 -070087 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, machine_type);
Christopher Ferris3958f802017-02-01 15:44:40 -080088
Christopher Ferris3958f802017-02-01 15:44:40 -080089 ehdr.e_phoff = 0x100;
Christopher Ferris3958f802017-02-01 15:44:40 -080090 ehdr.e_flags = 0x5000200;
91 ehdr.e_ehsize = sizeof(ehdr);
92 ehdr.e_phentsize = sizeof(Elf64_Phdr);
93 ehdr.e_phnum = 1;
94 ehdr.e_shentsize = sizeof(Elf64_Shdr);
Christopher Ferris3958f802017-02-01 15:44:40 -080095 memory_->SetMemory(0, &ehdr, sizeof(ehdr));
96
97 Elf64_Phdr phdr;
98 memset(&phdr, 0, sizeof(phdr));
99 phdr.p_type = PT_LOAD;
Christopher Ferris3958f802017-02-01 15:44:40 -0800100 phdr.p_filesz = 0x10000;
101 phdr.p_memsz = 0x10000;
102 phdr.p_flags = PF_R | PF_X;
103 phdr.p_align = 0x1000;
104 memory_->SetMemory(0x100, &phdr, sizeof(phdr));
105 }
106
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800107 void VerifyStepIfSignalHandler(uint64_t load_bias);
108
Christopher Ferris3958f802017-02-01 15:44:40 -0800109 MemoryFake* memory_;
110};
111
112TEST_F(ElfTest, invalid_memory) {
113 Elf elf(memory_);
114
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700115 ASSERT_FALSE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800116 ASSERT_FALSE(elf.valid());
117}
118
119TEST_F(ElfTest, elf_invalid) {
120 Elf elf(memory_);
121
122 InitElf32(EM_386);
123
124 // Corrupt the ELF signature.
125 memory_->SetData32(0, 0x7f000000);
126
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700127 ASSERT_FALSE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800128 ASSERT_FALSE(elf.valid());
129 ASSERT_TRUE(elf.interface() == nullptr);
130
Christopher Ferris02a6c442019-03-11 14:43:33 -0700131 ASSERT_EQ("", elf.GetSoname());
Christopher Ferris3958f802017-02-01 15:44:40 -0800132
Christopher Ferris02a6c442019-03-11 14:43:33 -0700133 std::string name;
Christopher Ferris3958f802017-02-01 15:44:40 -0800134 uint64_t func_offset;
135 ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
136
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700137 ASSERT_FALSE(elf.StepIfSignalHandler(0, nullptr, nullptr));
138 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
139
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700140 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700141 bool is_signal_frame;
142 ASSERT_FALSE(elf.Step(0, nullptr, nullptr, &finished, &is_signal_frame));
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700143 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
Christopher Ferris3958f802017-02-01 15:44:40 -0800144}
145
Christopher Ferrisa0196652017-07-18 16:09:20 -0700146TEST_F(ElfTest, elf32_invalid_machine) {
147 Elf elf(memory_);
148
149 InitElf32(EM_PPC);
150
151 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700152 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700153
154 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100155 ASSERT_EQ("4 unwind 32 bit elf that is neither arm nor x86 nor mips: e_machine = 20\n\n",
Christopher Ferrisa0196652017-07-18 16:09:20 -0700156 GetFakeLogPrint());
157}
158
159TEST_F(ElfTest, elf64_invalid_machine) {
160 Elf elf(memory_);
161
162 InitElf64(EM_PPC64);
163
164 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700165 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700166
167 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100168 ASSERT_EQ("4 unwind 64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = 21\n\n",
Christopher Ferrisa0196652017-07-18 16:09:20 -0700169 GetFakeLogPrint());
170}
171
Christopher Ferris3958f802017-02-01 15:44:40 -0800172TEST_F(ElfTest, elf_arm) {
173 Elf elf(memory_);
174
175 InitElf32(EM_ARM);
176
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700177 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800178 ASSERT_TRUE(elf.valid());
179 ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
180 ASSERT_EQ(ELFCLASS32, elf.class_type());
181 ASSERT_TRUE(elf.interface() != nullptr);
182}
183
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100184TEST_F(ElfTest, elf_mips) {
185 Elf elf(memory_);
186
187 InitElf32(EM_MIPS);
188
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700189 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100190 ASSERT_TRUE(elf.valid());
191 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
192 ASSERT_EQ(ELFCLASS32, elf.class_type());
193 ASSERT_TRUE(elf.interface() != nullptr);
194}
195
Christopher Ferris3958f802017-02-01 15:44:40 -0800196TEST_F(ElfTest, elf_x86) {
197 Elf elf(memory_);
198
199 InitElf32(EM_386);
200
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700201 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800202 ASSERT_TRUE(elf.valid());
203 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
204 ASSERT_EQ(ELFCLASS32, elf.class_type());
205 ASSERT_TRUE(elf.interface() != nullptr);
206}
207
208TEST_F(ElfTest, elf_arm64) {
209 Elf elf(memory_);
210
211 InitElf64(EM_AARCH64);
212
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700213 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800214 ASSERT_TRUE(elf.valid());
215 ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
216 ASSERT_EQ(ELFCLASS64, elf.class_type());
217 ASSERT_TRUE(elf.interface() != nullptr);
218}
219
220TEST_F(ElfTest, elf_x86_64) {
221 Elf elf(memory_);
222
223 InitElf64(EM_X86_64);
224
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700225 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800226 ASSERT_TRUE(elf.valid());
227 ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
228 ASSERT_EQ(ELFCLASS64, elf.class_type());
229 ASSERT_TRUE(elf.interface() != nullptr);
230}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700231
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100232TEST_F(ElfTest, elf_mips64) {
233 Elf elf(memory_);
234
235 InitElf64(EM_MIPS);
236
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700237 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100238 ASSERT_TRUE(elf.valid());
239 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
240 ASSERT_EQ(ELFCLASS64, elf.class_type());
241 ASSERT_TRUE(elf.interface() != nullptr);
242}
243
Christopher Ferris570b76f2017-06-30 17:18:16 -0700244TEST_F(ElfTest, gnu_debugdata_init32) {
245 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
246 [&](uint64_t offset, const void* ptr, size_t size) {
247 memory_->SetMemory(offset, ptr, size);
248 });
249
250 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700251 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700252 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700253 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700254 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
255 EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700256}
257
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700258TEST_F(ElfTest, gnu_debugdata_init64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700259 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
260 [&](uint64_t offset, const void* ptr, size_t size) {
261 memory_->SetMemory(offset, ptr, size);
262 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700263
Christopher Ferris570b76f2017-06-30 17:18:16 -0700264 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700265 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700266 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700267 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700268 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
269 EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700270}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700271
Christopher Ferrisd226a512017-07-14 10:37:19 -0700272TEST_F(ElfTest, rel_pc) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700273 ElfFake elf(memory_);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700274
Christopher Ferrise69f4702017-10-19 16:08:58 -0700275 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
276 elf.FakeSetInterface(interface);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700277
Christopher Ferrise69f4702017-10-19 16:08:58 -0700278 elf.FakeSetValid(true);
Christopher Ferris0f40a052020-01-22 12:17:06 -0800279 MapInfo map_info(nullptr, nullptr, 0x1000, 0x2000, 0, 0, "");
Christopher Ferrisd226a512017-07-14 10:37:19 -0700280
281 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
282
Christopher Ferrise69f4702017-10-19 16:08:58 -0700283 elf.FakeSetValid(false);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700284 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
285}
286
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800287void ElfTest::VerifyStepIfSignalHandler(uint64_t load_bias) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700288 ElfFake elf(memory_);
289
290 RegsArm regs;
291 regs[13] = 0x50000;
292 regs[15] = 0x8000;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700293
294 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
295 elf.FakeSetInterface(interface);
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800296 elf.FakeSetLoadBias(load_bias);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700297
298 memory_->SetData32(0x3000, 0xdf0027ad);
299 MemoryFake process_memory;
300 process_memory.SetData32(0x50000, 0);
301 for (size_t i = 0; i < 16; i++) {
302 process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
303 }
304
305 elf.FakeSetValid(true);
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800306 ASSERT_TRUE(elf.StepIfSignalHandler(0x3000 + load_bias, &regs, &process_memory));
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700307 EXPECT_EQ(ERROR_NONE, elf.GetLastErrorCode());
Christopher Ferrise69f4702017-10-19 16:08:58 -0700308 EXPECT_EQ(15U, regs.pc());
309 EXPECT_EQ(13U, regs.sp());
310}
311
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800312TEST_F(ElfTest, step_in_signal_map) {
313 VerifyStepIfSignalHandler(0);
314}
315
316TEST_F(ElfTest, step_in_signal_map_non_zero_load_bias) {
317 VerifyStepIfSignalHandler(0x1000);
318}
319
Christopher Ferrise69f4702017-10-19 16:08:58 -0700320class ElfInterfaceMock : public ElfInterface {
321 public:
322 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
323 virtual ~ElfInterfaceMock() = default;
324
Christopher Ferris819f1312019-10-03 13:35:48 -0700325 bool Init(int64_t*) override { return false; }
326 void InitHeaders() override {}
Christopher Ferris02a6c442019-03-11 14:43:33 -0700327 std::string GetSoname() override { return ""; }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700328 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800329 std::string GetBuildID() override { return ""; }
Christopher Ferris150db122017-12-20 18:49:01 -0800330
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700331 MOCK_METHOD(bool, Step, (uint64_t, Regs*, Memory*, bool*, bool*), (override));
Christopher Ferrisbaf058b2019-10-11 14:26:55 -0700332 MOCK_METHOD(bool, GetGlobalVariable, (const std::string&, uint64_t*), (override));
333 MOCK_METHOD(bool, IsValidPc, (uint64_t), (override));
Christopher Ferris150db122017-12-20 18:49:01 -0800334
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800335 void MockSetDataOffset(uint64_t offset) { data_offset_ = offset; }
336 void MockSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; }
337 void MockSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; }
338
Christopher Ferris150db122017-12-20 18:49:01 -0800339 void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800340 void MockSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; }
341 void MockSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700342};
343
344TEST_F(ElfTest, step_in_interface) {
345 ElfFake elf(memory_);
346 elf.FakeSetValid(true);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700347
348 RegsArm regs;
349
350 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
351 elf.FakeSetInterface(interface);
352 MemoryFake process_memory;
353
354 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700355 bool is_signal_frame;
356 EXPECT_CALL(*interface, Step(0x1000, &regs, &process_memory, &finished, &is_signal_frame))
Christopher Ferrise69f4702017-10-19 16:08:58 -0700357 .WillOnce(::testing::Return(true));
358
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700359 ASSERT_TRUE(elf.Step(0x1000, &regs, &process_memory, &finished, &is_signal_frame));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700360}
361
Christopher Ferris150db122017-12-20 18:49:01 -0800362TEST_F(ElfTest, get_global_invalid_elf) {
363 ElfFake elf(memory_);
364 elf.FakeSetValid(false);
365
366 std::string global("something");
367 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800368 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800369}
370
371TEST_F(ElfTest, get_global_valid_not_in_interface) {
372 ElfFake elf(memory_);
373 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800374
375 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
376 elf.FakeSetInterface(interface);
377
Christopher Ferris150db122017-12-20 18:49:01 -0800378 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800379 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
380 .WillOnce(::testing::Return(false));
Christopher Ferris150db122017-12-20 18:49:01 -0800381
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800382 uint64_t offset;
383 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800384}
385
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800386TEST_F(ElfTest, get_global_vaddr_in_no_sections) {
Christopher Ferris150db122017-12-20 18:49:01 -0800387 ElfFake elf(memory_);
388 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800389
390 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
391 elf.FakeSetInterface(interface);
392
Christopher Ferris150db122017-12-20 18:49:01 -0800393 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800394 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
Christopher Ferris150db122017-12-20 18:49:01 -0800395 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
396
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700397 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800398 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700399}
400
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800401TEST_F(ElfTest, get_global_vaddr_in_data_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800402 ElfFake elf(memory_);
403 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800404
405 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
406 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800407 interface->MockSetDataVaddrStart(0x500);
408 interface->MockSetDataVaddrEnd(0x600);
409 interface->MockSetDataOffset(0xa000);
Christopher Ferris150db122017-12-20 18:49:01 -0800410
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800411 std::string global("something");
412 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
413 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x580), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800414
415 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800416 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
417 EXPECT_EQ(0xa080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800418}
419
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800420TEST_F(ElfTest, get_global_vaddr_in_dynamic_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800421 ElfFake elf(memory_);
422 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800423
424 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
425 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800426 interface->MockSetDataVaddrStart(0x500);
427 interface->MockSetDataVaddrEnd(0x600);
428 interface->MockSetDataOffset(0xa000);
429
430 interface->MockSetDynamicVaddrStart(0x800);
431 interface->MockSetDynamicVaddrEnd(0x900);
432 interface->MockSetDynamicOffset(0xc000);
433
434 std::string global("something");
435 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
436 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x880), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800437
438 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800439 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
440 EXPECT_EQ(0xc080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800441}
442
Yong Lifbca3002020-04-23 20:40:29 +0000443TEST_F(ElfTest, get_global_vaddr_with_tagged_pointer) {
444 ElfFake elf(memory_);
445 elf.FakeSetValid(true);
446 elf.FakeSetArch(ARCH_ARM64);
447
448 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
449 elf.FakeSetInterface(interface);
450 interface->MockSetDataVaddrStart(0x500);
451 interface->MockSetDataVaddrEnd(0x600);
452 interface->MockSetDataOffset(0xa000);
453
454 std::string global("something");
455 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
456 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x8800000000000580),
457 ::testing::Return(true)));
458
459 uint64_t offset;
460 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
461 EXPECT_EQ(0xa080U, offset);
462}
463
464TEST_F(ElfTest, get_global_vaddr_without_tagged_pointer) {
465 ElfFake elf(memory_);
466 elf.FakeSetValid(true);
467 elf.FakeSetArch(ARCH_X86_64);
468
469 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
470 elf.FakeSetInterface(interface);
471 interface->MockSetDataVaddrStart(0x8800000000000500);
472 interface->MockSetDataVaddrEnd(0x8800000000000600);
473 interface->MockSetDataOffset(0x880000000000a000);
474
475 std::string global("something");
476 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
477 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x8800000000000580),
478 ::testing::Return(true)));
479
480 uint64_t offset;
481 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
482 EXPECT_EQ(0x880000000000a080U, offset);
483}
484
Christopher Ferris150db122017-12-20 18:49:01 -0800485TEST_F(ElfTest, is_valid_pc_elf_invalid) {
486 ElfFake elf(memory_);
487 elf.FakeSetValid(false);
Christopher Ferris150db122017-12-20 18:49:01 -0800488
489 EXPECT_FALSE(elf.IsValidPc(0x100));
490 EXPECT_FALSE(elf.IsValidPc(0x200));
491}
492
493TEST_F(ElfTest, is_valid_pc_interface) {
494 ElfFake elf(memory_);
495 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800496
497 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
498 elf.FakeSetInterface(interface);
499
500 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
501
502 EXPECT_TRUE(elf.IsValidPc(0x1500));
503}
504
Christopher Ferris150db122017-12-20 18:49:01 -0800505TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
506 ElfFake elf(memory_);
507 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800508
509 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
510 elf.FakeSetInterface(interface);
511 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
512 elf.FakeSetGnuDebugdataInterface(gnu_interface);
513
514 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
515 EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
516
517 EXPECT_TRUE(elf.IsValidPc(0x1500));
518}
519
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800520TEST_F(ElfTest, error_code_not_valid) {
521 ElfFake elf(memory_);
522 elf.FakeSetValid(false);
523
524 ErrorData error{ERROR_MEMORY_INVALID, 0x100};
525 elf.GetLastError(&error);
526 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
527 EXPECT_EQ(0x100U, error.address);
528}
529
530TEST_F(ElfTest, error_code_valid) {
531 ElfFake elf(memory_);
532 elf.FakeSetValid(true);
533 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
534 elf.FakeSetInterface(interface);
535 interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
536 interface->FakeSetErrorAddress(0x1000);
537
538 ErrorData error{ERROR_NONE, 0};
539 elf.GetLastError(&error);
540 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
541 EXPECT_EQ(0x1000U, error.address);
542 EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
543 EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
544}
545
Christopher Ferrisd226a512017-07-14 10:37:19 -0700546} // namespace unwindstack