blob: d227b60bdbbcf2cac0e8bc39580d8ed520c9d427 [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
107 MemoryFake* memory_;
108};
109
110TEST_F(ElfTest, invalid_memory) {
111 Elf elf(memory_);
112
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700113 ASSERT_FALSE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800114 ASSERT_FALSE(elf.valid());
115}
116
117TEST_F(ElfTest, elf_invalid) {
118 Elf elf(memory_);
119
120 InitElf32(EM_386);
121
122 // Corrupt the ELF signature.
123 memory_->SetData32(0, 0x7f000000);
124
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700125 ASSERT_FALSE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800126 ASSERT_FALSE(elf.valid());
127 ASSERT_TRUE(elf.interface() == nullptr);
128
Christopher Ferris02a6c442019-03-11 14:43:33 -0700129 ASSERT_EQ("", elf.GetSoname());
Christopher Ferris3958f802017-02-01 15:44:40 -0800130
Christopher Ferris02a6c442019-03-11 14:43:33 -0700131 std::string name;
Christopher Ferris3958f802017-02-01 15:44:40 -0800132 uint64_t func_offset;
133 ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
134
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700135 ASSERT_FALSE(elf.StepIfSignalHandler(0, nullptr, nullptr));
136 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
137
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700138 bool finished;
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700139 ASSERT_FALSE(elf.Step(0, nullptr, nullptr, &finished));
140 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
Christopher Ferris3958f802017-02-01 15:44:40 -0800141}
142
Christopher Ferrisa0196652017-07-18 16:09:20 -0700143TEST_F(ElfTest, elf32_invalid_machine) {
144 Elf elf(memory_);
145
146 InitElf32(EM_PPC);
147
148 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700149 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700150
151 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100152 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 -0700153 GetFakeLogPrint());
154}
155
156TEST_F(ElfTest, elf64_invalid_machine) {
157 Elf elf(memory_);
158
159 InitElf64(EM_PPC64);
160
161 ResetLogs();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700162 ASSERT_FALSE(elf.Init());
Christopher Ferrisa0196652017-07-18 16:09:20 -0700163
164 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100165 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 -0700166 GetFakeLogPrint());
167}
168
Christopher Ferris3958f802017-02-01 15:44:40 -0800169TEST_F(ElfTest, elf_arm) {
170 Elf elf(memory_);
171
172 InitElf32(EM_ARM);
173
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700174 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800175 ASSERT_TRUE(elf.valid());
176 ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
177 ASSERT_EQ(ELFCLASS32, elf.class_type());
178 ASSERT_TRUE(elf.interface() != nullptr);
179}
180
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100181TEST_F(ElfTest, elf_mips) {
182 Elf elf(memory_);
183
184 InitElf32(EM_MIPS);
185
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700186 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100187 ASSERT_TRUE(elf.valid());
188 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
189 ASSERT_EQ(ELFCLASS32, elf.class_type());
190 ASSERT_TRUE(elf.interface() != nullptr);
191}
192
Christopher Ferris3958f802017-02-01 15:44:40 -0800193TEST_F(ElfTest, elf_x86) {
194 Elf elf(memory_);
195
196 InitElf32(EM_386);
197
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700198 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800199 ASSERT_TRUE(elf.valid());
200 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
201 ASSERT_EQ(ELFCLASS32, elf.class_type());
202 ASSERT_TRUE(elf.interface() != nullptr);
203}
204
205TEST_F(ElfTest, elf_arm64) {
206 Elf elf(memory_);
207
208 InitElf64(EM_AARCH64);
209
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700210 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800211 ASSERT_TRUE(elf.valid());
212 ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
213 ASSERT_EQ(ELFCLASS64, elf.class_type());
214 ASSERT_TRUE(elf.interface() != nullptr);
215}
216
217TEST_F(ElfTest, elf_x86_64) {
218 Elf elf(memory_);
219
220 InitElf64(EM_X86_64);
221
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700222 ASSERT_TRUE(elf.Init());
Christopher Ferris3958f802017-02-01 15:44:40 -0800223 ASSERT_TRUE(elf.valid());
224 ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
225 ASSERT_EQ(ELFCLASS64, elf.class_type());
226 ASSERT_TRUE(elf.interface() != nullptr);
227}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700228
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100229TEST_F(ElfTest, elf_mips64) {
230 Elf elf(memory_);
231
232 InitElf64(EM_MIPS);
233
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700234 ASSERT_TRUE(elf.Init());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100235 ASSERT_TRUE(elf.valid());
236 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
237 ASSERT_EQ(ELFCLASS64, elf.class_type());
238 ASSERT_TRUE(elf.interface() != nullptr);
239}
240
Christopher Ferris570b76f2017-06-30 17:18:16 -0700241TEST_F(ElfTest, gnu_debugdata_init32) {
242 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
243 [&](uint64_t offset, const void* ptr, size_t size) {
244 memory_->SetMemory(offset, ptr, size);
245 });
246
247 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700248 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700249 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700250 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700251 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
252 EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700253}
254
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700255TEST_F(ElfTest, gnu_debugdata_init64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700256 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
257 [&](uint64_t offset, const void* ptr, size_t size) {
258 memory_->SetMemory(offset, ptr, size);
259 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700260
Christopher Ferris570b76f2017-06-30 17:18:16 -0700261 Elf elf(memory_);
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700262 ASSERT_TRUE(elf.Init());
Christopher Ferris570b76f2017-06-30 17:18:16 -0700263 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700264 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700265 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
266 EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700267}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700268
Christopher Ferrisd226a512017-07-14 10:37:19 -0700269TEST_F(ElfTest, rel_pc) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700270 ElfFake elf(memory_);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700271
Christopher Ferrise69f4702017-10-19 16:08:58 -0700272 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
273 elf.FakeSetInterface(interface);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700274
Christopher Ferrise69f4702017-10-19 16:08:58 -0700275 elf.FakeSetValid(true);
Christopher Ferrisa09c4a62018-12-13 16:08:50 -0800276 MapInfo map_info(nullptr, 0x1000, 0x2000, 0, 0, "");
Christopher Ferrisd226a512017-07-14 10:37:19 -0700277
278 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
279
Christopher Ferrise69f4702017-10-19 16:08:58 -0700280 elf.FakeSetValid(false);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700281 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
282}
283
Christopher Ferrise69f4702017-10-19 16:08:58 -0700284TEST_F(ElfTest, step_in_signal_map) {
285 ElfFake elf(memory_);
286
287 RegsArm regs;
288 regs[13] = 0x50000;
289 regs[15] = 0x8000;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700290
291 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
292 elf.FakeSetInterface(interface);
293
294 memory_->SetData32(0x3000, 0xdf0027ad);
295 MemoryFake process_memory;
296 process_memory.SetData32(0x50000, 0);
297 for (size_t i = 0; i < 16; i++) {
298 process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
299 }
300
301 elf.FakeSetValid(true);
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700302 ASSERT_TRUE(elf.StepIfSignalHandler(0x3000, &regs, &process_memory));
303 EXPECT_EQ(ERROR_NONE, elf.GetLastErrorCode());
Christopher Ferrise69f4702017-10-19 16:08:58 -0700304 EXPECT_EQ(15U, regs.pc());
305 EXPECT_EQ(13U, regs.sp());
306}
307
308class ElfInterfaceMock : public ElfInterface {
309 public:
310 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
311 virtual ~ElfInterfaceMock() = default;
312
Christopher Ferris819f1312019-10-03 13:35:48 -0700313 bool Init(int64_t*) override { return false; }
314 void InitHeaders() override {}
Christopher Ferris02a6c442019-03-11 14:43:33 -0700315 std::string GetSoname() override { return ""; }
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700316 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800317 std::string GetBuildID() override { return ""; }
Christopher Ferris150db122017-12-20 18:49:01 -0800318
Christopher Ferrisbaf058b2019-10-11 14:26:55 -0700319 MOCK_METHOD(bool, Step, (uint64_t, Regs*, Memory*, bool*), (override));
320 MOCK_METHOD(bool, GetGlobalVariable, (const std::string&, uint64_t*), (override));
321 MOCK_METHOD(bool, IsValidPc, (uint64_t), (override));
Christopher Ferris150db122017-12-20 18:49:01 -0800322
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800323 void MockSetDataOffset(uint64_t offset) { data_offset_ = offset; }
324 void MockSetDataVaddrStart(uint64_t vaddr) { data_vaddr_start_ = vaddr; }
325 void MockSetDataVaddrEnd(uint64_t vaddr) { data_vaddr_end_ = vaddr; }
326
Christopher Ferris150db122017-12-20 18:49:01 -0800327 void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800328 void MockSetDynamicVaddrStart(uint64_t vaddr) { dynamic_vaddr_start_ = vaddr; }
329 void MockSetDynamicVaddrEnd(uint64_t vaddr) { dynamic_vaddr_end_ = vaddr; }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700330};
331
332TEST_F(ElfTest, step_in_interface) {
333 ElfFake elf(memory_);
334 elf.FakeSetValid(true);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700335
336 RegsArm regs;
337
338 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
339 elf.FakeSetInterface(interface);
340 MemoryFake process_memory;
341
342 bool finished;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700343 EXPECT_CALL(*interface, Step(0x1000, &regs, &process_memory, &finished))
Christopher Ferrise69f4702017-10-19 16:08:58 -0700344 .WillOnce(::testing::Return(true));
345
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700346 ASSERT_TRUE(elf.Step(0x1000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700347}
348
Christopher Ferris150db122017-12-20 18:49:01 -0800349TEST_F(ElfTest, get_global_invalid_elf) {
350 ElfFake elf(memory_);
351 elf.FakeSetValid(false);
352
353 std::string global("something");
354 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800355 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800356}
357
358TEST_F(ElfTest, get_global_valid_not_in_interface) {
359 ElfFake elf(memory_);
360 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800361
362 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
363 elf.FakeSetInterface(interface);
364
Christopher Ferris150db122017-12-20 18:49:01 -0800365 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800366 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
367 .WillOnce(::testing::Return(false));
Christopher Ferris150db122017-12-20 18:49:01 -0800368
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800369 uint64_t offset;
370 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris150db122017-12-20 18:49:01 -0800371}
372
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800373TEST_F(ElfTest, get_global_vaddr_in_no_sections) {
Christopher Ferris150db122017-12-20 18:49:01 -0800374 ElfFake elf(memory_);
375 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800376
377 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
378 elf.FakeSetInterface(interface);
379
Christopher Ferris150db122017-12-20 18:49:01 -0800380 std::string global("something");
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800381 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
Christopher Ferris150db122017-12-20 18:49:01 -0800382 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
383
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700384 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800385 ASSERT_FALSE(elf.GetGlobalVariableOffset(global, &offset));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700386}
387
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800388TEST_F(ElfTest, get_global_vaddr_in_data_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800389 ElfFake elf(memory_);
390 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800391
392 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
393 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800394 interface->MockSetDataVaddrStart(0x500);
395 interface->MockSetDataVaddrEnd(0x600);
396 interface->MockSetDataOffset(0xa000);
Christopher Ferris150db122017-12-20 18:49:01 -0800397
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800398 std::string global("something");
399 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
400 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x580), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800401
402 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800403 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
404 EXPECT_EQ(0xa080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800405}
406
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800407TEST_F(ElfTest, get_global_vaddr_in_dynamic_section) {
Christopher Ferris150db122017-12-20 18:49:01 -0800408 ElfFake elf(memory_);
409 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800410
411 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
412 elf.FakeSetInterface(interface);
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800413 interface->MockSetDataVaddrStart(0x500);
414 interface->MockSetDataVaddrEnd(0x600);
415 interface->MockSetDataOffset(0xa000);
416
417 interface->MockSetDynamicVaddrStart(0x800);
418 interface->MockSetDynamicVaddrEnd(0x900);
419 interface->MockSetDynamicOffset(0xc000);
420
421 std::string global("something");
422 EXPECT_CALL(*interface, GetGlobalVariable(global, ::testing::_))
423 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x880), ::testing::Return(true)));
Christopher Ferris150db122017-12-20 18:49:01 -0800424
425 uint64_t offset;
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800426 ASSERT_TRUE(elf.GetGlobalVariableOffset(global, &offset));
427 EXPECT_EQ(0xc080U, offset);
Christopher Ferris150db122017-12-20 18:49:01 -0800428}
429
430TEST_F(ElfTest, is_valid_pc_elf_invalid) {
431 ElfFake elf(memory_);
432 elf.FakeSetValid(false);
Christopher Ferris150db122017-12-20 18:49:01 -0800433
434 EXPECT_FALSE(elf.IsValidPc(0x100));
435 EXPECT_FALSE(elf.IsValidPc(0x200));
436}
437
438TEST_F(ElfTest, is_valid_pc_interface) {
439 ElfFake elf(memory_);
440 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800441
442 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
443 elf.FakeSetInterface(interface);
444
445 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
446
447 EXPECT_TRUE(elf.IsValidPc(0x1500));
448}
449
Christopher Ferris150db122017-12-20 18:49:01 -0800450TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
451 ElfFake elf(memory_);
452 elf.FakeSetValid(true);
Christopher Ferris150db122017-12-20 18:49:01 -0800453
454 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
455 elf.FakeSetInterface(interface);
456 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
457 elf.FakeSetGnuDebugdataInterface(gnu_interface);
458
459 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
460 EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
461
462 EXPECT_TRUE(elf.IsValidPc(0x1500));
463}
464
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800465TEST_F(ElfTest, error_code_not_valid) {
466 ElfFake elf(memory_);
467 elf.FakeSetValid(false);
468
469 ErrorData error{ERROR_MEMORY_INVALID, 0x100};
470 elf.GetLastError(&error);
471 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
472 EXPECT_EQ(0x100U, error.address);
473}
474
475TEST_F(ElfTest, error_code_valid) {
476 ElfFake elf(memory_);
477 elf.FakeSetValid(true);
478 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
479 elf.FakeSetInterface(interface);
480 interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
481 interface->FakeSetErrorAddress(0x1000);
482
483 ErrorData error{ERROR_NONE, 0};
484 elf.GetLastError(&error);
485 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
486 EXPECT_EQ(0x1000U, error.address);
487 EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
488 EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
489}
490
Christopher Ferrisd226a512017-07-14 10:37:19 -0700491} // namespace unwindstack