blob: 00192f1a4d90d7e4a88e90201fbe5e1d95af7813 [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());
148 ASSERT_EQ("4 unwind 32 bit elf that is neither arm nor x86: e_machine = 20\n\n",
149 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());
161 ASSERT_EQ("4 unwind 64 bit elf that is neither aarch64 nor x86_64: e_machine = 21\n\n",
162 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
177TEST_F(ElfTest, elf_x86) {
178 Elf elf(memory_);
179
180 InitElf32(EM_386);
181
Christopher Ferrise69f4702017-10-19 16:08:58 -0700182 ASSERT_TRUE(elf.Init(false));
Christopher Ferris3958f802017-02-01 15:44:40 -0800183 ASSERT_TRUE(elf.valid());
184 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
185 ASSERT_EQ(ELFCLASS32, elf.class_type());
186 ASSERT_TRUE(elf.interface() != nullptr);
187}
188
189TEST_F(ElfTest, elf_arm64) {
190 Elf elf(memory_);
191
192 InitElf64(EM_AARCH64);
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_AARCH64), elf.machine_type());
197 ASSERT_EQ(ELFCLASS64, elf.class_type());
198 ASSERT_TRUE(elf.interface() != nullptr);
199}
200
201TEST_F(ElfTest, elf_x86_64) {
202 Elf elf(memory_);
203
204 InitElf64(EM_X86_64);
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_X86_64), elf.machine_type());
209 ASSERT_EQ(ELFCLASS64, elf.class_type());
210 ASSERT_TRUE(elf.interface() != nullptr);
211}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700212
Christopher Ferris570b76f2017-06-30 17:18:16 -0700213TEST_F(ElfTest, gnu_debugdata_init_fail32) {
214 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, false,
215 [&](uint64_t offset, const void* ptr, size_t size) {
216 memory_->SetMemory(offset, ptr, size);
217 });
218
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700219 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700220 ASSERT_TRUE(elf.Init(false));
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700221 ASSERT_TRUE(elf.interface() != nullptr);
222 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700223 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
224 EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700225}
226
227TEST_F(ElfTest, gnu_debugdata_init_fail64) {
Christopher Ferris570b76f2017-06-30 17:18:16 -0700228 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, false,
229 [&](uint64_t offset, const void* ptr, size_t size) {
230 memory_->SetMemory(offset, ptr, size);
231 });
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700232
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700233 Elf elf(memory_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700234 ASSERT_TRUE(elf.Init(false));
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700235 ASSERT_TRUE(elf.interface() != nullptr);
236 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700237 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
238 EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
239}
240
241TEST_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 Ferrise69f4702017-10-19 16:08:58 -0700248 ASSERT_TRUE(elf.Init(true));
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 Ferrise69f4702017-10-19 16:08:58 -0700262 ASSERT_TRUE(elf.Init(true));
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);
276 elf.FakeSetLoadBias(0);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800277 MapInfo map_info(0x1000, 0x2000);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700278
279 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
280
Christopher Ferrise69f4702017-10-19 16:08:58 -0700281 elf.FakeSetLoadBias(0x3000);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700282 ASSERT_EQ(0x3101U, elf.GetRelPc(0x1101, &map_info));
283
Christopher Ferrise69f4702017-10-19 16:08:58 -0700284 elf.FakeSetValid(false);
285 elf.FakeSetLoadBias(0);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700286 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
287}
288
Christopher Ferrise69f4702017-10-19 16:08:58 -0700289TEST_F(ElfTest, step_in_signal_map) {
290 ElfFake elf(memory_);
291
292 RegsArm regs;
293 regs[13] = 0x50000;
294 regs[15] = 0x8000;
295 regs.SetFromRaw();
296
297 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
298 elf.FakeSetInterface(interface);
299
300 memory_->SetData32(0x3000, 0xdf0027ad);
301 MemoryFake process_memory;
302 process_memory.SetData32(0x50000, 0);
303 for (size_t i = 0; i < 16; i++) {
304 process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
305 }
306
307 elf.FakeSetValid(true);
308 elf.FakeSetLoadBias(0);
309 bool finished;
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800310 ASSERT_TRUE(elf.Step(0x1000, 0x1000, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700311 EXPECT_FALSE(finished);
312 EXPECT_EQ(15U, regs.pc());
313 EXPECT_EQ(13U, regs.sp());
314}
315
316class ElfInterfaceMock : public ElfInterface {
317 public:
318 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
319 virtual ~ElfInterfaceMock() = default;
320
321 bool Init(uint64_t*) override { return false; }
322 void InitHeaders() override {}
323 bool GetSoname(std::string*) override { return false; }
324 bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override { return false; }
325 MOCK_METHOD4(Step, bool(uint64_t, Regs*, Memory*, bool*));
326};
327
328TEST_F(ElfTest, step_in_interface) {
329 ElfFake elf(memory_);
330 elf.FakeSetValid(true);
331 elf.FakeSetLoadBias(0);
332
333 RegsArm regs;
334
335 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
336 elf.FakeSetInterface(interface);
337 MemoryFake process_memory;
338
339 bool finished;
340 EXPECT_CALL(*interface, Step(0x1000, &regs, &process_memory, &finished))
341 .WillOnce(::testing::Return(true));
342
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800343 ASSERT_TRUE(elf.Step(0x1004, 0x1000, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700344}
345
346TEST_F(ElfTest, step_in_interface_non_zero_load_bias) {
347 ElfFake elf(memory_);
348 elf.FakeSetValid(true);
349 elf.FakeSetLoadBias(0x4000);
350
351 RegsArm regs;
352
353 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
354 elf.FakeSetInterface(interface);
355 MemoryFake process_memory;
356
357 // Invalid relative pc given load_bias.
358 bool finished;
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800359 ASSERT_FALSE(elf.Step(0x1004, 0x1000, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700360
361 EXPECT_CALL(*interface, Step(0x3300, &regs, &process_memory, &finished))
362 .WillOnce(::testing::Return(true));
363
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800364 ASSERT_TRUE(elf.Step(0x7304, 0x7300, 0x2000, &regs, &process_memory, &finished));
Christopher Ferrise69f4702017-10-19 16:08:58 -0700365}
366
Christopher Ferrisd226a512017-07-14 10:37:19 -0700367} // namespace unwindstack