Home | History | Annotate | Download | only in tests
      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:
     43   void SetUp() override {
     44     memory_ = new MemoryFake;
     45   }
     46 
     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 
     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 
    110 TEST_F(ElfTest, invalid_memory) {
    111   Elf elf(memory_);
    112 
    113   ASSERT_FALSE(elf.Init(false));
    114   ASSERT_FALSE(elf.valid());
    115 }
    116 
    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(false));
    126   ASSERT_FALSE(elf.valid());
    127   ASSERT_TRUE(elf.interface() == nullptr);
    128 
    129   std::string name;
    130   ASSERT_FALSE(elf.GetSoname(&name));
    131 
    132   uint64_t func_offset;
    133   ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
    134 
    135   bool finished;
    136   ASSERT_FALSE(elf.Step(0, 0, nullptr, nullptr, &finished));
    137 }
    138 
    139 TEST_F(ElfTest, elf32_invalid_machine) {
    140   Elf elf(memory_);
    141 
    142   InitElf32(EM_PPC);
    143 
    144   ResetLogs();
    145   ASSERT_FALSE(elf.Init(false));
    146 
    147   ASSERT_EQ("", GetFakeLogBuf());
    148   ASSERT_EQ("4 unwind 32 bit elf that is neither arm nor x86 nor mips: e_machine = 20\n\n",
    149             GetFakeLogPrint());
    150 }
    151 
    152 TEST_F(ElfTest, elf64_invalid_machine) {
    153   Elf elf(memory_);
    154 
    155   InitElf64(EM_PPC64);
    156 
    157   ResetLogs();
    158   ASSERT_FALSE(elf.Init(false));
    159 
    160   ASSERT_EQ("", GetFakeLogBuf());
    161   ASSERT_EQ("4 unwind 64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = 21\n\n",
    162             GetFakeLogPrint());
    163 }
    164 
    165 TEST_F(ElfTest, elf_arm) {
    166   Elf elf(memory_);
    167 
    168   InitElf32(EM_ARM);
    169 
    170   ASSERT_TRUE(elf.Init(false));
    171   ASSERT_TRUE(elf.valid());
    172   ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
    173   ASSERT_EQ(ELFCLASS32, elf.class_type());
    174   ASSERT_TRUE(elf.interface() != nullptr);
    175 }
    176 
    177 TEST_F(ElfTest, elf_mips) {
    178   Elf elf(memory_);
    179 
    180   InitElf32(EM_MIPS);
    181 
    182   ASSERT_TRUE(elf.Init(false));
    183   ASSERT_TRUE(elf.valid());
    184   ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
    185   ASSERT_EQ(ELFCLASS32, elf.class_type());
    186   ASSERT_TRUE(elf.interface() != nullptr);
    187 }
    188 
    189 TEST_F(ElfTest, elf_x86) {
    190   Elf elf(memory_);
    191 
    192   InitElf32(EM_386);
    193 
    194   ASSERT_TRUE(elf.Init(false));
    195   ASSERT_TRUE(elf.valid());
    196   ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
    197   ASSERT_EQ(ELFCLASS32, elf.class_type());
    198   ASSERT_TRUE(elf.interface() != nullptr);
    199 }
    200 
    201 TEST_F(ElfTest, elf_arm64) {
    202   Elf elf(memory_);
    203 
    204   InitElf64(EM_AARCH64);
    205 
    206   ASSERT_TRUE(elf.Init(false));
    207   ASSERT_TRUE(elf.valid());
    208   ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
    209   ASSERT_EQ(ELFCLASS64, elf.class_type());
    210   ASSERT_TRUE(elf.interface() != nullptr);
    211 }
    212 
    213 TEST_F(ElfTest, elf_x86_64) {
    214   Elf elf(memory_);
    215 
    216   InitElf64(EM_X86_64);
    217 
    218   ASSERT_TRUE(elf.Init(false));
    219   ASSERT_TRUE(elf.valid());
    220   ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
    221   ASSERT_EQ(ELFCLASS64, elf.class_type());
    222   ASSERT_TRUE(elf.interface() != nullptr);
    223 }
    224 
    225 TEST_F(ElfTest, elf_mips64) {
    226   Elf elf(memory_);
    227 
    228   InitElf64(EM_MIPS);
    229 
    230   ASSERT_TRUE(elf.Init(false));
    231   ASSERT_TRUE(elf.valid());
    232   ASSERT_EQ(static_cast<uint32_t>(EM_MIPS), elf.machine_type());
    233   ASSERT_EQ(ELFCLASS64, elf.class_type());
    234   ASSERT_TRUE(elf.interface() != nullptr);
    235 }
    236 
    237 TEST_F(ElfTest, gnu_debugdata_init_fail32) {
    238   TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, false,
    239                                                [&](uint64_t offset, const void* ptr, size_t size) {
    240                                                  memory_->SetMemory(offset, ptr, size);
    241                                                });
    242 
    243   Elf elf(memory_);
    244   ASSERT_TRUE(elf.Init(false));
    245   ASSERT_TRUE(elf.interface() != nullptr);
    246   ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
    247   EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
    248   EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
    249 }
    250 
    251 TEST_F(ElfTest, gnu_debugdata_init_fail64) {
    252   TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, false,
    253                                                [&](uint64_t offset, const void* ptr, size_t size) {
    254                                                  memory_->SetMemory(offset, ptr, size);
    255                                                });
    256 
    257   Elf elf(memory_);
    258   ASSERT_TRUE(elf.Init(false));
    259   ASSERT_TRUE(elf.interface() != nullptr);
    260   ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
    261   EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
    262   EXPECT_EQ(0x100U, elf.interface()->gnu_debugdata_size());
    263 }
    264 
    265 TEST_F(ElfTest, gnu_debugdata_init32) {
    266   TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
    267                                                [&](uint64_t offset, const void* ptr, size_t size) {
    268                                                  memory_->SetMemory(offset, ptr, size);
    269                                                });
    270 
    271   Elf elf(memory_);
    272   ASSERT_TRUE(elf.Init(true));
    273   ASSERT_TRUE(elf.interface() != nullptr);
    274   ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
    275   EXPECT_EQ(0x1acU, elf.interface()->gnu_debugdata_offset());
    276   EXPECT_EQ(0x8cU, elf.interface()->gnu_debugdata_size());
    277 }
    278 
    279 TEST_F(ElfTest, gnu_debugdata_init64) {
    280   TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
    281                                                [&](uint64_t offset, const void* ptr, size_t size) {
    282                                                  memory_->SetMemory(offset, ptr, size);
    283                                                });
    284 
    285   Elf elf(memory_);
    286   ASSERT_TRUE(elf.Init(true));
    287   ASSERT_TRUE(elf.interface() != nullptr);
    288   ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
    289   EXPECT_EQ(0x200U, elf.interface()->gnu_debugdata_offset());
    290   EXPECT_EQ(0x90U, elf.interface()->gnu_debugdata_size());
    291 }
    292 
    293 TEST_F(ElfTest, rel_pc) {
    294   ElfFake elf(memory_);
    295 
    296   ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
    297   elf.FakeSetInterface(interface);
    298 
    299   elf.FakeSetValid(true);
    300   elf.FakeSetLoadBias(0);
    301   MapInfo map_info(0x1000, 0x2000);
    302 
    303   ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
    304 
    305   elf.FakeSetLoadBias(0x3000);
    306   ASSERT_EQ(0x3101U, elf.GetRelPc(0x1101, &map_info));
    307 
    308   elf.FakeSetValid(false);
    309   elf.FakeSetLoadBias(0);
    310   ASSERT_EQ(0x101U, elf.GetRelPc(0x1101, &map_info));
    311 }
    312 
    313 TEST_F(ElfTest, step_in_signal_map) {
    314   ElfFake elf(memory_);
    315 
    316   RegsArm regs;
    317   regs[13] = 0x50000;
    318   regs[15] = 0x8000;
    319 
    320   ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
    321   elf.FakeSetInterface(interface);
    322 
    323   memory_->SetData32(0x3000, 0xdf0027ad);
    324   MemoryFake process_memory;
    325   process_memory.SetData32(0x50000, 0);
    326   for (size_t i = 0; i < 16; i++) {
    327     process_memory.SetData32(0x500a0 + i * sizeof(uint32_t), i);
    328   }
    329 
    330   elf.FakeSetValid(true);
    331   elf.FakeSetLoadBias(0);
    332   bool finished;
    333   ASSERT_TRUE(elf.Step(0x3000, 0x1000, &regs, &process_memory, &finished));
    334   EXPECT_FALSE(finished);
    335   EXPECT_EQ(15U, regs.pc());
    336   EXPECT_EQ(13U, regs.sp());
    337 }
    338 
    339 class ElfInterfaceMock : public ElfInterface {
    340  public:
    341   ElfInterfaceMock(Memory* memory) : ElfInterface(memory) {}
    342   virtual ~ElfInterfaceMock() = default;
    343 
    344   bool Init(uint64_t*) override { return false; }
    345   void InitHeaders() override {}
    346   bool GetSoname(std::string*) override { return false; }
    347   bool GetFunctionName(uint64_t, uint64_t, std::string*, uint64_t*) override { return false; }
    348 
    349   MOCK_METHOD5(Step, bool(uint64_t, uint64_t, Regs*, Memory*, bool*));
    350   MOCK_METHOD2(GetGlobalVariable, bool(const std::string&, uint64_t*));
    351   MOCK_METHOD1(IsValidPc, bool(uint64_t));
    352 
    353   void MockSetDynamicOffset(uint64_t offset) { dynamic_offset_ = offset; }
    354   void MockSetDynamicVaddr(uint64_t vaddr) { dynamic_vaddr_ = vaddr; }
    355   void MockSetDynamicSize(uint64_t size) { dynamic_size_ = size; }
    356 };
    357 
    358 TEST_F(ElfTest, step_in_interface) {
    359   ElfFake elf(memory_);
    360   elf.FakeSetValid(true);
    361   elf.FakeSetLoadBias(0);
    362 
    363   RegsArm regs;
    364 
    365   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    366   elf.FakeSetInterface(interface);
    367   MemoryFake process_memory;
    368 
    369   bool finished;
    370   EXPECT_CALL(*interface, Step(0x1000, 0, &regs, &process_memory, &finished))
    371       .WillOnce(::testing::Return(true));
    372 
    373   ASSERT_TRUE(elf.Step(0x1004, 0x1000, &regs, &process_memory, &finished));
    374 }
    375 
    376 TEST_F(ElfTest, step_in_interface_non_zero_load_bias) {
    377   ElfFake elf(memory_);
    378   elf.FakeSetValid(true);
    379   elf.FakeSetLoadBias(0x4000);
    380 
    381   RegsArm regs;
    382 
    383   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    384   elf.FakeSetInterface(interface);
    385   MemoryFake process_memory;
    386 
    387   bool finished;
    388   EXPECT_CALL(*interface, Step(0x7300, 0x4000, &regs, &process_memory, &finished))
    389       .WillOnce(::testing::Return(true));
    390 
    391   ASSERT_TRUE(elf.Step(0x7304, 0x7300, &regs, &process_memory, &finished));
    392 }
    393 
    394 TEST_F(ElfTest, get_global_invalid_elf) {
    395   ElfFake elf(memory_);
    396   elf.FakeSetValid(false);
    397 
    398   std::string global("something");
    399   uint64_t offset;
    400   ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
    401 }
    402 
    403 TEST_F(ElfTest, get_global_valid_not_in_interface) {
    404   ElfFake elf(memory_);
    405   elf.FakeSetValid(true);
    406   elf.FakeSetLoadBias(0);
    407 
    408   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    409   elf.FakeSetInterface(interface);
    410 
    411   uint64_t offset;
    412   std::string global("something");
    413   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
    414 
    415   ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
    416 }
    417 
    418 TEST_F(ElfTest, get_global_valid_below_load_bias) {
    419   ElfFake elf(memory_);
    420   elf.FakeSetValid(true);
    421   elf.FakeSetLoadBias(0x1000);
    422 
    423   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    424   elf.FakeSetInterface(interface);
    425 
    426   uint64_t offset;
    427   std::string global("something");
    428   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
    429       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
    430 
    431   ASSERT_FALSE(elf.GetGlobalVariable(global, &offset));
    432 }
    433 
    434 TEST_F(ElfTest, get_global_valid_dynamic_zero) {
    435   ElfFake elf(memory_);
    436   elf.FakeSetValid(true);
    437   elf.FakeSetLoadBias(0);
    438 
    439   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    440   elf.FakeSetInterface(interface);
    441 
    442   ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
    443   elf.FakeSetGnuDebugdataInterface(gnu_interface);
    444 
    445   uint64_t offset;
    446   std::string global("something");
    447   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset)).WillOnce(::testing::Return(false));
    448 
    449   EXPECT_CALL(*gnu_interface, GetGlobalVariable(global, &offset))
    450       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x500), ::testing::Return(true)));
    451 
    452   ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
    453   EXPECT_EQ(0x500U, offset);
    454 }
    455 
    456 TEST_F(ElfTest, get_global_valid_in_gnu_debugdata_dynamic_zero) {
    457   ElfFake elf(memory_);
    458   elf.FakeSetValid(true);
    459   elf.FakeSetLoadBias(0);
    460 
    461   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    462   elf.FakeSetInterface(interface);
    463 
    464   uint64_t offset;
    465   std::string global("something");
    466   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
    467       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
    468 
    469   ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
    470   EXPECT_EQ(0x300U, offset);
    471 }
    472 
    473 TEST_F(ElfTest, get_global_valid_dynamic_zero_non_zero_load_bias) {
    474   ElfFake elf(memory_);
    475   elf.FakeSetValid(true);
    476   elf.FakeSetLoadBias(0x100);
    477 
    478   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    479   elf.FakeSetInterface(interface);
    480 
    481   uint64_t offset;
    482   std::string global("something");
    483   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
    484       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x300), ::testing::Return(true)));
    485 
    486   ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
    487   EXPECT_EQ(0x200U, offset);
    488 }
    489 
    490 TEST_F(ElfTest, get_global_valid_dynamic_adjust_negative) {
    491   ElfFake elf(memory_);
    492   elf.FakeSetValid(true);
    493   elf.FakeSetLoadBias(0);
    494 
    495   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    496   interface->MockSetDynamicOffset(0x400);
    497   interface->MockSetDynamicVaddr(0x800);
    498   interface->MockSetDynamicSize(0x100);
    499   elf.FakeSetInterface(interface);
    500 
    501   uint64_t offset;
    502   std::string global("something");
    503   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
    504       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
    505 
    506   ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
    507   EXPECT_EQ(0x450U, offset);
    508 }
    509 
    510 TEST_F(ElfTest, get_global_valid_dynamic_adjust_positive) {
    511   ElfFake elf(memory_);
    512   elf.FakeSetValid(true);
    513   elf.FakeSetLoadBias(0);
    514 
    515   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    516   interface->MockSetDynamicOffset(0x1000);
    517   interface->MockSetDynamicVaddr(0x800);
    518   interface->MockSetDynamicSize(0x100);
    519   elf.FakeSetInterface(interface);
    520 
    521   uint64_t offset;
    522   std::string global("something");
    523   EXPECT_CALL(*interface, GetGlobalVariable(global, &offset))
    524       .WillOnce(::testing::DoAll(::testing::SetArgPointee<1>(0x850), ::testing::Return(true)));
    525 
    526   ASSERT_TRUE(elf.GetGlobalVariable(global, &offset));
    527   EXPECT_EQ(0x1050U, offset);
    528 }
    529 
    530 TEST_F(ElfTest, is_valid_pc_elf_invalid) {
    531   ElfFake elf(memory_);
    532   elf.FakeSetValid(false);
    533   elf.FakeSetLoadBias(0);
    534 
    535   EXPECT_FALSE(elf.IsValidPc(0x100));
    536   EXPECT_FALSE(elf.IsValidPc(0x200));
    537 }
    538 
    539 TEST_F(ElfTest, is_valid_pc_interface) {
    540   ElfFake elf(memory_);
    541   elf.FakeSetValid(true);
    542   elf.FakeSetLoadBias(0);
    543 
    544   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    545   elf.FakeSetInterface(interface);
    546 
    547   EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
    548 
    549   EXPECT_TRUE(elf.IsValidPc(0x1500));
    550 }
    551 
    552 TEST_F(ElfTest, is_valid_pc_non_zero_load_bias) {
    553   ElfFake elf(memory_);
    554   elf.FakeSetValid(true);
    555   elf.FakeSetLoadBias(0x1000);
    556 
    557   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    558   elf.FakeSetInterface(interface);
    559 
    560   EXPECT_CALL(*interface, IsValidPc(0x500)).WillOnce(::testing::Return(true));
    561 
    562   EXPECT_FALSE(elf.IsValidPc(0x100));
    563   EXPECT_FALSE(elf.IsValidPc(0x200));
    564   EXPECT_TRUE(elf.IsValidPc(0x1500));
    565 }
    566 
    567 TEST_F(ElfTest, is_valid_pc_from_gnu_debugdata) {
    568   ElfFake elf(memory_);
    569   elf.FakeSetValid(true);
    570   elf.FakeSetLoadBias(0);
    571 
    572   ElfInterfaceMock* interface = new ElfInterfaceMock(memory_);
    573   elf.FakeSetInterface(interface);
    574   ElfInterfaceMock* gnu_interface = new ElfInterfaceMock(memory_);
    575   elf.FakeSetGnuDebugdataInterface(gnu_interface);
    576 
    577   EXPECT_CALL(*interface, IsValidPc(0x1500)).WillOnce(::testing::Return(false));
    578   EXPECT_CALL(*gnu_interface, IsValidPc(0x1500)).WillOnce(::testing::Return(true));
    579 
    580   EXPECT_TRUE(elf.IsValidPc(0x1500));
    581 }
    582 
    583 TEST_F(ElfTest, error_code_not_valid) {
    584   ElfFake elf(memory_);
    585   elf.FakeSetValid(false);
    586 
    587   ErrorData error{ERROR_MEMORY_INVALID, 0x100};
    588   elf.GetLastError(&error);
    589   EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
    590   EXPECT_EQ(0x100U, error.address);
    591 }
    592 
    593 TEST_F(ElfTest, error_code_valid) {
    594   ElfFake elf(memory_);
    595   elf.FakeSetValid(true);
    596   ElfInterfaceFake* interface = new ElfInterfaceFake(memory_);
    597   elf.FakeSetInterface(interface);
    598   interface->FakeSetErrorCode(ERROR_MEMORY_INVALID);
    599   interface->FakeSetErrorAddress(0x1000);
    600 
    601   ErrorData error{ERROR_NONE, 0};
    602   elf.GetLastError(&error);
    603   EXPECT_EQ(ERROR_MEMORY_INVALID, error.code);
    604   EXPECT_EQ(0x1000U, error.address);
    605   EXPECT_EQ(ERROR_MEMORY_INVALID, elf.GetLastErrorCode());
    606   EXPECT_EQ(0x1000U, elf.GetLastErrorAddress());
    607 }
    608 
    609 }  // namespace unwindstack
    610