1 /*
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>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 #include <unwindstack/Elf.h>
27 #include <unwindstack/MapInfo.h>
28 #include <unwindstack/RegsArm.h>
29
30 #include "ElfFake.h"
31 #include "ElfTestUtils.h"
32 #include "LogFake.h"
33 #include "MemoryFake.h"
34
35 #if !defined(PT_ARM_EXIDX)
36 #define PT_ARM_EXIDX 0x70000001
37 #endif
38
39 namespace unwindstack {
40
41 class ElfTest : public ::testing::Test {
42 protected:
SetUp()43 void SetUp() override {
44 memory_ = new MemoryFake;
45 }
46
InitElf32(uint32_t machine_type)47 void InitElf32(uint32_t machine_type) {
48 Elf32_Ehdr ehdr;
49 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, machine_type);
50
51 ehdr.e_phoff = 0x100;
52 ehdr.e_ehsize = sizeof(ehdr);
53 ehdr.e_phentsize = sizeof(Elf32_Phdr);
54 ehdr.e_phnum = 1;
55 ehdr.e_shentsize = sizeof(Elf32_Shdr);
56 if (machine_type == EM_ARM) {
57 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;
65 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
71 if (machine_type == EM_ARM) {
72 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
InitElf64(uint32_t machine_type)85 void InitElf64(uint32_t machine_type) {
86 Elf64_Ehdr ehdr;
87 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, machine_type);
88
89 ehdr.e_phoff = 0x100;
90 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);
95 memory_->SetMemory(0, &ehdr, sizeof(ehdr));
96
97 Elf64_Phdr phdr;
98 memset(&phdr, 0, sizeof(phdr));
99 phdr.p_type = PT_LOAD;
100 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
TEST_F(ElfTest,invalid_memory)110 TEST_F(ElfTest, invalid_memory) {
111 Elf elf(memory_);
112
113 ASSERT_FALSE(elf.Init());
114 ASSERT_FALSE(elf.valid());
115 }
116
TEST_F(ElfTest,elf_invalid)117 TEST_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
125 ASSERT_FALSE(elf.Init());
126 ASSERT_FALSE(elf.valid());
127 ASSERT_TRUE(elf.interface() == nullptr);
128
129 ASSERT_EQ("", elf.GetSoname());
130
131 std::string name;
132 uint64_t func_offset;
133 ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
134
135 ASSERT_FALSE(elf.StepIfSignalHandler(0, nullptr, nullptr));
136 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
137
138 bool finished;
139 ASSERT_FALSE(elf.Step(0, nullptr, nullptr, &finished));
140 EXPECT_EQ(ERROR_INVALID_ELF, elf.GetLastErrorCode());
141 }
142
TEST_F(ElfTest,elf32_invalid_machine)143 TEST_F(ElfTest, elf32_invalid_machine) {
144 Elf elf(memory_);
145
146 InitElf32(EM_PPC);
147
148 ResetLogs();
149 ASSERT_FALSE(elf.Init());
150
151 ASSERT_EQ("", GetFakeLogBuf());
152 ASSERT_EQ("4 unwind 32 bit elf that is neither arm nor x86 nor mips: e_machine = 20\n\n",
153 GetFakeLogPrint());
154 }
155
TEST_F(ElfTest,elf64_invalid_machine)156 TEST_F(ElfTest, elf64_invalid_machine) {
157 Elf elf(memory_);
158
159 InitElf64(EM_PPC64);
160
161 ResetLogs();
162 ASSERT_FALSE(elf.Init());
163
164 ASSERT_EQ("", GetFakeLogBuf());
165 ASSERT_EQ("4 unwind 64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = 21\n\n",
166 GetFakeLogPrint());
167 }
168
TEST_F(ElfTest,elf_arm)169 TEST_F(ElfTest, elf_arm) {
170 Elf elf(memory_);
171
172 InitElf32(EM_ARM);
173
174 ASSERT_TRUE(elf.Init());
175 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
TEST_F(ElfTest,elf_mips)181 TEST_F(ElfTest, elf_mips) {
182 Elf elf(memory_);
183
184 InitElf32(EM_MIPS);
185
186 ASSERT_TRUE(elf.Init());
187 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
TEST_F(ElfTest,elf_x86)193 TEST_F(ElfTest, elf_x86) {
194 Elf elf(memory_);
195
196 InitElf32(EM_386);
197
198 ASSERT_TRUE(elf.Init());
199 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
TEST_F(ElfTest,elf_arm64)205 TEST_F(ElfTest, elf_arm64) {
206 Elf elf(memory_);
207
208 InitElf64(EM_AARCH64);
209
210 ASSERT_TRUE(elf.Init());
211 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
TEST_F(ElfTest,elf_x86_64)217 TEST_F(ElfTest, elf_x86_64) {
218 Elf elf(memory_);
219
220 InitElf64(EM_X86_64);
221
222 ASSERT_TRUE(elf.Init());
223 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 }
228
TEST_F(ElfTest,elf_mips64)229 TEST_F(ElfTest, elf_mips64) {
230 Elf elf(memory_);
231
232 InitElf64(EM_MIPS);
233
234 ASSERT_TRUE(elf.Init());
235 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
TEST_F(ElfTest,gnu_debugdata_init32)241 TEST_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_);
248 ASSERT_TRUE(elf.Init());
249 ASSERT_TRUE(elf.interface() != nullptr);
250 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
251 EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
252 EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
253 }
254
TEST_F(ElfTest,gnu_debugdata_init64)255 TEST_F(ElfTest, gnu_debugdata_init64) {
256 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 });
260
261 Elf elf(memory_);
262 ASSERT_TRUE(elf.Init());
263 ASSERT_TRUE(elf.interface() != nullptr);
264 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
265 EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
266 EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
267 }
268
TEST_F(ElfTest,rel_pc)269 TEST_F(ElfTest, rel_pc) {
270 ElfFake elf(memory_);
271
272 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
273 elf.FakeSetInterface(interface);
274
275 elf.FakeSetValid(true);
276 MapInfo map_info(nullptr, 0x1000, 0x2000, 0, 0, "");
277
278 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
279
280 elf.FakeSetValid(false);
281 ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
282 }
283
TEST_F(ElfTest,step_in_signal_map)284 TEST_F(ElfTest, step_in_signal_map) {
285 ElfFake elf(memory_);
286
287 RegsArm regs;
288 regs[13] = 0x50000;
289 regs[15] = 0x8000;
290
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);
302 ASSERT_TRUE(elf.StepIfSignalHandler(0x3000, ®s, &process_memory));
303 EXPECT_EQ(ERROR_NONE, elf.GetLastErrorCode());
304 EXPECT_EQ(15U, regs.pc());
305 EXPECT_EQ(13U, regs.sp());
306 }
307
308 class ElfInterfaceMock : public ElfInterface {
309 public:
ElfInterfaceMock(Memory * memory)310 ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
311 virtual ~ElfInterfaceMock() = default;
312
Init(uint64_t *)313 bool Init(uint64_t*) override { return false; }
InitHeaders(uint64_t)314 void InitHeaders(uint64_t) override {}
GetSoname()315 std::string GetSoname() override { return ""; }
GetFunctionName(uint64_t,std::string *,uint64_t *)316 bool GetFunctionName(uint64_t, std::string*, uint64_t*) override { return false; }
GetBuildID()317 std::string GetBuildID() override { return ""; }
318
319 MOCK_METHOD4(Step, bool(uint64_t, Regs*, Memory*, bool*));
320 MOCK_METHOD2(GetGlobalVariable, bool(const std::string&, uint64_t*));
321 MOCK_METHOD1(IsValidPc, bool(uint64_t));
322
MockSetDynamicOffset(uint64_t offset)323 void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
MockSetDynamicVaddr(uint64_t vaddr)324 void MockSetDynamicVaddr(uint64_t vaddr) { dynamic_vaddr_ = vaddr; }
MockSetDynamicSize(uint64_t size)325 void MockSetDynamicSize(uint64_t size) { dynamic_size_ = size; }
326 };
327
TEST_F(ElfTest,step_in_interface)328 TEST_F(ElfTest, step_in_interface) {
329 ElfFake elf(memory_);
330 elf.FakeSetValid(true);
331
332 RegsArm regs;
333
334 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
335 elf.FakeSetInterface(interface);
336 MemoryFake process_memory;
337
338 bool finished;
339 EXPECT_CALL(*interface, Step(0x1000, ®s, &process_memory, &finished))
340 .WillOnce(::testing::Return(true));
341
342 ASSERT_TRUE(elf.Step(0x1000, ®s, &process_memory, &finished));
343 }
344
TEST_F(ElfTest,get_global_invalid_elf)345 TEST_F(ElfTest, get_global_invalid_elf) {
346 ElfFake elf(memory_);
347 elf.FakeSetValid(false);
348
349 std::string global("something");
350 uint64_t offset;
351 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
352 }
353
TEST_F(ElfTest,get_global_valid_not_in_interface)354 TEST_F(ElfTest, get_global_valid_not_in_interface) {
355 ElfFake elf(memory_);
356 elf.FakeSetValid(true);
357
358 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
359 elf.FakeSetInterface(interface);
360
361 uint64_t offset;
362 std::string global("something");
363 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
364
365 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
366 }
367
TEST_F(ElfTest,get_global_valid_below_load_bias)368 TEST_F(ElfTest, get_global_valid_below_load_bias) {
369 ElfFake elf(memory_);
370 elf.FakeSetValid(true);
371 elf.FakeSetLoadBias(0x1000);
372
373 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
374 elf.FakeSetInterface(interface);
375
376 uint64_t offset;
377 std::string global("something");
378 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
379 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
380
381 ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
382 }
383
TEST_F(ElfTest,get_global_valid_dynamic_zero_non_zero_load_bias)384 TEST_F(ElfTest, get_global_valid_dynamic_zero_non_zero_load_bias) {
385 ElfFake elf(memory_);
386 elf.FakeSetValid(true);
387 elf.FakeSetLoadBias(0x100);
388
389 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
390 elf.FakeSetInterface(interface);
391
392 uint64_t offset;
393 std::string global("something");
394 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
395 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
396
397 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
398 EXPECT_EQ(0x200U, offset);
399 }
400
TEST_F(ElfTest,get_global_valid_dynamic_zero)401 TEST_F(ElfTest, get_global_valid_dynamic_zero) {
402 ElfFake elf(memory_);
403 elf.FakeSetValid(true);
404
405 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
406 elf.FakeSetInterface(interface);
407
408 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
409 elf.FakeSetGnuDebugdataInterface(gnu_interface);
410
411 uint64_t offset;
412 std::string global("something");
413 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
414
415 EXPECT_CALL(*gnu_interface, GetGlobalVariable(global, &offset))
416 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x500), ::testing::Return(true)));
417
418 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
419 EXPECT_EQ(0x500U, offset);
420 }
421
TEST_F(ElfTest,get_global_valid_in_gnu_debugdata_dynamic_zero)422 TEST_F(ElfTest, get_global_valid_in_gnu_debugdata_dynamic_zero) {
423 ElfFake elf(memory_);
424 elf.FakeSetValid(true);
425
426 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
427 elf.FakeSetInterface(interface);
428
429 uint64_t offset;
430 std::string global("something");
431 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
432 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
433
434 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
435 EXPECT_EQ(0x300U, offset);
436 }
437
TEST_F(ElfTest,get_global_valid_dynamic_adjust_negative)438 TEST_F(ElfTest, get_global_valid_dynamic_adjust_negative) {
439 ElfFake elf(memory_);
440 elf.FakeSetValid(true);
441
442 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
443 interface->MockSetDynamicOffset(0x400);
444 interface->MockSetDynamicVaddr(0x800);
445 interface->MockSetDynamicSize(0x100);
446 elf.FakeSetInterface(interface);
447
448 uint64_t offset;
449 std::string global("something");
450 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
451 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
452
453 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
454 EXPECT_EQ(0x450U, offset);
455 }
456
TEST_F(ElfTest,get_global_valid_dynamic_adjust_positive)457 TEST_F(ElfTest, get_global_valid_dynamic_adjust_positive) {
458 ElfFake elf(memory_);
459 elf.FakeSetValid(true);
460
461 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
462 interface->MockSetDynamicOffset(0x1000);
463 interface->MockSetDynamicVaddr(0x800);
464 interface->MockSetDynamicSize(0x100);
465 elf.FakeSetInterface(interface);
466
467 uint64_t offset;
468 std::string global("something");
469 EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
470 .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
471
472 ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
473 EXPECT_EQ(0x1050U, offset);
474 }
475
TEST_F(ElfTest,is_valid_pc_elf_invalid)476 TEST_F(ElfTest, is_valid_pc_elf_invalid) {
477 ElfFake elf(memory_);
478 elf.FakeSetValid(false);
479
480 EXPECT_FALSE(elf.IsValidPc(0x100));
481 EXPECT_FALSE(elf.IsValidPc(0x200));
482 }
483
TEST_F(ElfTest,is_valid_pc_interface)484 TEST_F(ElfTest, is_valid_pc_interface) {
485 ElfFake elf(memory_);
486 elf.FakeSetValid(true);
487
488 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
489 elf.FakeSetInterface(interface);
490
491 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
492
493 EXPECT_TRUE(elf.IsValidPc(0x1500));
494 }
495
TEST_F(ElfTest,is_valid_pc_from_gnu_debugdata)496 TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
497 ElfFake elf(memory_);
498 elf.FakeSetValid(true);
499
500 ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
501 elf.FakeSetInterface(interface);
502 ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
503 elf.FakeSetGnuDebugdataInterface(gnu_interface);
504
505 EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
506 EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
507
508 EXPECT_TRUE(elf.IsValidPc(0x1500));
509 }
510
TEST_F(ElfTest,error_code_not_valid)511 TEST_F(ElfTest, error_code_not_valid) {
512 ElfFake elf(memory_);
513 elf.FakeSetValid(false);
514
515 ErrorData error{ERROR_MEMORY_INVALID, 0x100};
516 elf.GetLastError(&error);
517 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
518 EXPECT_EQ(0x100U, error.address);
519 }
520
TEST_F(ElfTest,error_code_valid)521 TEST_F(ElfTest, error_code_valid) {
522 ElfFake elf(memory_);
523 elf.FakeSetValid(true);
524 ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
525 elf.FakeSetInterface(interface);
526 interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
527 interface->FakeSetErrorAddress(0x1000);
528
529 ErrorData error{ERROR_NONE, 0};
530 elf.GetLastError(&error);
531 EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
532 EXPECT_EQ(0x1000U, error.address);
533 EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
534 EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
535 }
536
537 } // namespace unwindstack
538