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