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:
SetUp()48   void SetUp() override {
49     memory_ = new MemoryFake;
50     process_memory_.reset(memory_);
51     elf_ = new ElfFake(new MemoryFake);
52     elf_container_.reset(elf_);
53     map_info_.reset(new MapInfo(nullptr, nullptr, 0x1000, 0x20000, 0, PROT_READ | PROT_WRITE, ""));
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 
TEST_F(MapInfoGetLoadBiasTest,no_elf_and_no_valid_elf_in_memory)65 TEST_F(MapInfoGetLoadBiasTest, no_elf_and_no_valid_elf_in_memory) {
66   MapInfo info(nullptr, nullptr, 0x1000, 0x2000, 0, PROT_READ, "");
67 
68   EXPECT_EQ(0U, info.GetLoadBias(process_memory_));
69 }
70 
TEST_F(MapInfoGetLoadBiasTest,load_bias_cached_from_elf)71 TEST_F(MapInfoGetLoadBiasTest, load_bias_cached_from_elf) {
72   map_info_->set_elf(elf_container_.release());
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 
TEST_F(MapInfoGetLoadBiasTest,elf_exists)81 TEST_F(MapInfoGetLoadBiasTest, elf_exists) {
82   map_info_->set_elf(elf_container_.release());
83 
84   elf_->FakeSetLoadBias(0);
85   EXPECT_EQ(0U, map_info_->GetLoadBias(process_memory_));
86 
87   map_info_->set_load_bias(INT64_MAX);
88   elf_->FakeSetLoadBias(0x1000);
89   EXPECT_EQ(0x1000U, map_info_->GetLoadBias(process_memory_));
90 }
91 
MultipleThreadTest(uint64_t expected_load_bias)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 
TEST_F(MapInfoGetLoadBiasTest,multiple_thread_elf_exists)124 TEST_F(MapInfoGetLoadBiasTest, multiple_thread_elf_exists) {
125   map_info_->set_elf(elf_container_.release());
126   elf_->FakeSetLoadBias(0x1000);
127 
128   MultipleThreadTest(0x1000);
129 }
130 
InitElfData(MemoryFake * memory,uint64_t offset)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_flags = PF_X;
145   phdr.p_offset = 0;
146   phdr.p_vaddr = 0xe000;
147   memory->SetMemory(offset + 0x5000 + sizeof(phdr), &phdr, sizeof(phdr));
148 }
149 
TEST_F(MapInfoGetLoadBiasTest,elf_exists_in_memory)150 TEST_F(MapInfoGetLoadBiasTest, elf_exists_in_memory) {
151   InitElfData(memory_, map_info_->start());
152 
153   EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_));
154 }
155 
TEST_F(MapInfoGetLoadBiasTest,elf_exists_in_memory_cached)156 TEST_F(MapInfoGetLoadBiasTest, elf_exists_in_memory_cached) {
157   InitElfData(memory_, map_info_->start());
158 
159   EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_));
160 
161   memory_->Clear();
162   EXPECT_EQ(0xe000U, map_info_->GetLoadBias(process_memory_));
163 }
164 
TEST_F(MapInfoGetLoadBiasTest,multiple_thread_elf_exists_in_memory)165 TEST_F(MapInfoGetLoadBiasTest, multiple_thread_elf_exists_in_memory) {
166   InitElfData(memory_, map_info_->start());
167 
168   MultipleThreadTest(0xe000);
169 }
170 
171 }  // namespace unwindstack
172