blob: d60b8b1afa7eef0c3c8b65c5e364cb7ee99ef11b [file] [log] [blame]
Christopher Ferris570b76f2017-06-30 17:18:16 -07001/*
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
Christopher Ferrisbe788d82017-11-27 14:50:38 -080026#include <atomic>
Christopher Ferris570b76f2017-06-30 17:18:16 -070027#include <memory>
Christopher Ferrisbe788d82017-11-27 14:50:38 -080028#include <thread>
Christopher Ferris570b76f2017-06-30 17:18:16 -070029#include <vector>
30
31#include <android-base/file.h>
Christopher Ferris570b76f2017-06-30 17:18:16 -070032#include <gtest/gtest.h>
33
Christopher Ferrisd226a512017-07-14 10:37:19 -070034#include <unwindstack/Elf.h>
35#include <unwindstack/MapInfo.h>
Christopher Ferris5f118512017-09-01 11:17:16 -070036#include <unwindstack/Maps.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070037#include <unwindstack/Memory.h>
38
Christopher Ferris570b76f2017-06-30 17:18:16 -070039#include "ElfTestUtils.h"
Christopher Ferris5f118512017-09-01 11:17:16 -070040#include "MemoryFake.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070041
42namespace unwindstack {
Christopher Ferris570b76f2017-06-30 17:18:16 -070043
44class MapInfoGetElfTest : public ::testing::Test {
45 protected:
46 void SetUp() override {
Christopher Ferris5f118512017-09-01 11:17:16 -070047 memory_ = new MemoryFake;
48 process_memory_.reset(memory_);
Christopher Ferris570b76f2017-06-30 17:18:16 -070049 }
50
Christopher Ferris5f118512017-09-01 11:17:16 -070051 template <typename Ehdr, typename Shdr>
52 static void InitElf(uint64_t sh_offset, Ehdr* ehdr, uint8_t class_type, uint8_t machine_type) {
53 memset(ehdr, 0, sizeof(*ehdr));
54 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
55 ehdr->e_ident[EI_CLASS] = class_type;
56 ehdr->e_machine = machine_type;
57 ehdr->e_shoff = sh_offset;
58 ehdr->e_shentsize = sizeof(Shdr) + 100;
59 ehdr->e_shnum = 4;
60 }
Christopher Ferris570b76f2017-06-30 17:18:16 -070061
62 const size_t kMapSize = 4096;
63
Christopher Ferris5f118512017-09-01 11:17:16 -070064 std::shared_ptr<Memory> process_memory_;
65 MemoryFake* memory_;
66
67 TemporaryFile elf_;
Christopher Ferris570b76f2017-06-30 17:18:16 -070068};
69
70TEST_F(MapInfoGetElfTest, invalid) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070071 MapInfo info(nullptr, 0x1000, 0x2000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -070072
Christopher Ferris570b76f2017-06-30 17:18:16 -070073 // The map is empty, but this should still create an invalid elf object.
Christopher Ferris4568f4b2018-10-23 17:42:41 -070074 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080075 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -070076 ASSERT_FALSE(elf->valid());
77}
78
79TEST_F(MapInfoGetElfTest, valid32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070080 MapInfo info(nullptr, 0x3000, 0x4000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -070081
Christopher Ferris570b76f2017-06-30 17:18:16 -070082 Elf32_Ehdr ehdr;
83 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
Christopher Ferris5f118512017-09-01 11:17:16 -070084 memory_->SetMemory(0x3000, &ehdr, sizeof(ehdr));
Christopher Ferris570b76f2017-06-30 17:18:16 -070085
Christopher Ferris4568f4b2018-10-23 17:42:41 -070086 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080087 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -070088 ASSERT_TRUE(elf->valid());
89 EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type());
90 EXPECT_EQ(ELFCLASS32, elf->class_type());
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080091
92 // Now verify that an empty process memory returns an invalid elf object.
93 info.elf.reset();
94 elf = info.GetElf(std::shared_ptr<Memory>(), ARCH_ARM);
95 ASSERT_TRUE(elf != nullptr);
96 ASSERT_FALSE(elf->valid());
Christopher Ferris570b76f2017-06-30 17:18:16 -070097}
98
99TEST_F(MapInfoGetElfTest, valid64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700100 MapInfo info(nullptr, 0x8000, 0x9000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -0700101
Christopher Ferris570b76f2017-06-30 17:18:16 -0700102 Elf64_Ehdr ehdr;
103 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
Christopher Ferris5f118512017-09-01 11:17:16 -0700104 memory_->SetMemory(0x8000, &ehdr, sizeof(ehdr));
Christopher Ferris570b76f2017-06-30 17:18:16 -0700105
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700106 Elf* elf = info.GetElf(process_memory_, ARCH_ARM64);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800107 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700108 ASSERT_TRUE(elf->valid());
109 EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type());
110 EXPECT_EQ(ELFCLASS64, elf->class_type());
111}
112
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700113TEST_F(MapInfoGetElfTest, invalid_arch_mismatch) {
114 MapInfo info(nullptr, 0x3000, 0x4000, 0, PROT_READ, "");
115
116 Elf32_Ehdr ehdr;
117 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
118 memory_->SetMemory(0x3000, &ehdr, sizeof(ehdr));
119
120 Elf* elf = info.GetElf(process_memory_, ARCH_X86);
121 ASSERT_TRUE(elf != nullptr);
122 ASSERT_FALSE(elf->valid());
123}
124
Christopher Ferris570b76f2017-06-30 17:18:16 -0700125TEST_F(MapInfoGetElfTest, gnu_debugdata_init32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700126 MapInfo info(nullptr, 0x2000, 0x3000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700127
Christopher Ferris5f118512017-09-01 11:17:16 -0700128 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
129 [&](uint64_t offset, const void* ptr, size_t size) {
130 memory_->SetMemory(0x2000 + offset, ptr, size);
131 });
132
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700133 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800134 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700135 ASSERT_TRUE(elf->valid());
136 EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type());
137 EXPECT_EQ(ELFCLASS32, elf->class_type());
138 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
139}
140
141TEST_F(MapInfoGetElfTest, gnu_debugdata_init64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700142 MapInfo info(nullptr, 0x5000, 0x8000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700143
Christopher Ferris5f118512017-09-01 11:17:16 -0700144 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
145 [&](uint64_t offset, const void* ptr, size_t size) {
146 memory_->SetMemory(0x5000 + offset, ptr, size);
147 });
148
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700149 Elf* elf = info.GetElf(process_memory_, ARCH_ARM64);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800150 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700151 ASSERT_TRUE(elf->valid());
152 EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type());
153 EXPECT_EQ(ELFCLASS64, elf->class_type());
154 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
155}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700156
Christopher Ferris5f118512017-09-01 11:17:16 -0700157TEST_F(MapInfoGetElfTest, end_le_start) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700158 MapInfo info(nullptr, 0x1000, 0x1000, 0, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700159
160 Elf32_Ehdr ehdr;
161 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
162 ASSERT_TRUE(android::base::WriteFully(elf_.fd, &ehdr, sizeof(ehdr)));
163
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700164 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800165 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700166 ASSERT_FALSE(elf->valid());
167
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800168 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700169 info.end = 0xfff;
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700170 elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800171 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700172 ASSERT_FALSE(elf->valid());
173
174 // Make sure this test is valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800175 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700176 info.end = 0x2000;
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700177 elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800178 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700179 ASSERT_TRUE(elf->valid());
180}
181
182// Verify that if the offset is non-zero but there is no elf at the offset,
183// that the full file is used.
184TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_full_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700185 MapInfo info(nullptr, 0x1000, 0x2000, 0x100, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700186
187 std::vector<uint8_t> buffer(0x1000);
188 memset(buffer.data(), 0, buffer.size());
189 Elf32_Ehdr ehdr;
190 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
191 memcpy(buffer.data(), &ehdr, sizeof(ehdr));
192 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
193
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700194 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800195 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700196 ASSERT_TRUE(elf->valid());
197 ASSERT_TRUE(elf->memory() != nullptr);
198 ASSERT_EQ(0x100U, info.elf_offset);
199
200 // Read the entire file.
201 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700202 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), buffer.size()));
Christopher Ferris5f118512017-09-01 11:17:16 -0700203 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
204 for (size_t i = sizeof(ehdr); i < buffer.size(); i++) {
205 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
206 }
207
Josh Gaoef35aa52017-10-18 11:44:51 -0700208 ASSERT_FALSE(elf->memory()->ReadFully(buffer.size(), buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700209}
210
211// Verify that if the offset is non-zero and there is an elf at that
212// offset, that only part of the file is used.
213TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700214 MapInfo info(nullptr, 0x1000, 0x2000, 0x2000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700215
216 std::vector<uint8_t> buffer(0x4000);
217 memset(buffer.data(), 0, buffer.size());
218 Elf32_Ehdr ehdr;
219 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
220 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
221 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
222
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700223 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800224 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700225 ASSERT_TRUE(elf->valid());
226 ASSERT_TRUE(elf->memory() != nullptr);
227 ASSERT_EQ(0U, info.elf_offset);
228
229 // Read the valid part of the file.
Josh Gaoef35aa52017-10-18 11:44:51 -0700230 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700231 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
232 for (size_t i = sizeof(ehdr); i < 0x1000; i++) {
233 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
234 }
235
Josh Gaoef35aa52017-10-18 11:44:51 -0700236 ASSERT_FALSE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700237}
238
239// Verify that if the offset is non-zero and there is an elf at that
240// offset, that only part of the file is used. Further verify that if the
241// embedded elf is bigger than the initial map, the new object is larger
242// than the original map size. Do this for a 32 bit elf and a 64 bit elf.
243TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700244 MapInfo info(nullptr, 0x5000, 0x6000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700245
246 std::vector<uint8_t> buffer(0x4000);
247 memset(buffer.data(), 0, buffer.size());
248 Elf32_Ehdr ehdr;
249 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
250 ehdr.e_shoff = 0x2000;
251 ehdr.e_shentsize = sizeof(Elf32_Shdr) + 100;
252 ehdr.e_shnum = 4;
253 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
254 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
255
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700256 Elf* elf = info.GetElf(process_memory_, ARCH_ARM);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800257 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700258 ASSERT_TRUE(elf->valid());
259 ASSERT_TRUE(elf->memory() != nullptr);
260 ASSERT_EQ(0U, info.elf_offset);
261
262 // Verify the memory is a valid elf.
263 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700264 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700265 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
266
267 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700268 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700269}
270
271TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700272 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700273
274 std::vector<uint8_t> buffer(0x4000);
275 memset(buffer.data(), 0, buffer.size());
276 Elf64_Ehdr ehdr;
277 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
278 ehdr.e_shoff = 0x2000;
279 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
280 ehdr.e_shnum = 4;
281 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
282 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
283
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700284 Elf* elf = info.GetElf(process_memory_, ARCH_ARM64);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800285 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700286 ASSERT_TRUE(elf->valid());
287 ASSERT_TRUE(elf->memory() != nullptr);
288 ASSERT_EQ(0U, info.elf_offset);
289
290 // Verify the memory is a valid elf.
291 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700292 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700293 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
294
295 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700296 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700297}
298
Christopher Ferris5f118512017-09-01 11:17:16 -0700299TEST_F(MapInfoGetElfTest, check_device_maps) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700300 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ | MAPS_FLAGS_DEVICE_MAP,
301 "/dev/something");
Christopher Ferris5f118512017-09-01 11:17:16 -0700302
303 // Create valid elf data in process memory for this to verify that only
304 // the name is causing invalid elf data.
305 Elf64_Ehdr ehdr;
306 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
307 ehdr.e_shoff = 0x2000;
308 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700309 ehdr.e_shnum = 0;
Christopher Ferris5f118512017-09-01 11:17:16 -0700310 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
311
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700312 Elf* elf = info.GetElf(process_memory_, ARCH_X86_64);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800313 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700314 ASSERT_FALSE(elf->valid());
315
316 // Set the name to nothing to verify that it still fails.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800317 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700318 info.name = "";
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700319 elf = info.GetElf(process_memory_, ARCH_X86_64);
Christopher Ferris5f118512017-09-01 11:17:16 -0700320 ASSERT_FALSE(elf->valid());
321
322 // Change the flags and verify the elf is valid now.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800323 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700324 info.flags = PROT_READ;
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700325 elf = info.GetElf(process_memory_, ARCH_X86_64);
Christopher Ferris5f118512017-09-01 11:17:16 -0700326 ASSERT_TRUE(elf->valid());
327}
328
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800329TEST_F(MapInfoGetElfTest, multiple_thread_get_elf) {
330 static constexpr size_t kNumConcurrentThreads = 100;
331
332 Elf64_Ehdr ehdr;
333 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
334 ehdr.e_shoff = 0x2000;
335 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700336 ehdr.e_shnum = 0;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800337 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
338
339 Elf* elf_in_threads[kNumConcurrentThreads];
340 std::vector<std::thread*> threads;
341
342 std::atomic_bool wait;
343 wait = true;
344 // Create all of the threads and have them do the GetElf at the same time
345 // to make it likely that a race will occur.
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700346 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, "");
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800347 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
348 std::thread* thread = new std::thread([i, this, &wait, &info, &elf_in_threads]() {
349 while (wait)
350 ;
Christopher Ferris4568f4b2018-10-23 17:42:41 -0700351 Elf* elf = info.GetElf(process_memory_, ARCH_X86_64);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800352 elf_in_threads[i] = elf;
353 });
354 threads.push_back(thread);
355 }
356 ASSERT_TRUE(info.elf == nullptr);
357
358 // Set them all going and wait for the threads to finish.
359 wait = false;
360 for (auto thread : threads) {
361 thread->join();
362 delete thread;
363 }
364
365 // Now verify that all of the elf files are exactly the same and valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800366 Elf* elf = info.elf.get();
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800367 ASSERT_TRUE(elf != nullptr);
368 EXPECT_TRUE(elf->valid());
369 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
370 EXPECT_EQ(elf, elf_in_threads[i]) << "Thread " << i << " mismatched.";
371 }
372}
373
Christopher Ferris02a6c442019-03-11 14:43:33 -0700374// Verify that previous maps don't automatically get the same elf object.
375TEST_F(MapInfoGetElfTest, prev_map_elf_not_set) {
376 MapInfo info1(nullptr, 0x1000, 0x2000, 0, PROT_READ, "/not/present");
377 MapInfo info2(&info1, 0x2000, 0x3000, 0, PROT_READ, elf_.path);
378
379 Elf32_Ehdr ehdr;
380 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
381 memory_->SetMemory(0x2000, &ehdr, sizeof(ehdr));
382 Elf* elf = info2.GetElf(process_memory_, ARCH_ARM);
383 ASSERT_TRUE(elf != nullptr);
384 ASSERT_TRUE(elf->valid());
385
386 ASSERT_NE(elf, info1.GetElf(process_memory_, ARCH_ARM));
387}
388
389// Verify that a read-only map followed by a read-execute map will result
390// in the same elf object in both maps.
391TEST_F(MapInfoGetElfTest, read_only_followed_by_read_exec_share_elf) {
392 MapInfo r_info(nullptr, 0x1000, 0x2000, 0, PROT_READ, elf_.path);
393 MapInfo rw_info(&r_info, 0x2000, 0x3000, 0x1000, PROT_READ | PROT_EXEC, elf_.path);
394
395 Elf32_Ehdr ehdr;
396 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
397 memory_->SetMemory(0x1000, &ehdr, sizeof(ehdr));
398 Elf* elf = rw_info.GetElf(process_memory_, ARCH_ARM);
399 ASSERT_TRUE(elf != nullptr);
400 ASSERT_TRUE(elf->valid());
401
402 ASSERT_EQ(elf, r_info.GetElf(process_memory_, ARCH_ARM));
403}
404
Christopher Ferrisd226a512017-07-14 10:37:19 -0700405} // namespace unwindstack