Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 <stdint.h> |
| 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 22 | #include <unwindstack/DwarfSection.h> |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 23 | #include <unwindstack/Elf.h> |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 24 | |
| 25 | #include "MemoryFake.h" |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 26 | #include "RegsFake.h" |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 27 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 28 | namespace unwindstack { |
| 29 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 30 | class MockDwarfSection : public DwarfSection { |
| 31 | public: |
| 32 | MockDwarfSection(Memory* memory) : DwarfSection(memory) {} |
| 33 | virtual ~MockDwarfSection() = default; |
| 34 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 35 | MOCK_METHOD(bool, Init, (uint64_t, uint64_t, int64_t), (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 37 | MOCK_METHOD(bool, Eval, (const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*), |
| 38 | (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 39 | |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 40 | MOCK_METHOD(bool, Log, (uint8_t, uint64_t, const DwarfFde*, ArchEnum arch), (override)); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 41 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 42 | MOCK_METHOD(void, GetFdes, (std::vector<const DwarfFde*>*), (override)); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 43 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 44 | MOCK_METHOD(const DwarfFde*, GetFdeFromPc, (uint64_t), (override)); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 45 | |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 46 | MOCK_METHOD(bool, GetCfaLocationInfo, |
| 47 | (uint64_t, const DwarfFde*, dwarf_loc_regs_t*, ArchEnum arch), (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 48 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 49 | MOCK_METHOD(uint64_t, GetCieOffsetFromFde32, (uint32_t), (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 50 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 51 | MOCK_METHOD(uint64_t, GetCieOffsetFromFde64, (uint64_t), (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 52 | |
Christopher Ferris | baf058b | 2019-10-11 14:26:55 -0700 | [diff] [blame] | 53 | MOCK_METHOD(uint64_t, AdjustPcFromFde, (uint64_t), (override)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class DwarfSectionTest : public ::testing::Test { |
| 57 | protected: |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 58 | void SetUp() override { section_.reset(new MockDwarfSection(&memory_)); } |
| 59 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 60 | MemoryFake memory_; |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 61 | std::unique_ptr<MockDwarfSection> section_; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 62 | static RegsFake regs_; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 65 | RegsFake DwarfSectionTest::regs_(10); |
| 66 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 67 | TEST_F(DwarfSectionTest, Step_fail_fde) { |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 68 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(nullptr)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 69 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 70 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 71 | bool is_signal_frame; |
| 72 | ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished, &is_signal_frame)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST_F(DwarfSectionTest, Step_fail_cie_null) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 76 | DwarfFde fde{}; |
| 77 | fde.pc_end = 0x2000; |
| 78 | fde.cie = nullptr; |
| 79 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 80 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 81 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 82 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 83 | bool is_signal_frame; |
| 84 | ASSERT_FALSE(section_->Step(0x1000, ®s_, nullptr, &finished, &is_signal_frame)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST_F(DwarfSectionTest, Step_fail_cfa_location) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 88 | DwarfCie cie{}; |
| 89 | DwarfFde fde{}; |
| 90 | fde.pc_end = 0x2000; |
| 91 | fde.cie = &cie; |
| 92 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 93 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde)); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 94 | EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_)) |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 95 | .WillOnce(::testing::Return(false)); |
| 96 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 97 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 98 | bool is_signal_frame; |
| 99 | ASSERT_FALSE(section_->Step(0x1000, ®s_, nullptr, &finished, &is_signal_frame)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(DwarfSectionTest, Step_pass) { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 103 | DwarfCie cie{}; |
| 104 | DwarfFde fde{}; |
| 105 | fde.pc_end = 0x2000; |
| 106 | fde.cie = &cie; |
| 107 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 108 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde)); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 109 | EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_)) |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 110 | .WillOnce(::testing::Return(true)); |
| 111 | |
| 112 | MemoryFake process; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 113 | EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, ®s_, ::testing::_)) |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 114 | .WillOnce(::testing::Return(true)); |
| 115 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 116 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 117 | bool is_signal_frame; |
| 118 | ASSERT_TRUE(section_->Step(0x1000, ®s_, &process, &finished, &is_signal_frame)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 119 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 120 | |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 121 | static bool MockGetCfaLocationInfo(::testing::Unused, const DwarfFde* fde, |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 122 | dwarf_loc_regs_t* loc_regs, ArchEnum) { |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 123 | loc_regs->pc_start = fde->pc_start; |
| 124 | loc_regs->pc_end = fde->pc_end; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | TEST_F(DwarfSectionTest, Step_cache) { |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 129 | DwarfCie cie{}; |
| 130 | DwarfFde fde{}; |
| 131 | fde.pc_start = 0x500; |
| 132 | fde.pc_end = 0x2000; |
| 133 | fde.cie = &cie; |
| 134 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 135 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde)); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 136 | EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_)) |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 137 | .WillOnce(::testing::Invoke(MockGetCfaLocationInfo)); |
| 138 | |
| 139 | MemoryFake process; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 140 | EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, ®s_, ::testing::_)) |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 141 | .WillRepeatedly(::testing::Return(true)); |
| 142 | |
| 143 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 144 | bool is_signal_frame; |
| 145 | ASSERT_TRUE(section_->Step(0x1000, ®s_, &process, &finished, &is_signal_frame)); |
| 146 | ASSERT_TRUE(section_->Step(0x1000, ®s_, &process, &finished, &is_signal_frame)); |
| 147 | ASSERT_TRUE(section_->Step(0x1500, ®s_, &process, &finished, &is_signal_frame)); |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST_F(DwarfSectionTest, Step_cache_not_in_pc) { |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 151 | DwarfCie cie{}; |
| 152 | DwarfFde fde0{}; |
| 153 | fde0.pc_start = 0x1000; |
| 154 | fde0.pc_end = 0x2000; |
| 155 | fde0.cie = &cie; |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 156 | EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0)); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 157 | EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_, ::testing::_)) |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 158 | .WillOnce(::testing::Invoke(MockGetCfaLocationInfo)); |
| 159 | |
| 160 | MemoryFake process; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 161 | EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, ®s_, ::testing::_)) |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 162 | .WillRepeatedly(::testing::Return(true)); |
| 163 | |
| 164 | bool finished; |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 165 | bool is_signal_frame; |
| 166 | ASSERT_TRUE(section_->Step(0x1000, ®s_, &process, &finished, &is_signal_frame)); |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 167 | |
| 168 | DwarfFde fde1{}; |
| 169 | fde1.pc_start = 0x500; |
| 170 | fde1.pc_end = 0x800; |
| 171 | fde1.cie = &cie; |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 172 | EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1)); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame] | 173 | EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_, ::testing::_)) |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 174 | .WillOnce(::testing::Invoke(MockGetCfaLocationInfo)); |
| 175 | |
Ryan Prichard | 9b8f545 | 2020-09-30 18:31:05 -0700 | [diff] [blame] | 176 | ASSERT_TRUE(section_->Step(0x600, ®s_, &process, &finished, &is_signal_frame)); |
| 177 | ASSERT_TRUE(section_->Step(0x700, ®s_, &process, &finished, &is_signal_frame)); |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 180 | } // namespace unwindstack |