blob: c6c1c34287dee8adf445730d50f6255fab8b6348 [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>
32#include <android-base/test_utils.h>
33#include <gtest/gtest.h>
34
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#include <unwindstack/Elf.h>
36#include <unwindstack/MapInfo.h>
Christopher Ferris5f118512017-09-01 11:17:16 -070037#include <unwindstack/Maps.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070038#include <unwindstack/Memory.h>
39
Christopher Ferris570b76f2017-06-30 17:18:16 -070040#include "ElfTestUtils.h"
Christopher Ferris5f118512017-09-01 11:17:16 -070041#include "MemoryFake.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070042
43namespace unwindstack {
Christopher Ferris570b76f2017-06-30 17:18:16 -070044
45class MapInfoGetElfTest : public ::testing::Test {
46 protected:
47 void SetUp() override {
Christopher Ferris5f118512017-09-01 11:17:16 -070048 memory_ = new MemoryFake;
49 process_memory_.reset(memory_);
Christopher Ferris570b76f2017-06-30 17:18:16 -070050 }
51
Christopher Ferris5f118512017-09-01 11:17:16 -070052 template <typename Ehdr, typename Shdr>
53 static void InitElf(uint64_t sh_offset, Ehdr* ehdr, uint8_t class_type, uint8_t machine_type) {
54 memset(ehdr, 0, sizeof(*ehdr));
55 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
56 ehdr->e_ident[EI_CLASS] = class_type;
57 ehdr->e_machine = machine_type;
58 ehdr->e_shoff = sh_offset;
59 ehdr->e_shentsize = sizeof(Shdr) + 100;
60 ehdr->e_shnum = 4;
61 }
Christopher Ferris570b76f2017-06-30 17:18:16 -070062
63 const size_t kMapSize = 4096;
64
Christopher Ferris5f118512017-09-01 11:17:16 -070065 std::shared_ptr<Memory> process_memory_;
66 MemoryFake* memory_;
67
68 TemporaryFile elf_;
Christopher Ferris570b76f2017-06-30 17:18:16 -070069};
70
71TEST_F(MapInfoGetElfTest, invalid) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070072 MapInfo info(nullptr, 0x1000, 0x2000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -070073
Christopher Ferris570b76f2017-06-30 17:18:16 -070074 // The map is empty, but this should still create an invalid elf object.
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -070075 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080076 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -070077 ASSERT_FALSE(elf->valid());
78}
79
80TEST_F(MapInfoGetElfTest, valid32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070081 MapInfo info(nullptr, 0x3000, 0x4000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -070082
Christopher Ferris570b76f2017-06-30 17:18:16 -070083 Elf32_Ehdr ehdr;
84 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
Christopher Ferris5f118512017-09-01 11:17:16 -070085 memory_->SetMemory(0x3000, &ehdr, sizeof(ehdr));
Christopher Ferris570b76f2017-06-30 17:18:16 -070086
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -070087 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080088 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -070089 ASSERT_TRUE(elf->valid());
90 EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type());
91 EXPECT_EQ(ELFCLASS32, elf->class_type());
92}
93
94TEST_F(MapInfoGetElfTest, valid64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -070095 MapInfo info(nullptr, 0x8000, 0x9000, 0, PROT_READ, "");
Christopher Ferris5f118512017-09-01 11:17:16 -070096
Christopher Ferris570b76f2017-06-30 17:18:16 -070097 Elf64_Ehdr ehdr;
98 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
Christopher Ferris5f118512017-09-01 11:17:16 -070099 memory_->SetMemory(0x8000, &ehdr, sizeof(ehdr));
Christopher Ferris570b76f2017-06-30 17:18:16 -0700100
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700101 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800102 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700103 ASSERT_TRUE(elf->valid());
104 EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type());
105 EXPECT_EQ(ELFCLASS64, elf->class_type());
106}
107
Christopher Ferris570b76f2017-06-30 17:18:16 -0700108TEST_F(MapInfoGetElfTest, gnu_debugdata_init32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700109 MapInfo info(nullptr, 0x2000, 0x3000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700110
Christopher Ferris5f118512017-09-01 11:17:16 -0700111 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
112 [&](uint64_t offset, const void* ptr, size_t size) {
113 memory_->SetMemory(0x2000 + offset, ptr, size);
114 });
115
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700116 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800117 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700118 ASSERT_TRUE(elf->valid());
119 EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type());
120 EXPECT_EQ(ELFCLASS32, elf->class_type());
121 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
122}
123
124TEST_F(MapInfoGetElfTest, gnu_debugdata_init64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700125 MapInfo info(nullptr, 0x5000, 0x8000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700126
Christopher Ferris5f118512017-09-01 11:17:16 -0700127 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
128 [&](uint64_t offset, const void* ptr, size_t size) {
129 memory_->SetMemory(0x5000 + offset, ptr, size);
130 });
131
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700132 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800133 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700134 ASSERT_TRUE(elf->valid());
135 EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type());
136 EXPECT_EQ(ELFCLASS64, elf->class_type());
137 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
138}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700139
Christopher Ferris5f118512017-09-01 11:17:16 -0700140TEST_F(MapInfoGetElfTest, end_le_start) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700141 MapInfo info(nullptr, 0x1000, 0x1000, 0, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700142
143 Elf32_Ehdr ehdr;
144 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
145 ASSERT_TRUE(android::base::WriteFully(elf_.fd, &ehdr, sizeof(ehdr)));
146
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700147 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800148 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700149 ASSERT_FALSE(elf->valid());
150
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800151 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700152 info.end = 0xfff;
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700153 elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800154 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700155 ASSERT_FALSE(elf->valid());
156
157 // Make sure this test is valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800158 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700159 info.end = 0x2000;
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700160 elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800161 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700162 ASSERT_TRUE(elf->valid());
163}
164
165// Verify that if the offset is non-zero but there is no elf at the offset,
166// that the full file is used.
167TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_full_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700168 MapInfo info(nullptr, 0x1000, 0x2000, 0x100, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700169
170 std::vector<uint8_t> buffer(0x1000);
171 memset(buffer.data(), 0, buffer.size());
172 Elf32_Ehdr ehdr;
173 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
174 memcpy(buffer.data(), &ehdr, sizeof(ehdr));
175 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
176
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700177 Elf* elf = info.GetElf(process_memory_);
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 ASSERT_TRUE(elf->memory() != nullptr);
181 ASSERT_EQ(0x100U, info.elf_offset);
182
183 // Read the entire file.
184 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700185 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), buffer.size()));
Christopher Ferris5f118512017-09-01 11:17:16 -0700186 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
187 for (size_t i = sizeof(ehdr); i < buffer.size(); i++) {
188 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
189 }
190
Josh Gaoef35aa52017-10-18 11:44:51 -0700191 ASSERT_FALSE(elf->memory()->ReadFully(buffer.size(), buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700192}
193
194// Verify that if the offset is non-zero and there is an elf at that
195// offset, that only part of the file is used.
196TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700197 MapInfo info(nullptr, 0x1000, 0x2000, 0x2000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700198
199 std::vector<uint8_t> buffer(0x4000);
200 memset(buffer.data(), 0, buffer.size());
201 Elf32_Ehdr ehdr;
202 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
203 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
204 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
205
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700206 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800207 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700208 ASSERT_TRUE(elf->valid());
209 ASSERT_TRUE(elf->memory() != nullptr);
210 ASSERT_EQ(0U, info.elf_offset);
211
212 // Read the valid part of the file.
Josh Gaoef35aa52017-10-18 11:44:51 -0700213 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700214 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
215 for (size_t i = sizeof(ehdr); i < 0x1000; i++) {
216 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
217 }
218
Josh Gaoef35aa52017-10-18 11:44:51 -0700219 ASSERT_FALSE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700220}
221
222// Verify that if the offset is non-zero and there is an elf at that
223// offset, that only part of the file is used. Further verify that if the
224// embedded elf is bigger than the initial map, the new object is larger
225// than the original map size. Do this for a 32 bit elf and a 64 bit elf.
226TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700227 MapInfo info(nullptr, 0x5000, 0x6000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700228
229 std::vector<uint8_t> buffer(0x4000);
230 memset(buffer.data(), 0, buffer.size());
231 Elf32_Ehdr ehdr;
232 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
233 ehdr.e_shoff = 0x2000;
234 ehdr.e_shentsize = sizeof(Elf32_Shdr) + 100;
235 ehdr.e_shnum = 4;
236 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
237 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
238
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700239 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800240 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700241 ASSERT_TRUE(elf->valid());
242 ASSERT_TRUE(elf->memory() != nullptr);
243 ASSERT_EQ(0U, info.elf_offset);
244
245 // Verify the memory is a valid elf.
246 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700247 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700248 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
249
250 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700251 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700252}
253
254TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700255 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700256
257 std::vector<uint8_t> buffer(0x4000);
258 memset(buffer.data(), 0, buffer.size());
259 Elf64_Ehdr ehdr;
260 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
261 ehdr.e_shoff = 0x2000;
262 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
263 ehdr.e_shnum = 4;
264 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
265 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
266
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700267 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800268 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700269 ASSERT_TRUE(elf->valid());
270 ASSERT_TRUE(elf->memory() != nullptr);
271 ASSERT_EQ(0U, info.elf_offset);
272
273 // Verify the memory is a valid elf.
274 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700275 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700276 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
277
278 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700279 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700280}
281
282TEST_F(MapInfoGetElfTest, process_memory_not_read_only) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700283 MapInfo info(nullptr, 0x9000, 0xa000, 0x1000, 0, "");
Christopher Ferris5f118512017-09-01 11:17:16 -0700284
285 // Create valid elf data in process memory only.
286 Elf64_Ehdr ehdr;
287 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
288 ehdr.e_shoff = 0x2000;
289 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700290 ehdr.e_shnum = 0;
Christopher Ferris5f118512017-09-01 11:17:16 -0700291 memory_->SetMemory(0x9000, &ehdr, sizeof(ehdr));
292
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700293 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800294 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700295 ASSERT_FALSE(elf->valid());
296
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800297 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700298 info.flags = PROT_READ;
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700299 elf = info.GetElf(process_memory_);
Christopher Ferris5f118512017-09-01 11:17:16 -0700300 ASSERT_TRUE(elf->valid());
301}
302
303TEST_F(MapInfoGetElfTest, check_device_maps) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700304 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ | MAPS_FLAGS_DEVICE_MAP,
305 "/dev/something");
Christopher Ferris5f118512017-09-01 11:17:16 -0700306
307 // Create valid elf data in process memory for this to verify that only
308 // the name is causing invalid elf data.
309 Elf64_Ehdr ehdr;
310 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
311 ehdr.e_shoff = 0x2000;
312 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700313 ehdr.e_shnum = 0;
Christopher Ferris5f118512017-09-01 11:17:16 -0700314 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
315
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700316 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800317 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700318 ASSERT_FALSE(elf->valid());
319
320 // Set the name to nothing to verify that it still fails.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800321 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700322 info.name = "";
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700323 elf = info.GetElf(process_memory_);
Christopher Ferris5f118512017-09-01 11:17:16 -0700324 ASSERT_FALSE(elf->valid());
325
326 // Change the flags and verify the elf is valid now.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800327 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700328 info.flags = PROT_READ;
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700329 elf = info.GetElf(process_memory_);
Christopher Ferris5f118512017-09-01 11:17:16 -0700330 ASSERT_TRUE(elf->valid());
331}
332
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800333TEST_F(MapInfoGetElfTest, multiple_thread_get_elf) {
334 static constexpr size_t kNumConcurrentThreads = 100;
335
336 Elf64_Ehdr ehdr;
337 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
338 ehdr.e_shoff = 0x2000;
339 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700340 ehdr.e_shnum = 0;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800341 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
342
343 Elf* elf_in_threads[kNumConcurrentThreads];
344 std::vector<std::thread*> threads;
345
346 std::atomic_bool wait;
347 wait = true;
348 // Create all of the threads and have them do the GetElf at the same time
349 // to make it likely that a race will occur.
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700350 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, "");
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800351 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
352 std::thread* thread = new std::thread([i, this, &wait, &info, &elf_in_threads]() {
353 while (wait)
354 ;
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -0700355 Elf* elf = info.GetElf(process_memory_);
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800356 elf_in_threads[i] = elf;
357 });
358 threads.push_back(thread);
359 }
360 ASSERT_TRUE(info.elf == nullptr);
361
362 // Set them all going and wait for the threads to finish.
363 wait = false;
364 for (auto thread : threads) {
365 thread->join();
366 delete thread;
367 }
368
369 // Now verify that all of the elf files are exactly the same and valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800370 Elf* elf = info.elf.get();
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800371 ASSERT_TRUE(elf != nullptr);
372 EXPECT_TRUE(elf->valid());
373 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
374 EXPECT_EQ(elf, elf_in_threads[i]) << "Thread " << i << " mismatched.";
375 }
376}
377
Christopher Ferrisd226a512017-07-14 10:37:19 -0700378} // namespace unwindstack