| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 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 <elf.h> | 
|  | 18 | #include <string.h> | 
|  | 19 |  | 
|  | 20 | #include <memory> | 
|  | 21 | #include <vector> | 
|  | 22 |  | 
|  | 23 | #include <gtest/gtest.h> | 
|  | 24 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 25 | #include <unwindstack/DexFiles.h> | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 26 | #include <unwindstack/Elf.h> | 
|  | 27 | #include <unwindstack/MapInfo.h> | 
|  | 28 | #include <unwindstack/Maps.h> | 
|  | 29 | #include <unwindstack/Memory.h> | 
|  | 30 |  | 
|  | 31 | #include "DexFileData.h" | 
|  | 32 | #include "ElfFake.h" | 
|  | 33 | #include "MemoryFake.h" | 
|  | 34 |  | 
|  | 35 | namespace unwindstack { | 
|  | 36 |  | 
|  | 37 | class DexFilesTest : public ::testing::Test { | 
|  | 38 | protected: | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 39 | void CreateFakeElf(MapInfo* map_info, uint64_t global_offset, uint64_t data_offset, | 
|  | 40 | uint64_t data_vaddr, uint64_t data_size) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 41 | MemoryFake* memory = new MemoryFake; | 
|  | 42 | ElfFake* elf = new ElfFake(memory); | 
|  | 43 | elf->FakeSetValid(true); | 
|  | 44 | ElfInterfaceFake* interface = new ElfInterfaceFake(memory); | 
|  | 45 | elf->FakeSetInterface(interface); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 46 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 47 | interface->FakeSetGlobalVariable("__dex_debug_descriptor", global_offset); | 
|  | 48 | interface->FakeSetDataOffset(data_offset); | 
|  | 49 | interface->FakeSetDataVaddrStart(data_vaddr); | 
|  | 50 | interface->FakeSetDataVaddrEnd(data_vaddr + data_size); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 51 | map_info->elf.reset(elf); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | void Init(ArchEnum arch) { | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 55 | dex_files_.reset(new DexFiles(process_memory_)); | 
|  | 56 | dex_files_->SetArch(arch); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 57 |  | 
|  | 58 | maps_.reset( | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 59 | new BufferMaps("1000-4000 ---s 00000000 00:00 0 /fake/elf\n" | 
|  | 60 | "4000-6000 r--s 00000000 00:00 0 /fake/elf\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 61 | "6000-8000 -wxs 00002000 00:00 0 /fake/elf\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 62 | "a000-c000 r--p 00000000 00:00 0 /fake/elf2\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 63 | "c000-f000 rw-p 00002000 00:00 0 /fake/elf2\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 64 | "f000-11000 r--p 00000000 00:00 0 /fake/elf3\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 65 | "100000-110000 rw-p 00f1000 00:00 0 /fake/elf3\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 66 | "200000-210000 rw-p 0002000 00:00 0 /fake/elf3\n" | 
| Christopher Ferris | de5cd8c | 2020-01-21 17:56:25 -0800 | [diff] [blame] | 67 | "300000-400000 rw-p 0003000 00:00 0 /fake/elf3\n" | 
|  | 68 | "500000-501000 r--p 0000000 00:00 0 /fake/elf4\n" | 
|  | 69 | "501000-502000 ---p 0000000 00:00 0\n" | 
|  | 70 | "503000-510000 rw-p 0003000 00:00 0 /fake/elf4\n" | 
|  | 71 | "510000-520000 rw-p 0010000 00:00 0 /fake/elf4\n")); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 72 | ASSERT_TRUE(maps_->Parse()); | 
|  | 73 |  | 
| Christopher Ferris | 1f34c0e | 2018-10-08 17:39:39 -0700 | [diff] [blame] | 74 | // Global variable in a section that is not readable. | 
|  | 75 | MapInfo* map_info = maps_->Get(kMapGlobalNonReadable); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 76 | ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 77 | CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | // Global variable not set by default. | 
|  | 80 | map_info = maps_->Get(kMapGlobalSetToZero); | 
|  | 81 | ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 82 | CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | // Global variable set in this map. | 
|  | 85 | map_info = maps_->Get(kMapGlobal); | 
|  | 86 | ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 87 | CreateFakeElf(map_info, 0xf1800, 0xf1000, 0xf1000, 0x10000); | 
| Christopher Ferris | de5cd8c | 2020-01-21 17:56:25 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | // Global variable set in this map, but there is an empty map before rw map. | 
|  | 90 | map_info = maps_->Get(kMapGlobalAfterEmpty); | 
|  | 91 | ASSERT_TRUE(map_info != nullptr); | 
|  | 92 | CreateFakeElf(map_info, 0x3800, 0x3000, 0x3000, 0xd000); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | void SetUp() override { | 
|  | 96 | memory_ = new MemoryFake; | 
|  | 97 | process_memory_.reset(memory_); | 
|  | 98 |  | 
|  | 99 | Init(ARCH_ARM); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 102 | void WriteDescriptor32(uint64_t addr, uint32_t head); | 
|  | 103 | void WriteDescriptor64(uint64_t addr, uint64_t head); | 
|  | 104 | void WriteEntry32(uint64_t entry_addr, uint32_t next, uint32_t prev, uint32_t dex_file); | 
|  | 105 | void WriteEntry64(uint64_t entry_addr, uint64_t next, uint64_t prev, uint64_t dex_file); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 106 | void WriteDex(uint64_t dex_file); | 
|  | 107 |  | 
| Christopher Ferris | 1f34c0e | 2018-10-08 17:39:39 -0700 | [diff] [blame] | 108 | static constexpr size_t kMapGlobalNonReadable = 2; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 109 | static constexpr size_t kMapGlobalSetToZero = 3; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 110 | static constexpr size_t kMapGlobal = 5; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 111 | static constexpr size_t kMapGlobalRw = 6; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 112 | static constexpr size_t kMapDexFileEntries = 7; | 
|  | 113 | static constexpr size_t kMapDexFiles = 8; | 
| Christopher Ferris | de5cd8c | 2020-01-21 17:56:25 -0800 | [diff] [blame] | 114 | static constexpr size_t kMapGlobalAfterEmpty = 9; | 
|  | 115 | static constexpr size_t kMapDexFilesAfterEmpty = 12; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 116 |  | 
|  | 117 | std::shared_ptr<Memory> process_memory_; | 
|  | 118 | MemoryFake* memory_; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 119 | std::unique_ptr<DexFiles> dex_files_; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 120 | std::unique_ptr<BufferMaps> maps_; | 
|  | 121 | }; | 
|  | 122 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 123 | void DexFilesTest::WriteDescriptor32(uint64_t addr, uint32_t head) { | 
|  | 124 | //   void* first_entry_ | 
|  | 125 | memory_->SetData32(addr + 12, head); | 
| David Srbecky | 4015ef4 | 2018-02-15 17:57:16 +0000 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 128 | void DexFilesTest::WriteDescriptor64(uint64_t addr, uint64_t head) { | 
|  | 129 | //   void* first_entry_ | 
|  | 130 | memory_->SetData64(addr + 16, head); | 
| David Srbecky | 4015ef4 | 2018-02-15 17:57:16 +0000 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 133 | void DexFilesTest::WriteEntry32(uint64_t entry_addr, uint32_t next, uint32_t prev, | 
|  | 134 | uint32_t dex_file) { | 
|  | 135 | // Format of the 32 bit DEXFileEntry structure: | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 136 | //   uint32_t next | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 137 | memory_->SetData32(entry_addr, next); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 138 | //   uint32_t prev | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 139 | memory_->SetData32(entry_addr + 4, prev); | 
|  | 140 | //   uint32_t dex_file | 
|  | 141 | memory_->SetData32(entry_addr + 8, dex_file); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 144 | void DexFilesTest::WriteEntry64(uint64_t entry_addr, uint64_t next, uint64_t prev, | 
|  | 145 | uint64_t dex_file) { | 
|  | 146 | // Format of the 64 bit DEXFileEntry structure: | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 147 | //   uint64_t next | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 148 | memory_->SetData64(entry_addr, next); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 149 | //   uint64_t prev | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 150 | memory_->SetData64(entry_addr + 8, prev); | 
|  | 151 | //   uint64_t dex_file | 
|  | 152 | memory_->SetData64(entry_addr + 16, dex_file); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
|  | 155 | void DexFilesTest::WriteDex(uint64_t dex_file) { | 
|  | 156 | memory_->SetMemory(dex_file, kDexData, sizeof(kDexData) * sizeof(uint32_t)); | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | TEST_F(DexFilesTest, get_method_information_invalid) { | 
|  | 160 | std::string method_name = "nothing"; | 
|  | 161 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 162 | MapInfo* info = maps_->Get(kMapDexFileEntries); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 163 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 164 | dex_files_->GetMethodInformation(maps_.get(), info, 0, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 165 | EXPECT_EQ("nothing", method_name); | 
|  | 166 | EXPECT_EQ(0x124U, method_offset); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | TEST_F(DexFilesTest, get_method_information_32) { | 
|  | 170 | std::string method_name = "nothing"; | 
|  | 171 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 172 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 173 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 174 | WriteDescriptor32(0x100800, 0x200000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 175 | WriteEntry32(0x200000, 0, 0, 0x300000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 176 | WriteDex(0x300000); | 
|  | 177 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 178 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 179 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 180 | EXPECT_EQ(0U, method_offset); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | TEST_F(DexFilesTest, get_method_information_64) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 184 | Init(ARCH_ARM64); | 
|  | 185 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 186 | std::string method_name = "nothing"; | 
|  | 187 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 188 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 189 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 190 | WriteDescriptor64(0x100800, 0x200000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 191 | WriteEntry64(0x200000, 0, 0, 0x301000); | 
|  | 192 | WriteDex(0x301000); | 
|  | 193 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 194 | dex_files_->GetMethodInformation(maps_.get(), info, 0x301102, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 195 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 196 | EXPECT_EQ(2U, method_offset); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | TEST_F(DexFilesTest, get_method_information_not_first_entry_32) { | 
|  | 200 | std::string method_name = "nothing"; | 
|  | 201 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 202 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 203 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 204 | WriteDescriptor32(0x100800, 0x200000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 205 | WriteEntry32(0x200000, 0x200100, 0, 0x100000); | 
|  | 206 | WriteEntry32(0x200100, 0, 0x200000, 0x300000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 207 | WriteDex(0x300000); | 
|  | 208 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 209 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300104, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 210 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 211 | EXPECT_EQ(4U, method_offset); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | TEST_F(DexFilesTest, get_method_information_not_first_entry_64) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 215 | Init(ARCH_ARM64); | 
|  | 216 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 217 | std::string method_name = "nothing"; | 
|  | 218 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 219 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 220 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 221 | WriteDescriptor64(0x100800, 0x200000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 222 | WriteEntry64(0x200000, 0x200100, 0, 0x100000); | 
|  | 223 | WriteEntry64(0x200100, 0, 0x200000, 0x300000); | 
|  | 224 | WriteDex(0x300000); | 
|  | 225 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 226 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300106, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 227 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 228 | EXPECT_EQ(6U, method_offset); | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | TEST_F(DexFilesTest, get_method_information_cached) { | 
|  | 232 | std::string method_name = "nothing"; | 
|  | 233 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 234 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 235 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 236 | WriteDescriptor32(0x100800, 0x200000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 237 | WriteEntry32(0x200000, 0, 0, 0x300000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 238 | WriteDex(0x300000); | 
|  | 239 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 240 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 241 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 242 | EXPECT_EQ(0U, method_offset); | 
|  | 243 |  | 
|  | 244 | // Clear all memory and make sure that data is acquired from the cache. | 
|  | 245 | memory_->Clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 246 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 247 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 248 | EXPECT_EQ(0U, method_offset); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | TEST_F(DexFilesTest, get_method_information_search_libs) { | 
|  | 252 | std::string method_name = "nothing"; | 
|  | 253 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 254 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 255 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 256 | WriteDescriptor32(0x100800, 0x200000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 257 | WriteEntry32(0x200000, 0x200100, 0, 0x100000); | 
|  | 258 | WriteEntry32(0x200100, 0, 0x200000, 0x300000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 259 | WriteDex(0x300000); | 
|  | 260 |  | 
|  | 261 | // Only search a given named list of libs. | 
|  | 262 | std::vector<std::string> libs{"libart.so"}; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 263 | dex_files_.reset(new DexFiles(process_memory_, libs)); | 
|  | 264 | dex_files_->SetArch(ARCH_ARM); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 265 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 266 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300104, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 267 | EXPECT_EQ("nothing", method_name); | 
|  | 268 | EXPECT_EQ(0x124U, method_offset); | 
|  | 269 |  | 
|  | 270 | MapInfo* map_info = maps_->Get(kMapGlobal); | 
|  | 271 | map_info->name = "/system/lib/libart.so"; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 272 | dex_files_.reset(new DexFiles(process_memory_, libs)); | 
|  | 273 | dex_files_->SetArch(ARCH_ARM); | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 274 | // Set the rw map to the same name or this will not scan this entry. | 
|  | 275 | map_info = maps_->Get(kMapGlobalRw); | 
|  | 276 | map_info->name = "/system/lib/libart.so"; | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 277 | // Make sure that clearing out copy of the libs doesn't affect the | 
|  | 278 | // DexFiles object. | 
|  | 279 | libs.clear(); | 
|  | 280 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 281 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300104, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 282 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 283 | EXPECT_EQ(4U, method_offset); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | TEST_F(DexFilesTest, get_method_information_global_skip_zero_32) { | 
|  | 287 | std::string method_name = "nothing"; | 
|  | 288 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 289 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 290 |  | 
|  | 291 | // First global variable found, but value is zero. | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 292 | WriteDescriptor32(0xc800, 0); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 293 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 294 | WriteDescriptor32(0x100800, 0x200000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 295 | WriteEntry32(0x200000, 0, 0, 0x300000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 296 | WriteDex(0x300000); | 
|  | 297 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 298 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 299 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 300 | EXPECT_EQ(0U, method_offset); | 
|  | 301 |  | 
|  | 302 | // Verify that second is ignored when first is set to non-zero | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 303 | dex_files_.reset(new DexFiles(process_memory_)); | 
|  | 304 | dex_files_->SetArch(ARCH_ARM); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 305 | method_name = "fail"; | 
|  | 306 | method_offset = 0x123; | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 307 | WriteDescriptor32(0xc800, 0x100000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 308 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 309 | EXPECT_EQ("fail", method_name); | 
|  | 310 | EXPECT_EQ(0x123U, method_offset); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | TEST_F(DexFilesTest, get_method_information_global_skip_zero_64) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 314 | Init(ARCH_ARM64); | 
|  | 315 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 316 | std::string method_name = "nothing"; | 
|  | 317 | uint64_t method_offset = 0x124; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 318 | MapInfo* info = maps_->Get(kMapDexFiles); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 319 |  | 
|  | 320 | // First global variable found, but value is zero. | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 321 | WriteDescriptor64(0xc800, 0); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 322 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 323 | WriteDescriptor64(0x100800, 0x200000); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 324 | WriteEntry64(0x200000, 0, 0, 0x300000); | 
|  | 325 | WriteDex(0x300000); | 
|  | 326 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 327 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 328 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 329 | EXPECT_EQ(0U, method_offset); | 
|  | 330 |  | 
|  | 331 | // Verify that second is ignored when first is set to non-zero | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 332 | dex_files_.reset(new DexFiles(process_memory_)); | 
|  | 333 | dex_files_->SetArch(ARCH_ARM64); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 334 | method_name = "fail"; | 
|  | 335 | method_offset = 0x123; | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 336 | WriteDescriptor64(0xc800, 0x100000); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 337 | dex_files_->GetMethodInformation(maps_.get(), info, 0x300100, &method_name, &method_offset); | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 338 | EXPECT_EQ("fail", method_name); | 
|  | 339 | EXPECT_EQ(0x123U, method_offset); | 
|  | 340 | } | 
|  | 341 |  | 
| Christopher Ferris | de5cd8c | 2020-01-21 17:56:25 -0800 | [diff] [blame] | 342 | TEST_F(DexFilesTest, get_method_information_with_empty_map) { | 
|  | 343 | std::string method_name = "nothing"; | 
|  | 344 | uint64_t method_offset = 0x124; | 
|  | 345 | MapInfo* info = maps_->Get(kMapDexFilesAfterEmpty); | 
|  | 346 |  | 
|  | 347 | WriteDescriptor32(0x503800, 0x506000); | 
|  | 348 | WriteEntry32(0x506000, 0, 0, 0x510000); | 
|  | 349 | WriteDex(0x510000); | 
|  | 350 |  | 
|  | 351 | dex_files_->GetMethodInformation(maps_.get(), info, 0x510100, &method_name, &method_offset); | 
|  | 352 | EXPECT_EQ("Main.<init>", method_name); | 
|  | 353 | EXPECT_EQ(0U, method_offset); | 
|  | 354 | } | 
|  | 355 |  | 
| Christopher Ferris | 7747b60 | 2018-01-31 19:05:19 -0800 | [diff] [blame] | 356 | }  // namespace unwindstack |