• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unwindstack/Elf.h>
28 #include <unwindstack/Maps.h>
29 #include <unwindstack/Memory.h>
30 #include <unwindstack/Regs.h>
31 #include <unwindstack/RegsArm.h>
32 #include <unwindstack/RegsArm64.h>
33 #include <unwindstack/RegsX86.h>
34 #include <unwindstack/RegsX86_64.h>
35 #include <unwindstack/RegsMips.h>
36 #include <unwindstack/RegsMips64.h>
37 #include <unwindstack/Unwinder.h>
38 
39 #include "ElfFake.h"
40 #include "MemoryFake.h"
41 #include "RegsFake.h"
42 
43 namespace unwindstack {
44 
45 class MapsFake : public Maps {
46  public:
47   MapsFake() = default;
48   virtual ~MapsFake() = default;
49 
Parse()50   bool Parse() { return true; }
51 
FakeClear()52   void FakeClear() { maps_.clear(); }
53 
FakeAddMapInfo(MapInfo * map_info)54   void FakeAddMapInfo(MapInfo* map_info) { maps_.push_back(map_info); }
55 };
56 
57 class UnwinderTest : public ::testing::Test {
58  protected:
SetUpTestCase()59   static void SetUpTestCase() {
60     maps_.FakeClear();
61     MapInfo* info = new MapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so");
62     ElfFake* elf = new ElfFake(new MemoryFake);
63     info->elf.reset(elf);
64     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
65     maps_.FakeAddMapInfo(info);
66 
67     info = new MapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]");
68     maps_.FakeAddMapInfo(info);
69 
70     info = new MapInfo(0x13000, 0x15000, 0, PROT_READ | PROT_WRITE | MAPS_FLAGS_DEVICE_MAP,
71                        "/dev/fake_device");
72     maps_.FakeAddMapInfo(info);
73 
74     info = new MapInfo(0x20000, 0x22000, 0, PROT_READ | PROT_WRITE, "/system/fake/libunwind.so");
75     elf = new ElfFake(new MemoryFake);
76     info->elf.reset(elf);
77     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
78     maps_.FakeAddMapInfo(info);
79 
80     info = new MapInfo(0x23000, 0x24000, 0, PROT_READ | PROT_WRITE, "/fake/libanother.so");
81     elf = new ElfFake(new MemoryFake);
82     info->elf.reset(elf);
83     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
84     maps_.FakeAddMapInfo(info);
85 
86     info = new MapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so");
87     elf = new ElfFake(new MemoryFake);
88     info->elf.reset(elf);
89     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
90     maps_.FakeAddMapInfo(info);
91 
92     info = new MapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk");
93     elf = new ElfFake(new MemoryFake);
94     info->elf.reset(elf);
95     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
96     maps_.FakeAddMapInfo(info);
97 
98     info = new MapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat");
99     maps_.FakeAddMapInfo(info);
100 
101     info = new MapInfo(0xa3000, 0xa4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.vdex");
102     info->load_bias = 0;
103     maps_.FakeAddMapInfo(info);
104 
105     info = new MapInfo(0xa5000, 0xa6000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
106                        "/fake/fake_load_bias.so");
107     elf = new ElfFake(new MemoryFake);
108     info->elf.reset(elf);
109     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
110     elf->FakeSetLoadBias(0x5000);
111     maps_.FakeAddMapInfo(info);
112 
113     info = new MapInfo(0xa7000, 0xa8000, 0, PROT_READ | PROT_WRITE | PROT_EXEC,
114                        "/fake/fake_offset.oat");
115     elf = new ElfFake(new MemoryFake);
116     info->elf.reset(elf);
117     elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
118     info->elf_offset = 0x8000;
119     maps_.FakeAddMapInfo(info);
120 
121     process_memory_.reset(new MemoryFake);
122   }
123 
SetUp()124   void SetUp() override {
125     ElfInterfaceFake::FakeClear();
126     regs_.FakeSetArch(ARCH_ARM);
127     regs_.FakeSetReturnAddressValid(false);
128   }
129 
130   static MapsFake maps_;
131   static RegsFake regs_;
132   static std::shared_ptr<Memory> process_memory_;
133 };
134 
135 MapsFake UnwinderTest::maps_;
136 RegsFake UnwinderTest::regs_(5);
137 std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
138 
TEST_F(UnwinderTest,multiple_frames)139 TEST_F(UnwinderTest, multiple_frames) {
140   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
141   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
142   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
143 
144   regs_.set_pc(0x1000);
145   regs_.set_sp(0x10000);
146   ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
147   ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
148   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
149 
150   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
151   unwinder.Unwind();
152   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
153 
154   ASSERT_EQ(3U, unwinder.NumFrames());
155 
156   auto* frame = &unwinder.frames()[0];
157   EXPECT_EQ(0U, frame->num);
158   EXPECT_EQ(0U, frame->rel_pc);
159   EXPECT_EQ(0x1000U, frame->pc);
160   EXPECT_EQ(0x10000U, frame->sp);
161   EXPECT_EQ("Frame0", frame->function_name);
162   EXPECT_EQ(0U, frame->function_offset);
163   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
164   EXPECT_EQ(0U, frame->map_offset);
165   EXPECT_EQ(0x1000U, frame->map_start);
166   EXPECT_EQ(0x8000U, frame->map_end);
167   EXPECT_EQ(0U, frame->map_load_bias);
168   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
169 
170   frame = &unwinder.frames()[1];
171   EXPECT_EQ(1U, frame->num);
172   EXPECT_EQ(0x100U, frame->rel_pc);
173   EXPECT_EQ(0x1100U, frame->pc);
174   EXPECT_EQ(0x10010U, frame->sp);
175   EXPECT_EQ("Frame1", frame->function_name);
176   EXPECT_EQ(1U, frame->function_offset);
177   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
178   EXPECT_EQ(0U, frame->map_offset);
179   EXPECT_EQ(0x1000U, frame->map_start);
180   EXPECT_EQ(0x8000U, frame->map_end);
181   EXPECT_EQ(0U, frame->map_load_bias);
182   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
183 
184   frame = &unwinder.frames()[2];
185   EXPECT_EQ(2U, frame->num);
186   EXPECT_EQ(0x200U, frame->rel_pc);
187   EXPECT_EQ(0x1200U, frame->pc);
188   EXPECT_EQ(0x10020U, frame->sp);
189   EXPECT_EQ("Frame2", frame->function_name);
190   EXPECT_EQ(2U, frame->function_offset);
191   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
192   EXPECT_EQ(0U, frame->map_offset);
193   EXPECT_EQ(0x1000U, frame->map_start);
194   EXPECT_EQ(0x8000U, frame->map_end);
195   EXPECT_EQ(0U, frame->map_load_bias);
196   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
197 }
198 
TEST_F(UnwinderTest,multiple_frames_dont_resolve_names)199 TEST_F(UnwinderTest, multiple_frames_dont_resolve_names) {
200   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
201   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
202   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
203 
204   regs_.set_pc(0x1000);
205   regs_.set_sp(0x10000);
206   ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
207   ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
208   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
209 
210   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
211   unwinder.SetResolveNames(false);
212   unwinder.Unwind();
213   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
214 
215   ASSERT_EQ(3U, unwinder.NumFrames());
216 
217   auto* frame = &unwinder.frames()[0];
218   EXPECT_EQ(0U, frame->num);
219   EXPECT_EQ(0U, frame->rel_pc);
220   EXPECT_EQ(0x1000U, frame->pc);
221   EXPECT_EQ(0x10000U, frame->sp);
222   EXPECT_EQ("", frame->function_name);
223   EXPECT_EQ(0U, frame->function_offset);
224   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
225   EXPECT_EQ(0U, frame->map_offset);
226   EXPECT_EQ(0x1000U, frame->map_start);
227   EXPECT_EQ(0x8000U, frame->map_end);
228   EXPECT_EQ(0U, frame->map_load_bias);
229   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
230 
231   frame = &unwinder.frames()[1];
232   EXPECT_EQ(1U, frame->num);
233   EXPECT_EQ(0x100U, frame->rel_pc);
234   EXPECT_EQ(0x1100U, frame->pc);
235   EXPECT_EQ(0x10010U, frame->sp);
236   EXPECT_EQ("", frame->function_name);
237   EXPECT_EQ(0U, frame->function_offset);
238   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
239   EXPECT_EQ(0U, frame->map_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("", frame->function_name);
251   EXPECT_EQ(0U, frame->function_offset);
252   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
253   EXPECT_EQ(0U, frame->map_offset);
254   EXPECT_EQ(0x1000U, frame->map_start);
255   EXPECT_EQ(0x8000U, frame->map_end);
256   EXPECT_EQ(0U, frame->map_load_bias);
257   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
258 }
259 
TEST_F(UnwinderTest,non_zero_load_bias)260 TEST_F(UnwinderTest, non_zero_load_bias) {
261   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
262 
263   regs_.set_pc(0xa5500);
264   regs_.set_sp(0x10000);
265   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
266 
267   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
268   unwinder.Unwind();
269   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
270 
271   ASSERT_EQ(1U, unwinder.NumFrames());
272 
273   auto* frame = &unwinder.frames()[0];
274   EXPECT_EQ(0U, frame->num);
275   EXPECT_EQ(0x5500U, frame->rel_pc);
276   EXPECT_EQ(0xa5500U, frame->pc);
277   EXPECT_EQ(0x10000U, frame->sp);
278   EXPECT_EQ("Frame0", frame->function_name);
279   EXPECT_EQ(0U, frame->function_offset);
280   EXPECT_EQ("/fake/fake_load_bias.so", frame->map_name);
281   EXPECT_EQ(0U, frame->map_offset);
282   EXPECT_EQ(0xa5000U, frame->map_start);
283   EXPECT_EQ(0xa6000U, frame->map_end);
284   EXPECT_EQ(0x5000U, frame->map_load_bias);
285   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
286 }
287 
TEST_F(UnwinderTest,non_zero_elf_offset)288 TEST_F(UnwinderTest, non_zero_elf_offset) {
289   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
290 
291   regs_.set_pc(0xa7500);
292   regs_.set_sp(0x10000);
293   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
294 
295   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
296   unwinder.Unwind();
297   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
298 
299   ASSERT_EQ(1U, unwinder.NumFrames());
300 
301   auto* frame = &unwinder.frames()[0];
302   EXPECT_EQ(0U, frame->num);
303   EXPECT_EQ(0x8500U, frame->rel_pc);
304   EXPECT_EQ(0xa7500U, frame->pc);
305   EXPECT_EQ(0x10000U, frame->sp);
306   EXPECT_EQ("Frame0", frame->function_name);
307   EXPECT_EQ(0U, frame->function_offset);
308   EXPECT_EQ("/fake/fake_offset.oat", frame->map_name);
309   EXPECT_EQ(0U, frame->map_offset);
310   EXPECT_EQ(0xa7000U, frame->map_start);
311   EXPECT_EQ(0xa8000U, frame->map_end);
312   EXPECT_EQ(0U, frame->map_load_bias);
313   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
314 }
315 
TEST_F(UnwinderTest,non_zero_map_offset)316 TEST_F(UnwinderTest, non_zero_map_offset) {
317   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
318 
319   regs_.set_pc(0x43000);
320   regs_.set_sp(0x10000);
321   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
322 
323   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
324   unwinder.Unwind();
325   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
326 
327   ASSERT_EQ(1U, unwinder.NumFrames());
328 
329   auto* frame = &unwinder.frames()[0];
330   EXPECT_EQ(0U, frame->num);
331   EXPECT_EQ(0U, frame->rel_pc);
332   EXPECT_EQ(0x43000U, frame->pc);
333   EXPECT_EQ(0x10000U, frame->sp);
334   EXPECT_EQ("Frame0", frame->function_name);
335   EXPECT_EQ(0U, frame->function_offset);
336   EXPECT_EQ("/fake/fake.apk", frame->map_name);
337   EXPECT_EQ(0x1d000U, frame->map_offset);
338   EXPECT_EQ(0x43000U, frame->map_start);
339   EXPECT_EQ(0x44000U, frame->map_end);
340   EXPECT_EQ(0U, frame->map_load_bias);
341   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
342 }
343 
344 // Verify that no attempt to continue after the step indicates it is done.
TEST_F(UnwinderTest,no_frames_after_finished)345 TEST_F(UnwinderTest, no_frames_after_finished) {
346   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
347   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
348   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
349   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
350   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
351 
352   regs_.set_pc(0x1000);
353   regs_.set_sp(0x10000);
354   ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
355   ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
356   ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
357 
358   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
359   unwinder.Unwind();
360   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
361 
362   ASSERT_EQ(1U, unwinder.NumFrames());
363 
364   auto* frame = &unwinder.frames()[0];
365   EXPECT_EQ(0U, frame->num);
366   EXPECT_EQ(0U, frame->rel_pc);
367   EXPECT_EQ(0x1000U, frame->pc);
368   EXPECT_EQ(0x10000U, frame->sp);
369   EXPECT_EQ("Frame0", frame->function_name);
370   EXPECT_EQ(0U, frame->function_offset);
371   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
372   EXPECT_EQ(0U, frame->map_offset);
373   EXPECT_EQ(0x1000U, frame->map_start);
374   EXPECT_EQ(0x8000U, frame->map_end);
375   EXPECT_EQ(0U, frame->map_load_bias);
376   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
377 }
378 
379 // Verify the maximum frames to save.
TEST_F(UnwinderTest,max_frames)380 TEST_F(UnwinderTest, max_frames) {
381   for (size_t i = 0; i < 30; i++) {
382     ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
383     ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
384   }
385 
386   regs_.set_pc(0x1000);
387   regs_.set_sp(0x10000);
388 
389   Unwinder unwinder(20, &maps_, &regs_, process_memory_);
390   unwinder.Unwind();
391   EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode());
392 
393   ASSERT_EQ(20U, unwinder.NumFrames());
394 
395   for (size_t i = 0; i < 20; i++) {
396     auto* frame = &unwinder.frames()[i];
397     EXPECT_EQ(i, frame->num);
398     EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
399     EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
400     EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
401     EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
402     EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
403     EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
404     EXPECT_EQ(0U, frame->map_offset) << "Failed at frame " << i;
405     EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
406     EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
407     EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
408     EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
409   }
410 }
411 
412 // Verify that initial map names frames are removed.
TEST_F(UnwinderTest,verify_frames_skipped)413 TEST_F(UnwinderTest, verify_frames_skipped) {
414   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
415   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
416   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
417 
418   regs_.set_pc(0x20000);
419   regs_.set_sp(0x10000);
420   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
421   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
422   ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
423   ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10040, false));
424   ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
425   ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10060, false));
426   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
427   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
428 
429   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
430   std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
431   unwinder.Unwind(&skip_libs);
432   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
433 
434   ASSERT_EQ(3U, 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(0x1000U, frame->pc);
440   EXPECT_EQ(0x10050U, frame->sp);
441   EXPECT_EQ("Frame0", frame->function_name);
442   EXPECT_EQ(0U, frame->function_offset);
443   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
444   EXPECT_EQ(0U, frame->map_offset);
445   EXPECT_EQ(0x1000U, frame->map_start);
446   EXPECT_EQ(0x8000U, frame->map_end);
447   EXPECT_EQ(0U, frame->map_load_bias);
448   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
449 
450   frame = &unwinder.frames()[1];
451   EXPECT_EQ(1U, frame->num);
452   EXPECT_EQ(0x1000U, frame->rel_pc);
453   EXPECT_EQ(0x21000U, frame->pc);
454   EXPECT_EQ(0x10060U, frame->sp);
455   EXPECT_EQ("Frame1", frame->function_name);
456   EXPECT_EQ(1U, frame->function_offset);
457   EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
458   EXPECT_EQ(0U, frame->map_offset);
459   EXPECT_EQ(0x20000U, frame->map_start);
460   EXPECT_EQ(0x22000U, frame->map_end);
461   EXPECT_EQ(0U, frame->map_load_bias);
462   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
463 
464   frame = &unwinder.frames()[2];
465   EXPECT_EQ(2U, frame->num);
466   EXPECT_EQ(0U, frame->rel_pc);
467   EXPECT_EQ(0x23000U, frame->pc);
468   EXPECT_EQ(0x10070U, frame->sp);
469   EXPECT_EQ("Frame2", frame->function_name);
470   EXPECT_EQ(2U, frame->function_offset);
471   EXPECT_EQ("/fake/libanother.so", frame->map_name);
472   EXPECT_EQ(0U, frame->map_offset);
473   EXPECT_EQ(0x23000U, frame->map_start);
474   EXPECT_EQ(0x24000U, frame->map_end);
475   EXPECT_EQ(0U, frame->map_load_bias);
476   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
477 }
478 
479 // Verify SP in a non-existant map is okay.
TEST_F(UnwinderTest,sp_not_in_map)480 TEST_F(UnwinderTest, sp_not_in_map) {
481   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
482   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
483 
484   regs_.set_pc(0x1000);
485   regs_.set_sp(0x63000);
486   ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
487   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
488 
489   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
490   unwinder.Unwind();
491   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
492 
493   ASSERT_EQ(2U, unwinder.NumFrames());
494 
495   auto* frame = &unwinder.frames()[0];
496   EXPECT_EQ(0U, frame->num);
497   EXPECT_EQ(0U, frame->rel_pc);
498   EXPECT_EQ(0x1000U, frame->pc);
499   EXPECT_EQ(0x63000U, frame->sp);
500   EXPECT_EQ("Frame0", frame->function_name);
501   EXPECT_EQ(0U, frame->function_offset);
502   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
503   EXPECT_EQ(0U, frame->map_offset);
504   EXPECT_EQ(0x1000U, frame->map_start);
505   EXPECT_EQ(0x8000U, frame->map_end);
506   EXPECT_EQ(0U, frame->map_load_bias);
507   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
508 
509   frame = &unwinder.frames()[1];
510   EXPECT_EQ(1U, frame->num);
511   EXPECT_EQ(0x1000U, frame->rel_pc);
512   EXPECT_EQ(0x21000U, frame->pc);
513   EXPECT_EQ(0x50020U, frame->sp);
514   EXPECT_EQ("Frame1", frame->function_name);
515   EXPECT_EQ(1U, frame->function_offset);
516   EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
517   EXPECT_EQ(0U, frame->map_offset);
518   EXPECT_EQ(0x20000U, frame->map_start);
519   EXPECT_EQ(0x22000U, frame->map_end);
520   EXPECT_EQ(0U, frame->map_load_bias);
521   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
522 }
523 
524 // Verify PC in a device stops the unwind.
TEST_F(UnwinderTest,pc_in_device_stops_unwind)525 TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
526   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
527   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
528   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
529 
530   regs_.set_pc(0x13000);
531   regs_.set_sp(0x10000);
532   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
533   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
534   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
535 
536   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
537   unwinder.Unwind();
538   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
539 
540   ASSERT_EQ(1U, unwinder.NumFrames());
541 }
542 
543 // Verify SP in a device stops the unwind.
TEST_F(UnwinderTest,sp_in_device_stops_unwind)544 TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
545   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
546   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
547   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
548 
549   regs_.set_pc(0x1000);
550   regs_.set_sp(0x13000);
551   ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
552   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
553   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
554 
555   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
556   unwinder.Unwind();
557   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
558 
559   ASSERT_EQ(1U, unwinder.NumFrames());
560 }
561 
562 // Verify a no map info frame gets a frame.
TEST_F(UnwinderTest,pc_without_map)563 TEST_F(UnwinderTest, pc_without_map) {
564   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
565 
566   regs_.set_pc(0x41000);
567   regs_.set_sp(0x13000);
568 
569   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
570   unwinder.Unwind();
571   EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode());
572 
573   ASSERT_EQ(1U, unwinder.NumFrames());
574 
575   auto* frame = &unwinder.frames()[0];
576   EXPECT_EQ(0U, frame->num);
577   EXPECT_EQ(0x41000U, frame->rel_pc);
578   EXPECT_EQ(0x41000U, frame->pc);
579   EXPECT_EQ(0x13000U, frame->sp);
580   EXPECT_EQ("", frame->function_name);
581   EXPECT_EQ(0U, frame->function_offset);
582   EXPECT_EQ("", frame->map_name);
583   EXPECT_EQ(0U, frame->map_offset);
584   EXPECT_EQ(0U, frame->map_start);
585   EXPECT_EQ(0U, frame->map_end);
586   EXPECT_EQ(0U, frame->map_load_bias);
587   EXPECT_EQ(0, frame->map_flags);
588 }
589 
590 // Verify that a speculative frame is added.
TEST_F(UnwinderTest,speculative_frame)591 TEST_F(UnwinderTest, speculative_frame) {
592   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
593   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
594 
595   // Fake as if code called a nullptr function.
596   regs_.set_pc(0);
597   regs_.set_sp(0x10000);
598   regs_.FakeSetReturnAddress(0x1202);
599   regs_.FakeSetReturnAddressValid(true);
600 
601   ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
602   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
603 
604   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
605   unwinder.Unwind();
606   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
607 
608   ASSERT_EQ(3U, unwinder.NumFrames());
609 
610   auto* frame = &unwinder.frames()[0];
611   EXPECT_EQ(0U, frame->num);
612   EXPECT_EQ(0U, frame->rel_pc);
613   EXPECT_EQ(0U, frame->pc);
614   EXPECT_EQ(0x10000U, frame->sp);
615   EXPECT_EQ("", frame->function_name);
616   EXPECT_EQ(0U, frame->function_offset);
617   EXPECT_EQ("", frame->map_name);
618   EXPECT_EQ(0U, frame->map_offset);
619   EXPECT_EQ(0U, frame->map_start);
620   EXPECT_EQ(0U, frame->map_end);
621   EXPECT_EQ(0U, frame->map_load_bias);
622   EXPECT_EQ(0, frame->map_flags);
623 
624   frame = &unwinder.frames()[1];
625   EXPECT_EQ(1U, frame->num);
626   EXPECT_EQ(0x200U, frame->rel_pc);
627   EXPECT_EQ(0x1200U, frame->pc);
628   EXPECT_EQ(0x10000U, frame->sp);
629   EXPECT_EQ("Frame0", frame->function_name);
630   EXPECT_EQ(0U, frame->function_offset);
631   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
632   EXPECT_EQ(0U, frame->map_offset);
633   EXPECT_EQ(0x1000U, frame->map_start);
634   EXPECT_EQ(0x8000U, frame->map_end);
635   EXPECT_EQ(0U, frame->map_load_bias);
636   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
637 
638   frame = &unwinder.frames()[2];
639   EXPECT_EQ(2U, frame->num);
640   EXPECT_EQ(0x100U, frame->rel_pc);
641   EXPECT_EQ(0x23100U, frame->pc);
642   EXPECT_EQ(0x10020U, frame->sp);
643   EXPECT_EQ("Frame1", frame->function_name);
644   EXPECT_EQ(1U, frame->function_offset);
645   EXPECT_EQ("/fake/libanother.so", frame->map_name);
646   EXPECT_EQ(0U, frame->map_offset);
647   EXPECT_EQ(0x23000U, frame->map_start);
648   EXPECT_EQ(0x24000U, frame->map_end);
649   EXPECT_EQ(0U, frame->map_load_bias);
650   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
651 }
652 
653 // Verify that a speculative frame is added then removed because no other
654 // frames are added.
TEST_F(UnwinderTest,speculative_frame_removed)655 TEST_F(UnwinderTest, speculative_frame_removed) {
656   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
657   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
658 
659   // Fake as if code called a nullptr function.
660   regs_.set_pc(0);
661   regs_.set_sp(0x10000);
662   regs_.FakeSetReturnAddress(0x1202);
663   regs_.FakeSetReturnAddressValid(true);
664 
665   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
666   unwinder.Unwind();
667   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
668 
669   ASSERT_EQ(1U, unwinder.NumFrames());
670 
671   auto* frame = &unwinder.frames()[0];
672   EXPECT_EQ(0U, frame->num);
673   EXPECT_EQ(0U, frame->rel_pc);
674   EXPECT_EQ(0U, frame->pc);
675   EXPECT_EQ(0x10000U, frame->sp);
676   EXPECT_EQ("", frame->function_name);
677   EXPECT_EQ(0U, frame->function_offset);
678   EXPECT_EQ("", frame->map_name);
679   EXPECT_EQ(0U, frame->map_offset);
680   EXPECT_EQ(0U, frame->map_start);
681   EXPECT_EQ(0U, frame->map_end);
682   EXPECT_EQ(0U, frame->map_load_bias);
683   EXPECT_EQ(0, frame->map_flags);
684 }
685 
686 // Verify that an unwind stops when a frame is in given suffix.
TEST_F(UnwinderTest,map_ignore_suffixes)687 TEST_F(UnwinderTest, map_ignore_suffixes) {
688   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
689   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
690   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
691   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
692 
693   // Fake as if code called a nullptr function.
694   regs_.set_pc(0x1000);
695   regs_.set_sp(0x10000);
696   ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
697   ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
698   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
699 
700   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
701   std::vector<std::string> suffixes{"oat"};
702   unwinder.Unwind(nullptr, &suffixes);
703   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
704 
705   ASSERT_EQ(2U, unwinder.NumFrames());
706   // Make sure the elf was not initialized.
707   MapInfo* map_info = maps_.Find(0x53000);
708   ASSERT_TRUE(map_info != nullptr);
709   EXPECT_TRUE(map_info->elf == nullptr);
710 
711   auto* frame = &unwinder.frames()[0];
712   EXPECT_EQ(0U, frame->num);
713   EXPECT_EQ(0U, frame->rel_pc);
714   EXPECT_EQ(0x1000U, frame->pc);
715   EXPECT_EQ(0x10000U, frame->sp);
716   EXPECT_EQ("Frame0", frame->function_name);
717   EXPECT_EQ(0U, frame->function_offset);
718   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
719   EXPECT_EQ(0U, frame->map_offset);
720   EXPECT_EQ(0x1000U, frame->map_start);
721   EXPECT_EQ(0x8000U, frame->map_end);
722   EXPECT_EQ(0U, frame->map_load_bias);
723   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
724 
725   frame = &unwinder.frames()[1];
726   EXPECT_EQ(1U, frame->num);
727   EXPECT_EQ(0x400U, frame->rel_pc);
728   EXPECT_EQ(0x43400U, frame->pc);
729   EXPECT_EQ(0x10010U, frame->sp);
730   EXPECT_EQ("Frame1", frame->function_name);
731   EXPECT_EQ(1U, frame->function_offset);
732   EXPECT_EQ("/fake/fake.apk", frame->map_name);
733   EXPECT_EQ(0x1d000U, frame->map_offset);
734   EXPECT_EQ(0x43000U, frame->map_start);
735   EXPECT_EQ(0x44000U, frame->map_end);
736   EXPECT_EQ(0U, frame->map_load_bias);
737   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
738 }
739 
740 // Verify that an unwind stops when the sp and pc don't change.
TEST_F(UnwinderTest,sp_pc_do_not_change)741 TEST_F(UnwinderTest, sp_pc_do_not_change) {
742   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
743   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
744   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
745   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
746   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
747 
748   regs_.set_pc(0x1000);
749   regs_.set_sp(0x10000);
750   ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
751   ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
752   ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
753   ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
754   ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
755   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
756 
757   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
758   unwinder.Unwind();
759   EXPECT_EQ(ERROR_REPEATED_FRAME, unwinder.LastErrorCode());
760 
761   ASSERT_EQ(3U, unwinder.NumFrames());
762 
763   auto* frame = &unwinder.frames()[0];
764   EXPECT_EQ(0U, frame->num);
765   EXPECT_EQ(0U, frame->rel_pc);
766   EXPECT_EQ(0x1000U, frame->pc);
767   EXPECT_EQ(0x10000U, frame->sp);
768   EXPECT_EQ("Frame0", frame->function_name);
769   EXPECT_EQ(0U, frame->function_offset);
770   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
771   EXPECT_EQ(0U, frame->map_offset);
772   EXPECT_EQ(0x1000U, frame->map_start);
773   EXPECT_EQ(0x8000U, frame->map_end);
774   EXPECT_EQ(0U, frame->map_load_bias);
775   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
776 
777   frame = &unwinder.frames()[1];
778   EXPECT_EQ(1U, frame->num);
779   EXPECT_EQ(0x400U, frame->rel_pc);
780   EXPECT_EQ(0x33400U, frame->pc);
781   EXPECT_EQ(0x10010U, frame->sp);
782   EXPECT_EQ("Frame1", frame->function_name);
783   EXPECT_EQ(1U, frame->function_offset);
784   EXPECT_EQ("/fake/compressed.so", frame->map_name);
785   EXPECT_EQ(0U, frame->map_offset);
786   EXPECT_EQ(0x33000U, frame->map_start);
787   EXPECT_EQ(0x34000U, frame->map_end);
788   EXPECT_EQ(0U, frame->map_load_bias);
789   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
790 
791   frame = &unwinder.frames()[2];
792   EXPECT_EQ(2U, frame->num);
793   EXPECT_EQ(0x500U, frame->rel_pc);
794   EXPECT_EQ(0x33500U, frame->pc);
795   EXPECT_EQ(0x10020U, frame->sp);
796   EXPECT_EQ("Frame2", frame->function_name);
797   EXPECT_EQ(2U, frame->function_offset);
798   EXPECT_EQ("/fake/compressed.so", frame->map_name);
799   EXPECT_EQ(0U, frame->map_offset);
800   EXPECT_EQ(0x33000U, frame->map_start);
801   EXPECT_EQ(0x34000U, frame->map_end);
802   EXPECT_EQ(0U, frame->map_load_bias);
803   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
804 }
805 
TEST_F(UnwinderTest,dex_pc_in_map)806 TEST_F(UnwinderTest, dex_pc_in_map) {
807   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
808   regs_.set_pc(0x1000);
809   regs_.set_sp(0x10000);
810   regs_.FakeSetDexPc(0xa3400);
811 
812   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
813   unwinder.Unwind();
814   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
815 
816   ASSERT_EQ(2U, unwinder.NumFrames());
817 
818   auto* frame = &unwinder.frames()[0];
819   EXPECT_EQ(0U, frame->num);
820   EXPECT_EQ(0x400U, frame->rel_pc);
821   EXPECT_EQ(0xa3400U, frame->pc);
822   EXPECT_EQ(0x10000U, frame->sp);
823   EXPECT_EQ("", frame->function_name);
824   EXPECT_EQ(0U, frame->function_offset);
825   EXPECT_EQ("/fake/fake.vdex", frame->map_name);
826   EXPECT_EQ(0U, frame->map_offset);
827   EXPECT_EQ(0xa3000U, frame->map_start);
828   EXPECT_EQ(0xa4000U, frame->map_end);
829   EXPECT_EQ(0U, frame->map_load_bias);
830   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
831 
832   frame = &unwinder.frames()[1];
833   EXPECT_EQ(1U, frame->num);
834   EXPECT_EQ(0U, frame->rel_pc);
835   EXPECT_EQ(0x1000U, frame->pc);
836   EXPECT_EQ(0x10000U, frame->sp);
837   EXPECT_EQ("Frame0", frame->function_name);
838   EXPECT_EQ(0U, frame->function_offset);
839   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
840   EXPECT_EQ(0U, frame->map_offset);
841   EXPECT_EQ(0x1000U, frame->map_start);
842   EXPECT_EQ(0x8000U, frame->map_end);
843   EXPECT_EQ(0U, frame->map_load_bias);
844   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
845 }
846 
TEST_F(UnwinderTest,dex_pc_not_in_map)847 TEST_F(UnwinderTest, dex_pc_not_in_map) {
848   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
849   regs_.set_pc(0x1000);
850   regs_.set_sp(0x10000);
851   regs_.FakeSetDexPc(0x50000);
852 
853   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
854   unwinder.Unwind();
855   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
856 
857   ASSERT_EQ(2U, unwinder.NumFrames());
858 
859   auto* frame = &unwinder.frames()[0];
860   EXPECT_EQ(0U, frame->num);
861   EXPECT_EQ(0x50000U, frame->rel_pc);
862   EXPECT_EQ(0x50000U, frame->pc);
863   EXPECT_EQ(0x10000U, frame->sp);
864   EXPECT_EQ("", frame->function_name);
865   EXPECT_EQ(0U, frame->function_offset);
866   EXPECT_EQ("", frame->map_name);
867   EXPECT_EQ(0U, frame->map_offset);
868   EXPECT_EQ(0U, frame->map_start);
869   EXPECT_EQ(0U, frame->map_end);
870   EXPECT_EQ(0U, frame->map_load_bias);
871   EXPECT_EQ(0, frame->map_flags);
872 
873   frame = &unwinder.frames()[1];
874   EXPECT_EQ(1U, frame->num);
875   EXPECT_EQ(0U, frame->rel_pc);
876   EXPECT_EQ(0x1000U, frame->pc);
877   EXPECT_EQ(0x10000U, frame->sp);
878   EXPECT_EQ("Frame0", frame->function_name);
879   EXPECT_EQ(0U, frame->function_offset);
880   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
881   EXPECT_EQ(0U, frame->map_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 
TEST_F(UnwinderTest,dex_pc_multiple_frames)888 TEST_F(UnwinderTest, dex_pc_multiple_frames) {
889   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
890   ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
891   regs_.set_pc(0x1000);
892   regs_.set_sp(0x10000);
893   regs_.FakeSetDexPc(0xa3400);
894   ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
895   ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
896 
897   Unwinder unwinder(64, &maps_, &regs_, process_memory_);
898   unwinder.Unwind();
899   EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
900 
901   ASSERT_EQ(3U, unwinder.NumFrames());
902 
903   auto* frame = &unwinder.frames()[0];
904   EXPECT_EQ(0U, frame->num);
905   EXPECT_EQ(0x400U, frame->rel_pc);
906   EXPECT_EQ(0xa3400U, frame->pc);
907   EXPECT_EQ(0x10000U, frame->sp);
908   EXPECT_EQ("", frame->function_name);
909   EXPECT_EQ(0U, frame->function_offset);
910   EXPECT_EQ("/fake/fake.vdex", frame->map_name);
911   EXPECT_EQ(0U, frame->map_offset);
912   EXPECT_EQ(0xa3000U, frame->map_start);
913   EXPECT_EQ(0xa4000U, frame->map_end);
914   EXPECT_EQ(0U, frame->map_load_bias);
915   EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame->map_flags);
916 
917   frame = &unwinder.frames()[1];
918   EXPECT_EQ(1U, frame->num);
919   EXPECT_EQ(0U, frame->rel_pc);
920   EXPECT_EQ(0x1000U, frame->pc);
921   EXPECT_EQ(0x10000U, frame->sp);
922   EXPECT_EQ("Frame0", frame->function_name);
923   EXPECT_EQ(0U, frame->function_offset);
924   EXPECT_EQ("/system/fake/libc.so", frame->map_name);
925   EXPECT_EQ(0U, frame->map_offset);
926   EXPECT_EQ(0x1000U, frame->map_start);
927   EXPECT_EQ(0x8000U, frame->map_end);
928   EXPECT_EQ(0U, frame->map_load_bias);
929   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
930 
931   frame = &unwinder.frames()[2];
932   EXPECT_EQ(2U, frame->num);
933   EXPECT_EQ(0x400U, frame->rel_pc);
934   EXPECT_EQ(0x33400U, frame->pc);
935   EXPECT_EQ(0x10010U, frame->sp);
936   EXPECT_EQ("Frame1", frame->function_name);
937   EXPECT_EQ(1U, frame->function_offset);
938   EXPECT_EQ("/fake/compressed.so", frame->map_name);
939   EXPECT_EQ(0U, frame->map_offset);
940   EXPECT_EQ(0x33000U, frame->map_start);
941   EXPECT_EQ(0x34000U, frame->map_end);
942   EXPECT_EQ(0U, frame->map_load_bias);
943   EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
944 }
945 
946 // Verify format frame code.
TEST_F(UnwinderTest,format_frame_static)947 TEST_F(UnwinderTest, format_frame_static) {
948   FrameData frame;
949   frame.num = 1;
950   frame.rel_pc = 0x1000;
951   frame.pc = 0x4000;
952   frame.sp = 0x1000;
953   frame.function_name = "function";
954   frame.function_offset = 100;
955   frame.map_name = "/fake/libfake.so";
956   frame.map_offset = 0x2000;
957   frame.map_start = 0x3000;
958   frame.map_end = 0x6000;
959   frame.map_flags = PROT_READ;
960 
961   EXPECT_EQ("  #01 pc 0000000000001000 (offset 0x2000)  /fake/libfake.so (function+100)",
962             Unwinder::FormatFrame(frame, false));
963   EXPECT_EQ("  #01 pc 00001000 (offset 0x2000)  /fake/libfake.so (function+100)",
964             Unwinder::FormatFrame(frame, true));
965 
966   frame.map_offset = 0;
967   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (function+100)",
968             Unwinder::FormatFrame(frame, false));
969   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function+100)",
970             Unwinder::FormatFrame(frame, true));
971 
972   frame.function_offset = 0;
973   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so (function)",
974             Unwinder::FormatFrame(frame, false));
975   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
976 
977   frame.function_name = "";
978   EXPECT_EQ("  #01 pc 0000000000001000  /fake/libfake.so", Unwinder::FormatFrame(frame, false));
979   EXPECT_EQ("  #01 pc 00001000  /fake/libfake.so", Unwinder::FormatFrame(frame, true));
980 
981   frame.map_name = "";
982   EXPECT_EQ("  #01 pc 0000000000001000  <anonymous:3000>", Unwinder::FormatFrame(frame, false));
983   EXPECT_EQ("  #01 pc 00001000  <anonymous:3000>", Unwinder::FormatFrame(frame, true));
984 
985   frame.map_start = 0;
986   frame.map_end = 0;
987   EXPECT_EQ("  #01 pc 0000000000001000  <unknown>", Unwinder::FormatFrame(frame, false));
988   EXPECT_EQ("  #01 pc 00001000  <unknown>", Unwinder::FormatFrame(frame, true));
989 }
990 
ArchToString(ArchEnum arch)991 static std::string ArchToString(ArchEnum arch) {
992   if (arch == ARCH_ARM) {
993     return "Arm";
994   } else if (arch == ARCH_ARM64) {
995     return "Arm64";
996   } else if (arch == ARCH_X86) {
997     return "X86";
998   } else if (arch == ARCH_X86_64) {
999     return "X86_64";
1000   } else {
1001     return "Unknown";
1002   }
1003 }
1004 
1005 // Verify format frame code.
TEST_F(UnwinderTest,format_frame)1006 TEST_F(UnwinderTest, format_frame) {
1007   std::vector<Regs*> reg_list;
1008   RegsArm* arm = new RegsArm;
1009   arm->set_pc(0x2300);
1010   arm->set_sp(0x10000);
1011   reg_list.push_back(arm);
1012 
1013   RegsArm64* arm64 = new RegsArm64;
1014   arm64->set_pc(0x2300);
1015   arm64->set_sp(0x10000);
1016   reg_list.push_back(arm64);
1017 
1018   RegsX86* x86 = new RegsX86;
1019   x86->set_pc(0x2300);
1020   x86->set_sp(0x10000);
1021   reg_list.push_back(x86);
1022 
1023   RegsX86_64* x86_64 = new RegsX86_64;
1024   x86_64->set_pc(0x2300);
1025   x86_64->set_sp(0x10000);
1026   reg_list.push_back(x86_64);
1027 
1028   RegsMips* mips = new RegsMips;
1029   mips->set_pc(0x2300);
1030   mips->set_sp(0x10000);
1031   reg_list.push_back(mips);
1032 
1033   RegsMips64* mips64 = new RegsMips64;
1034   mips64->set_pc(0x2300);
1035   mips64->set_sp(0x10000);
1036   reg_list.push_back(mips64);
1037 
1038   for (auto regs : reg_list) {
1039     ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
1040 
1041     Unwinder unwinder(64, &maps_, regs, process_memory_);
1042     unwinder.Unwind();
1043 
1044     ASSERT_EQ(1U, unwinder.NumFrames());
1045     std::string expected;
1046     switch (regs->Arch()) {
1047       case ARCH_ARM:
1048       case ARCH_X86:
1049       case ARCH_MIPS:
1050         expected = "  #00 pc 00001300  /system/fake/libc.so (Frame0+10)";
1051         break;
1052       case ARCH_ARM64:
1053       case ARCH_X86_64:
1054       case ARCH_MIPS64:
1055         expected = "  #00 pc 0000000000001300  /system/fake/libc.so (Frame0+10)";
1056         break;
1057       default:
1058         expected = "";
1059     }
1060     EXPECT_EQ(expected, unwinder.FormatFrame(0))
1061         << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
1062     delete regs;
1063   }
1064 }
1065 
1066 }  // namespace unwindstack
1067