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