| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -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 |  | 
 | 25 | #include <unwindstack/Elf.h> | 
 | 26 | #include <unwindstack/JitDebug.h> | 
 | 27 | #include <unwindstack/MapInfo.h> | 
 | 28 | #include <unwindstack/Maps.h> | 
 | 29 | #include <unwindstack/Memory.h> | 
 | 30 |  | 
 | 31 | #include "ElfFake.h" | 
 | 32 | #include "MemoryFake.h" | 
 | 33 |  | 
 | 34 | namespace unwindstack { | 
 | 35 |  | 
 | 36 | class JitDebugTest : public ::testing::Test { | 
 | 37 |  protected: | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 38 |   void CreateFakeElf(MapInfo* map_info, uint64_t global_offset, uint64_t data_offset, | 
 | 39 |                      uint64_t data_vaddr, uint64_t data_size) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 40 |     MemoryFake* memory = new MemoryFake; | 
 | 41 |     ElfFake* elf = new ElfFake(memory); | 
 | 42 |     elf->FakeSetValid(true); | 
 | 43 |     ElfInterfaceFake* interface = new ElfInterfaceFake(memory); | 
 | 44 |     elf->FakeSetInterface(interface); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 45 |     interface->FakeSetGlobalVariable("__jit_debug_descriptor", global_offset); | 
 | 46 |     interface->FakeSetDataOffset(data_offset); | 
 | 47 |     interface->FakeSetDataVaddrStart(data_vaddr); | 
 | 48 |     interface->FakeSetDataVaddrEnd(data_vaddr + data_size); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 49 |     map_info->elf.reset(elf); | 
 | 50 |   } | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 51 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 52 |   void Init(ArchEnum arch) { | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 53 |     jit_debug_.reset(new JitDebug(process_memory_)); | 
 | 54 |     jit_debug_->SetArch(arch); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 55 |  | 
 | 56 |     maps_.reset( | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 57 |         new BufferMaps("1000-4000 ---s 00000000 00:00 0 /fake/elf1\n" | 
 | 58 |                        "4000-6000 r--s 00000000 00:00 0 /fake/elf1\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 59 |                        "6000-8000 -wxs 00002000 00:00 0 /fake/elf1\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 60 |                        "a000-c000 --xp 00000000 00:00 0 /fake/elf2\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 61 |                        "c000-f000 rw-p 00002000 00:00 0 /fake/elf2\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 62 |                        "f000-11000 r--p 00000000 00:00 0 /fake/elf3\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 63 |                        "11000-12000 rw-p 00002000 00:00 0 /fake/elf3\n" | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 64 |                        "12000-14000 r--p 00000000 00:00 0 /fake/elf4\n" | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 65 |                        "100000-110000 rw-p 00ee000 00:00 0 /fake/elf4\n" | 
 | 66 |                        "200000-210000 rw-p 01ee000 00:00 0 /fake/elf4\n")); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 67 |     ASSERT_TRUE(maps_->Parse()); | 
 | 68 |  | 
 | 69 |     MapInfo* map_info = maps_->Get(3); | 
 | 70 |     ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 71 |     CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 72 |  | 
 | 73 |     map_info = maps_->Get(5); | 
 | 74 |     ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 75 |     CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 76 |  | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 77 |     map_info = maps_->Get(7); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 78 |     ASSERT_TRUE(map_info != nullptr); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 79 |     CreateFakeElf(map_info, 0xee800, 0xee000, 0xee000, 0x10000); | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 80 |   } | 
 | 81 |  | 
 | 82 |   void SetUp() override { | 
 | 83 |     memory_ = new MemoryFake; | 
 | 84 |     process_memory_.reset(memory_); | 
 | 85 |  | 
 | 86 |     Init(ARCH_ARM); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 87 |   } | 
 | 88 |  | 
 | 89 |   template <typename EhdrType, typename ShdrType> | 
 | 90 |   void CreateElf(uint64_t offset, uint8_t class_type, uint8_t machine_type, uint32_t pc, | 
 | 91 |                  uint32_t size) { | 
 | 92 |     EhdrType ehdr; | 
 | 93 |     memset(&ehdr, 0, sizeof(ehdr)); | 
 | 94 |     uint64_t sh_offset = sizeof(ehdr); | 
 | 95 |     memcpy(ehdr.e_ident, ELFMAG, SELFMAG); | 
 | 96 |     ehdr.e_ident[EI_CLASS] = class_type; | 
 | 97 |     ehdr.e_machine = machine_type; | 
 | 98 |     ehdr.e_shstrndx = 1; | 
 | 99 |     ehdr.e_shoff = sh_offset; | 
 | 100 |     ehdr.e_shentsize = sizeof(ShdrType); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 101 |     ehdr.e_shnum = 3; | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 102 |     memory_->SetMemory(offset, &ehdr, sizeof(ehdr)); | 
 | 103 |  | 
 | 104 |     ShdrType shdr; | 
 | 105 |     memset(&shdr, 0, sizeof(shdr)); | 
 | 106 |     shdr.sh_type = SHT_NULL; | 
 | 107 |     memory_->SetMemory(offset + sh_offset, &shdr, sizeof(shdr)); | 
 | 108 |  | 
 | 109 |     sh_offset += sizeof(shdr); | 
 | 110 |     memset(&shdr, 0, sizeof(shdr)); | 
 | 111 |     shdr.sh_type = SHT_STRTAB; | 
 | 112 |     shdr.sh_name = 1; | 
 | 113 |     shdr.sh_offset = 0x500; | 
 | 114 |     shdr.sh_size = 0x100; | 
 | 115 |     memory_->SetMemory(offset + sh_offset, &shdr, sizeof(shdr)); | 
 | 116 |     memory_->SetMemory(offset + 0x500, ".debug_frame"); | 
 | 117 |  | 
 | 118 |     sh_offset += sizeof(shdr); | 
 | 119 |     memset(&shdr, 0, sizeof(shdr)); | 
 | 120 |     shdr.sh_type = SHT_PROGBITS; | 
 | 121 |     shdr.sh_name = 0; | 
 | 122 |     shdr.sh_addr = 0x600; | 
 | 123 |     shdr.sh_offset = 0x600; | 
 | 124 |     shdr.sh_size = 0x200; | 
 | 125 |     memory_->SetMemory(offset + sh_offset, &shdr, sizeof(shdr)); | 
 | 126 |  | 
 | 127 |     // Now add a single cie/fde. | 
 | 128 |     uint64_t dwarf_offset = offset + 0x600; | 
 | 129 |     if (class_type == ELFCLASS32) { | 
 | 130 |       // CIE 32 information. | 
 | 131 |       memory_->SetData32(dwarf_offset, 0xfc); | 
 | 132 |       memory_->SetData32(dwarf_offset + 0x4, 0xffffffff); | 
 | 133 |       memory_->SetData8(dwarf_offset + 0x8, 1); | 
 | 134 |       memory_->SetData8(dwarf_offset + 0x9, '\0'); | 
 | 135 |       memory_->SetData8(dwarf_offset + 0xa, 0x4); | 
 | 136 |       memory_->SetData8(dwarf_offset + 0xb, 0x4); | 
 | 137 |       memory_->SetData8(dwarf_offset + 0xc, 0x1); | 
 | 138 |  | 
 | 139 |       // FDE 32 information. | 
 | 140 |       memory_->SetData32(dwarf_offset + 0x100, 0xfc); | 
 | 141 |       memory_->SetData32(dwarf_offset + 0x104, 0); | 
 | 142 |       memory_->SetData32(dwarf_offset + 0x108, pc); | 
 | 143 |       memory_->SetData32(dwarf_offset + 0x10c, size); | 
 | 144 |     } else { | 
 | 145 |       // CIE 64 information. | 
 | 146 |       memory_->SetData32(dwarf_offset, 0xffffffff); | 
 | 147 |       memory_->SetData64(dwarf_offset + 4, 0xf4); | 
 | 148 |       memory_->SetData64(dwarf_offset + 0xc, 0xffffffffffffffffULL); | 
 | 149 |       memory_->SetData8(dwarf_offset + 0x14, 1); | 
 | 150 |       memory_->SetData8(dwarf_offset + 0x15, '\0'); | 
 | 151 |       memory_->SetData8(dwarf_offset + 0x16, 0x4); | 
 | 152 |       memory_->SetData8(dwarf_offset + 0x17, 0x4); | 
 | 153 |       memory_->SetData8(dwarf_offset + 0x18, 0x1); | 
 | 154 |  | 
 | 155 |       // FDE 64 information. | 
 | 156 |       memory_->SetData32(dwarf_offset + 0x100, 0xffffffff); | 
 | 157 |       memory_->SetData64(dwarf_offset + 0x104, 0xf4); | 
 | 158 |       memory_->SetData64(dwarf_offset + 0x10c, 0); | 
 | 159 |       memory_->SetData64(dwarf_offset + 0x114, pc); | 
 | 160 |       memory_->SetData64(dwarf_offset + 0x11c, size); | 
 | 161 |     } | 
 | 162 |   } | 
 | 163 |  | 
 | 164 |   void WriteDescriptor32(uint64_t addr, uint32_t entry); | 
 | 165 |   void WriteDescriptor64(uint64_t addr, uint64_t entry); | 
 | 166 |   void WriteEntry32Pack(uint64_t addr, uint32_t prev, uint32_t next, uint32_t elf_addr, | 
 | 167 |                         uint64_t elf_size); | 
 | 168 |   void WriteEntry32Pad(uint64_t addr, uint32_t prev, uint32_t next, uint32_t elf_addr, | 
 | 169 |                        uint64_t elf_size); | 
 | 170 |   void WriteEntry64(uint64_t addr, uint64_t prev, uint64_t next, uint64_t elf_addr, | 
 | 171 |                     uint64_t elf_size); | 
 | 172 |  | 
 | 173 |   std::shared_ptr<Memory> process_memory_; | 
 | 174 |   MemoryFake* memory_; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 175 |   std::unique_ptr<JitDebug> jit_debug_; | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 176 |   std::unique_ptr<BufferMaps> maps_; | 
 | 177 | }; | 
 | 178 |  | 
 | 179 | void JitDebugTest::WriteDescriptor32(uint64_t addr, uint32_t entry) { | 
 | 180 |   // Format of the 32 bit JITDescriptor structure: | 
 | 181 |   //   uint32_t version | 
 | 182 |   memory_->SetData32(addr, 1); | 
 | 183 |   //   uint32_t action_flag | 
 | 184 |   memory_->SetData32(addr + 4, 0); | 
 | 185 |   //   uint32_t relevant_entry | 
 | 186 |   memory_->SetData32(addr + 8, 0); | 
 | 187 |   //   uint32_t first_entry | 
 | 188 |   memory_->SetData32(addr + 12, entry); | 
 | 189 | } | 
 | 190 |  | 
 | 191 | void JitDebugTest::WriteDescriptor64(uint64_t addr, uint64_t entry) { | 
 | 192 |   // Format of the 64 bit JITDescriptor structure: | 
 | 193 |   //   uint32_t version | 
 | 194 |   memory_->SetData32(addr, 1); | 
 | 195 |   //   uint32_t action_flag | 
 | 196 |   memory_->SetData32(addr + 4, 0); | 
 | 197 |   //   uint64_t relevant_entry | 
 | 198 |   memory_->SetData64(addr + 8, 0); | 
 | 199 |   //   uint64_t first_entry | 
 | 200 |   memory_->SetData64(addr + 16, entry); | 
 | 201 | } | 
 | 202 |  | 
 | 203 | void JitDebugTest::WriteEntry32Pack(uint64_t addr, uint32_t prev, uint32_t next, uint32_t elf_addr, | 
 | 204 |                                     uint64_t elf_size) { | 
 | 205 |   // Format of the 32 bit JITCodeEntry structure: | 
 | 206 |   //   uint32_t next | 
 | 207 |   memory_->SetData32(addr, next); | 
 | 208 |   //   uint32_t prev | 
 | 209 |   memory_->SetData32(addr + 4, prev); | 
 | 210 |   //   uint32_t symfile_addr | 
 | 211 |   memory_->SetData32(addr + 8, elf_addr); | 
 | 212 |   //   uint64_t symfile_size | 
 | 213 |   memory_->SetData64(addr + 12, elf_size); | 
 | 214 | } | 
 | 215 |  | 
 | 216 | void JitDebugTest::WriteEntry32Pad(uint64_t addr, uint32_t prev, uint32_t next, uint32_t elf_addr, | 
 | 217 |                                    uint64_t elf_size) { | 
 | 218 |   // Format of the 32 bit JITCodeEntry structure: | 
 | 219 |   //   uint32_t next | 
 | 220 |   memory_->SetData32(addr, next); | 
 | 221 |   //   uint32_t prev | 
 | 222 |   memory_->SetData32(addr + 4, prev); | 
 | 223 |   //   uint32_t symfile_addr | 
 | 224 |   memory_->SetData32(addr + 8, elf_addr); | 
 | 225 |   //   uint32_t pad | 
 | 226 |   memory_->SetData32(addr + 12, 0); | 
 | 227 |   //   uint64_t symfile_size | 
 | 228 |   memory_->SetData64(addr + 16, elf_size); | 
 | 229 | } | 
 | 230 |  | 
 | 231 | void JitDebugTest::WriteEntry64(uint64_t addr, uint64_t prev, uint64_t next, uint64_t elf_addr, | 
 | 232 |                                 uint64_t elf_size) { | 
 | 233 |   // Format of the 64 bit JITCodeEntry structure: | 
 | 234 |   //   uint64_t next | 
 | 235 |   memory_->SetData64(addr, next); | 
 | 236 |   //   uint64_t prev | 
 | 237 |   memory_->SetData64(addr + 8, prev); | 
 | 238 |   //   uint64_t symfile_addr | 
 | 239 |   memory_->SetData64(addr + 16, elf_addr); | 
 | 240 |   //   uint64_t symfile_size | 
 | 241 |   memory_->SetData64(addr + 24, elf_size); | 
 | 242 | } | 
 | 243 |  | 
 | 244 | TEST_F(JitDebugTest, get_elf_invalid) { | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 245 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 246 |   ASSERT_TRUE(elf == nullptr); | 
 | 247 | } | 
 | 248 |  | 
 | 249 | TEST_F(JitDebugTest, get_elf_no_global_variable) { | 
 | 250 |   maps_.reset(new BufferMaps("")); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 251 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 252 |   ASSERT_TRUE(elf == nullptr); | 
 | 253 | } | 
 | 254 |  | 
 | 255 | TEST_F(JitDebugTest, get_elf_no_valid_descriptor_in_memory) { | 
 | 256 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 257 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 258 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 259 |   ASSERT_TRUE(elf == nullptr); | 
 | 260 | } | 
 | 261 |  | 
 | 262 | TEST_F(JitDebugTest, get_elf_no_valid_code_entry) { | 
 | 263 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 264 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 265 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 266 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 267 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 268 |   ASSERT_TRUE(elf == nullptr); | 
 | 269 | } | 
 | 270 |  | 
 | 271 | TEST_F(JitDebugTest, get_elf_invalid_descriptor_first_entry) { | 
 | 272 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 273 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 274 |   WriteDescriptor32(0x11800, 0); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 275 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 276 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 277 |   ASSERT_TRUE(elf == nullptr); | 
 | 278 | } | 
 | 279 |  | 
 | 280 | TEST_F(JitDebugTest, get_elf_invalid_descriptor_version) { | 
 | 281 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 282 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 283 |   WriteDescriptor32(0x11800, 0x20000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 284 |   // Set the version to an invalid value. | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 285 |   memory_->SetData32(0x11800, 2); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 286 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 287 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 288 |   ASSERT_TRUE(elf == nullptr); | 
 | 289 | } | 
 | 290 |  | 
 | 291 | TEST_F(JitDebugTest, get_elf_32) { | 
 | 292 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 293 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 294 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 295 |   WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000); | 
 | 296 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 297 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 298 |   ASSERT_TRUE(elf != nullptr); | 
 | 299 |  | 
 | 300 |   // Clear the memory and verify all of the data is cached. | 
 | 301 |   memory_->Clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 302 |   Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 303 |   ASSERT_TRUE(elf2 != nullptr); | 
 | 304 |   EXPECT_EQ(elf, elf2); | 
 | 305 | } | 
 | 306 |  | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 307 | TEST_F(JitDebugTest, get_multiple_jit_debug_descriptors_valid) { | 
 | 308 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 309 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x5000, ELFCLASS32, EM_ARM, 0x2000, 0x300); | 
 | 310 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 311 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 312 |   WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000); | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 313 |   WriteDescriptor32(0x100800, 0x201000); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 314 |   WriteEntry32Pad(0x201000, 0, 0, 0x5000, 0x1000); | 
 | 315 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 316 |   ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) != nullptr); | 
 | 317 |   ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x2000) == nullptr); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 318 |  | 
 | 319 |   // Now clear the descriptor entry for the first one. | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 320 |   WriteDescriptor32(0x11800, 0); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 321 |   jit_debug_.reset(new JitDebug(process_memory_)); | 
 | 322 |   jit_debug_->SetArch(ARCH_ARM); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 323 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 324 |   ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) == nullptr); | 
 | 325 |   ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x2000) != nullptr); | 
| Christopher Ferris | ed37aca | 2018-01-12 15:53:19 -0800 | [diff] [blame] | 326 | } | 
 | 327 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 328 | TEST_F(JitDebugTest, get_elf_x86) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 329 |   Init(ARCH_X86); | 
 | 330 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 331 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 332 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 333 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 334 |   WriteEntry32Pack(0x200000, 0, 0, 0x4000, 0x1000); | 
 | 335 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 336 |   jit_debug_->SetArch(ARCH_X86); | 
 | 337 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 338 |   ASSERT_TRUE(elf != nullptr); | 
 | 339 |  | 
 | 340 |   // Clear the memory and verify all of the data is cached. | 
 | 341 |   memory_->Clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 342 |   Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 343 |   ASSERT_TRUE(elf2 != nullptr); | 
 | 344 |   EXPECT_EQ(elf, elf2); | 
 | 345 | } | 
 | 346 |  | 
 | 347 | TEST_F(JitDebugTest, get_elf_64) { | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 348 |   Init(ARCH_ARM64); | 
 | 349 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 350 |   CreateElf<Elf64_Ehdr, Elf64_Shdr>(0x4000, ELFCLASS64, EM_AARCH64, 0x1500, 0x200); | 
 | 351 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 352 |   WriteDescriptor64(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 353 |   WriteEntry64(0x200000, 0, 0, 0x4000, 0x1000); | 
 | 354 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 355 |   Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 356 |   ASSERT_TRUE(elf != nullptr); | 
 | 357 |  | 
 | 358 |   // Clear the memory and verify all of the data is cached. | 
 | 359 |   memory_->Clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 360 |   Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 361 |   ASSERT_TRUE(elf2 != nullptr); | 
 | 362 |   EXPECT_EQ(elf, elf2); | 
 | 363 | } | 
 | 364 |  | 
 | 365 | TEST_F(JitDebugTest, get_elf_multiple_entries) { | 
 | 366 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 367 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x5000, ELFCLASS32, EM_ARM, 0x2300, 0x400); | 
 | 368 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 369 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 370 |   WriteEntry32Pad(0x200000, 0, 0x200100, 0x4000, 0x1000); | 
 | 371 |   WriteEntry32Pad(0x200100, 0x200100, 0, 0x5000, 0x1000); | 
 | 372 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 373 |   Elf* elf_2 = jit_debug_->GetElf(maps_.get(), 0x2400); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 374 |   ASSERT_TRUE(elf_2 != nullptr); | 
 | 375 |  | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 376 |   Elf* elf_1 = jit_debug_->GetElf(maps_.get(), 0x1600); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 377 |   ASSERT_TRUE(elf_1 != nullptr); | 
 | 378 |  | 
 | 379 |   // Clear the memory and verify all of the data is cached. | 
 | 380 |   memory_->Clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 381 |   EXPECT_EQ(elf_1, jit_debug_->GetElf(maps_.get(), 0x1500)); | 
 | 382 |   EXPECT_EQ(elf_1, jit_debug_->GetElf(maps_.get(), 0x16ff)); | 
 | 383 |   EXPECT_EQ(elf_2, jit_debug_->GetElf(maps_.get(), 0x2300)); | 
 | 384 |   EXPECT_EQ(elf_2, jit_debug_->GetElf(maps_.get(), 0x26ff)); | 
 | 385 |   EXPECT_EQ(nullptr, jit_debug_->GetElf(maps_.get(), 0x1700)); | 
 | 386 |   EXPECT_EQ(nullptr, jit_debug_->GetElf(maps_.get(), 0x2700)); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 387 | } | 
 | 388 |  | 
 | 389 | TEST_F(JitDebugTest, get_elf_search_libs) { | 
 | 390 |   CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200); | 
 | 391 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 392 |   WriteDescriptor32(0x11800, 0x200000); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 393 |   WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000); | 
 | 394 |  | 
 | 395 |   // Only search a given named list of libs. | 
 | 396 |   std::vector<std::string> libs{"libart.so"}; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 397 |   jit_debug_.reset(new JitDebug(process_memory_, libs)); | 
 | 398 |   jit_debug_->SetArch(ARCH_ARM); | 
 | 399 |   EXPECT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) == nullptr); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 400 |  | 
 | 401 |   // Change the name of the map that includes the value and verify this works. | 
 | 402 |   MapInfo* map_info = maps_->Get(5); | 
 | 403 |   map_info->name = "/system/lib/libart.so"; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 404 |   map_info = maps_->Get(6); | 
 | 405 |   map_info->name = "/system/lib/libart.so"; | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 406 |   jit_debug_.reset(new JitDebug(process_memory_, libs)); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 407 |   // Make sure that clearing our copy of the libs doesn't affect the | 
 | 408 |   // JitDebug object. | 
 | 409 |   libs.clear(); | 
| David Srbecky | b9cc4fb | 2019-04-05 18:23:32 +0000 | [diff] [blame] | 410 |   jit_debug_->SetArch(ARCH_ARM); | 
 | 411 |   EXPECT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) != nullptr); | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 412 | } | 
 | 413 |  | 
 | 414 | }  // namespace unwindstack |