• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_METHOD4(Log, bool(uint8_t, uint64_t, uint64_t, const DwarfFde*));
34 
35   MOCK_METHOD4(Eval, bool(const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*));
36 
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 
58 class DwarfSectionTest : public ::testing::Test {
59  protected:
60   MemoryFake memory_;
61 };
62 
TEST_F(DwarfSectionTest,GetFdeOffsetFromPc_fail_from_pc)63 TEST_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 
TEST_F(DwarfSectionTest,GetFdeOffsetFromPc_fail_fde_pc_end)73 TEST_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 
TEST_F(DwarfSectionTest,GetFdeOffsetFromPc_pass)87 TEST_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 
TEST_F(DwarfSectionTest,Step_fail_fde)101 TEST_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 
107   ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
108 }
109 
TEST_F(DwarfSectionTest,Step_fail_cie_null)110 TEST_F(DwarfSectionTest, Step_fail_cie_null) {
111   MockDwarfSection mock_section(&memory_);
112 
113   DwarfFde fde{};
114   fde.pc_end = 0x2000;
115   fde.cie = nullptr;
116 
117   EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
118       .WillOnce(::testing::Return(true));
119   EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
120 
121   ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
122 }
123 
TEST_F(DwarfSectionTest,Step_fail_cfa_location)124 TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
125   MockDwarfSection mock_section(&memory_);
126 
127   DwarfCie cie{};
128   DwarfFde fde{};
129   fde.pc_end = 0x2000;
130   fde.cie = &cie;
131 
132   EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
133       .WillOnce(::testing::Return(true));
134   EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
135 
136   EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
137       .WillOnce(::testing::Return(false));
138 
139   ASSERT_FALSE(mock_section.Step(0x1000, nullptr, nullptr));
140 }
141 
TEST_F(DwarfSectionTest,Step_pass)142 TEST_F(DwarfSectionTest, Step_pass) {
143   MockDwarfSection mock_section(&memory_);
144 
145   DwarfCie cie{};
146   DwarfFde fde{};
147   fde.pc_end = 0x2000;
148   fde.cie = &cie;
149 
150   EXPECT_CALL(mock_section, GetFdeOffsetFromPc(0x1000, ::testing::_))
151       .WillOnce(::testing::Return(true));
152   EXPECT_CALL(mock_section, GetFdeFromOffset(::testing::_)).WillOnce(::testing::Return(&fde));
153 
154   EXPECT_CALL(mock_section, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
155       .WillOnce(::testing::Return(true));
156 
157   MemoryFake process;
158   EXPECT_CALL(mock_section, Eval(&cie, &process, ::testing::_, nullptr))
159       .WillOnce(::testing::Return(true));
160 
161   ASSERT_TRUE(mock_section.Step(0x1000, nullptr, &process));
162 }
163 
164 }  // namespace unwindstack
165