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
21 #include <map>
22
23 #include <android-base/file.h>
24 #include <android-base/test_utils.h>
25
26 #include "get_test_data.h"
27 #include "test_util.h"
28 #include "utils.h"
29
30 #define ELF_NOTE_GNU "GNU"
31 #define NT_GNU_BUILD_ID 3
32
TEST(read_elf,GetBuildIdFromNoteSection)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 }
62
TEST(read_elf,GetBuildIdFromElfFile)63 TEST(read_elf, GetBuildIdFromElfFile) {
64 BuildId build_id;
65 ASSERT_EQ(ElfStatus::NO_ERROR, GetBuildIdFromElfFile(GetTestData(ELF_FILE), &build_id));
66 ASSERT_EQ(build_id, BuildId(elf_file_build_id));
67 }
68
TEST(read_elf,GetBuildIdFromEmbeddedElfFile)69 TEST(read_elf, GetBuildIdFromEmbeddedElfFile) {
70 BuildId build_id;
71 ASSERT_EQ(ElfStatus::NO_ERROR, GetBuildIdFromEmbeddedElfFile(GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK,
72 NATIVELIB_SIZE_IN_APK, &build_id));
73 ASSERT_EQ(build_id, native_lib_build_id);
74 }
75
ParseSymbol(const ElfFileSymbol & symbol,std::map<std::string,ElfFileSymbol> * symbols)76 void ParseSymbol(const ElfFileSymbol& symbol, std::map<std::string, ElfFileSymbol>* symbols) {
77 (*symbols)[symbol.name] = symbol;
78 }
79
CheckGlobalVariableSymbols(const std::map<std::string,ElfFileSymbol> & symbols)80 static void CheckGlobalVariableSymbols(const std::map<std::string, ElfFileSymbol>& symbols) {
81 auto pos = symbols.find("GlobalVar");
82 ASSERT_NE(pos, symbols.end());
83 ASSERT_FALSE(pos->second.is_func);
84 }
85
CheckFunctionSymbols(const std::map<std::string,ElfFileSymbol> & symbols)86 static void CheckFunctionSymbols(const std::map<std::string, ElfFileSymbol>& symbols) {
87 auto pos = symbols.find("GlobalFunc");
88 ASSERT_NE(pos, symbols.end());
89 ASSERT_TRUE(pos->second.is_func);
90 ASSERT_TRUE(pos->second.is_in_text_section);
91 }
92
CheckElfFileSymbols(const std::map<std::string,ElfFileSymbol> & symbols)93 void CheckElfFileSymbols(const std::map<std::string, ElfFileSymbol>& symbols) {
94 CheckGlobalVariableSymbols(symbols);
95 CheckFunctionSymbols(symbols);
96 }
97
TEST(read_elf,parse_symbols_from_elf_file_with_correct_build_id)98 TEST(read_elf, parse_symbols_from_elf_file_with_correct_build_id) {
99 std::map<std::string, ElfFileSymbol> symbols;
100 ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), elf_file_build_id,
101 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
102 CheckElfFileSymbols(symbols);
103 }
104
TEST(read_elf,parse_symbols_from_elf_file_without_build_id)105 TEST(read_elf, parse_symbols_from_elf_file_without_build_id) {
106 std::map<std::string, ElfFileSymbol> symbols;
107 ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), BuildId(),
108 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
109 CheckElfFileSymbols(symbols);
110 }
111
TEST(read_elf,parse_symbols_from_elf_file_with_wrong_build_id)112 TEST(read_elf, parse_symbols_from_elf_file_with_wrong_build_id) {
113 BuildId build_id("01010101010101010101");
114 std::map<std::string, ElfFileSymbol> symbols;
115 ASSERT_EQ(ElfStatus::BUILD_ID_MISMATCH, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), build_id,
116 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
117 }
118
TEST(read_elf,ParseSymbolsFromEmbeddedElfFile)119 TEST(read_elf, ParseSymbolsFromEmbeddedElfFile) {
120 std::map<std::string, ElfFileSymbol> symbols;
121 ASSERT_EQ(ElfStatus::NO_SYMBOL_TABLE, ParseSymbolsFromEmbeddedElfFile(GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK,
122 NATIVELIB_SIZE_IN_APK, native_lib_build_id,
123 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
124 CheckElfFileSymbols(symbols);
125 }
126
TEST(read_elf,ParseSymbolFromMiniDebugInfoElfFile)127 TEST(read_elf, ParseSymbolFromMiniDebugInfoElfFile) {
128 std::map<std::string, ElfFileSymbol> symbols;
129 ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE_WITH_MINI_DEBUG_INFO), BuildId(),
130 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
131 CheckFunctionSymbols(symbols);
132 }
133
TEST(read_elf,arm_mapping_symbol)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 }
140
TEST(read_elf,IsValidElfPath)141 TEST(read_elf, IsValidElfPath) {
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)));
148 }
149
TEST(read_elf,check_symbol_for_plt_section)150 TEST(read_elf, check_symbol_for_plt_section) {
151 std::map<std::string, ElfFileSymbol> symbols;
152 ASSERT_EQ(ElfStatus::NO_ERROR, ParseSymbolsFromElfFile(GetTestData(ELF_FILE), BuildId(),
153 std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
154 ASSERT_NE(symbols.find("@plt"), symbols.end());
155 }
156