1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "base/debug/elf_reader.h"
#include <dlfcn.h>
#include <cstdint>
#include <optional>
#include <string_view>
#include "base/debug/test_elf_image_builder.h"
#include "base/files/memory_mapped_file.h"
#include "base/native_library.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
extern char __executable_start;
namespace base::debug {
namespace {
constexpr uint8_t kBuildIdBytes[] = {0xab, 0xcd, 0x12, 0x34};
constexpr const char kBuildIdHexString[] = "ABCD1234";
constexpr const char kBuildIdHexStringLower[] = "ABCD1234";
std::string ParamInfoToString(
const ::testing::TestParamInfo<base::TestElfImageBuilder::MappingType>&
param_info) {
switch (param_info.param) {
case TestElfImageBuilder::RELOCATABLE:
return "Relocatable";
case TestElfImageBuilder::RELOCATABLE_WITH_BIAS:
return "RelocatableWithBias";
case TestElfImageBuilder::NON_RELOCATABLE:
return "NonRelocatable";
}
}
} // namespace
using ElfReaderTest =
::testing::TestWithParam<TestElfImageBuilder::MappingType>;
TEST_P(ElfReaderTest, ReadElfBuildIdUppercase) {
TestElfImage image =
TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddNoteSegment(NT_GNU_BUILD_ID, "GNU", kBuildIdBytes)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), true, build_id);
EXPECT_EQ(8u, build_id_size);
EXPECT_EQ(kBuildIdHexString, std::string_view(&build_id[0], build_id_size));
}
TEST_P(ElfReaderTest, ReadElfBuildIdLowercase) {
TestElfImage image =
TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddNoteSegment(NT_GNU_BUILD_ID, "GNU", kBuildIdBytes)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), false, build_id);
EXPECT_EQ(8u, build_id_size);
EXPECT_EQ(ToLowerASCII(kBuildIdHexStringLower),
std::string_view(&build_id[0], build_id_size));
}
TEST_P(ElfReaderTest, ReadElfBuildIdMultipleNotes) {
constexpr uint8_t kOtherNoteBytes[] = {0xef, 0x56};
TestElfImage image =
TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddNoteSegment(NT_GNU_BUILD_ID + 1, "ABC", kOtherNoteBytes)
.AddNoteSegment(NT_GNU_BUILD_ID, "GNU", kBuildIdBytes)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), true, build_id);
EXPECT_EQ(8u, build_id_size);
EXPECT_EQ(kBuildIdHexString, std::string_view(&build_id[0], build_id_size));
}
TEST_P(ElfReaderTest, ReadElfBuildIdWrongName) {
TestElfImage image =
TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddNoteSegment(NT_GNU_BUILD_ID, "ABC", kBuildIdBytes)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), true, build_id);
EXPECT_EQ(0u, build_id_size);
}
TEST_P(ElfReaderTest, ReadElfBuildIdWrongType) {
TestElfImage image =
TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddNoteSegment(NT_GNU_BUILD_ID + 1, "GNU", kBuildIdBytes)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), true, build_id);
EXPECT_EQ(0u, build_id_size);
}
TEST_P(ElfReaderTest, ReadElfBuildIdNoBuildId) {
TestElfImage image = TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.Build();
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(image.elf_start(), true, build_id);
EXPECT_EQ(0u, build_id_size);
}
TEST_P(ElfReaderTest, ReadElfLibraryName) {
TestElfImage image = TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.AddSoName("mysoname")
.Build();
std::optional<std::string_view> library_name =
ReadElfLibraryName(image.elf_start());
ASSERT_NE(std::nullopt, library_name);
EXPECT_EQ("mysoname", *library_name);
}
TEST_P(ElfReaderTest, ReadElfLibraryNameNoSoName) {
TestElfImage image = TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.Build();
std::optional<std::string_view> library_name =
ReadElfLibraryName(image.elf_start());
EXPECT_EQ(std::nullopt, library_name);
}
TEST_P(ElfReaderTest, GetRelocationOffset) {
TestElfImage image = TestElfImageBuilder(GetParam())
.AddLoadSegment(PF_R | PF_X, /* size = */ 2000)
.Build();
switch (GetParam()) {
case TestElfImageBuilder::RELOCATABLE:
EXPECT_EQ(reinterpret_cast<size_t>(image.elf_start()),
GetRelocationOffset(image.elf_start()));
break;
case TestElfImageBuilder::RELOCATABLE_WITH_BIAS:
EXPECT_EQ(reinterpret_cast<size_t>(image.elf_start()) -
TestElfImageBuilder::kLoadBias,
GetRelocationOffset(image.elf_start()));
break;
case TestElfImageBuilder::NON_RELOCATABLE:
EXPECT_EQ(0u, GetRelocationOffset(image.elf_start()));
break;
}
}
INSTANTIATE_TEST_SUITE_P(
MappingTypes,
ElfReaderTest,
::testing::Values(TestElfImageBuilder::RELOCATABLE,
TestElfImageBuilder::RELOCATABLE_WITH_BIAS,
TestElfImageBuilder::NON_RELOCATABLE),
&ParamInfoToString);
TEST(ElfReaderTestWithCurrentElfImage, ReadElfBuildId) {
ElfBuildIdBuffer build_id;
size_t build_id_size = ReadElfBuildId(&__executable_start, true, build_id);
ASSERT_NE(build_id_size, 0u);
#if defined(OFFICIAL_BUILD)
constexpr size_t kExpectedBuildIdStringLength = 40; // SHA1 hash in hex.
#else
constexpr size_t kExpectedBuildIdStringLength = 16; // 64-bit int in hex.
#endif
EXPECT_EQ(kExpectedBuildIdStringLength, build_id_size);
for (size_t i = 0; i < build_id_size; ++i) {
char c = build_id[i];
EXPECT_TRUE(IsHexDigit(c));
EXPECT_FALSE(IsAsciiLower(c));
}
}
TEST(ElfReaderTestWithCurrentImage, ReadElfBuildId) {
#if BUILDFLAG(IS_ANDROID)
// On Android the library loader memory maps the full so file.
const char kLibraryName[] = "libbase_unittests__library";
const void* addr = &__executable_start;
#else
const char kLibraryName[] = MALLOC_WRAPPER_LIB;
// On Linux the executable does not contain soname and is not mapped till
// dynamic segment. So, use malloc wrapper so file on which the test already
// depends on.
// Find any symbol in the loaded file.
//
NativeLibraryLoadError error;
NativeLibrary library =
LoadNativeLibrary(base::FilePath(kLibraryName), &error);
void* init_addr =
GetFunctionPointerFromNativeLibrary(library, "MallocWrapper");
// Use this symbol to get full path to the loaded library.
Dl_info info;
int res = dladdr(init_addr, &info);
ASSERT_NE(0, res);
const void* addr = info.dli_fbase;
#endif
auto name = ReadElfLibraryName(addr);
ASSERT_TRUE(name);
EXPECT_NE(std::string::npos, name->find(kLibraryName))
<< "Library name " << *name << " doesn't contain expected "
<< kLibraryName;
#if !BUILDFLAG(IS_ANDROID)
UnloadNativeLibrary(library);
#endif
}
} // namespace base::debug
|