blob: cd468074eb8054f478900d16750b3e5ceeef2e19 [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 Ferrisbe788d82017-11-27 14:50:38 -080063 info->elf = 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 Ferrisbe788d82017-11-27 14:50:38 -080076 info->elf = 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 Ferrisbe788d82017-11-27 14:50:38 -080082 info->elf = 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 Ferrisbe788d82017-11-27 14:50:38 -080088 info->elf = 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 Ferrisbe788d82017-11-27 14:50:38 -080094 info->elf = 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();
132
133 ASSERT_EQ(3U, unwinder.NumFrames());
134
135 auto* frame = &unwinder.frames()[0];
136 EXPECT_EQ(0U, frame->num);
137 EXPECT_EQ(0U, frame->rel_pc);
138 EXPECT_EQ(0x1000U, frame->pc);
139 EXPECT_EQ(0x10000U, frame->sp);
140 EXPECT_EQ("Frame0", frame->function_name);
141 EXPECT_EQ(0U, frame->function_offset);
142 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
143 EXPECT_EQ(0U, frame->map_offset);
144 EXPECT_EQ(0x1000U, frame->map_start);
145 EXPECT_EQ(0x8000U, frame->map_end);
146 EXPECT_EQ(0U, frame->map_load_bias);
147 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
148
149 frame = &unwinder.frames()[1];
150 EXPECT_EQ(1U, frame->num);
151 EXPECT_EQ(0x100U, frame->rel_pc);
152 EXPECT_EQ(0x1100U, frame->pc);
153 EXPECT_EQ(0x10010U, frame->sp);
154 EXPECT_EQ("Frame1", frame->function_name);
155 EXPECT_EQ(1U, frame->function_offset);
156 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
157 EXPECT_EQ(0U, frame->map_offset);
158 EXPECT_EQ(0x1000U, frame->map_start);
159 EXPECT_EQ(0x8000U, frame->map_end);
160 EXPECT_EQ(0U, frame->map_load_bias);
161 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
162
163 frame = &unwinder.frames()[2];
164 EXPECT_EQ(2U, frame->num);
165 EXPECT_EQ(0x200U, frame->rel_pc);
166 EXPECT_EQ(0x1200U, frame->pc);
167 EXPECT_EQ(0x10020U, frame->sp);
168 EXPECT_EQ("Frame2", frame->function_name);
169 EXPECT_EQ(2U, frame->function_offset);
170 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
171 EXPECT_EQ(0U, frame->map_offset);
172 EXPECT_EQ(0x1000U, frame->map_start);
173 EXPECT_EQ(0x8000U, frame->map_end);
174 EXPECT_EQ(0U, frame->map_load_bias);
175 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
176}
177
Christopher Ferrisc5a3baa2017-10-17 18:42:03 -0700178TEST_F(UnwinderTest, non_zero_map_offset) {
179 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
180
181 regs_.FakeSetPc(0x43000);
182 regs_.FakeSetSp(0x10000);
183 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
184
185 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
186 unwinder.Unwind();
187
188 ASSERT_EQ(1U, unwinder.NumFrames());
189
190 auto* frame = &unwinder.frames()[0];
191 EXPECT_EQ(0U, frame->num);
192 EXPECT_EQ(0U, frame->rel_pc);
193 EXPECT_EQ(0x43000U, frame->pc);
194 EXPECT_EQ(0x10000U, frame->sp);
195 EXPECT_EQ("Frame0", frame->function_name);
196 EXPECT_EQ(0U, frame->function_offset);
197 EXPECT_EQ("/fake/fake.apk", frame->map_name);
198 EXPECT_EQ(0x1d000U, frame->map_offset);
199 EXPECT_EQ(0x43000U, frame->map_start);
200 EXPECT_EQ(0x44000U, frame->map_end);
201 EXPECT_EQ(0U, frame->map_load_bias);
202 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
203}
204
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700205// Verify that no attempt to continue after the step indicates it is done.
206TEST_F(UnwinderTest, no_frames_after_finished) {
207 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
208 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
209 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
210 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
211 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
212
213 regs_.FakeSetPc(0x1000);
214 regs_.FakeSetSp(0x10000);
215 ElfInterfaceFake::FakePushStepData(StepData(0x1000, 0x10000, true));
216 ElfInterfaceFake::FakePushStepData(StepData(0x1102, 0x10010, false));
217 ElfInterfaceFake::FakePushStepData(StepData(0x1202, 0x10020, false));
218
219 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
220 unwinder.Unwind();
221
222 ASSERT_EQ(1U, unwinder.NumFrames());
223
224 auto* frame = &unwinder.frames()[0];
225 EXPECT_EQ(0U, frame->num);
226 EXPECT_EQ(0U, frame->rel_pc);
227 EXPECT_EQ(0x1000U, frame->pc);
228 EXPECT_EQ(0x10000U, frame->sp);
229 EXPECT_EQ("Frame0", frame->function_name);
230 EXPECT_EQ(0U, frame->function_offset);
231 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
232 EXPECT_EQ(0U, frame->map_offset);
233 EXPECT_EQ(0x1000U, frame->map_start);
234 EXPECT_EQ(0x8000U, frame->map_end);
235 EXPECT_EQ(0U, frame->map_load_bias);
236 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
237}
238
239// Verify the maximum frames to save.
240TEST_F(UnwinderTest, max_frames) {
241 for (size_t i = 0; i < 30; i++) {
242 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame" + std::to_string(i), i));
243 ElfInterfaceFake::FakePushStepData(StepData(0x1102 + i * 0x100, 0x10010 + i * 0x10, false));
244 }
245
246 regs_.FakeSetPc(0x1000);
247 regs_.FakeSetSp(0x10000);
248
249 Unwinder unwinder(20, &maps_, &regs_, process_memory_);
250 unwinder.Unwind();
251
252 ASSERT_EQ(20U, unwinder.NumFrames());
253
254 for (size_t i = 0; i < 20; i++) {
255 auto* frame = &unwinder.frames()[i];
256 EXPECT_EQ(i, frame->num);
257 EXPECT_EQ(i * 0x100, frame->rel_pc) << "Failed at frame " << i;
258 EXPECT_EQ(0x1000 + i * 0x100, frame->pc) << "Failed at frame " << i;
259 EXPECT_EQ(0x10000 + 0x10 * i, frame->sp) << "Failed at frame " << i;
260 EXPECT_EQ("Frame" + std::to_string(i), frame->function_name) << "Failed at frame " << i;
261 EXPECT_EQ(i, frame->function_offset) << "Failed at frame " << i;
262 EXPECT_EQ("/system/fake/libc.so", frame->map_name) << "Failed at frame " << i;
263 EXPECT_EQ(0U, frame->map_offset) << "Failed at frame " << i;
264 EXPECT_EQ(0x1000U, frame->map_start) << "Failed at frame " << i;
265 EXPECT_EQ(0x8000U, frame->map_end) << "Failed at frame " << i;
266 EXPECT_EQ(0U, frame->map_load_bias) << "Failed at frame " << i;
267 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags) << "Failed at frame " << i;
268 }
269}
270
271// Verify that initial map names frames are removed.
272TEST_F(UnwinderTest, verify_frames_skipped) {
273 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
274 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
275 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
276
277 regs_.FakeSetPc(0x20000);
278 regs_.FakeSetSp(0x10000);
279 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
280 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
281 ElfInterfaceFake::FakePushStepData(StepData(0x20002, 0x10030, false));
282 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10040, false));
283 ElfInterfaceFake::FakePushStepData(StepData(0x1002, 0x10050, false));
284 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x10060, false));
285 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10070, false));
286 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
287
288 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700289 std::vector<std::string> skip_libs{"libunwind.so", "libanother.so"};
290 unwinder.Unwind(&skip_libs);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700291
292 ASSERT_EQ(3U, unwinder.NumFrames());
293
294 auto* frame = &unwinder.frames()[0];
295 EXPECT_EQ(0U, frame->num);
296 EXPECT_EQ(0U, frame->rel_pc);
297 EXPECT_EQ(0x1000U, frame->pc);
298 EXPECT_EQ(0x10050U, frame->sp);
299 EXPECT_EQ("Frame0", frame->function_name);
300 EXPECT_EQ(0U, frame->function_offset);
301 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
302 EXPECT_EQ(0U, frame->map_offset);
303 EXPECT_EQ(0x1000U, frame->map_start);
304 EXPECT_EQ(0x8000U, frame->map_end);
305 EXPECT_EQ(0U, frame->map_load_bias);
306 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
307
308 frame = &unwinder.frames()[1];
309 EXPECT_EQ(1U, frame->num);
310 EXPECT_EQ(0x1000U, frame->rel_pc);
311 EXPECT_EQ(0x21000U, frame->pc);
312 EXPECT_EQ(0x10060U, frame->sp);
313 EXPECT_EQ("Frame1", frame->function_name);
314 EXPECT_EQ(1U, frame->function_offset);
315 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
316 EXPECT_EQ(0U, frame->map_offset);
317 EXPECT_EQ(0x20000U, frame->map_start);
318 EXPECT_EQ(0x22000U, frame->map_end);
319 EXPECT_EQ(0U, frame->map_load_bias);
320 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
321
322 frame = &unwinder.frames()[2];
323 EXPECT_EQ(2U, frame->num);
324 EXPECT_EQ(0U, frame->rel_pc);
325 EXPECT_EQ(0x23000U, frame->pc);
326 EXPECT_EQ(0x10070U, frame->sp);
327 EXPECT_EQ("Frame2", frame->function_name);
328 EXPECT_EQ(2U, frame->function_offset);
329 EXPECT_EQ("/fake/libanother.so", frame->map_name);
330 EXPECT_EQ(0U, frame->map_offset);
331 EXPECT_EQ(0x23000U, frame->map_start);
332 EXPECT_EQ(0x24000U, frame->map_end);
333 EXPECT_EQ(0U, frame->map_load_bias);
334 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
335}
336
337// Verify SP in a non-existant map is okay.
338TEST_F(UnwinderTest, sp_not_in_map) {
339 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
340 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
341
342 regs_.FakeSetPc(0x1000);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700343 regs_.FakeSetSp(0x63000);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700344 ElfInterfaceFake::FakePushStepData(StepData(0x21002, 0x50020, false));
345 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
346
347 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
348 unwinder.Unwind();
349
350 ASSERT_EQ(2U, unwinder.NumFrames());
351
352 auto* frame = &unwinder.frames()[0];
353 EXPECT_EQ(0U, frame->num);
354 EXPECT_EQ(0U, frame->rel_pc);
355 EXPECT_EQ(0x1000U, frame->pc);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700356 EXPECT_EQ(0x63000U, frame->sp);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700357 EXPECT_EQ("Frame0", frame->function_name);
358 EXPECT_EQ(0U, frame->function_offset);
359 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
360 EXPECT_EQ(0U, frame->map_offset);
361 EXPECT_EQ(0x1000U, frame->map_start);
362 EXPECT_EQ(0x8000U, frame->map_end);
363 EXPECT_EQ(0U, frame->map_load_bias);
364 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
365
366 frame = &unwinder.frames()[1];
367 EXPECT_EQ(1U, frame->num);
368 EXPECT_EQ(0x1000U, frame->rel_pc);
369 EXPECT_EQ(0x21000U, frame->pc);
370 EXPECT_EQ(0x50020U, frame->sp);
371 EXPECT_EQ("Frame1", frame->function_name);
372 EXPECT_EQ(1U, frame->function_offset);
373 EXPECT_EQ("/system/fake/libunwind.so", frame->map_name);
374 EXPECT_EQ(0U, frame->map_offset);
375 EXPECT_EQ(0x20000U, frame->map_start);
376 EXPECT_EQ(0x22000U, frame->map_end);
377 EXPECT_EQ(0U, frame->map_load_bias);
378 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
379}
380
381// Verify PC in a device stops the unwind.
382TEST_F(UnwinderTest, pc_in_device_stops_unwind) {
383 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
384 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
385 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
386
387 regs_.FakeSetPc(0x13000);
388 regs_.FakeSetSp(0x10000);
389 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
390 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
391 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
392
393 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
394 unwinder.Unwind();
395
396 ASSERT_EQ(1U, unwinder.NumFrames());
397}
398
399// Verify SP in a device stops the unwind.
400TEST_F(UnwinderTest, sp_in_device_stops_unwind) {
401 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
402 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
403 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
404
405 regs_.FakeSetPc(0x1000);
406 regs_.FakeSetSp(0x13000);
407 ElfInterfaceFake::FakePushStepData(StepData(0x23002, 0x10010, false));
408 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
409 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
410
411 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
412 unwinder.Unwind();
413
414 ASSERT_EQ(1U, unwinder.NumFrames());
415}
416
417// Verify a no map info frame gets a frame.
418TEST_F(UnwinderTest, pc_without_map) {
419 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
420
421 regs_.FakeSetPc(0x41000);
422 regs_.FakeSetSp(0x13000);
423
424 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
425 unwinder.Unwind();
426
427 ASSERT_EQ(1U, unwinder.NumFrames());
428
429 auto* frame = &unwinder.frames()[0];
430 EXPECT_EQ(0U, frame->num);
431 EXPECT_EQ(0x41000U, frame->rel_pc);
432 EXPECT_EQ(0x41000U, frame->pc);
433 EXPECT_EQ(0x13000U, frame->sp);
434 EXPECT_EQ("", frame->function_name);
435 EXPECT_EQ(0U, frame->function_offset);
436 EXPECT_EQ("", frame->map_name);
437 EXPECT_EQ(0U, frame->map_offset);
438 EXPECT_EQ(0U, frame->map_start);
439 EXPECT_EQ(0U, frame->map_end);
440 EXPECT_EQ(0U, frame->map_load_bias);
441 EXPECT_EQ(0, frame->map_flags);
442}
443
444// Verify that a speculative frame is added.
445TEST_F(UnwinderTest, speculative_frame) {
446 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
447 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
448
449 // Fake as if code called a nullptr function.
450 regs_.FakeSetPc(0);
451 regs_.FakeSetSp(0x10000);
452 regs_.FakeSetReturnAddress(0x1202);
453 regs_.FakeSetReturnAddressValid(true);
454
455 ElfInterfaceFake::FakePushStepData(StepData(0x23102, 0x10020, false));
456 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
457
458 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
459 unwinder.Unwind();
460
461 ASSERT_EQ(3U, unwinder.NumFrames());
462
463 auto* frame = &unwinder.frames()[0];
464 EXPECT_EQ(0U, frame->num);
465 EXPECT_EQ(0U, frame->rel_pc);
466 EXPECT_EQ(0U, frame->pc);
467 EXPECT_EQ(0x10000U, frame->sp);
468 EXPECT_EQ("", frame->function_name);
469 EXPECT_EQ(0U, frame->function_offset);
470 EXPECT_EQ("", frame->map_name);
471 EXPECT_EQ(0U, frame->map_offset);
472 EXPECT_EQ(0U, frame->map_start);
473 EXPECT_EQ(0U, frame->map_end);
474 EXPECT_EQ(0U, frame->map_load_bias);
475 EXPECT_EQ(0, frame->map_flags);
476
477 frame = &unwinder.frames()[1];
478 EXPECT_EQ(1U, frame->num);
479 EXPECT_EQ(0x200U, frame->rel_pc);
480 EXPECT_EQ(0x1200U, frame->pc);
481 EXPECT_EQ(0x10000U, frame->sp);
482 EXPECT_EQ("Frame0", frame->function_name);
483 EXPECT_EQ(0U, frame->function_offset);
484 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
485 EXPECT_EQ(0U, frame->map_offset);
486 EXPECT_EQ(0x1000U, frame->map_start);
487 EXPECT_EQ(0x8000U, frame->map_end);
488 EXPECT_EQ(0U, frame->map_load_bias);
489 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
490
491 frame = &unwinder.frames()[2];
492 EXPECT_EQ(2U, frame->num);
493 EXPECT_EQ(0x100U, frame->rel_pc);
494 EXPECT_EQ(0x23100U, frame->pc);
495 EXPECT_EQ(0x10020U, frame->sp);
496 EXPECT_EQ("Frame1", frame->function_name);
497 EXPECT_EQ(1U, frame->function_offset);
498 EXPECT_EQ("/fake/libanother.so", frame->map_name);
499 EXPECT_EQ(0U, frame->map_offset);
500 EXPECT_EQ(0x23000U, frame->map_start);
501 EXPECT_EQ(0x24000U, frame->map_end);
502 EXPECT_EQ(0U, frame->map_load_bias);
503 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
504}
505
506// Verify that a speculative frame is added then removed because no other
507// frames are added.
508TEST_F(UnwinderTest, speculative_frame_removed) {
509 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
510 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
511
512 // Fake as if code called a nullptr function.
513 regs_.FakeSetPc(0);
514 regs_.FakeSetSp(0x10000);
515 regs_.FakeSetReturnAddress(0x1202);
516 regs_.FakeSetReturnAddressValid(true);
517
518 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
519 unwinder.Unwind();
520
521 ASSERT_EQ(1U, unwinder.NumFrames());
522
523 auto* frame = &unwinder.frames()[0];
524 EXPECT_EQ(0U, frame->num);
525 EXPECT_EQ(0U, frame->rel_pc);
526 EXPECT_EQ(0U, frame->pc);
527 EXPECT_EQ(0x10000U, frame->sp);
528 EXPECT_EQ("", frame->function_name);
529 EXPECT_EQ(0U, frame->function_offset);
530 EXPECT_EQ("", frame->map_name);
531 EXPECT_EQ(0U, frame->map_offset);
532 EXPECT_EQ(0U, frame->map_start);
533 EXPECT_EQ(0U, frame->map_end);
534 EXPECT_EQ(0U, frame->map_load_bias);
535 EXPECT_EQ(0, frame->map_flags);
536}
537
Christopher Ferrise69f4702017-10-19 16:08:58 -0700538// Verify that an unwind stops when a frame is in given suffix.
539TEST_F(UnwinderTest, map_ignore_suffixes) {
540 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
541 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
542 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
543 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
544
545 // Fake as if code called a nullptr function.
546 regs_.FakeSetPc(0x1000);
547 regs_.FakeSetSp(0x10000);
548 ElfInterfaceFake::FakePushStepData(StepData(0x43402, 0x10010, false));
549 ElfInterfaceFake::FakePushStepData(StepData(0x53502, 0x10020, false));
550 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
551
552 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
Christopher Ferris1cb84ce2017-10-24 15:36:00 -0700553 std::vector<std::string> suffixes{"oat"};
Christopher Ferrise69f4702017-10-19 16:08:58 -0700554 unwinder.Unwind(nullptr, &suffixes);
555
556 ASSERT_EQ(2U, unwinder.NumFrames());
557 // Make sure the elf was not initialized.
558 MapInfo* map_info = maps_.Find(0x53000);
559 ASSERT_TRUE(map_info != nullptr);
560 EXPECT_TRUE(map_info->elf == nullptr);
561
562 auto* frame = &unwinder.frames()[0];
563 EXPECT_EQ(0U, frame->num);
564 EXPECT_EQ(0U, frame->rel_pc);
565 EXPECT_EQ(0x1000U, frame->pc);
566 EXPECT_EQ(0x10000U, frame->sp);
567 EXPECT_EQ("Frame0", frame->function_name);
568 EXPECT_EQ(0U, frame->function_offset);
569 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
570 EXPECT_EQ(0U, frame->map_offset);
571 EXPECT_EQ(0x1000U, frame->map_start);
572 EXPECT_EQ(0x8000U, frame->map_end);
573 EXPECT_EQ(0U, frame->map_load_bias);
574 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
575
576 frame = &unwinder.frames()[1];
577 EXPECT_EQ(1U, frame->num);
578 EXPECT_EQ(0x400U, frame->rel_pc);
579 EXPECT_EQ(0x43400U, frame->pc);
580 EXPECT_EQ(0x10010U, frame->sp);
581 EXPECT_EQ("Frame1", frame->function_name);
582 EXPECT_EQ(1U, frame->function_offset);
583 EXPECT_EQ("/fake/fake.apk", frame->map_name);
584 EXPECT_EQ(0x1d000U, frame->map_offset);
585 EXPECT_EQ(0x43000U, frame->map_start);
586 EXPECT_EQ(0x44000U, frame->map_end);
587 EXPECT_EQ(0U, frame->map_load_bias);
588 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
589}
590
Christopher Ferrisfda7edd2017-10-31 16:10:42 -0700591// Verify that an unwind stops when the sp and pc don't change.
592TEST_F(UnwinderTest, sp_pc_do_not_change) {
593 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0));
594 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1));
595 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame2", 2));
596 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame3", 3));
597 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame4", 4));
598
599 regs_.FakeSetPc(0x1000);
600 regs_.FakeSetSp(0x10000);
601 ElfInterfaceFake::FakePushStepData(StepData(0x33402, 0x10010, false));
602 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
603 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
604 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
605 ElfInterfaceFake::FakePushStepData(StepData(0x33502, 0x10020, false));
606 ElfInterfaceFake::FakePushStepData(StepData(0, 0, true));
607
608 Unwinder unwinder(64, &maps_, &regs_, process_memory_);
609 unwinder.Unwind();
610
611 ASSERT_EQ(3U, unwinder.NumFrames());
612
613 auto* frame = &unwinder.frames()[0];
614 EXPECT_EQ(0U, frame->num);
615 EXPECT_EQ(0U, frame->rel_pc);
616 EXPECT_EQ(0x1000U, frame->pc);
617 EXPECT_EQ(0x10000U, frame->sp);
618 EXPECT_EQ("Frame0", frame->function_name);
619 EXPECT_EQ(0U, frame->function_offset);
620 EXPECT_EQ("/system/fake/libc.so", frame->map_name);
621 EXPECT_EQ(0U, frame->map_offset);
622 EXPECT_EQ(0x1000U, frame->map_start);
623 EXPECT_EQ(0x8000U, frame->map_end);
624 EXPECT_EQ(0U, frame->map_load_bias);
625 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
626
627 frame = &unwinder.frames()[1];
628 EXPECT_EQ(1U, frame->num);
629 EXPECT_EQ(0x400U, frame->rel_pc);
630 EXPECT_EQ(0x33400U, frame->pc);
631 EXPECT_EQ(0x10010U, frame->sp);
632 EXPECT_EQ("Frame1", frame->function_name);
633 EXPECT_EQ(1U, frame->function_offset);
634 EXPECT_EQ("/fake/compressed.so", frame->map_name);
635 EXPECT_EQ(0U, frame->map_offset);
636 EXPECT_EQ(0x33000U, frame->map_start);
637 EXPECT_EQ(0x34000U, frame->map_end);
638 EXPECT_EQ(0U, frame->map_load_bias);
639 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
640
641 frame = &unwinder.frames()[2];
642 EXPECT_EQ(2U, frame->num);
643 EXPECT_EQ(0x500U, frame->rel_pc);
644 EXPECT_EQ(0x33500U, frame->pc);
645 EXPECT_EQ(0x10020U, frame->sp);
646 EXPECT_EQ("Frame2", frame->function_name);
647 EXPECT_EQ(2U, frame->function_offset);
648 EXPECT_EQ("/fake/compressed.so", frame->map_name);
649 EXPECT_EQ(0U, frame->map_offset);
650 EXPECT_EQ(0x33000U, frame->map_start);
651 EXPECT_EQ(0x34000U, frame->map_end);
652 EXPECT_EQ(0U, frame->map_load_bias);
653 EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags);
654}
655
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700656// Verify format frame code.
657TEST_F(UnwinderTest, format_frame_static) {
658 FrameData frame;
659 frame.num = 1;
660 frame.rel_pc = 0x1000;
661 frame.pc = 0x4000;
662 frame.sp = 0x1000;
663 frame.function_name = "function";
664 frame.function_offset = 100;
665 frame.map_name = "/fake/libfake.so";
666 frame.map_offset = 0x2000;
667 frame.map_start = 0x3000;
668 frame.map_end = 0x6000;
669 frame.map_flags = PROT_READ;
670
671 EXPECT_EQ(" #01 pc 0000000000001000 (offset 0x2000) /fake/libfake.so (function+100)",
672 Unwinder::FormatFrame(frame, false));
673 EXPECT_EQ(" #01 pc 00001000 (offset 0x2000) /fake/libfake.so (function+100)",
674 Unwinder::FormatFrame(frame, true));
675
676 frame.map_offset = 0;
677 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function+100)",
678 Unwinder::FormatFrame(frame, false));
679 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function+100)",
680 Unwinder::FormatFrame(frame, true));
681
682 frame.function_offset = 0;
683 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so (function)",
684 Unwinder::FormatFrame(frame, false));
685 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so (function)", Unwinder::FormatFrame(frame, true));
686
687 frame.function_name = "";
688 EXPECT_EQ(" #01 pc 0000000000001000 /fake/libfake.so", Unwinder::FormatFrame(frame, false));
689 EXPECT_EQ(" #01 pc 00001000 /fake/libfake.so", Unwinder::FormatFrame(frame, true));
690
691 frame.map_name = "";
692 EXPECT_EQ(" #01 pc 0000000000001000 <anonymous:3000>", Unwinder::FormatFrame(frame, false));
693 EXPECT_EQ(" #01 pc 00001000 <anonymous:3000>", Unwinder::FormatFrame(frame, true));
694
695 frame.map_start = 0;
696 frame.map_end = 0;
697 EXPECT_EQ(" #01 pc 0000000000001000 <unknown>", Unwinder::FormatFrame(frame, false));
698 EXPECT_EQ(" #01 pc 00001000 <unknown>", Unwinder::FormatFrame(frame, true));
699}
700
Christopher Ferris02fdb562017-12-08 15:04:49 -0800701static std::string ArchToString(ArchEnum arch) {
702 if (arch == ARCH_ARM) {
703 return "Arm";
704 } else if (arch == ARCH_ARM64) {
705 return "Arm64";
706 } else if (arch == ARCH_X86) {
707 return "X86";
708 } else if (arch == ARCH_X86_64) {
709 return "X86_64";
710 } else {
711 return "Unknown";
712 }
713}
714
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700715// Verify format frame code.
716TEST_F(UnwinderTest, format_frame) {
Christopher Ferris02fdb562017-12-08 15:04:49 -0800717 std::vector<Regs*> reg_list;
718 RegsArm* arm = new RegsArm;
719 arm->set_pc(0x2300);
720 arm->set_sp(0x10000);
721 reg_list.push_back(arm);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700722
Christopher Ferris02fdb562017-12-08 15:04:49 -0800723 RegsArm64* arm64 = new RegsArm64;
724 arm64->set_pc(0x2300);
725 arm64->set_sp(0x10000);
726 reg_list.push_back(arm64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700727
Christopher Ferris02fdb562017-12-08 15:04:49 -0800728 RegsX86* x86 = new RegsX86;
729 x86->set_pc(0x2300);
730 x86->set_sp(0x10000);
731 reg_list.push_back(x86);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700732
Christopher Ferris02fdb562017-12-08 15:04:49 -0800733 RegsX86_64* x86_64 = new RegsX86_64;
734 x86_64->set_pc(0x2300);
735 x86_64->set_sp(0x10000);
736 reg_list.push_back(x86_64);
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700737
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100738 RegsMips* mips = new RegsMips;
739 mips->set_pc(0x2300);
740 mips->set_sp(0x10000);
741 reg_list.push_back(mips);
742
743 RegsMips64* mips64 = new RegsMips64;
744 mips64->set_pc(0x2300);
745 mips64->set_sp(0x10000);
746 reg_list.push_back(mips64);
747
Christopher Ferris02fdb562017-12-08 15:04:49 -0800748 for (auto regs : reg_list) {
749 ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10));
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700750
Christopher Ferris02fdb562017-12-08 15:04:49 -0800751 Unwinder unwinder(64, &maps_, regs, process_memory_);
752 unwinder.Unwind();
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700753
Christopher Ferris02fdb562017-12-08 15:04:49 -0800754 ASSERT_EQ(1U, unwinder.NumFrames());
755 std::string expected;
756 switch (regs->Arch()) {
757 case ARCH_ARM:
758 case ARCH_X86:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100759 case ARCH_MIPS:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800760 expected = " #00 pc 00001300 /system/fake/libc.so (Frame0+10)";
761 break;
762 case ARCH_ARM64:
763 case ARCH_X86_64:
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100764 case ARCH_MIPS64:
Christopher Ferris02fdb562017-12-08 15:04:49 -0800765 expected = " #00 pc 0000000000001300 /system/fake/libc.so (Frame0+10)";
766 break;
767 default:
768 expected = "";
769 }
770 EXPECT_EQ(expected, unwinder.FormatFrame(0))
771 << "Mismatch of frame format for regs arch " << ArchToString(regs->Arch());
772 delete regs;
773 }
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700774}
775
776} // namespace unwindstack