1 /*
2 * Copyright (C) 2017 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 "gtest/gtest.h"
18
19 #include "berberis/tiny_loader/tiny_loader.h"
20
21 #include <string>
22
23 #include <sys/user.h>
24
25 #include "berberis/base/file.h"
26 #include "berberis/base/stringprintf.h"
27
28 namespace {
29
30 const constexpr char* kTestSymbolName = "tiny_symbol";
31 const constexpr char* kTestLibInvalidElfClassName = "libtinytest_invalid_elf_class.so";
32 const constexpr char* kTestLibGnuName = "libtinytest.so";
33 const constexpr char* kTestLibSysvName = "libtinytest_sysv.so";
34 const constexpr char* kTestExecutableName = "tiny_static_executable";
35
36 #if defined(__LP64__)
37 constexpr uintptr_t kStaticExecutableEntryPoint = 0x1ce00;
38 constexpr const char* kTestFilesDir = "/tiny_loader/tests/files/64/";
39 #else
40 constexpr uintptr_t kStaticExecutableEntryPoint = 0x410f30;
41 constexpr const char* kTestFilesDir = "/tiny_loader/tests/files/32/";
42 #endif
43
AssertLoadedElfFilesEqual(const LoadedElfFile & actual,const LoadedElfFile & expected)44 void AssertLoadedElfFilesEqual(const LoadedElfFile& actual, const LoadedElfFile& expected) {
45 ASSERT_EQ(actual.e_type(), expected.e_type());
46 ASSERT_EQ(actual.base_addr(), expected.base_addr());
47 ASSERT_EQ(actual.load_bias(), expected.load_bias());
48 ASSERT_EQ(actual.entry_point(), expected.entry_point());
49 ASSERT_EQ(actual.phdr_table(), expected.phdr_table());
50 ASSERT_EQ(actual.phdr_count(), expected.phdr_count());
51 }
52
GetTestElfFilepath(const char * name,std::string * real_path,std::string * error_msg)53 bool GetTestElfFilepath(const char* name, std::string* real_path, std::string* error_msg) {
54 std::string path = berberis::GetExecutableDirectory();
55 path += kTestFilesDir;
56
57 std::string out_path;
58 if (!berberis::Realpath(path.c_str(), &out_path)) {
59 *error_msg = berberis::StringPrintf("Failed to get realpath for \"%s\"", path.c_str());
60 return false;
61 }
62
63 out_path += "/";
64 out_path += name;
65
66 if (!berberis::Realpath(out_path, real_path)) {
67 *error_msg = berberis::StringPrintf("\"%s\": does not exist", out_path.c_str());
68 return false;
69 }
70
71 return true;
72 }
73
TestLoadLibrary(const char * test_library_name)74 void TestLoadLibrary(const char* test_library_name) {
75 LoadedElfFile loaded_elf_file;
76 std::string error_msg;
77 std::string elf_filepath;
78 ASSERT_TRUE(GetTestElfFilepath(test_library_name, &elf_filepath, &error_msg)) << error_msg;
79 ASSERT_TRUE(TinyLoader::LoadFromFile(elf_filepath.c_str(), &loaded_elf_file, &error_msg))
80 << error_msg;
81
82 // Get AT_BASE -> note that even though linker does not use
83 // AT_BASE this is needed for dynamic vdso and passed to the linker
84 // as AT_SYSINFO_EHDR
85 void* base_addr = loaded_elf_file.base_addr();
86 ElfAddr load_bias = loaded_elf_file.load_bias();
87 ASSERT_TRUE(base_addr != nullptr);
88 ASSERT_TRUE(reinterpret_cast<void*>(load_bias) == base_addr);
89 ASSERT_TRUE(loaded_elf_file.phdr_table() != nullptr);
90 ASSERT_EQ(loaded_elf_file.phdr_count(), 9U);
91 void* symbol_addr = loaded_elf_file.FindSymbol(kTestSymbolName);
92 ASSERT_TRUE(symbol_addr != nullptr);
93 ASSERT_TRUE(static_cast<uint8_t*>(symbol_addr) > static_cast<uint8_t*>(base_addr));
94
95 std::vector<std::pair<std::string, void*>> symbols;
96 loaded_elf_file.ForEachSymbol([&symbols](const char* name, void* address, const ElfSym* s) {
97 if (s->st_size != 0) {
98 symbols.emplace_back(std::string(name), address);
99 }
100 });
101
102 ASSERT_EQ(1U, symbols.size());
103 ASSERT_EQ(kTestSymbolName, symbols.begin()->first);
104 ASSERT_EQ(symbol_addr, symbols.begin()->second);
105
106 // AT_ENTRY for this file is 0
107 ASSERT_TRUE(loaded_elf_file.entry_point() == nullptr);
108
109 ASSERT_EQ(ET_DYN, loaded_elf_file.e_type());
110
111 ASSERT_NE(nullptr, loaded_elf_file.dynamic());
112
113 // The second part of the test - to Load this file from already mapped memory.
114 // Check that resulted loaded_elf_file is effectively the same
115 LoadedElfFile memory_elf_file;
116 ASSERT_TRUE(TinyLoader::LoadFromMemory(elf_filepath.c_str(), base_addr, PAGE_SIZE,
117 &memory_elf_file, &error_msg))
118 << error_msg;
119 AssertLoadedElfFilesEqual(memory_elf_file, loaded_elf_file);
120 void* memory_symbol_addr = memory_elf_file.FindSymbol(kTestSymbolName);
121 ASSERT_EQ(symbol_addr, memory_symbol_addr);
122 }
123
124 } // namespace
125
TEST(tiny_loader,library_gnu_hash)126 TEST(tiny_loader, library_gnu_hash) {
127 TestLoadLibrary(kTestLibGnuName);
128 }
129
TEST(tiny_loader,library_sysv_hash)130 TEST(tiny_loader, library_sysv_hash) {
131 TestLoadLibrary(kTestLibSysvName);
132 }
133
TEST(tiny_loader,library_invalid_elf_class)134 TEST(tiny_loader, library_invalid_elf_class) {
135 LoadedElfFile loaded_elf_file;
136 std::string error_msg;
137 std::string elf_filepath;
138 ASSERT_TRUE(GetTestElfFilepath(kTestLibInvalidElfClassName, &elf_filepath, &error_msg))
139 << error_msg;
140 ASSERT_FALSE(TinyLoader::LoadFromFile(elf_filepath.c_str(), &loaded_elf_file, &error_msg));
141 #if defined(__LP64__)
142 std::string expected_error_msg =
143 "\"" + elf_filepath + "\" ELFCLASS32 is not supported, expected ELFCLASS64.";
144 #else
145 std::string expected_error_msg =
146 "\"" + elf_filepath + "\" ELFCLASS64 is not supported, expected ELFCLASS32.";
147 #endif
148 ASSERT_EQ(expected_error_msg, error_msg);
149 }
150
TEST(tiny_loader,binary)151 TEST(tiny_loader, binary) {
152 LoadedElfFile loaded_elf_file;
153 std::string error_msg;
154
155 std::string elf_filepath;
156 ASSERT_TRUE(GetTestElfFilepath(kTestExecutableName, &elf_filepath, &error_msg)) << error_msg;
157 ASSERT_TRUE(TinyLoader::LoadFromFile(elf_filepath.c_str(), &loaded_elf_file, &error_msg))
158 << error_msg;
159
160 ASSERT_EQ(reinterpret_cast<void*>(kStaticExecutableEntryPoint), loaded_elf_file.entry_point());
161 ASSERT_EQ(ET_EXEC, loaded_elf_file.e_type());
162
163 ASSERT_NE(nullptr, loaded_elf_file.phdr_table());
164
165 ASSERT_EQ(nullptr, loaded_elf_file.dynamic());
166
167 // The second part of the test - to Load this file from already mapped memory.
168 // Check that resulted loaded_elf_file is effectively the same
169 LoadedElfFile memory_elf_file;
170 ASSERT_TRUE(TinyLoader::LoadFromMemory(elf_filepath.c_str(), loaded_elf_file.base_addr(),
171 PAGE_SIZE, &memory_elf_file, &error_msg))
172 << error_msg;
173 AssertLoadedElfFilesEqual(memory_elf_file, loaded_elf_file);
174 }
175