blob: 70e136bcee1e62943500e4d06a0328fcae67f4c7 [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_);
Christopher Ferris0f40a052020-01-22 12:17:06 -080053 map_info_.reset(
54 new MapInfo(nullptr, nullptr, 0x1000, 0x20000, 0, PROT_READ | PROT_WRITE, tf_->path));
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080055 }
56
57 void MultipleThreadTest(std::string expected_build_id);
58
59 MemoryFake* memory_;
60 ElfFake* elf_;
61 ElfInterfaceFake* elf_interface_;
62 std::unique_ptr<ElfFake> elf_container_;
63 std::unique_ptr<MapInfo> map_info_;
64 std::unique_ptr<TemporaryFile> tf_;
65};
66
67TEST_F(MapInfoGetBuildIDTest, no_elf_and_no_valid_elf_in_memory) {
Christopher Ferris0f40a052020-01-22 12:17:06 -080068 MapInfo info(nullptr, nullptr, 0x1000, 0x2000, 0, PROT_READ, "");
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080069
70 EXPECT_EQ("", info.GetBuildID());
Christopher Ferrisb1c9c202019-01-25 14:28:13 -080071 EXPECT_EQ("", info.GetPrintableBuildID());
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080072}
73
74TEST_F(MapInfoGetBuildIDTest, from_elf) {
75 map_info_->elf.reset(elf_container_.release());
76 elf_interface_->FakeSetBuildID("FAKE_BUILD_ID");
77
78 EXPECT_EQ("FAKE_BUILD_ID", map_info_->GetBuildID());
Christopher Ferrisb1c9c202019-01-25 14:28:13 -080079 EXPECT_EQ("46414b455f4255494c445f4944", map_info_->GetPrintableBuildID());
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080080}
81
Christopher Ferrisce34d622019-01-30 10:55:27 -080082TEST_F(MapInfoGetBuildIDTest, from_elf_no_sign_extension) {
83 map_info_->elf.reset(elf_container_.release());
84
85 std::string build_id = {static_cast<char>(0xfa), static_cast<char>(0xab), static_cast<char>(0x12),
86 static_cast<char>(0x02)};
87 elf_interface_->FakeSetBuildID(build_id);
88
89 EXPECT_EQ("\xFA\xAB\x12\x2", map_info_->GetBuildID());
90 EXPECT_EQ("faab1202", map_info_->GetPrintableBuildID());
91}
92
Christopher Ferrisbf373ed2019-01-16 17:23:39 -080093void MapInfoGetBuildIDTest::MultipleThreadTest(std::string expected_build_id) {
94 static constexpr size_t kNumConcurrentThreads = 100;
95
96 std::string build_id_values[kNumConcurrentThreads];
97 std::vector<std::thread*> threads;
98
99 std::atomic_bool wait;
100 wait = true;
101 // Create all of the threads and have them do the GetLoadBias at the same time
102 // to make it likely that a race will occur.
103 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
104 std::thread* thread = new std::thread([i, this, &wait, &build_id_values]() {
105 while (wait)
106 ;
107 build_id_values[i] = map_info_->GetBuildID();
108 });
109 threads.push_back(thread);
110 }
111
112 // Set them all going and wait for the threads to finish.
113 wait = false;
114 for (auto thread : threads) {
115 thread->join();
116 delete thread;
117 }
118
119 // Now verify that all of the elf files are exactly the same and valid.
120 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
121 EXPECT_EQ(expected_build_id, build_id_values[i]) << "Thread " << i << " mismatched.";
122 }
123}
124
125TEST_F(MapInfoGetBuildIDTest, multiple_thread_elf_exists) {
126 map_info_->elf.reset(elf_container_.release());
127 elf_interface_->FakeSetBuildID("FAKE_BUILD_ID");
128
129 MultipleThreadTest("FAKE_BUILD_ID");
130}
131
132static void InitElfData(int fd) {
133 Elf32_Ehdr ehdr;
134 TestInitEhdr(&ehdr, ELFCLASS32, EM_ARM);
135 ehdr.e_shoff = 0x2000;
136 ehdr.e_shnum = 3;
137 ehdr.e_shentsize = sizeof(Elf32_Shdr);
138 ehdr.e_shstrndx = 2;
139 off_t offset = 0;
140 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
141 ASSERT_EQ(static_cast<ssize_t>(sizeof(ehdr)), write(fd, &ehdr, sizeof(ehdr)));
142
143 char note_section[128];
144 Elf32_Nhdr note_header = {};
Peter Collingbournea7b4c5d2020-03-30 18:38:21 -0700145 note_header.n_namesz = sizeof("GNU");
146 note_header.n_descsz = sizeof("ELF_BUILDID") - 1;
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800147 note_header.n_type = NT_GNU_BUILD_ID;
148 memcpy(&note_section, &note_header, sizeof(note_header));
149 size_t note_offset = sizeof(note_header);
Peter Collingbournea7b4c5d2020-03-30 18:38:21 -0700150 memcpy(&note_section[note_offset], "GNU", note_header.n_namesz);
151 note_offset += note_header.n_namesz;
152 memcpy(&note_section[note_offset], "ELF_BUILDID", note_header.n_descsz);
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800153
154 Elf32_Shdr shdr = {};
155 shdr.sh_type = SHT_NOTE;
156 shdr.sh_name = 0x500;
157 shdr.sh_offset = 0xb000;
158 shdr.sh_size = sizeof(note_section);
159 offset += ehdr.e_shoff + sizeof(shdr);
160 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
161 ASSERT_EQ(static_cast<ssize_t>(sizeof(shdr)), write(fd, &shdr, sizeof(shdr)));
162
163 // The string data for section header names.
164 memset(&shdr, 0, sizeof(shdr));
165 shdr.sh_type = SHT_STRTAB;
166 shdr.sh_name = 0x20000;
167 shdr.sh_offset = 0xf000;
168 shdr.sh_size = 0x1000;
169 offset += sizeof(shdr);
170 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
171 ASSERT_EQ(static_cast<ssize_t>(sizeof(shdr)), write(fd, &shdr, sizeof(shdr)));
172
173 offset = 0xf500;
174 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
175 ASSERT_EQ(static_cast<ssize_t>(sizeof(".note.gnu.build-id")),
176 write(fd, ".note.gnu.build-id", sizeof(".note.gnu.build-id")));
177
178 offset = 0xb000;
179 ASSERT_EQ(offset, lseek(fd, offset, SEEK_SET));
180 ASSERT_EQ(static_cast<ssize_t>(sizeof(note_section)),
181 write(fd, note_section, sizeof(note_section)));
182}
183
184TEST_F(MapInfoGetBuildIDTest, from_memory) {
185 InitElfData(tf_->fd);
186
187 EXPECT_EQ("ELF_BUILDID", map_info_->GetBuildID());
Christopher Ferrisb1c9c202019-01-25 14:28:13 -0800188 EXPECT_EQ("454c465f4255494c444944", map_info_->GetPrintableBuildID());
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800189}
190
191TEST_F(MapInfoGetBuildIDTest, multiple_thread_elf_exists_in_memory) {
192 InitElfData(tf_->fd);
193
194 MultipleThreadTest("ELF_BUILDID");
195}
196
Peter Collingbournea7b4c5d2020-03-30 18:38:21 -0700197TEST_F(MapInfoGetBuildIDTest, real_elf) {
198 MapInfo map_info(nullptr, nullptr, 0x1000, 0x20000, 0, PROT_READ | PROT_WRITE,
199 TestGetFileDirectory() + "offline/empty_arm64/libc.so");
200 EXPECT_EQ("6df0590c4920f4c7b9f34fe833f37d54", map_info.GetPrintableBuildID());
201}
202
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800203} // namespace unwindstack