blob: 45cf9074d5da19ed7912b536d9875272e2c52ddc [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>
Douglas Leung61b1a1a2017-11-08 10:53:53 +010035#include <unwindstack/RegsMips.h>
36#include <unwindstack/RegsMips64.h>
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070037#include <unwindstack/Unwinder.h>
38
39#include "ElfFake.h"
40#include "MemoryFake.h"
41#include "RegsFake.h"
42
43namespace unwindstack {
44
45class MapsFake : public Maps {
46 public:
47 MapsFake() = default;
48 virtual ~MapsFake() = default;
49
50 bool Parse() { return true; }
51
52 void FakeClear() { maps_.clear(); }
53
Christopher Ferrisbe788d82017-11-27 14:50:38 -080054 void FakeAddMapInfo(MapInfo* map_info) { maps_.push_back(map_info); }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070055};
56
57class UnwinderTest : public ::testing::Test {
58 protected:
59 static void SetUpTestCase() {
60 maps_.FakeClear();
Christopher Ferrisbe788d82017-11-27 14:50:38 -080061 MapInfo* info = new MapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080062 ElfFake* elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080063 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070064 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070065 maps_.FakeAddMapInfo(info);
66
Christopher Ferrisbe788d82017-11-27 14:50:38 -080067 info = new MapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070068 maps_.FakeAddMapInfo(info);
69
Christopher Ferrisbe788d82017-11-27 14:50:38 -080070 info = new MapInfo(0x13000, 0x15000, 0, PROT_READ | PROT_WRITE | MAPS_FLAGS_DEVICE_MAP,
71 "/dev/fake_device");
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070072 maps_.FakeAddMapInfo(info);
73
Christopher Ferrisbe788d82017-11-27 14:50:38 -080074 info = new MapInfo(0x20000, 0x22000, 0, PROT_READ | PROT_WRITE, "/system/fake/libunwind.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080075 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080076 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070077 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
78 maps_.FakeAddMapInfo(info);
79
Christopher Ferrisbe788d82017-11-27 14:50:38 -080080 info = new MapInfo(0x23000, 0x24000, 0, PROT_READ | PROT_WRITE, "/fake/libanother.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080081 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080082 info->elf.reset(elf);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070083 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
84 maps_.FakeAddMapInfo(info);
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070085
Christopher Ferrisbe788d82017-11-27 14:50:38 -080086 info = new MapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so");
Christopher Ferris02fdb562017-12-08 15:04:49 -080087 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080088 info->elf.reset(elf);
Christopher Ferrise69f4702017-10-19 16:08:58 -070089 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
90 maps_.FakeAddMapInfo(info);
91
Christopher Ferrisbe788d82017-11-27 14:50:38 -080092 info = new MapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk");
Christopher Ferris02fdb562017-12-08 15:04:49 -080093 elf = new ElfFake(new MemoryFake);
Christopher Ferris0b79ae12018-01-25 12:15:56 -080094 info->elf.reset(elf);
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -070095 elf->FakeSetInterface(new ElfInterfaceFake(nullptr));
96 maps_.FakeAddMapInfo(info);
Christopher Ferrise69f4702017-10-19 16:08:58 -070097
Christopher Ferrisbe788d82017-11-27 14:50:38 -080098 info = new MapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat");
Christopher Ferrise69f4702017-10-19 16:08:58 -070099 maps_.FakeAddMapInfo(info);
Christopher Ferris02fdb562017-12-08 15:04:49 -0800100
101 process_memory_.reset(new MemoryFake);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700102 }
103
104 void SetUp() override {
105 ElfInterfaceFake::FakeClear();
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800106 regs_.FakeSetArch(ARCH_ARM);
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700107 regs_.FakeSetReturnAddressValid(false);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700108 }
109
110 static MapsFake maps_;
111 static RegsFake regs_;
112 static std::shared_ptr<Memory> process_memory_;
113};
114
115MapsFake UnwinderTest::maps_;
116RegsFake UnwinderTest::regs_(5, 0);
117std::shared_ptr<Memory> UnwinderTest::process_memory_(nullptr);
118
119TEST_F(UnwinderTest, multiple_frames) {
120 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
121 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
122 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
123
124 regs_.FakeSetPc(0x1000);
125 regs_.FakeSetSp(0x10000);
126 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
127 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
128 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
129
130 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
131 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800132 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700133
134 ASSERT_EQ(3U, unwinder.NumFrames());
135
136 auto* frame = &unwinder.frames()[0];
137 EXPECT_EQ(0U, frame->num);
138 EXPECT_EQ(0U, frame->rel_pc);
139 EXPECT_EQ(0x1000U, frame->pc);
140 EXPECT_EQ(0x10000U, frame->sp);
141 EXPECT_EQ("Frame0", frame->function_name);
142 EXPECT_EQ(0U, frame->function_offset);
143 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
144 EXPECT_EQ(0U, frame->map_offset);
145 EXPECT_EQ(0x1000U, frame->map_start);
146 EXPECT_EQ(0x8000U, frame->map_end);
147 EXPECT_EQ(0U, frame->map_load_bias);
148 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
149
150 frame = &unwinder.frames()[1];
151 EXPECT_EQ(1U, frame->num);
152 EXPECT_EQ(0x100U, frame->rel_pc);
153 EXPECT_EQ(0x1100U, frame->pc);
154 EXPECT_EQ(0x10010U, frame->sp);
155 EXPECT_EQ("Frame1", frame->function_name);
156 EXPECT_EQ(1U, frame->function_offset);
157 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
158 EXPECT_EQ(0U, frame->map_offset);
159 EXPECT_EQ(0x1000U, frame->map_start);
160 EXPECT_EQ(0x8000U, frame->map_end);
161 EXPECT_EQ(0U, frame->map_load_bias);
162 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
163
164 frame = &unwinder.frames()[2];
165 EXPECT_EQ(2U, frame->num);
166 EXPECT_EQ(0x200U, frame->rel_pc);
167 EXPECT_EQ(0x1200U, frame->pc);
168 EXPECT_EQ(0x10020U, frame->sp);
169 EXPECT_EQ("Frame2", frame->function_name);
170 EXPECT_EQ(2U, frame->function_offset);
171 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
172 EXPECT_EQ(0U, frame->map_offset);
173 EXPECT_EQ(0x1000U, frame->map_start);
174 EXPECT_EQ(0x8000U, frame->map_end);
175 EXPECT_EQ(0U, frame->map_load_bias);
176 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
177}
178
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700179TEST_F(UnwinderTest, non_zero_map_offset) {
180 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
181
182 regs_.FakeSetPc(0x43000);
183 regs_.FakeSetSp(0x10000);
184 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
185
186 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
187 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800188 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700189
190 ASSERT_EQ(1U, unwinder.NumFrames());
191
192 auto* frame = &unwinder.frames()[0];
193 EXPECT_EQ(0U, frame->num);
194 EXPECT_EQ(0U, frame->rel_pc);
195 EXPECT_EQ(0x43000U, frame->pc);
196 EXPECT_EQ(0x10000U, frame->sp);
197 EXPECT_EQ("Frame0", frame->function_name);
198 EXPECT_EQ(0U, frame->function_offset);
199 EXPECT_EQ("/fake/fake.apk", frame->map_name);
200 EXPECT_EQ(0x1d000U, frame->map_offset);
201 EXPECT_EQ(0x43000U, frame->map_start);
202 EXPECT_EQ(0x44000U, frame->map_end);
203 EXPECT_EQ(0U, frame->map_load_bias);
204 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
205}
206
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700207// Verify that no attempt to continue after the step indicates it is done.
208TEST_F(UnwinderTest, no_frames_after_finished) {
209 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
210 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
211 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
212 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
213 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
214
215 regs_.FakeSetPc(0x1000);
216 regs_.FakeSetSp(0x10000);
217 ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
218 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
219 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
220
221 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
222 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800223 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700224
225 ASSERT_EQ(1U, unwinder.NumFrames());
226
227 auto* frame = &unwinder.frames()[0];
228 EXPECT_EQ(0U, frame->num);
229 EXPECT_EQ(0U, frame->rel_pc);
230 EXPECT_EQ(0x1000U, frame->pc);
231 EXPECT_EQ(0x10000U, frame->sp);
232 EXPECT_EQ("Frame0", frame->function_name);
233 EXPECT_EQ(0U, frame->function_offset);
234 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
235 EXPECT_EQ(0U, frame->map_offset);
236 EXPECT_EQ(0x1000U, frame->map_start);
237 EXPECT_EQ(0x8000U, frame->map_end);
238 EXPECT_EQ(0U, frame->map_load_bias);
239 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
240}
241
242// Verify the maximum frames to save.
243TEST_F(UnwinderTest, max_frames) {
244 for (size_t i = 0; i < 30; i++) {
245 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
246 ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
247 }
248
249 regs_.FakeSetPc(0x1000);
250 regs_.FakeSetSp(0x10000);
251
252 Unwinder unwinder(20, &maps_, &regs_, process_memory_);
253 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800254 EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700255
256 ASSERT_EQ(20U, unwinder.NumFrames());
257
258 for (size_t i = 0; i < 20; i++) {
259 auto* frame = &unwinder.frames()[i];
260 EXPECT_EQ(i, frame->num);
261 EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
262 EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
263 EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
264 EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
265 EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
266 EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
267 EXPECT_EQ(0U, frame->map_offset) << "Failed at frame " << i;
268 EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
269 EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
270 EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
271 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
272 }
273}
274
275// Verify that initial map names frames are removed.
276TEST_F(UnwinderTest, verify_frames_skipped) {
277 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
278 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
279 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
280
281 regs_.FakeSetPc(0x20000);
282 regs_.FakeSetSp(0x10000);
283 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
284 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
285 ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
286 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10040, false));
287 ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
288 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10060, false));
289 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
290 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
291
292 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700293 std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
294 unwinder.Unwind(&skip_libs);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800295 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700296
297 ASSERT_EQ(3U, unwinder.NumFrames());
298
299 auto* frame = &unwinder.frames()[0];
300 EXPECT_EQ(0U, frame->num);
301 EXPECT_EQ(0U, frame->rel_pc);
302 EXPECT_EQ(0x1000U, frame->pc);
303 EXPECT_EQ(0x10050U, frame->sp);
304 EXPECT_EQ("Frame0", frame->function_name);
305 EXPECT_EQ(0U, frame->function_offset);
306 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
307 EXPECT_EQ(0U, frame->map_offset);
308 EXPECT_EQ(0x1000U, frame->map_start);
309 EXPECT_EQ(0x8000U, frame->map_end);
310 EXPECT_EQ(0U, frame->map_load_bias);
311 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
312
313 frame = &unwinder.frames()[1];
314 EXPECT_EQ(1U, frame->num);
315 EXPECT_EQ(0x1000U, frame->rel_pc);
316 EXPECT_EQ(0x21000U, frame->pc);
317 EXPECT_EQ(0x10060U, frame->sp);
318 EXPECT_EQ("Frame1", frame->function_name);
319 EXPECT_EQ(1U, frame->function_offset);
320 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
321 EXPECT_EQ(0U, frame->map_offset);
322 EXPECT_EQ(0x20000U, frame->map_start);
323 EXPECT_EQ(0x22000U, frame->map_end);
324 EXPECT_EQ(0U, frame->map_load_bias);
325 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
326
327 frame = &unwinder.frames()[2];
328 EXPECT_EQ(2U, frame->num);
329 EXPECT_EQ(0U, frame->rel_pc);
330 EXPECT_EQ(0x23000U, frame->pc);
331 EXPECT_EQ(0x10070U, frame->sp);
332 EXPECT_EQ("Frame2", frame->function_name);
333 EXPECT_EQ(2U, frame->function_offset);
334 EXPECT_EQ("/fake/libanother.so", frame->map_name);
335 EXPECT_EQ(0U, frame->map_offset);
336 EXPECT_EQ(0x23000U, frame->map_start);
337 EXPECT_EQ(0x24000U, frame->map_end);
338 EXPECT_EQ(0U, frame->map_load_bias);
339 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
340}
341
342// Verify SP in a non-existant map is okay.
343TEST_F(UnwinderTest, sp_not_in_map) {
344 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
345 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
346
347 regs_.FakeSetPc(0x1000);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700348 regs_.FakeSetSp(0x63000);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700349 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
350 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
351
352 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
353 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800354 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700355
356 ASSERT_EQ(2U, unwinder.NumFrames());
357
358 auto* frame = &unwinder.frames()[0];
359 EXPECT_EQ(0U, frame->num);
360 EXPECT_EQ(0U, frame->rel_pc);
361 EXPECT_EQ(0x1000U, frame->pc);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700362 EXPECT_EQ(0x63000U, frame->sp);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700363 EXPECT_EQ("Frame0", frame->function_name);
364 EXPECT_EQ(0U, frame->function_offset);
365 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
366 EXPECT_EQ(0U, frame->map_offset);
367 EXPECT_EQ(0x1000U, frame->map_start);
368 EXPECT_EQ(0x8000U, frame->map_end);
369 EXPECT_EQ(0U, frame->map_load_bias);
370 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
371
372 frame = &unwinder.frames()[1];
373 EXPECT_EQ(1U, frame->num);
374 EXPECT_EQ(0x1000U, frame->rel_pc);
375 EXPECT_EQ(0x21000U, frame->pc);
376 EXPECT_EQ(0x50020U, frame->sp);
377 EXPECT_EQ("Frame1", frame->function_name);
378 EXPECT_EQ(1U, frame->function_offset);
379 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
380 EXPECT_EQ(0U, frame->map_offset);
381 EXPECT_EQ(0x20000U, frame->map_start);
382 EXPECT_EQ(0x22000U, frame->map_end);
383 EXPECT_EQ(0U, frame->map_load_bias);
384 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
385}
386
387// Verify PC in a device stops the unwind.
388TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
389 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
390 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
391 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
392
393 regs_.FakeSetPc(0x13000);
394 regs_.FakeSetSp(0x10000);
395 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
396 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
397 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
398
399 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
400 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800401 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700402
403 ASSERT_EQ(1U, unwinder.NumFrames());
404}
405
406// Verify SP in a device stops the unwind.
407TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
408 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
409 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
410 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
411
412 regs_.FakeSetPc(0x1000);
413 regs_.FakeSetSp(0x13000);
414 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
415 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
416 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
417
418 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
419 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800420 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700421
422 ASSERT_EQ(1U, unwinder.NumFrames());
423}
424
425// Verify a no map info frame gets a frame.
426TEST_F(UnwinderTest, pc_without_map) {
427 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
428
429 regs_.FakeSetPc(0x41000);
430 regs_.FakeSetSp(0x13000);
431
432 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
433 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800434 EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700435
436 ASSERT_EQ(1U, unwinder.NumFrames());
437
438 auto* frame = &unwinder.frames()[0];
439 EXPECT_EQ(0U, frame->num);
440 EXPECT_EQ(0x41000U, frame->rel_pc);
441 EXPECT_EQ(0x41000U, frame->pc);
442 EXPECT_EQ(0x13000U, frame->sp);
443 EXPECT_EQ("", frame->function_name);
444 EXPECT_EQ(0U, frame->function_offset);
445 EXPECT_EQ("", frame->map_name);
446 EXPECT_EQ(0U, frame->map_offset);
447 EXPECT_EQ(0U, frame->map_start);
448 EXPECT_EQ(0U, frame->map_end);
449 EXPECT_EQ(0U, frame->map_load_bias);
450 EXPECT_EQ(0, frame->map_flags);
451}
452
453// Verify that a speculative frame is added.
454TEST_F(UnwinderTest, speculative_frame) {
455 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
456 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
457
458 // Fake as if code called a nullptr function.
459 regs_.FakeSetPc(0);
460 regs_.FakeSetSp(0x10000);
461 regs_.FakeSetReturnAddress(0x1202);
462 regs_.FakeSetReturnAddressValid(true);
463
464 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
465 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
466
467 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
468 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800469 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700470
471 ASSERT_EQ(3U, unwinder.NumFrames());
472
473 auto* frame = &unwinder.frames()[0];
474 EXPECT_EQ(0U, frame->num);
475 EXPECT_EQ(0U, frame->rel_pc);
476 EXPECT_EQ(0U, frame->pc);
477 EXPECT_EQ(0x10000U, frame->sp);
478 EXPECT_EQ("", frame->function_name);
479 EXPECT_EQ(0U, frame->function_offset);
480 EXPECT_EQ("", frame->map_name);
481 EXPECT_EQ(0U, frame->map_offset);
482 EXPECT_EQ(0U, frame->map_start);
483 EXPECT_EQ(0U, frame->map_end);
484 EXPECT_EQ(0U, frame->map_load_bias);
485 EXPECT_EQ(0, frame->map_flags);
486
487 frame = &unwinder.frames()[1];
488 EXPECT_EQ(1U, frame->num);
489 EXPECT_EQ(0x200U, frame->rel_pc);
490 EXPECT_EQ(0x1200U, frame->pc);
491 EXPECT_EQ(0x10000U, frame->sp);
492 EXPECT_EQ("Frame0", frame->function_name);
493 EXPECT_EQ(0U, frame->function_offset);
494 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
495 EXPECT_EQ(0U, frame->map_offset);
496 EXPECT_EQ(0x1000U, frame->map_start);
497 EXPECT_EQ(0x8000U, frame->map_end);
498 EXPECT_EQ(0U, frame->map_load_bias);
499 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
500
501 frame = &unwinder.frames()[2];
502 EXPECT_EQ(2U, frame->num);
503 EXPECT_EQ(0x100U, frame->rel_pc);
504 EXPECT_EQ(0x23100U, frame->pc);
505 EXPECT_EQ(0x10020U, frame->sp);
506 EXPECT_EQ("Frame1", frame->function_name);
507 EXPECT_EQ(1U, frame->function_offset);
508 EXPECT_EQ("/fake/libanother.so", frame->map_name);
509 EXPECT_EQ(0U, frame->map_offset);
510 EXPECT_EQ(0x23000U, frame->map_start);
511 EXPECT_EQ(0x24000U, frame->map_end);
512 EXPECT_EQ(0U, frame->map_load_bias);
513 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
514}
515
516// Verify that a speculative frame is added then removed because no other
517// frames are added.
518TEST_F(UnwinderTest, speculative_frame_removed) {
519 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
520 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
521
522 // Fake as if code called a nullptr function.
523 regs_.FakeSetPc(0);
524 regs_.FakeSetSp(0x10000);
525 regs_.FakeSetReturnAddress(0x1202);
526 regs_.FakeSetReturnAddressValid(true);
527
528 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
529 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800530 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700531
532 ASSERT_EQ(1U, unwinder.NumFrames());
533
534 auto* frame = &unwinder.frames()[0];
535 EXPECT_EQ(0U, frame->num);
536 EXPECT_EQ(0U, frame->rel_pc);
537 EXPECT_EQ(0U, frame->pc);
538 EXPECT_EQ(0x10000U, frame->sp);
539 EXPECT_EQ("", frame->function_name);
540 EXPECT_EQ(0U, frame->function_offset);
541 EXPECT_EQ("", frame->map_name);
542 EXPECT_EQ(0U, frame->map_offset);
543 EXPECT_EQ(0U, frame->map_start);
544 EXPECT_EQ(0U, frame->map_end);
545 EXPECT_EQ(0U, frame->map_load_bias);
546 EXPECT_EQ(0, frame->map_flags);
547}
548
Christopher Ferrise69f4702017-10-19 16:08:58 -0700549// Verify that an unwind stops when a frame is in given suffix.
550TEST_F(UnwinderTest, map_ignore_suffixes) {
551 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
552 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
553 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
554 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
555
556 // Fake as if code called a nullptr function.
557 regs_.FakeSetPc(0x1000);
558 regs_.FakeSetSp(0x10000);
559 ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
560 ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
561 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
562
563 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700564 std::vector<std::string> suffixes{"oat"};
Christopher Ferrise69f4702017-10-19 16:08:58 -0700565 unwinder.Unwind(nullptr, &suffixes);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800566 EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode());
Christopher Ferrise69f4702017-10-19 16:08:58 -0700567
568 ASSERT_EQ(2U, unwinder.NumFrames());
569 // Make sure the elf was not initialized.
570 MapInfo* map_info = maps_.Find(0x53000);
571 ASSERT_TRUE(map_info != nullptr);
572 EXPECT_TRUE(map_info->elf == nullptr);
573
574 auto* frame = &unwinder.frames()[0];
575 EXPECT_EQ(0U, frame->num);
576 EXPECT_EQ(0U, frame->rel_pc);
577 EXPECT_EQ(0x1000U, frame->pc);
578 EXPECT_EQ(0x10000U, frame->sp);
579 EXPECT_EQ("Frame0", frame->function_name);
580 EXPECT_EQ(0U, frame->function_offset);
581 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
582 EXPECT_EQ(0U, frame->map_offset);
583 EXPECT_EQ(0x1000U, frame->map_start);
584 EXPECT_EQ(0x8000U, frame->map_end);
585 EXPECT_EQ(0U, frame->map_load_bias);
586 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
587
588 frame = &unwinder.frames()[1];
589 EXPECT_EQ(1U, frame->num);
590 EXPECT_EQ(0x400U, frame->rel_pc);
591 EXPECT_EQ(0x43400U, frame->pc);
592 EXPECT_EQ(0x10010U, frame->sp);
593 EXPECT_EQ("Frame1", frame->function_name);
594 EXPECT_EQ(1U, frame->function_offset);
595 EXPECT_EQ("/fake/fake.apk", frame->map_name);
596 EXPECT_EQ(0x1d000U, frame->map_offset);
597 EXPECT_EQ(0x43000U, frame->map_start);
598 EXPECT_EQ(0x44000U, frame->map_end);
599 EXPECT_EQ(0U, frame->map_load_bias);
600 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
601}
602
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700603// Verify that an unwind stops when the sp and pc don't change.
604TEST_F(UnwinderTest, sp_pc_do_not_change) {
605 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
606 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
607 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
608 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
609 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
610
611 regs_.FakeSetPc(0x1000);
612 regs_.FakeSetSp(0x10000);
613 ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
614 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
615 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
616 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
617 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
618 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
619
620 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
621 unwinder.Unwind();
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800622 EXPECT_EQ(ERROR_REPEATED_FRAME, unwinder.LastErrorCode());
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700623
624 ASSERT_EQ(3U, unwinder.NumFrames());
625
626 auto* frame = &unwinder.frames()[0];
627 EXPECT_EQ(0U, frame->num);
628 EXPECT_EQ(0U, frame->rel_pc);
629 EXPECT_EQ(0x1000U, frame->pc);
630 EXPECT_EQ(0x10000U, frame->sp);
631 EXPECT_EQ("Frame0", frame->function_name);
632 EXPECT_EQ(0U, frame->function_offset);
633 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
634 EXPECT_EQ(0U, frame->map_offset);
635 EXPECT_EQ(0x1000U, frame->map_start);
636 EXPECT_EQ(0x8000U, frame->map_end);
637 EXPECT_EQ(0U, frame->map_load_bias);
638 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
639
640 frame = &unwinder.frames()[1];
641 EXPECT_EQ(1U, frame->num);
642 EXPECT_EQ(0x400U, frame->rel_pc);
643 EXPECT_EQ(0x33400U, frame->pc);
644 EXPECT_EQ(0x10010U, frame->sp);
645 EXPECT_EQ("Frame1", frame->function_name);
646 EXPECT_EQ(1U, frame->function_offset);
647 EXPECT_EQ("/fake/compressed.so", frame->map_name);
648 EXPECT_EQ(0U, frame->map_offset);
649 EXPECT_EQ(0x33000U, frame->map_start);
650 EXPECT_EQ(0x34000U, frame->map_end);
651 EXPECT_EQ(0U, frame->map_load_bias);
652 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
653
654 frame = &unwinder.frames()[2];
655 EXPECT_EQ(2U, frame->num);
656 EXPECT_EQ(0x500U, frame->rel_pc);
657 EXPECT_EQ(0x33500U, frame->pc);
658 EXPECT_EQ(0x10020U, frame->sp);
659 EXPECT_EQ("Frame2", frame->function_name);
660 EXPECT_EQ(2U, frame->function_offset);
661 EXPECT_EQ("/fake/compressed.so", frame->map_name);
662 EXPECT_EQ(0U, frame->map_offset);
663 EXPECT_EQ(0x33000U, frame->map_start);
664 EXPECT_EQ(0x34000U, frame->map_end);
665 EXPECT_EQ(0U, frame->map_load_bias);
666 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
667}
668
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700669// Verify format frame code.
670TEST_F(UnwinderTest, format_frame_static) {
671 FrameData frame;
672 frame.num = 1;
673 frame.rel_pc = 0x1000;
674 frame.pc = 0x4000;
675 frame.sp = 0x1000;
676 frame.function_name = "function";
677 frame.function_offset = 100;
678 frame.map_name = "/fake/libfake.so";
679 frame.map_offset = 0x2000;
680 frame.map_start = 0x3000;
681 frame.map_end = 0x6000;
682 frame.map_flags = PROT_READ;
683
684 EXPECT_EQ(" #01 pc 0000000000001000 (offset 0x2000) /fake/libfake.so (function+100)",
685 Unwinder::FormatFrame(frame, false));
686 EXPECT_EQ(" #01 pc 00001000 (offset 0x2000) /fake/libfake.so (function+100)",
687 Unwinder::FormatFrame(frame, true));
688
689 frame.map_offset = 0;
690 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function+100)",
691 Unwinder::FormatFrame(frame, false));
692 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function+100)",
693 Unwinder::FormatFrame(frame, true));
694
695 frame.function_offset = 0;
696 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function)",
697 Unwinder::FormatFrame(frame, false));
698 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
699
700 frame.function_name = "";
701 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so", Unwinder::FormatFrame(frame, false));
702 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so", Unwinder::FormatFrame(frame, true));
703
704 frame.map_name = "";
705 EXPECT_EQ(" #01 pc 0000000000001000 <anonymous:3000>", Unwinder::FormatFrame(frame, false));
706 EXPECT_EQ(" #01 pc 00001000 <anonymous:3000>", Unwinder::FormatFrame(frame, true));
707
708 frame.map_start = 0;
709 frame.map_end = 0;
710 EXPECT_EQ(" #01 pc 0000000000001000 <unknown>", Unwinder::FormatFrame(frame, false));
711 EXPECT_EQ(" #01 pc 00001000 <unknown>", Unwinder::FormatFrame(frame, true));
712}
713
Christopher Ferris02fdb562017-12-08 15:04:49 -0800714static std::string ArchToString(ArchEnum arch) {
715 if (arch == ARCH_ARM) {
716 return "Arm";
717 } else if (arch == ARCH_ARM64) {
718 return "Arm64";
719 } else if (arch == ARCH_X86) {
720 return "X86";
721 } else if (arch == ARCH_X86_64) {
722 return "X86_64";
723 } else {
724 return "Unknown";
725 }
726}
727
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700728// Verify format frame code.
729TEST_F(UnwinderTest, format_frame) {
Christopher Ferris02fdb562017-12-08 15:04:49 -0800730 std::vector<Regs*> reg_list;
731 RegsArm* arm = new RegsArm;
732 arm->set_pc(0x2300);
733 arm->set_sp(0x10000);
734 reg_list.push_back(arm);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700735
Christopher Ferris02fdb562017-12-08 15:04:49 -0800736 RegsArm64* arm64 = new RegsArm64;
737 arm64->set_pc(0x2300);
738 arm64->set_sp(0x10000);
739 reg_list.push_back(arm64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700740
Christopher Ferris02fdb562017-12-08 15:04:49 -0800741 RegsX86* x86 = new RegsX86;
742 x86->set_pc(0x2300);
743 x86->set_sp(0x10000);
744 reg_list.push_back(x86);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700745
Christopher Ferris02fdb562017-12-08 15:04:49 -0800746 RegsX86_64* x86_64 = new RegsX86_64;
747 x86_64->set_pc(0x2300);
748 x86_64->set_sp(0x10000);
749 reg_list.push_back(x86_64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700750
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100751 RegsMips* mips = new RegsMips;
752 mips->set_pc(0x2300);
753 mips->set_sp(0x10000);
754 reg_list.push_back(mips);
755
756 RegsMips64* mips64 = new RegsMips64;
757 mips64->set_pc(0x2300);
758 mips64->set_sp(0x10000);
759 reg_list.push_back(mips64);
760
Christopher Ferris02fdb562017-12-08 15:04:49 -0800761 for (auto regs : reg_list) {
762 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700763
Christopher Ferris02fdb562017-12-08 15:04:49 -0800764 Unwinder unwinder(64, &maps_, regs, process_memory_);
765 unwinder.Unwind();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700766
Christopher Ferris02fdb562017-12-08 15:04:49 -0800767 ASSERT_EQ(1U, unwinder.NumFrames());
768 std::string expected;
769 switch (regs->Arch()) {
770 case ARCH_ARM:
771 case ARCH_X86:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100772 case ARCH_MIPS:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800773 expected = " #00 pc 00001300 /system/fake/libc.so (Frame0+10)";
774 break;
775 case ARCH_ARM64:
776 case ARCH_X86_64:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100777 case ARCH_MIPS64:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800778 expected = " #00 pc 0000000000001300 /system/fake/libc.so (Frame0+10)";
779 break;
780 default:
781 expected = "";
782 }
783 EXPECT_EQ(expected, unwinder.FormatFrame(0))
784 << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
785 delete regs;
786 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700787}
788
789} // namespace unwindstack