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