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