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