blob: 2034191e2de4b0d92da8bea40c2186b71e6b73cc [file] [log] [blame]
Christopher Ferrisf6f691b2017-09-25 19:23:07 -07001/*
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>
Christopher Ferris02fdb562017-12-08 15:04:49 -080031#include <unwindstack/RegsArm.h>
32#include <unwindstack/RegsArm64.h>
33#include <unwindstack/RegsX86.h>
34#include <unwindstack/RegsX86_64.h>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070035#include <unwindstack/Unwinder.h>
36
37#include "ElfFake.h"
38#include "MemoryFake.h"
39#include "RegsFake.h"
40
41namespace unwindstack {
42
43class MapsFake : public Maps {
44 public:
45 MapsFake() = default;
46 virtual ~MapsFake() = default;
47
48 bool Parse() { return true; }
49
50 void FakeClear() { maps_.clear(); }
51
Christopher Ferrisbe788d82017-11-27 14:50:38 -080052 void FakeAddMapInfo(MapInfo* map_info) { maps_.push_back(map_info); }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070053};
54
55class UnwinderTest : public ::testing::Test {
56 protected:
57 static void SetUpTestCase() {
58 maps_.FakeClear();
Christopher Ferrisbe788d82017-11-27 14:50:38 -080059 MapInfo* info = new MapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080060 ElfFake* elf = new ElfFake(new MemoryFake);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080061 info->elf = elf;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070062 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070063 maps_.FakeAddMapInfo(info);
64
Christopher Ferrisbe788d82017-11-27 14:50:38 -080065 info = new MapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070066 maps_.FakeAddMapInfo(info);
67
Christopher Ferrisbe788d82017-11-27 14:50:38 -080068 info = new MapInfo(0x13000, 0x15000, 0, PROT_READ | PROT_WRITE | MAPS_FLAGS_DEVICE_MAP,
69 "/dev/fake_device");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070070 maps_.FakeAddMapInfo(info);
71
Christopher Ferrisbe788d82017-11-27 14:50:38 -080072 info = new MapInfo(0x20000, 0x22000, 0, PROT_READ | PROT_WRITE, "/system/fake/libunwind.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080073 elf = new ElfFake(new MemoryFake);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080074 info->elf = elf;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070075 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
76 maps_.FakeAddMapInfo(info);
77
Christopher Ferrisbe788d82017-11-27 14:50:38 -080078 info = new MapInfo(0x23000, 0x24000, 0, PROT_READ | PROT_WRITE, "/fake/libanother.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080079 elf = new ElfFake(new MemoryFake);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080080 info->elf = elf;
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070081 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
82 maps_.FakeAddMapInfo(info);
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070083
Christopher Ferrisbe788d82017-11-27 14:50:38 -080084 info = new MapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080085 elf = new ElfFake(new MemoryFake);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080086 info->elf = elf;
Christopher Ferrise69f4702017-10-19 16:08:58 -070087 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
88 maps_.FakeAddMapInfo(info);
89
Christopher Ferrisbe788d82017-11-27 14:50:38 -080090 info = new MapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk");
Christopher Ferris02fdb562017-12-08 15:04:49 -080091 elf = new ElfFake(new MemoryFake);
Christopher Ferrisbe788d82017-11-27 14:50:38 -080092 info->elf = elf;
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070093 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
94 maps_.FakeAddMapInfo(info);
Christopher Ferrise69f4702017-10-19 16:08:58 -070095
Christopher Ferrisbe788d82017-11-27 14:50:38 -080096 info = new MapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat");
Christopher Ferrise69f4702017-10-19 16:08:58 -070097 maps_.FakeAddMapInfo(info);
Christopher Ferris02fdb562017-12-08 15:04:49 -080098
99 process_memory_.reset(new MemoryFake);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700100 }
101
102 void SetUp() override {
103 ElfInterfaceFake::FakeClear();
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800104 regs_.FakeSetArch(ARCH_ARM);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700105 regs_.FakeSetReturnAddressValid(false);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700106 }
107
108 static MapsFake maps_;
109 static RegsFake regs_;
110 static std::shared_ptr<Memory> process_memory_;
111};
112
113MapsFake UnwinderTest::maps_;
114RegsFake UnwinderTest::regs_(5, 0);
115std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
116
117TEST_F(UnwinderTest, multiple_frames) {
118 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
119 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
120 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
121
122 regs_.FakeSetPc(0x1000);
123 regs_.FakeSetSp(0x10000);
124 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
125 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
126 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
127
128 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
129 unwinder.Unwind();
130
131 ASSERT_EQ(3U, unwinder.NumFrames());
132
133 auto* frame = &unwinder.frames()[0];
134 EXPECT_EQ(0U, frame->num);
135 EXPECT_EQ(0U, frame->rel_pc);
136 EXPECT_EQ(0x1000U, frame->pc);
137 EXPECT_EQ(0x10000U, frame->sp);
138 EXPECT_EQ("Frame0", frame->function_name);
139 EXPECT_EQ(0U, frame->function_offset);
140 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
141 EXPECT_EQ(0U, frame->map_offset);
142 EXPECT_EQ(0x1000U, frame->map_start);
143 EXPECT_EQ(0x8000U, frame->map_end);
144 EXPECT_EQ(0U, frame->map_load_bias);
145 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
146
147 frame = &unwinder.frames()[1];
148 EXPECT_EQ(1U, frame->num);
149 EXPECT_EQ(0x100U, frame->rel_pc);
150 EXPECT_EQ(0x1100U, frame->pc);
151 EXPECT_EQ(0x10010U, frame->sp);
152 EXPECT_EQ("Frame1", frame->function_name);
153 EXPECT_EQ(1U, frame->function_offset);
154 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
155 EXPECT_EQ(0U, frame->map_offset);
156 EXPECT_EQ(0x1000U, frame->map_start);
157 EXPECT_EQ(0x8000U, frame->map_end);
158 EXPECT_EQ(0U, frame->map_load_bias);
159 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
160
161 frame = &unwinder.frames()[2];
162 EXPECT_EQ(2U, frame->num);
163 EXPECT_EQ(0x200U, frame->rel_pc);
164 EXPECT_EQ(0x1200U, frame->pc);
165 EXPECT_EQ(0x10020U, frame->sp);
166 EXPECT_EQ("Frame2", frame->function_name);
167 EXPECT_EQ(2U, frame->function_offset);
168 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
169 EXPECT_EQ(0U, frame->map_offset);
170 EXPECT_EQ(0x1000U, frame->map_start);
171 EXPECT_EQ(0x8000U, frame->map_end);
172 EXPECT_EQ(0U, frame->map_load_bias);
173 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
174}
175
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700176TEST_F(UnwinderTest, non_zero_map_offset) {
177 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
178
179 regs_.FakeSetPc(0x43000);
180 regs_.FakeSetSp(0x10000);
181 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
182
183 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
184 unwinder.Unwind();
185
186 ASSERT_EQ(1U, unwinder.NumFrames());
187
188 auto* frame = &unwinder.frames()[0];
189 EXPECT_EQ(0U, frame->num);
190 EXPECT_EQ(0U, frame->rel_pc);
191 EXPECT_EQ(0x43000U, frame->pc);
192 EXPECT_EQ(0x10000U, frame->sp);
193 EXPECT_EQ("Frame0", frame->function_name);
194 EXPECT_EQ(0U, frame->function_offset);
195 EXPECT_EQ("/fake/fake.apk", frame->map_name);
196 EXPECT_EQ(0x1d000U, frame->map_offset);
197 EXPECT_EQ(0x43000U, frame->map_start);
198 EXPECT_EQ(0x44000U, frame->map_end);
199 EXPECT_EQ(0U, frame->map_load_bias);
200 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
201}
202
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700203// Verify that no attempt to continue after the step indicates it is done.
204TEST_F(UnwinderTest, no_frames_after_finished) {
205 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
206 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
207 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
208 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
209 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
210
211 regs_.FakeSetPc(0x1000);
212 regs_.FakeSetSp(0x10000);
213 ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
214 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
215 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
216
217 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
218 unwinder.Unwind();
219
220 ASSERT_EQ(1U, unwinder.NumFrames());
221
222 auto* frame = &unwinder.frames()[0];
223 EXPECT_EQ(0U, frame->num);
224 EXPECT_EQ(0U, frame->rel_pc);
225 EXPECT_EQ(0x1000U, frame->pc);
226 EXPECT_EQ(0x10000U, frame->sp);
227 EXPECT_EQ("Frame0", frame->function_name);
228 EXPECT_EQ(0U, frame->function_offset);
229 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
230 EXPECT_EQ(0U, frame->map_offset);
231 EXPECT_EQ(0x1000U, frame->map_start);
232 EXPECT_EQ(0x8000U, frame->map_end);
233 EXPECT_EQ(0U, frame->map_load_bias);
234 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
235}
236
237// Verify the maximum frames to save.
238TEST_F(UnwinderTest, max_frames) {
239 for (size_t i = 0; i < 30; i++) {
240 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
241 ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
242 }
243
244 regs_.FakeSetPc(0x1000);
245 regs_.FakeSetSp(0x10000);
246
247 Unwinder unwinder(20, &maps_, &regs_, process_memory_);
248 unwinder.Unwind();
249
250 ASSERT_EQ(20U, unwinder.NumFrames());
251
252 for (size_t i = 0; i < 20; i++) {
253 auto* frame = &unwinder.frames()[i];
254 EXPECT_EQ(i, frame->num);
255 EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
256 EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
257 EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
258 EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
259 EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
260 EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
261 EXPECT_EQ(0U, frame->map_offset) << "Failed at frame " << i;
262 EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
263 EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
264 EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
265 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
266 }
267}
268
269// Verify that initial map names frames are removed.
270TEST_F(UnwinderTest, verify_frames_skipped) {
271 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
272 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
273 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
274
275 regs_.FakeSetPc(0x20000);
276 regs_.FakeSetSp(0x10000);
277 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
278 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
279 ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
280 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10040, false));
281 ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
282 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10060, false));
283 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
284 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
285
286 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700287 std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
288 unwinder.Unwind(&skip_libs);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700289
290 ASSERT_EQ(3U, unwinder.NumFrames());
291
292 auto* frame = &unwinder.frames()[0];
293 EXPECT_EQ(0U, frame->num);
294 EXPECT_EQ(0U, frame->rel_pc);
295 EXPECT_EQ(0x1000U, frame->pc);
296 EXPECT_EQ(0x10050U, frame->sp);
297 EXPECT_EQ("Frame0", frame->function_name);
298 EXPECT_EQ(0U, frame->function_offset);
299 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
300 EXPECT_EQ(0U, frame->map_offset);
301 EXPECT_EQ(0x1000U, frame->map_start);
302 EXPECT_EQ(0x8000U, frame->map_end);
303 EXPECT_EQ(0U, frame->map_load_bias);
304 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
305
306 frame = &unwinder.frames()[1];
307 EXPECT_EQ(1U, frame->num);
308 EXPECT_EQ(0x1000U, frame->rel_pc);
309 EXPECT_EQ(0x21000U, frame->pc);
310 EXPECT_EQ(0x10060U, frame->sp);
311 EXPECT_EQ("Frame1", frame->function_name);
312 EXPECT_EQ(1U, frame->function_offset);
313 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
314 EXPECT_EQ(0U, frame->map_offset);
315 EXPECT_EQ(0x20000U, frame->map_start);
316 EXPECT_EQ(0x22000U, frame->map_end);
317 EXPECT_EQ(0U, frame->map_load_bias);
318 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
319
320 frame = &unwinder.frames()[2];
321 EXPECT_EQ(2U, frame->num);
322 EXPECT_EQ(0U, frame->rel_pc);
323 EXPECT_EQ(0x23000U, frame->pc);
324 EXPECT_EQ(0x10070U, frame->sp);
325 EXPECT_EQ("Frame2", frame->function_name);
326 EXPECT_EQ(2U, frame->function_offset);
327 EXPECT_EQ("/fake/libanother.so", frame->map_name);
328 EXPECT_EQ(0U, frame->map_offset);
329 EXPECT_EQ(0x23000U, frame->map_start);
330 EXPECT_EQ(0x24000U, frame->map_end);
331 EXPECT_EQ(0U, frame->map_load_bias);
332 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
333}
334
335// Verify SP in a non-existant map is okay.
336TEST_F(UnwinderTest, sp_not_in_map) {
337 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
338 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
339
340 regs_.FakeSetPc(0x1000);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700341 regs_.FakeSetSp(0x63000);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700342 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
343 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
344
345 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
346 unwinder.Unwind();
347
348 ASSERT_EQ(2U, unwinder.NumFrames());
349
350 auto* frame = &unwinder.frames()[0];
351 EXPECT_EQ(0U, frame->num);
352 EXPECT_EQ(0U, frame->rel_pc);
353 EXPECT_EQ(0x1000U, frame->pc);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700354 EXPECT_EQ(0x63000U, frame->sp);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700355 EXPECT_EQ("Frame0", frame->function_name);
356 EXPECT_EQ(0U, frame->function_offset);
357 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
358 EXPECT_EQ(0U, frame->map_offset);
359 EXPECT_EQ(0x1000U, frame->map_start);
360 EXPECT_EQ(0x8000U, frame->map_end);
361 EXPECT_EQ(0U, frame->map_load_bias);
362 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
363
364 frame = &unwinder.frames()[1];
365 EXPECT_EQ(1U, frame->num);
366 EXPECT_EQ(0x1000U, frame->rel_pc);
367 EXPECT_EQ(0x21000U, frame->pc);
368 EXPECT_EQ(0x50020U, frame->sp);
369 EXPECT_EQ("Frame1", frame->function_name);
370 EXPECT_EQ(1U, frame->function_offset);
371 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
372 EXPECT_EQ(0U, frame->map_offset);
373 EXPECT_EQ(0x20000U, frame->map_start);
374 EXPECT_EQ(0x22000U, 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 PC in a device stops the unwind.
380TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
381 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
382 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
383 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
384
385 regs_.FakeSetPc(0x13000);
386 regs_.FakeSetSp(0x10000);
387 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
388 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
389 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
390
391 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
392 unwinder.Unwind();
393
394 ASSERT_EQ(1U, unwinder.NumFrames());
395}
396
397// Verify SP in a device stops the unwind.
398TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
399 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
400 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
401 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
402
403 regs_.FakeSetPc(0x1000);
404 regs_.FakeSetSp(0x13000);
405 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
406 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
407 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
408
409 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
410 unwinder.Unwind();
411
412 ASSERT_EQ(1U, unwinder.NumFrames());
413}
414
415// Verify a no map info frame gets a frame.
416TEST_F(UnwinderTest, pc_without_map) {
417 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
418
419 regs_.FakeSetPc(0x41000);
420 regs_.FakeSetSp(0x13000);
421
422 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
423 unwinder.Unwind();
424
425 ASSERT_EQ(1U, unwinder.NumFrames());
426
427 auto* frame = &unwinder.frames()[0];
428 EXPECT_EQ(0U, frame->num);
429 EXPECT_EQ(0x41000U, frame->rel_pc);
430 EXPECT_EQ(0x41000U, frame->pc);
431 EXPECT_EQ(0x13000U, frame->sp);
432 EXPECT_EQ("", frame->function_name);
433 EXPECT_EQ(0U, frame->function_offset);
434 EXPECT_EQ("", frame->map_name);
435 EXPECT_EQ(0U, frame->map_offset);
436 EXPECT_EQ(0U, frame->map_start);
437 EXPECT_EQ(0U, frame->map_end);
438 EXPECT_EQ(0U, frame->map_load_bias);
439 EXPECT_EQ(0, frame->map_flags);
440}
441
442// Verify that a speculative frame is added.
443TEST_F(UnwinderTest, speculative_frame) {
444 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
445 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
446
447 // Fake as if code called a nullptr function.
448 regs_.FakeSetPc(0);
449 regs_.FakeSetSp(0x10000);
450 regs_.FakeSetReturnAddress(0x1202);
451 regs_.FakeSetReturnAddressValid(true);
452
453 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
454 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
455
456 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
457 unwinder.Unwind();
458
459 ASSERT_EQ(3U, unwinder.NumFrames());
460
461 auto* frame = &unwinder.frames()[0];
462 EXPECT_EQ(0U, frame->num);
463 EXPECT_EQ(0U, frame->rel_pc);
464 EXPECT_EQ(0U, frame->pc);
465 EXPECT_EQ(0x10000U, frame->sp);
466 EXPECT_EQ("", frame->function_name);
467 EXPECT_EQ(0U, frame->function_offset);
468 EXPECT_EQ("", frame->map_name);
469 EXPECT_EQ(0U, frame->map_offset);
470 EXPECT_EQ(0U, frame->map_start);
471 EXPECT_EQ(0U, frame->map_end);
472 EXPECT_EQ(0U, frame->map_load_bias);
473 EXPECT_EQ(0, frame->map_flags);
474
475 frame = &unwinder.frames()[1];
476 EXPECT_EQ(1U, frame->num);
477 EXPECT_EQ(0x200U, frame->rel_pc);
478 EXPECT_EQ(0x1200U, frame->pc);
479 EXPECT_EQ(0x10000U, frame->sp);
480 EXPECT_EQ("Frame0", frame->function_name);
481 EXPECT_EQ(0U, frame->function_offset);
482 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
483 EXPECT_EQ(0U, frame->map_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 frame = &unwinder.frames()[2];
490 EXPECT_EQ(2U, frame->num);
491 EXPECT_EQ(0x100U, frame->rel_pc);
492 EXPECT_EQ(0x23100U, frame->pc);
493 EXPECT_EQ(0x10020U, frame->sp);
494 EXPECT_EQ("Frame1", frame->function_name);
495 EXPECT_EQ(1U, frame->function_offset);
496 EXPECT_EQ("/fake/libanother.so", frame->map_name);
497 EXPECT_EQ(0U, frame->map_offset);
498 EXPECT_EQ(0x23000U, frame->map_start);
499 EXPECT_EQ(0x24000U, frame->map_end);
500 EXPECT_EQ(0U, frame->map_load_bias);
501 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
502}
503
504// Verify that a speculative frame is added then removed because no other
505// frames are added.
506TEST_F(UnwinderTest, speculative_frame_removed) {
507 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
508 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
509
510 // Fake as if code called a nullptr function.
511 regs_.FakeSetPc(0);
512 regs_.FakeSetSp(0x10000);
513 regs_.FakeSetReturnAddress(0x1202);
514 regs_.FakeSetReturnAddressValid(true);
515
516 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
517 unwinder.Unwind();
518
519 ASSERT_EQ(1U, unwinder.NumFrames());
520
521 auto* frame = &unwinder.frames()[0];
522 EXPECT_EQ(0U, frame->num);
523 EXPECT_EQ(0U, frame->rel_pc);
524 EXPECT_EQ(0U, frame->pc);
525 EXPECT_EQ(0x10000U, frame->sp);
526 EXPECT_EQ("", frame->function_name);
527 EXPECT_EQ(0U, frame->function_offset);
528 EXPECT_EQ("", frame->map_name);
529 EXPECT_EQ(0U, frame->map_offset);
530 EXPECT_EQ(0U, frame->map_start);
531 EXPECT_EQ(0U, frame->map_end);
532 EXPECT_EQ(0U, frame->map_load_bias);
533 EXPECT_EQ(0, frame->map_flags);
534}
535
Christopher Ferrise69f4702017-10-19 16:08:58 -0700536// Verify that an unwind stops when a frame is in given suffix.
537TEST_F(UnwinderTest, map_ignore_suffixes) {
538 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
539 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
540 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
541 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
542
543 // Fake as if code called a nullptr function.
544 regs_.FakeSetPc(0x1000);
545 regs_.FakeSetSp(0x10000);
546 ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
547 ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
548 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
549
550 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700551 std::vector<std::string> suffixes{"oat"};
Christopher Ferrise69f4702017-10-19 16:08:58 -0700552 unwinder.Unwind(nullptr, &suffixes);
553
554 ASSERT_EQ(2U, unwinder.NumFrames());
555 // Make sure the elf was not initialized.
556 MapInfo* map_info = maps_.Find(0x53000);
557 ASSERT_TRUE(map_info != nullptr);
558 EXPECT_TRUE(map_info->elf == nullptr);
559
560 auto* frame = &unwinder.frames()[0];
561 EXPECT_EQ(0U, frame->num);
562 EXPECT_EQ(0U, frame->rel_pc);
563 EXPECT_EQ(0x1000U, frame->pc);
564 EXPECT_EQ(0x10000U, frame->sp);
565 EXPECT_EQ("Frame0", frame->function_name);
566 EXPECT_EQ(0U, frame->function_offset);
567 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
568 EXPECT_EQ(0U, frame->map_offset);
569 EXPECT_EQ(0x1000U, frame->map_start);
570 EXPECT_EQ(0x8000U, frame->map_end);
571 EXPECT_EQ(0U, frame->map_load_bias);
572 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
573
574 frame = &unwinder.frames()[1];
575 EXPECT_EQ(1U, frame->num);
576 EXPECT_EQ(0x400U, frame->rel_pc);
577 EXPECT_EQ(0x43400U, frame->pc);
578 EXPECT_EQ(0x10010U, frame->sp);
579 EXPECT_EQ("Frame1", frame->function_name);
580 EXPECT_EQ(1U, frame->function_offset);
581 EXPECT_EQ("/fake/fake.apk", frame->map_name);
582 EXPECT_EQ(0x1d000U, frame->map_offset);
583 EXPECT_EQ(0x43000U, frame->map_start);
584 EXPECT_EQ(0x44000U, frame->map_end);
585 EXPECT_EQ(0U, frame->map_load_bias);
586 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
587}
588
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700589// Verify that an unwind stops when the sp and pc don't change.
590TEST_F(UnwinderTest, sp_pc_do_not_change) {
591 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
592 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
593 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
594 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
595 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
596
597 regs_.FakeSetPc(0x1000);
598 regs_.FakeSetSp(0x10000);
599 ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
600 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
601 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
602 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
603 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
604 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
605
606 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
607 unwinder.Unwind();
608
609 ASSERT_EQ(3U, unwinder.NumFrames());
610
611 auto* frame = &unwinder.frames()[0];
612 EXPECT_EQ(0U, frame->num);
613 EXPECT_EQ(0U, frame->rel_pc);
614 EXPECT_EQ(0x1000U, frame->pc);
615 EXPECT_EQ(0x10000U, frame->sp);
616 EXPECT_EQ("Frame0", frame->function_name);
617 EXPECT_EQ(0U, frame->function_offset);
618 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
619 EXPECT_EQ(0U, frame->map_offset);
620 EXPECT_EQ(0x1000U, frame->map_start);
621 EXPECT_EQ(0x8000U, frame->map_end);
622 EXPECT_EQ(0U, frame->map_load_bias);
623 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
624
625 frame = &unwinder.frames()[1];
626 EXPECT_EQ(1U, frame->num);
627 EXPECT_EQ(0x400U, frame->rel_pc);
628 EXPECT_EQ(0x33400U, frame->pc);
629 EXPECT_EQ(0x10010U, frame->sp);
630 EXPECT_EQ("Frame1", frame->function_name);
631 EXPECT_EQ(1U, frame->function_offset);
632 EXPECT_EQ("/fake/compressed.so", frame->map_name);
633 EXPECT_EQ(0U, frame->map_offset);
634 EXPECT_EQ(0x33000U, frame->map_start);
635 EXPECT_EQ(0x34000U, frame->map_end);
636 EXPECT_EQ(0U, frame->map_load_bias);
637 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
638
639 frame = &unwinder.frames()[2];
640 EXPECT_EQ(2U, frame->num);
641 EXPECT_EQ(0x500U, frame->rel_pc);
642 EXPECT_EQ(0x33500U, frame->pc);
643 EXPECT_EQ(0x10020U, frame->sp);
644 EXPECT_EQ("Frame2", frame->function_name);
645 EXPECT_EQ(2U, frame->function_offset);
646 EXPECT_EQ("/fake/compressed.so", frame->map_name);
647 EXPECT_EQ(0U, frame->map_offset);
648 EXPECT_EQ(0x33000U, frame->map_start);
649 EXPECT_EQ(0x34000U, frame->map_end);
650 EXPECT_EQ(0U, frame->map_load_bias);
651 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
652}
653
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700654// Verify format frame code.
655TEST_F(UnwinderTest, format_frame_static) {
656 FrameData frame;
657 frame.num = 1;
658 frame.rel_pc = 0x1000;
659 frame.pc = 0x4000;
660 frame.sp = 0x1000;
661 frame.function_name = "function";
662 frame.function_offset = 100;
663 frame.map_name = "/fake/libfake.so";
664 frame.map_offset = 0x2000;
665 frame.map_start = 0x3000;
666 frame.map_end = 0x6000;
667 frame.map_flags = PROT_READ;
668
669 EXPECT_EQ(" #01 pc 0000000000001000 (offset 0x2000) /fake/libfake.so (function+100)",
670 Unwinder::FormatFrame(frame, false));
671 EXPECT_EQ(" #01 pc 00001000 (offset 0x2000) /fake/libfake.so (function+100)",
672 Unwinder::FormatFrame(frame, true));
673
674 frame.map_offset = 0;
675 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function+100)",
676 Unwinder::FormatFrame(frame, false));
677 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function+100)",
678 Unwinder::FormatFrame(frame, true));
679
680 frame.function_offset = 0;
681 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function)",
682 Unwinder::FormatFrame(frame, false));
683 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
684
685 frame.function_name = "";
686 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so", Unwinder::FormatFrame(frame, false));
687 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so", Unwinder::FormatFrame(frame, true));
688
689 frame.map_name = "";
690 EXPECT_EQ(" #01 pc 0000000000001000 <anonymous:3000>", Unwinder::FormatFrame(frame, false));
691 EXPECT_EQ(" #01 pc 00001000 <anonymous:3000>", Unwinder::FormatFrame(frame, true));
692
693 frame.map_start = 0;
694 frame.map_end = 0;
695 EXPECT_EQ(" #01 pc 0000000000001000 <unknown>", Unwinder::FormatFrame(frame, false));
696 EXPECT_EQ(" #01 pc 00001000 <unknown>", Unwinder::FormatFrame(frame, true));
697}
698
Christopher Ferris02fdb562017-12-08 15:04:49 -0800699static std::string ArchToString(ArchEnum arch) {
700 if (arch == ARCH_ARM) {
701 return "Arm";
702 } else if (arch == ARCH_ARM64) {
703 return "Arm64";
704 } else if (arch == ARCH_X86) {
705 return "X86";
706 } else if (arch == ARCH_X86_64) {
707 return "X86_64";
708 } else {
709 return "Unknown";
710 }
711}
712
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700713// Verify format frame code.
714TEST_F(UnwinderTest, format_frame) {
Christopher Ferris02fdb562017-12-08 15:04:49 -0800715 std::vector<Regs*> reg_list;
716 RegsArm* arm = new RegsArm;
717 arm->set_pc(0x2300);
718 arm->set_sp(0x10000);
719 reg_list.push_back(arm);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700720
Christopher Ferris02fdb562017-12-08 15:04:49 -0800721 RegsArm64* arm64 = new RegsArm64;
722 arm64->set_pc(0x2300);
723 arm64->set_sp(0x10000);
724 reg_list.push_back(arm64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700725
Christopher Ferris02fdb562017-12-08 15:04:49 -0800726 RegsX86* x86 = new RegsX86;
727 x86->set_pc(0x2300);
728 x86->set_sp(0x10000);
729 reg_list.push_back(x86);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700730
Christopher Ferris02fdb562017-12-08 15:04:49 -0800731 RegsX86_64* x86_64 = new RegsX86_64;
732 x86_64->set_pc(0x2300);
733 x86_64->set_sp(0x10000);
734 reg_list.push_back(x86_64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700735
Christopher Ferris02fdb562017-12-08 15:04:49 -0800736 for (auto regs : reg_list) {
737 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700738
Christopher Ferris02fdb562017-12-08 15:04:49 -0800739 Unwinder unwinder(64, &maps_, regs, process_memory_);
740 unwinder.Unwind();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700741
Christopher Ferris02fdb562017-12-08 15:04:49 -0800742 ASSERT_EQ(1U, unwinder.NumFrames());
743 std::string expected;
744 switch (regs->Arch()) {
745 case ARCH_ARM:
746 case ARCH_X86:
747 expected = " #00 pc 00001300 /system/fake/libc.so (Frame0+10)";
748 break;
749 case ARCH_ARM64:
750 case ARCH_X86_64:
751 expected = " #00 pc 0000000000001300 /system/fake/libc.so (Frame0+10)";
752 break;
753 default:
754 expected = "";
755 }
756 EXPECT_EQ(expected, unwinder.FormatFrame(0))
757 << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
758 delete regs;
759 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700760}
761
762} // namespace unwindstack