blob: e5a1aed12d6507fcd07cc35a4eec17d4faac8107 [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>
Tamas Petz6835d012020-01-22 14:22:41 +010023#include <unwindstack/Elf.h>
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070024
25#include "MemoryFake.h"
Tamas Petz6835d012020-01-22 14:22:41 +010026#include "RegsFake.h"
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070027
Christopher Ferrisd226a512017-07-14 10:37:19 -070028namespace unwindstack {
29
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070030class MockDwarfSection : public DwarfSection {
31 public:
32 MockDwarfSection(Memory* memory) : DwarfSection(memory) {}
33 virtual ~MockDwarfSection() = default;
34
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070035 MOCK_METHOD(bool, Init, (uint64_t, uint64_t, int64_t), (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070036
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070037 MOCK_METHOD(bool, Eval, (const DwarfCie*, Memory*, const dwarf_loc_regs_t&, Regs*, bool*),
38 (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070039
Tamas Petz6835d012020-01-22 14:22:41 +010040 MOCK_METHOD(bool, Log, (uint8_t, uint64_t, const DwarfFde*, ArchEnum arch), (override));
Christopher Ferris92acaac2018-06-21 10:44:02 -070041
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070042 MOCK_METHOD(void, GetFdes, (std::vector<const DwarfFde*>*), (override));
Christopher Ferris92acaac2018-06-21 10:44:02 -070043
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070044 MOCK_METHOD(const DwarfFde*, GetFdeFromPc, (uint64_t), (override));
Christopher Ferris92acaac2018-06-21 10:44:02 -070045
Tamas Petz6835d012020-01-22 14:22:41 +010046 MOCK_METHOD(bool, GetCfaLocationInfo,
47 (uint64_t, const DwarfFde*, dwarf_loc_regs_t*, ArchEnum arch), (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070048
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070049 MOCK_METHOD(uint64_t, GetCieOffsetFromFde32, (uint32_t), (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070050
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070051 MOCK_METHOD(uint64_t, GetCieOffsetFromFde64, (uint64_t), (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070052
Christopher Ferrisbaf058b2019-10-11 14:26:55 -070053 MOCK_METHOD(uint64_t, AdjustPcFromFde, (uint64_t), (override));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070054};
55
56class DwarfSectionTest : public ::testing::Test {
57 protected:
Christopher Ferris92acaac2018-06-21 10:44:02 -070058 void SetUp() override { section_.reset(new MockDwarfSection(&memory_)); }
59
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070060 MemoryFake memory_;
Christopher Ferris92acaac2018-06-21 10:44:02 -070061 std::unique_ptr<MockDwarfSection> section_;
Tamas Petz6835d012020-01-22 14:22:41 +010062 static RegsFake regs_;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070063};
64
Tamas Petz6835d012020-01-22 14:22:41 +010065RegsFake DwarfSectionTest::regs_(10);
66
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070067TEST_F(DwarfSectionTest, Step_fail_fde) {
Christopher Ferris92acaac2018-06-21 10:44:02 -070068 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(nullptr));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070069
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070070 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -070071 bool is_signal_frame;
72 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished, &is_signal_frame));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070073}
74
75TEST_F(DwarfSectionTest, Step_fail_cie_null) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070076 DwarfFde fde{};
77 fde.pc_end = 0x2000;
78 fde.cie = nullptr;
79
Christopher Ferris92acaac2018-06-21 10:44:02 -070080 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070081
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070082 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -070083 bool is_signal_frame;
84 ASSERT_FALSE(section_->Step(0x1000, &regs_, nullptr, &finished, &is_signal_frame));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070085}
86
87TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070088 DwarfCie cie{};
89 DwarfFde fde{};
90 fde.pc_end = 0x2000;
91 fde.cie = &cie;
92
Christopher Ferris92acaac2018-06-21 10:44:02 -070093 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +010094 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070095 .WillOnce(::testing::Return(false));
96
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070097 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -070098 bool is_signal_frame;
99 ASSERT_FALSE(section_->Step(0x1000, &regs_, nullptr, &finished, &is_signal_frame));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700100}
101
102TEST_F(DwarfSectionTest, Step_pass) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700103 DwarfCie cie{};
104 DwarfFde fde{};
105 fde.pc_end = 0x2000;
106 fde.cie = &cie;
107
Christopher Ferris92acaac2018-06-21 10:44:02 -0700108 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +0100109 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700110 .WillOnce(::testing::Return(true));
111
112 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100113 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700114 .WillOnce(::testing::Return(true));
115
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700116 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700117 bool is_signal_frame;
118 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished, &is_signal_frame));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700119}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700120
David Srbecky3386eba2018-03-14 21:30:25 +0000121static bool MockGetCfaLocationInfo(::testing::Unused, const DwarfFde* fde,
Tamas Petz6835d012020-01-22 14:22:41 +0100122 dwarf_loc_regs_t* loc_regs, ArchEnum) {
David Srbecky3386eba2018-03-14 21:30:25 +0000123 loc_regs->pc_start = fde->pc_start;
124 loc_regs->pc_end = fde->pc_end;
125 return true;
126}
127
128TEST_F(DwarfSectionTest, Step_cache) {
David Srbecky3386eba2018-03-14 21:30:25 +0000129 DwarfCie cie{};
130 DwarfFde fde{};
131 fde.pc_start = 0x500;
132 fde.pc_end = 0x2000;
133 fde.cie = &cie;
134
Christopher Ferris92acaac2018-06-21 10:44:02 -0700135 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +0100136 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000137 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
138
139 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100140 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000141 .WillRepeatedly(::testing::Return(true));
142
143 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700144 bool is_signal_frame;
145 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished, &is_signal_frame));
146 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished, &is_signal_frame));
147 ASSERT_TRUE(section_->Step(0x1500, &regs_, &process, &finished, &is_signal_frame));
David Srbecky3386eba2018-03-14 21:30:25 +0000148}
149
150TEST_F(DwarfSectionTest, Step_cache_not_in_pc) {
David Srbecky3386eba2018-03-14 21:30:25 +0000151 DwarfCie cie{};
152 DwarfFde fde0{};
153 fde0.pc_start = 0x1000;
154 fde0.pc_end = 0x2000;
155 fde0.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700156 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0));
Tamas Petz6835d012020-01-22 14:22:41 +0100157 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000158 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
159
160 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100161 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000162 .WillRepeatedly(::testing::Return(true));
163
164 bool finished;
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700165 bool is_signal_frame;
166 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished, &is_signal_frame));
David Srbecky3386eba2018-03-14 21:30:25 +0000167
168 DwarfFde fde1{};
169 fde1.pc_start = 0x500;
170 fde1.pc_end = 0x800;
171 fde1.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700172 EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1));
Tamas Petz6835d012020-01-22 14:22:41 +0100173 EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000174 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
175
Ryan Prichard9b8f5452020-09-30 18:31:05 -0700176 ASSERT_TRUE(section_->Step(0x600, &regs_, &process, &finished, &is_signal_frame));
177 ASSERT_TRUE(section_->Step(0x700, &regs_, &process, &finished, &is_signal_frame));
David Srbecky3386eba2018-03-14 21:30:25 +0000178}
179
Christopher Ferrisd226a512017-07-14 10:37:19 -0700180} // namespace unwindstack