From 96e966eb3d5cfd83015e1c6519825a59d2b93d72 Mon Sep 17 00:00:00 2001 From: Florian Mayer Date: Thu, 13 Aug 2020 09:41:29 +0100 Subject: Set warning for dex pc not in map. This way, the profilers will know they have to reparse maps. Bug: 163130539 Test: Verified updated unit tests pass. Change-Id: I88a801ffdda12811eab5e4833dcf472f2d75c09a --- libunwindstack/tests/UnwinderTest.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'libunwindstack/tests/UnwinderTest.cpp') diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp index dd33aa9d7..d10af2f0b 100644 --- a/libunwindstack/tests/UnwinderTest.cpp +++ b/libunwindstack/tests/UnwinderTest.cpp @@ -168,6 +168,7 @@ TEST_F(UnwinderTest, multiple_frames) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -233,6 +234,7 @@ TEST_F(UnwinderTest, multiple_frames_dont_resolve_names) { unwinder.SetResolveNames(false); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -293,6 +295,7 @@ TEST_F(UnwinderTest, non_zero_load_bias) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -323,6 +326,7 @@ TEST_F(UnwinderTest, non_zero_elf_offset) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -353,6 +357,7 @@ TEST_F(UnwinderTest, non_zero_map_offset) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -384,6 +389,7 @@ TEST_F(UnwinderTest, disable_embedded_soname) { unwinder.SetEmbeddedSoname(false); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -421,6 +427,7 @@ TEST_F(UnwinderTest, no_frames_after_finished) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -454,6 +461,7 @@ TEST_F(UnwinderTest, max_frames) { Unwinder unwinder(20, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(20U, unwinder.NumFrames()); @@ -497,6 +505,7 @@ TEST_F(UnwinderTest, verify_frames_skipped) { std::vector skip_libs{"libunwind.so", "libanother.so"}; unwinder.Unwind(&skip_libs); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -559,6 +568,7 @@ TEST_F(UnwinderTest, sp_not_in_map) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -607,6 +617,7 @@ TEST_F(UnwinderTest, pc_in_device_stops_unwind) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -627,6 +638,7 @@ TEST_F(UnwinderTest, sp_in_device_stops_unwind) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -642,6 +654,7 @@ TEST_F(UnwinderTest, pc_without_map) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -679,6 +692,7 @@ TEST_F(UnwinderTest, speculative_frame) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -745,6 +759,7 @@ TEST_F(UnwinderTest, speculative_frame_removed) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_INVALID_MAP, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -795,6 +810,7 @@ TEST_F(UnwinderTest, speculative_frame_not_removed_pc_bad) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -843,6 +859,7 @@ TEST_F(UnwinderTest, speculative_frame_check_with_no_frames) { std::vector skip_names{"libanother.so"}; unwinder.Unwind(&skip_names); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(0U, unwinder.NumFrames()); @@ -866,6 +883,7 @@ TEST_F(UnwinderTest, map_ignore_suffixes) { std::vector suffixes{"oat"}; unwinder.Unwind(nullptr, &suffixes); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -925,6 +943,7 @@ TEST_F(UnwinderTest, sp_pc_do_not_change) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_REPEATED_FRAME, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -984,6 +1003,7 @@ TEST_F(UnwinderTest, dex_pc_in_map) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -1028,6 +1048,7 @@ TEST_F(UnwinderTest, dex_pc_in_map_non_zero_offset) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -1072,6 +1093,7 @@ TEST_F(UnwinderTest, dex_pc_not_in_map) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_DEX_PC_NOT_IN_MAP, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(2U, unwinder.NumFrames()); @@ -1119,6 +1141,7 @@ TEST_F(UnwinderTest, dex_pc_multiple_frames) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(3U, unwinder.NumFrames()); @@ -1178,6 +1201,7 @@ TEST_F(UnwinderTest, dex_pc_max_frames) { Unwinder unwinder(1, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_MAX_FRAMES_EXCEEDED, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -1208,6 +1232,7 @@ TEST_F(UnwinderTest, elf_from_memory_not_file) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_TRUE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -1238,6 +1263,7 @@ TEST_F(UnwinderTest, elf_from_memory_but_no_valid_file_with_bracket) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -1268,6 +1294,7 @@ TEST_F(UnwinderTest, elf_from_memory_but_empty_filename) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); @@ -1298,6 +1325,7 @@ TEST_F(UnwinderTest, elf_from_memory_but_from_memfd) { Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_NONE, unwinder.warnings()); EXPECT_FALSE(unwinder.elf_from_memory_not_file()); ASSERT_EQ(1U, unwinder.NumFrames()); -- cgit v1.2.3 From afbed690f091956a85484ac1409c9f1b53dd1df0 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 27 Aug 2020 11:46:36 -0700 Subject: Fix nullptr dereference. A previous cl missed adding a return in the new path which could result in a crash if the map info is null. Add the return back. Added a new test to catch this case. Bug: 166188440 Test: Unit tests pass. Verified the new test crashes on the old code. Change-Id: I9420b47dae0f880493c0e6a60d97bb3468ff2906 --- libunwindstack/tests/UnwinderTest.cpp | 66 ++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) (limited to 'libunwindstack/tests/UnwinderTest.cpp') diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp index d10af2f0b..ddd4b4d1b 100644 --- a/libunwindstack/tests/UnwinderTest.cpp +++ b/libunwindstack/tests/UnwinderTest.cpp @@ -57,6 +57,9 @@ class UnwinderTest : public ::testing::Test { static void SetUpTestSuite() { maps_.reset(new Maps); + memory_ = new MemoryFake; + process_memory_.reset(memory_); + ElfFake* elf = new ElfFake(new MemoryFake); ElfInterfaceFake* interface_fake = new ElfInterfaceFake(nullptr); interface_fake->FakeSetBuildID("FAKE"); @@ -136,7 +139,19 @@ class UnwinderTest : public ::testing::Test { const auto& info7 = *--maps_->end(); info7->load_bias = 0; - process_memory_.reset(new MemoryFake); + elf = new ElfFake(new MemoryFake); + interface = new ElfInterfaceFake(nullptr); + elf->FakeSetInterface(interface); + interface->FakeSetGlobalVariable("__dex_debug_descriptor", 0x1800); + interface->FakeSetDataOffset(0x1000); + interface->FakeSetDataVaddrStart(0x1000); + interface->FakeSetDataVaddrEnd(0x3000); + AddMapInfo(0xf0000, 0xf1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/global.so", elf); + AddMapInfo(0xf1000, 0xf9000, 0x1000, PROT_READ | PROT_WRITE, "/fake/global.so"); + memory_->SetData32(0xf180c, 0xf3000); + memory_->SetData32(0xf3000, 0xf4000); + memory_->SetData32(0xf3004, 0xf4000); + memory_->SetData32(0xf3008, 0xf5000); } void SetUp() override { @@ -147,11 +162,13 @@ class UnwinderTest : public ::testing::Test { static std::unique_ptr maps_; static RegsFake regs_; + static MemoryFake* memory_; static std::shared_ptr process_memory_; }; std::unique_ptr UnwinderTest::maps_; RegsFake UnwinderTest::regs_(5); +MemoryFake* UnwinderTest::memory_; std::shared_ptr UnwinderTest::process_memory_(nullptr); TEST_F(UnwinderTest, multiple_frames) { @@ -1129,6 +1146,53 @@ TEST_F(UnwinderTest, dex_pc_not_in_map) { EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags); } +TEST_F(UnwinderTest, dex_pc_not_in_map_valid_dex_files) { + ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0)); + regs_.set_pc(0x1000); + regs_.set_sp(0x10000); + regs_.FakeSetDexPc(0x50000); + + DexFiles dex_files(process_memory_); + Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); + unwinder.SetDexFiles(&dex_files, ARCH_ARM); + unwinder.Unwind(); + EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); + EXPECT_EQ(WARNING_DEX_PC_NOT_IN_MAP, unwinder.warnings()); + EXPECT_FALSE(unwinder.elf_from_memory_not_file()); + + ASSERT_EQ(2U, unwinder.NumFrames()); + + auto* frame = &unwinder.frames()[0]; + EXPECT_EQ(0U, frame->num); + EXPECT_EQ(0x50000U, frame->rel_pc); + EXPECT_EQ(0x50000U, frame->pc); + EXPECT_EQ(0x10000U, frame->sp); + EXPECT_EQ("", frame->function_name); + EXPECT_EQ(0U, frame->function_offset); + EXPECT_EQ("", frame->map_name); + EXPECT_EQ(0U, frame->map_elf_start_offset); + EXPECT_EQ(0U, frame->map_exact_offset); + EXPECT_EQ(0U, frame->map_start); + EXPECT_EQ(0U, frame->map_end); + EXPECT_EQ(0U, frame->map_load_bias); + EXPECT_EQ(0, frame->map_flags); + + frame = &unwinder.frames()[1]; + EXPECT_EQ(1U, frame->num); + EXPECT_EQ(0U, frame->rel_pc); + EXPECT_EQ(0x1000U, frame->pc); + EXPECT_EQ(0x10000U, frame->sp); + EXPECT_EQ("Frame0", frame->function_name); + EXPECT_EQ(0U, frame->function_offset); + EXPECT_EQ("/system/fake/libc.so", frame->map_name); + EXPECT_EQ(0U, frame->map_elf_start_offset); + EXPECT_EQ(0U, frame->map_exact_offset); + EXPECT_EQ(0x1000U, frame->map_start); + EXPECT_EQ(0x8000U, frame->map_end); + EXPECT_EQ(0U, frame->map_load_bias); + EXPECT_EQ(PROT_READ | PROT_WRITE, frame->map_flags); +} + TEST_F(UnwinderTest, dex_pc_multiple_frames) { ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 0)); ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame1", 1)); -- cgit v1.2.3 From 06996a819609c25b5dae9b611f4bab9112b077c3 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 27 Aug 2020 19:33:12 -0700 Subject: Fix bugs in BuildFrameFromPcOnly. There are a few bugs in this routine, so fix them and add unit tests to cover the cases. Test: Unit tests pass. Change-Id: I9bcb9b9fbe33d56a17a613ae3aa88036bd1d0ef1 --- libunwindstack/tests/UnwinderTest.cpp | 253 +++++++++++++++++++++++++++++----- 1 file changed, 219 insertions(+), 34 deletions(-) (limited to 'libunwindstack/tests/UnwinderTest.cpp') diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp index ddd4b4d1b..915f24884 100644 --- a/libunwindstack/tests/UnwinderTest.cpp +++ b/libunwindstack/tests/UnwinderTest.cpp @@ -37,6 +37,7 @@ #include #include "ElfFake.h" +#include "ElfTestUtils.h" #include "MemoryFake.h" #include "RegsFake.h" @@ -44,14 +45,15 @@ namespace unwindstack { class UnwinderTest : public ::testing::Test { protected: - static void AddMapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, - const char* name, Elf* elf = nullptr) { + static MapInfo* AddMapInfo(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, + const char* name, Elf* elf = nullptr) { std::string str_name(name); maps_->Add(start, end, offset, flags, name, static_cast(-1)); + MapInfo* map_info = maps_->Find(start); if (elf != nullptr) { - const auto& map_info = *--maps_->end(); map_info->elf.reset(elf); } + return map_info; } static void SetUpTestSuite() { @@ -60,10 +62,14 @@ class UnwinderTest : public ::testing::Test { memory_ = new MemoryFake; process_memory_.reset(memory_); - ElfFake* elf = new ElfFake(new MemoryFake); - ElfInterfaceFake* interface_fake = new ElfInterfaceFake(nullptr); - interface_fake->FakeSetBuildID("FAKE"); - elf->FakeSetInterface(interface_fake); + ElfFake* elf; + ElfInterfaceFake* interface; + MapInfo* map_info; + + elf = new ElfFake(new MemoryFake); + interface = new ElfInterfaceFake(nullptr); + interface->FakeSetBuildID("FAKE"); + elf->FakeSetInterface(interface); AddMapInfo(0x1000, 0x8000, 0, PROT_READ | PROT_WRITE, "/system/fake/libc.so", elf); AddMapInfo(0x10000, 0x12000, 0, PROT_READ | PROT_WRITE, "[stack]"); @@ -84,19 +90,17 @@ class UnwinderTest : public ::testing::Test { AddMapInfo(0x33000, 0x34000, 0, PROT_READ | PROT_WRITE, "/fake/compressed.so", elf); elf = new ElfFake(new MemoryFake); - ElfInterfaceFake* interface = new ElfInterfaceFake(nullptr); + interface = new ElfInterfaceFake(nullptr); interface->FakeSetSoname("lib_fake.so"); elf->FakeSetInterface(interface); - AddMapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk", elf); - MapInfo* map_info = maps_->Find(0x43000); - ASSERT_TRUE(map_info != nullptr); + map_info = AddMapInfo(0x43000, 0x44000, 0x1d000, PROT_READ | PROT_WRITE, "/fake/fake.apk", elf); map_info->elf_start_offset = 0x1d000; AddMapInfo(0x53000, 0x54000, 0, PROT_READ | PROT_WRITE, "/fake/fake.oat"); - AddMapInfo(0xa3000, 0xa4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.vdex"); - const auto& info = *--maps_->end(); - info->load_bias = 0; + map_info = + AddMapInfo(0xa3000, 0xa4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.vdex"); + map_info->load_bias = 0; elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); @@ -106,52 +110,76 @@ class UnwinderTest : public ::testing::Test { elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); - AddMapInfo(0xa7000, 0xa8000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake_offset.oat", - elf); - const auto& info2 = *--maps_->end(); - info2->elf_offset = 0x8000; + map_info = AddMapInfo(0xa7000, 0xa8000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, + "/fake/fake_offset.oat", elf); + map_info->elf_offset = 0x8000; elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); - AddMapInfo(0xc0000, 0xc1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/unreadable.so", elf); - const auto& info3 = *--maps_->end(); - info3->memory_backed_elf = true; + map_info = AddMapInfo(0xc0000, 0xc1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, + "/fake/unreadable.so", elf); + map_info->memory_backed_elf = true; elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); - AddMapInfo(0xc1000, 0xc2000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "[vdso]", elf); - const auto& info4 = *--maps_->end(); - info4->memory_backed_elf = true; + map_info = AddMapInfo(0xc1000, 0xc2000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "[vdso]", elf); + map_info->memory_backed_elf = true; elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); - AddMapInfo(0xc2000, 0xc3000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "", elf); - const auto& info5 = *--maps_->end(); - info5->memory_backed_elf = true; + map_info = AddMapInfo(0xc2000, 0xc3000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "", elf); + map_info->memory_backed_elf = true; elf = new ElfFake(new MemoryFake); elf->FakeSetInterface(new ElfInterfaceFake(nullptr)); - AddMapInfo(0xc3000, 0xc4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/memfd:/jit-cache", elf); - const auto& info6 = *--maps_->end(); - info6->memory_backed_elf = true; + map_info = AddMapInfo(0xc3000, 0xc4000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, + "/memfd:/jit-cache", elf); + map_info->memory_backed_elf = true; - AddMapInfo(0xd0000, 0xd1000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.apk"); - const auto& info7 = *--maps_->end(); - info7->load_bias = 0; + map_info = + AddMapInfo(0xd0000, 0xd1000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/fake.apk"); + map_info->load_bias = 0; elf = new ElfFake(new MemoryFake); interface = new ElfInterfaceFake(nullptr); elf->FakeSetInterface(interface); interface->FakeSetGlobalVariable("__dex_debug_descriptor", 0x1800); + interface->FakeSetGlobalVariable("__jit_debug_descriptor", 0x1900); interface->FakeSetDataOffset(0x1000); interface->FakeSetDataVaddrStart(0x1000); - interface->FakeSetDataVaddrEnd(0x3000); + interface->FakeSetDataVaddrEnd(0x8000); AddMapInfo(0xf0000, 0xf1000, 0, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/global.so", elf); AddMapInfo(0xf1000, 0xf9000, 0x1000, PROT_READ | PROT_WRITE, "/fake/global.so"); + // dex debug data memory_->SetData32(0xf180c, 0xf3000); memory_->SetData32(0xf3000, 0xf4000); memory_->SetData32(0xf3004, 0xf4000); memory_->SetData32(0xf3008, 0xf5000); + // jit debug data + memory_->SetData32(0xf1900, 1); + memory_->SetData32(0xf1904, 0); + memory_->SetData32(0xf1908, 0xf6000); + memory_->SetData32(0xf190c, 0xf6000); + memory_->SetData32(0xf6000, 0); + memory_->SetData32(0xf6004, 0); + memory_->SetData32(0xf6008, 0xf7000); + memory_->SetData32(0xf600c, 0); + memory_->SetData64(0xf6010, 0x1000); + + elf = new ElfFake(new MemoryFake); + elf->FakeSetValid(false); + elf->FakeSetLoadBias(0x300); + map_info = AddMapInfo(0x100000, 0x101000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, + "/fake/jit.so", elf); + map_info->elf_start_offset = 0x100; + map_info->offset = 0x200; + +#if 0 + elf = new ElfFake(new MemoryFake); + interface = new ElfInterfaceFake(nullptr); + interface->FakePushFunctionData(FunctionData("Fake0", 10)); + AddMapInfo(0x110000, 0x111000, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, "/fake/elf.so", elf); +#endif } void SetUp() override { @@ -1566,4 +1594,161 @@ TEST_F(UnwinderTest, format_frame_by_arch) { } } +TEST_F(UnwinderTest, build_frame_pc_only_errors) { + RegsFake regs(10); + regs.FakeSetArch(ARCH_ARM); + Unwinder unwinder(10, maps_.get(), ®s, process_memory_); + + FrameData frame; + + // Pc not in map + frame = unwinder.BuildFrameFromPcOnly(0x10); + EXPECT_EQ(0x10U, frame.pc); + EXPECT_EQ(0x10U, frame.rel_pc); + + // No regs set + unwinder.SetRegs(nullptr); + frame = unwinder.BuildFrameFromPcOnly(0x100310); + EXPECT_EQ(0x100310U, frame.pc); + EXPECT_EQ(0x100310U, frame.rel_pc); + unwinder.SetRegs(®s); + + // Invalid elf + frame = unwinder.BuildFrameFromPcOnly(0x100310); + EXPECT_EQ(0x10030eU, frame.pc); + EXPECT_EQ(0x60eU, frame.rel_pc); + EXPECT_EQ("/fake/jit.so", frame.map_name); + EXPECT_EQ(0x100U, frame.map_elf_start_offset); + EXPECT_EQ(0x200U, frame.map_exact_offset); + EXPECT_EQ(0x100000U, frame.map_start); + EXPECT_EQ(0x101000U, frame.map_end); + EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame.map_flags); + EXPECT_EQ(0x300U, frame.map_load_bias); + EXPECT_EQ("", frame.function_name); + EXPECT_EQ(0U, frame.function_offset); +} + +TEST_F(UnwinderTest, build_frame_pc_valid_elf) { + RegsFake regs(10); + regs.FakeSetArch(ARCH_ARM); + Unwinder unwinder(10, maps_.get(), ®s, process_memory_); + + FrameData frame; + + // Valid elf, no function data. + frame = unwinder.BuildFrameFromPcOnly(0x1010); + EXPECT_EQ(0x100cU, frame.pc); + EXPECT_EQ(0xcU, frame.rel_pc); + EXPECT_EQ("/system/fake/libc.so", frame.map_name); + EXPECT_EQ(0U, frame.map_elf_start_offset); + EXPECT_EQ(0U, frame.map_exact_offset); + EXPECT_EQ(0x1000U, frame.map_start); + EXPECT_EQ(0x8000U, frame.map_end); + EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags); + EXPECT_EQ(0U, frame.map_load_bias); + EXPECT_EQ("", frame.function_name); + EXPECT_EQ(0U, frame.function_offset); + + // Valid elf, function data present, but do not resolve. + ElfInterfaceFake::FakePushFunctionData(FunctionData("Frame0", 10)); + unwinder.SetResolveNames(false); + + frame = unwinder.BuildFrameFromPcOnly(0x1010); + EXPECT_EQ(0x100cU, frame.pc); + EXPECT_EQ(0xcU, frame.rel_pc); + EXPECT_EQ("/system/fake/libc.so", frame.map_name); + EXPECT_EQ(0U, frame.map_elf_start_offset); + EXPECT_EQ(0U, frame.map_exact_offset); + EXPECT_EQ(0x1000U, frame.map_start); + EXPECT_EQ(0x8000U, frame.map_end); + EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags); + EXPECT_EQ(0U, frame.map_load_bias); + EXPECT_EQ("", frame.function_name); + EXPECT_EQ(0U, frame.function_offset); + + // Valid elf, function data present. + unwinder.SetResolveNames(true); + + frame = unwinder.BuildFrameFromPcOnly(0x1010); + EXPECT_EQ(0x100cU, frame.pc); + EXPECT_EQ(0xcU, frame.rel_pc); + EXPECT_EQ("/system/fake/libc.so", frame.map_name); + EXPECT_EQ(0U, frame.map_elf_start_offset); + EXPECT_EQ(0U, frame.map_exact_offset); + EXPECT_EQ(0x1000U, frame.map_start); + EXPECT_EQ(0x8000U, frame.map_end); + EXPECT_EQ(PROT_READ | PROT_WRITE, frame.map_flags); + EXPECT_EQ(0U, frame.map_load_bias); + EXPECT_EQ("Frame0", frame.function_name); + EXPECT_EQ(10U, frame.function_offset); +} + +TEST_F(UnwinderTest, build_frame_pc_in_jit) { + // Create the elf data for the jit debug information. + Elf32_Ehdr ehdr = {}; + TestInitEhdr(&ehdr, ELFCLASS32, EM_ARM); + ehdr.e_phoff = 0x50; + ehdr.e_phnum = 1; + ehdr.e_phentsize = sizeof(Elf32_Phdr); + ehdr.e_shoff = 0x100; + ehdr.e_shstrndx = 1; + ehdr.e_shentsize = sizeof(Elf32_Shdr); + ehdr.e_shnum = 3; + memory_->SetMemory(0xf7000, &ehdr, sizeof(ehdr)); + + Elf32_Phdr phdr = {}; + phdr.p_flags = PF_X; + phdr.p_type = PT_LOAD; + phdr.p_offset = 0x100000; + phdr.p_vaddr = 0x100000; + phdr.p_memsz = 0x1000; + memory_->SetMemory(0xf7050, &phdr, sizeof(phdr)); + + Elf32_Shdr shdr = {}; + shdr.sh_type = SHT_NULL; + memory_->SetMemory(0xf7100, &shdr, sizeof(shdr)); + + shdr.sh_type = SHT_SYMTAB; + shdr.sh_link = 2; + shdr.sh_addr = 0x300; + shdr.sh_offset = 0x300; + shdr.sh_entsize = sizeof(Elf32_Sym); + shdr.sh_size = shdr.sh_entsize; + memory_->SetMemory(0xf7100 + sizeof(shdr), &shdr, sizeof(shdr)); + + memset(&shdr, 0, sizeof(shdr)); + shdr.sh_type = SHT_STRTAB; + shdr.sh_name = 0x500; + shdr.sh_offset = 0x400; + shdr.sh_size = 0x100; + memory_->SetMemory(0xf7100 + 2 * sizeof(shdr), &shdr, sizeof(shdr)); + + Elf32_Sym sym = {}; + sym.st_shndx = 2; + sym.st_info = STT_FUNC; + sym.st_value = 0x100300; + sym.st_size = 0x100; + memory_->SetMemory(0xf7300, &sym, sizeof(sym)); + memory_->SetMemory(0xf7400, "FakeJitFunction"); + + RegsFake regs(10); + regs.FakeSetArch(ARCH_ARM); + JitDebug jit_debug(process_memory_); + Unwinder unwinder(10, maps_.get(), ®s, process_memory_); + unwinder.SetJitDebug(&jit_debug, ARCH_ARM); + + FrameData frame = unwinder.BuildFrameFromPcOnly(0x100310); + EXPECT_EQ(0x10030eU, frame.pc); + EXPECT_EQ(0x60eU, frame.rel_pc); + EXPECT_EQ("/fake/jit.so", frame.map_name); + EXPECT_EQ(0x100U, frame.map_elf_start_offset); + EXPECT_EQ(0x200U, frame.map_exact_offset); + EXPECT_EQ(0x100000U, frame.map_start); + EXPECT_EQ(0x101000U, frame.map_end); + EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, frame.map_flags); + EXPECT_EQ(0U, frame.map_load_bias); + EXPECT_EQ("FakeJitFunction", frame.function_name); + EXPECT_EQ(0xeU, frame.function_offset); +} + } // namespace unwindstack -- cgit v1.2.3