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 
TEST(DebugElfFileFinder,use_build_id_list)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 
ConvertPathSeparator(const std::string & path)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 
TEST(DebugElfFileFinder,concatenating_symfs_dir)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 
TEST(DebugElfFileFinder,use_vdso)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 
TEST(DebugElfFileFinder,add_symbol_dir)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 
TEST(DebugElfFileFinder,build_id_list)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 
TEST(dso,dex_file_dso)109 TEST(dso, dex_file_dso) {
110 #if defined(__linux__)
111   for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
112     std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
113     ASSERT_TRUE(dso);
114     dso->AddDexFileOffset(0x28);
115     ASSERT_EQ(DSO_DEX_FILE, dso->type());
116     const Symbol* symbol = dso->FindSymbol(0x6c77e);
117     ASSERT_NE(symbol, nullptr);
118     ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
119     ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
120     ASSERT_STREQ(symbol->DemangledName(),
121                  "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
122     uint64_t min_vaddr;
123     uint64_t file_offset_of_min_vaddr;
124     dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
125     ASSERT_EQ(min_vaddr, 0);
126     ASSERT_EQ(file_offset_of_min_vaddr, 0);
127 
128     // Don't crash on not exist zip entry.
129     dso = Dso::CreateDso(dso_type, GetTestData("base.zip!/not_exist_entry"));
130     ASSERT_TRUE(dso);
131     ASSERT_EQ(nullptr, dso->FindSymbol(0));
132   }
133 #else
134   GTEST_LOG_(INFO) << "This test only runs on linux because of libdexfile";
135 #endif  // defined(__linux__)
136 }
137 
TEST(dso,dex_file_offsets)138 TEST(dso, dex_file_offsets) {
139   std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_DEX_FILE, "");
140   ASSERT_TRUE(dso);
141   for (uint64_t offset : {0x3, 0x1, 0x5, 0x4, 0x2, 0x4, 0x3}) {
142     dso->AddDexFileOffset(offset);
143   }
144   ASSERT_EQ(*dso->DexFileOffsets(), std::vector<uint64_t>({0x1, 0x2, 0x3, 0x4, 0x5}));
145 }
146 
TEST(dso,embedded_elf)147 TEST(dso, embedded_elf) {
148   const std::string file_path = GetUrlInApk(GetTestData(APK_FILE), NATIVELIB_IN_APK);
149   std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, file_path);
150   ASSERT_TRUE(dso);
151   ASSERT_EQ(dso->Path(), file_path);
152   ASSERT_EQ(dso->GetDebugFilePath(), file_path);
153   uint64_t min_vaddr;
154   uint64_t file_offset_of_min_vaddr;
155   dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
156   ASSERT_EQ(min_vaddr, 0);
157   ASSERT_EQ(file_offset_of_min_vaddr, 0);
158   const Symbol* symbol = dso->FindSymbol(0x9a4);
159   ASSERT_TRUE(symbol != nullptr);
160   ASSERT_STREQ(symbol->Name(), "Java_com_example_hellojni_HelloJni_callFunc1");
161   BuildId build_id;
162   ASSERT_TRUE(GetBuildIdFromDsoPath(file_path, &build_id));
163   ASSERT_EQ(build_id, native_lib_build_id);
164 }
165 
TEST(dso,IpToVaddrInFile)166 TEST(dso, IpToVaddrInFile) {
167   std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("libc.so"));
168   ASSERT_TRUE(dso);
169   ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000));
170 }
171