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
22 #include <unwindstack/DwarfSection.h>
23
24 #include "MemoryFake.h"
25
26 namespace unwindstack {
27
28 class MockDwarfSection : public DwarfSection {
29 public:
MockDwarfSection(Memory * memory)30 MockDwarfSection(Memory* memory) : DwarfSection(memory) {}
31 virtual ~MockDwarfSection() = default;
32
33 MOCK_METHOD3(Init, bool(uint64_t, uint64_t, uint64_t));
34
35 MOCK_METHOD5(Eval, bool(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*));
36
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
43 MOCK_METHOD3(GetCfaLocationInfo, bool(uint64_t, const DwarfFde*, dwarf_loc_regs_t*));
44
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:
SetUp()54 void SetUp() override { section_.reset(new MockDwarfSection(&memory_)); }
55
56 MemoryFake memory_;
57 std::unique_ptr<MockDwarfSection> section_;
58 };
59
TEST_F(DwarfSectionTest,Step_fail_fde)60 TEST_F(DwarfSectionTest, Step_fail_fde) {
61 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(nullptr));
62
63 bool finished;
64 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
65 }
66
TEST_F(DwarfSectionTest,Step_fail_cie_null)67 TEST_F(DwarfSectionTest, Step_fail_cie_null) {
68 DwarfFde fde{};
69 fde.pc_end = 0x2000;
70 fde.cie = nullptr;
71
72 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
73
74 bool finished;
75 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
76 }
77
TEST_F(DwarfSectionTest,Step_fail_cfa_location)78 TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
79 DwarfCie cie{};
80 DwarfFde fde{};
81 fde.pc_end = 0x2000;
82 fde.cie = &cie;
83
84 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
85 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
86 .WillOnce(::testing::Return(false));
87
88 bool finished;
89 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
90 }
91
TEST_F(DwarfSectionTest,Step_pass)92 TEST_F(DwarfSectionTest, Step_pass) {
93 DwarfCie cie{};
94 DwarfFde fde{};
95 fde.pc_end = 0x2000;
96 fde.cie = &cie;
97
98 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
99 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
100 .WillOnce(::testing::Return(true));
101
102 MemoryFake process;
103 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
104 .WillOnce(::testing::Return(true));
105
106 bool finished;
107 ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
108 }
109
MockGetCfaLocationInfo(::testing::Unused,const DwarfFde * fde,dwarf_loc_regs_t * loc_regs)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
TEST_F(DwarfSectionTest,Step_cache)117 TEST_F(DwarfSectionTest, Step_cache) {
118 DwarfCie cie{};
119 DwarfFde fde{};
120 fde.pc_start = 0x500;
121 fde.pc_end = 0x2000;
122 fde.cie = &cie;
123
124 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
125 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
126 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
127
128 MemoryFake process;
129 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
130 .WillRepeatedly(::testing::Return(true));
131
132 bool finished;
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));
136 }
137
TEST_F(DwarfSectionTest,Step_cache_not_in_pc)138 TEST_F(DwarfSectionTest, Step_cache_not_in_pc) {
139 DwarfCie cie{};
140 DwarfFde fde0{};
141 fde0.pc_start = 0x1000;
142 fde0.pc_end = 0x2000;
143 fde0.cie = &cie;
144 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0));
145 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_))
146 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
147
148 MemoryFake process;
149 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
150 .WillRepeatedly(::testing::Return(true));
151
152 bool finished;
153 ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
154
155 DwarfFde fde1{};
156 fde1.pc_start = 0x500;
157 fde1.pc_end = 0x800;
158 fde1.cie = &cie;
159 EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1));
160 EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_))
161 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
162
163 ASSERT_TRUE(section_->Step(0x600, nullptr, &process, &finished));
164 ASSERT_TRUE(section_->Step(0x700, nullptr, &process, &finished));
165 }
166
167 } // namespace unwindstack
168