Christopher Ferris | 570b76f | 2017-06-30 17:18:16 -0700 | [diff] [blame] | 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 <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <sys/ptrace.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <android-base/file.h> |
| 30 | #include <android-base/test_utils.h> |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | #include "Elf.h" |
| 34 | #include "ElfTestUtils.h" |
| 35 | #include "MapInfo.h" |
| 36 | |
| 37 | class MapInfoGetElfTest : public ::testing::Test { |
| 38 | protected: |
| 39 | void SetUp() override { |
| 40 | map_ = mmap(nullptr, kMapSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 41 | ASSERT_NE(MAP_FAILED, map_); |
| 42 | |
| 43 | uint64_t start = reinterpret_cast<uint64_t>(map_); |
| 44 | info_.reset(new MapInfo{.start = start, .end = start + 1024, .offset = 0, .name = ""}); |
| 45 | } |
| 46 | |
| 47 | void TearDown() override { munmap(map_, kMapSize); } |
| 48 | |
| 49 | const size_t kMapSize = 4096; |
| 50 | |
| 51 | void* map_ = nullptr; |
| 52 | std::unique_ptr<MapInfo> info_; |
| 53 | }; |
| 54 | |
| 55 | TEST_F(MapInfoGetElfTest, invalid) { |
| 56 | // The map is empty, but this should still create an invalid elf object. |
| 57 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), false)); |
| 58 | ASSERT_TRUE(elf.get() != nullptr); |
| 59 | ASSERT_FALSE(elf->valid()); |
| 60 | } |
| 61 | |
| 62 | TEST_F(MapInfoGetElfTest, valid32) { |
| 63 | Elf32_Ehdr ehdr; |
| 64 | TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM); |
| 65 | memcpy(map_, &ehdr, sizeof(ehdr)); |
| 66 | |
| 67 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), false)); |
| 68 | ASSERT_TRUE(elf.get() != nullptr); |
| 69 | ASSERT_TRUE(elf->valid()); |
| 70 | EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type()); |
| 71 | EXPECT_EQ(ELFCLASS32, elf->class_type()); |
| 72 | } |
| 73 | |
| 74 | TEST_F(MapInfoGetElfTest, valid64) { |
| 75 | Elf64_Ehdr ehdr; |
| 76 | TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64); |
| 77 | memcpy(map_, &ehdr, sizeof(ehdr)); |
| 78 | |
| 79 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), false)); |
| 80 | ASSERT_TRUE(elf.get() != nullptr); |
| 81 | ASSERT_TRUE(elf->valid()); |
| 82 | EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type()); |
| 83 | EXPECT_EQ(ELFCLASS64, elf->class_type()); |
| 84 | } |
| 85 | |
| 86 | TEST_F(MapInfoGetElfTest, gnu_debugdata_do_not_init32) { |
| 87 | TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>( |
| 88 | ELFCLASS32, EM_ARM, false, [&](uint64_t offset, const void* ptr, size_t size) { |
| 89 | memcpy(&reinterpret_cast<uint8_t*>(map_)[offset], ptr, size); |
| 90 | }); |
| 91 | |
| 92 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), false)); |
| 93 | ASSERT_TRUE(elf.get() != nullptr); |
| 94 | ASSERT_TRUE(elf->valid()); |
| 95 | EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type()); |
| 96 | EXPECT_EQ(ELFCLASS32, elf->class_type()); |
| 97 | EXPECT_TRUE(elf->gnu_debugdata_interface() == nullptr); |
| 98 | } |
| 99 | |
| 100 | TEST_F(MapInfoGetElfTest, gnu_debugdata_do_not_init64) { |
| 101 | TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>( |
| 102 | ELFCLASS64, EM_AARCH64, false, [&](uint64_t offset, const void* ptr, size_t size) { |
| 103 | memcpy(&reinterpret_cast<uint8_t*>(map_)[offset], ptr, size); |
| 104 | }); |
| 105 | |
| 106 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), false)); |
| 107 | ASSERT_TRUE(elf.get() != nullptr); |
| 108 | ASSERT_TRUE(elf->valid()); |
| 109 | EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type()); |
| 110 | EXPECT_EQ(ELFCLASS64, elf->class_type()); |
| 111 | EXPECT_TRUE(elf->gnu_debugdata_interface() == nullptr); |
| 112 | } |
| 113 | |
| 114 | TEST_F(MapInfoGetElfTest, gnu_debugdata_init32) { |
| 115 | TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>( |
| 116 | ELFCLASS32, EM_ARM, true, [&](uint64_t offset, const void* ptr, size_t size) { |
| 117 | memcpy(&reinterpret_cast<uint8_t*>(map_)[offset], ptr, size); |
| 118 | }); |
| 119 | |
| 120 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), true)); |
| 121 | ASSERT_TRUE(elf.get() != nullptr); |
| 122 | ASSERT_TRUE(elf->valid()); |
| 123 | EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type()); |
| 124 | EXPECT_EQ(ELFCLASS32, elf->class_type()); |
| 125 | EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr); |
| 126 | } |
| 127 | |
| 128 | TEST_F(MapInfoGetElfTest, gnu_debugdata_init64) { |
| 129 | TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>( |
| 130 | ELFCLASS64, EM_AARCH64, true, [&](uint64_t offset, const void* ptr, size_t size) { |
| 131 | memcpy(&reinterpret_cast<uint8_t*>(map_)[offset], ptr, size); |
| 132 | }); |
| 133 | |
| 134 | std::unique_ptr<Elf> elf(info_->GetElf(getpid(), true)); |
| 135 | ASSERT_TRUE(elf.get() != nullptr); |
| 136 | ASSERT_TRUE(elf->valid()); |
| 137 | EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type()); |
| 138 | EXPECT_EQ(ELFCLASS64, elf->class_type()); |
| 139 | EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr); |
| 140 | } |