blob: d754fcc500874c765d425034eedd30c493d22399 [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
Christopher Ferris92acaac2018-06-21 10:44:02 -070033 MOCK_METHOD3(Init, bool(uint64_t, uint64_t, uint64_t));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070034
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
Christopher Ferris92acaac2018-06-21 10:44:02 -070037 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 Ferris53a3c9b2017-05-10 18:34:15 -070043 MOCK_METHOD3(GetCfaLocationInfo, bool(uint64_t, const DwarfFde*, dwarf_loc_regs_t*));
44
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070045 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
52class DwarfSectionTest : public ::testing::Test {
53 protected:
Christopher Ferris92acaac2018-06-21 10:44:02 -070054 void SetUp() override { section_.reset(new MockDwarfSection(&memory_)); }
55
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070056 MemoryFake memory_;
Christopher Ferris92acaac2018-06-21 10:44:02 -070057 std::unique_ptr<MockDwarfSection> section_;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070058};
59
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070060TEST_F(DwarfSectionTest, Step_fail_fde) {
Christopher Ferris92acaac2018-06-21 10:44:02 -070061 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(nullptr));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070062
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070063 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -070064 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070065}
66
67TEST_F(DwarfSectionTest, Step_fail_cie_null) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070068 DwarfFde fde{};
69 fde.pc_end = 0x2000;
70 fde.cie = nullptr;
71
Christopher Ferris92acaac2018-06-21 10:44:02 -070072 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070073
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070074 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -070075 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070076}
77
78TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070079 DwarfCie cie{};
80 DwarfFde fde{};
81 fde.pc_end = 0x2000;
82 fde.cie = &cie;
83
Christopher Ferris92acaac2018-06-21 10:44:02 -070084 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
85 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070086 .WillOnce(::testing::Return(false));
87
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070088 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -070089 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070090}
91
92TEST_F(DwarfSectionTest, Step_pass) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070093 DwarfCie cie{};
94 DwarfFde fde{};
95 fde.pc_end = 0x2000;
96 fde.cie = &cie;
97
Christopher Ferris92acaac2018-06-21 10:44:02 -070098 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
99 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700100 .WillOnce(::testing::Return(true));
101
102 MemoryFake process;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700103 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700104 .WillOnce(::testing::Return(true));
105
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700106 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700107 ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700108}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700109
David Srbecky3386eba2018-03-14 21:30:25 +0000110static 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
117TEST_F(DwarfSectionTest, Step_cache) {
David Srbecky3386eba2018-03-14 21:30:25 +0000118 DwarfCie cie{};
119 DwarfFde fde{};
120 fde.pc_start = 0x500;
121 fde.pc_end = 0x2000;
122 fde.cie = &cie;
123
Christopher Ferris92acaac2018-06-21 10:44:02 -0700124 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
125 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000126 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
127
128 MemoryFake process;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700129 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000130 .WillRepeatedly(::testing::Return(true));
131
132 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700133 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 Srbecky3386eba2018-03-14 21:30:25 +0000136}
137
138TEST_F(DwarfSectionTest, Step_cache_not_in_pc) {
David Srbecky3386eba2018-03-14 21:30:25 +0000139 DwarfCie cie{};
140 DwarfFde fde0{};
141 fde0.pc_start = 0x1000;
142 fde0.pc_end = 0x2000;
143 fde0.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700144 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0));
145 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000146 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
147
148 MemoryFake process;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700149 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, nullptr, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000150 .WillRepeatedly(::testing::Return(true));
151
152 bool finished;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700153 ASSERT_TRUE(section_->Step(0x1000, nullptr, &process, &finished));
David Srbecky3386eba2018-03-14 21:30:25 +0000154
155 DwarfFde fde1{};
156 fde1.pc_start = 0x500;
157 fde1.pc_end = 0x800;
158 fde1.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700159 EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1));
160 EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000161 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
162
Christopher Ferris92acaac2018-06-21 10:44:02 -0700163 ASSERT_TRUE(section_->Step(0x600, nullptr, &process, &finished));
164 ASSERT_TRUE(section_->Step(0x700, nullptr, &process, &finished));
David Srbecky3386eba2018-03-14 21:30:25 +0000165}
166
Christopher Ferrisd226a512017-07-14 10:37:19 -0700167} // namespace unwindstack