blob: 3b89c596161cf86099d103d3411b60b26a721b5d [file] [log] [blame]
Christopher Ferrisbf373ed2019-01-16 17:23:39 -08001/*
2 * Copyright (C) 2019 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 <string.h>
19#include <sys/mman.h>
20#include <unistd.h>
21
22#include <atomic>
23#include <memory>
24#include <string>
25#include <thread>
26#include <vector>
27
28#include <android-base/test_utils.h>
29
30#include <gtest/gtest.h>
31
32#include <unwindstack/Elf.h>
33#include <unwindstack/MapInfo.h>
34#include <unwindstack/Maps.h>
35#include <unwindstack/Memory.h>
36
37#include "ElfFake.h"
38#include "ElfTestUtils.h"
39#include "MemoryFake.h"
40
41namespace unwindstack {
42
43class MapInfoGetBuildIDTest : public ::testing::Test {
44 protected:
45 void SetUp() override {
46 tf_.reset(new TemporaryFile);
47
48 memory_ = new MemoryFake;
49 elf_ = new ElfFake(new MemoryFake);
50 elf_interface_ = new ElfInterfaceFake(memory_);
51 elf_->FakeSetInterface(elf_interface_);
52 elf_container_.reset(elf_);
53 map_info_.reset(new MapInfo(nullptr, 0x1000, 0x20000, 0, PROT_READ | PROT_WRITE, tf_->path));
54 }
55
56 void MultipleThreadTest(std::string expected_build_id);
57
58 MemoryFake* memory_;
59 ElfFake* elf_;
60 ElfInterfaceFake* elf_interface_;
61 std::unique_ptr<ElfFake> elf_container_;
62 std::unique_ptr<MapInfo> map_info_;
63 std::unique_ptr<TemporaryFile> tf_;
64};
65
66TEST_F(MapInfoGetBuildIDTest, no_elf_and_no_valid_elf_in_memory) {
67 MapInfo info(nullptr, 0x1000, 0x2000, 0, PROT_READ, "");
68
69 EXPECT_EQ("", info.GetBuildID());
70}
71
72TEST_F(MapInfoGetBuildIDTest, from_elf) {
73 map_info_->elf.reset(elf_container_.release());
74 elf_interface_->FakeSetBuildID("FAKE_BUILD_ID");
75
76 EXPECT_EQ("FAKE_BUILD_ID", map_info_->GetBuildID());
77}
78
79void MapInfoGetBuildIDTest::MultipleThreadTest(std::string expected_build_id) {
80 static constexpr size_t kNumConcurrentThreads = 100;
81
82 std::string build_id_values[kNumConcurrentThreads];
83 std::vector<std::thread*> threads;
84
85 std::atomic_bool wait;
86 wait = true;
87 // Create all of the threads and have them do the GetLoadBias at the same time
88 // to make it likely that a race will occur.
89 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
90 std::thread* thread = new std::thread([i, this, &wait, &build_id_values]() {
91 while (wait)
92 ;
93 build_id_values[i] = map_info_->GetBuildID();
94 });
95 threads.push_back(thread);
96 }
97
98 // Set them all going and wait for the threads to finish.
99 wait = false;
100 for (auto thread : threads) {
101 thread->join();
102 delete thread;
103 }
104
105 // Now verify that all of the elf files are exactly the same and valid.
106 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
107 EXPECT_EQ(expected_build_id, build_id_values[i]) << "Thread " << i << " mismatched.";
108 }
109}
110
111TEST_F(MapInfoGetBuildIDTest, multiple_thread_elf_exists) {
112 map_info_->elf.reset(elf_container_.release());
113 elf_interface_->FakeSetBuildID("FAKE_BUILD_ID");
114
115 MultipleThreadTest("FAKE_BUILD_ID");
116}
117
118static void InitElfData(int fd) {
119 Elf32_Ehdr ehdr;
120 TestInitEhdr(&ehdr, ELFCLASS32, EM_ARM);
121 ehdr.e_shoff = 0x2000;
122 ehdr.e_shnum = 3;
123 ehdr.e_shentsize = sizeof(Elf32_Shdr);
124 ehdr.e_shstrndx = 2;
125 off_t offset = 0;
126 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
127 ASSERT_EQ(static_cast<ssize_t>(sizeof(ehdr)), write(fd, &ehdr, sizeof(ehdr)));
128
129 char note_section[128];
130 Elf32_Nhdr note_header = {};
131 note_header.n_namesz = 4; // "GNU"
132 note_header.n_descsz = 12; // "ELF_BUILDID"
133 note_header.n_type = NT_GNU_BUILD_ID;
134 memcpy(&note_section, &note_header, sizeof(note_header));
135 size_t note_offset = sizeof(note_header);
136 memcpy(&note_section[note_offset], "GNU", sizeof("GNU"));
137 note_offset += sizeof("GNU");
138 memcpy(&note_section[note_offset], "ELF_BUILDID", sizeof("ELF_BUILDID"));
139 note_offset += sizeof("ELF_BUILDID");
140
141 Elf32_Shdr shdr = {};
142 shdr.sh_type = SHT_NOTE;
143 shdr.sh_name = 0x500;
144 shdr.sh_offset = 0xb000;
145 shdr.sh_size = sizeof(note_section);
146 offset += ehdr.e_shoff + sizeof(shdr);
147 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
148 ASSERT_EQ(static_cast<ssize_t>(sizeof(shdr)), write(fd, &shdr, sizeof(shdr)));
149
150 // The string data for section header names.
151 memset(&shdr, 0, sizeof(shdr));
152 shdr.sh_type = SHT_STRTAB;
153 shdr.sh_name = 0x20000;
154 shdr.sh_offset = 0xf000;
155 shdr.sh_size = 0x1000;
156 offset += sizeof(shdr);
157 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
158 ASSERT_EQ(static_cast<ssize_t>(sizeof(shdr)), write(fd, &shdr, sizeof(shdr)));
159
160 offset = 0xf500;
161 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
162 ASSERT_EQ(static_cast<ssize_t>(sizeof(".note.gnu.build-id")),
163 write(fd, ".note.gnu.build-id", sizeof(".note.gnu.build-id")));
164
165 offset = 0xb000;
166 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
167 ASSERT_EQ(static_cast<ssize_t>(sizeof(note_section)),
168 write(fd, note_section, sizeof(note_section)));
169}
170
171TEST_F(MapInfoGetBuildIDTest, from_memory) {
172 InitElfData(tf_->fd);
173
174 EXPECT_EQ("ELF_BUILDID", map_info_->GetBuildID());
175}
176
177TEST_F(MapInfoGetBuildIDTest, multiple_thread_elf_exists_in_memory) {
178 InitElfData(tf_->fd);
179
180 MultipleThreadTest("ELF_BUILDID");
181}
182
183} // namespace unwindstack