| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "read_elf.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 21 | #include <map> |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <android-base/test_utils.h> |
| 25 | |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 26 | #include "get_test_data.h" |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 27 | #include "test_util.h" |
| Yabin Cui | f79a008 | 2016-11-14 11:23:14 -0800 | [diff] [blame] | 28 | #include "utils.h" |
| 29 | |
| 30 | #define ELF_NOTE_GNU "GNU" |
| 31 | #define NT_GNU_BUILD_ID 3 |
| 32 | |
| 33 | TEST(read_elf, GetBuildIdFromNoteSection) { |
| 34 | BuildId build_id; |
| 35 | std::vector<char> data; |
| 36 | // Fail to read build id for no data. |
| 37 | ASSERT_FALSE(GetBuildIdFromNoteSection(data.data(), 0, &build_id)); |
| 38 | |
| 39 | // Read build id from data starting from different alignment addresses. |
| 40 | char build_id_data[20]; |
| 41 | for (int i = 0; i < 20; ++i) { |
| 42 | build_id_data[i] = i; |
| 43 | } |
| 44 | BuildId expected_build_id(build_id_data, 20); |
| 45 | data.resize(100, '\0'); |
| 46 | |
| 47 | for (size_t alignment = 0; alignment <= 3; ++alignment) { |
| 48 | char* start = data.data() + alignment; |
| 49 | char* p = start; |
| 50 | uint32_t type = NT_GNU_BUILD_ID; |
| 51 | uint32_t namesz = 4; |
| 52 | uint32_t descsz = 20; |
| 53 | MoveToBinaryFormat(namesz, p); |
| 54 | MoveToBinaryFormat(descsz, p); |
| 55 | MoveToBinaryFormat(type, p); |
| 56 | MoveToBinaryFormat(ELF_NOTE_GNU, 4, p); |
| 57 | MoveToBinaryFormat(build_id_data, 20, p); |
| 58 | ASSERT_TRUE(GetBuildIdFromNoteSection(start, p - start, &build_id)); |
| 59 | ASSERT_TRUE(build_id == expected_build_id); |
| 60 | } |
| 61 | } |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 62 | |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 63 | TEST(read_elf, GetBuildIdFromElfFile) { |
| 64 | BuildId build_id; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 65 | ASSERT_EQ(ElfStatus::NO_ERROR, GetBuildIdFromElfFile(GetTestData(ELF_FILE), &build_id)); |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 66 | ASSERT_EQ(build_id, BuildId(elf_file_build_id)); |
| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 69 | TEST(read_elf, GetBuildIdFromEmbeddedElfFile) { |
| 70 | BuildId build_id; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 71 | ASSERT_EQ(ElfStatus::NO_ERROR, GetBuildIdFromEmbeddedElfFile(GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK, |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 72 | NATIVELIB_SIZE_IN_APK, &build_id)); |
| 73 | ASSERT_EQ(build_id, native_lib_build_id); |
| 74 | } |
| 75 | |
| 76 | void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols) { |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 77 | (*symbols)[symbol.name] = symbol; |
| 78 | } |
| 79 | |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 80 | static void CheckGlobalVariableSymbols(const std::map<std::string, ElfFileSymbol>& symbols) { |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 81 | auto pos = symbols.find("GlobalVar"); |
| 82 | ASSERT_NE(pos, symbols.end()); |
| 83 | ASSERT_FALSE(pos->second.is_func); |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void CheckFunctionSymbols(const std::map<std::string, ElfFileSymbol>& symbols) { |
| 87 | auto pos = symbols.find("GlobalFunc"); |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 88 | ASSERT_NE(pos, symbols.end()); |
| 89 | ASSERT_TRUE(pos->second.is_func); |
| 90 | ASSERT_TRUE(pos->second.is_in_text_section); |
| 91 | } |
| 92 | |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 93 | void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols) { |
| 94 | CheckGlobalVariableSymbols(symbols); |
| 95 | CheckFunctionSymbols(symbols); |
| 96 | } |
| 97 | |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 98 | TEST(read_elf, parse_symbols_from_elf_file_with_correct_build_id) { |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 99 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 100 | ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), elf_file_build_id, |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 101 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| 102 | CheckElfFileSymbols(symbols); |
| 103 | } |
| 104 | |
| 105 | TEST(read_elf, parse_symbols_from_elf_file_without_build_id) { |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 106 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 107 | ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), BuildId(), |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 108 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| 109 | CheckElfFileSymbols(symbols); |
| 110 | } |
| 111 | |
| 112 | TEST(read_elf, parse_symbols_from_elf_file_with_wrong_build_id) { |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 113 | BuildId build_id("01010101010101010101"); |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 114 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 115 | ASSERT_EQ(ElfStatus::BUILD_ID_MISMATCH, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), build_id, |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 116 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| Yabin Cui | ec12ed9 | 2015-06-08 10:38:10 -0700 | [diff] [blame] | 117 | } |
| Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 118 | |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 119 | TEST(read_elf, ParseSymbolsFromEmbeddedElfFile) { |
| 120 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 121 | ASSERT_EQ(ElfStatus::NO_SYMBOL_TABLE, ParseSymbolsFromEmbeddedElfFile(GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK, |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 122 | NATIVELIB_SIZE_IN_APK, native_lib_build_id, |
| 123 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| 124 | CheckElfFileSymbols(symbols); |
| 125 | } |
| 126 | |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 127 | TEST(read_elf, ParseSymbolFromMiniDebugInfoElfFile) { |
| 128 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 129 | ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE_WITH_MINI_DEBUG_INFO), BuildId(), |
| Yabin Cui | 0540053 | 2016-03-17 21:18:53 -0700 | [diff] [blame] | 130 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| 131 | CheckFunctionSymbols(symbols); |
| 132 | } |
| 133 | |
| Yabin Cui | a5bdf0a | 2015-06-22 19:41:39 -0700 | [diff] [blame] | 134 | TEST(read_elf, arm_mapping_symbol) { |
| 135 | ASSERT_TRUE(IsArmMappingSymbol("$a")); |
| 136 | ASSERT_FALSE(IsArmMappingSymbol("$b")); |
| 137 | ASSERT_TRUE(IsArmMappingSymbol("$a.anything")); |
| 138 | ASSERT_FALSE(IsArmMappingSymbol("$a_no_dot")); |
| 139 | } |
| Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 140 | |
| 141 | TEST(read_elf, IsValidElfPath) { |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 142 | ASSERT_NE(ElfStatus::NO_ERROR, IsValidElfPath("/dev/zero")); |
| 143 | TemporaryFile tmp_file; |
| 144 | ASSERT_EQ(ElfStatus::READ_FAILED, IsValidElfPath(tmp_file.path)); |
| 145 | ASSERT_TRUE(android::base::WriteStringToFile("wrong format for elf", tmp_file.path)); |
| 146 | ASSERT_EQ(ElfStatus::FILE_MALFORMED, IsValidElfPath(tmp_file.path)); |
| 147 | ASSERT_EQ(ElfStatus::NO_ERROR, IsValidElfPath(GetTestData(ELF_FILE))); |
| Yabin Cui | 797116b | 2015-12-08 17:43:15 -0800 | [diff] [blame] | 148 | } |
| Yabin Cui | 5783fa0 | 2016-07-06 18:29:00 -0700 | [diff] [blame] | 149 | |
| 150 | TEST(read_elf, check_symbol_for_plt_section) { |
| 151 | std::map<std::string, ElfFileSymbol> symbols; |
| Yabin Cui | dec43c1 | 2016-07-29 16:40:40 -0700 | [diff] [blame] | 152 | ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), BuildId(), |
| Yabin Cui | 5783fa0 | 2016-07-06 18:29:00 -0700 | [diff] [blame] | 153 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| 154 | ASSERT_NE(symbols.find("@plt"), symbols.end()); |
| 155 | } |
| Yabin Cui | e4e13f3 | 2017-12-15 11:36:51 -0800 | [diff] [blame] | 156 | |
| 157 | TEST(read_elf, read_elf_with_broken_section_table) { |
| Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 158 | std::string elf_path = GetTestData("libsgmainso-6.4.36.so"); |
| Yabin Cui | e4e13f3 | 2017-12-15 11:36:51 -0800 | [diff] [blame] | 159 | std::map<std::string, ElfFileSymbol> symbols; |
| 160 | ASSERT_EQ(ElfStatus::NO_SYMBOL_TABLE, |
| Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 161 | ParseSymbolsFromElfFile(elf_path, BuildId(), |
| Yabin Cui | e4e13f3 | 2017-12-15 11:36:51 -0800 | [diff] [blame] | 162 | std::bind(ParseSymbol, std::placeholders::_1, &symbols))); |
| Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 163 | BuildId build_id; |
| 164 | ASSERT_EQ(ElfStatus::NO_BUILD_ID, GetBuildIdFromElfFile(elf_path, &build_id)); |
| 165 | uint64_t min_vaddr; |
| 166 | ASSERT_EQ(ElfStatus::NO_ERROR, ReadMinExecutableVirtualAddressFromElfFile(elf_path, BuildId(), |
| 167 | &min_vaddr)); |
| 168 | ASSERT_EQ(min_vaddr, 0u); |
| Yabin Cui | e4e13f3 | 2017-12-15 11:36:51 -0800 | [diff] [blame] | 169 | } |