blob: 20f0e2a499e3798bd563c1a2e3af7030b758ead3 [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
22#include "DwarfSection.h"
23
24#include "MemoryFake.h"
25
26class MockDwarfSection : public DwarfSection {
27 public:
28 MockDwarfSection(Memory* memory) : DwarfSection(memory) {}
29 virtual ~MockDwarfSection() = default;
30
31 MOCK_METHOD4(Log, bool(uint8_t, uint64_t, uint64_t, const DwarfFde*));
32
33 MOCK_METHOD4(Eval, bool(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*));
34
35 MOCK_METHOD3(GetCfaLocationInfo, bool(uint64_t, const DwarfFde*, dwarf_loc_regs_t*));
36
37 MOCK_METHOD2(Init, bool(uint64_t, uint64_t));
38
39 MOCK_METHOD2(GetFdeOffsetFromPc, bool(uint64_t, uint64_t*));
40
41 MOCK_METHOD1(GetFdeFromOffset, const DwarfFde*(uint64_t));
42
43 MOCK_METHOD1(GetFdeFromIndex, const DwarfFde*(size_t));
44
45 MOCK_METHOD1(IsCie32, bool(uint32_t));
46
47 MOCK_METHOD1(IsCie64, bool(uint64_t));
48
49 MOCK_METHOD1(GetCieOffsetFromFde32, uint64_t(uint32_t));
50
51 MOCK_METHOD1(GetCieOffsetFromFde64, uint64_t(uint64_t));
52
53 MOCK_METHOD1(AdjustPcFromFde, uint64_t(uint64_t));
54};
55
56class DwarfSectionTest : public ::testing::Test {
57 protected:
58 MemoryFake memory_;
59};
60
61TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_from_pc) {
62 MockDwarfSection mock_section(&memory_);
63
64 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
65 .WillOnce(::testing::Return(false));
66
67 // Verify nullptr when GetFdeOffsetFromPc fails.
68 ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
69}
70
71TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_fail_fde_pc_end) {
72 MockDwarfSection mock_section(&memory_);
73
74 DwarfFde fde{};
75 fde.pc_end = 0x500;
76
77 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
78 .WillOnce(::testing::Return(true));
79 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
80
81 // Verify nullptr when GetFdeOffsetFromPc fails.
82 ASSERT_TRUE(mock_section.GetFdeFromPc(0x1000) == nullptr);
83}
84
85TEST_F(DwarfSectionTest, GetFdeOffsetFromPc_pass) {
86 MockDwarfSection mock_section(&memory_);
87
88 DwarfFde fde{};
89 fde.pc_end = 0x2000;
90
91 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
92 .WillOnce(::testing::Return(true));
93 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
94
95 // Verify nullptr when GetFdeOffsetFromPc fails.
96 ASSERT_EQ(&fde, mock_section.GetFdeFromPc(0x1000));
97}
98
99TEST_F(DwarfSectionTest, Step_fail_fde) {
100 MockDwarfSection mock_section(&memory_);
101
102 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
103 .WillOnce(::testing::Return(false));
104
105 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
106}
107
108TEST_F(DwarfSectionTest, Step_fail_cie_null) {
109 MockDwarfSection mock_section(&memory_);
110
111 DwarfFde fde{};
112 fde.pc_end = 0x2000;
113 fde.cie = nullptr;
114
115 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
116 .WillOnce(::testing::Return(true));
117 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
118
119 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
120}
121
122TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
123 MockDwarfSection mock_section(&memory_);
124
125 DwarfCie cie{};
126 DwarfFde fde{};
127 fde.pc_end = 0x2000;
128 fde.cie = &cie;
129
130 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
131 .WillOnce(::testing::Return(true));
132 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
133
134 EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
135 .WillOnce(::testing::Return(false));
136
137 ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
138}
139
140TEST_F(DwarfSectionTest, Step_pass) {
141 MockDwarfSection mock_section(&memory_);
142
143 DwarfCie cie{};
144 DwarfFde fde{};
145 fde.pc_end = 0x2000;
146 fde.cie = &cie;
147
148 EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
149 .WillOnce(::testing::Return(true));
150 EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
151
152 EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
153 .WillOnce(::testing::Return(true));
154
155 MemoryFake process;
156 EXPECT_CALL(mock_section, Eval(&cie, &process, ::testing::_, nullptr))
157 .WillOnce(::testing::Return(true));
158
159 ASSERT_TRUE(mock_section.Step(0x1000, nullptr, &process));
160}