blob: eb850336b2d3f8a68134b5e92de69f6d6db81710 [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 Ferrise69f4702017-10-19 16:08:58 -0700113 ASSERT_FALSE(elf.Init(false));
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 Ferrise69f4702017-10-19 16:08:58 -0700125 ASSERT_FALSE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800126 ASSERT_FALSE(elf.valid());
127 ASSERT_TRUE(elf.interface() == nullptr);
128
129 std::string name;
130 ASSERT_FALSE(elf.GetSoname(&name));
131
132 uint64_t func_offset;
133 ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
134
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700135 bool finished;
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800136 ASSERT_FALSE(elf.Step(0, 0, 0, nullptr, nullptr, &finished));
Christopher Ferris3958f802017-02-01 15:44:40 -0800137}
138
Christopher Ferrisa0196652017-07-18 16:09:20 -0700139TEST_F(ElfTest, elf32_invalid_machine) {
140 Elf elf(memory_);
141
142 InitElf32(EM_PPC);
143
144 ResetLogs();
Christopher Ferrise69f4702017-10-19 16:08:58 -0700145 ASSERT_FALSE(elf.Init(false));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700146
147 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100148 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 -0700149 GetFakeLogPrint());
150}
151
152TEST_F(ElfTest, elf64_invalid_machine) {
153 Elf elf(memory_);
154
155 InitElf64(EM_PPC64);
156
157 ResetLogs();
Christopher Ferrise69f4702017-10-19 16:08:58 -0700158 ASSERT_FALSE(elf.Init(false));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700159
160 ASSERT_EQ("", GetFakeLogBuf());
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100161 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 -0700162 GetFakeLogPrint());
163}
164
Christopher Ferris3958f802017-02-01 15:44:40 -0800165TEST_F(ElfTest, elf_arm) {
166 Elf elf(memory_);
167
168 InitElf32(EM_ARM);
169
Christopher Ferrise69f4702017-10-19 16:08:58 -0700170 ASSERT_TRUE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800171 ASSERT_TRUE(elf.valid());
172 ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
173 ASSERT_EQ(ELFCLASS32, elf.class_type());
174 ASSERT_TRUE(elf.interface() != nullptr);
175}
176
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100177TEST_F(ElfTest, elf_mips) {
178 Elf elf(memory_);
179
180 InitElf32(EM_MIPS);
181
182 ASSERT_TRUE(elf.Init(false));
183 ASSERT_TRUE(elf.valid());
184 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
185 ASSERT_EQ(ELFCLASS32, elf.class_type());
186 ASSERT_TRUE(elf.interface() != nullptr);
187}
188
Christopher Ferris3958f802017-02-01 15:44:40 -0800189TEST_F(ElfTest, elf_x86) {
190 Elf elf(memory_);
191
192 InitElf32(EM_386);
193
Christopher Ferrise69f4702017-10-19 16:08:58 -0700194 ASSERT_TRUE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800195 ASSERT_TRUE(elf.valid());
196 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
197 ASSERT_EQ(ELFCLASS32, elf.class_type());
198 ASSERT_TRUE(elf.interface() != nullptr);
199}
200
201TEST_F(ElfTest, elf_arm64) {
202 Elf elf(memory_);
203
204 InitElf64(EM_AARCH64);
205
Christopher Ferrise69f4702017-10-19 16:08:58 -0700206 ASSERT_TRUE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800207 ASSERT_TRUE(elf.valid());
208 ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
209 ASSERT_EQ(ELFCLASS64, elf.class_type());
210 ASSERT_TRUE(elf.interface() != nullptr);
211}
212
213TEST_F(ElfTest, elf_x86_64) {
214 Elf elf(memory_);
215
216 InitElf64(EM_X86_64);
217
Christopher Ferrise69f4702017-10-19 16:08:58 -0700218 ASSERT_TRUE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800219 ASSERT_TRUE(elf.valid());
220 ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
221 ASSERT_EQ(ELFCLASS64, elf.class_type());
222 ASSERT_TRUE(elf.interface() != nullptr);
223}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700224
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100225TEST_F(ElfTest, elf_mips64) {
226 Elf elf(memory_);
227
228 InitElf64(EM_MIPS);
229
230 ASSERT_TRUE(elf.Init(false));
231 ASSERT_TRUE(elf.valid());
232 ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
233 ASSERT_EQ(ELFCLASS64, elf.class_type());
234 ASSERT_TRUE(elf.interface() != nullptr);
235}
236
Christopher Ferris570b76f2017-06-30 17:18:16 -0700237TEST_F(ElfTest, gnu_debugdata_init_fail32) {
238 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, false,
239 [&](uint64_t offset, const void* ptr, size_t size) {
240 memory_->SetMemory(offset, ptr, size);
241 });
242
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700243 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700244 ASSERT_TRUE(elf.Init(false));
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700245 ASSERT_TRUE(elf.interface() != nullptr);
246 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700247 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
248 EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700249}
250
251TEST_F(ElfTest, gnu_debugdata_init_fail64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700252 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, false,
253 [&](uint64_t offset, const void* ptr, size_t size) {
254 memory_->SetMemory(offset, ptr, size);
255 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700256
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700257 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700258 ASSERT_TRUE(elf.Init(false));
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700259 ASSERT_TRUE(elf.interface() != nullptr);
260 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700261 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
262 EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
263}
264
265TEST_F(ElfTest, gnu_debugdata_init32) {
266 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
267 [&](uint64_t offset, const void* ptr, size_t size) {
268 memory_->SetMemory(offset, ptr, size);
269 });
270
271 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700272 ASSERT_TRUE(elf.Init(true));
Christopher Ferris570b76f2017-06-30 17:18:16 -0700273 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700274 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700275 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
276 EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700277}
278
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700279TEST_F(ElfTest, gnu_debugdata_init64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700280 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
281 [&](uint64_t offset, const void* ptr, size_t size) {
282 memory_->SetMemory(offset, ptr, size);
283 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700284
Christopher Ferris570b76f2017-06-30 17:18:16 -0700285 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700286 ASSERT_TRUE(elf.Init(true));
Christopher Ferris570b76f2017-06-30 17:18:16 -0700287 ASSERT_TRUE(elf.interface() != nullptr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700288 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700289 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
290 EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700291}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700292
Christopher Ferrisd226a512017-07-14 10:37:19 -0700293TEST_F(ElfTest, rel_pc) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700294 ElfFake elf(memory_);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700295
Christopher Ferrise69f4702017-10-19 16:08:58 -0700296 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
297 elf.FakeSetInterface(interface);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700298
Christopher Ferrise69f4702017-10-19 16:08:58 -0700299 elf.FakeSetValid(true);
300 elf.FakeSetLoadBias(0);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800301 MapInfo map_info(0x1000, 0x2000);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700302
303 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
304
Christopher Ferrise69f4702017-10-19 16:08:58 -0700305 elf.FakeSetLoadBias(0x3000);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700306 ASSERT_EQ(0x3101U, elf.GetRelPc(0x1101, &map_info));
307
Christopher Ferrise69f4702017-10-19 16:08:58 -0700308 elf.FakeSetValid(false);
309 elf.FakeSetLoadBias(0);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700310 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
311}
312
Christopher Ferrise69f4702017-10-19 16:08:58 -0700313TEST_F(ElfTest, step_in_signal_map) {
314 ElfFake elf(memory_);
315
316 RegsArm regs;
317 regs[13] = 0x50000;
318 regs[15] = 0x8000;
319 regs.SetFromRaw();
320
321 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
322 elf.FakeSetInterface(interface);
323
324 memory_->SetData32(0x3000, 0xdf0027ad);
325 MemoryFake process_memory;
326 process_memory.SetData32(0x50000, 0);
327 for (size_t i = 0; i < 16; i++) {
328 process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
329 }
330
331 elf.FakeSetValid(true);
332 elf.FakeSetLoadBias(0);
333 bool finished;
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800334 ASSERT_TRUE(elf.Step(0x1000, 0x1000, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700335 EXPECT_FALSE(finished);
336 EXPECT_EQ(15U, regs.pc());
337 EXPECT_EQ(13U, regs.sp());
338}
339
340class ElfInterfaceMock : public ElfInterface {
341 public:
342 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
343 virtual ~ElfInterfaceMock() = default;
344
345 bool Init(uint64_t*) override { return false; }
346 void InitHeaders() override {}
347 bool GetSoname(std::string*) override { return false; }
348 bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override { return false; }
Christopher Ferris150db122017-12-20 18:49:01 -0800349
Christopher Ferrise7b66242017-12-15 11:17:45 -0800350 MOCK_METHOD5(Step, bool(uint64_t, uint64_t, Regs*, Memory*, bool*));
Christopher Ferris150db122017-12-20 18:49:01 -0800351 MOCK_METHOD2(GetGlobalVariable, bool(const std::string&, uint64_t*));
352 MOCK_METHOD1(IsValidPc, bool(uint64_t));
353
354 void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
355 void MockSetDynamicVaddr(uint64_t vaddr) { dynamic_vaddr_ = vaddr; }
356 void MockSetDynamicSize(uint64_t size) { dynamic_size_ = size; }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700357};
358
359TEST_F(ElfTest, step_in_interface) {
360 ElfFake elf(memory_);
361 elf.FakeSetValid(true);
362 elf.FakeSetLoadBias(0);
363
364 RegsArm regs;
365
366 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
367 elf.FakeSetInterface(interface);
368 MemoryFake process_memory;
369
370 bool finished;
Christopher Ferrise7b66242017-12-15 11:17:45 -0800371 EXPECT_CALL(*interface, Step(0x1000, 0, &regs, &process_memory, &finished))
Christopher Ferrise69f4702017-10-19 16:08:58 -0700372 .WillOnce(::testing::Return(true));
373
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800374 ASSERT_TRUE(elf.Step(0x1004, 0x1000, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700375}
376
377TEST_F(ElfTest, step_in_interface_non_zero_load_bias) {
378 ElfFake elf(memory_);
379 elf.FakeSetValid(true);
380 elf.FakeSetLoadBias(0x4000);
381
382 RegsArm regs;
383
384 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
385 elf.FakeSetInterface(interface);
386 MemoryFake process_memory;
387
Christopher Ferrise69f4702017-10-19 16:08:58 -0700388 bool finished;
Christopher Ferrise7b66242017-12-15 11:17:45 -0800389 EXPECT_CALL(*interface, Step(0x7300, 0x4000, &regs, &process_memory, &finished))
Christopher Ferrise69f4702017-10-19 16:08:58 -0700390 .WillOnce(::testing::Return(true));
391
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800392 ASSERT_TRUE(elf.Step(0x7304, 0x7300, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700393}
394
Christopher Ferris150db122017-12-20 18:49:01 -0800395TEST_F(ElfTest, get_global_invalid_elf) {
396 ElfFake elf(memory_);
397 elf.FakeSetValid(false);
398
399 std::string global("something");
400 uint64_t offset;
401 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
402}
403
404TEST_F(ElfTest, get_global_valid_not_in_interface) {
405 ElfFake elf(memory_);
406 elf.FakeSetValid(true);
407 elf.FakeSetLoadBias(0);
408
409 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
410 elf.FakeSetInterface(interface);
411
412 uint64_t offset;
413 std::string global("something");
414 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
415
416 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
417}
418
419TEST_F(ElfTest, get_global_valid_below_load_bias) {
420 ElfFake elf(memory_);
421 elf.FakeSetValid(true);
422 elf.FakeSetLoadBias(0x1000);
423
424 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
425 elf.FakeSetInterface(interface);
426
427 uint64_t offset;
428 std::string global("something");
429 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
430 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
431
432 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
433}
434
435TEST_F(ElfTest, get_global_valid_dynamic_zero) {
436 ElfFake elf(memory_);
437 elf.FakeSetValid(true);
438 elf.FakeSetLoadBias(0);
439
440 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
441 elf.FakeSetInterface(interface);
442
443 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
444 elf.FakeSetGnuDebugdataInterface(gnu_interface);
445
446 uint64_t offset;
447 std::string global("something");
448 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
449
450 EXPECT_CALL(*gnu_interface, GetGlobalVariable(global, &offset))
451 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x500), ::testing::Return(true)));
452
453 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
454 EXPECT_EQ(0x500U, offset);
455}
456
457TEST_F(ElfTest, get_global_valid_in_gnu_debugdata_dynamic_zero) {
458 ElfFake elf(memory_);
459 elf.FakeSetValid(true);
460 elf.FakeSetLoadBias(0);
461
462 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
463 elf.FakeSetInterface(interface);
464
465 uint64_t offset;
466 std::string global("something");
467 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
468 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
469
470 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
471 EXPECT_EQ(0x300U, offset);
472}
473
474TEST_F(ElfTest, get_global_valid_dynamic_zero_non_zero_load_bias) {
475 ElfFake elf(memory_);
476 elf.FakeSetValid(true);
477 elf.FakeSetLoadBias(0x100);
478
479 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
480 elf.FakeSetInterface(interface);
481
482 uint64_t offset;
483 std::string global("something");
484 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
485 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
486
487 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
488 EXPECT_EQ(0x200U, offset);
489}
490
491TEST_F(ElfTest, get_global_valid_dynamic_adjust_negative) {
492 ElfFake elf(memory_);
493 elf.FakeSetValid(true);
494 elf.FakeSetLoadBias(0);
495
496 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
497 interface->MockSetDynamicOffset(0x400);
498 interface->MockSetDynamicVaddr(0x800);
499 interface->MockSetDynamicSize(0x100);
500 elf.FakeSetInterface(interface);
501
502 uint64_t offset;
503 std::string global("something");
504 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
505 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
506
507 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
508 EXPECT_EQ(0x450U, offset);
509}
510
511TEST_F(ElfTest, get_global_valid_dynamic_adjust_positive) {
512 ElfFake elf(memory_);
513 elf.FakeSetValid(true);
514 elf.FakeSetLoadBias(0);
515
516 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
517 interface->MockSetDynamicOffset(0x1000);
518 interface->MockSetDynamicVaddr(0x800);
519 interface->MockSetDynamicSize(0x100);
520 elf.FakeSetInterface(interface);
521
522 uint64_t offset;
523 std::string global("something");
524 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
525 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
526
527 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
528 EXPECT_EQ(0x1050U, offset);
529}
530
531TEST_F(ElfTest, is_valid_pc_elf_invalid) {
532 ElfFake elf(memory_);
533 elf.FakeSetValid(false);
534 elf.FakeSetLoadBias(0);
535
536 EXPECT_FALSE(elf.IsValidPc(0x100));
537 EXPECT_FALSE(elf.IsValidPc(0x200));
538}
539
540TEST_F(ElfTest, is_valid_pc_interface) {
541 ElfFake elf(memory_);
542 elf.FakeSetValid(true);
543 elf.FakeSetLoadBias(0);
544
545 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
546 elf.FakeSetInterface(interface);
547
548 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
549
550 EXPECT_TRUE(elf.IsValidPc(0x1500));
551}
552
553TEST_F(ElfTest, is_valid_pc_non_zero_load_bias) {
554 ElfFake elf(memory_);
555 elf.FakeSetValid(true);
556 elf.FakeSetLoadBias(0x1000);
557
558 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
559 elf.FakeSetInterface(interface);
560
561 EXPECT_CALL(*interface, IsValidPc(0x500)).WillOnce(::testing::Return(true));
562
563 EXPECT_FALSE(elf.IsValidPc(0x100));
564 EXPECT_FALSE(elf.IsValidPc(0x200));
565 EXPECT_TRUE(elf.IsValidPc(0x1500));
566}
567
568TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
569 ElfFake elf(memory_);
570 elf.FakeSetValid(true);
571 elf.FakeSetLoadBias(0);
572
573 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
574 elf.FakeSetInterface(interface);
575 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
576 elf.FakeSetGnuDebugdataInterface(gnu_interface);
577
578 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
579 EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
580
581 EXPECT_TRUE(elf.IsValidPc(0x1500));
582}
583
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800584TEST_F(ElfTest, error_code_not_valid) {
585 ElfFake elf(memory_);
586 elf.FakeSetValid(false);
587
588 ErrorData error{ERROR_MEMORY_INVALID, 0x100};
589 elf.GetLastError(&error);
590 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
591 EXPECT_EQ(0x100U, error.address);
592}
593
594TEST_F(ElfTest, error_code_valid) {
595 ElfFake elf(memory_);
596 elf.FakeSetValid(true);
597 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
598 elf.FakeSetInterface(interface);
599 interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
600 interface->FakeSetErrorAddress(0x1000);
601
602 ErrorData error{ERROR_NONE, 0};
603 elf.GetLastError(&error);
604 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
605 EXPECT_EQ(0x1000U, error.address);
606 EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
607 EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
608}
609
Christopher Ferrisd226a512017-07-14 10:37:19 -0700610} // namespace unwindstack