blob: 1f3ed8190d315d41d37ee2fe378ad6a32712aa8c [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;
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700141 ASSERT_FALSE(elf.Step(0, nullptr, nullptr, &finished));
142 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
Christopher Ferris3958f802017-02-01 15:44:40 -0800143}
144
Christopher Ferrisa0196652017-07-18 16:09:20 -0700145TEST_F(ElfTest, elf32_invalid_machine) {
146 Elf elf(memory_);
147
148 InitElf32(EM_PPC);
149
150 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700151 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700152
153 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100154 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 -0700155 GetFakeLogPrint());
156}
157
158TEST_F(ElfTest, elf64_invalid_machine) {
159 Elf elf(memory_);
160
161 InitElf64(EM_PPC64);
162
163 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700164 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700165
166 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100167 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 -0700168 GetFakeLogPrint());
169}
170
Christopher Ferris3958f802017-02-01 15:44:40 -0800171TEST_F(ElfTest, elf_arm) {
172 Elf elf(memory_);
173
174 InitElf32(EM_ARM);
175
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700176 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800177 ASSERT_TRUE(elf.valid());
178 ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
179 ASSERT_EQ(ELFCLASS32, elf.class_type());
180 ASSERT_TRUE(elf.interface() != nullptr);
181}
182
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100183TEST_F(ElfTest, elf_mips) {
184 Elf elf(memory_);
185
186 InitElf32(EM_MIPS);
187
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700188 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100189 ASSERT_TRUE(elf.valid());
190 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
191 ASSERT_EQ(ELFCLASS32, elf.class_type());
192 ASSERT_TRUE(elf.interface() != nullptr);
193}
194
Christopher Ferris3958f802017-02-01 15:44:40 -0800195TEST_F(ElfTest, elf_x86) {
196 Elf elf(memory_);
197
198 InitElf32(EM_386);
199
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700200 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800201 ASSERT_TRUE(elf.valid());
202 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
203 ASSERT_EQ(ELFCLASS32, elf.class_type());
204 ASSERT_TRUE(elf.interface() != nullptr);
205}
206
207TEST_F(ElfTest, elf_arm64) {
208 Elf elf(memory_);
209
210 InitElf64(EM_AARCH64);
211
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700212 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800213 ASSERT_TRUE(elf.valid());
214 ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
215 ASSERT_EQ(ELFCLASS64, elf.class_type());
216 ASSERT_TRUE(elf.interface() != nullptr);
217}
218
219TEST_F(ElfTest, elf_x86_64) {
220 Elf elf(memory_);
221
222 InitElf64(EM_X86_64);
223
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700224 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800225 ASSERT_TRUE(elf.valid());
226 ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
227 ASSERT_EQ(ELFCLASS64, elf.class_type());
228 ASSERT_TRUE(elf.interface() != nullptr);
229}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700230
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100231TEST_F(ElfTest, elf_mips64) {
232 Elf elf(memory_);
233
234 InitElf64(EM_MIPS);
235
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700236 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100237 ASSERT_TRUE(elf.valid());
238 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
239 ASSERT_EQ(ELFCLASS64, elf.class_type());
240 ASSERT_TRUE(elf.interface() != nullptr);
241}
242
Christopher Ferris570b76f2017-06-30 17:18:16 -0700243TEST_F(ElfTest, gnu_debugdata_init32) {
244 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
245 [&](uint64_t offset, const void* ptr, size_t size) {
246 memory_->SetMemory(offset, ptr, size);
247 });
248
249 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700250 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700251 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700252 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700253 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
254 EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700255}
256
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700257TEST_F(ElfTest, gnu_debugdata_init64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700258 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
259 [&](uint64_t offset, const void* ptr, size_t size) {
260 memory_->SetMemory(offset, ptr, size);
261 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700262
Christopher Ferris570b76f2017-06-30 17:18:16 -0700263 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700264 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700265 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700266 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700267 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
268 EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700269}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700270
Christopher Ferrisd226a512017-07-14 10:37:19 -0700271TEST_F(ElfTest, rel_pc) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700272 ElfFake elf(memory_);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700273
Christopher Ferrise69f4702017-10-19 16:08:58 -0700274 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
275 elf.FakeSetInterface(interface);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700276
Christopher Ferrise69f4702017-10-19 16:08:58 -0700277 elf.FakeSetValid(true);
Christopher Ferris0f40a052020-01-22 12:17:06 -0800278 MapInfo map_info(nullptr, nullptr, 0x1000, 0x2000, 0, 0, "");
Christopher Ferrisd226a512017-07-14 10:37:19 -0700279
280 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
281
Christopher Ferrise69f4702017-10-19 16:08:58 -0700282 elf.FakeSetValid(false);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700283 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
284}
285
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800286void ElfTest::VerifyStepIfSignalHandler(uint64_t load_bias) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700287 ElfFake elf(memory_);
288
289 RegsArm regs;
290 regs[13] = 0x50000;
291 regs[15] = 0x8000;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700292
293 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
294 elf.FakeSetInterface(interface);
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800295 elf.FakeSetLoadBias(load_bias);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700296
297 memory_->SetData32(0x3000, 0xdf0027ad);
298 MemoryFake process_memory;
299 process_memory.SetData32(0x50000, 0);
300 for (size_t i = 0; i < 16; i++) {
301 process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
302 }
303
304 elf.FakeSetValid(true);
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800305 ASSERT_TRUE(elf.StepIfSignalHandler(0x3000 + load_bias, &regs, &process_memory));
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700306 EXPECT_EQ(ERROR_NONE, elf.GetLastErrorCode());
Christopher Ferrise69f4702017-10-19 16:08:58 -0700307 EXPECT_EQ(15U, regs.pc());
308 EXPECT_EQ(13U, regs.sp());
309}
310
Christopher Ferrisf0c82e72019-12-04 13:37:11 -0800311TEST_F(ElfTest, step_in_signal_map) {
312 VerifyStepIfSignalHandler(0);
313}
314
315TEST_F(ElfTest, step_in_signal_map_non_zero_load_bias) {
316 VerifyStepIfSignalHandler(0x1000);
317}
318
Christopher Ferrise69f4702017-10-19 16:08:58 -0700319class ElfInterfaceMock : public ElfInterface {
320 public:
321 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
322 virtual ~ElfInterfaceMock() = default;
323
Christopher Ferris819f1312019-10-03 13:35:48 -0700324 bool Init(int64_t*) override { return false; }
325 void InitHeaders() override {}
Christopher Ferris02a6c442019-03-11 14:43:33 -0700326 std::string GetSoname() override { return ""; }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700327 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800328 std::string GetBuildID() override { return ""; }
Christopher Ferris150db122017-12-20 18:49:01 -0800329
Christopher Ferrisbaf058b2019-10-11 14:26:55 -0700330 MOCK_METHOD(bool, Step, (uint64_t, Regs*, Memory*, bool*), (override));
331 MOCK_METHOD(bool, GetGlobalVariable, (const std::string&, uint64_t*), (override));
332 MOCK_METHOD(bool, IsValidPc, (uint64_t), (override));
Christopher Ferris150db122017-12-20 18:49:01 -0800333
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800334 void MockSetDataOffset(uint64_t offset) { data_offset_ = offset; }
335 void MockSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; }
336 void MockSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; }
337
Christopher Ferris150db122017-12-20 18:49:01 -0800338 void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800339 void MockSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; }
340 void MockSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700341};
342
343TEST_F(ElfTest, step_in_interface) {
344 ElfFake elf(memory_);
345 elf.FakeSetValid(true);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700346
347 RegsArm regs;
348
349 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
350 elf.FakeSetInterface(interface);
351 MemoryFake process_memory;
352
353 bool finished;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700354 EXPECT_CALL(*interface, Step(0x1000, &regs, &process_memory, &finished))
Christopher Ferrise69f4702017-10-19 16:08:58 -0700355 .WillOnce(::testing::Return(true));
356
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700357 ASSERT_TRUE(elf.Step(0x1000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700358}
359
Christopher Ferris150db122017-12-20 18:49:01 -0800360TEST_F(ElfTest, get_global_invalid_elf) {
361 ElfFake elf(memory_);
362 elf.FakeSetValid(false);
363
364 std::string global("something");
365 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800366 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800367}
368
369TEST_F(ElfTest, get_global_valid_not_in_interface) {
370 ElfFake elf(memory_);
371 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800372
373 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
374 elf.FakeSetInterface(interface);
375
Christopher Ferris150db122017-12-20 18:49:01 -0800376 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800377 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
378 .WillOnce(::testing::Return(false));
Christopher Ferris150db122017-12-20 18:49:01 -0800379
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800380 uint64_t offset;
381 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800382}
383
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800384TEST_F(ElfTest, get_global_vaddr_in_no_sections) {
Christopher Ferris150db122017-12-20 18:49:01 -0800385 ElfFake elf(memory_);
386 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800387
388 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
389 elf.FakeSetInterface(interface);
390
Christopher Ferris150db122017-12-20 18:49:01 -0800391 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800392 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
Christopher Ferris150db122017-12-20 18:49:01 -0800393 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
394
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700395 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800396 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700397}
398
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800399TEST_F(ElfTest, get_global_vaddr_in_data_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800400 ElfFake elf(memory_);
401 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800402
403 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
404 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800405 interface->MockSetDataVaddrStart(0x500);
406 interface->MockSetDataVaddrEnd(0x600);
407 interface->MockSetDataOffset(0xa000);
Christopher Ferris150db122017-12-20 18:49:01 -0800408
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800409 std::string global("something");
410 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
411 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x580), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800412
413 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800414 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
415 EXPECT_EQ(0xa080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800416}
417
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800418TEST_F(ElfTest, get_global_vaddr_in_dynamic_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800419 ElfFake elf(memory_);
420 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800421
422 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
423 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800424 interface->MockSetDataVaddrStart(0x500);
425 interface->MockSetDataVaddrEnd(0x600);
426 interface->MockSetDataOffset(0xa000);
427
428 interface->MockSetDynamicVaddrStart(0x800);
429 interface->MockSetDynamicVaddrEnd(0x900);
430 interface->MockSetDynamicOffset(0xc000);
431
432 std::string global("something");
433 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
434 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x880), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800435
436 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800437 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
438 EXPECT_EQ(0xc080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800439}
440
441TEST_F(ElfTest, is_valid_pc_elf_invalid) {
442 ElfFake elf(memory_);
443 elf.FakeSetValid(false);
Christopher Ferris150db122017-12-20 18:49:01 -0800444
445 EXPECT_FALSE(elf.IsValidPc(0x100));
446 EXPECT_FALSE(elf.IsValidPc(0x200));
447}
448
449TEST_F(ElfTest, is_valid_pc_interface) {
450 ElfFake elf(memory_);
451 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800452
453 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
454 elf.FakeSetInterface(interface);
455
456 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
457
458 EXPECT_TRUE(elf.IsValidPc(0x1500));
459}
460
Christopher Ferris150db122017-12-20 18:49:01 -0800461TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
462 ElfFake elf(memory_);
463 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800464
465 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
466 elf.FakeSetInterface(interface);
467 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
468 elf.FakeSetGnuDebugdataInterface(gnu_interface);
469
470 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
471 EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
472
473 EXPECT_TRUE(elf.IsValidPc(0x1500));
474}
475
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800476TEST_F(ElfTest, error_code_not_valid) {
477 ElfFake elf(memory_);
478 elf.FakeSetValid(false);
479
480 ErrorData error{ERROR_MEMORY_INVALID, 0x100};
481 elf.GetLastError(&error);
482 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
483 EXPECT_EQ(0x100U, error.address);
484}
485
486TEST_F(ElfTest, error_code_valid) {
487 ElfFake elf(memory_);
488 elf.FakeSetValid(true);
489 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
490 elf.FakeSetInterface(interface);
491 interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
492 interface->FakeSetErrorAddress(0x1000);
493
494 ErrorData error{ERROR_NONE, 0};
495 elf.GetLastError(&error);
496 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
497 EXPECT_EQ(0x1000U, error.address);
498 EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
499 EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
500}
501
Christopher Ferrisd226a512017-07-14 10:37:19 -0700502} // namespace unwindstack