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/mman.h>
20 #include <sys/stat.h>
21
22 #include <memory>
23 #include <vector>
24
25 #include <android-base/unique_fd.h>
26 #include <gtest/gtest.h>
27
28 #include <unwindstack/ElfInterface.h>
29
30 #include "DwarfEncoding.h"
31 #include "ElfInterfaceArm.h"
32 #include "MemoryRange.h"
33
34 #include "ElfFake.h"
35 #include "ElfTestUtils.h"
36 #include "utils/MemoryFake.h"
37
38 #if __has_feature(address_sanitizer)
39 // There is a test that tries to allocate a large value, allow it to fail
40 // if asan is enabled.
__asan_default_options()41 extern "C" const char* __asan_default_options() {
42 return "allocator_may_return_null=1";
43 }
44 #endif
45
46 namespace unwindstack {
47
48 class ElfInterfaceTest : public ::testing::Test {
49 protected:
SetUp()50 void SetUp() override {
51 fake_memory_ = new MemoryFake;
52 memory_.reset(fake_memory_);
53 }
54
SetStringMemory(uint64_t offset,const char * string)55 void SetStringMemory(uint64_t offset, const char* string) {
56 fake_memory_->SetMemory(offset, string, strlen(string) + 1);
57 }
58
59 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
60 void SinglePtLoad();
61
62 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
63 void MultipleExecutablePtLoads();
64
65 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
66 void MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr();
67
68 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
69 void NonExecutablePtLoads();
70
71 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
72 void ManyPhdrs();
73
74 enum SonameTestEnum : uint8_t {
75 SONAME_NORMAL,
76 SONAME_DTNULL_AFTER,
77 SONAME_DTSIZE_SMALL,
78 SONAME_MISSING_MAP,
79 };
80
81 template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
82 void SonameInit(SonameTestEnum test_type = SONAME_NORMAL);
83
84 template <typename ElfInterfaceType>
85 void Soname();
86
87 template <typename ElfInterfaceType>
88 void SonameAfterDtNull();
89
90 template <typename ElfInterfaceType>
91 void SonameSize();
92
93 template <typename ElfInterfaceType>
94 void SonameMissingMap();
95
96 template <typename ElfType>
97 void InitHeadersEhFrameTest();
98
99 template <typename ElfType>
100 void InitHeadersDebugFrame();
101
102 template <typename ElfType>
103 void InitHeadersEhFrameFail();
104
105 template <typename ElfType>
106 void InitHeadersDebugFrameFail();
107
108 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
109 void InitProgramHeadersMalformed();
110
111 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
112 void InitSectionHeadersMalformed();
113
114 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
115 void InitSectionHeadersMalformedSymData();
116
117 template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
118 void InitSectionHeaders(uint64_t entry_size);
119
120 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
121 void InitSectionHeadersOffsets();
122
123 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
124 void InitSectionHeadersOffsetsEhFrameSectionBias(uint64_t addr, uint64_t offset,
125 int64_t expected_bias);
126
127 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
128 void InitSectionHeadersOffsetsEhFrameHdrSectionBias(uint64_t addr, uint64_t offset,
129 int64_t expected_bias);
130
131 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
132 void InitSectionHeadersOffsetsDebugFrameSectionBias(uint64_t addr, uint64_t offset,
133 int64_t expected_bias);
134
135 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
136 void CheckGnuEhFrame(uint64_t addr, uint64_t offset, int64_t expected_bias);
137
138 template <typename Sym>
139 void InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset,
140 uint64_t sym_offset, const char* name);
141
142 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
143 void BuildID();
144
145 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
146 void BuildIDTwoNotes();
147
148 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
149 void BuildIDSectionTooSmallForName();
150
151 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
152 void BuildIDSectionTooSmallForDesc();
153
154 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
155 void BuildIDSectionTooSmallForHeader();
156
157 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
158 void CheckLoadBiasInFirstPhdr(int64_t load_bias);
159
160 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
161 void CheckLoadBiasInFirstExecPhdr(uint64_t offset, uint64_t vaddr, int64_t load_bias);
162
163 MemoryFake* fake_memory_;
164 std::shared_ptr<Memory> memory_;
165 };
166
167 template <typename Sym>
InitSym(uint64_t offset,uint32_t value,uint32_t size,uint32_t name_offset,uint64_t sym_offset,const char * name)168 void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset,
169 uint64_t sym_offset, const char* name) {
170 Sym sym = {};
171 sym.st_info = STT_FUNC;
172 sym.st_value = value;
173 sym.st_size = size;
174 sym.st_name = name_offset;
175 sym.st_shndx = SHN_COMMON;
176
177 fake_memory_->SetMemory(offset, &sym, sizeof(sym));
178 fake_memory_->SetMemory(sym_offset + name_offset, name, strlen(name) + 1);
179 }
180
181 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
SinglePtLoad()182 void ElfInterfaceTest::SinglePtLoad() {
183 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
184
185 Ehdr ehdr = {};
186 ehdr.e_phoff = 0x100;
187 ehdr.e_phnum = 1;
188 ehdr.e_phentsize = sizeof(Phdr);
189 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
190
191 Phdr phdr = {};
192 phdr.p_type = PT_LOAD;
193 phdr.p_vaddr = 0x2000;
194 phdr.p_memsz = 0x10000;
195 phdr.p_flags = PF_R | PF_X;
196 phdr.p_align = 0x1000;
197 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
198
199 int64_t load_bias = 0;
200 ASSERT_TRUE(elf->Init(&load_bias));
201 EXPECT_EQ(0x2000, load_bias);
202
203 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
204 ASSERT_EQ(1U, pt_loads.size());
205 LoadInfo load_data = pt_loads.at(0);
206 ASSERT_EQ(0U, load_data.offset);
207 ASSERT_EQ(0x2000U, load_data.table_offset);
208 ASSERT_EQ(0x10000U, load_data.table_size);
209 }
210
TEST_F(ElfInterfaceTest,single_pt_load_32)211 TEST_F(ElfInterfaceTest, single_pt_load_32) {
212 SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
213 }
214
TEST_F(ElfInterfaceTest,single_pt_load_64)215 TEST_F(ElfInterfaceTest, single_pt_load_64) {
216 SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
217 }
218
219 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
MultipleExecutablePtLoads()220 void ElfInterfaceTest::MultipleExecutablePtLoads() {
221 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
222
223 Ehdr ehdr = {};
224 ehdr.e_phoff = 0x100;
225 ehdr.e_phnum = 3;
226 ehdr.e_phentsize = sizeof(Phdr);
227 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
228
229 Phdr phdr = {};
230 phdr.p_type = PT_LOAD;
231 phdr.p_vaddr = 0x2000;
232 phdr.p_memsz = 0x10000;
233 phdr.p_flags = PF_R | PF_X;
234 phdr.p_align = 0x1000;
235 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
236
237 memset(&phdr, 0, sizeof(phdr));
238 phdr.p_type = PT_LOAD;
239 phdr.p_offset = 0x1000;
240 phdr.p_vaddr = 0x2001;
241 phdr.p_memsz = 0x10001;
242 phdr.p_flags = PF_R | PF_X;
243 phdr.p_align = 0x1001;
244 fake_memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
245
246 memset(&phdr, 0, sizeof(phdr));
247 phdr.p_type = PT_LOAD;
248 phdr.p_offset = 0x2000;
249 phdr.p_vaddr = 0x2002;
250 phdr.p_memsz = 0x10002;
251 phdr.p_flags = PF_R | PF_X;
252 phdr.p_align = 0x1002;
253 fake_memory_->SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
254
255 int64_t load_bias = 0;
256 ASSERT_TRUE(elf->Init(&load_bias));
257 EXPECT_EQ(0x2000, load_bias);
258
259 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
260 ASSERT_EQ(3U, pt_loads.size());
261
262 LoadInfo load_data = pt_loads.at(0);
263 ASSERT_EQ(0U, load_data.offset);
264 ASSERT_EQ(0x2000U, load_data.table_offset);
265 ASSERT_EQ(0x10000U, load_data.table_size);
266
267 load_data = pt_loads.at(0x1000);
268 ASSERT_EQ(0x1000U, load_data.offset);
269 ASSERT_EQ(0x2001U, load_data.table_offset);
270 ASSERT_EQ(0x10001U, load_data.table_size);
271
272 load_data = pt_loads.at(0x2000);
273 ASSERT_EQ(0x2000U, load_data.offset);
274 ASSERT_EQ(0x2002U, load_data.table_offset);
275 ASSERT_EQ(0x10002U, load_data.table_size);
276 }
277
TEST_F(ElfInterfaceTest,multiple_executable_pt_loads_32)278 TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_32) {
279 MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
280 }
281
TEST_F(ElfInterfaceTest,multiple_executable_pt_loads_64)282 TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_64) {
283 MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
284 }
285
286 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr()287 void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() {
288 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
289
290 Ehdr ehdr = {};
291 ehdr.e_phoff = 0x100;
292 ehdr.e_phnum = 3;
293 ehdr.e_phentsize = sizeof(Phdr) + 100;
294 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
295
296 Phdr phdr = {};
297 phdr.p_type = PT_LOAD;
298 phdr.p_vaddr = 0x2000;
299 phdr.p_memsz = 0x10000;
300 phdr.p_flags = PF_R | PF_X;
301 phdr.p_align = 0x1000;
302 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
303
304 memset(&phdr, 0, sizeof(phdr));
305 phdr.p_type = PT_LOAD;
306 phdr.p_offset = 0x1000;
307 phdr.p_vaddr = 0x2001;
308 phdr.p_memsz = 0x10001;
309 phdr.p_flags = PF_R | PF_X;
310 phdr.p_align = 0x1001;
311 fake_memory_->SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr));
312
313 memset(&phdr, 0, sizeof(phdr));
314 phdr.p_type = PT_LOAD;
315 phdr.p_offset = 0x2000;
316 phdr.p_vaddr = 0x2002;
317 phdr.p_memsz = 0x10002;
318 phdr.p_flags = PF_R | PF_X;
319 phdr.p_align = 0x1002;
320 fake_memory_->SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr));
321
322 int64_t load_bias = 0;
323 ASSERT_TRUE(elf->Init(&load_bias));
324 EXPECT_EQ(0x2000, load_bias);
325
326 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
327 ASSERT_EQ(3U, pt_loads.size());
328
329 LoadInfo load_data = pt_loads.at(0);
330 ASSERT_EQ(0U, load_data.offset);
331 ASSERT_EQ(0x2000U, load_data.table_offset);
332 ASSERT_EQ(0x10000U, load_data.table_size);
333
334 load_data = pt_loads.at(0x1000);
335 ASSERT_EQ(0x1000U, load_data.offset);
336 ASSERT_EQ(0x2001U, load_data.table_offset);
337 ASSERT_EQ(0x10001U, load_data.table_size);
338
339 load_data = pt_loads.at(0x2000);
340 ASSERT_EQ(0x2000U, load_data.offset);
341 ASSERT_EQ(0x2002U, load_data.table_offset);
342 ASSERT_EQ(0x10002U, load_data.table_size);
343 }
344
TEST_F(ElfInterfaceTest,multiple_executable_pt_loads_increments_not_size_of_phdr_32)345 TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_increments_not_size_of_phdr_32) {
346 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn,
347 ElfInterface32>();
348 }
349
TEST_F(ElfInterfaceTest,multiple_executable_pt_loads_increments_not_size_of_phdr_64)350 TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_increments_not_size_of_phdr_64) {
351 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn,
352 ElfInterface64>();
353 }
354
355 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
NonExecutablePtLoads()356 void ElfInterfaceTest::NonExecutablePtLoads() {
357 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
358
359 Ehdr ehdr = {};
360 ehdr.e_phoff = 0x100;
361 ehdr.e_phnum = 3;
362 ehdr.e_phentsize = sizeof(Phdr);
363 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
364
365 Phdr phdr = {};
366 phdr.p_type = PT_LOAD;
367 phdr.p_vaddr = 0x2000;
368 phdr.p_memsz = 0x10000;
369 phdr.p_flags = PF_R;
370 phdr.p_align = 0x1000;
371 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
372
373 memset(&phdr, 0, sizeof(phdr));
374 phdr.p_type = PT_LOAD;
375 phdr.p_offset = 0x1000;
376 phdr.p_vaddr = 0x2001;
377 phdr.p_memsz = 0x10001;
378 phdr.p_flags = PF_R | PF_X;
379 phdr.p_align = 0x1001;
380 fake_memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
381
382 memset(&phdr, 0, sizeof(phdr));
383 phdr.p_type = PT_LOAD;
384 phdr.p_offset = 0x2000;
385 phdr.p_vaddr = 0x2002;
386 phdr.p_memsz = 0x10002;
387 phdr.p_flags = PF_R;
388 phdr.p_align = 0x1002;
389 fake_memory_->SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
390
391 int64_t load_bias = 0;
392 ASSERT_TRUE(elf->Init(&load_bias));
393 EXPECT_EQ(0x1001, load_bias);
394
395 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
396 ASSERT_EQ(1U, pt_loads.size());
397
398 LoadInfo load_data = pt_loads.at(0x1000);
399 ASSERT_EQ(0x1000U, load_data.offset);
400 ASSERT_EQ(0x2001U, load_data.table_offset);
401 ASSERT_EQ(0x10001U, load_data.table_size);
402 }
403
TEST_F(ElfInterfaceTest,non_executable_pt_loads_32)404 TEST_F(ElfInterfaceTest, non_executable_pt_loads_32) {
405 NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
406 }
407
TEST_F(ElfInterfaceTest,non_executable_pt_loads_64)408 TEST_F(ElfInterfaceTest, non_executable_pt_loads_64) {
409 NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
410 }
411
412 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
ManyPhdrs()413 void ElfInterfaceTest::ManyPhdrs() {
414 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
415
416 Ehdr ehdr = {};
417 ehdr.e_phoff = 0x100;
418 ehdr.e_phnum = 7;
419 ehdr.e_phentsize = sizeof(Phdr);
420 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
421
422 uint64_t phdr_offset = 0x100;
423
424 Phdr phdr = {};
425 phdr.p_type = PT_LOAD;
426 phdr.p_vaddr = 0x2000;
427 phdr.p_memsz = 0x10000;
428 phdr.p_flags = PF_R | PF_X;
429 phdr.p_align = 0x1000;
430 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
431 phdr_offset += sizeof(phdr);
432
433 memset(&phdr, 0, sizeof(phdr));
434 phdr.p_type = PT_GNU_EH_FRAME;
435 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
436 phdr_offset += sizeof(phdr);
437
438 memset(&phdr, 0, sizeof(phdr));
439 phdr.p_type = PT_DYNAMIC;
440 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
441 phdr_offset += sizeof(phdr);
442
443 memset(&phdr, 0, sizeof(phdr));
444 phdr.p_type = PT_INTERP;
445 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
446 phdr_offset += sizeof(phdr);
447
448 memset(&phdr, 0, sizeof(phdr));
449 phdr.p_type = PT_NOTE;
450 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
451 phdr_offset += sizeof(phdr);
452
453 memset(&phdr, 0, sizeof(phdr));
454 phdr.p_type = PT_SHLIB;
455 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
456 phdr_offset += sizeof(phdr);
457
458 memset(&phdr, 0, sizeof(phdr));
459 phdr.p_type = PT_GNU_EH_FRAME;
460 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
461
462 int64_t load_bias = 0;
463 ASSERT_TRUE(elf->Init(&load_bias));
464 EXPECT_EQ(0x2000, load_bias);
465
466 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
467 ASSERT_EQ(1U, pt_loads.size());
468
469 LoadInfo load_data = pt_loads.at(0);
470 ASSERT_EQ(0U, load_data.offset);
471 ASSERT_EQ(0x2000U, load_data.table_offset);
472 ASSERT_EQ(0x10000U, load_data.table_size);
473 }
474
TEST_F(ElfInterfaceTest,many_phdrs_32)475 TEST_F(ElfInterfaceTest, many_phdrs_32) {
476 ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
477 }
478
TEST_F(ElfInterfaceTest,many_phdrs_64)479 TEST_F(ElfInterfaceTest, many_phdrs_64) {
480 ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
481 }
482
TEST_F(ElfInterfaceTest,arm32)483 TEST_F(ElfInterfaceTest, arm32) {
484 ElfInterfaceArm elf_arm(memory_);
485
486 Elf32_Ehdr ehdr = {};
487 ehdr.e_phoff = 0x100;
488 ehdr.e_phnum = 1;
489 ehdr.e_phentsize = sizeof(Elf32_Phdr);
490 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
491
492 Elf32_Phdr phdr = {};
493 phdr.p_type = PT_ARM_EXIDX;
494 phdr.p_offset = 0x2000;
495 phdr.p_filesz = 16;
496 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
497
498 // Add arm exidx entries.
499 fake_memory_->SetData32(0x2000, 0x1000);
500 fake_memory_->SetData32(0x2008, 0x1000);
501
502 int64_t load_bias = 0;
503 ASSERT_TRUE(elf_arm.Init(&load_bias));
504 EXPECT_EQ(0, load_bias);
505
506 std::vector<uint32_t> entries;
507 for (auto addr : elf_arm) {
508 entries.push_back(addr);
509 }
510 ASSERT_EQ(2U, entries.size());
511 ASSERT_EQ(0x3000U, entries[0]);
512 ASSERT_EQ(0x3008U, entries[1]);
513
514 ASSERT_EQ(0x2000U, elf_arm.start_offset());
515 ASSERT_EQ(2U, elf_arm.total_entries());
516 }
517
518 template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
SonameInit(SonameTestEnum test_type)519 void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) {
520 Ehdr ehdr = {};
521 ehdr.e_shoff = 0x200;
522 ehdr.e_shnum = 2;
523 ehdr.e_shentsize = sizeof(Shdr);
524 ehdr.e_phoff = 0x100;
525 ehdr.e_phnum = 1;
526 ehdr.e_phentsize = sizeof(Phdr);
527 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
528
529 Shdr shdr = {};
530 shdr.sh_type = SHT_STRTAB;
531 if (test_type == SONAME_MISSING_MAP) {
532 shdr.sh_addr = 0x20100;
533 } else {
534 shdr.sh_addr = 0x10100;
535 }
536 shdr.sh_offset = 0x10000;
537 fake_memory_->SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr));
538
539 Phdr phdr = {};
540 phdr.p_type = PT_DYNAMIC;
541 phdr.p_offset = 0x2000;
542 phdr.p_memsz = sizeof(Dyn) * 3;
543 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
544
545 uint64_t offset = 0x2000;
546 Dyn dyn;
547
548 dyn.d_tag = DT_STRTAB;
549 dyn.d_un.d_ptr = 0x10100;
550 fake_memory_->SetMemory(offset, &dyn, sizeof(dyn));
551 offset += sizeof(dyn);
552
553 dyn.d_tag = DT_STRSZ;
554 if (test_type == SONAME_DTSIZE_SMALL) {
555 dyn.d_un.d_val = 0x10;
556 } else {
557 dyn.d_un.d_val = 0x1000;
558 }
559 fake_memory_->SetMemory(offset, &dyn, sizeof(dyn));
560 offset += sizeof(dyn);
561
562 if (test_type == SONAME_DTNULL_AFTER) {
563 dyn.d_tag = DT_NULL;
564 fake_memory_->SetMemory(offset, &dyn, sizeof(dyn));
565 offset += sizeof(dyn);
566 }
567
568 dyn.d_tag = DT_SONAME;
569 dyn.d_un.d_val = 0x10;
570 fake_memory_->SetMemory(offset, &dyn, sizeof(dyn));
571 offset += sizeof(dyn);
572
573 dyn.d_tag = DT_NULL;
574 fake_memory_->SetMemory(offset, &dyn, sizeof(dyn));
575
576 SetStringMemory(0x10010, "fake_soname.so");
577 }
578
579 template <typename ElfInterfaceType>
Soname()580 void ElfInterfaceTest::Soname() {
581 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
582
583 int64_t load_bias = 0;
584 ASSERT_TRUE(elf->Init(&load_bias));
585 EXPECT_EQ(0, load_bias);
586
587 ASSERT_EQ("fake_soname.so", elf->GetSoname());
588 }
589
TEST_F(ElfInterfaceTest,soname_32)590 TEST_F(ElfInterfaceTest, soname_32) {
591 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>();
592 Soname<ElfInterface32>();
593 }
594
TEST_F(ElfInterfaceTest,soname_64)595 TEST_F(ElfInterfaceTest, soname_64) {
596 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>();
597 Soname<ElfInterface64>();
598 }
599
600 template <typename ElfInterfaceType>
SonameAfterDtNull()601 void ElfInterfaceTest::SonameAfterDtNull() {
602 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
603
604 int64_t load_bias = 0;
605 ASSERT_TRUE(elf->Init(&load_bias));
606 EXPECT_EQ(0, load_bias);
607
608 ASSERT_EQ("", elf->GetSoname());
609 }
610
TEST_F(ElfInterfaceTest,soname_after_dt_null_32)611 TEST_F(ElfInterfaceTest, soname_after_dt_null_32) {
612 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER);
613 SonameAfterDtNull<ElfInterface32>();
614 }
615
TEST_F(ElfInterfaceTest,soname_after_dt_null_64)616 TEST_F(ElfInterfaceTest, soname_after_dt_null_64) {
617 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER);
618 SonameAfterDtNull<ElfInterface64>();
619 }
620
621 template <typename ElfInterfaceType>
SonameSize()622 void ElfInterfaceTest::SonameSize() {
623 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
624
625 int64_t load_bias = 0;
626 ASSERT_TRUE(elf->Init(&load_bias));
627 EXPECT_EQ(0, load_bias);
628
629 ASSERT_EQ("", elf->GetSoname());
630 }
631
TEST_F(ElfInterfaceTest,soname_size_32)632 TEST_F(ElfInterfaceTest, soname_size_32) {
633 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL);
634 SonameSize<ElfInterface32>();
635 }
636
TEST_F(ElfInterfaceTest,soname_size_64)637 TEST_F(ElfInterfaceTest, soname_size_64) {
638 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTSIZE_SMALL);
639 SonameSize<ElfInterface64>();
640 }
641
642 // Verify that there is no map from STRTAB in the dynamic section to a
643 // STRTAB entry in the section headers.
644 template <typename ElfInterfaceType>
SonameMissingMap()645 void ElfInterfaceTest::SonameMissingMap() {
646 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
647
648 int64_t load_bias = 0;
649 ASSERT_TRUE(elf->Init(&load_bias));
650 EXPECT_EQ(0, load_bias);
651
652 ASSERT_EQ("", elf->GetSoname());
653 }
654
TEST_F(ElfInterfaceTest,soname_missing_map_32)655 TEST_F(ElfInterfaceTest, soname_missing_map_32) {
656 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP);
657 SonameMissingMap<ElfInterface32>();
658 }
659
TEST_F(ElfInterfaceTest,soname_missing_map_64)660 TEST_F(ElfInterfaceTest, soname_missing_map_64) {
661 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP);
662 SonameMissingMap<ElfInterface64>();
663 }
664
665 template <typename ElfType>
InitHeadersEhFrameTest()666 void ElfInterfaceTest::InitHeadersEhFrameTest() {
667 ElfType elf(memory_);
668
669 elf.FakeSetEhFrameInfo(SectionInfo{.offset = 0x10000});
670 elf.FakeSetDebugFrameInfo(SectionInfo{});
671
672 fake_memory_->SetMemory(
673 0x10000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2});
674 fake_memory_->SetData32(0x10004, 0x500);
675 fake_memory_->SetData32(0x10008, 250);
676
677 elf.InitHeaders();
678
679 EXPECT_FALSE(elf.eh_frame() == nullptr);
680 EXPECT_TRUE(elf.debug_frame() == nullptr);
681 }
682
TEST_F(ElfInterfaceTest,init_headers_eh_frame_32)683 TEST_F(ElfInterfaceTest, init_headers_eh_frame_32) {
684 InitHeadersEhFrameTest<ElfInterface32Fake>();
685 }
686
TEST_F(ElfInterfaceTest,init_headers_eh_frame_64)687 TEST_F(ElfInterfaceTest, init_headers_eh_frame_64) {
688 InitHeadersEhFrameTest<ElfInterface64Fake>();
689 }
690
691 template <typename ElfType>
InitHeadersDebugFrame()692 void ElfInterfaceTest::InitHeadersDebugFrame() {
693 ElfType elf(memory_);
694
695 elf.FakeSetEhFrameInfo(SectionInfo{});
696 elf.FakeSetDebugFrameInfo(SectionInfo{.offset = 0x5000, .size = 0x200});
697
698 fake_memory_->SetData32(0x5000, 0xfc);
699 fake_memory_->SetData32(0x5004, 0xffffffff);
700 fake_memory_->SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 4, 8, 2});
701
702 fake_memory_->SetData32(0x5100, 0xfc);
703 fake_memory_->SetData32(0x5104, 0);
704 fake_memory_->SetData32(0x5108, 0x1500);
705 fake_memory_->SetData32(0x510c, 0x200);
706
707 elf.InitHeaders();
708
709 EXPECT_TRUE(elf.eh_frame() == nullptr);
710 EXPECT_FALSE(elf.debug_frame() == nullptr);
711 }
712
TEST_F(ElfInterfaceTest,init_headers_debug_frame_32)713 TEST_F(ElfInterfaceTest, init_headers_debug_frame_32) {
714 InitHeadersDebugFrame<ElfInterface32Fake>();
715 }
716
TEST_F(ElfInterfaceTest,init_headers_debug_frame_64)717 TEST_F(ElfInterfaceTest, init_headers_debug_frame_64) {
718 InitHeadersDebugFrame<ElfInterface64Fake>();
719 }
720
721 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
InitProgramHeadersMalformed()722 void ElfInterfaceTest::InitProgramHeadersMalformed() {
723 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
724
725 Ehdr ehdr = {};
726 ehdr.e_phoff = 0x100;
727 ehdr.e_phnum = 3;
728 ehdr.e_phentsize = sizeof(Phdr);
729 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
730
731 int64_t load_bias = 0;
732 ASSERT_TRUE(elf->Init(&load_bias));
733 EXPECT_EQ(0, load_bias);
734 }
735
TEST_F(ElfInterfaceTest,init_program_headers_malformed_32)736 TEST_F(ElfInterfaceTest, init_program_headers_malformed_32) {
737 InitProgramHeadersMalformed<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>();
738 }
739
TEST_F(ElfInterfaceTest,init_program_headers_malformed_64)740 TEST_F(ElfInterfaceTest, init_program_headers_malformed_64) {
741 InitProgramHeadersMalformed<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>();
742 }
743
744 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersMalformed()745 void ElfInterfaceTest::InitSectionHeadersMalformed() {
746 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
747
748 Ehdr ehdr = {};
749 ehdr.e_shoff = 0x1000;
750 ehdr.e_shnum = 10;
751 ehdr.e_shentsize = sizeof(Shdr);
752 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
753
754 int64_t load_bias = 0;
755 ASSERT_TRUE(elf->Init(&load_bias));
756 EXPECT_EQ(0, load_bias);
757 }
758
TEST_F(ElfInterfaceTest,init_section_headers_malformed_32)759 TEST_F(ElfInterfaceTest, init_section_headers_malformed_32) {
760 InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
761 }
762
TEST_F(ElfInterfaceTest,init_section_headers_malformed_64)763 TEST_F(ElfInterfaceTest, init_section_headers_malformed_64) {
764 InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
765 }
766
767 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersMalformedSymData()768 void ElfInterfaceTest::InitSectionHeadersMalformedSymData() {
769 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
770
771 uint64_t offset = 0x1000;
772
773 Ehdr ehdr = {};
774 ehdr.e_shoff = offset;
775 ehdr.e_shnum = 5;
776 ehdr.e_shentsize = sizeof(Shdr);
777 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
778
779 offset += ehdr.e_shentsize;
780
781 Shdr shdr = {};
782 shdr.sh_type = SHT_SYMTAB;
783 shdr.sh_link = 4;
784 shdr.sh_addr = 0x5000;
785 shdr.sh_offset = 0x5000;
786 shdr.sh_entsize = 0x100;
787 shdr.sh_size = shdr.sh_entsize * 10;
788 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
789 offset += ehdr.e_shentsize;
790
791 memset(&shdr, 0, sizeof(shdr));
792 shdr.sh_type = SHT_DYNSYM;
793 shdr.sh_link = 10;
794 shdr.sh_addr = 0x6000;
795 shdr.sh_offset = 0x6000;
796 shdr.sh_entsize = 0x100;
797 shdr.sh_size = shdr.sh_entsize * 10;
798 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
799 offset += ehdr.e_shentsize;
800
801 memset(&shdr, 0, sizeof(shdr));
802 shdr.sh_type = SHT_DYNSYM;
803 shdr.sh_link = 2;
804 shdr.sh_addr = 0x6000;
805 shdr.sh_offset = 0x6000;
806 shdr.sh_entsize = 0x100;
807 shdr.sh_size = shdr.sh_entsize * 10;
808 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
809 offset += ehdr.e_shentsize;
810
811 // The string data for the entries.
812 memset(&shdr, 0, sizeof(shdr));
813 shdr.sh_type = SHT_STRTAB;
814 shdr.sh_name = 0x20000;
815 shdr.sh_offset = 0xf000;
816 shdr.sh_size = 0x1000;
817 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
818
819 int64_t load_bias = 0;
820 ASSERT_TRUE(elf->Init(&load_bias));
821 EXPECT_EQ(0, load_bias);
822 EXPECT_EQ(0U, elf->debug_frame_info().offset);
823 EXPECT_EQ(0U, elf->debug_frame_info().size);
824 EXPECT_EQ(0U, elf->gnu_debugdata_offset());
825 EXPECT_EQ(0U, elf->gnu_debugdata_size());
826
827 SharedString name;
828 uint64_t name_offset;
829 ASSERT_FALSE(elf->GetFunctionName(0x90010, &name, &name_offset));
830 }
831
TEST_F(ElfInterfaceTest,init_section_headers_malformed_symdata_32)832 TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata_32) {
833 InitSectionHeadersMalformedSymData<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
834 }
835
TEST_F(ElfInterfaceTest,init_section_headers_malformed_symdata_64)836 TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata_64) {
837 InitSectionHeadersMalformedSymData<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
838 }
839
840 template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
InitSectionHeaders(uint64_t entry_size)841 void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) {
842 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
843
844 uint64_t offset = 0x1000;
845
846 Ehdr ehdr = {};
847 ehdr.e_shoff = offset;
848 ehdr.e_shnum = 5;
849 ehdr.e_shentsize = entry_size;
850 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
851
852 offset += ehdr.e_shentsize;
853
854 Shdr shdr = {};
855 shdr.sh_type = SHT_SYMTAB;
856 shdr.sh_link = 4;
857 shdr.sh_addr = 0x5000;
858 shdr.sh_offset = 0x5000;
859 shdr.sh_entsize = sizeof(Sym);
860 shdr.sh_size = shdr.sh_entsize * 10;
861 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
862 offset += ehdr.e_shentsize;
863
864 memset(&shdr, 0, sizeof(shdr));
865 shdr.sh_type = SHT_DYNSYM;
866 shdr.sh_link = 4;
867 shdr.sh_addr = 0x6000;
868 shdr.sh_offset = 0x6000;
869 shdr.sh_entsize = sizeof(Sym);
870 shdr.sh_size = shdr.sh_entsize * 10;
871 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
872 offset += ehdr.e_shentsize;
873
874 memset(&shdr, 0, sizeof(shdr));
875 shdr.sh_type = SHT_PROGBITS;
876 shdr.sh_name = 0xa000;
877 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
878 offset += ehdr.e_shentsize;
879
880 // The string data for the entries.
881 memset(&shdr, 0, sizeof(shdr));
882 shdr.sh_type = SHT_STRTAB;
883 shdr.sh_name = 0x20000;
884 shdr.sh_offset = 0xf000;
885 shdr.sh_size = 0x1000;
886 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
887
888 InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one");
889 InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two");
890
891 int64_t load_bias = 0;
892 ASSERT_TRUE(elf->Init(&load_bias));
893 EXPECT_EQ(0, load_bias);
894 EXPECT_EQ(0U, elf->debug_frame_info().offset);
895 EXPECT_EQ(0U, elf->debug_frame_info().size);
896 EXPECT_EQ(0U, elf->gnu_debugdata_offset());
897 EXPECT_EQ(0U, elf->gnu_debugdata_size());
898
899 // Look in the first symbol table.
900 SharedString name;
901 uint64_t name_offset;
902 ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset));
903 EXPECT_EQ("function_one", name);
904 EXPECT_EQ(16U, name_offset);
905 ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset));
906 EXPECT_EQ("function_two", name);
907 EXPECT_EQ(32U, name_offset);
908 }
909
TEST_F(ElfInterfaceTest,init_section_headers_32)910 TEST_F(ElfInterfaceTest, init_section_headers_32) {
911 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr));
912 }
913
TEST_F(ElfInterfaceTest,init_section_headers_64)914 TEST_F(ElfInterfaceTest, init_section_headers_64) {
915 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr));
916 }
917
TEST_F(ElfInterfaceTest,init_section_headers_non_std_entry_size_32)918 TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size_32) {
919 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100);
920 }
921
TEST_F(ElfInterfaceTest,init_section_headers_non_std_entry_size_64)922 TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size_64) {
923 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100);
924 }
925
926 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersOffsets()927 void ElfInterfaceTest::InitSectionHeadersOffsets() {
928 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
929
930 uint64_t offset = 0x2000;
931
932 Ehdr ehdr = {};
933 ehdr.e_shoff = offset;
934 ehdr.e_shnum = 7;
935 ehdr.e_shentsize = sizeof(Shdr);
936 ehdr.e_shstrndx = 2;
937 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
938
939 offset += ehdr.e_shentsize;
940
941 Shdr shdr = {};
942 shdr.sh_type = SHT_PROGBITS;
943 shdr.sh_link = 2;
944 shdr.sh_name = 0x200;
945 shdr.sh_addr = 0x5000;
946 shdr.sh_offset = 0x5000;
947 shdr.sh_entsize = 0x100;
948 shdr.sh_size = 0x800;
949 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
950 offset += ehdr.e_shentsize;
951
952 // The string data for section header names.
953 memset(&shdr, 0, sizeof(shdr));
954 shdr.sh_type = SHT_STRTAB;
955 shdr.sh_name = 0x20000;
956 shdr.sh_offset = 0xf000;
957 shdr.sh_size = 0x1000;
958 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
959 offset += ehdr.e_shentsize;
960
961 memset(&shdr, 0, sizeof(shdr));
962 shdr.sh_type = SHT_PROGBITS;
963 shdr.sh_link = 2;
964 shdr.sh_name = 0x100;
965 shdr.sh_addr = 0x6000;
966 shdr.sh_offset = 0x6000;
967 shdr.sh_entsize = 0x100;
968 shdr.sh_size = 0x500;
969 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
970 offset += ehdr.e_shentsize;
971
972 memset(&shdr, 0, sizeof(shdr));
973 shdr.sh_type = SHT_PROGBITS;
974 shdr.sh_link = 2;
975 shdr.sh_name = 0x300;
976 shdr.sh_addr = 0x7000;
977 shdr.sh_offset = 0x7000;
978 shdr.sh_entsize = 0x100;
979 shdr.sh_size = 0x800;
980 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
981 offset += ehdr.e_shentsize;
982
983 memset(&shdr, 0, sizeof(shdr));
984 shdr.sh_type = SHT_PROGBITS;
985 shdr.sh_link = 2;
986 shdr.sh_name = 0x400;
987 shdr.sh_addr = 0xa000;
988 shdr.sh_offset = 0xa000;
989 shdr.sh_entsize = 0x100;
990 shdr.sh_size = 0xf00;
991 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
992 offset += ehdr.e_shentsize;
993
994 memset(&shdr, 0, sizeof(shdr));
995 shdr.sh_type = SHT_NOTE;
996 shdr.sh_name = 0x500;
997 shdr.sh_addr = 0xb000;
998 shdr.sh_offset = 0xb000;
999 shdr.sh_size = 0xf00;
1000 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1001
1002 fake_memory_->SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame"));
1003 fake_memory_->SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata"));
1004 fake_memory_->SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame"));
1005 fake_memory_->SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr"));
1006 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1007
1008 int64_t load_bias = 0;
1009 ASSERT_TRUE(elf->Init(&load_bias));
1010 EXPECT_EQ(0, load_bias);
1011 EXPECT_EQ(0x6000U, elf->debug_frame_info().offset);
1012 EXPECT_EQ(0, elf->debug_frame_info().bias);
1013 EXPECT_EQ(0x500U, elf->debug_frame_info().size);
1014 EXPECT_EQ(0U, elf->debug_frame_info().flags);
1015
1016 EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset());
1017 EXPECT_EQ(0x800U, elf->gnu_debugdata_size());
1018
1019 EXPECT_EQ(0x7000U, elf->eh_frame_info().offset);
1020 EXPECT_EQ(0, elf->eh_frame_info().bias);
1021 EXPECT_EQ(0x800U, elf->eh_frame_info().size);
1022 EXPECT_EQ(0U, elf->eh_frame_info().flags);
1023
1024 EXPECT_EQ(0xa000U, elf->eh_frame_hdr_info().offset);
1025 EXPECT_EQ(0, elf->eh_frame_hdr_info().bias);
1026 EXPECT_EQ(0xf00U, elf->eh_frame_hdr_info().size);
1027 EXPECT_EQ(0U, elf->eh_frame_hdr_info().flags);
1028
1029 EXPECT_EQ(0xb000U, elf->gnu_build_id_offset());
1030 EXPECT_EQ(0xf00U, elf->gnu_build_id_size());
1031 }
1032
TEST_F(ElfInterfaceTest,init_section_headers_offsets_32)1033 TEST_F(ElfInterfaceTest, init_section_headers_offsets_32) {
1034 InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
1035 }
1036
TEST_F(ElfInterfaceTest,init_section_headers_offsets_64)1037 TEST_F(ElfInterfaceTest, init_section_headers_offsets_64) {
1038 InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
1039 }
1040
1041 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersOffsetsEhFrameSectionBias(uint64_t addr,uint64_t offset,int64_t expected_bias)1042 void ElfInterfaceTest::InitSectionHeadersOffsetsEhFrameSectionBias(uint64_t addr, uint64_t offset,
1043 int64_t expected_bias) {
1044 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1045
1046 uint64_t elf_offset = 0x2000;
1047
1048 Ehdr ehdr = {};
1049 ehdr.e_shoff = elf_offset;
1050 ehdr.e_shnum = 4;
1051 ehdr.e_shentsize = sizeof(Shdr);
1052 ehdr.e_shstrndx = 2;
1053 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1054
1055 elf_offset += ehdr.e_shentsize;
1056
1057 Shdr shdr = {};
1058 shdr.sh_type = SHT_PROGBITS;
1059 shdr.sh_link = 2;
1060 shdr.sh_name = 0x200;
1061 shdr.sh_addr = 0x8000;
1062 shdr.sh_offset = 0x8000;
1063 shdr.sh_entsize = 0x100;
1064 shdr.sh_size = 0x800;
1065 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1066 elf_offset += ehdr.e_shentsize;
1067
1068 // The string data for section header names.
1069 memset(&shdr, 0, sizeof(shdr));
1070 shdr.sh_type = SHT_STRTAB;
1071 shdr.sh_name = 0x20000;
1072 shdr.sh_offset = 0xf000;
1073 shdr.sh_size = 0x1000;
1074 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1075 elf_offset += ehdr.e_shentsize;
1076
1077 memset(&shdr, 0, sizeof(shdr));
1078 shdr.sh_type = SHT_PROGBITS;
1079 shdr.sh_link = 2;
1080 shdr.sh_name = 0x100;
1081 shdr.sh_addr = addr;
1082 shdr.sh_offset = offset;
1083 shdr.sh_entsize = 0x100;
1084 shdr.sh_size = 0x500;
1085 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1086
1087 fake_memory_->SetMemory(0xf100, ".eh_frame", sizeof(".eh_frame"));
1088 fake_memory_->SetMemory(0xf200, ".eh_frame_hdr", sizeof(".eh_frame_hdr"));
1089
1090 int64_t load_bias = 0;
1091 ASSERT_TRUE(elf->Init(&load_bias));
1092 EXPECT_EQ(0, load_bias);
1093 EXPECT_EQ(offset, elf->eh_frame_info().offset);
1094 EXPECT_EQ(expected_bias, elf->eh_frame_info().bias);
1095 EXPECT_EQ(0x500U, elf->eh_frame_info().size);
1096 EXPECT_EQ(0U, elf->eh_frame_info().flags);
1097
1098 EXPECT_EQ(0x8000U, elf->eh_frame_hdr_info().offset);
1099 EXPECT_EQ(0, elf->eh_frame_hdr_info().bias);
1100 EXPECT_EQ(0x800U, elf->eh_frame_hdr_info().size);
1101 EXPECT_EQ(0U, elf->eh_frame_hdr_info().flags);
1102 }
1103
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_zero_32)1104 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_zero_32) {
1105 InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x4000,
1106 0x4000, 0);
1107 }
1108
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_zero_64)1109 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_zero_64) {
1110 InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0x6000,
1111 0x6000, 0);
1112 }
1113
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_positive_32)1114 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_positive_32) {
1115 InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1116 0x5000, 0x4000, 0x1000);
1117 }
1118
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_positive_64)1119 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_positive_64) {
1120 InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1121 0x6000, 0x4000, 0x2000);
1122 }
1123
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_negative_32)1124 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_negative_32) {
1125 InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1126 0x3000, 0x4000, -0x1000);
1127 }
1128
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_section_bias_negative_64)1129 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_negative_64) {
1130 InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1131 0x6000, 0x9000, -0x3000);
1132 }
1133
1134 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersOffsetsEhFrameHdrSectionBias(uint64_t addr,uint64_t offset,int64_t expected_bias)1135 void ElfInterfaceTest::InitSectionHeadersOffsetsEhFrameHdrSectionBias(uint64_t addr,
1136 uint64_t offset,
1137 int64_t expected_bias) {
1138 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1139
1140 uint64_t elf_offset = 0x2000;
1141
1142 Ehdr ehdr = {};
1143 ehdr.e_shoff = elf_offset;
1144 ehdr.e_shnum = 4;
1145 ehdr.e_shentsize = sizeof(Shdr);
1146 ehdr.e_shstrndx = 2;
1147 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1148
1149 elf_offset += ehdr.e_shentsize;
1150
1151 Shdr shdr = {};
1152 shdr.sh_type = SHT_PROGBITS;
1153 shdr.sh_link = 2;
1154 shdr.sh_name = 0x200;
1155 shdr.sh_addr = addr;
1156 shdr.sh_offset = offset;
1157 shdr.sh_entsize = 0x100;
1158 shdr.sh_size = 0x800;
1159 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1160 elf_offset += ehdr.e_shentsize;
1161
1162 // The string data for section header names.
1163 memset(&shdr, 0, sizeof(shdr));
1164 shdr.sh_type = SHT_STRTAB;
1165 shdr.sh_name = 0x20000;
1166 shdr.sh_offset = 0xf000;
1167 shdr.sh_size = 0x1000;
1168 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1169 elf_offset += ehdr.e_shentsize;
1170
1171 memset(&shdr, 0, sizeof(shdr));
1172 shdr.sh_type = SHT_PROGBITS;
1173 shdr.sh_link = 2;
1174 shdr.sh_name = 0x100;
1175 shdr.sh_addr = 0x5000;
1176 shdr.sh_offset = 0x5000;
1177 shdr.sh_entsize = 0x100;
1178 shdr.sh_size = 0x500;
1179 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1180
1181 fake_memory_->SetMemory(0xf100, ".eh_frame", sizeof(".eh_frame"));
1182 fake_memory_->SetMemory(0xf200, ".eh_frame_hdr", sizeof(".eh_frame_hdr"));
1183
1184 int64_t load_bias = 0;
1185 ASSERT_TRUE(elf->Init(&load_bias));
1186 EXPECT_EQ(0, load_bias);
1187 EXPECT_EQ(0x5000U, elf->eh_frame_info().offset);
1188 EXPECT_EQ(0, elf->eh_frame_info().bias);
1189 EXPECT_EQ(0x500U, elf->eh_frame_info().size);
1190 EXPECT_EQ(0U, elf->eh_frame_info().flags);
1191 EXPECT_EQ(offset, elf->eh_frame_hdr_info().offset);
1192 EXPECT_EQ(expected_bias, elf->eh_frame_hdr_info().bias);
1193 EXPECT_EQ(0x800U, elf->eh_frame_hdr_info().size);
1194 EXPECT_EQ(0U, elf->eh_frame_hdr_info().flags);
1195 }
1196
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_zero_32)1197 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_zero_32) {
1198 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x9000,
1199 0x9000, 0);
1200 }
1201
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_zero_64)1202 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_zero_64) {
1203 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0xa000,
1204 0xa000, 0);
1205 }
1206
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_positive_32)1207 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_positive_32) {
1208 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1209 0x9000, 0x4000, 0x5000);
1210 }
1211
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_positive_64)1212 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_positive_64) {
1213 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1214 0x6000, 0x1000, 0x5000);
1215 }
1216
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_negative_32)1217 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_negative_32) {
1218 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1219 0x3000, 0x5000, -0x2000);
1220 }
1221
TEST_F(ElfInterfaceTest,init_section_headers_offsets_eh_frame_hdr_section_bias_negative_64)1222 TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_negative_64) {
1223 InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1224 0x5000, 0x9000, -0x4000);
1225 }
1226
1227 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
InitSectionHeadersOffsetsDebugFrameSectionBias(uint64_t addr,uint64_t offset,int64_t expected_bias)1228 void ElfInterfaceTest::InitSectionHeadersOffsetsDebugFrameSectionBias(uint64_t addr,
1229 uint64_t offset,
1230 int64_t expected_bias) {
1231 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1232
1233 uint64_t elf_offset = 0x2000;
1234
1235 Ehdr ehdr = {};
1236 ehdr.e_shoff = elf_offset;
1237 ehdr.e_shnum = 3;
1238 ehdr.e_shentsize = sizeof(Shdr);
1239 ehdr.e_shstrndx = 2;
1240 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1241
1242 elf_offset += ehdr.e_shentsize;
1243
1244 Shdr shdr = {};
1245 shdr.sh_type = SHT_PROGBITS;
1246 shdr.sh_link = 2;
1247 shdr.sh_name = 0x100;
1248 shdr.sh_addr = addr;
1249 shdr.sh_offset = offset;
1250 shdr.sh_entsize = 0x100;
1251 shdr.sh_size = 0x800;
1252 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1253 elf_offset += ehdr.e_shentsize;
1254
1255 // The string data for section header names.
1256 memset(&shdr, 0, sizeof(shdr));
1257 shdr.sh_type = SHT_STRTAB;
1258 shdr.sh_name = 0x20000;
1259 shdr.sh_offset = 0xf000;
1260 shdr.sh_size = 0x1000;
1261 fake_memory_->SetMemory(elf_offset, &shdr, sizeof(shdr));
1262
1263 fake_memory_->SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame"));
1264
1265 int64_t load_bias = 0;
1266 ASSERT_TRUE(elf->Init(&load_bias));
1267 EXPECT_EQ(0, load_bias);
1268 EXPECT_EQ(offset, elf->debug_frame_info().offset);
1269 EXPECT_EQ(expected_bias, elf->debug_frame_info().bias);
1270 EXPECT_EQ(0x800U, elf->debug_frame_info().size);
1271 }
1272
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_zero_32)1273 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_zero_32) {
1274 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x5000,
1275 0x5000, 0);
1276 }
1277
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_zero_64)1278 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_zero_64) {
1279 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0xa000,
1280 0xa000, 0);
1281 }
1282
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_positive_32)1283 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_positive_32) {
1284 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1285 0x5000, 0x2000, 0x3000);
1286 }
1287
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_positive_64)1288 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_positive_64) {
1289 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1290 0x7000, 0x1000, 0x6000);
1291 }
1292
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_negative_32)1293 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_negative_32) {
1294 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(
1295 0x6000, 0x7000, -0x1000);
1296 }
1297
TEST_F(ElfInterfaceTest,init_section_headers_offsets_debug_frame_section_bias_negative_64)1298 TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_negative_64) {
1299 InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(
1300 0x3000, 0x5000, -0x2000);
1301 }
1302
1303 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
CheckGnuEhFrame(uint64_t addr,uint64_t offset,int64_t expected_bias)1304 void ElfInterfaceTest::CheckGnuEhFrame(uint64_t addr, uint64_t offset, int64_t expected_bias) {
1305 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(memory_));
1306
1307 Ehdr ehdr = {};
1308 ehdr.e_phoff = 0x100;
1309 ehdr.e_phnum = 2;
1310 ehdr.e_phentsize = sizeof(Phdr);
1311 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1312
1313 uint64_t phdr_offset = 0x100;
1314
1315 Phdr phdr = {};
1316 phdr.p_type = PT_LOAD;
1317 phdr.p_memsz = 0x10000;
1318 phdr.p_flags = PF_R | PF_X;
1319 phdr.p_align = 0x1000;
1320 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
1321 phdr_offset += sizeof(phdr);
1322
1323 memset(&phdr, 0, sizeof(phdr));
1324 phdr.p_type = PT_GNU_EH_FRAME;
1325 phdr.p_vaddr = addr;
1326 phdr.p_offset = offset;
1327 fake_memory_->SetMemory(phdr_offset, &phdr, sizeof(phdr));
1328
1329 int64_t load_bias = 0;
1330 ASSERT_TRUE(elf->Init(&load_bias));
1331 EXPECT_EQ(0, load_bias);
1332 EXPECT_EQ(expected_bias, elf->eh_frame_hdr_info().bias);
1333 }
1334
TEST_F(ElfInterfaceTest,eh_frame_zero_section_bias_32)1335 TEST_F(ElfInterfaceTest, eh_frame_zero_section_bias_32) {
1336 ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x4000, 0);
1337 }
1338
TEST_F(ElfInterfaceTest,eh_frame_zero_section_bias_64)1339 TEST_F(ElfInterfaceTest, eh_frame_zero_section_bias_64) {
1340 ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x4000, 0);
1341 }
1342
TEST_F(ElfInterfaceTest,eh_frame_positive_section_bias_32)1343 TEST_F(ElfInterfaceTest, eh_frame_positive_section_bias_32) {
1344 ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x1000, 0x3000);
1345 }
1346
TEST_F(ElfInterfaceTest,eh_frame_positive_section_bias_64)1347 TEST_F(ElfInterfaceTest, eh_frame_positive_section_bias_64) {
1348 ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x1000, 0x3000);
1349 }
1350
TEST_F(ElfInterfaceTest,eh_frame_negative_section_bias_32)1351 TEST_F(ElfInterfaceTest, eh_frame_negative_section_bias_32) {
1352 ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x5000,
1353 -0x1000);
1354 }
1355
TEST_F(ElfInterfaceTest,eh_frame_negative_section_bias_64)1356 TEST_F(ElfInterfaceTest, eh_frame_negative_section_bias_64) {
1357 ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x5000,
1358 -0x1000);
1359 }
1360
TEST_F(ElfInterfaceTest,is_valid_pc_from_pt_load)1361 TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) {
1362 std::unique_ptr<ElfInterface> elf(new ElfInterface32(memory_));
1363
1364 Elf32_Ehdr ehdr = {};
1365 ehdr.e_phoff = 0x100;
1366 ehdr.e_phnum = 1;
1367 ehdr.e_phentsize = sizeof(Elf32_Phdr);
1368 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1369
1370 Elf32_Phdr phdr = {};
1371 phdr.p_type = PT_LOAD;
1372 phdr.p_vaddr = 0;
1373 phdr.p_memsz = 0x10000;
1374 phdr.p_flags = PF_R | PF_X;
1375 phdr.p_align = 0x1000;
1376 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
1377
1378 int64_t load_bias = 0;
1379 ASSERT_TRUE(elf->Init(&load_bias));
1380 EXPECT_EQ(0, load_bias);
1381 EXPECT_TRUE(elf->IsValidPc(0));
1382 EXPECT_TRUE(elf->IsValidPc(0x5000));
1383 EXPECT_TRUE(elf->IsValidPc(0xffff));
1384 EXPECT_FALSE(elf->IsValidPc(0x10000));
1385 }
1386
TEST_F(ElfInterfaceTest,is_valid_pc_from_pt_load_non_zero_load_bias)1387 TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) {
1388 std::unique_ptr<ElfInterface> elf(new ElfInterface32(memory_));
1389
1390 Elf32_Ehdr ehdr = {};
1391 ehdr.e_phoff = 0x100;
1392 ehdr.e_phnum = 1;
1393 ehdr.e_phentsize = sizeof(Elf32_Phdr);
1394 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1395
1396 Elf32_Phdr phdr = {};
1397 phdr.p_type = PT_LOAD;
1398 phdr.p_vaddr = 0x2000;
1399 phdr.p_memsz = 0x10000;
1400 phdr.p_flags = PF_R | PF_X;
1401 phdr.p_align = 0x1000;
1402 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
1403
1404 int64_t load_bias = 0;
1405 ASSERT_TRUE(elf->Init(&load_bias));
1406 EXPECT_EQ(0x2000, load_bias);
1407 EXPECT_FALSE(elf->IsValidPc(0));
1408 EXPECT_FALSE(elf->IsValidPc(0x1000));
1409 EXPECT_FALSE(elf->IsValidPc(0x1fff));
1410 EXPECT_TRUE(elf->IsValidPc(0x2000));
1411 EXPECT_TRUE(elf->IsValidPc(0x5000));
1412 EXPECT_TRUE(elf->IsValidPc(0x11fff));
1413 EXPECT_FALSE(elf->IsValidPc(0x12000));
1414 }
1415
TEST_F(ElfInterfaceTest,is_valid_pc_from_debug_frame)1416 TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) {
1417 std::unique_ptr<ElfInterface> elf(new ElfInterface32(memory_));
1418
1419 uint64_t sh_offset = 0x100;
1420
1421 Elf32_Ehdr ehdr = {};
1422 ehdr.e_shstrndx = 1;
1423 ehdr.e_shoff = sh_offset;
1424 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1425 ehdr.e_shnum = 3;
1426 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1427
1428 Elf32_Shdr shdr = {};
1429 shdr.sh_type = SHT_NULL;
1430 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1431
1432 sh_offset += sizeof(shdr);
1433 memset(&shdr, 0, sizeof(shdr));
1434 shdr.sh_type = SHT_STRTAB;
1435 shdr.sh_name = 1;
1436 shdr.sh_offset = 0x500;
1437 shdr.sh_size = 0x100;
1438 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1439 fake_memory_->SetMemory(0x500, ".debug_frame");
1440
1441 sh_offset += sizeof(shdr);
1442 memset(&shdr, 0, sizeof(shdr));
1443 shdr.sh_type = SHT_PROGBITS;
1444 shdr.sh_name = 0;
1445 shdr.sh_addr = 0x600;
1446 shdr.sh_offset = 0x600;
1447 shdr.sh_size = 0x200;
1448 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1449
1450 // CIE 32.
1451 fake_memory_->SetData32(0x600, 0xfc);
1452 fake_memory_->SetData32(0x604, 0xffffffff);
1453 fake_memory_->SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1});
1454
1455 // FDE 32.
1456 fake_memory_->SetData32(0x700, 0xfc);
1457 fake_memory_->SetData32(0x704, 0);
1458 fake_memory_->SetData32(0x708, 0x2100);
1459 fake_memory_->SetData32(0x70c, 0x200);
1460
1461 int64_t load_bias = 0;
1462 ASSERT_TRUE(elf->Init(&load_bias));
1463 elf->InitHeaders();
1464 EXPECT_EQ(0, load_bias);
1465 EXPECT_FALSE(elf->IsValidPc(0));
1466 EXPECT_FALSE(elf->IsValidPc(0x20ff));
1467 EXPECT_TRUE(elf->IsValidPc(0x2100));
1468 EXPECT_TRUE(elf->IsValidPc(0x2200));
1469 EXPECT_TRUE(elf->IsValidPc(0x22ff));
1470 EXPECT_FALSE(elf->IsValidPc(0x2300));
1471 }
1472
TEST_F(ElfInterfaceTest,is_valid_pc_from_eh_frame)1473 TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) {
1474 std::unique_ptr<ElfInterface> elf(new ElfInterface32(memory_));
1475
1476 uint64_t sh_offset = 0x100;
1477
1478 Elf32_Ehdr ehdr = {};
1479 ehdr.e_shstrndx = 1;
1480 ehdr.e_shoff = sh_offset;
1481 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1482 ehdr.e_shnum = 3;
1483 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1484
1485 Elf32_Shdr shdr = {};
1486 shdr.sh_type = SHT_NULL;
1487 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1488
1489 sh_offset += sizeof(shdr);
1490 memset(&shdr, 0, sizeof(shdr));
1491 shdr.sh_type = SHT_STRTAB;
1492 shdr.sh_name = 1;
1493 shdr.sh_offset = 0x500;
1494 shdr.sh_size = 0x100;
1495 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1496 fake_memory_->SetMemory(0x500, ".eh_frame");
1497
1498 sh_offset += sizeof(shdr);
1499 memset(&shdr, 0, sizeof(shdr));
1500 shdr.sh_type = SHT_PROGBITS;
1501 shdr.sh_name = 0;
1502 shdr.sh_addr = 0x600;
1503 shdr.sh_offset = 0x600;
1504 shdr.sh_size = 0x200;
1505 fake_memory_->SetMemory(sh_offset, &shdr, sizeof(shdr));
1506
1507 // CIE 32.
1508 fake_memory_->SetData32(0x600, 0xfc);
1509 fake_memory_->SetData32(0x604, 0);
1510 fake_memory_->SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1});
1511
1512 // FDE 32.
1513 fake_memory_->SetData32(0x700, 0xfc);
1514 fake_memory_->SetData32(0x704, 0x104);
1515 fake_memory_->SetData32(0x708, 0x20f8);
1516 fake_memory_->SetData32(0x70c, 0x200);
1517
1518 int64_t load_bias = 0;
1519 ASSERT_TRUE(elf->Init(&load_bias));
1520 elf->InitHeaders();
1521 EXPECT_EQ(0, load_bias);
1522 EXPECT_FALSE(elf->IsValidPc(0));
1523 EXPECT_FALSE(elf->IsValidPc(0x27ff));
1524 EXPECT_TRUE(elf->IsValidPc(0x2800));
1525 EXPECT_TRUE(elf->IsValidPc(0x2900));
1526 EXPECT_TRUE(elf->IsValidPc(0x29ff));
1527 EXPECT_FALSE(elf->IsValidPc(0x2a00));
1528 }
1529
1530 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
BuildID()1531 void ElfInterfaceTest::BuildID() {
1532 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1533
1534 uint64_t offset = 0x2000;
1535
1536 Ehdr ehdr = {};
1537 ehdr.e_shoff = offset;
1538 ehdr.e_shnum = 3;
1539 ehdr.e_shentsize = sizeof(Shdr);
1540 ehdr.e_shstrndx = 2;
1541 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1542
1543 offset += ehdr.e_shentsize;
1544
1545 char note_section[128];
1546 Nhdr note_header = {};
1547 note_header.n_namesz = 4; // "GNU"
1548 note_header.n_descsz = 7; // "BUILDID"
1549 note_header.n_type = NT_GNU_BUILD_ID;
1550 memcpy(¬e_section, ¬e_header, sizeof(note_header));
1551 size_t note_offset = sizeof(note_header);
1552 // The note information contains the GNU and trailing '\0'.
1553 memcpy(¬e_section[note_offset], "GNU", sizeof("GNU"));
1554 note_offset += sizeof("GNU");
1555 // This part of the note does not contain any trailing '\0'.
1556 memcpy(¬e_section[note_offset], "BUILDID", 7);
1557
1558 Shdr shdr = {};
1559 shdr.sh_type = SHT_NOTE;
1560 shdr.sh_name = 0x500;
1561 shdr.sh_offset = 0xb000;
1562 shdr.sh_size = sizeof(note_section);
1563 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1564 offset += ehdr.e_shentsize;
1565
1566 // The string data for section header names.
1567 memset(&shdr, 0, sizeof(shdr));
1568 shdr.sh_type = SHT_STRTAB;
1569 shdr.sh_name = 0x20000;
1570 shdr.sh_offset = 0xf000;
1571 shdr.sh_size = 0x1000;
1572 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1573
1574 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1575 fake_memory_->SetMemory(0xb000, note_section, sizeof(note_section));
1576
1577 int64_t load_bias = 0;
1578 ASSERT_TRUE(elf->Init(&load_bias));
1579 ASSERT_EQ("BUILDID", elf->GetBuildID());
1580 }
1581
TEST_F(ElfInterfaceTest,build_id_32)1582 TEST_F(ElfInterfaceTest, build_id_32) {
1583 BuildID<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>();
1584 }
1585
TEST_F(ElfInterfaceTest,build_id_64)1586 TEST_F(ElfInterfaceTest, build_id_64) {
1587 BuildID<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>();
1588 }
1589
1590 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
BuildIDTwoNotes()1591 void ElfInterfaceTest::BuildIDTwoNotes() {
1592 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1593
1594 uint64_t offset = 0x2000;
1595
1596 Ehdr ehdr = {};
1597 ehdr.e_shoff = offset;
1598 ehdr.e_shnum = 3;
1599 ehdr.e_shentsize = sizeof(Shdr);
1600 ehdr.e_shstrndx = 2;
1601 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1602
1603 offset += ehdr.e_shentsize;
1604
1605 char note_section[128];
1606 Nhdr note_header = {};
1607 note_header.n_namesz = 8; // "WRONG" aligned to 4
1608 note_header.n_descsz = 7; // "BUILDID"
1609 note_header.n_type = NT_GNU_BUILD_ID;
1610 memcpy(¬e_section, ¬e_header, sizeof(note_header));
1611 size_t note_offset = sizeof(note_header);
1612 memcpy(¬e_section[note_offset], "WRONG", sizeof("WRONG"));
1613 note_offset += 8;
1614 // This part of the note does not contain any trailing '\0'.
1615 memcpy(¬e_section[note_offset], "BUILDID", 7);
1616 note_offset += 8;
1617
1618 note_header.n_namesz = 4; // "GNU"
1619 note_header.n_descsz = 7; // "BUILDID"
1620 note_header.n_type = NT_GNU_BUILD_ID;
1621 memcpy(¬e_section[note_offset], ¬e_header, sizeof(note_header));
1622 note_offset += sizeof(note_header);
1623 // The note information contains the GNU and trailing '\0'.
1624 memcpy(¬e_section[note_offset], "GNU", sizeof("GNU"));
1625 note_offset += sizeof("GNU");
1626 // This part of the note does not contain any trailing '\0'.
1627 memcpy(¬e_section[note_offset], "BUILDID", 7);
1628
1629 Shdr shdr = {};
1630 shdr.sh_type = SHT_NOTE;
1631 shdr.sh_name = 0x500;
1632 shdr.sh_offset = 0xb000;
1633 shdr.sh_size = sizeof(note_section);
1634 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1635 offset += ehdr.e_shentsize;
1636
1637 // The string data for section header names.
1638 memset(&shdr, 0, sizeof(shdr));
1639 shdr.sh_type = SHT_STRTAB;
1640 shdr.sh_name = 0x20000;
1641 shdr.sh_offset = 0xf000;
1642 shdr.sh_size = 0x1000;
1643 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1644
1645 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1646 fake_memory_->SetMemory(0xb000, note_section, sizeof(note_section));
1647
1648 int64_t load_bias = 0;
1649 ASSERT_TRUE(elf->Init(&load_bias));
1650 ASSERT_EQ("BUILDID", elf->GetBuildID());
1651 }
1652
TEST_F(ElfInterfaceTest,build_id_two_notes_32)1653 TEST_F(ElfInterfaceTest, build_id_two_notes_32) {
1654 BuildIDTwoNotes<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>();
1655 }
1656
TEST_F(ElfInterfaceTest,build_id_two_notes_64)1657 TEST_F(ElfInterfaceTest, build_id_two_notes_64) {
1658 BuildIDTwoNotes<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>();
1659 }
1660
1661 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
BuildIDSectionTooSmallForName()1662 void ElfInterfaceTest::BuildIDSectionTooSmallForName () {
1663 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1664
1665 uint64_t offset = 0x2000;
1666
1667 Ehdr ehdr = {};
1668 ehdr.e_shoff = offset;
1669 ehdr.e_shnum = 3;
1670 ehdr.e_shentsize = sizeof(Shdr);
1671 ehdr.e_shstrndx = 2;
1672 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1673
1674 offset += ehdr.e_shentsize;
1675
1676 char note_section[128];
1677 Nhdr note_header = {};
1678 note_header.n_namesz = 4; // "GNU"
1679 note_header.n_descsz = 7; // "BUILDID"
1680 note_header.n_type = NT_GNU_BUILD_ID;
1681 memcpy(¬e_section, ¬e_header, sizeof(note_header));
1682 size_t note_offset = sizeof(note_header);
1683 // The note information contains the GNU and trailing '\0'.
1684 memcpy(¬e_section[note_offset], "GNU", sizeof("GNU"));
1685 note_offset += sizeof("GNU");
1686 // This part of the note does not contain any trailing '\0'.
1687 memcpy(¬e_section[note_offset], "BUILDID", 7);
1688
1689 Shdr shdr = {};
1690 shdr.sh_type = SHT_NOTE;
1691 shdr.sh_name = 0x500;
1692 shdr.sh_offset = 0xb000;
1693 shdr.sh_size = sizeof(note_header) + 1;
1694 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1695 offset += ehdr.e_shentsize;
1696
1697 // The string data for section header names.
1698 memset(&shdr, 0, sizeof(shdr));
1699 shdr.sh_type = SHT_STRTAB;
1700 shdr.sh_name = 0x20000;
1701 shdr.sh_offset = 0xf000;
1702 shdr.sh_size = 0x1000;
1703 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1704
1705 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1706 fake_memory_->SetMemory(0xb000, note_section, sizeof(note_section));
1707
1708 int64_t load_bias = 0;
1709 ASSERT_TRUE(elf->Init(&load_bias));
1710 ASSERT_EQ("", elf->GetBuildID());
1711 }
1712
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_name_32)1713 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name_32) {
1714 BuildIDSectionTooSmallForName<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>();
1715 }
1716
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_name_64)1717 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name_64) {
1718 BuildIDSectionTooSmallForName<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>();
1719 }
1720
1721 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
BuildIDSectionTooSmallForDesc()1722 void ElfInterfaceTest::BuildIDSectionTooSmallForDesc () {
1723 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1724
1725 uint64_t offset = 0x2000;
1726
1727 Ehdr ehdr = {};
1728 ehdr.e_shoff = offset;
1729 ehdr.e_shnum = 3;
1730 ehdr.e_shentsize = sizeof(Shdr);
1731 ehdr.e_shstrndx = 2;
1732 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1733
1734 offset += ehdr.e_shentsize;
1735
1736 char note_section[128];
1737 Nhdr note_header = {};
1738 note_header.n_namesz = 4; // "GNU"
1739 note_header.n_descsz = 7; // "BUILDID"
1740 note_header.n_type = NT_GNU_BUILD_ID;
1741 memcpy(¬e_section, ¬e_header, sizeof(note_header));
1742 size_t note_offset = sizeof(note_header);
1743 // The note information contains the GNU and trailing '\0'.
1744 memcpy(¬e_section[note_offset], "GNU", sizeof("GNU"));
1745 note_offset += sizeof("GNU");
1746 // This part of the note does not contain any trailing '\0'.
1747 memcpy(¬e_section[note_offset], "BUILDID", 7);
1748
1749 Shdr shdr = {};
1750 shdr.sh_type = SHT_NOTE;
1751 shdr.sh_name = 0x500;
1752 shdr.sh_offset = 0xb000;
1753 shdr.sh_size = sizeof(note_header) + sizeof("GNU") + 1;
1754 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1755 offset += ehdr.e_shentsize;
1756
1757 // The string data for section header names.
1758 memset(&shdr, 0, sizeof(shdr));
1759 shdr.sh_type = SHT_STRTAB;
1760 shdr.sh_name = 0x20000;
1761 shdr.sh_offset = 0xf000;
1762 shdr.sh_size = 0x1000;
1763 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1764
1765 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1766 fake_memory_->SetMemory(0xb000, note_section, sizeof(note_section));
1767
1768 int64_t load_bias = 0;
1769 ASSERT_TRUE(elf->Init(&load_bias));
1770 ASSERT_EQ("", elf->GetBuildID());
1771 }
1772
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_desc_32)1773 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc_32) {
1774 BuildIDSectionTooSmallForDesc<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>();
1775 }
1776
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_desc_64)1777 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc_64) {
1778 BuildIDSectionTooSmallForDesc<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>();
1779 }
1780
1781 template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType>
BuildIDSectionTooSmallForHeader()1782 void ElfInterfaceTest::BuildIDSectionTooSmallForHeader () {
1783 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1784
1785 uint64_t offset = 0x2000;
1786
1787 Ehdr ehdr = {};
1788 ehdr.e_shoff = offset;
1789 ehdr.e_shnum = 3;
1790 ehdr.e_shentsize = sizeof(Shdr);
1791 ehdr.e_shstrndx = 2;
1792 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1793
1794 offset += ehdr.e_shentsize;
1795
1796 char note_section[128];
1797 Nhdr note_header = {};
1798 note_header.n_namesz = 4; // "GNU"
1799 note_header.n_descsz = 7; // "BUILDID"
1800 note_header.n_type = NT_GNU_BUILD_ID;
1801 memcpy(¬e_section, ¬e_header, sizeof(note_header));
1802 size_t note_offset = sizeof(note_header);
1803 // The note information contains the GNU and trailing '\0'.
1804 memcpy(¬e_section[note_offset], "GNU", sizeof("GNU"));
1805 note_offset += sizeof("GNU");
1806 // This part of the note does not contain any trailing '\0'.
1807 memcpy(¬e_section[note_offset], "BUILDID", 7);
1808
1809 Shdr shdr = {};
1810 shdr.sh_type = SHT_NOTE;
1811 shdr.sh_name = 0x500;
1812 shdr.sh_offset = 0xb000;
1813 shdr.sh_size = sizeof(note_header) - 1;
1814 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1815 offset += ehdr.e_shentsize;
1816
1817 // The string data for section header names.
1818 memset(&shdr, 0, sizeof(shdr));
1819 shdr.sh_type = SHT_STRTAB;
1820 shdr.sh_name = 0x20000;
1821 shdr.sh_offset = 0xf000;
1822 shdr.sh_size = 0x1000;
1823 fake_memory_->SetMemory(offset, &shdr, sizeof(shdr));
1824
1825 fake_memory_->SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id"));
1826 fake_memory_->SetMemory(0xb000, note_section, sizeof(note_section));
1827
1828 int64_t load_bias = 0;
1829 ASSERT_TRUE(elf->Init(&load_bias));
1830 ASSERT_EQ("", elf->GetBuildID());
1831 }
1832
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_header_32)1833 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header_32) {
1834 BuildIDSectionTooSmallForHeader<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>();
1835 }
1836
TEST_F(ElfInterfaceTest,build_id_section_too_small_for_header_64)1837 TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header_64) {
1838 BuildIDSectionTooSmallForHeader<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>();
1839 }
1840
1841 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
CheckLoadBiasInFirstPhdr(int64_t load_bias)1842 void ElfInterfaceTest::CheckLoadBiasInFirstPhdr(int64_t load_bias) {
1843 Ehdr ehdr = {};
1844 ehdr.e_phoff = 0x100;
1845 ehdr.e_phnum = 2;
1846 ehdr.e_phentsize = sizeof(Phdr);
1847 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1848
1849 Phdr phdr = {};
1850 phdr.p_type = PT_LOAD;
1851 phdr.p_offset = 0;
1852 phdr.p_vaddr = load_bias;
1853 phdr.p_memsz = 0x10000;
1854 phdr.p_flags = PF_R | PF_X;
1855 phdr.p_align = 0x1000;
1856 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
1857
1858 memset(&phdr, 0, sizeof(phdr));
1859 phdr.p_type = PT_LOAD;
1860 phdr.p_offset = 0x1000;
1861 phdr.p_memsz = 0x2000;
1862 phdr.p_flags = PF_R | PF_X;
1863 phdr.p_align = 0x1000;
1864 fake_memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
1865
1866 int64_t static_load_bias = ElfInterface::GetLoadBias<Ehdr, Phdr>(memory_.get());
1867 ASSERT_EQ(load_bias, static_load_bias);
1868
1869 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1870 int64_t init_load_bias = 0;
1871 ASSERT_TRUE(elf->Init(&init_load_bias));
1872 ASSERT_EQ(init_load_bias, static_load_bias);
1873 }
1874
TEST_F(ElfInterfaceTest,get_load_bias_zero_32)1875 TEST_F(ElfInterfaceTest, get_load_bias_zero_32) {
1876 CheckLoadBiasInFirstPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0);
1877 }
1878
TEST_F(ElfInterfaceTest,get_load_bias_zero_64)1879 TEST_F(ElfInterfaceTest, get_load_bias_zero_64) {
1880 CheckLoadBiasInFirstPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0);
1881 }
1882
TEST_F(ElfInterfaceTest,get_load_bias_non_zero_32)1883 TEST_F(ElfInterfaceTest, get_load_bias_non_zero_32) {
1884 CheckLoadBiasInFirstPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000);
1885 }
1886
TEST_F(ElfInterfaceTest,get_load_bias_non_zero_64)1887 TEST_F(ElfInterfaceTest, get_load_bias_non_zero_64) {
1888 CheckLoadBiasInFirstPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000);
1889 }
1890
1891 template <typename Ehdr, typename Phdr, typename ElfInterfaceType>
CheckLoadBiasInFirstExecPhdr(uint64_t offset,uint64_t vaddr,int64_t load_bias)1892 void ElfInterfaceTest::CheckLoadBiasInFirstExecPhdr(uint64_t offset, uint64_t vaddr,
1893 int64_t load_bias) {
1894 Ehdr ehdr = {};
1895 ehdr.e_phoff = 0x100;
1896 ehdr.e_phnum = 3;
1897 ehdr.e_phentsize = sizeof(Phdr);
1898 fake_memory_->SetMemory(0, &ehdr, sizeof(ehdr));
1899
1900 Phdr phdr = {};
1901 phdr.p_type = PT_LOAD;
1902 phdr.p_memsz = 0x10000;
1903 phdr.p_flags = PF_R;
1904 phdr.p_align = 0x1000;
1905 fake_memory_->SetMemory(0x100, &phdr, sizeof(phdr));
1906
1907 memset(&phdr, 0, sizeof(phdr));
1908 phdr.p_type = PT_LOAD;
1909 phdr.p_offset = offset;
1910 phdr.p_vaddr = vaddr;
1911 phdr.p_memsz = 0x2000;
1912 phdr.p_flags = PF_R | PF_X;
1913 phdr.p_align = 0x1000;
1914 fake_memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
1915
1916 // Second executable load should be ignored for load bias computation.
1917 memset(&phdr, 0, sizeof(phdr));
1918 phdr.p_type = PT_LOAD;
1919 phdr.p_offset = 0x1234;
1920 phdr.p_vaddr = 0x2000;
1921 phdr.p_memsz = 0x2000;
1922 phdr.p_flags = PF_R | PF_X;
1923 phdr.p_align = 0x1000;
1924 fake_memory_->SetMemory(0x200 + sizeof(phdr), &phdr, sizeof(phdr));
1925
1926 int64_t static_load_bias = ElfInterface::GetLoadBias<Ehdr, Phdr>(memory_.get());
1927 ASSERT_EQ(load_bias, static_load_bias);
1928
1929 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(memory_));
1930 int64_t init_load_bias = 0;
1931 ASSERT_TRUE(elf->Init(&init_load_bias));
1932 ASSERT_EQ(init_load_bias, static_load_bias);
1933 }
1934
TEST_F(ElfInterfaceTest,get_load_bias_exec_zero_32)1935 TEST_F(ElfInterfaceTest, get_load_bias_exec_zero_32) {
1936 CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000, 0x1000, 0);
1937 }
1938
TEST_F(ElfInterfaceTest,get_load_bias_exec_zero_64)1939 TEST_F(ElfInterfaceTest, get_load_bias_exec_zero_64) {
1940 CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000, 0x1000, 0);
1941 }
1942
TEST_F(ElfInterfaceTest,get_load_bias_exec_positive_32)1943 TEST_F(ElfInterfaceTest, get_load_bias_exec_positive_32) {
1944 CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000, 0x4000, 0x3000);
1945 }
1946
TEST_F(ElfInterfaceTest,get_load_bias_exec_positive_64)1947 TEST_F(ElfInterfaceTest, get_load_bias_exec_positive_64) {
1948 CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000, 0x4000, 0x3000);
1949 }
1950
TEST_F(ElfInterfaceTest,get_load_bias_exec_negative_32)1951 TEST_F(ElfInterfaceTest, get_load_bias_exec_negative_32) {
1952 CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x5000, 0x1000, -0x4000);
1953 }
1954
TEST_F(ElfInterfaceTest,get_load_bias_exec_negative_64)1955 TEST_F(ElfInterfaceTest, get_load_bias_exec_negative_64) {
1956 CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x5000, 0x1000, -0x4000);
1957 }
1958
TEST_F(ElfInterfaceTest,huge_gnu_debugdata_size)1959 TEST_F(ElfInterfaceTest, huge_gnu_debugdata_size) {
1960 std::shared_ptr<Memory> empty;
1961 ElfInterfaceFake interface(empty);
1962
1963 interface.FakeSetGnuDebugdataOffset(0x1000);
1964 interface.FakeSetGnuDebugdataSize(0xffffffffffffffffUL);
1965 ASSERT_TRUE(interface.CreateGnuDebugdataMemory() == nullptr);
1966
1967 interface.FakeSetGnuDebugdataSize(0x4000000000000UL);
1968 ASSERT_TRUE(interface.CreateGnuDebugdataMemory() == nullptr);
1969
1970 // This should exceed the size_t value of the first allocation.
1971 #if defined(__LP64__)
1972 interface.FakeSetGnuDebugdataSize(0x3333333333333334ULL);
1973 #else
1974 interface.FakeSetGnuDebugdataSize(0x33333334);
1975 #endif
1976 ASSERT_TRUE(interface.CreateGnuDebugdataMemory() == nullptr);
1977 }
1978
TEST_F(ElfInterfaceTest,compressed_eh_frames)1979 TEST_F(ElfInterfaceTest, compressed_eh_frames) {
1980 SectionInfo eh_hdr_info = {.offset = 0x1000};
1981 uint8_t data[5] = {/*version*/ 1, /*ptr_encoding DW_EH_PE_omit*/ 0xff,
1982 /*fde_count_encoding DW_EH_PE_udata1*/ 0xd,
1983 /*table_encoding DW_EH_PE_absptr*/ 0, /*fde_count*/ 1};
1984 fake_memory_->SetMemory(0x1000, data, sizeof(data));
1985 SectionInfo eh_info = {.offset = 0x2000};
1986
1987 // Verify that the eh_frame and eh_frame_hdr are created properly.
1988 ElfInterface32Fake interface(memory_);
1989 eh_hdr_info.flags = 0;
1990 interface.FakeSetEhFrameHdrInfo(eh_hdr_info);
1991 eh_info.flags = 0;
1992 interface.FakeSetEhFrameInfo(eh_info);
1993 interface.InitHeaders();
1994 EXPECT_NE(0U, interface.eh_frame_hdr_info().offset);
1995 EXPECT_NE(0U, interface.eh_frame_info().offset);
1996 EXPECT_TRUE(interface.eh_frame() != nullptr);
1997
1998 // Init setting SHF_COMPRESSED for both sections, both should fail to init.
1999 ElfInterface32Fake interface_both(memory_);
2000 eh_hdr_info.flags = 0x800;
2001 interface_both.FakeSetEhFrameHdrInfo(eh_hdr_info);
2002 eh_info.flags = 0x800;
2003 interface_both.FakeSetEhFrameInfo(eh_info);
2004 interface_both.InitHeaders();
2005 EXPECT_EQ(0U, interface_both.eh_frame_hdr_info().offset);
2006 EXPECT_EQ(0U, interface_both.eh_frame_info().offset);
2007 EXPECT_TRUE(interface_both.eh_frame() == nullptr);
2008
2009 // Init setting SHF_COMPRESSED for only the eh_frame_hdr, eh_frame should init.
2010 ElfInterface32Fake interface_hdr(memory_);
2011 eh_hdr_info.flags = 0x800;
2012 interface_hdr.FakeSetEhFrameHdrInfo(eh_hdr_info);
2013 eh_info.flags = 0;
2014 interface_hdr.FakeSetEhFrameInfo(eh_info);
2015 interface_hdr.InitHeaders();
2016 EXPECT_EQ(0U, interface_hdr.eh_frame_hdr_info().offset);
2017 EXPECT_NE(0U, interface_hdr.eh_frame_info().offset);
2018 EXPECT_TRUE(interface_hdr.eh_frame() != nullptr);
2019
2020 // Init setting SHF_COMPRESSED for only the eh_frame, both should fail to init.
2021 ElfInterface32Fake interface_eh(memory_);
2022 eh_hdr_info.flags = 0;
2023 interface_eh.FakeSetEhFrameHdrInfo(eh_hdr_info);
2024 eh_info.flags = 0x800;
2025 interface_eh.FakeSetEhFrameInfo(eh_info);
2026 interface_eh.InitHeaders();
2027 EXPECT_EQ(0U, interface_eh.eh_frame_hdr_info().offset);
2028 EXPECT_EQ(0U, interface_eh.eh_frame_info().offset);
2029 EXPECT_TRUE(interface_eh.eh_frame() == nullptr);
2030 }
2031
TEST_F(ElfInterfaceTest,compressed_debug_frame_fde_verify)2032 TEST_F(ElfInterfaceTest, compressed_debug_frame_fde_verify) {
2033 std::string lib_dir = TestGetFileDirectory() + "libs/";
2034 auto elf_memory = Memory::CreateFileMemory(lib_dir + "libc.so", 0);
2035 Elf elf(elf_memory);
2036 ASSERT_TRUE(elf.Init());
2037 ASSERT_TRUE(elf.valid());
2038 auto section = elf.interface()->debug_frame();
2039 ASSERT_TRUE(section != nullptr);
2040
2041 elf_memory = Memory::CreateFileMemory(lib_dir + "libc_zlib.so", 0);
2042 Elf elf_zlib(elf_memory);
2043 ASSERT_TRUE(elf_zlib.Init());
2044 ASSERT_TRUE(elf_zlib.valid());
2045 auto section_zlib = elf_zlib.interface()->debug_frame();
2046 ASSERT_TRUE(section_zlib != nullptr);
2047
2048 elf_memory = Memory::CreateFileMemory(lib_dir + "libc_zstd.so", 0);
2049 Elf elf_zstd(elf_memory);
2050 ASSERT_TRUE(elf_zstd.Init());
2051 ASSERT_TRUE(elf_zstd.valid());
2052 auto section_zstd = elf_zstd.interface()->debug_frame();
2053 ASSERT_TRUE(section_zstd != nullptr);
2054
2055 auto iter = section->begin();
2056 auto iter_zlib = section_zlib->begin();
2057 auto iter_zstd = section_zstd->begin();
2058
2059 // Check that all of the fdes are in the same order, and contain the same data.
2060 size_t total_fdes = 0;
2061 while (iter != section->end() && iter_zlib != section_zlib->end() &&
2062 iter_zstd != section_zstd->end()) {
2063 ASSERT_TRUE(iter != section->end());
2064 ASSERT_TRUE(iter_zlib != section_zlib->end());
2065 ASSERT_TRUE(iter_zstd != section_zstd->end());
2066 auto fde = *iter;
2067 auto fde_zlib = *iter_zlib;
2068 auto fde_zstd = *iter_zstd;
2069 EXPECT_EQ(fde->cie_offset, fde_zlib->cie_offset);
2070 EXPECT_EQ(fde->cie_offset, fde_zstd->cie_offset);
2071 EXPECT_EQ(fde->cfa_instructions_offset, fde_zlib->cfa_instructions_offset);
2072 EXPECT_EQ(fde->cfa_instructions_offset, fde_zstd->cfa_instructions_offset);
2073 EXPECT_EQ(fde->cfa_instructions_end, fde_zlib->cfa_instructions_end);
2074 EXPECT_EQ(fde->cfa_instructions_end, fde_zstd->cfa_instructions_end);
2075 EXPECT_EQ(fde->pc_start, fde_zlib->pc_start);
2076 EXPECT_EQ(fde->pc_start, fde_zstd->pc_start);
2077 EXPECT_EQ(fde->pc_end, fde_zlib->pc_end);
2078 EXPECT_EQ(fde->pc_end, fde_zstd->pc_end);
2079 EXPECT_EQ(fde->lsda_address, fde_zlib->lsda_address);
2080 EXPECT_EQ(fde->lsda_address, fde_zstd->lsda_address);
2081 ++iter;
2082 ++iter_zlib;
2083 ++iter_zstd;
2084 ++total_fdes;
2085 }
2086 EXPECT_EQ(2320U, total_fdes);
2087 }
2088
TEST_F(ElfInterfaceTest,compressed_debug_frame_from_memory)2089 TEST_F(ElfInterfaceTest, compressed_debug_frame_from_memory) {
2090 std::string lib_dir = TestGetFileDirectory() + "libs/";
2091 android::base::unique_fd fd(
2092 TEMP_FAILURE_RETRY(open((lib_dir + "libc_zstd.so").c_str(), O_RDONLY | O_CLOEXEC)));
2093 ASSERT_NE(-1, fd);
2094 struct stat buf;
2095 ASSERT_NE(-1, fstat(fd, &buf));
2096 void* map_addr = mmap(nullptr, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2097 ASSERT_NE(MAP_FAILED, map_addr);
2098 auto process_memory = Memory::CreateProcessMemory(getpid());
2099 std::shared_ptr<Memory> elf_memory(
2100 new MemoryRange(process_memory, reinterpret_cast<uint64_t>(map_addr), buf.st_size, 0));
2101
2102 Elf elf(elf_memory);
2103 ASSERT_TRUE(elf.Init());
2104 ASSERT_TRUE(elf.valid());
2105 auto section = elf.interface()->debug_frame();
2106 ASSERT_TRUE(section != nullptr);
2107
2108 // Don't check all of the fdes, just verify the first one.
2109 std::vector<const DwarfFde*> fdes;
2110 section->GetFdes(&fdes);
2111 EXPECT_EQ(0x9309cU, fdes[0]->cie_offset);
2112 EXPECT_EQ(0x930c0U, fdes[0]->cfa_instructions_offset);
2113 EXPECT_EQ(0x930c0U, fdes[0]->cfa_instructions_end);
2114 EXPECT_EQ(0U, fdes[0]->pc_start);
2115 EXPECT_EQ(2U, fdes[0]->pc_end);
2116 EXPECT_EQ(0U, fdes[0]->lsda_address);
2117 EXPECT_EQ(2320U, fdes.size());
2118
2119 munmap(map_addr, buf.st_size);
2120 }
2121
TEST_F(ElfInterfaceTest,bad_compressed_debug_frame)2122 TEST_F(ElfInterfaceTest, bad_compressed_debug_frame) {
2123 std::string lib_dir = TestGetFileDirectory() + "libs/";
2124 auto elf_memory = Memory::CreateFileMemory(TestGetFileDirectory() + "libs/elf_bad_compress", 0);
2125 Elf elf(elf_memory);
2126 ASSERT_TRUE(elf.Init());
2127 ASSERT_TRUE(elf.valid());
2128 // This elf file has a compressed debug frame, but it's bad compress data.
2129 ASSERT_TRUE(elf.interface()->debug_frame() == nullptr);
2130 }
2131
2132 } // namespace unwindstack
2133