Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [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 <atomic> |
| 27 | #include <memory> |
| 28 | #include <thread> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include <android-base/file.h> |
| 32 | #include <android-base/test_utils.h> |
| 33 | #include <gtest/gtest.h> |
| 34 | |
| 35 | #include <unwindstack/Elf.h> |
| 36 | #include <unwindstack/MapInfo.h> |
| 37 | #include <unwindstack/Maps.h> |
| 38 | #include <unwindstack/Memory.h> |
| 39 | |
| 40 | #include "ElfFake.h" |
| 41 | #include "ElfTestUtils.h" |
| 42 | #include "MemoryFake.h" |
| 43 | |
| 44 | namespace unwindstack { |
| 45 | |
| 46 | class MapInfoGetLoadBiasTest : public ::testing::Test { |
| 47 | protected: |
| 48 | void SetUp() override { |
| 49 | memory_ = new MemoryFake; |
| 50 | process_memory_.reset(memory_); |
| 51 | elf_ = new ElfFake(new MemoryFake); |
| 52 | elf_container_.reset(elf_); |
Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 53 | map_info_.reset(new MapInfo(nullptr, 0x1000, 0x20000, 0, PROT_READ | PROT_WRITE, "")); |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void MultipleThreadTest(uint64_t expected_load_bias); |
| 57 | |
| 58 | std::shared_ptr<Memory> process_memory_; |
| 59 | MemoryFake* memory_; |
| 60 | ElfFake* elf_; |
| 61 | std::unique_ptr<ElfFake> elf_container_; |
| 62 | std::unique_ptr<MapInfo> map_info_; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(MapInfoGetLoadBiasTest, no_elf_and_no_valid_elf_in_memory) { |
Christopher Ferris | 9d5712c | 2018-10-01 21:01:09 -0700 | [diff] [blame] | 66 | MapInfo info(nullptr, 0x1000, 0x2000, 0, PROT_READ, ""); |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 67 | |
| 68 | EXPECT_EQ(0U, info.GetLoadBias(process_memory_)); |
| 69 | } |
| 70 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 71 | TEST_F(MapInfoGetLoadBiasTest, load_bias_cached_from_elf) { |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 72 | map_info_->elf.reset(elf_container_.release()); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 73 | |
| 74 | elf_->FakeSetLoadBias(0); |
| 75 | EXPECT_EQ(0U, map_info_->GetLoadBias(process_memory_)); |
| 76 | |
| 77 | elf_->FakeSetLoadBias(0x1000); |
| 78 | EXPECT_EQ(0U, map_info_->GetLoadBias(process_memory_)); |
| 79 | } |
| 80 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 81 | TEST_F(MapInfoGetLoadBiasTest, elf_exists) { |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 82 | map_info_->elf.reset(elf_container_.release()); |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 83 | |
| 84 | elf_->FakeSetLoadBias(0); |
| 85 | EXPECT_EQ(0U, map_info_->GetLoadBias(process_memory_)); |
| 86 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 87 | map_info_->load_bias = static_cast<uint64_t>(-1); |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 88 | elf_->FakeSetLoadBias(0x1000); |
| 89 | EXPECT_EQ(0x1000U, map_info_->GetLoadBias(process_memory_)); |
| 90 | } |
| 91 | |
| 92 | void MapInfoGetLoadBiasTest::MultipleThreadTest(uint64_t expected_load_bias) { |
| 93 | static constexpr size_t kNumConcurrentThreads = 100; |
| 94 | |
| 95 | uint64_t load_bias_values[kNumConcurrentThreads]; |
| 96 | std::vector<std::thread*> threads; |
| 97 | |
| 98 | std::atomic_bool wait; |
| 99 | wait = true; |
| 100 | // Create all of the threads and have them do the GetLoadBias at the same time |
| 101 | // to make it likely that a race will occur. |
| 102 | for (size_t i = 0; i < kNumConcurrentThreads; i++) { |
| 103 | std::thread* thread = new std::thread([i, this, &wait, &load_bias_values]() { |
| 104 | while (wait) |
| 105 | ; |
| 106 | load_bias_values[i] = map_info_->GetLoadBias(process_memory_); |
| 107 | }); |
| 108 | threads.push_back(thread); |
| 109 | } |
| 110 | |
| 111 | // Set them all going and wait for the threads to finish. |
| 112 | wait = false; |
| 113 | for (auto thread : threads) { |
| 114 | thread->join(); |
| 115 | delete thread; |
| 116 | } |
| 117 | |
| 118 | // Now verify that all of the elf files are exactly the same and valid. |
| 119 | for (size_t i = 0; i < kNumConcurrentThreads; i++) { |
| 120 | EXPECT_EQ(expected_load_bias, load_bias_values[i]) << "Thread " << i << " mismatched."; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | TEST_F(MapInfoGetLoadBiasTest, multiple_thread_elf_exists) { |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 125 | map_info_->elf.reset(elf_container_.release()); |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 126 | elf_->FakeSetLoadBias(0x1000); |
| 127 | |
| 128 | MultipleThreadTest(0x1000); |
| 129 | } |
| 130 | |
| 131 | static void InitElfData(MemoryFake* memory, uint64_t offset) { |
| 132 | Elf32_Ehdr ehdr; |
| 133 | TestInitEhdr(&ehdr, ELFCLASS32, EM_ARM); |
| 134 | ehdr.e_phoff = 0x5000; |
| 135 | ehdr.e_phnum = 2; |
| 136 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 137 | memory->SetMemory(offset, &ehdr, sizeof(ehdr)); |
| 138 | |
| 139 | Elf32_Phdr phdr; |
| 140 | memset(&phdr, 0, sizeof(phdr)); |
| 141 | phdr.p_type = PT_NULL; |
| 142 | memory->SetMemory(offset + 0x5000, &phdr, sizeof(phdr)); |
| 143 | phdr.p_type = PT_LOAD; |
| 144 | phdr.p_offset = 0; |
| 145 | phdr.p_vaddr = 0xe000; |
| 146 | memory->SetMemory(offset + 0x5000 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 147 | } |
| 148 | |
| 149 | TEST_F(MapInfoGetLoadBiasTest, elf_exists_in_memory) { |
| 150 | InitElfData(memory_, map_info_->start); |
| 151 | |
| 152 | EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_)); |
| 153 | } |
| 154 | |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 155 | TEST_F(MapInfoGetLoadBiasTest, elf_exists_in_memory_cached) { |
| 156 | InitElfData(memory_, map_info_->start); |
| 157 | |
| 158 | EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_)); |
| 159 | |
| 160 | memory_->Clear(); |
| 161 | EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_)); |
| 162 | } |
| 163 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 164 | TEST_F(MapInfoGetLoadBiasTest, multiple_thread_elf_exists_in_memory) { |
| 165 | InitElfData(memory_, map_info_->start); |
| 166 | |
| 167 | MultipleThreadTest(0xe000); |
| 168 | } |
| 169 | |
| 170 | } // namespace unwindstack |