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