blob: 3fcd2b61f18c6177fcc41d3881d8ad0fb4b5edce [file] [log] [blame]
Christopher Ferris53a3c9b2017-05-10 18:34:15 -07001/*
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 Ferrisd226a512017-07-14 10:37:19 -070022#include <unwindstack/DwarfSection.h>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070023
24#include "MemoryFake.h"
25
Christopher Ferrisd226a512017-07-14 10:37:19 -070026namespace unwindstack {
27
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070028class MockDwarfSection : public DwarfSection {
29 public:
30 MockDwarfSection(Memory* memory) : DwarfSection(memory) {}
31 virtual ~MockDwarfSection() = default;
32
33 MOCK_METHOD4(Log, bool(uint8_t, uint64_t, uint64_t, const DwarfFde*));
34
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070035 MOCK_METHOD5(Eval, bool(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070036
37 MOCK_METHOD3(GetCfaLocationInfo, bool(uint64_t, const DwarfFde*, dwarf_loc_regs_t*));
38
39 MOCK_METHOD2(Init, bool(uint64_t, uint64_t));
40
41 MOCK_METHOD2(GetFdeOffsetFromPc, bool(uint64_t, uint64_t*));
42
43 MOCK_METHOD1(GetFdeFromOffset, const DwarfFde*(uint64_t));
44
45 MOCK_METHOD1(GetFdeFromIndex, const DwarfFde*(size_t));
46
47 MOCK_METHOD1(IsCie32, bool(uint32_t));
48
49 MOCK_METHOD1(IsCie64, bool(uint64_t));
50
51 MOCK_METHOD1(GetCieOffsetFromFde32, uint64_t(uint32_t));
52
53 MOCK_METHOD1(GetCieOffsetFromFde64, uint64_t(uint64_t));
54
55 MOCK_METHOD1(AdjustPcFromFde, uint64_t(uint64_t));
56};
57
58class DwarfSectionTest : public ::testing::Test {
59 protected:
60 MemoryFake memory_;
61};
62
63TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_from_pc) {
64 MockDwarfSection mock_section(&memory_);
65
66 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
67 .WillOnce(::testing::Return(false));
68
69 // Verify nullptr when GetFdeOffsetFromPc fails.
70 ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
71}
72
73TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_fde_pc_end) {
74 MockDwarfSection mock_section(&memory_);
75
76 DwarfFde fde{};
77 fde.pc_end = 0x500;
78
79 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
80 .WillOnce(::testing::Return(true));
81 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
82
83 // Verify nullptr when GetFdeOffsetFromPc fails.
84 ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
85}
86
87TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_pass) {
88 MockDwarfSection mock_section(&memory_);
89
90 DwarfFde fde{};
91 fde.pc_end = 0x2000;
92
93 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
94 .WillOnce(::testing::Return(true));
95 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
96
97 // Verify nullptr when GetFdeOffsetFromPc fails.
98 ASSERT_EQ(&fde, mock_section.GetFdeFromPc(0x1000));
99}
100
101TEST_F(DwarfSectionTest, Step_fail_fde) {
102 MockDwarfSection mock_section(&memory_);
103
104 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
105 .WillOnce(::testing::Return(false));
106
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700107 bool finished;
108 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700109}
110
111TEST_F(DwarfSectionTest, Step_fail_cie_null) {
112 MockDwarfSection mock_section(&memory_);
113
114 DwarfFde fde{};
115 fde.pc_end = 0x2000;
116 fde.cie = nullptr;
117
118 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
119 .WillOnce(::testing::Return(true));
120 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
121
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700122 bool finished;
123 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700124}
125
126TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
127 MockDwarfSection mock_section(&memory_);
128
129 DwarfCie cie{};
130 DwarfFde fde{};
131 fde.pc_end = 0x2000;
132 fde.cie = &cie;
133
134 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
135 .WillOnce(::testing::Return(true));
136 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
137
138 EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
139 .WillOnce(::testing::Return(false));
140
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700141 bool finished;
142 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700143}
144
145TEST_F(DwarfSectionTest, Step_pass) {
146 MockDwarfSection mock_section(&memory_);
147
148 DwarfCie cie{};
149 DwarfFde fde{};
150 fde.pc_end = 0x2000;
151 fde.cie = &cie;
152
153 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
154 .WillOnce(::testing::Return(true));
155 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
156
157 EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
158 .WillOnce(::testing::Return(true));
159
160 MemoryFake process;
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700161 EXPECT_CALL(mock_section, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700162 .WillOnce(::testing::Return(true));
163
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700164 bool finished;
165 ASSERT_TRUE(mock_section.Step(0x1000, nullptr, &process, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700166}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700167
168} // namespace unwindstack