1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/debug/elf_reader_linux.h"
6
7 #include <dlfcn.h>
8
9 #include "base/files/memory_mapped_file.h"
10 #include "base/strings/string_util.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 extern char __executable_start;
15
16 namespace base {
17 namespace debug {
18
19 // The linker flag --build-id is passed only on official builds. Clang does not
20 // enable it by default and we do not have build id section in non-official
21 // builds.
22 #if defined(OFFICIAL_BUILD)
TEST(ElfReaderTest,ReadElfBuildId)23 TEST(ElfReaderTest, ReadElfBuildId) {
24 Optional<std::string> build_id = ReadElfBuildId(&__executable_start);
25 ASSERT_TRUE(build_id);
26 const size_t kGuidBytes = 20;
27 EXPECT_EQ(2 * kGuidBytes, build_id.value().size());
28 for (char c : *build_id) {
29 EXPECT_TRUE(IsHexDigit(c));
30 EXPECT_FALSE(IsAsciiLower(c));
31 }
32 }
33 #endif
34
TEST(ElfReaderTest,ReadElfLibraryName)35 TEST(ElfReaderTest, ReadElfLibraryName) {
36 #if defined(OS_ANDROID)
37 // On Android the library loader memory maps the full so file.
38 const char kLibraryName[] = "lib_base_unittests__library";
39 const void* addr = &__executable_start;
40 #else
41 // On Linux the executable does not contain soname and is not mapped till
42 // dynamic segment. So, use malloc wrapper so file on which the test already
43 // depends on.
44 const char kLibraryName[] = MALLOC_WRAPPER_LIB;
45 // Find any symbol in the loaded file.
46 void* handle = dlopen(kLibraryName, RTLD_NOW | RTLD_LOCAL);
47 const void* init_addr = dlsym(handle, "_init");
48 // Use this symbol to get full path to the loaded library.
49 Dl_info info;
50 int res = dladdr(init_addr, &info);
51 ASSERT_NE(0, res);
52 std::string filename(info.dli_fname);
53 EXPECT_FALSE(filename.empty());
54 EXPECT_NE(std::string::npos, filename.find(kLibraryName));
55
56 // Memory map the so file and use it to test reading so name.
57 MemoryMappedFile file;
58 file.Initialize(FilePath(filename));
59 const void* addr = file.data();
60 #endif
61
62 auto name = ReadElfLibraryName(addr);
63 ASSERT_TRUE(name);
64 EXPECT_NE(std::string::npos, name->find(kLibraryName))
65 << "Library name " << *name << " doesn't contain expected "
66 << kLibraryName;
67 }
68
69 } // namespace debug
70 } // namespace base
71