Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 21 | #include <unwindstack/DwarfError.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 22 | #include <unwindstack/DwarfSection.h> |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame^] | 23 | #include <unwindstack/Elf.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 24 | |
| 25 | #include "DwarfEncoding.h" |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 26 | |
| 27 | #include "LogFake.h" |
| 28 | #include "MemoryFake.h" |
| 29 | #include "RegsFake.h" |
| 30 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 | namespace unwindstack { |
| 32 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 33 | template <typename TypeParam> |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 34 | class TestDwarfSectionImpl : public DwarfSectionImpl<TypeParam> { |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 35 | public: |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 36 | TestDwarfSectionImpl(Memory* memory) : DwarfSectionImpl<TypeParam>(memory) {} |
| 37 | virtual ~TestDwarfSectionImpl() = default; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 38 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 39 | bool Init(uint64_t, uint64_t, int64_t) override { return false; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 40 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 41 | void GetFdes(std::vector<const DwarfFde*>*) override {} |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 42 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 43 | const DwarfFde* GetFdeFromPc(uint64_t) override { return nullptr; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 44 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 45 | uint64_t GetCieOffsetFromFde32(uint32_t) { return 0; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 46 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 47 | uint64_t GetCieOffsetFromFde64(uint64_t) { return 0; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 48 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 49 | uint64_t AdjustPcFromFde(uint64_t) override { return 0; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 50 | |
| 51 | void TestSetCachedCieLocRegs(uint64_t offset, const dwarf_loc_regs_t& loc_regs) { |
| 52 | this->cie_loc_regs_[offset] = loc_regs; |
| 53 | } |
| 54 | void TestClearCachedCieLocRegs() { this->cie_loc_regs_.clear(); } |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 55 | void TestClearError() { this->last_error_.code = DWARF_ERROR_NONE; } |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | template <typename TypeParam> |
| 59 | class DwarfSectionImplTest : public ::testing::Test { |
| 60 | protected: |
| 61 | void SetUp() override { |
| 62 | memory_.Clear(); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 63 | section_ = new TestDwarfSectionImpl<TypeParam>(&memory_); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 64 | ResetLogs(); |
| 65 | } |
| 66 | |
| 67 | void TearDown() override { delete section_; } |
| 68 | |
| 69 | MemoryFake memory_; |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 70 | TestDwarfSectionImpl<TypeParam>* section_ = nullptr; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 71 | }; |
Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 72 | TYPED_TEST_SUITE_P(DwarfSectionImplTest); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 73 | |
| 74 | // NOTE: All test class variables need to be referenced as this->. |
| 75 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 76 | TYPED_TEST_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache) { |
| 77 | ASSERT_TRUE(this->section_->GetCieFromOffset(0x4000) == nullptr); |
| 78 | EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode()); |
| 79 | EXPECT_EQ(0x4000U, this->section_->LastErrorAddress()); |
| 80 | |
| 81 | this->section_->TestClearError(); |
| 82 | ASSERT_TRUE(this->section_->GetCieFromOffset(0x4000) == nullptr); |
| 83 | EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode()); |
| 84 | EXPECT_EQ(0x4000U, this->section_->LastErrorAddress()); |
| 85 | } |
| 86 | |
| 87 | TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_fail_should_not_cache) { |
| 88 | ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr); |
| 89 | EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode()); |
| 90 | EXPECT_EQ(0x4000U, this->section_->LastErrorAddress()); |
| 91 | |
| 92 | this->section_->TestClearError(); |
| 93 | ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr); |
| 94 | EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode()); |
| 95 | EXPECT_EQ(0x4000U, this->section_->LastErrorAddress()); |
| 96 | } |
| 97 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 98 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_eval_fail) { |
| 99 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 100 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 101 | dwarf_loc_regs_t loc_regs; |
| 102 | |
| 103 | regs.set_pc(0x100); |
| 104 | regs.set_sp(0x2000); |
| 105 | regs[5] = 0x20; |
| 106 | regs[9] = 0x3000; |
David Srbecky | 3692f25 | 2018-03-08 16:57:19 +0000 | [diff] [blame] | 107 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 108 | bool finished; |
| 109 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 110 | EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode()); |
| 111 | EXPECT_EQ(0x5000U, this->section_->LastErrorAddress()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_no_stack) { |
| 115 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 116 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 117 | dwarf_loc_regs_t loc_regs; |
| 118 | |
| 119 | regs.set_pc(0x100); |
| 120 | regs.set_sp(0x2000); |
| 121 | regs[5] = 0x20; |
| 122 | regs[9] = 0x3000; |
| 123 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x96, 0x96, 0x96}); |
David Srbecky | 3692f25 | 2018-03-08 16:57:19 +0000 | [diff] [blame] | 124 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 125 | bool finished; |
| 126 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 127 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_STATE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr) { |
| 131 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 132 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 133 | dwarf_loc_regs_t loc_regs; |
| 134 | |
| 135 | regs.set_pc(0x100); |
| 136 | regs.set_sp(0x2000); |
| 137 | regs[5] = 0x20; |
| 138 | regs[9] = 0x3000; |
| 139 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80}); |
| 140 | TypeParam cfa_value = 0x12345; |
| 141 | this->memory_.SetMemory(0x80000000, &cfa_value, sizeof(cfa_value)); |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 142 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x4, 0x5004}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 143 | bool finished; |
David Srbecky | 3692f25 | 2018-03-08 16:57:19 +0000 | [diff] [blame] | 144 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 145 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_val_expr) { |
| 149 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 150 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 151 | dwarf_loc_regs_t loc_regs; |
| 152 | |
| 153 | regs.set_pc(0x100); |
| 154 | regs.set_sp(0x2000); |
| 155 | regs[5] = 0x20; |
| 156 | regs[9] = 0x3000; |
| 157 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80}); |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 158 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x4, 0x5004}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 159 | bool finished; |
| 160 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 161 | ASSERT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 162 | EXPECT_EQ(0x80000000U, regs.sp()); |
| 163 | EXPECT_EQ(0x20U, regs.pc()); |
| 164 | } |
| 165 | |
| 166 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_is_register) { |
| 167 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 168 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 169 | dwarf_loc_regs_t loc_regs; |
| 170 | |
| 171 | regs.set_pc(0x100); |
| 172 | regs.set_sp(0x2000); |
| 173 | regs[5] = 0x20; |
| 174 | regs[9] = 0x3000; |
| 175 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x50, 0x96, 0x96}); |
David Srbecky | 3692f25 | 2018-03-08 16:57:19 +0000 | [diff] [blame] | 176 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x2, 0x5002}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 177 | bool finished; |
| 178 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 179 | EXPECT_EQ(DWARF_ERROR_NOT_IMPLEMENTED, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | TYPED_TEST_P(DwarfSectionImplTest, Eval_bad_regs) { |
| 183 | DwarfCie cie{.return_address_register = 60}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 184 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 185 | dwarf_loc_regs_t loc_regs; |
| 186 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 187 | bool finished; |
| 188 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 189 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TYPED_TEST_P(DwarfSectionImplTest, Eval_no_cfa) { |
| 193 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 194 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 195 | dwarf_loc_regs_t loc_regs; |
| 196 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 197 | bool finished; |
| 198 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 199 | EXPECT_EQ(DWARF_ERROR_CFA_NOT_DEFINED, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_bad) { |
| 203 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 204 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 205 | dwarf_loc_regs_t loc_regs; |
| 206 | |
| 207 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {20, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 208 | bool finished; |
| 209 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 210 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 211 | |
| 212 | this->section_->TestClearError(); |
| 213 | loc_regs.erase(CFA_REG); |
| 214 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_INVALID, {0, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 215 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 216 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 217 | |
| 218 | this->section_->TestClearError(); |
| 219 | loc_regs.erase(CFA_REG); |
| 220 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_OFFSET, {0, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 221 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 222 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 223 | |
| 224 | this->section_->TestClearError(); |
| 225 | loc_regs.erase(CFA_REG); |
| 226 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_OFFSET, {0, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 227 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 228 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_prev) { |
| 232 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 233 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 234 | dwarf_loc_regs_t loc_regs; |
| 235 | |
| 236 | regs.set_pc(0x100); |
| 237 | regs.set_sp(0x2000); |
| 238 | regs[5] = 0x20; |
| 239 | regs[9] = 0x3000; |
| 240 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {9, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 241 | bool finished; |
| 242 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 243 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 244 | EXPECT_EQ(0x20U, regs.pc()); |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 245 | EXPECT_EQ(0x3000U, regs.sp()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_from_value) { |
| 249 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 250 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 251 | dwarf_loc_regs_t loc_regs; |
| 252 | |
| 253 | regs.set_pc(0x100); |
| 254 | regs.set_sp(0x2000); |
| 255 | regs[5] = 0x20; |
| 256 | regs[6] = 0x4000; |
| 257 | regs[9] = 0x3000; |
| 258 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {6, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 259 | bool finished; |
| 260 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 261 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 262 | EXPECT_EQ(0x20U, regs.pc()); |
| 263 | EXPECT_EQ(0x4000U, regs.sp()); |
| 264 | } |
| 265 | |
| 266 | TYPED_TEST_P(DwarfSectionImplTest, Eval_double_indirection) { |
| 267 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 268 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 269 | dwarf_loc_regs_t loc_regs; |
| 270 | |
| 271 | regs.set_pc(0x100); |
| 272 | regs.set_sp(0x2000); |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 273 | regs[1] = 0x100; |
| 274 | regs[3] = 0x300; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 275 | regs[8] = 0x10; |
| 276 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 277 | loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {3, 1}}; |
| 278 | loc_regs[9] = DwarfLocation{DWARF_LOCATION_REGISTER, {1, 2}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 279 | bool finished; |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 280 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 281 | EXPECT_EQ(0x301U, regs[1]); |
| 282 | EXPECT_EQ(0x300U, regs[3]); |
| 283 | EXPECT_EQ(0x10U, regs[8]); |
| 284 | EXPECT_EQ(0x102U, regs[9]); |
| 285 | } |
| 286 | |
| 287 | TYPED_TEST_P(DwarfSectionImplTest, Eval_register_reference_chain) { |
| 288 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 289 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 290 | dwarf_loc_regs_t loc_regs; |
| 291 | |
| 292 | regs.set_pc(0x100); |
| 293 | regs.set_sp(0x2000); |
| 294 | regs[0] = 0x10; |
| 295 | regs[1] = 0x20; |
| 296 | regs[2] = 0x30; |
| 297 | regs[3] = 0x40; |
| 298 | regs[4] = 0x50; |
| 299 | regs[5] = 0x60; |
| 300 | regs[8] = 0x20; |
| 301 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 302 | loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {0, 1}}; |
| 303 | loc_regs[2] = DwarfLocation{DWARF_LOCATION_REGISTER, {1, 2}}; |
| 304 | loc_regs[3] = DwarfLocation{DWARF_LOCATION_REGISTER, {2, 3}}; |
| 305 | loc_regs[4] = DwarfLocation{DWARF_LOCATION_REGISTER, {3, 4}}; |
| 306 | loc_regs[5] = DwarfLocation{DWARF_LOCATION_REGISTER, {4, 5}}; |
| 307 | bool finished; |
| 308 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 309 | EXPECT_EQ(0x10U, regs[0]); |
| 310 | EXPECT_EQ(0x11U, regs[1]); |
| 311 | EXPECT_EQ(0x22U, regs[2]); |
| 312 | EXPECT_EQ(0x33U, regs[3]); |
| 313 | EXPECT_EQ(0x44U, regs[4]); |
| 314 | EXPECT_EQ(0x55U, regs[5]); |
| 315 | EXPECT_EQ(0x20U, regs[8]); |
| 316 | } |
| 317 | |
| 318 | TYPED_TEST_P(DwarfSectionImplTest, Eval_dex_pc) { |
| 319 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 320 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 321 | dwarf_loc_regs_t loc_regs; |
| 322 | |
| 323 | regs.set_pc(0x100); |
| 324 | regs.set_sp(0x2000); |
| 325 | regs[0] = 0x10; |
| 326 | regs[8] = 0x20; |
| 327 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 328 | loc_regs[1] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x8, 0x5008}}; |
| 329 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 'D', 'E', 'X', '1', 0x13, 0x08, 0x11}); |
Christopher Ferris | 98984b4 | 2018-01-17 12:59:45 -0800 | [diff] [blame] | 330 | bool finished; |
| 331 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 332 | EXPECT_EQ(0x10U, regs[0]); |
| 333 | EXPECT_EQ(0x20U, regs[8]); |
| 334 | EXPECT_EQ(0x11U, regs.dex_pc()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | TYPED_TEST_P(DwarfSectionImplTest, Eval_invalid_register) { |
| 338 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 339 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 340 | dwarf_loc_regs_t loc_regs; |
| 341 | |
| 342 | regs.set_pc(0x100); |
| 343 | regs.set_sp(0x2000); |
| 344 | regs[8] = 0x10; |
| 345 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 346 | loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {10, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 347 | bool finished; |
| 348 | ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 349 | EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode()); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | TYPED_TEST_P(DwarfSectionImplTest, Eval_different_reg_locations) { |
| 353 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 354 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 355 | dwarf_loc_regs_t loc_regs; |
| 356 | |
| 357 | if (sizeof(TypeParam) == sizeof(uint64_t)) { |
| 358 | this->memory_.SetData64(0x2150, 0x12345678abcdef00ULL); |
| 359 | } else { |
| 360 | this->memory_.SetData32(0x2150, 0x12345678); |
| 361 | } |
| 362 | |
| 363 | regs.set_pc(0x100); |
| 364 | regs.set_sp(0x2000); |
| 365 | regs[3] = 0x234; |
| 366 | regs[5] = 0x10; |
| 367 | regs[8] = 0x2100; |
| 368 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 369 | loc_regs[1] = DwarfLocation{DWARF_LOCATION_VAL_OFFSET, {0x100, 0}}; |
| 370 | loc_regs[2] = DwarfLocation{DWARF_LOCATION_OFFSET, {0x50, 0}}; |
| 371 | loc_regs[3] = DwarfLocation{DWARF_LOCATION_UNDEFINED, {0, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 372 | bool finished; |
| 373 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 374 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 375 | EXPECT_EQ(0x10U, regs.pc()); |
| 376 | EXPECT_EQ(0x2100U, regs.sp()); |
| 377 | EXPECT_EQ(0x2200U, regs[1]); |
| 378 | EXPECT_EQ(0x234U, regs[3]); |
| 379 | if (sizeof(TypeParam) == sizeof(uint64_t)) { |
| 380 | EXPECT_EQ(0x12345678abcdef00ULL, regs[2]); |
| 381 | } else { |
| 382 | EXPECT_EQ(0x12345678U, regs[2]); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address_undefined) { |
| 387 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 388 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 389 | dwarf_loc_regs_t loc_regs; |
| 390 | |
| 391 | regs.set_pc(0x100); |
| 392 | regs.set_sp(0x2000); |
| 393 | regs[5] = 0x20; |
| 394 | regs[8] = 0x10; |
| 395 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 396 | loc_regs[5] = DwarfLocation{DWARF_LOCATION_UNDEFINED, {0, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 397 | bool finished; |
| 398 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 399 | EXPECT_TRUE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 400 | EXPECT_EQ(0U, regs.pc()); |
| 401 | EXPECT_EQ(0x10U, regs.sp()); |
| 402 | } |
| 403 | |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 404 | TYPED_TEST_P(DwarfSectionImplTest, Eval_pc_zero) { |
| 405 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 406 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 2502a60 | 2017-10-23 13:51:54 -0700 | [diff] [blame] | 407 | dwarf_loc_regs_t loc_regs; |
| 408 | |
| 409 | regs.set_pc(0x100); |
| 410 | regs.set_sp(0x2000); |
| 411 | regs[5] = 0; |
| 412 | regs[8] = 0x10; |
| 413 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 414 | bool finished; |
| 415 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 416 | EXPECT_TRUE(finished); |
| 417 | EXPECT_EQ(0U, regs.pc()); |
| 418 | EXPECT_EQ(0x10U, regs.sp()); |
| 419 | } |
| 420 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 421 | TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address) { |
| 422 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 423 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 424 | dwarf_loc_regs_t loc_regs; |
| 425 | |
| 426 | regs.set_pc(0x100); |
| 427 | regs.set_sp(0x2000); |
| 428 | regs[5] = 0x20; |
| 429 | regs[8] = 0x10; |
| 430 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 431 | bool finished; |
| 432 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 433 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 434 | EXPECT_EQ(0x20U, regs.pc()); |
| 435 | EXPECT_EQ(0x10U, regs.sp()); |
| 436 | } |
| 437 | |
| 438 | TYPED_TEST_P(DwarfSectionImplTest, Eval_ignore_large_reg_loc) { |
| 439 | DwarfCie cie{.return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 440 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 441 | dwarf_loc_regs_t loc_regs; |
| 442 | |
| 443 | regs.set_pc(0x100); |
| 444 | regs.set_sp(0x2000); |
| 445 | regs[5] = 0x20; |
| 446 | regs[8] = 0x10; |
| 447 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
| 448 | // This should not result in any errors. |
| 449 | loc_regs[20] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 450 | bool finished; |
| 451 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 452 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 453 | EXPECT_EQ(0x20U, regs.pc()); |
| 454 | EXPECT_EQ(0x10U, regs.sp()); |
| 455 | } |
| 456 | |
| 457 | TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_expr) { |
| 458 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 459 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 460 | dwarf_loc_regs_t loc_regs; |
| 461 | |
| 462 | regs.set_pc(0x100); |
| 463 | regs.set_sp(0x2000); |
| 464 | regs[8] = 0x3000; |
| 465 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80}); |
| 466 | TypeParam cfa_value = 0x12345; |
| 467 | this->memory_.SetMemory(0x80000000, &cfa_value, sizeof(cfa_value)); |
| 468 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 469 | loc_regs[5] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x4, 0x5004}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 470 | bool finished; |
| 471 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 472 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 473 | EXPECT_EQ(0x3000U, regs.sp()); |
| 474 | EXPECT_EQ(0x12345U, regs.pc()); |
| 475 | } |
| 476 | |
| 477 | TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_val_expr) { |
| 478 | DwarfCie cie{.version = 3, .return_address_register = 5}; |
Yabin Cui | 11e96fe | 2018-03-14 18:16:22 -0700 | [diff] [blame] | 479 | RegsImplFake<TypeParam> regs(10); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 480 | dwarf_loc_regs_t loc_regs; |
| 481 | |
| 482 | regs.set_pc(0x100); |
| 483 | regs.set_sp(0x2000); |
| 484 | regs[8] = 0x3000; |
| 485 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80}); |
| 486 | loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}}; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 487 | loc_regs[5] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x4, 0x5004}}; |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 488 | bool finished; |
| 489 | ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, ®s, &finished)); |
| 490 | EXPECT_FALSE(finished); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 491 | EXPECT_EQ(0x3000U, regs.sp()); |
| 492 | EXPECT_EQ(0x80000000U, regs.pc()); |
| 493 | } |
| 494 | |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 495 | TYPED_TEST_P(DwarfSectionImplTest, GetCfaLocationInfo_cie_not_cached) { |
| 496 | DwarfCie cie{}; |
| 497 | cie.cfa_instructions_offset = 0x3000; |
| 498 | cie.cfa_instructions_end = 0x3002; |
| 499 | DwarfFde fde{}; |
| 500 | fde.cie = &cie; |
| 501 | fde.cie_offset = 0x8000; |
| 502 | fde.cfa_instructions_offset = 0x6000; |
| 503 | fde.cfa_instructions_end = 0x6002; |
| 504 | |
| 505 | this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x09, 0x02, 0x01}); |
| 506 | this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0x09, 0x04, 0x03}); |
| 507 | |
| 508 | dwarf_loc_regs_t loc_regs; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame^] | 509 | ASSERT_TRUE(this->section_->GetCfaLocationInfo(0x100, &fde, &loc_regs, ARCH_UNKNOWN)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 510 | ASSERT_EQ(2U, loc_regs.size()); |
| 511 | |
| 512 | auto entry = loc_regs.find(2); |
| 513 | ASSERT_NE(entry, loc_regs.end()); |
| 514 | ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type); |
| 515 | ASSERT_EQ(1U, entry->second.values[0]); |
| 516 | |
| 517 | entry = loc_regs.find(4); |
| 518 | ASSERT_NE(entry, loc_regs.end()); |
| 519 | ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type); |
| 520 | ASSERT_EQ(3U, entry->second.values[0]); |
| 521 | } |
| 522 | |
| 523 | TYPED_TEST_P(DwarfSectionImplTest, GetCfaLocationInfo_cie_cached) { |
| 524 | DwarfCie cie{}; |
| 525 | cie.cfa_instructions_offset = 0x3000; |
| 526 | cie.cfa_instructions_end = 0x3002; |
| 527 | DwarfFde fde{}; |
| 528 | fde.cie = &cie; |
| 529 | fde.cie_offset = 0x8000; |
| 530 | fde.cfa_instructions_offset = 0x6000; |
| 531 | fde.cfa_instructions_end = 0x6002; |
| 532 | |
David Srbecky | 3386eba | 2018-03-14 21:30:25 +0000 | [diff] [blame] | 533 | dwarf_loc_regs_t cie_loc_regs; |
| 534 | cie_loc_regs[6] = DwarfLocation{DWARF_LOCATION_REGISTER, {4, 0}}; |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 535 | this->section_->TestSetCachedCieLocRegs(0x8000, cie_loc_regs); |
| 536 | this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0x09, 0x04, 0x03}); |
| 537 | |
| 538 | dwarf_loc_regs_t loc_regs; |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame^] | 539 | ASSERT_TRUE(this->section_->GetCfaLocationInfo(0x100, &fde, &loc_regs, ARCH_UNKNOWN)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 540 | ASSERT_EQ(2U, loc_regs.size()); |
| 541 | |
| 542 | auto entry = loc_regs.find(6); |
| 543 | ASSERT_NE(entry, loc_regs.end()); |
| 544 | ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type); |
| 545 | ASSERT_EQ(4U, entry->second.values[0]); |
| 546 | |
| 547 | entry = loc_regs.find(4); |
| 548 | ASSERT_NE(entry, loc_regs.end()); |
| 549 | ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type); |
| 550 | ASSERT_EQ(3U, entry->second.values[0]); |
| 551 | } |
| 552 | |
| 553 | TYPED_TEST_P(DwarfSectionImplTest, Log) { |
| 554 | DwarfCie cie{}; |
| 555 | cie.cfa_instructions_offset = 0x5000; |
| 556 | cie.cfa_instructions_end = 0x5001; |
| 557 | DwarfFde fde{}; |
| 558 | fde.cie = &cie; |
| 559 | fde.cfa_instructions_offset = 0x6000; |
| 560 | fde.cfa_instructions_end = 0x6001; |
| 561 | |
| 562 | this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x00}); |
| 563 | this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0xc2}); |
Tamas Petz | 6835d01 | 2020-01-22 14:22:41 +0100 | [diff] [blame^] | 564 | ASSERT_TRUE(this->section_->Log(2, 0x1000, &fde, ARCH_UNKNOWN)); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 565 | |
| 566 | ASSERT_EQ( |
| 567 | "4 unwind DW_CFA_nop\n" |
| 568 | "4 unwind Raw Data: 0x00\n" |
| 569 | "4 unwind DW_CFA_restore register(2)\n" |
| 570 | "4 unwind Raw Data: 0xc2\n", |
| 571 | GetFakeLogPrint()); |
| 572 | ASSERT_EQ("", GetFakeLogBuf()); |
| 573 | } |
| 574 | |
Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 575 | REGISTER_TYPED_TEST_SUITE_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache, |
| 576 | GetFdeFromOffset_fail_should_not_cache, Eval_cfa_expr_eval_fail, |
| 577 | Eval_cfa_expr_no_stack, Eval_cfa_expr_is_register, Eval_cfa_expr, |
| 578 | Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa, Eval_cfa_bad, |
| 579 | Eval_cfa_register_prev, Eval_cfa_register_from_value, |
| 580 | Eval_double_indirection, Eval_register_reference_chain, Eval_dex_pc, |
| 581 | Eval_invalid_register, Eval_different_reg_locations, |
| 582 | Eval_return_address_undefined, Eval_pc_zero, Eval_return_address, |
| 583 | Eval_ignore_large_reg_loc, Eval_reg_expr, Eval_reg_val_expr, |
| 584 | GetCfaLocationInfo_cie_not_cached, GetCfaLocationInfo_cie_cached, Log); |
Christopher Ferris | 53a3c9b | 2017-05-10 18:34:15 -0700 | [diff] [blame] | 585 | |
| 586 | typedef ::testing::Types<uint32_t, uint64_t> DwarfSectionImplTestTypes; |
Haibo Huang | 0c01bb6 | 2019-12-11 12:46:20 -0800 | [diff] [blame] | 587 | INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfSectionImplTest, DwarfSectionImplTestTypes); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 588 | |
| 589 | } // namespace unwindstack |