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