blob: febd6d326c283c33677a3c866dd2b5db925203b2 [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;
Christopher Ferris92acaac2018-06-21 10:44:02 -070071 ASSERT_FALSE(section_->Step(0x1000, nullptr, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070072}
73
74TEST_F(DwarfSectionTest, Step_fail_cie_null) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070075 DwarfFde fde{};
76 fde.pc_end = 0x2000;
77 fde.cie = nullptr;
78
Christopher Ferris92acaac2018-06-21 10:44:02 -070079 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070080
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070081 bool finished;
Tamas Petz6835d012020-01-22 14:22:41 +010082 ASSERT_FALSE(section_->Step(0x1000, &regs_, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070083}
84
85TEST_F(DwarfSectionTest, Step_fail_cfa_location) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070086 DwarfCie cie{};
87 DwarfFde fde{};
88 fde.pc_end = 0x2000;
89 fde.cie = &cie;
90
Christopher Ferris92acaac2018-06-21 10:44:02 -070091 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +010092 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070093 .WillOnce(::testing::Return(false));
94
Christopher Ferrisb9de87f2017-09-20 13:37:24 -070095 bool finished;
Tamas Petz6835d012020-01-22 14:22:41 +010096 ASSERT_FALSE(section_->Step(0x1000, &regs_, nullptr, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070097}
98
99TEST_F(DwarfSectionTest, Step_pass) {
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700100 DwarfCie cie{};
101 DwarfFde fde{};
102 fde.pc_end = 0x2000;
103 fde.cie = &cie;
104
Christopher Ferris92acaac2018-06-21 10:44:02 -0700105 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +0100106 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700107 .WillOnce(::testing::Return(true));
108
109 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100110 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700111 .WillOnce(::testing::Return(true));
112
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700113 bool finished;
Tamas Petz6835d012020-01-22 14:22:41 +0100114 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700115}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700116
David Srbecky3386eba2018-03-14 21:30:25 +0000117static bool MockGetCfaLocationInfo(::testing::Unused, const DwarfFde* fde,
Tamas Petz6835d012020-01-22 14:22:41 +0100118 dwarf_loc_regs_t* loc_regs, ArchEnum) {
David Srbecky3386eba2018-03-14 21:30:25 +0000119 loc_regs->pc_start = fde->pc_start;
120 loc_regs->pc_end = fde->pc_end;
121 return true;
122}
123
124TEST_F(DwarfSectionTest, Step_cache) {
David Srbecky3386eba2018-03-14 21:30:25 +0000125 DwarfCie cie{};
126 DwarfFde fde{};
127 fde.pc_start = 0x500;
128 fde.pc_end = 0x2000;
129 fde.cie = &cie;
130
Christopher Ferris92acaac2018-06-21 10:44:02 -0700131 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde));
Tamas Petz6835d012020-01-22 14:22:41 +0100132 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000133 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
134
135 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100136 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000137 .WillRepeatedly(::testing::Return(true));
138
139 bool finished;
Tamas Petz6835d012020-01-22 14:22:41 +0100140 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished));
141 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished));
142 ASSERT_TRUE(section_->Step(0x1500, &regs_, &process, &finished));
David Srbecky3386eba2018-03-14 21:30:25 +0000143}
144
145TEST_F(DwarfSectionTest, Step_cache_not_in_pc) {
David Srbecky3386eba2018-03-14 21:30:25 +0000146 DwarfCie cie{};
147 DwarfFde fde0{};
148 fde0.pc_start = 0x1000;
149 fde0.pc_end = 0x2000;
150 fde0.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700151 EXPECT_CALL(*section_, GetFdeFromPc(0x1000)).WillOnce(::testing::Return(&fde0));
Tamas Petz6835d012020-01-22 14:22:41 +0100152 EXPECT_CALL(*section_, GetCfaLocationInfo(0x1000, &fde0, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000153 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
154
155 MemoryFake process;
Tamas Petz6835d012020-01-22 14:22:41 +0100156 EXPECT_CALL(*section_, Eval(&cie, &process, ::testing::_, &regs_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000157 .WillRepeatedly(::testing::Return(true));
158
159 bool finished;
Tamas Petz6835d012020-01-22 14:22:41 +0100160 ASSERT_TRUE(section_->Step(0x1000, &regs_, &process, &finished));
David Srbecky3386eba2018-03-14 21:30:25 +0000161
162 DwarfFde fde1{};
163 fde1.pc_start = 0x500;
164 fde1.pc_end = 0x800;
165 fde1.cie = &cie;
Christopher Ferris92acaac2018-06-21 10:44:02 -0700166 EXPECT_CALL(*section_, GetFdeFromPc(0x600)).WillOnce(::testing::Return(&fde1));
Tamas Petz6835d012020-01-22 14:22:41 +0100167 EXPECT_CALL(*section_, GetCfaLocationInfo(0x600, &fde1, ::testing::_, ::testing::_))
David Srbecky3386eba2018-03-14 21:30:25 +0000168 .WillOnce(::testing::Invoke(MockGetCfaLocationInfo));
169
Tamas Petz6835d012020-01-22 14:22:41 +0100170 ASSERT_TRUE(section_->Step(0x600, &regs_, &process, &finished));
171 ASSERT_TRUE(section_->Step(0x700, &regs_, &process, &finished));
David Srbecky3386eba2018-03-14 21:30:25 +0000172}
173
Christopher Ferrisd226a512017-07-14 10:37:19 -0700174} // namespace unwindstack