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