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:
CreateFakeElf(MapInfo * map_info,uint64_t global_offset,uint64_t data_offset,uint64_t data_vaddr,uint64_t data_size)38 void CreateFakeElf(MapInfo* map_info, uint64_t global_offset, uint64_t data_offset,
39 uint64_t data_vaddr, uint64_t data_size) {
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);
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);
49 map_info->elf.reset(elf);
50 }
51
Init(ArchEnum arch)52 void Init(ArchEnum arch) {
53 jit_debug_.reset(new JitDebug(process_memory_));
54 jit_debug_->SetArch(arch);
55
56 maps_.reset(
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"
59 "6000-8000 -wxs 00002000 00:00 0 /fake/elf1\n"
60 "a000-c000 --xp 00000000 00:00 0 /fake/elf2\n"
61 "c000-f000 rw-p 00002000 00:00 0 /fake/elf2\n"
62 "f000-11000 r--p 00000000 00:00 0 /fake/elf3\n"
63 "11000-12000 rw-p 00002000 00:00 0 /fake/elf3\n"
64 "12000-14000 r--p 00000000 00:00 0 /fake/elf4\n"
65 "100000-110000 rw-p 00ee000 00:00 0 /fake/elf4\n"
66 "200000-210000 rw-p 01ee000 00:00 0 /fake/elf4\n"));
67 ASSERT_TRUE(maps_->Parse());
68
69 MapInfo* map_info = maps_->Get(3);
70 ASSERT_TRUE(map_info != nullptr);
71 CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000);
72
73 map_info = maps_->Get(5);
74 ASSERT_TRUE(map_info != nullptr);
75 CreateFakeElf(map_info, 0x2800, 0x2000, 0x2000, 0x3000);
76
77 map_info = maps_->Get(7);
78 ASSERT_TRUE(map_info != nullptr);
79 CreateFakeElf(map_info, 0xee800, 0xee000, 0xee000, 0x10000);
80 }
81
SetUp()82 void SetUp() override {
83 memory_ = new MemoryFake;
84 process_memory_.reset(memory_);
85
86 Init(ARCH_ARM);
87 }
88
89 template <typename EhdrType, typename ShdrType>
CreateElf(uint64_t offset,uint8_t class_type,uint8_t machine_type,uint32_t pc,uint32_t size)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_;
175 std::unique_ptr<JitDebug> jit_debug_;
176 std::unique_ptr<BufferMaps> maps_;
177 };
178
WriteDescriptor32(uint64_t addr,uint32_t entry)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
WriteDescriptor64(uint64_t addr,uint64_t entry)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
WriteEntry32Pack(uint64_t addr,uint32_t prev,uint32_t next,uint32_t elf_addr,uint64_t elf_size)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
WriteEntry32Pad(uint64_t addr,uint32_t prev,uint32_t next,uint32_t elf_addr,uint64_t elf_size)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
WriteEntry64(uint64_t addr,uint64_t prev,uint64_t next,uint64_t elf_addr,uint64_t elf_size)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
TEST_F(JitDebugTest,get_elf_invalid)244 TEST_F(JitDebugTest, get_elf_invalid) {
245 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
246 ASSERT_TRUE(elf == nullptr);
247 }
248
TEST_F(JitDebugTest,get_elf_no_global_variable)249 TEST_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
TEST_F(JitDebugTest,get_elf_no_valid_descriptor_in_memory)255 TEST_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
TEST_F(JitDebugTest,get_elf_no_valid_code_entry)262 TEST_F(JitDebugTest, get_elf_no_valid_code_entry) {
263 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
264
265 WriteDescriptor32(0x11800, 0x200000);
266
267 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
268 ASSERT_TRUE(elf == nullptr);
269 }
270
TEST_F(JitDebugTest,get_elf_invalid_descriptor_first_entry)271 TEST_F(JitDebugTest, get_elf_invalid_descriptor_first_entry) {
272 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
273
274 WriteDescriptor32(0x11800, 0);
275
276 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
277 ASSERT_TRUE(elf == nullptr);
278 }
279
TEST_F(JitDebugTest,get_elf_invalid_descriptor_version)280 TEST_F(JitDebugTest, get_elf_invalid_descriptor_version) {
281 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
282
283 WriteDescriptor32(0x11800, 0x20000);
284 // Set the version to an invalid value.
285 memory_->SetData32(0x11800, 2);
286
287 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
288 ASSERT_TRUE(elf == nullptr);
289 }
290
TEST_F(JitDebugTest,get_elf_32)291 TEST_F(JitDebugTest, get_elf_32) {
292 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
293
294 WriteDescriptor32(0x11800, 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
TEST_F(JitDebugTest,get_multiple_jit_debug_descriptors_valid)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
311 WriteDescriptor32(0x11800, 0x200000);
312 WriteEntry32Pad(0x200000, 0, 0, 0x4000, 0x1000);
313 WriteDescriptor32(0x100800, 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(0x11800, 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
TEST_F(JitDebugTest,get_elf_x86)328 TEST_F(JitDebugTest, get_elf_x86) {
329 Init(ARCH_X86);
330
331 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
332
333 WriteDescriptor32(0x11800, 0x200000);
334 WriteEntry32Pack(0x200000, 0, 0, 0x4000, 0x1000);
335
336 jit_debug_->SetArch(ARCH_X86);
337 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
338 ASSERT_TRUE(elf != nullptr);
339
340 // Clear the memory and verify all of the data is cached.
341 memory_->Clear();
342 Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500);
343 ASSERT_TRUE(elf2 != nullptr);
344 EXPECT_EQ(elf, elf2);
345 }
346
TEST_F(JitDebugTest,get_elf_64)347 TEST_F(JitDebugTest, get_elf_64) {
348 Init(ARCH_ARM64);
349
350 CreateElf<Elf64_Ehdr, Elf64_Shdr>(0x4000, ELFCLASS64, EM_AARCH64, 0x1500, 0x200);
351
352 WriteDescriptor64(0x11800, 0x200000);
353 WriteEntry64(0x200000, 0, 0, 0x4000, 0x1000);
354
355 Elf* elf = jit_debug_->GetElf(maps_.get(), 0x1500);
356 ASSERT_TRUE(elf != nullptr);
357
358 // Clear the memory and verify all of the data is cached.
359 memory_->Clear();
360 Elf* elf2 = jit_debug_->GetElf(maps_.get(), 0x1500);
361 ASSERT_TRUE(elf2 != nullptr);
362 EXPECT_EQ(elf, elf2);
363 }
364
TEST_F(JitDebugTest,get_elf_multiple_entries)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
369 WriteDescriptor32(0x11800, 0x200000);
370 WriteEntry32Pad(0x200000, 0, 0x200100, 0x4000, 0x1000);
371 WriteEntry32Pad(0x200100, 0x200100, 0, 0x5000, 0x1000);
372
373 Elf* elf_2 = jit_debug_->GetElf(maps_.get(), 0x2400);
374 ASSERT_TRUE(elf_2 != nullptr);
375
376 Elf* elf_1 = jit_debug_->GetElf(maps_.get(), 0x1600);
377 ASSERT_TRUE(elf_1 != nullptr);
378
379 // Clear the memory and verify all of the data is cached.
380 memory_->Clear();
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));
387 }
388
TEST_F(JitDebugTest,get_elf_search_libs)389 TEST_F(JitDebugTest, get_elf_search_libs) {
390 CreateElf<Elf32_Ehdr, Elf32_Shdr>(0x4000, ELFCLASS32, EM_ARM, 0x1500, 0x200);
391
392 WriteDescriptor32(0x11800, 0x200000);
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"};
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);
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";
404 map_info = maps_->Get(6);
405 map_info->name = "/system/lib/libart.so";
406 jit_debug_.reset(new JitDebug(process_memory_, libs));
407 // Make sure that clearing our copy of the libs doesn't affect the
408 // JitDebug object.
409 libs.clear();
410 jit_debug_->SetArch(ARCH_ARM);
411 EXPECT_TRUE(jit_debug_->GetElf(maps_.get(), 0x1500) != nullptr);
412 }
413
414 } // namespace unwindstack
415