1 /* 2 * Copyright (C) 2018 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 "dso.h" 18 19 #include <gtest/gtest.h> 20 21 #include <android-base/file.h> 22 #include <android-base/stringprintf.h> 23 24 #include "get_test_data.h" 25 #include "read_apk.h" 26 #include "utils.h" 27 28 using namespace simpleperf_dso_impl; 29 30 TEST(DebugElfFileFinder, use_build_id_list) { 31 // Create a temp symdir with build_id_list. 32 TemporaryDir tmpdir; 33 TemporaryFile tmpfile(tmpdir.path); 34 std::string data; 35 ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data)); 36 ASSERT_TRUE(android::base::WriteStringToFile(data, tmpfile.path)); 37 BuildId build_id(ELF_FILE_BUILD_ID); 38 std::string build_id_list = android::base::StringPrintf( 39 "%s=%s\n", build_id.ToString().c_str(), android::base::Basename(tmpfile.path).c_str()); 40 std::string build_id_list_file = std::string(tmpdir.path) + "/build_id_list"; 41 ASSERT_TRUE(android::base::WriteStringToFile(build_id_list, build_id_list_file)); 42 43 DebugElfFileFinder finder; 44 ASSERT_TRUE(finder.SetSymFsDir(tmpdir.path)); 45 ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), std::string(tmpfile.path)); 46 unlink(build_id_list_file.c_str()); 47 } 48 49 static std::string ConvertPathSeparator(const std::string& path) { 50 std::string result = path; 51 if (OS_PATH_SEPARATOR != '/') { 52 std::replace(result.begin(), result.end(), '/', OS_PATH_SEPARATOR); 53 } 54 return result; 55 } 56 57 TEST(DebugElfFileFinder, concatenating_symfs_dir) { 58 DebugElfFileFinder finder; 59 ASSERT_TRUE(finder.SetSymFsDir(GetTestDataDir())); 60 ASSERT_EQ(finder.GetPathInSymFsDir("/system/libc.so"), 61 GetTestDataDir() + "system" + OS_PATH_SEPARATOR + "libc.so"); 62 ASSERT_EQ(finder.GetPathInSymFsDir("/data/base.apk!/lib/base.so"), 63 GetTestDataDir() + "data" + OS_PATH_SEPARATOR + "base.apk!/lib/base.so"); 64 65 BuildId build_id(ELF_FILE_BUILD_ID); 66 ASSERT_EQ(finder.FindDebugFile(ELF_FILE, false, build_id), GetTestDataDir() + ELF_FILE); 67 std::string native_lib_in_apk = APK_FILE + "!/" + NATIVELIB_IN_APK; 68 std::string apk_path = ConvertPathSeparator(APK_FILE); 69 ASSERT_EQ(finder.FindDebugFile(native_lib_in_apk, false, native_lib_build_id), 70 GetTestDataDir() + apk_path + "!/" + NATIVELIB_IN_APK); 71 } 72 73 TEST(DebugElfFileFinder, use_vdso) { 74 DebugElfFileFinder finder; 75 std::string fake_vdso32 = "fake_vdso32"; 76 std::string fake_vdso64 = "fake_vdso64"; 77 finder.SetVdsoFile(fake_vdso32, false); 78 finder.SetVdsoFile(fake_vdso64, true); 79 BuildId build_id; 80 ASSERT_EQ(finder.FindDebugFile("[vdso]", false, build_id), fake_vdso32); 81 ASSERT_EQ(finder.FindDebugFile("[vdso]", true, build_id), fake_vdso64); 82 } 83 84 TEST(DebugElfFileFinder, add_symbol_dir) { 85 DebugElfFileFinder finder; 86 ASSERT_FALSE(finder.AddSymbolDir(GetTestDataDir() + "dir_not_exist")); 87 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf"); 88 std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + CORRECT_SYMFS_FOR_BUILD_ID_CHECK); 89 ASSERT_TRUE(finder.AddSymbolDir(symfs_dir)); 90 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), 91 symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check"); 92 } 93 94 TEST(DebugElfFileFinder, build_id_list) { 95 DebugElfFileFinder finder; 96 // Find file in symfs dir with correct build_id_list. 97 std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_with_build_id_list"); 98 ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); 99 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), 100 symfs_dir + OS_PATH_SEPARATOR + "elf_for_build_id_check"); 101 102 // Find file in symfs_dir with wrong build_id_list. 103 symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_with_wrong_build_id_list"); 104 finder.Reset(); 105 ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); 106 ASSERT_EQ(finder.FindDebugFile("elf", false, CHECK_ELF_FILE_BUILD_ID), "elf"); 107 } 108 109 TEST(DebugElfFileFinder, no_build_id) { 110 DebugElfFileFinder finder; 111 // If not given a build id, we should match an elf in symfs without build id. 112 std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_without_build_id"); 113 ASSERT_TRUE(finder.SetSymFsDir(symfs_dir)); 114 BuildId build_id; 115 ASSERT_EQ(finder.FindDebugFile("elf", false, build_id), symfs_dir + OS_PATH_SEPARATOR + "elf"); 116 } 117 118 TEST(DebugElfFileFinder, find_basename_in_symfs_dir) { 119 DebugElfFileFinder finder; 120 // Find normal elf file. 121 finder.SetSymFsDir(GetTestDataDir()); 122 BuildId build_id(ELF_FILE_BUILD_ID); 123 ASSERT_EQ(finder.FindDebugFile("random_dir/elf", false, build_id), GetTestData("elf")); 124 125 // Find embedded native library. 126 ASSERT_EQ(finder.FindDebugFile("base.apk!/lib/x86_64/elf", false, build_id), GetTestData("elf")); 127 128 // Find elf file without build id. 129 std::string symfs_dir = ConvertPathSeparator(GetTestDataDir() + "data/symfs_without_build_id"); 130 finder.SetSymFsDir(symfs_dir); 131 build_id = BuildId(); 132 ASSERT_EQ(finder.FindDebugFile("random_dir/elf", false, build_id), 133 symfs_dir + OS_PATH_SEPARATOR + "elf"); 134 } 135 136 TEST(dso, dex_file_dso) { 137 #if defined(__linux__) 138 for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) { 139 std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex")); 140 ASSERT_TRUE(dso); 141 dso->AddDexFileOffset(0x28); 142 ASSERT_EQ(DSO_DEX_FILE, dso->type()); 143 const Symbol* symbol = dso->FindSymbol(0x6c77e); 144 ASSERT_NE(symbol, nullptr); 145 ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e)); 146 ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16)); 147 ASSERT_STREQ(symbol->DemangledName(), 148 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run"); 149 uint64_t min_vaddr; 150 uint64_t file_offset_of_min_vaddr; 151 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr); 152 ASSERT_EQ(min_vaddr, 0); 153 ASSERT_EQ(file_offset_of_min_vaddr, 0); 154 155 // Don't crash on not exist zip entry. 156 dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry")); 157 ASSERT_TRUE(dso); 158 ASSERT_EQ(nullptr, dso->FindSymbol(0)); 159 } 160 #else 161 GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile"; 162 #endif // defined(__linux__) 163 } 164 165 TEST(dso, dex_file_offsets) { 166 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, ""); 167 ASSERT_TRUE(dso); 168 for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) { 169 dso->AddDexFileOffset(offset); 170 } 171 ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5})); 172 } 173 174 TEST(dso, embedded_elf) { 175 const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK); 176 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path); 177 ASSERT_TRUE(dso); 178 ASSERT_EQ(dso->Path(), file_path); 179 ASSERT_EQ(dso->GetDebugFilePath(), file_path); 180 uint64_t min_vaddr; 181 uint64_t file_offset_of_min_vaddr; 182 dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr); 183 ASSERT_EQ(min_vaddr, 0); 184 ASSERT_EQ(file_offset_of_min_vaddr, 0); 185 const Symbol* symbol = dso->FindSymbol(0x9a4); 186 ASSERT_TRUE(symbol != nullptr); 187 ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1"); 188 BuildId build_id; 189 ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id)); 190 ASSERT_EQ(build_id, native_lib_build_id); 191 } 192 193 TEST(dso, IpToVaddrInFile) { 194 std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("libc.so")); 195 ASSERT_TRUE(dso); 196 ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000)); 197 } 198