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