blob: 459852632dfca20c6183797a28f32f4d9d09fec4 [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:
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(
Christopher Ferris56d0e072018-10-17 10:57:53 -070046 new BufferMaps("1000-4000 ---s 00000000 00:00 0 /fake/elf1\n"
47 "4000-6000 r--s 00000000 00:00 0 /fake/elf1\n"
48 "6000-8000 -wxs 00000000 00:00 0 /fake/elf1\n"
49 "a000-c000 --xp 00000000 00:00 0 /fake/elf2\n"
50 "c000-f000 rw-p 00001000 00:00 0 /fake/elf2\n"
51 "f000-11000 r--p 00000000 00:00 0 /fake/elf3\n"
52 "11000-12000 rw-p 00001000 00:00 0 /fake/elf3\n"
53 "12000-14000 r--p 00000000 00:00 0 /fake/elf4\n"
54 "100000-110000 rw-p 0001000 00:00 0 /fake/elf4\n"
55 "200000-210000 rw-p 0002000 00:00 0 /fake/elf4\n"));
Christopher Ferris150db122017-12-20 18:49:01 -080056 ASSERT_TRUE(maps_->Parse());
57
58 MapInfo* map_info = maps_->Get(3);
59 ASSERT_TRUE(map_info != nullptr);
Christopher Ferris7747b602018-01-31 19:05:19 -080060 MemoryFake* memory = new MemoryFake;
61 ElfFake* elf = new ElfFake(memory);
Christopher Ferris150db122017-12-20 18:49:01 -080062 elf->FakeSetValid(true);
Christopher Ferris7747b602018-01-31 19:05:19 -080063 ElfInterfaceFake* interface = new ElfInterfaceFake(memory);
Christopher Ferris150db122017-12-20 18:49:01 -080064 elf->FakeSetInterface(interface);
65 interface->FakeSetGlobalVariable("__jit_debug_descriptor", 0x800);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080066 map_info->elf.reset(elf);
Christopher Ferris150db122017-12-20 18:49:01 -080067
68 map_info = maps_->Get(5);
69 ASSERT_TRUE(map_info != nullptr);
Christopher Ferris7747b602018-01-31 19:05:19 -080070 memory = new MemoryFake;
71 elf = new ElfFake(memory);
Christopher Ferris150db122017-12-20 18:49:01 -080072 elf->FakeSetValid(true);
Christopher Ferris7747b602018-01-31 19:05:19 -080073 interface = new ElfInterfaceFake(memory);
Christopher Ferris150db122017-12-20 18:49:01 -080074 elf->FakeSetInterface(interface);
75 interface->FakeSetGlobalVariable("__jit_debug_descriptor", 0x800);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080076 map_info->elf.reset(elf);
Christopher Ferrised37aca2018-01-12 15:53:19 -080077
Christopher Ferris56d0e072018-10-17 10:57:53 -070078 map_info = maps_->Get(7);
Christopher Ferrised37aca2018-01-12 15:53:19 -080079 ASSERT_TRUE(map_info != nullptr);
Christopher Ferris7747b602018-01-31 19:05:19 -080080 memory = new MemoryFake;
81 elf = new ElfFake(memory);
Christopher Ferrised37aca2018-01-12 15:53:19 -080082 elf->FakeSetValid(true);
Christopher Ferris7747b602018-01-31 19:05:19 -080083 interface = new ElfInterfaceFake(memory);
Christopher Ferrised37aca2018-01-12 15:53:19 -080084 elf->FakeSetInterface(interface);
85 interface->FakeSetGlobalVariable("__jit_debug_descriptor", 0x800);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080086 map_info->elf.reset(elf);
Christopher Ferris150db122017-12-20 18:49:01 -080087 }
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);
101 ehdr.e_shnum = 3;
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_;
Christopher Ferris150db122017-12-20 18:49:01 -0800175 std::unique_ptr<JitDebug> jit_debug_;
176 std::unique_ptr<BufferMaps> maps_;
177};
178
179void 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
191void 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
203void 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
216void 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
231void 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
244TEST_F(JitDebugTest, get_elf_invalid) {
245 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
246 ASSERT_TRUE(elf == nullptr);
247}
248
249TEST_F(JitDebugTest, get_elf_no_global_variable) {
250 maps_.reset(new BufferMaps(""));
251 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
252 ASSERT_TRUE(elf == nullptr);
253}
254
255TEST_F(JitDebugTest, get_elf_no_valid_descriptor_in_memory) {
256 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
257
258 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
259 ASSERT_TRUE(elf == nullptr);
260}
261
262TEST_F(JitDebugTest, get_elf_no_valid_code_entry) {
263 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
264
265 WriteDescriptor32(0xf800, 0x200000);
266
267 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
268 ASSERT_TRUE(elf == nullptr);
269}
270
271TEST_F(JitDebugTest, get_elf_invalid_descriptor_first_entry) {
272 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
273
274 WriteDescriptor32(0xf800, 0);
275
276 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
277 ASSERT_TRUE(elf == nullptr);
278}
279
280TEST_F(JitDebugTest, get_elf_invalid_descriptor_version) {
281 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
282
283 WriteDescriptor32(0xf800, 0x20000);
284 // Set the version to an invalid value.
285 memory_->SetData32(0xf800, 2);
286
287 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
288 ASSERT_TRUE(elf == nullptr);
289}
290
291TEST_F(JitDebugTest, get_elf_32) {
292 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
293
294 WriteDescriptor32(0xf800, 0x200000);
295 WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000);
296
297 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
298 ASSERT_TRUE(elf != nullptr);
299
300 // Clear the memory and verify all of the data is cached.
301 memory_->Clear();
302 Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500);
303 ASSERT_TRUE(elf2 != nullptr);
304 EXPECT_EQ(elf, elf2);
305}
306
Christopher Ferrised37aca2018-01-12 15:53:19 -0800307TEST_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
311 WriteDescriptor32(0xf800, 0x200000);
312 WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000);
313 WriteDescriptor32(0x12800, 0x201000);
314 WriteEntry32Pad(0x201000, 0, 0, 0x5000, 0x1000);
315
316 ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) != nullptr);
317 ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x2000) == nullptr);
318
319 // Now clear the descriptor entry for the first one.
320 WriteDescriptor32(0xf800, 0);
321 jit_debug_.reset(new JitDebug(process_memory_));
322 jit_debug_->SetArch(ARCH_ARM);
323
324 ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) == nullptr);
325 ASSERT_TRUE(jit_debug_->GetElf(maps_.get(), 0x2000) != nullptr);
326}
327
Christopher Ferris150db122017-12-20 18:49:01 -0800328TEST_F(JitDebugTest, get_elf_x86) {
329 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
330
331 WriteDescriptor32(0xf800, 0x200000);
332 WriteEntry32Pack(0x200000, 0, 0, 0x4000, 0x1000);
333
334 jit_debug_->SetArch(ARCH_X86);
335 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
336 ASSERT_TRUE(elf != nullptr);
337
338 // Clear the memory and verify all of the data is cached.
339 memory_->Clear();
340 Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500);
341 ASSERT_TRUE(elf2 != nullptr);
342 EXPECT_EQ(elf, elf2);
343}
344
345TEST_F(JitDebugTest, get_elf_64) {
346 CreateElf<Elf64_Ehdr, Elf64_Shdr>(0x4000, ELFCLASS64, EM_AARCH64, 0x1500, 0x200);
347
348 WriteDescriptor64(0xf800, 0x200000);
349 WriteEntry64(0x200000, 0, 0, 0x4000, 0x1000);
350
351 jit_debug_->SetArch(ARCH_ARM64);
352 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
353 ASSERT_TRUE(elf != nullptr);
354
355 // Clear the memory and verify all of the data is cached.
356 memory_->Clear();
357 Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500);
358 ASSERT_TRUE(elf2 != nullptr);
359 EXPECT_EQ(elf, elf2);
360}
361
362TEST_F(JitDebugTest, get_elf_multiple_entries) {
363 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
364 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x5000, ELFCLASS32, EM_ARM, 0x2300, 0x400);
365
366 WriteDescriptor32(0xf800, 0x200000);
367 WriteEntry32Pad(0x200000, 0, 0x200100, 0x4000, 0x1000);
368 WriteEntry32Pad(0x200100, 0x200100, 0, 0x5000, 0x1000);
369
370 Elf* elf_2 = jit_debug_->GetElf(maps_.get(), 0x2400);
371 ASSERT_TRUE(elf_2 != nullptr);
372
373 Elf* elf_1 = jit_debug_->GetElf(maps_.get(), 0x1600);
374 ASSERT_TRUE(elf_1 != nullptr);
375
376 // Clear the memory and verify all of the data is cached.
377 memory_->Clear();
378 EXPECT_EQ(elf_1, jit_debug_->GetElf(maps_.get(), 0x1500));
379 EXPECT_EQ(elf_1, jit_debug_->GetElf(maps_.get(), 0x16ff));
380 EXPECT_EQ(elf_2, jit_debug_->GetElf(maps_.get(), 0x2300));
381 EXPECT_EQ(elf_2, jit_debug_->GetElf(maps_.get(), 0x26ff));
382 EXPECT_EQ(nullptr, jit_debug_->GetElf(maps_.get(), 0x1700));
383 EXPECT_EQ(nullptr, jit_debug_->GetElf(maps_.get(), 0x2700));
384}
385
386TEST_F(JitDebugTest, get_elf_search_libs) {
387 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
388
389 WriteDescriptor32(0xf800, 0x200000);
390 WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000);
391
392 // Only search a given named list of libs.
393 std::vector<std::string> libs{"libart.so"};
394 jit_debug_.reset(new JitDebug(process_memory_, libs));
395 jit_debug_->SetArch(ARCH_ARM);
396 EXPECT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) == nullptr);
397
398 // Change the name of the map that includes the value and verify this works.
399 MapInfo* map_info = maps_->Get(5);
400 map_info->name = "/system/lib/libart.so";
Christopher Ferris56d0e072018-10-17 10:57:53 -0700401 map_info = maps_->Get(6);
402 map_info->name = "/system/lib/libart.so";
Christopher Ferris150db122017-12-20 18:49:01 -0800403 jit_debug_.reset(new JitDebug(process_memory_, libs));
404 // Make sure that clearing our copy of the libs doesn't affect the
405 // JitDebug object.
406 libs.clear();
407 jit_debug_->SetArch(ARCH_ARM);
408 EXPECT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) != nullptr);
409}
410
411} // namespace unwindstack