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