blob: 918c028c7a4bae099624f9d40b969e617cdc1327 [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 Ferrisbe788d82017-11-27 14:50:38 -080075 Elf* elf = info.GetElf(process_memory_, false);
76 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 Ferrisbe788d82017-11-27 14:50:38 -080087 Elf* elf = info.GetElf(process_memory_, false);
88 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 Ferrisbe788d82017-11-27 14:50:38 -0800101 Elf* elf = info.GetElf(process_memory_, false);
102 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
108TEST_F(MapInfoGetElfTest, gnu_debugdata_do_not_init32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700109 MapInfo info(nullptr, 0x4000, 0x8000, 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, false,
112 [&](uint64_t offset, const void* ptr, size_t size) {
113 memory_->SetMemory(0x4000 + offset, ptr, size);
114 });
115
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800116 Elf* elf = info.GetElf(process_memory_, false);
117 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_do_not_init64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700125 MapInfo info(nullptr, 0x6000, 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, false,
128 [&](uint64_t offset, const void* ptr, size_t size) {
129 memory_->SetMemory(0x6000 + offset, ptr, size);
130 });
131
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800132 Elf* elf = info.GetElf(process_memory_, false);
133 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}
139
140TEST_F(MapInfoGetElfTest, gnu_debugdata_init32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700141 MapInfo info(nullptr, 0x2000, 0x3000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700142
Christopher Ferris5f118512017-09-01 11:17:16 -0700143 TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(ELFCLASS32, EM_ARM, true,
144 [&](uint64_t offset, const void* ptr, size_t size) {
145 memory_->SetMemory(0x2000 + offset, ptr, size);
146 });
147
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800148 Elf* elf = info.GetElf(process_memory_, true);
149 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700150 ASSERT_TRUE(elf->valid());
151 EXPECT_EQ(static_cast<uint32_t>(EM_ARM), elf->machine_type());
152 EXPECT_EQ(ELFCLASS32, elf->class_type());
153 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
154}
155
156TEST_F(MapInfoGetElfTest, gnu_debugdata_init64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700157 MapInfo info(nullptr, 0x5000, 0x8000, 0, PROT_READ, "");
Christopher Ferris570b76f2017-06-30 17:18:16 -0700158
Christopher Ferris5f118512017-09-01 11:17:16 -0700159 TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(ELFCLASS64, EM_AARCH64, true,
160 [&](uint64_t offset, const void* ptr, size_t size) {
161 memory_->SetMemory(0x5000 + offset, ptr, size);
162 });
163
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800164 Elf* elf = info.GetElf(process_memory_, true);
165 ASSERT_TRUE(elf != nullptr);
Christopher Ferris570b76f2017-06-30 17:18:16 -0700166 ASSERT_TRUE(elf->valid());
167 EXPECT_EQ(static_cast<uint32_t>(EM_AARCH64), elf->machine_type());
168 EXPECT_EQ(ELFCLASS64, elf->class_type());
169 EXPECT_TRUE(elf->gnu_debugdata_interface() != nullptr);
170}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700171
Christopher Ferris5f118512017-09-01 11:17:16 -0700172TEST_F(MapInfoGetElfTest, end_le_start) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700173 MapInfo info(nullptr, 0x1000, 0x1000, 0, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700174
175 Elf32_Ehdr ehdr;
176 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
177 ASSERT_TRUE(android::base::WriteFully(elf_.fd, &ehdr, sizeof(ehdr)));
178
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800179 Elf* elf = info.GetElf(process_memory_, false);
180 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700181 ASSERT_FALSE(elf->valid());
182
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800183 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700184 info.end = 0xfff;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800185 elf = info.GetElf(process_memory_, false);
186 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700187 ASSERT_FALSE(elf->valid());
188
189 // Make sure this test is valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800190 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700191 info.end = 0x2000;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800192 elf = info.GetElf(process_memory_, false);
193 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700194 ASSERT_TRUE(elf->valid());
195}
196
197// Verify that if the offset is non-zero but there is no elf at the offset,
198// that the full file is used.
199TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_full_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700200 MapInfo info(nullptr, 0x1000, 0x2000, 0x100, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700201
202 std::vector<uint8_t> buffer(0x1000);
203 memset(buffer.data(), 0, buffer.size());
204 Elf32_Ehdr ehdr;
205 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
206 memcpy(buffer.data(), &ehdr, sizeof(ehdr));
207 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
208
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800209 Elf* elf = info.GetElf(process_memory_, false);
210 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700211 ASSERT_TRUE(elf->valid());
212 ASSERT_TRUE(elf->memory() != nullptr);
213 ASSERT_EQ(0x100U, info.elf_offset);
214
215 // Read the entire file.
216 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700217 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), buffer.size()));
Christopher Ferris5f118512017-09-01 11:17:16 -0700218 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
219 for (size_t i = sizeof(ehdr); i < buffer.size(); i++) {
220 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
221 }
222
Josh Gaoef35aa52017-10-18 11:44:51 -0700223 ASSERT_FALSE(elf->memory()->ReadFully(buffer.size(), buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700224}
225
226// Verify that if the offset is non-zero and there is an elf at that
227// offset, that only part of the file is used.
228TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700229 MapInfo info(nullptr, 0x1000, 0x2000, 0x2000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700230
231 std::vector<uint8_t> buffer(0x4000);
232 memset(buffer.data(), 0, buffer.size());
233 Elf32_Ehdr ehdr;
234 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
235 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
236 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
237
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800238 Elf* elf = info.GetElf(process_memory_, false);
239 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700240 ASSERT_TRUE(elf->valid());
241 ASSERT_TRUE(elf->memory() != nullptr);
242 ASSERT_EQ(0U, info.elf_offset);
243
244 // Read the valid part of the file.
Josh Gaoef35aa52017-10-18 11:44:51 -0700245 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700246 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
247 for (size_t i = sizeof(ehdr); i < 0x1000; i++) {
248 ASSERT_EQ(0, buffer[i]) << "Failed at byte " << i;
249 }
250
Josh Gaoef35aa52017-10-18 11:44:51 -0700251 ASSERT_FALSE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700252}
253
254// Verify that if the offset is non-zero and there is an elf at that
255// offset, that only part of the file is used. Further verify that if the
256// embedded elf is bigger than the initial map, the new object is larger
257// than the original map size. Do this for a 32 bit elf and a 64 bit elf.
258TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf32) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700259 MapInfo info(nullptr, 0x5000, 0x6000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700260
261 std::vector<uint8_t> buffer(0x4000);
262 memset(buffer.data(), 0, buffer.size());
263 Elf32_Ehdr ehdr;
264 TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
265 ehdr.e_shoff = 0x2000;
266 ehdr.e_shentsize = sizeof(Elf32_Shdr) + 100;
267 ehdr.e_shnum = 4;
268 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
269 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
270
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800271 Elf* elf = info.GetElf(process_memory_, false);
272 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700273 ASSERT_TRUE(elf->valid());
274 ASSERT_TRUE(elf->memory() != nullptr);
275 ASSERT_EQ(0U, info.elf_offset);
276
277 // Verify the memory is a valid elf.
278 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700279 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700280 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
281
282 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700283 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700284}
285
286TEST_F(MapInfoGetElfTest, file_backed_non_zero_offset_partial_file_whole_elf64) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700287 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, elf_.path);
Christopher Ferris5f118512017-09-01 11:17:16 -0700288
289 std::vector<uint8_t> buffer(0x4000);
290 memset(buffer.data(), 0, buffer.size());
291 Elf64_Ehdr ehdr;
292 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
293 ehdr.e_shoff = 0x2000;
294 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
295 ehdr.e_shnum = 4;
296 memcpy(&buffer[info.offset], &ehdr, sizeof(ehdr));
297 ASSERT_TRUE(android::base::WriteFully(elf_.fd, buffer.data(), buffer.size()));
298
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800299 Elf* elf = info.GetElf(process_memory_, false);
300 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700301 ASSERT_TRUE(elf->valid());
302 ASSERT_TRUE(elf->memory() != nullptr);
303 ASSERT_EQ(0U, info.elf_offset);
304
305 // Verify the memory is a valid elf.
306 memset(buffer.data(), 0, buffer.size());
Josh Gaoef35aa52017-10-18 11:44:51 -0700307 ASSERT_TRUE(elf->memory()->ReadFully(0, buffer.data(), 0x1000));
Christopher Ferris5f118512017-09-01 11:17:16 -0700308 ASSERT_EQ(0, memcmp(buffer.data(), &ehdr, sizeof(ehdr)));
309
310 // Read past the end of what would normally be the size of the map.
Josh Gaoef35aa52017-10-18 11:44:51 -0700311 ASSERT_TRUE(elf->memory()->ReadFully(0x1000, buffer.data(), 1));
Christopher Ferris5f118512017-09-01 11:17:16 -0700312}
313
314TEST_F(MapInfoGetElfTest, process_memory_not_read_only) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700315 MapInfo info(nullptr, 0x9000, 0xa000, 0x1000, 0, "");
Christopher Ferris5f118512017-09-01 11:17:16 -0700316
317 // Create valid elf data in process memory only.
318 Elf64_Ehdr ehdr;
319 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_AARCH64);
320 ehdr.e_shoff = 0x2000;
321 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700322 ehdr.e_shnum = 0;
Christopher Ferris5f118512017-09-01 11:17:16 -0700323 memory_->SetMemory(0x9000, &ehdr, sizeof(ehdr));
324
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800325 Elf* elf = info.GetElf(process_memory_, false);
326 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700327 ASSERT_FALSE(elf->valid());
328
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800329 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700330 info.flags = PROT_READ;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800331 elf = info.GetElf(process_memory_, false);
Christopher Ferris5f118512017-09-01 11:17:16 -0700332 ASSERT_TRUE(elf->valid());
333}
334
335TEST_F(MapInfoGetElfTest, check_device_maps) {
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700336 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ | MAPS_FLAGS_DEVICE_MAP,
337 "/dev/something");
Christopher Ferris5f118512017-09-01 11:17:16 -0700338
339 // Create valid elf data in process memory for this to verify that only
340 // the name is causing invalid elf data.
341 Elf64_Ehdr ehdr;
342 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
343 ehdr.e_shoff = 0x2000;
344 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700345 ehdr.e_shnum = 0;
Christopher Ferris5f118512017-09-01 11:17:16 -0700346 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
347
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800348 Elf* elf = info.GetElf(process_memory_, false);
349 ASSERT_TRUE(elf != nullptr);
Christopher Ferris5f118512017-09-01 11:17:16 -0700350 ASSERT_FALSE(elf->valid());
351
352 // Set the name to nothing to verify that it still fails.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800353 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700354 info.name = "";
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800355 elf = info.GetElf(process_memory_, false);
Christopher Ferris5f118512017-09-01 11:17:16 -0700356 ASSERT_FALSE(elf->valid());
357
358 // Change the flags and verify the elf is valid now.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800359 info.elf.reset();
Christopher Ferris5f118512017-09-01 11:17:16 -0700360 info.flags = PROT_READ;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800361 elf = info.GetElf(process_memory_, false);
Christopher Ferris5f118512017-09-01 11:17:16 -0700362 ASSERT_TRUE(elf->valid());
363}
364
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800365TEST_F(MapInfoGetElfTest, multiple_thread_get_elf) {
366 static constexpr size_t kNumConcurrentThreads = 100;
367
368 Elf64_Ehdr ehdr;
369 TestInitEhdr<Elf64_Ehdr>(&ehdr, ELFCLASS64, EM_X86_64);
370 ehdr.e_shoff = 0x2000;
371 ehdr.e_shentsize = sizeof(Elf64_Shdr) + 100;
Christopher Ferris5acf0692018-08-01 13:10:46 -0700372 ehdr.e_shnum = 0;
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800373 memory_->SetMemory(0x7000, &ehdr, sizeof(ehdr));
374
375 Elf* elf_in_threads[kNumConcurrentThreads];
376 std::vector<std::thread*> threads;
377
378 std::atomic_bool wait;
379 wait = true;
380 // Create all of the threads and have them do the GetElf at the same time
381 // to make it likely that a race will occur.
Christopher Ferris9d5712c2018-10-01 21:01:09 -0700382 MapInfo info(nullptr, 0x7000, 0x8000, 0x1000, PROT_READ, "");
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800383 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
384 std::thread* thread = new std::thread([i, this, &wait, &info, &elf_in_threads]() {
385 while (wait)
386 ;
387 Elf* elf = info.GetElf(process_memory_, false);
388 elf_in_threads[i] = elf;
389 });
390 threads.push_back(thread);
391 }
392 ASSERT_TRUE(info.elf == nullptr);
393
394 // Set them all going and wait for the threads to finish.
395 wait = false;
396 for (auto thread : threads) {
397 thread->join();
398 delete thread;
399 }
400
401 // Now verify that all of the elf files are exactly the same and valid.
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800402 Elf* elf = info.elf.get();
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800403 ASSERT_TRUE(elf != nullptr);
404 EXPECT_TRUE(elf->valid());
405 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
406 EXPECT_EQ(elf, elf_in_threads[i]) << "Thread " << i << " mismatched.";
407 }
408}
409
Christopher Ferrisd226a512017-07-14 10:37:19 -0700410} // namespace unwindstack