1 /*
2  * Copyright (C) 2017 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 <stdint.h>
19 #include <sys/mman.h>
20 
21 #include <memory>
22 #include <set>
23 #include <string>
24 
25 #include <gtest/gtest.h>
26 
27 #include <android-base/silent_death_test.h>
28 #include <unwindstack/Elf.h>
29 #include <unwindstack/Maps.h>
30 #include <unwindstack/Memory.h>
31 #include <unwindstack/Regs.h>
32 #include <unwindstack/RegsArm.h>
33 #include <unwindstack/RegsArm64.h>
34 #include <unwindstack/RegsMips.h>
35 #include <unwindstack/RegsMips64.h>
36 #include <unwindstack/RegsX86.h>
37 #include <unwindstack/RegsX86_64.h>
38 #include <unwindstack/Unwinder.h>
39 
40 #include "ElfFake.h"
41 #include "ElfTestUtils.h"
42 #include "MemoryFake.h"
43 #include "RegsFake.h"
44 
45 namespace unwindstack {
46 
47 class UnwinderTest : public ::testing::Test {
48  protected:
AddMapInfo(uint64_t start,uint64_t end,uint64_t offset,uint64_t flags,const char * name,Elf * elf=nullptr)49   static MapInfo* AddMapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
50                              const char* name, Elf* elf = nullptr) {
51     std::string str_name(name);
52     maps_->Add(start, end, offset, flags, name, static_cast<uint64_t>(-1));
53     MapInfo* map_info = maps_->Find(start);
54     if (elf != nullptr) {
55       map_info->set_elf(elf);
56     }
57     return map_info;
58   }
59 
SetUpTestSuite()60   static void SetUpTestSuite() {
61     maps_.reset(new Maps);
62 
63     memory_ = new MemoryFake;
64     process_memory_.reset(memory_);
65 
66     ElfFake* elf;
67     ElfInterfaceFake* interface;
68     MapInfo* map_info;
69 
70     elf = new ElfFake(new MemoryFake);
71     interface = new ElfInterfaceFake(nullptr);
72     interface->FakeSetBuildID("FAKE");
73     elf->FakeSetInterface(interface);
74     AddMapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so", elf);
75 
76     AddMapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]");
77 
78     AddMapInfo(0x13000, 0x15000, 0, PROT_READ | PROT_WRITE | MAPS_FLAGS_DEVICE_MAP,
79                "/dev/fake_device");
80 
81     elf = new ElfFake(new MemoryFake);
82     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
83     AddMapInfo(0x20000, 0x22000, 0, PROT_READ | PROT_WRITE, "/system/fake/libunwind.so", elf);
84 
85     elf = new ElfFake(new MemoryFake);
86     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
87     AddMapInfo(0x23000, 0x24000, 0, PROT_READ | PROT_WRITE, "/fake/libanother.so", elf);
88 
89     elf = new ElfFake(new MemoryFake);
90     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
91     AddMapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so", elf);
92 
93     elf = new ElfFake(new MemoryFake);
94     interface = new ElfInterfaceFake(nullptr);
95     interface->FakeSetSoname("lib_fake.so");
96     elf->FakeSetInterface(interface);
97     map_info = AddMapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk", elf);
98     map_info->set_elf_start_offset(0x1d000);
99 
100     AddMapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat");
101 
102     map_info =
103         AddMapInfo(0xa3000, 0xa4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.vdex");
104     map_info->set_load_bias(0);
105 
106     elf = new ElfFake(new MemoryFake);
107     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
108     elf->FakeSetLoadBias(0x5000);
109     AddMapInfo(0xa5000, 0xa6000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake_load_bias.so",
110                elf);
111 
112     elf = new ElfFake(new MemoryFake);
113     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
114     map_info = AddMapInfo(0xa7000, 0xa8000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
115                           "/fake/fake_offset.oat", elf);
116     map_info->set_elf_offset(0x8000);
117 
118     elf = new ElfFake(new MemoryFake);
119     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
120     map_info = AddMapInfo(0xc0000, 0xc1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
121                           "/fake/unreadable.so", elf);
122     map_info->set_memory_backed_elf(true);
123 
124     elf = new ElfFake(new MemoryFake);
125     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
126     map_info = AddMapInfo(0xc1000, 0xc2000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "[vdso]", elf);
127     map_info->set_memory_backed_elf(true);
128 
129     elf = new ElfFake(new MemoryFake);
130     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
131     map_info = AddMapInfo(0xc2000, 0xc3000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "", elf);
132     map_info->set_memory_backed_elf(true);
133 
134     elf = new ElfFake(new MemoryFake);
135     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
136     map_info = AddMapInfo(0xc3000, 0xc4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
137                           "/memfd:/jit-cache", elf);
138     map_info->set_memory_backed_elf(true);
139 
140     map_info =
141         AddMapInfo(0xd0000, 0xd1000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.apk");
142     map_info->set_load_bias(0);
143 
144     elf = new ElfFake(new MemoryFake);
145     interface = new ElfInterfaceFake(nullptr);
146     elf->FakeSetInterface(interface);
147     interface->FakeSetGlobalVariable("__dex_debug_descriptor", 0x1800);
148     interface->FakeSetGlobalVariable("__jit_debug_descriptor", 0x1900);
149     interface->FakeSetDataOffset(0x1000);
150     interface->FakeSetDataVaddrStart(0x1000);
151     interface->FakeSetDataVaddrEnd(0x8000);
152     AddMapInfo(0xf0000, 0xf1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/global.so", elf);
153     AddMapInfo(0xf1000, 0xf9000, 0x1000, PROT_READ | PROT_WRITE, "/fake/global.so");
154     // dex debug data
155     memory_->SetData32(0xf180c, 0xf3000);
156     memory_->SetData32(0xf3000, 0xf4000);
157     memory_->SetData32(0xf3004, 0xf4000);
158     memory_->SetData32(0xf3008, 0xf5000);
159     // jit debug data
160     memory_->SetData32(0xf1900, 1);
161     memory_->SetData32(0xf1904, 0);
162     memory_->SetData32(0xf1908, 0xf6000);
163     memory_->SetData32(0xf190c, 0xf6000);
164     memory_->SetData32(0xf6000, 0);
165     memory_->SetData32(0xf6004, 0);
166     memory_->SetData32(0xf6008, 0xf7000);
167     memory_->SetData32(0xf600c, 0);
168     memory_->SetData64(0xf6010, 0x1000);
169 
170     elf = new ElfFake(new MemoryFake);
171     elf->FakeSetValid(false);
172     elf->FakeSetLoadBias(0x300);
173     map_info = AddMapInfo(0x100000, 0x101000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC,
174                           "/fake/jit.so", elf);
175     map_info->set_elf_start_offset(0x100);
176     map_info->set_offset(0x200);
177   }
178 
SetUp()179   void SetUp() override {
180     ElfInterfaceFake::FakeClear();
181     regs_.FakeSetArch(ARCH_ARM);
182     regs_.FakeSetReturnAddressValid(false);
183   }
184 
185   static std::unique_ptr<Maps> maps_;
186   static RegsFake regs_;
187   static MemoryFake* memory_;
188   static std::shared_ptr<Memory> process_memory_;
189 };
190 
191 std::unique_ptr<Maps> UnwinderTest::maps_;
192 RegsFake UnwinderTest::regs_(5);
193 MemoryFake* UnwinderTest::memory_;
194 std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
195 
TEST_F(UnwinderTest,multiple_frames)196 TEST_F(UnwinderTest, multiple_frames) {
197   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
198   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
199   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
200 
201   regs_.set_pc(0x1000);
202   regs_.set_sp(0x10000);
203   ElfInterfaceFake::FakePushStepData(StepData(0x1104, 0x10010, false));
204   ElfInterfaceFake::FakePushStepData(StepData(0x1204, 0x10020, false));
205   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
206 
207   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
208   unwinder.Unwind();
209   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
210   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
211   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
212 
213   ASSERT_EQ(3U, unwinder.NumFrames());
214 
215   auto* frame = &unwinder.frames()[0];
216   EXPECT_EQ(0U, frame->num);
217   EXPECT_EQ(0U, frame->rel_pc);
218   EXPECT_EQ(0x1000U, frame->pc);
219   EXPECT_EQ(0x10000U, frame->sp);
220   EXPECT_EQ("Frame0", frame->function_name);
221   EXPECT_EQ(0U, frame->function_offset);
222   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
223   EXPECT_EQ(0U, frame->map_elf_start_offset);
224   EXPECT_EQ(0U, frame->map_exact_offset);
225   EXPECT_EQ(0x1000U, frame->map_start);
226   EXPECT_EQ(0x8000U, frame->map_end);
227   EXPECT_EQ(0U, frame->map_load_bias);
228   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
229 
230   frame = &unwinder.frames()[1];
231   EXPECT_EQ(1U, frame->num);
232   EXPECT_EQ(0x100U, frame->rel_pc);
233   EXPECT_EQ(0x1100U, frame->pc);
234   EXPECT_EQ(0x10010U, frame->sp);
235   EXPECT_EQ("Frame1", frame->function_name);
236   EXPECT_EQ(1U, frame->function_offset);
237   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
238   EXPECT_EQ(0U, frame->map_elf_start_offset);
239   EXPECT_EQ(0U, frame->map_exact_offset);
240   EXPECT_EQ(0x1000U, frame->map_start);
241   EXPECT_EQ(0x8000U, frame->map_end);
242   EXPECT_EQ(0U, frame->map_load_bias);
243   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
244 
245   frame = &unwinder.frames()[2];
246   EXPECT_EQ(2U, frame->num);
247   EXPECT_EQ(0x200U, frame->rel_pc);
248   EXPECT_EQ(0x1200U, frame->pc);
249   EXPECT_EQ(0x10020U, frame->sp);
250   EXPECT_EQ("Frame2", frame->function_name);
251   EXPECT_EQ(2U, frame->function_offset);
252   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
253   EXPECT_EQ(0U, frame->map_elf_start_offset);
254   EXPECT_EQ(0U, frame->map_exact_offset);
255   EXPECT_EQ(0x1000U, frame->map_start);
256   EXPECT_EQ(0x8000U, frame->map_end);
257   EXPECT_EQ(0U, frame->map_load_bias);
258   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
259 }
260 
TEST_F(UnwinderTest,multiple_frames_dont_resolve_names)261 TEST_F(UnwinderTest, multiple_frames_dont_resolve_names) {
262   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
263   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
264   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
265 
266   regs_.set_pc(0x1000);
267   regs_.set_sp(0x10000);
268   ElfInterfaceFake::FakePushStepData(StepData(0x1104, 0x10010, false));
269   ElfInterfaceFake::FakePushStepData(StepData(0x1204, 0x10020, false));
270   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
271 
272   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
273   unwinder.SetResolveNames(false);
274   unwinder.Unwind();
275   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
276   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
277   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
278 
279   ASSERT_EQ(3U, unwinder.NumFrames());
280 
281   auto* frame = &unwinder.frames()[0];
282   EXPECT_EQ(0U, frame->num);
283   EXPECT_EQ(0U, frame->rel_pc);
284   EXPECT_EQ(0x1000U, frame->pc);
285   EXPECT_EQ(0x10000U, frame->sp);
286   EXPECT_EQ("", frame->function_name);
287   EXPECT_EQ(0U, frame->function_offset);
288   EXPECT_EQ("", frame->map_name);
289   EXPECT_EQ(0U, frame->map_elf_start_offset);
290   EXPECT_EQ(0U, frame->map_exact_offset);
291   EXPECT_EQ(0x1000U, frame->map_start);
292   EXPECT_EQ(0x8000U, frame->map_end);
293   EXPECT_EQ(0U, frame->map_load_bias);
294   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
295 
296   frame = &unwinder.frames()[1];
297   EXPECT_EQ(1U, frame->num);
298   EXPECT_EQ(0x100U, frame->rel_pc);
299   EXPECT_EQ(0x1100U, frame->pc);
300   EXPECT_EQ(0x10010U, frame->sp);
301   EXPECT_EQ("", frame->function_name);
302   EXPECT_EQ(0U, frame->function_offset);
303   EXPECT_EQ("", frame->map_name);
304   EXPECT_EQ(0U, frame->map_elf_start_offset);
305   EXPECT_EQ(0U, frame->map_exact_offset);
306   EXPECT_EQ(0x1000U, frame->map_start);
307   EXPECT_EQ(0x8000U, frame->map_end);
308   EXPECT_EQ(0U, frame->map_load_bias);
309   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
310 
311   frame = &unwinder.frames()[2];
312   EXPECT_EQ(2U, frame->num);
313   EXPECT_EQ(0x200U, frame->rel_pc);
314   EXPECT_EQ(0x1200U, frame->pc);
315   EXPECT_EQ(0x10020U, frame->sp);
316   EXPECT_EQ("", frame->function_name);
317   EXPECT_EQ(0U, frame->function_offset);
318   EXPECT_EQ("", frame->map_name);
319   EXPECT_EQ(0U, frame->map_elf_start_offset);
320   EXPECT_EQ(0U, frame->map_exact_offset);
321   EXPECT_EQ(0x1000U, frame->map_start);
322   EXPECT_EQ(0x8000U, frame->map_end);
323   EXPECT_EQ(0U, frame->map_load_bias);
324   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
325 }
326 
TEST_F(UnwinderTest,non_zero_load_bias)327 TEST_F(UnwinderTest, non_zero_load_bias) {
328   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
329 
330   regs_.set_pc(0xa5500);
331   regs_.set_sp(0x10000);
332   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
333 
334   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
335   unwinder.Unwind();
336   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
337   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
338   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
339 
340   ASSERT_EQ(1U, unwinder.NumFrames());
341 
342   auto* frame = &unwinder.frames()[0];
343   EXPECT_EQ(0U, frame->num);
344   EXPECT_EQ(0x5500U, frame->rel_pc);
345   EXPECT_EQ(0xa5500U, frame->pc);
346   EXPECT_EQ(0x10000U, frame->sp);
347   EXPECT_EQ("Frame0", frame->function_name);
348   EXPECT_EQ(0U, frame->function_offset);
349   EXPECT_EQ("/fake/fake_load_bias.so", frame->map_name);
350   EXPECT_EQ(0U, frame->map_elf_start_offset);
351   EXPECT_EQ(0U, frame->map_exact_offset);
352   EXPECT_EQ(0xa5000U, frame->map_start);
353   EXPECT_EQ(0xa6000U, frame->map_end);
354   EXPECT_EQ(0x5000U, frame->map_load_bias);
355   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
356 }
357 
TEST_F(UnwinderTest,non_zero_elf_offset)358 TEST_F(UnwinderTest, non_zero_elf_offset) {
359   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
360 
361   regs_.set_pc(0xa7500);
362   regs_.set_sp(0x10000);
363   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
364 
365   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
366   unwinder.Unwind();
367   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
368   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
369   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
370 
371   ASSERT_EQ(1U, unwinder.NumFrames());
372 
373   auto* frame = &unwinder.frames()[0];
374   EXPECT_EQ(0U, frame->num);
375   EXPECT_EQ(0x8500U, frame->rel_pc);
376   EXPECT_EQ(0xa7500U, frame->pc);
377   EXPECT_EQ(0x10000U, frame->sp);
378   EXPECT_EQ("Frame0", frame->function_name);
379   EXPECT_EQ(0U, frame->function_offset);
380   EXPECT_EQ("/fake/fake_offset.oat", frame->map_name);
381   EXPECT_EQ(0U, frame->map_elf_start_offset);
382   EXPECT_EQ(0U, frame->map_exact_offset);
383   EXPECT_EQ(0xa7000U, frame->map_start);
384   EXPECT_EQ(0xa8000U, frame->map_end);
385   EXPECT_EQ(0U, frame->map_load_bias);
386   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
387 }
388 
TEST_F(UnwinderTest,non_zero_map_offset)389 TEST_F(UnwinderTest, non_zero_map_offset) {
390   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
391 
392   regs_.set_pc(0x43000);
393   regs_.set_sp(0x10000);
394   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
395 
396   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
397   unwinder.Unwind();
398   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
399   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
400   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
401 
402   ASSERT_EQ(1U, unwinder.NumFrames());
403 
404   auto* frame = &unwinder.frames()[0];
405   EXPECT_EQ(0U, frame->num);
406   EXPECT_EQ(0U, frame->rel_pc);
407   EXPECT_EQ(0x43000U, frame->pc);
408   EXPECT_EQ(0x10000U, frame->sp);
409   EXPECT_EQ("Frame0", frame->function_name);
410   EXPECT_EQ(0U, frame->function_offset);
411   EXPECT_EQ("/fake/fake.apk!lib_fake.so", frame->map_name);
412   EXPECT_EQ(0x1d000U, frame->map_elf_start_offset);
413   EXPECT_EQ(0x1d000U, frame->map_exact_offset);
414   EXPECT_EQ(0x43000U, frame->map_start);
415   EXPECT_EQ(0x44000U, frame->map_end);
416   EXPECT_EQ(0U, frame->map_load_bias);
417   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
418 }
419 
TEST_F(UnwinderTest,disable_embedded_soname)420 TEST_F(UnwinderTest, disable_embedded_soname) {
421   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
422 
423   regs_.set_pc(0x43000);
424   regs_.set_sp(0x10000);
425   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
426 
427   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
428   unwinder.SetEmbeddedSoname(false);
429   unwinder.Unwind();
430   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
431   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
432   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
433 
434   ASSERT_EQ(1U, unwinder.NumFrames());
435 
436   auto* frame = &unwinder.frames()[0];
437   EXPECT_EQ(0U, frame->num);
438   EXPECT_EQ(0U, frame->rel_pc);
439   EXPECT_EQ(0x43000U, frame->pc);
440   EXPECT_EQ(0x10000U, frame->sp);
441   EXPECT_EQ("Frame0", frame->function_name);
442   EXPECT_EQ(0U, frame->function_offset);
443   EXPECT_EQ("/fake/fake.apk", frame->map_name);
444   EXPECT_EQ(0x1d000U, frame->map_elf_start_offset);
445   EXPECT_EQ(0x1d000U, frame->map_exact_offset);
446   EXPECT_EQ(0x43000U, frame->map_start);
447   EXPECT_EQ(0x44000U, frame->map_end);
448   EXPECT_EQ(0U, frame->map_load_bias);
449   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
450 }
451 
452 // Verify that no attempt to continue after the step indicates it is done.
TEST_F(UnwinderTest,no_frames_after_finished)453 TEST_F(UnwinderTest, no_frames_after_finished) {
454   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
455   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
456   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
457   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
458   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
459 
460   regs_.set_pc(0x1000);
461   regs_.set_sp(0x10000);
462   ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
463   ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
464   ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
465 
466   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
467   unwinder.Unwind();
468   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
469   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
470   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
471 
472   ASSERT_EQ(1U, unwinder.NumFrames());
473 
474   auto* frame = &unwinder.frames()[0];
475   EXPECT_EQ(0U, frame->num);
476   EXPECT_EQ(0U, frame->rel_pc);
477   EXPECT_EQ(0x1000U, frame->pc);
478   EXPECT_EQ(0x10000U, frame->sp);
479   EXPECT_EQ("Frame0", frame->function_name);
480   EXPECT_EQ(0U, frame->function_offset);
481   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
482   EXPECT_EQ(0U, frame->map_elf_start_offset);
483   EXPECT_EQ(0U, frame->map_exact_offset);
484   EXPECT_EQ(0x1000U, frame->map_start);
485   EXPECT_EQ(0x8000U, frame->map_end);
486   EXPECT_EQ(0U, frame->map_load_bias);
487   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
488 }
489 
490 // Verify the maximum frames to save.
TEST_F(UnwinderTest,max_frames)491 TEST_F(UnwinderTest, max_frames) {
492   for (size_t i = 0; i < 30; i++) {
493     ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
494     ElfInterfaceFake::FakePushStepData(StepData(0x1104 + i * 0x100, 0x10010 + i * 0x10, false));
495   }
496 
497   regs_.set_pc(0x1000);
498   regs_.set_sp(0x10000);
499 
500   Unwinder unwinder(20, maps_.get(), &regs_, process_memory_);
501   unwinder.Unwind();
502   EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode());
503   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
504   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
505 
506   ASSERT_EQ(20U, unwinder.NumFrames());
507 
508   for (size_t i = 0; i < 20; i++) {
509     auto* frame = &unwinder.frames()[i];
510     EXPECT_EQ(i, frame->num);
511     EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
512     EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
513     EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
514     EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
515     EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
516     EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
517     EXPECT_EQ(0U, frame->map_elf_start_offset) << "Failed at frame " << i;
518     EXPECT_EQ(0U, frame->map_exact_offset) << "Failed at frame " << i;
519     EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
520     EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
521     EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
522     EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
523   }
524 }
525 
526 // Verify that initial map names frames are removed.
TEST_F(UnwinderTest,verify_frames_skipped)527 TEST_F(UnwinderTest, verify_frames_skipped) {
528   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
529   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
530   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
531 
532   regs_.set_pc(0x20000);
533   regs_.set_sp(0x10000);
534   ElfInterfaceFake::FakePushStepData(StepData(0x23004, 0x10010, false));
535   ElfInterfaceFake::FakePushStepData(StepData(0x23104, 0x10020, false));
536   ElfInterfaceFake::FakePushStepData(StepData(0x20004, 0x10030, false));
537   ElfInterfaceFake::FakePushStepData(StepData(0x21004, 0x10040, false));
538   ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
539   ElfInterfaceFake::FakePushStepData(StepData(0x21004, 0x10060, false));
540   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
541   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
542 
543   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
544   std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
545   unwinder.Unwind(&skip_libs);
546   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
547   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
548   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
549 
550   ASSERT_EQ(3U, unwinder.NumFrames());
551 
552   auto* frame = &unwinder.frames()[0];
553   EXPECT_EQ(0U, frame->num);
554   EXPECT_EQ(0U, frame->rel_pc);
555   EXPECT_EQ(0x1000U, frame->pc);
556   EXPECT_EQ(0x10050U, frame->sp);
557   EXPECT_EQ("Frame0", frame->function_name);
558   EXPECT_EQ(0U, frame->function_offset);
559   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
560   EXPECT_EQ(0U, frame->map_elf_start_offset);
561   EXPECT_EQ(0U, frame->map_exact_offset);
562   EXPECT_EQ(0x1000U, frame->map_start);
563   EXPECT_EQ(0x8000U, frame->map_end);
564   EXPECT_EQ(0U, frame->map_load_bias);
565   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
566 
567   frame = &unwinder.frames()[1];
568   EXPECT_EQ(1U, frame->num);
569   EXPECT_EQ(0x1000U, frame->rel_pc);
570   EXPECT_EQ(0x21000U, frame->pc);
571   EXPECT_EQ(0x10060U, frame->sp);
572   EXPECT_EQ("Frame1", frame->function_name);
573   EXPECT_EQ(1U, frame->function_offset);
574   EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
575   EXPECT_EQ(0U, frame->map_elf_start_offset);
576   EXPECT_EQ(0U, frame->map_exact_offset);
577   EXPECT_EQ(0x20000U, frame->map_start);
578   EXPECT_EQ(0x22000U, frame->map_end);
579   EXPECT_EQ(0U, frame->map_load_bias);
580   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
581 
582   frame = &unwinder.frames()[2];
583   EXPECT_EQ(2U, frame->num);
584   EXPECT_EQ(0U, frame->rel_pc);
585   EXPECT_EQ(0x23000U, frame->pc);
586   EXPECT_EQ(0x10070U, frame->sp);
587   EXPECT_EQ("Frame2", frame->function_name);
588   EXPECT_EQ(2U, frame->function_offset);
589   EXPECT_EQ("/fake/libanother.so", frame->map_name);
590   EXPECT_EQ(0U, frame->map_exact_offset);
591   EXPECT_EQ(0x23000U, frame->map_start);
592   EXPECT_EQ(0x24000U, frame->map_end);
593   EXPECT_EQ(0U, frame->map_load_bias);
594   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
595 }
596 
597 // Verify SP in a non-existant map is okay.
TEST_F(UnwinderTest,sp_not_in_map)598 TEST_F(UnwinderTest, sp_not_in_map) {
599   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
600   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
601 
602   regs_.set_pc(0x1000);
603   regs_.set_sp(0x63000);
604   ElfInterfaceFake::FakePushStepData(StepData(0x21004, 0x50020, false));
605   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
606 
607   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
608   unwinder.Unwind();
609   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
610   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
611   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
612 
613   ASSERT_EQ(2U, unwinder.NumFrames());
614 
615   auto* frame = &unwinder.frames()[0];
616   EXPECT_EQ(0U, frame->num);
617   EXPECT_EQ(0U, frame->rel_pc);
618   EXPECT_EQ(0x1000U, frame->pc);
619   EXPECT_EQ(0x63000U, frame->sp);
620   EXPECT_EQ("Frame0", frame->function_name);
621   EXPECT_EQ(0U, frame->function_offset);
622   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
623   EXPECT_EQ(0U, frame->map_exact_offset);
624   EXPECT_EQ(0x1000U, frame->map_start);
625   EXPECT_EQ(0x8000U, frame->map_end);
626   EXPECT_EQ(0U, frame->map_load_bias);
627   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
628 
629   frame = &unwinder.frames()[1];
630   EXPECT_EQ(1U, frame->num);
631   EXPECT_EQ(0x1000U, frame->rel_pc);
632   EXPECT_EQ(0x21000U, frame->pc);
633   EXPECT_EQ(0x50020U, frame->sp);
634   EXPECT_EQ("Frame1", frame->function_name);
635   EXPECT_EQ(1U, frame->function_offset);
636   EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
637   EXPECT_EQ(0U, frame->map_exact_offset);
638   EXPECT_EQ(0x20000U, frame->map_start);
639   EXPECT_EQ(0x22000U, frame->map_end);
640   EXPECT_EQ(0U, frame->map_load_bias);
641   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
642 }
643 
644 // Verify PC in a device stops the unwind.
TEST_F(UnwinderTest,pc_in_device_stops_unwind)645 TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
646   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
647   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
648   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
649 
650   regs_.set_pc(0x13000);
651   regs_.set_sp(0x10000);
652   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
653   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
654   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
655 
656   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
657   unwinder.Unwind();
658   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
659   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
660   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
661 
662   ASSERT_EQ(1U, unwinder.NumFrames());
663 }
664 
665 // Verify SP in a device stops the unwind.
TEST_F(UnwinderTest,sp_in_device_stops_unwind)666 TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
667   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
668   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
669   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
670 
671   regs_.set_pc(0x1000);
672   regs_.set_sp(0x13000);
673   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
674   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
675   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
676 
677   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
678   unwinder.Unwind();
679   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
680   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
681   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
682 
683   ASSERT_EQ(1U, unwinder.NumFrames());
684 }
685 
686 // Verify a no map info frame gets a frame.
TEST_F(UnwinderTest,pc_without_map)687 TEST_F(UnwinderTest, pc_without_map) {
688   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
689 
690   regs_.set_pc(0x41000);
691   regs_.set_sp(0x13000);
692 
693   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
694   unwinder.Unwind();
695   EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode());
696   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
697   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
698 
699   ASSERT_EQ(1U, unwinder.NumFrames());
700 
701   auto* frame = &unwinder.frames()[0];
702   EXPECT_EQ(0U, frame->num);
703   EXPECT_EQ(0x41000U, frame->rel_pc);
704   EXPECT_EQ(0x41000U, frame->pc);
705   EXPECT_EQ(0x13000U, frame->sp);
706   EXPECT_EQ("", frame->function_name);
707   EXPECT_EQ(0U, frame->function_offset);
708   EXPECT_EQ("", frame->map_name);
709   EXPECT_EQ(0U, frame->map_elf_start_offset);
710   EXPECT_EQ(0U, frame->map_exact_offset);
711   EXPECT_EQ(0U, frame->map_start);
712   EXPECT_EQ(0U, frame->map_end);
713   EXPECT_EQ(0U, frame->map_load_bias);
714   EXPECT_EQ(0, frame->map_flags);
715 }
716 
717 // Verify that a speculative frame is added.
TEST_F(UnwinderTest,speculative_frame)718 TEST_F(UnwinderTest, speculative_frame) {
719   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
720   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
721 
722   // Fake as if code called a nullptr function.
723   regs_.set_pc(0);
724   regs_.set_sp(0x10000);
725   regs_.FakeSetReturnAddress(0x1204);
726   regs_.FakeSetReturnAddressValid(true);
727 
728   ElfInterfaceFake::FakePushStepData(StepData(0x23104, 0x10020, false));
729   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
730 
731   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
732   unwinder.Unwind();
733   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
734   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
735   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
736 
737   ASSERT_EQ(3U, unwinder.NumFrames());
738 
739   auto* frame = &unwinder.frames()[0];
740   EXPECT_EQ(0U, frame->num);
741   EXPECT_EQ(0U, frame->rel_pc);
742   EXPECT_EQ(0U, frame->pc);
743   EXPECT_EQ(0x10000U, frame->sp);
744   EXPECT_EQ("", frame->function_name);
745   EXPECT_EQ(0U, frame->function_offset);
746   EXPECT_EQ("", frame->map_name);
747   EXPECT_EQ(0U, frame->map_elf_start_offset);
748   EXPECT_EQ(0U, frame->map_exact_offset);
749   EXPECT_EQ(0U, frame->map_start);
750   EXPECT_EQ(0U, frame->map_end);
751   EXPECT_EQ(0U, frame->map_load_bias);
752   EXPECT_EQ(0, frame->map_flags);
753 
754   frame = &unwinder.frames()[1];
755   EXPECT_EQ(1U, frame->num);
756   EXPECT_EQ(0x200U, frame->rel_pc);
757   EXPECT_EQ(0x1200U, frame->pc);
758   EXPECT_EQ(0x10000U, frame->sp);
759   EXPECT_EQ("Frame0", frame->function_name);
760   EXPECT_EQ(0U, frame->function_offset);
761   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
762   EXPECT_EQ(0U, frame->map_elf_start_offset);
763   EXPECT_EQ(0U, frame->map_exact_offset);
764   EXPECT_EQ(0x1000U, frame->map_start);
765   EXPECT_EQ(0x8000U, frame->map_end);
766   EXPECT_EQ(0U, frame->map_load_bias);
767   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
768 
769   frame = &unwinder.frames()[2];
770   EXPECT_EQ(2U, frame->num);
771   EXPECT_EQ(0x100U, frame->rel_pc);
772   EXPECT_EQ(0x23100U, frame->pc);
773   EXPECT_EQ(0x10020U, frame->sp);
774   EXPECT_EQ("Frame1", frame->function_name);
775   EXPECT_EQ(1U, frame->function_offset);
776   EXPECT_EQ("/fake/libanother.so", frame->map_name);
777   EXPECT_EQ(0U, frame->map_elf_start_offset);
778   EXPECT_EQ(0U, frame->map_exact_offset);
779   EXPECT_EQ(0x23000U, frame->map_start);
780   EXPECT_EQ(0x24000U, frame->map_end);
781   EXPECT_EQ(0U, frame->map_load_bias);
782   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
783 }
784 
785 // Verify that a speculative frame is added then removed because no other
786 // frames are added.
TEST_F(UnwinderTest,speculative_frame_removed)787 TEST_F(UnwinderTest, speculative_frame_removed) {
788   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
789   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
790 
791   // Fake as if code called a nullptr function.
792   regs_.set_pc(0x20000);
793   regs_.set_sp(0x10000);
794   ElfInterfaceFake::FakePushStepData(StepData(0, 0x10010, false));
795   regs_.FakeSetReturnAddress(0x12);
796   regs_.FakeSetReturnAddressValid(true);
797 
798   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
799   unwinder.Unwind();
800   EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode());
801   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
802   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
803 
804   ASSERT_EQ(2U, unwinder.NumFrames());
805 
806   auto* frame = &unwinder.frames()[0];
807   EXPECT_EQ(0U, frame->num);
808   EXPECT_EQ(0U, frame->rel_pc);
809   EXPECT_EQ(0x20000U, frame->pc);
810   EXPECT_EQ(0x10000U, frame->sp);
811   EXPECT_EQ("Frame0", frame->function_name);
812   EXPECT_EQ(0U, frame->function_offset);
813   EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
814   EXPECT_EQ(0U, frame->map_elf_start_offset);
815   EXPECT_EQ(0U, frame->map_exact_offset);
816   EXPECT_EQ(0x20000U, frame->map_start);
817   EXPECT_EQ(0x22000U, frame->map_end);
818   EXPECT_EQ(0U, frame->map_load_bias);
819   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
820 
821   frame = &unwinder.frames()[1];
822   EXPECT_EQ(1U, frame->num);
823   EXPECT_EQ(0U, frame->rel_pc);
824   EXPECT_EQ(0U, frame->pc);
825   EXPECT_EQ(0x10010U, frame->sp);
826   EXPECT_EQ("", frame->function_name);
827   EXPECT_EQ(0U, frame->function_offset);
828   EXPECT_EQ("", frame->map_name);
829   EXPECT_EQ(0U, frame->map_elf_start_offset);
830   EXPECT_EQ(0U, frame->map_exact_offset);
831   EXPECT_EQ(0U, frame->map_start);
832   EXPECT_EQ(0U, frame->map_end);
833   EXPECT_EQ(0U, frame->map_load_bias);
834   EXPECT_EQ(0, frame->map_flags);
835 }
836 
837 // Verify that a speculative frame is added and left if there are only
838 // two frames and the pc is in the middle nowhere.
TEST_F(UnwinderTest,speculative_frame_not_removed_pc_bad)839 TEST_F(UnwinderTest, speculative_frame_not_removed_pc_bad) {
840   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
841   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
842 
843   // Fake as if code called a nullptr function.
844   regs_.set_pc(0);
845   regs_.set_sp(0x10000);
846   regs_.FakeSetReturnAddress(0x1204);
847   regs_.FakeSetReturnAddressValid(true);
848 
849   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
850   unwinder.Unwind();
851   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
852   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
853   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
854 
855   ASSERT_EQ(2U, unwinder.NumFrames());
856 
857   auto* frame = &unwinder.frames()[0];
858   EXPECT_EQ(0U, frame->num);
859   EXPECT_EQ(0U, frame->rel_pc);
860   EXPECT_EQ(0U, frame->pc);
861   EXPECT_EQ(0x10000U, frame->sp);
862   EXPECT_EQ("", frame->function_name);
863   EXPECT_EQ(0U, frame->function_offset);
864   EXPECT_EQ("", frame->map_name);
865   EXPECT_EQ(0U, frame->map_elf_start_offset);
866   EXPECT_EQ(0U, frame->map_exact_offset);
867   EXPECT_EQ(0U, frame->map_start);
868   EXPECT_EQ(0U, frame->map_end);
869   EXPECT_EQ(0U, frame->map_load_bias);
870   EXPECT_EQ(0, frame->map_flags);
871 
872   frame = &unwinder.frames()[1];
873   EXPECT_EQ(1U, frame->num);
874   EXPECT_EQ(0x200U, frame->rel_pc);
875   EXPECT_EQ(0x1200U, frame->pc);
876   EXPECT_EQ(0x10000U, frame->sp);
877   EXPECT_EQ("Frame0", frame->function_name);
878   EXPECT_EQ(0U, frame->function_offset);
879   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
880   EXPECT_EQ(0U, frame->map_elf_start_offset);
881   EXPECT_EQ(0U, frame->map_exact_offset);
882   EXPECT_EQ(0x1000U, frame->map_start);
883   EXPECT_EQ(0x8000U, frame->map_end);
884   EXPECT_EQ(0U, frame->map_load_bias);
885   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
886 }
887 
888 // Verify that a speculative frame does not cause a crash when it wasn't
889 // really added due to a filter.
TEST_F(UnwinderTest,speculative_frame_check_with_no_frames)890 TEST_F(UnwinderTest, speculative_frame_check_with_no_frames) {
891   regs_.set_pc(0x23000);
892   regs_.set_sp(0x10000);
893   regs_.FakeSetReturnAddress(0x23100);
894   regs_.FakeSetReturnAddressValid(true);
895 
896   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
897 
898   std::vector<std::string> skip_names{"libanother.so"};
899   unwinder.Unwind(&skip_names);
900   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
901   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
902   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
903 
904   ASSERT_EQ(0U, unwinder.NumFrames());
905 }
906 
907 // Verify that an unwind stops when a frame is in given suffix.
TEST_F(UnwinderTest,map_ignore_suffixes)908 TEST_F(UnwinderTest, map_ignore_suffixes) {
909   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
910   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
911   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
912   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
913 
914   // Fake as if code called a nullptr function.
915   regs_.set_pc(0x1000);
916   regs_.set_sp(0x10000);
917   ElfInterfaceFake::FakePushStepData(StepData(0x43404, 0x10010, false));
918   ElfInterfaceFake::FakePushStepData(StepData(0x53504, 0x10020, false));
919   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
920 
921   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
922   std::vector<std::string> suffixes{"oat"};
923   unwinder.Unwind(nullptr, &suffixes);
924   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
925   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
926   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
927 
928   ASSERT_EQ(2U, unwinder.NumFrames());
929   // Make sure the elf was not initialized.
930   MapInfo* map_info = maps_->Find(0x53000);
931   ASSERT_TRUE(map_info != nullptr);
932   EXPECT_TRUE(map_info->elf() == nullptr);
933 
934   auto* frame = &unwinder.frames()[0];
935   EXPECT_EQ(0U, frame->num);
936   EXPECT_EQ(0U, frame->rel_pc);
937   EXPECT_EQ(0x1000U, frame->pc);
938   EXPECT_EQ(0x10000U, frame->sp);
939   EXPECT_EQ("Frame0", frame->function_name);
940   EXPECT_EQ(0U, frame->function_offset);
941   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
942   EXPECT_EQ(0U, frame->map_elf_start_offset);
943   EXPECT_EQ(0U, frame->map_exact_offset);
944   EXPECT_EQ(0x1000U, frame->map_start);
945   EXPECT_EQ(0x8000U, frame->map_end);
946   EXPECT_EQ(0U, frame->map_load_bias);
947   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
948 
949   frame = &unwinder.frames()[1];
950   EXPECT_EQ(1U, frame->num);
951   EXPECT_EQ(0x400U, frame->rel_pc);
952   EXPECT_EQ(0x43400U, frame->pc);
953   EXPECT_EQ(0x10010U, frame->sp);
954   EXPECT_EQ("Frame1", frame->function_name);
955   EXPECT_EQ(1U, frame->function_offset);
956   EXPECT_EQ("/fake/fake.apk!lib_fake.so", frame->map_name);
957   EXPECT_EQ(0x1d000U, frame->map_elf_start_offset);
958   EXPECT_EQ(0x1d000U, frame->map_exact_offset);
959   EXPECT_EQ(0x43000U, frame->map_start);
960   EXPECT_EQ(0x44000U, frame->map_end);
961   EXPECT_EQ(0U, frame->map_load_bias);
962   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
963 }
964 
965 // Verify that an unwind stops when the sp and pc don't change.
TEST_F(UnwinderTest,sp_pc_do_not_change)966 TEST_F(UnwinderTest, sp_pc_do_not_change) {
967   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
968   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
969   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
970   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
971   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
972 
973   regs_.set_pc(0x1000);
974   regs_.set_sp(0x10000);
975   ElfInterfaceFake::FakePushStepData(StepData(0x33404, 0x10010, false));
976   ElfInterfaceFake::FakePushStepData(StepData(0x33504, 0x10020, false));
977   ElfInterfaceFake::FakePushStepData(StepData(0x33504, 0x10020, false));
978   ElfInterfaceFake::FakePushStepData(StepData(0x33504, 0x10020, false));
979   ElfInterfaceFake::FakePushStepData(StepData(0x33504, 0x10020, false));
980   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
981 
982   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
983   unwinder.Unwind();
984   EXPECT_EQ(ERROR_REPEATED_FRAME, unwinder.LastErrorCode());
985   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
986   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
987 
988   ASSERT_EQ(3U, unwinder.NumFrames());
989 
990   auto* frame = &unwinder.frames()[0];
991   EXPECT_EQ(0U, frame->num);
992   EXPECT_EQ(0U, frame->rel_pc);
993   EXPECT_EQ(0x1000U, frame->pc);
994   EXPECT_EQ(0x10000U, frame->sp);
995   EXPECT_EQ("Frame0", frame->function_name);
996   EXPECT_EQ(0U, frame->function_offset);
997   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
998   EXPECT_EQ(0U, frame->map_elf_start_offset);
999   EXPECT_EQ(0U, frame->map_exact_offset);
1000   EXPECT_EQ(0x1000U, frame->map_start);
1001   EXPECT_EQ(0x8000U, frame->map_end);
1002   EXPECT_EQ(0U, frame->map_load_bias);
1003   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1004 
1005   frame = &unwinder.frames()[1];
1006   EXPECT_EQ(1U, frame->num);
1007   EXPECT_EQ(0x400U, frame->rel_pc);
1008   EXPECT_EQ(0x33400U, frame->pc);
1009   EXPECT_EQ(0x10010U, frame->sp);
1010   EXPECT_EQ("Frame1", frame->function_name);
1011   EXPECT_EQ(1U, frame->function_offset);
1012   EXPECT_EQ("/fake/compressed.so", frame->map_name);
1013   EXPECT_EQ(0U, frame->map_elf_start_offset);
1014   EXPECT_EQ(0U, frame->map_exact_offset);
1015   EXPECT_EQ(0x33000U, frame->map_start);
1016   EXPECT_EQ(0x34000U, frame->map_end);
1017   EXPECT_EQ(0U, frame->map_load_bias);
1018   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1019 
1020   frame = &unwinder.frames()[2];
1021   EXPECT_EQ(2U, frame->num);
1022   EXPECT_EQ(0x500U, frame->rel_pc);
1023   EXPECT_EQ(0x33500U, frame->pc);
1024   EXPECT_EQ(0x10020U, frame->sp);
1025   EXPECT_EQ("Frame2", frame->function_name);
1026   EXPECT_EQ(2U, frame->function_offset);
1027   EXPECT_EQ("/fake/compressed.so", frame->map_name);
1028   EXPECT_EQ(0U, frame->map_elf_start_offset);
1029   EXPECT_EQ(0U, frame->map_exact_offset);
1030   EXPECT_EQ(0x33000U, frame->map_start);
1031   EXPECT_EQ(0x34000U, frame->map_end);
1032   EXPECT_EQ(0U, frame->map_load_bias);
1033   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1034 }
1035 
TEST_F(UnwinderTest,dex_pc_in_map)1036 TEST_F(UnwinderTest, dex_pc_in_map) {
1037   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1038   regs_.set_pc(0x1000);
1039   regs_.set_sp(0x10000);
1040   regs_.FakeSetDexPc(0xa3400);
1041 
1042   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1043   unwinder.Unwind();
1044   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1045   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1046   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1047 
1048   ASSERT_EQ(2U, unwinder.NumFrames());
1049 
1050   auto* frame = &unwinder.frames()[0];
1051   EXPECT_EQ(0U, frame->num);
1052   EXPECT_EQ(0x400U, frame->rel_pc);
1053   EXPECT_EQ(0xa3400U, frame->pc);
1054   EXPECT_EQ(0x10000U, frame->sp);
1055   EXPECT_EQ("", frame->function_name);
1056   EXPECT_EQ(0U, frame->function_offset);
1057   EXPECT_EQ("/fake/fake.vdex", frame->map_name);
1058   EXPECT_EQ(0U, frame->map_elf_start_offset);
1059   EXPECT_EQ(0U, frame->map_exact_offset);
1060   EXPECT_EQ(0xa3000U, frame->map_start);
1061   EXPECT_EQ(0xa4000U, frame->map_end);
1062   EXPECT_EQ(0U, frame->map_load_bias);
1063   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1064 
1065   frame = &unwinder.frames()[1];
1066   EXPECT_EQ(1U, frame->num);
1067   EXPECT_EQ(0U, frame->rel_pc);
1068   EXPECT_EQ(0x1000U, frame->pc);
1069   EXPECT_EQ(0x10000U, frame->sp);
1070   EXPECT_EQ("Frame0", frame->function_name);
1071   EXPECT_EQ(0U, frame->function_offset);
1072   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
1073   EXPECT_EQ(0U, frame->map_elf_start_offset);
1074   EXPECT_EQ(0U, frame->map_exact_offset);
1075   EXPECT_EQ(0x1000U, frame->map_start);
1076   EXPECT_EQ(0x8000U, frame->map_end);
1077   EXPECT_EQ(0U, frame->map_load_bias);
1078   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1079 }
1080 
TEST_F(UnwinderTest,dex_pc_in_map_non_zero_offset)1081 TEST_F(UnwinderTest, dex_pc_in_map_non_zero_offset) {
1082   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1083   regs_.set_pc(0x1000);
1084   regs_.set_sp(0x10000);
1085   regs_.FakeSetDexPc(0xd0400);
1086 
1087   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1088   unwinder.Unwind();
1089   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1090   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1091   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1092 
1093   ASSERT_EQ(2U, unwinder.NumFrames());
1094 
1095   auto* frame = &unwinder.frames()[0];
1096   EXPECT_EQ(0U, frame->num);
1097   EXPECT_EQ(0x400U, frame->rel_pc);
1098   EXPECT_EQ(0xd0400U, frame->pc);
1099   EXPECT_EQ(0x10000U, frame->sp);
1100   EXPECT_EQ("", frame->function_name);
1101   EXPECT_EQ(0U, frame->function_offset);
1102   EXPECT_EQ("/fake/fake.apk", frame->map_name);
1103   EXPECT_EQ(0x1000U, frame->map_elf_start_offset);
1104   EXPECT_EQ(0x1000U, frame->map_exact_offset);
1105   EXPECT_EQ(0xd0000U, frame->map_start);
1106   EXPECT_EQ(0xd1000U, frame->map_end);
1107   EXPECT_EQ(0U, frame->map_load_bias);
1108   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1109 
1110   frame = &unwinder.frames()[1];
1111   EXPECT_EQ(1U, frame->num);
1112   EXPECT_EQ(0U, frame->rel_pc);
1113   EXPECT_EQ(0x1000U, frame->pc);
1114   EXPECT_EQ(0x10000U, frame->sp);
1115   EXPECT_EQ("Frame0", frame->function_name);
1116   EXPECT_EQ(0U, frame->function_offset);
1117   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
1118   EXPECT_EQ(0U, frame->map_elf_start_offset);
1119   EXPECT_EQ(0U, frame->map_exact_offset);
1120   EXPECT_EQ(0x1000U, frame->map_start);
1121   EXPECT_EQ(0x8000U, frame->map_end);
1122   EXPECT_EQ(0U, frame->map_load_bias);
1123   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1124 }
1125 
TEST_F(UnwinderTest,dex_pc_not_in_map)1126 TEST_F(UnwinderTest, dex_pc_not_in_map) {
1127   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1128   regs_.set_pc(0x1000);
1129   regs_.set_sp(0x10000);
1130   regs_.FakeSetDexPc(0x50000);
1131 
1132   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1133   unwinder.Unwind();
1134   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1135   EXPECT_EQ(WARNING_DEX_PC_NOT_IN_MAP, unwinder.warnings());
1136   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1137 
1138   ASSERT_EQ(2U, unwinder.NumFrames());
1139 
1140   auto* frame = &unwinder.frames()[0];
1141   EXPECT_EQ(0U, frame->num);
1142   EXPECT_EQ(0x50000U, frame->rel_pc);
1143   EXPECT_EQ(0x50000U, frame->pc);
1144   EXPECT_EQ(0x10000U, frame->sp);
1145   EXPECT_EQ("", frame->function_name);
1146   EXPECT_EQ(0U, frame->function_offset);
1147   EXPECT_EQ("", frame->map_name);
1148   EXPECT_EQ(0U, frame->map_elf_start_offset);
1149   EXPECT_EQ(0U, frame->map_exact_offset);
1150   EXPECT_EQ(0U, frame->map_start);
1151   EXPECT_EQ(0U, frame->map_end);
1152   EXPECT_EQ(0U, frame->map_load_bias);
1153   EXPECT_EQ(0, frame->map_flags);
1154 
1155   frame = &unwinder.frames()[1];
1156   EXPECT_EQ(1U, frame->num);
1157   EXPECT_EQ(0U, frame->rel_pc);
1158   EXPECT_EQ(0x1000U, frame->pc);
1159   EXPECT_EQ(0x10000U, frame->sp);
1160   EXPECT_EQ("Frame0", frame->function_name);
1161   EXPECT_EQ(0U, frame->function_offset);
1162   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
1163   EXPECT_EQ(0U, frame->map_elf_start_offset);
1164   EXPECT_EQ(0U, frame->map_exact_offset);
1165   EXPECT_EQ(0x1000U, frame->map_start);
1166   EXPECT_EQ(0x8000U, frame->map_end);
1167   EXPECT_EQ(0U, frame->map_load_bias);
1168   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1169 }
1170 
TEST_F(UnwinderTest,dex_pc_not_in_map_valid_dex_files)1171 TEST_F(UnwinderTest, dex_pc_not_in_map_valid_dex_files) {
1172   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1173   regs_.set_pc(0x1000);
1174   regs_.set_sp(0x10000);
1175   regs_.FakeSetDexPc(0x50000);
1176 
1177   std::unique_ptr<DexFiles> dex_files = CreateDexFiles(regs_.Arch(), process_memory_);
1178   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1179   unwinder.SetDexFiles(dex_files.get());
1180   unwinder.Unwind();
1181   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1182   EXPECT_EQ(WARNING_DEX_PC_NOT_IN_MAP, unwinder.warnings());
1183   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1184 
1185   ASSERT_EQ(2U, unwinder.NumFrames());
1186 
1187   auto* frame = &unwinder.frames()[0];
1188   EXPECT_EQ(0U, frame->num);
1189   EXPECT_EQ(0x50000U, frame->rel_pc);
1190   EXPECT_EQ(0x50000U, frame->pc);
1191   EXPECT_EQ(0x10000U, frame->sp);
1192   EXPECT_EQ("", frame->function_name);
1193   EXPECT_EQ(0U, frame->function_offset);
1194   EXPECT_EQ("", frame->map_name);
1195   EXPECT_EQ(0U, frame->map_elf_start_offset);
1196   EXPECT_EQ(0U, frame->map_exact_offset);
1197   EXPECT_EQ(0U, frame->map_start);
1198   EXPECT_EQ(0U, frame->map_end);
1199   EXPECT_EQ(0U, frame->map_load_bias);
1200   EXPECT_EQ(0, frame->map_flags);
1201 
1202   frame = &unwinder.frames()[1];
1203   EXPECT_EQ(1U, frame->num);
1204   EXPECT_EQ(0U, frame->rel_pc);
1205   EXPECT_EQ(0x1000U, frame->pc);
1206   EXPECT_EQ(0x10000U, frame->sp);
1207   EXPECT_EQ("Frame0", frame->function_name);
1208   EXPECT_EQ(0U, frame->function_offset);
1209   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
1210   EXPECT_EQ(0U, frame->map_elf_start_offset);
1211   EXPECT_EQ(0U, frame->map_exact_offset);
1212   EXPECT_EQ(0x1000U, frame->map_start);
1213   EXPECT_EQ(0x8000U, frame->map_end);
1214   EXPECT_EQ(0U, frame->map_load_bias);
1215   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1216 }
1217 
TEST_F(UnwinderTest,dex_pc_multiple_frames)1218 TEST_F(UnwinderTest, dex_pc_multiple_frames) {
1219   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1220   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
1221   regs_.set_pc(0x1000);
1222   regs_.set_sp(0x10000);
1223   regs_.FakeSetDexPc(0xa3400);
1224   ElfInterfaceFake::FakePushStepData(StepData(0x33404, 0x10010, false));
1225   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
1226 
1227   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1228   unwinder.Unwind();
1229   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1230   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1231   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1232 
1233   ASSERT_EQ(3U, unwinder.NumFrames());
1234 
1235   auto* frame = &unwinder.frames()[0];
1236   EXPECT_EQ(0U, frame->num);
1237   EXPECT_EQ(0x400U, frame->rel_pc);
1238   EXPECT_EQ(0xa3400U, frame->pc);
1239   EXPECT_EQ(0x10000U, frame->sp);
1240   EXPECT_EQ("", frame->function_name);
1241   EXPECT_EQ(0U, frame->function_offset);
1242   EXPECT_EQ("/fake/fake.vdex", frame->map_name);
1243   EXPECT_EQ(0U, frame->map_elf_start_offset);
1244   EXPECT_EQ(0U, frame->map_exact_offset);
1245   EXPECT_EQ(0xa3000U, frame->map_start);
1246   EXPECT_EQ(0xa4000U, frame->map_end);
1247   EXPECT_EQ(0U, frame->map_load_bias);
1248   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1249 
1250   frame = &unwinder.frames()[1];
1251   EXPECT_EQ(1U, frame->num);
1252   EXPECT_EQ(0U, frame->rel_pc);
1253   EXPECT_EQ(0x1000U, frame->pc);
1254   EXPECT_EQ(0x10000U, frame->sp);
1255   EXPECT_EQ("Frame0", frame->function_name);
1256   EXPECT_EQ(0U, frame->function_offset);
1257   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
1258   EXPECT_EQ(0U, frame->map_elf_start_offset);
1259   EXPECT_EQ(0U, frame->map_exact_offset);
1260   EXPECT_EQ(0x1000U, frame->map_start);
1261   EXPECT_EQ(0x8000U, frame->map_end);
1262   EXPECT_EQ(0U, frame->map_load_bias);
1263   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1264 
1265   frame = &unwinder.frames()[2];
1266   EXPECT_EQ(2U, frame->num);
1267   EXPECT_EQ(0x400U, frame->rel_pc);
1268   EXPECT_EQ(0x33400U, frame->pc);
1269   EXPECT_EQ(0x10010U, frame->sp);
1270   EXPECT_EQ("Frame1", frame->function_name);
1271   EXPECT_EQ(1U, frame->function_offset);
1272   EXPECT_EQ("/fake/compressed.so", frame->map_name);
1273   EXPECT_EQ(0U, frame->map_elf_start_offset);
1274   EXPECT_EQ(0U, frame->map_exact_offset);
1275   EXPECT_EQ(0x33000U, frame->map_start);
1276   EXPECT_EQ(0x34000U, frame->map_end);
1277   EXPECT_EQ(0U, frame->map_load_bias);
1278   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
1279 }
1280 
TEST_F(UnwinderTest,dex_pc_max_frames)1281 TEST_F(UnwinderTest, dex_pc_max_frames) {
1282   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1283   regs_.set_pc(0x1000);
1284   regs_.set_sp(0x10000);
1285   regs_.FakeSetDexPc(0xa3400);
1286 
1287   Unwinder unwinder(1, maps_.get(), &regs_, process_memory_);
1288   unwinder.Unwind();
1289   EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode());
1290   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1291   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1292 
1293   ASSERT_EQ(1U, unwinder.NumFrames());
1294 
1295   auto* frame = &unwinder.frames()[0];
1296   EXPECT_EQ(0U, frame->num);
1297   EXPECT_EQ(0x400U, frame->rel_pc);
1298   EXPECT_EQ(0xa3400U, frame->pc);
1299   EXPECT_EQ(0x10000U, frame->sp);
1300   EXPECT_EQ("", frame->function_name);
1301   EXPECT_EQ(0U, frame->function_offset);
1302   EXPECT_EQ("/fake/fake.vdex", frame->map_name);
1303   EXPECT_EQ(0U, frame->map_elf_start_offset);
1304   EXPECT_EQ(0U, frame->map_exact_offset);
1305   EXPECT_EQ(0xa3000U, frame->map_start);
1306   EXPECT_EQ(0xa4000U, frame->map_end);
1307   EXPECT_EQ(0U, frame->map_load_bias);
1308   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1309 }
1310 
TEST_F(UnwinderTest,elf_from_memory_not_file)1311 TEST_F(UnwinderTest, elf_from_memory_not_file) {
1312   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1313 
1314   regs_.set_pc(0xc0050);
1315   regs_.set_sp(0x10000);
1316   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
1317 
1318   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1319   unwinder.Unwind();
1320   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1321   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1322   EXPECT_TRUE(unwinder.elf_from_memory_not_file());
1323 
1324   ASSERT_EQ(1U, unwinder.NumFrames());
1325 
1326   auto* frame = &unwinder.frames()[0];
1327   EXPECT_EQ(0U, frame->num);
1328   EXPECT_EQ(0x50U, frame->rel_pc);
1329   EXPECT_EQ(0xc0050U, frame->pc);
1330   EXPECT_EQ(0x10000U, frame->sp);
1331   EXPECT_EQ("Frame0", frame->function_name);
1332   EXPECT_EQ(0U, frame->function_offset);
1333   EXPECT_EQ("/fake/unreadable.so", frame->map_name);
1334   EXPECT_EQ(0U, frame->map_elf_start_offset);
1335   EXPECT_EQ(0U, frame->map_exact_offset);
1336   EXPECT_EQ(0xc0000U, frame->map_start);
1337   EXPECT_EQ(0xc1000U, frame->map_end);
1338   EXPECT_EQ(0U, frame->map_load_bias);
1339   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1340 }
1341 
TEST_F(UnwinderTest,elf_from_memory_but_no_valid_file_with_bracket)1342 TEST_F(UnwinderTest, elf_from_memory_but_no_valid_file_with_bracket) {
1343   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1344 
1345   regs_.set_pc(0xc1050);
1346   regs_.set_sp(0x10000);
1347   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
1348 
1349   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1350   unwinder.Unwind();
1351   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1352   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1353   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1354 
1355   ASSERT_EQ(1U, unwinder.NumFrames());
1356 
1357   auto* frame = &unwinder.frames()[0];
1358   EXPECT_EQ(0U, frame->num);
1359   EXPECT_EQ(0x50U, frame->rel_pc);
1360   EXPECT_EQ(0xc1050U, frame->pc);
1361   EXPECT_EQ(0x10000U, frame->sp);
1362   EXPECT_EQ("Frame0", frame->function_name);
1363   EXPECT_EQ(0U, frame->function_offset);
1364   EXPECT_EQ("[vdso]", frame->map_name);
1365   EXPECT_EQ(0U, frame->map_elf_start_offset);
1366   EXPECT_EQ(0U, frame->map_exact_offset);
1367   EXPECT_EQ(0xc1000U, frame->map_start);
1368   EXPECT_EQ(0xc2000U, frame->map_end);
1369   EXPECT_EQ(0U, frame->map_load_bias);
1370   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1371 }
1372 
TEST_F(UnwinderTest,elf_from_memory_but_empty_filename)1373 TEST_F(UnwinderTest, elf_from_memory_but_empty_filename) {
1374   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1375 
1376   regs_.set_pc(0xc2050);
1377   regs_.set_sp(0x10000);
1378   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
1379 
1380   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1381   unwinder.Unwind();
1382   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1383   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1384   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1385 
1386   ASSERT_EQ(1U, unwinder.NumFrames());
1387 
1388   auto* frame = &unwinder.frames()[0];
1389   EXPECT_EQ(0U, frame->num);
1390   EXPECT_EQ(0x50U, frame->rel_pc);
1391   EXPECT_EQ(0xc2050U, frame->pc);
1392   EXPECT_EQ(0x10000U, frame->sp);
1393   EXPECT_EQ("Frame0", frame->function_name);
1394   EXPECT_EQ(0U, frame->function_offset);
1395   EXPECT_EQ("", frame->map_name);
1396   EXPECT_EQ(0U, frame->map_elf_start_offset);
1397   EXPECT_EQ(0U, frame->map_exact_offset);
1398   EXPECT_EQ(0xc2000U, frame->map_start);
1399   EXPECT_EQ(0xc3000U, frame->map_end);
1400   EXPECT_EQ(0U, frame->map_load_bias);
1401   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1402 }
1403 
TEST_F(UnwinderTest,elf_from_memory_but_from_memfd)1404 TEST_F(UnwinderTest, elf_from_memory_but_from_memfd) {
1405   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
1406 
1407   regs_.set_pc(0xc3050);
1408   regs_.set_sp(0x10000);
1409   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
1410 
1411   Unwinder unwinder(64, maps_.get(), &regs_, process_memory_);
1412   unwinder.Unwind();
1413   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
1414   EXPECT_EQ(WARNING_NONE, unwinder.warnings());
1415   EXPECT_FALSE(unwinder.elf_from_memory_not_file());
1416 
1417   ASSERT_EQ(1U, unwinder.NumFrames());
1418 
1419   auto* frame = &unwinder.frames()[0];
1420   EXPECT_EQ(0U, frame->num);
1421   EXPECT_EQ(0x50U, frame->rel_pc);
1422   EXPECT_EQ(0xc3050U, frame->pc);
1423   EXPECT_EQ(0x10000U, frame->sp);
1424   EXPECT_EQ("Frame0", frame->function_name);
1425   EXPECT_EQ(0U, frame->function_offset);
1426   EXPECT_EQ("/memfd:/jit-cache", frame->map_name);
1427   EXPECT_EQ(0U, frame->map_elf_start_offset);
1428   EXPECT_EQ(0U, frame->map_exact_offset);
1429   EXPECT_EQ(0xc3000U, frame->map_start);
1430   EXPECT_EQ(0xc4000U, frame->map_end);
1431   EXPECT_EQ(0U, frame->map_load_bias);
1432   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
1433 }
1434 
1435 // Verify format frame code.
TEST_F(UnwinderTest,format_frame)1436 TEST_F(UnwinderTest, format_frame) {
1437   RegsFake regs_arm(10);
1438   regs_arm.FakeSetArch(ARCH_ARM);
1439   Unwinder unwinder32(10, maps_.get(), &regs_arm, process_memory_);
1440 
1441   RegsFake regs_arm64(10);
1442   regs_arm64.FakeSetArch(ARCH_ARM64);
1443   Unwinder unwinder64(10, maps_.get(), &regs_arm64, process_memory_);
1444 
1445   FrameData frame;
1446   frame.num = 1;
1447   frame.rel_pc = 0x1000;
1448   frame.pc = 0x4000;
1449   frame.sp = 0x1000;
1450   frame.function_name = "function";
1451   frame.function_offset = 100;
1452   frame.map_name = "/fake/libfake.so";
1453   frame.map_elf_start_offset = 0x2000;
1454   frame.map_start = 0x3000;
1455   frame.map_end = 0x6000;
1456   frame.map_flags = PROT_READ;
1457 
1458   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (offset 0x2000) (function+100)",
1459             unwinder64.FormatFrame(frame));
1460   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (offset 0x2000) (function+100)",
1461             unwinder32.FormatFrame(frame));
1462 
1463   frame.map_elf_start_offset = 0;
1464   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (function+100)",
1465             unwinder64.FormatFrame(frame));
1466   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function+100)", unwinder32.FormatFrame(frame));
1467 
1468   frame.function_offset = 0;
1469   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (function)",
1470             unwinder64.FormatFrame(frame));
1471   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function)", unwinder32.FormatFrame(frame));
1472 
1473   // Verify the function name is demangled.
1474   frame.function_name = "_ZN4funcEv";
1475   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (func())", unwinder64.FormatFrame(frame));
1476   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (func())", unwinder32.FormatFrame(frame));
1477 
1478   frame.function_name = "";
1479   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so", unwinder64.FormatFrame(frame));
1480   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so", unwinder32.FormatFrame(frame));
1481 
1482   frame.map_name = "";
1483   EXPECT_EQ("  #01 pc 0000000000001000  <anonymous:3000>", unwinder64.FormatFrame(frame));
1484   EXPECT_EQ("  #01 pc 00001000  <anonymous:3000>", unwinder32.FormatFrame(frame));
1485 
1486   frame.map_start = 0;
1487   frame.map_end = 0;
1488   EXPECT_EQ("  #01 pc 0000000000001000  <unknown>", unwinder64.FormatFrame(frame));
1489   EXPECT_EQ("  #01 pc 00001000  <unknown>", unwinder32.FormatFrame(frame));
1490 }
1491 
TEST_F(UnwinderTest,format_frame_build_id)1492 TEST_F(UnwinderTest, format_frame_build_id) {
1493   RegsFake regs(10);
1494   regs.FakeSetArch(ARCH_ARM);
1495   Unwinder unwinder(10, maps_.get(), &regs, process_memory_);
1496 
1497   FrameData frame;
1498   frame.num = 1;
1499   frame.rel_pc = 0x1000;
1500   frame.pc = 0x4000;
1501   frame.sp = 0x1000;
1502   frame.function_name = "function";
1503   frame.function_offset = 100;
1504   frame.map_name = "/fake/libfake.so";
1505   frame.map_elf_start_offset = 0;
1506   frame.map_start = 0x3000;
1507   frame.map_end = 0x6000;
1508   frame.map_flags = PROT_READ;
1509 
1510   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function+100)", unwinder.FormatFrame(frame));
1511   unwinder.SetDisplayBuildID(true);
1512   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function+100) (BuildId: 46414b45)",
1513             unwinder.FormatFrame(frame));
1514 }
1515 
ArchToString(ArchEnum arch)1516 static std::string ArchToString(ArchEnum arch) {
1517   if (arch == ARCH_ARM) {
1518     return "Arm";
1519   } else if (arch == ARCH_ARM64) {
1520     return "Arm64";
1521   } else if (arch == ARCH_X86) {
1522     return "X86";
1523   } else if (arch == ARCH_X86_64) {
1524     return "X86_64";
1525   } else {
1526     return "Unknown";
1527   }
1528 }
1529 
1530 // Verify format frame code.
TEST_F(UnwinderTest,format_frame_by_arch)1531 TEST_F(UnwinderTest, format_frame_by_arch) {
1532   std::vector<Regs*> reg_list;
1533   RegsArm* arm = new RegsArm;
1534   arm->set_pc(0x2300);
1535   arm->set_sp(0x10000);
1536   reg_list.push_back(arm);
1537 
1538   RegsArm64* arm64 = new RegsArm64;
1539   arm64->set_pc(0x2300);
1540   arm64->set_sp(0x10000);
1541   reg_list.push_back(arm64);
1542 
1543   RegsX86* x86 = new RegsX86;
1544   x86->set_pc(0x2300);
1545   x86->set_sp(0x10000);
1546   reg_list.push_back(x86);
1547 
1548   RegsX86_64* x86_64 = new RegsX86_64;
1549   x86_64->set_pc(0x2300);
1550   x86_64->set_sp(0x10000);
1551   reg_list.push_back(x86_64);
1552 
1553   RegsMips* mips = new RegsMips;
1554   mips->set_pc(0x2300);
1555   mips->set_sp(0x10000);
1556   reg_list.push_back(mips);
1557 
1558   RegsMips64* mips64 = new RegsMips64;
1559   mips64->set_pc(0x2300);
1560   mips64->set_sp(0x10000);
1561   reg_list.push_back(mips64);
1562 
1563   for (auto regs : reg_list) {
1564     ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
1565 
1566     Unwinder unwinder(64, maps_.get(), regs, process_memory_);
1567     unwinder.Unwind();
1568 
1569     ASSERT_EQ(1U, unwinder.NumFrames());
1570     std::string expected;
1571     switch (regs->Arch()) {
1572       case ARCH_ARM:
1573       case ARCH_X86:
1574       case ARCH_MIPS:
1575         expected = "  #00 pc 00001300  /system/fake/libc.so (Frame0+10)";
1576         break;
1577       case ARCH_ARM64:
1578       case ARCH_X86_64:
1579       case ARCH_MIPS64:
1580         expected = "  #00 pc 0000000000001300  /system/fake/libc.so (Frame0+10)";
1581         break;
1582       default:
1583         expected = "";
1584     }
1585     EXPECT_EQ(expected, unwinder.FormatFrame(0))
1586         << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
1587     delete regs;
1588   }
1589 }
1590 
TEST_F(UnwinderTest,build_frame_pc_only_errors)1591 TEST_F(UnwinderTest, build_frame_pc_only_errors) {
1592   RegsFake regs(10);
1593   regs.FakeSetArch(ARCH_ARM);
1594   Unwinder unwinder(10, maps_.get(), &regs, process_memory_);
1595 
1596   FrameData frame;
1597 
1598   // Pc not in map
1599   frame = unwinder.BuildFrameFromPcOnly(0x10);
1600   EXPECT_EQ(0x10U, frame.pc);
1601   EXPECT_EQ(0x10U, frame.rel_pc);
1602 
1603   // No regs set
1604   unwinder.SetRegs(nullptr);
1605   frame = unwinder.BuildFrameFromPcOnly(0x100310);
1606   EXPECT_EQ(0x100310U, frame.pc);
1607   EXPECT_EQ(0x100310U, frame.rel_pc);
1608   unwinder.SetRegs(&regs);
1609 
1610   // Invalid elf
1611   frame = unwinder.BuildFrameFromPcOnly(0x100310);
1612   EXPECT_EQ(0x10030eU, frame.pc);
1613   EXPECT_EQ(0x60eU, frame.rel_pc);
1614   EXPECT_EQ("/fake/jit.so", frame.map_name);
1615   EXPECT_EQ(0x100U, frame.map_elf_start_offset);
1616   EXPECT_EQ(0x200U, frame.map_exact_offset);
1617   EXPECT_EQ(0x100000U, frame.map_start);
1618   EXPECT_EQ(0x101000U, frame.map_end);
1619   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame.map_flags);
1620   EXPECT_EQ(0x300U, frame.map_load_bias);
1621   EXPECT_EQ("", frame.function_name);
1622   EXPECT_EQ(0U, frame.function_offset);
1623 }
1624 
TEST_F(UnwinderTest,build_frame_pc_valid_elf)1625 TEST_F(UnwinderTest, build_frame_pc_valid_elf) {
1626   RegsFake regs(10);
1627   regs.FakeSetArch(ARCH_ARM);
1628   Unwinder unwinder(10, maps_.get(), &regs, process_memory_);
1629 
1630   FrameData frame;
1631 
1632   // Valid elf, no function data.
1633   frame = unwinder.BuildFrameFromPcOnly(0x1010);
1634   EXPECT_EQ(0x100cU, frame.pc);
1635   EXPECT_EQ(0xcU, frame.rel_pc);
1636   EXPECT_EQ("/system/fake/libc.so", frame.map_name);
1637   EXPECT_EQ(0U, frame.map_elf_start_offset);
1638   EXPECT_EQ(0U, frame.map_exact_offset);
1639   EXPECT_EQ(0x1000U, frame.map_start);
1640   EXPECT_EQ(0x8000U, frame.map_end);
1641   EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags);
1642   EXPECT_EQ(0U, frame.map_load_bias);
1643   EXPECT_EQ("", frame.function_name);
1644   EXPECT_EQ(0U, frame.function_offset);
1645 
1646   // Valid elf, function data present, but do not resolve.
1647   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
1648   unwinder.SetResolveNames(false);
1649 
1650   frame = unwinder.BuildFrameFromPcOnly(0x1010);
1651   EXPECT_EQ(0x100cU, frame.pc);
1652   EXPECT_EQ(0xcU, frame.rel_pc);
1653   EXPECT_EQ("/system/fake/libc.so", frame.map_name);
1654   EXPECT_EQ(0U, frame.map_elf_start_offset);
1655   EXPECT_EQ(0U, frame.map_exact_offset);
1656   EXPECT_EQ(0x1000U, frame.map_start);
1657   EXPECT_EQ(0x8000U, frame.map_end);
1658   EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags);
1659   EXPECT_EQ(0U, frame.map_load_bias);
1660   EXPECT_EQ("", frame.function_name);
1661   EXPECT_EQ(0U, frame.function_offset);
1662 
1663   // Valid elf, function data present.
1664   unwinder.SetResolveNames(true);
1665 
1666   frame = unwinder.BuildFrameFromPcOnly(0x1010);
1667   EXPECT_EQ(0x100cU, frame.pc);
1668   EXPECT_EQ(0xcU, frame.rel_pc);
1669   EXPECT_EQ("/system/fake/libc.so", frame.map_name);
1670   EXPECT_EQ(0U, frame.map_elf_start_offset);
1671   EXPECT_EQ(0U, frame.map_exact_offset);
1672   EXPECT_EQ(0x1000U, frame.map_start);
1673   EXPECT_EQ(0x8000U, frame.map_end);
1674   EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags);
1675   EXPECT_EQ(0U, frame.map_load_bias);
1676   EXPECT_EQ("Frame0", frame.function_name);
1677   EXPECT_EQ(10U, frame.function_offset);
1678 }
1679 
TEST_F(UnwinderTest,build_frame_pc_in_jit)1680 TEST_F(UnwinderTest, build_frame_pc_in_jit) {
1681   // The whole ELF will be copied (read), so it must be valid (readable) memory.
1682   memory_->SetMemoryBlock(0xf7000, 0x1000, 0);
1683 
1684   Elf32_Ehdr ehdr = {};
1685   TestInitEhdr<Elf32_Ehdr>(&ehdr, ELFCLASS32, EM_ARM);
1686   ehdr.e_phoff = 0x50;
1687   ehdr.e_phnum = 1;
1688   ehdr.e_phentsize = sizeof(Elf32_Phdr);
1689   ehdr.e_shoff = 0x100;
1690   ehdr.e_shstrndx = 1;
1691   ehdr.e_shentsize = sizeof(Elf32_Shdr);
1692   ehdr.e_shnum = 3;
1693   memory_->SetMemory(0xf7000, &ehdr, sizeof(ehdr));
1694 
1695   Elf32_Phdr phdr = {};
1696   phdr.p_flags = PF_X;
1697   phdr.p_type = PT_LOAD;
1698   phdr.p_offset = 0x100000;
1699   phdr.p_vaddr = 0x100000;
1700   phdr.p_memsz = 0x1000;
1701   memory_->SetMemory(0xf7050, &phdr, sizeof(phdr));
1702 
1703   Elf32_Shdr shdr = {};
1704   shdr.sh_type = SHT_NULL;
1705   memory_->SetMemory(0xf7100, &shdr, sizeof(shdr));
1706 
1707   shdr.sh_type = SHT_SYMTAB;
1708   shdr.sh_link = 2;
1709   shdr.sh_addr = 0x300;
1710   shdr.sh_offset = 0x300;
1711   shdr.sh_entsize = sizeof(Elf32_Sym);
1712   shdr.sh_size = shdr.sh_entsize;
1713   memory_->SetMemory(0xf7100 + sizeof(shdr), &shdr, sizeof(shdr));
1714 
1715   memset(&shdr, 0, sizeof(shdr));
1716   shdr.sh_type = SHT_STRTAB;
1717   shdr.sh_name = 0x500;
1718   shdr.sh_offset = 0x400;
1719   shdr.sh_size = 0x100;
1720   memory_->SetMemory(0xf7100 + 2 * sizeof(shdr), &shdr, sizeof(shdr));
1721 
1722   Elf32_Sym sym = {};
1723   sym.st_shndx = 2;
1724   sym.st_info = STT_FUNC;
1725   sym.st_value = 0x100300;
1726   sym.st_size = 0x100;
1727   sym.st_name = 1;
1728   memory_->SetMemory(0xf7300, &sym, sizeof(sym));
1729   memory_->SetMemory(0xf7401, "FakeJitFunction");
1730 
1731   RegsFake regs(10);
1732   regs.FakeSetArch(ARCH_ARM);
1733   std::unique_ptr<JitDebug> jit_debug = CreateJitDebug(regs.Arch(), process_memory_);
1734   Unwinder unwinder(10, maps_.get(), &regs, process_memory_);
1735   unwinder.SetJitDebug(jit_debug.get());
1736 
1737   FrameData frame = unwinder.BuildFrameFromPcOnly(0x100310);
1738   EXPECT_EQ(0x10030eU, frame.pc);
1739   EXPECT_EQ(0x60eU, frame.rel_pc);
1740   EXPECT_EQ("/fake/jit.so", frame.map_name);
1741   EXPECT_EQ(0x100U, frame.map_elf_start_offset);
1742   EXPECT_EQ(0x200U, frame.map_exact_offset);
1743   EXPECT_EQ(0x100000U, frame.map_start);
1744   EXPECT_EQ(0x101000U, frame.map_end);
1745   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame.map_flags);
1746   EXPECT_EQ(0U, frame.map_load_bias);
1747   EXPECT_EQ("FakeJitFunction", frame.function_name);
1748   EXPECT_EQ(0xeU, frame.function_offset);
1749 }
1750 
TEST_F(UnwinderTest,unwinder_from_pid_set_process_memory)1751 TEST_F(UnwinderTest, unwinder_from_pid_set_process_memory) {
1752   auto process_memory = Memory::CreateProcessMemoryCached(getpid());
1753   UnwinderFromPid unwinder(10, getpid());
1754   unwinder.SetProcessMemory(process_memory);
1755   unwinder.SetArch(unwindstack::Regs::CurrentArch());
1756   ASSERT_TRUE(unwinder.Init());
1757   ASSERT_EQ(process_memory.get(), unwinder.GetProcessMemory().get());
1758 }
1759 
1760 using UnwinderDeathTest = SilentDeathTest;
1761 
TEST_F(UnwinderDeathTest,unwinder_from_pid_init_error)1762 TEST_F(UnwinderDeathTest, unwinder_from_pid_init_error) {
1763   UnwinderFromPid unwinder(10, getpid());
1764   ASSERT_DEATH(unwinder.Init(), "");
1765 }
1766 
TEST_F(UnwinderDeathTest,set_jit_debug_error)1767 TEST_F(UnwinderDeathTest, set_jit_debug_error) {
1768   Maps maps;
1769   std::shared_ptr<Memory> process_memory(new MemoryFake);
1770   Unwinder unwinder(10, &maps, process_memory);
1771   ASSERT_DEATH(CreateJitDebug(ARCH_UNKNOWN, process_memory), "");
1772 }
1773 
TEST_F(UnwinderDeathTest,set_dex_files_error)1774 TEST_F(UnwinderDeathTest, set_dex_files_error) {
1775   Maps maps;
1776   std::shared_ptr<Memory> process_memory(new MemoryFake);
1777   Unwinder unwinder(10, &maps, process_memory);
1778   ASSERT_DEATH(CreateDexFiles(ARCH_UNKNOWN, process_memory), "");
1779 }
1780 
1781 }  // namespace unwindstack
1782