Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -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 | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 23 | #include <unwindstack/DwarfError.h> |
| 24 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 25 | #include "DwarfDebugFrame.h" |
| 26 | #include "DwarfEncoding.h" |
| 27 | |
| 28 | #include "LogFake.h" |
| 29 | #include "MemoryFake.h" |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 30 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 | namespace unwindstack { |
| 32 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 33 | template <typename TypeParam> |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 34 | class DwarfDebugFrameTest : public ::testing::Test { |
| 35 | protected: |
| 36 | void SetUp() override { |
| 37 | memory_.Clear(); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 38 | debug_frame_ = new DwarfDebugFrame<TypeParam>(&memory_); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 39 | ResetLogs(); |
| 40 | } |
| 41 | |
| 42 | void TearDown() override { delete debug_frame_; } |
| 43 | |
| 44 | MemoryFake memory_; |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 45 | DwarfDebugFrame<TypeParam>* debug_frame_ = nullptr; |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 46 | }; |
Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 47 | TYPED_TEST_SUITE_P(DwarfDebugFrameTest); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 48 | |
| 49 | // NOTE: All test class variables need to be referenced as this->. |
| 50 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 51 | static void SetCie32(MemoryFake* memory, uint64_t offset, uint32_t length, |
| 52 | std::vector<uint8_t> data) { |
| 53 | memory->SetData32(offset, length); |
| 54 | offset += 4; |
| 55 | // Indicates this is a cie. |
| 56 | memory->SetData32(offset, 0xffffffff); |
| 57 | offset += 4; |
| 58 | memory->SetMemory(offset, data); |
| 59 | } |
| 60 | |
| 61 | static void SetCie64(MemoryFake* memory, uint64_t offset, uint64_t length, |
| 62 | std::vector<uint8_t> data) { |
| 63 | memory->SetData32(offset, 0xffffffff); |
| 64 | offset += 4; |
| 65 | memory->SetData64(offset, length); |
| 66 | offset += 8; |
| 67 | // Indicates this is a cie. |
| 68 | memory->SetData64(offset, 0xffffffffffffffffUL); |
| 69 | offset += 8; |
| 70 | memory->SetMemory(offset, data); |
| 71 | } |
| 72 | |
| 73 | static void SetFde32(MemoryFake* memory, uint64_t offset, uint32_t length, uint64_t cie_offset, |
| 74 | uint32_t pc_start, uint32_t pc_length, uint64_t segment_length = 0, |
| 75 | std::vector<uint8_t>* data = nullptr) { |
| 76 | memory->SetData32(offset, length); |
| 77 | offset += 4; |
| 78 | memory->SetData32(offset, cie_offset); |
| 79 | offset += 4 + segment_length; |
| 80 | memory->SetData32(offset, pc_start); |
| 81 | offset += 4; |
| 82 | memory->SetData32(offset, pc_length); |
| 83 | if (data != nullptr) { |
| 84 | offset += 4; |
| 85 | memory->SetMemory(offset, *data); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void SetFde64(MemoryFake* memory, uint64_t offset, uint64_t length, uint64_t cie_offset, |
| 90 | uint64_t pc_start, uint64_t pc_length, uint64_t segment_length = 0, |
| 91 | std::vector<uint8_t>* data = nullptr) { |
| 92 | memory->SetData32(offset, 0xffffffff); |
| 93 | offset += 4; |
| 94 | memory->SetData64(offset, length); |
| 95 | offset += 8; |
| 96 | memory->SetData64(offset, cie_offset); |
| 97 | offset += 8 + segment_length; |
| 98 | memory->SetData64(offset, pc_start); |
| 99 | offset += 8; |
| 100 | memory->SetData64(offset, pc_length); |
| 101 | if (data != nullptr) { |
| 102 | offset += 8; |
| 103 | memory->SetMemory(offset, *data); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static void SetFourFdes32(MemoryFake* memory) { |
| 108 | SetCie32(memory, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 109 | |
| 110 | // FDE 32 information. |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 111 | SetFde32(memory, 0x5100, 0xfc, 0, 0x1500, 0x200); |
| 112 | SetFde32(memory, 0x5200, 0xfc, 0, 0x2500, 0x300); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 113 | |
| 114 | // CIE 32 information. |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 115 | SetCie32(memory, 0x5300, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 116 | |
| 117 | // FDE 32 information. |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 118 | SetFde32(memory, 0x5400, 0xfc, 0x300, 0x3500, 0x400); |
| 119 | SetFde32(memory, 0x5500, 0xfc, 0x300, 0x4500, 0x500); |
| 120 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 121 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 122 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32) { |
| 123 | SetFourFdes32(&this->memory_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 124 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 125 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 126 | std::vector<const DwarfFde*> fdes; |
| 127 | this->debug_frame_->GetFdes(&fdes); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 128 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 129 | ASSERT_EQ(4U, fdes.size()); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 130 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 131 | EXPECT_EQ(0x5000U, fdes[0]->cie_offset); |
| 132 | EXPECT_EQ(0x5110U, fdes[0]->cfa_instructions_offset); |
| 133 | EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end); |
| 134 | EXPECT_EQ(0x1500U, fdes[0]->pc_start); |
| 135 | EXPECT_EQ(0x1700U, fdes[0]->pc_end); |
| 136 | EXPECT_EQ(0U, fdes[0]->lsda_address); |
| 137 | EXPECT_TRUE(fdes[0]->cie != nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 138 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 139 | EXPECT_EQ(0x5000U, fdes[1]->cie_offset); |
| 140 | EXPECT_EQ(0x5210U, fdes[1]->cfa_instructions_offset); |
| 141 | EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end); |
| 142 | EXPECT_EQ(0x2500U, fdes[1]->pc_start); |
| 143 | EXPECT_EQ(0x2800U, fdes[1]->pc_end); |
| 144 | EXPECT_EQ(0U, fdes[1]->lsda_address); |
| 145 | EXPECT_TRUE(fdes[1]->cie != nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 146 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 147 | EXPECT_EQ(0x5300U, fdes[2]->cie_offset); |
| 148 | EXPECT_EQ(0x5410U, fdes[2]->cfa_instructions_offset); |
| 149 | EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end); |
| 150 | EXPECT_EQ(0x3500U, fdes[2]->pc_start); |
| 151 | EXPECT_EQ(0x3900U, fdes[2]->pc_end); |
| 152 | EXPECT_EQ(0U, fdes[2]->lsda_address); |
| 153 | EXPECT_TRUE(fdes[2]->cie != nullptr); |
| 154 | |
| 155 | EXPECT_EQ(0x5300U, fdes[3]->cie_offset); |
| 156 | EXPECT_EQ(0x5510U, fdes[3]->cfa_instructions_offset); |
| 157 | EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end); |
| 158 | EXPECT_EQ(0x4500U, fdes[3]->pc_start); |
| 159 | EXPECT_EQ(0x4a00U, fdes[3]->pc_end); |
| 160 | EXPECT_EQ(0U, fdes[3]->lsda_address); |
| 161 | EXPECT_TRUE(fdes[3]->cie != nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 164 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_after_GetFdeFromPc) { |
| 165 | SetFourFdes32(&this->memory_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 166 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 167 | |
| 168 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x3600); |
| 169 | ASSERT_TRUE(fde != nullptr); |
| 170 | EXPECT_EQ(0x3500U, fde->pc_start); |
| 171 | EXPECT_EQ(0x3900U, fde->pc_end); |
| 172 | |
| 173 | std::vector<const DwarfFde*> fdes; |
| 174 | this->debug_frame_->GetFdes(&fdes); |
| 175 | ASSERT_EQ(4U, fdes.size()); |
| 176 | |
| 177 | // Verify that they got added in the correct order. |
| 178 | EXPECT_EQ(0x1500U, fdes[0]->pc_start); |
| 179 | EXPECT_EQ(0x1700U, fdes[0]->pc_end); |
| 180 | EXPECT_EQ(0x2500U, fdes[1]->pc_start); |
| 181 | EXPECT_EQ(0x2800U, fdes[1]->pc_end); |
| 182 | EXPECT_EQ(0x3500U, fdes[2]->pc_start); |
| 183 | EXPECT_EQ(0x3900U, fdes[2]->pc_end); |
| 184 | EXPECT_EQ(0x4500U, fdes[3]->pc_start); |
| 185 | EXPECT_EQ(0x4a00U, fdes[3]->pc_end); |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 188 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes32_not_in_section) { |
| 189 | SetFourFdes32(&this->memory_); |
| 190 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0)); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 191 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 192 | std::vector<const DwarfFde*> fdes; |
| 193 | this->debug_frame_->GetFdes(&fdes); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 194 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 195 | ASSERT_EQ(3U, fdes.size()); |
| 196 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 197 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 198 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32) { |
| 199 | SetFourFdes32(&this->memory_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 200 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 201 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 202 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600); |
| 203 | ASSERT_TRUE(fde != nullptr); |
| 204 | EXPECT_EQ(0x1500U, fde->pc_start); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 205 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 206 | fde = this->debug_frame_->GetFdeFromPc(0x2600); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 207 | ASSERT_TRUE(fde != nullptr); |
| 208 | EXPECT_EQ(0x2500U, fde->pc_start); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 209 | |
| 210 | fde = this->debug_frame_->GetFdeFromPc(0x3600); |
| 211 | ASSERT_TRUE(fde != nullptr); |
| 212 | EXPECT_EQ(0x3500U, fde->pc_start); |
| 213 | |
| 214 | fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 215 | ASSERT_TRUE(fde != nullptr); |
| 216 | EXPECT_EQ(0x4500U, fde->pc_start); |
| 217 | |
| 218 | fde = this->debug_frame_->GetFdeFromPc(0); |
| 219 | ASSERT_TRUE(fde == nullptr); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 222 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_reverse) { |
| 223 | SetFourFdes32(&this->memory_); |
| 224 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 225 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 226 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 227 | ASSERT_TRUE(fde != nullptr); |
| 228 | EXPECT_EQ(0x4500U, fde->pc_start); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 229 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 230 | fde = this->debug_frame_->GetFdeFromPc(0x3600); |
| 231 | ASSERT_TRUE(fde != nullptr); |
| 232 | EXPECT_EQ(0x3500U, fde->pc_start); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 233 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 234 | fde = this->debug_frame_->GetFdeFromPc(0x2600); |
| 235 | ASSERT_TRUE(fde != nullptr); |
| 236 | EXPECT_EQ(0x2500U, fde->pc_start); |
| 237 | |
| 238 | fde = this->debug_frame_->GetFdeFromPc(0x1600); |
| 239 | ASSERT_TRUE(fde != nullptr); |
| 240 | EXPECT_EQ(0x1500U, fde->pc_start); |
| 241 | |
| 242 | fde = this->debug_frame_->GetFdeFromPc(0); |
| 243 | ASSERT_TRUE(fde == nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 246 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc32_not_in_section) { |
| 247 | SetFourFdes32(&this->memory_); |
| 248 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0)); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 249 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 250 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 251 | ASSERT_TRUE(fde == nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 254 | static void SetFourFdes64(MemoryFake* memory) { |
| 255 | // CIE 64 information. |
| 256 | SetCie64(memory, 0x5000, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 257 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 258 | // FDE 64 information. |
| 259 | SetFde64(memory, 0x5100, 0xf4, 0, 0x1500, 0x200); |
| 260 | SetFde64(memory, 0x5200, 0xf4, 0, 0x2500, 0x300); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 261 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 262 | // CIE 64 information. |
| 263 | SetCie64(memory, 0x5300, 0xf4, std::vector<uint8_t>{1, '\0', 0, 0, 1}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 264 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 265 | // FDE 64 information. |
| 266 | SetFde64(memory, 0x5400, 0xf4, 0x300, 0x3500, 0x400); |
| 267 | SetFde64(memory, 0x5500, 0xf4, 0x300, 0x4500, 0x500); |
| 268 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 269 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 270 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64) { |
| 271 | SetFourFdes64(&this->memory_); |
| 272 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
| 273 | |
| 274 | std::vector<const DwarfFde*> fdes; |
| 275 | this->debug_frame_->GetFdes(&fdes); |
| 276 | |
| 277 | ASSERT_EQ(4U, fdes.size()); |
| 278 | |
| 279 | EXPECT_EQ(0x5000U, fdes[0]->cie_offset); |
| 280 | EXPECT_EQ(0x5124U, fdes[0]->cfa_instructions_offset); |
| 281 | EXPECT_EQ(0x5200U, fdes[0]->cfa_instructions_end); |
| 282 | EXPECT_EQ(0x1500U, fdes[0]->pc_start); |
| 283 | EXPECT_EQ(0x1700U, fdes[0]->pc_end); |
| 284 | EXPECT_EQ(0U, fdes[0]->lsda_address); |
| 285 | EXPECT_TRUE(fdes[0]->cie != nullptr); |
| 286 | |
| 287 | EXPECT_EQ(0x5000U, fdes[1]->cie_offset); |
| 288 | EXPECT_EQ(0x5224U, fdes[1]->cfa_instructions_offset); |
| 289 | EXPECT_EQ(0x5300U, fdes[1]->cfa_instructions_end); |
| 290 | EXPECT_EQ(0x2500U, fdes[1]->pc_start); |
| 291 | EXPECT_EQ(0x2800U, fdes[1]->pc_end); |
| 292 | EXPECT_EQ(0U, fdes[1]->lsda_address); |
| 293 | EXPECT_TRUE(fdes[1]->cie != nullptr); |
| 294 | |
| 295 | EXPECT_EQ(0x5300U, fdes[2]->cie_offset); |
| 296 | EXPECT_EQ(0x5424U, fdes[2]->cfa_instructions_offset); |
| 297 | EXPECT_EQ(0x5500U, fdes[2]->cfa_instructions_end); |
| 298 | EXPECT_EQ(0x3500U, fdes[2]->pc_start); |
| 299 | EXPECT_EQ(0x3900U, fdes[2]->pc_end); |
| 300 | EXPECT_EQ(0U, fdes[2]->lsda_address); |
| 301 | EXPECT_TRUE(fdes[2]->cie != nullptr); |
| 302 | |
| 303 | EXPECT_EQ(0x5300U, fdes[3]->cie_offset); |
| 304 | EXPECT_EQ(0x5524U, fdes[3]->cfa_instructions_offset); |
| 305 | EXPECT_EQ(0x5600U, fdes[3]->cfa_instructions_end); |
| 306 | EXPECT_EQ(0x4500U, fdes[3]->pc_start); |
| 307 | EXPECT_EQ(0x4a00U, fdes[3]->pc_end); |
| 308 | EXPECT_EQ(0U, fdes[3]->lsda_address); |
| 309 | EXPECT_TRUE(fdes[3]->cie != nullptr); |
| 310 | } |
| 311 | |
| 312 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_after_GetFdeFromPc) { |
| 313 | SetFourFdes64(&this->memory_); |
| 314 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
| 315 | |
| 316 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x2600); |
| 317 | ASSERT_TRUE(fde != nullptr); |
| 318 | EXPECT_EQ(0x2500U, fde->pc_start); |
| 319 | EXPECT_EQ(0x2800U, fde->pc_end); |
| 320 | |
| 321 | std::vector<const DwarfFde*> fdes; |
| 322 | this->debug_frame_->GetFdes(&fdes); |
| 323 | ASSERT_EQ(4U, fdes.size()); |
| 324 | |
| 325 | // Verify that they got added in the correct order. |
| 326 | EXPECT_EQ(0x1500U, fdes[0]->pc_start); |
| 327 | EXPECT_EQ(0x1700U, fdes[0]->pc_end); |
| 328 | EXPECT_EQ(0x2500U, fdes[1]->pc_start); |
| 329 | EXPECT_EQ(0x2800U, fdes[1]->pc_end); |
| 330 | EXPECT_EQ(0x3500U, fdes[2]->pc_start); |
| 331 | EXPECT_EQ(0x3900U, fdes[2]->pc_end); |
| 332 | EXPECT_EQ(0x4500U, fdes[3]->pc_start); |
| 333 | EXPECT_EQ(0x4a00U, fdes[3]->pc_end); |
| 334 | } |
| 335 | |
| 336 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdes64_not_in_section) { |
| 337 | SetFourFdes64(&this->memory_); |
| 338 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0)); |
| 339 | |
| 340 | std::vector<const DwarfFde*> fdes; |
| 341 | this->debug_frame_->GetFdes(&fdes); |
| 342 | |
| 343 | ASSERT_EQ(3U, fdes.size()); |
| 344 | } |
| 345 | |
| 346 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64) { |
| 347 | SetFourFdes64(&this->memory_); |
| 348 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
| 349 | |
| 350 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x1600); |
| 351 | ASSERT_TRUE(fde != nullptr); |
| 352 | EXPECT_EQ(0x1500U, fde->pc_start); |
| 353 | |
| 354 | fde = this->debug_frame_->GetFdeFromPc(0x2600); |
| 355 | ASSERT_TRUE(fde != nullptr); |
| 356 | EXPECT_EQ(0x2500U, fde->pc_start); |
| 357 | |
| 358 | fde = this->debug_frame_->GetFdeFromPc(0x3600); |
| 359 | ASSERT_TRUE(fde != nullptr); |
| 360 | EXPECT_EQ(0x3500U, fde->pc_start); |
| 361 | |
| 362 | fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 363 | ASSERT_TRUE(fde != nullptr); |
| 364 | EXPECT_EQ(0x4500U, fde->pc_start); |
| 365 | |
| 366 | fde = this->debug_frame_->GetFdeFromPc(0); |
| 367 | ASSERT_TRUE(fde == nullptr); |
| 368 | } |
| 369 | |
| 370 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_reverse) { |
| 371 | SetFourFdes64(&this->memory_); |
| 372 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0)); |
| 373 | |
| 374 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 375 | ASSERT_TRUE(fde != nullptr); |
| 376 | EXPECT_EQ(0x4500U, fde->pc_start); |
| 377 | |
| 378 | fde = this->debug_frame_->GetFdeFromPc(0x3600); |
| 379 | ASSERT_TRUE(fde != nullptr); |
| 380 | EXPECT_EQ(0x3500U, fde->pc_start); |
| 381 | |
| 382 | fde = this->debug_frame_->GetFdeFromPc(0x2600); |
| 383 | ASSERT_TRUE(fde != nullptr); |
| 384 | EXPECT_EQ(0x2500U, fde->pc_start); |
| 385 | |
| 386 | fde = this->debug_frame_->GetFdeFromPc(0x1600); |
| 387 | ASSERT_TRUE(fde != nullptr); |
| 388 | EXPECT_EQ(0x1500U, fde->pc_start); |
| 389 | |
| 390 | fde = this->debug_frame_->GetFdeFromPc(0); |
| 391 | ASSERT_TRUE(fde == nullptr); |
| 392 | } |
| 393 | |
| 394 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc64_not_in_section) { |
| 395 | SetFourFdes64(&this->memory_); |
| 396 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x500, 0)); |
| 397 | |
| 398 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x4600); |
| 399 | ASSERT_TRUE(fde == nullptr); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) { |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 403 | SetCie32(&this->memory_, 0xf000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 404 | SetFde32(&this->memory_, 0x14000, 0x20, 0xf000, 0x9000, 0x100); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 405 | |
| 406 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000); |
| 407 | ASSERT_TRUE(fde != nullptr); |
| 408 | EXPECT_EQ(0x14010U, fde->cfa_instructions_offset); |
| 409 | EXPECT_EQ(0x14024U, fde->cfa_instructions_end); |
| 410 | EXPECT_EQ(0x9000U, fde->pc_start); |
| 411 | EXPECT_EQ(0x9100U, fde->pc_end); |
| 412 | EXPECT_EQ(0xf000U, fde->cie_offset); |
| 413 | EXPECT_EQ(0U, fde->lsda_address); |
| 414 | |
| 415 | ASSERT_TRUE(fde->cie != nullptr); |
| 416 | EXPECT_EQ(1U, fde->cie->version); |
| 417 | EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding); |
| 418 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); |
| 419 | EXPECT_EQ(0U, fde->cie->segment_size); |
| 420 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); |
| 421 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); |
| 422 | EXPECT_EQ(0U, fde->cie->personality_handler); |
| 423 | EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset); |
| 424 | EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end); |
| 425 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); |
| 426 | EXPECT_EQ(8, fde->cie->data_alignment_factor); |
| 427 | EXPECT_EQ(0x20U, fde->cie->return_address_register); |
| 428 | } |
| 429 | |
| 430 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) { |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 431 | SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 432 | SetFde64(&this->memory_, 0x8000, 0x200, 0x6000, 0x5000, 0x300); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 433 | |
| 434 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000); |
| 435 | ASSERT_TRUE(fde != nullptr); |
| 436 | EXPECT_EQ(0x8024U, fde->cfa_instructions_offset); |
| 437 | EXPECT_EQ(0x820cU, fde->cfa_instructions_end); |
| 438 | EXPECT_EQ(0x5000U, fde->pc_start); |
| 439 | EXPECT_EQ(0x5300U, fde->pc_end); |
| 440 | EXPECT_EQ(0x6000U, fde->cie_offset); |
| 441 | EXPECT_EQ(0U, fde->lsda_address); |
| 442 | |
| 443 | ASSERT_TRUE(fde->cie != nullptr); |
| 444 | EXPECT_EQ(1U, fde->cie->version); |
| 445 | EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding); |
| 446 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); |
| 447 | EXPECT_EQ(0U, fde->cie->segment_size); |
| 448 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); |
| 449 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); |
| 450 | EXPECT_EQ(0U, fde->cie->personality_handler); |
| 451 | EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset); |
| 452 | EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end); |
| 453 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); |
| 454 | EXPECT_EQ(8, fde->cie->data_alignment_factor); |
| 455 | EXPECT_EQ(0x20U, fde->cie->return_address_register); |
| 456 | } |
| 457 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 458 | static void VerifyCieVersion(const DwarfCie* cie, uint8_t version, uint8_t segment_size, |
| 459 | uint8_t fde_encoding, uint64_t return_address, uint64_t start_offset, |
| 460 | uint64_t end_offset) { |
| 461 | EXPECT_EQ(version, cie->version); |
| 462 | EXPECT_EQ(fde_encoding, cie->fde_address_encoding); |
| 463 | EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding); |
| 464 | EXPECT_EQ(segment_size, cie->segment_size); |
| 465 | EXPECT_EQ(1U, cie->augmentation_string.size()); |
| 466 | EXPECT_EQ('\0', cie->augmentation_string[0]); |
| 467 | EXPECT_EQ(0U, cie->personality_handler); |
| 468 | EXPECT_EQ(4U, cie->code_alignment_factor); |
| 469 | EXPECT_EQ(8, cie->data_alignment_factor); |
| 470 | EXPECT_EQ(return_address, cie->return_address_register); |
| 471 | EXPECT_EQ(0x5000U + start_offset, cie->cfa_instructions_offset); |
| 472 | EXPECT_EQ(0x5000U + end_offset, cie->cfa_instructions_end); |
| 473 | } |
| 474 | |
| 475 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_cie_cached) { |
| 476 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 477 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 478 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 479 | ASSERT_TRUE(cie != nullptr); |
| 480 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104); |
| 481 | |
| 482 | std::vector<uint8_t> zero(0x100, 0); |
| 483 | this->memory_.SetMemory(0x5000, zero); |
| 484 | cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 485 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 486 | ASSERT_TRUE(cie != nullptr); |
| 487 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104); |
| 488 | } |
| 489 | |
| 490 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_cie_cached) { |
| 491 | SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 492 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 493 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 494 | ASSERT_TRUE(cie != nullptr); |
| 495 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c); |
| 496 | |
| 497 | std::vector<uint8_t> zero(0x100, 0); |
| 498 | this->memory_.SetMemory(0x5000, zero); |
| 499 | cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 500 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 501 | ASSERT_TRUE(cie != nullptr); |
| 502 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c); |
| 503 | } |
| 504 | |
| 505 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version1) { |
| 506 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 507 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 508 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 509 | ASSERT_TRUE(cie != nullptr); |
| 510 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata4, 0x20, 0xd, 0x104); |
| 511 | } |
| 512 | |
| 513 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version1) { |
| 514 | SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{1, '\0', 4, 8, 0x20}); |
| 515 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 516 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 517 | ASSERT_TRUE(cie != nullptr); |
| 518 | VerifyCieVersion(cie, 1, 0, DW_EH_PE_sdata8, 0x20, 0x19, 0x10c); |
| 519 | } |
| 520 | |
| 521 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version3) { |
| 522 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3}); |
| 523 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 524 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 525 | ASSERT_TRUE(cie != nullptr); |
| 526 | VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata4, 0x181, 0xe, 0x104); |
| 527 | } |
| 528 | |
| 529 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version3) { |
| 530 | SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{3, '\0', 4, 8, 0x81, 3}); |
| 531 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 532 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 533 | ASSERT_TRUE(cie != nullptr); |
| 534 | VerifyCieVersion(cie, 3, 0, DW_EH_PE_sdata8, 0x181, 0x1a, 0x10c); |
| 535 | } |
| 536 | |
| 537 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version4) { |
| 538 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3}); |
| 539 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 540 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 541 | ASSERT_TRUE(cie != nullptr); |
| 542 | VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata4, 0x181, 0x10, 0x104); |
| 543 | } |
| 544 | |
| 545 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version4) { |
| 546 | SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{4, '\0', 0, 10, 4, 8, 0x81, 3}); |
| 547 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 548 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 549 | ASSERT_TRUE(cie != nullptr); |
| 550 | VerifyCieVersion(cie, 4, 10, DW_EH_PE_sdata8, 0x181, 0x1c, 0x10c); |
| 551 | } |
| 552 | |
Christopher Ferris | c312c9a | 2019-04-01 16:53:56 -0700 | [diff] [blame] | 553 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_version5) { |
| 554 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{5, '\0', 0, 10, 4, 8, 0x81, 3}); |
| 555 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 556 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 557 | ASSERT_TRUE(cie != nullptr); |
| 558 | VerifyCieVersion(cie, 5, 10, DW_EH_PE_sdata4, 0x181, 0x10, 0x104); |
| 559 | } |
| 560 | |
| 561 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_version5) { |
| 562 | SetCie64(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{5, '\0', 0, 10, 4, 8, 0x81, 3}); |
| 563 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 564 | EXPECT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode()); |
| 565 | ASSERT_TRUE(cie != nullptr); |
| 566 | VerifyCieVersion(cie, 5, 10, DW_EH_PE_sdata8, 0x181, 0x1c, 0x10c); |
| 567 | } |
| 568 | |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 569 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset_version_invalid) { |
| 570 | SetCie32(&this->memory_, 0x5000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7}); |
| 571 | ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x5000) == nullptr); |
| 572 | EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode()); |
| 573 | SetCie64(&this->memory_, 0x6000, 0x100, std::vector<uint8_t>{0, '\0', 1, 2, 3, 4, 5, 6, 7}); |
| 574 | ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x6000) == nullptr); |
| 575 | EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode()); |
| 576 | |
Christopher Ferris | c312c9a | 2019-04-01 16:53:56 -0700 | [diff] [blame] | 577 | SetCie32(&this->memory_, 0x7000, 0x100, std::vector<uint8_t>{6, '\0', 1, 2, 3, 4, 5, 6, 7}); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 578 | ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x7000) == nullptr); |
| 579 | EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode()); |
Christopher Ferris | c312c9a | 2019-04-01 16:53:56 -0700 | [diff] [blame] | 580 | SetCie64(&this->memory_, 0x8000, 0x100, std::vector<uint8_t>{6, '\0', 1, 2, 3, 4, 5, 6, 7}); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 581 | ASSERT_TRUE(this->debug_frame_->GetCieFromOffset(0x8000) == nullptr); |
| 582 | EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->debug_frame_->LastErrorCode()); |
| 583 | } |
| 584 | |
| 585 | static void VerifyCieAugment(const DwarfCie* cie, uint64_t inst_offset, uint64_t inst_end) { |
| 586 | EXPECT_EQ(1U, cie->version); |
| 587 | EXPECT_EQ(DW_EH_PE_udata2, cie->fde_address_encoding); |
| 588 | EXPECT_EQ(DW_EH_PE_textrel | DW_EH_PE_udata2, cie->lsda_encoding); |
| 589 | EXPECT_EQ(0U, cie->segment_size); |
| 590 | EXPECT_EQ(5U, cie->augmentation_string.size()); |
| 591 | EXPECT_EQ('z', cie->augmentation_string[0]); |
| 592 | EXPECT_EQ('L', cie->augmentation_string[1]); |
| 593 | EXPECT_EQ('P', cie->augmentation_string[2]); |
| 594 | EXPECT_EQ('R', cie->augmentation_string[3]); |
| 595 | EXPECT_EQ('\0', cie->augmentation_string[4]); |
| 596 | EXPECT_EQ(0x12345678U, cie->personality_handler); |
| 597 | EXPECT_EQ(4U, cie->code_alignment_factor); |
| 598 | EXPECT_EQ(8, cie->data_alignment_factor); |
| 599 | EXPECT_EQ(0x10U, cie->return_address_register); |
| 600 | EXPECT_EQ(inst_offset, cie->cfa_instructions_offset); |
| 601 | EXPECT_EQ(inst_end, cie->cfa_instructions_end); |
| 602 | } |
| 603 | |
| 604 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset32_augment) { |
| 605 | SetCie32(&this->memory_, 0x5000, 0x100, |
| 606 | std::vector<uint8_t>{/* version */ 1, |
| 607 | /* augment string */ 'z', 'L', 'P', 'R', '\0', |
| 608 | /* code alignment factor */ 4, |
| 609 | /* data alignment factor */ 8, |
| 610 | /* return address register */ 0x10, |
| 611 | /* augment length */ 0xf, |
| 612 | /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2, |
| 613 | /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12, |
| 614 | /* R data */ DW_EH_PE_udata2}); |
| 615 | |
| 616 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 617 | ASSERT_TRUE(cie != nullptr); |
| 618 | VerifyCieAugment(cie, 0x5021, 0x5104); |
| 619 | } |
| 620 | |
| 621 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFromOffset64_augment) { |
| 622 | SetCie64(&this->memory_, 0x5000, 0x100, |
| 623 | std::vector<uint8_t>{/* version */ 1, |
| 624 | /* augment string */ 'z', 'L', 'P', 'R', '\0', |
| 625 | /* code alignment factor */ 4, |
| 626 | /* data alignment factor */ 8, |
| 627 | /* return address register */ 0x10, |
| 628 | /* augment length */ 0xf, |
| 629 | /* L data */ DW_EH_PE_textrel | DW_EH_PE_udata2, |
| 630 | /* P data */ DW_EH_PE_udata4, 0x78, 0x56, 0x34, 0x12, |
| 631 | /* R data */ DW_EH_PE_udata2}); |
| 632 | |
| 633 | const DwarfCie* cie = this->debug_frame_->GetCieFromOffset(0x5000); |
| 634 | ASSERT_TRUE(cie != nullptr); |
| 635 | VerifyCieAugment(cie, 0x502d, 0x510c); |
| 636 | } |
| 637 | |
| 638 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_augment) { |
| 639 | SetCie32(&this->memory_, 0x5000, 0xfc, |
| 640 | std::vector<uint8_t>{/* version */ 4, |
| 641 | /* augment string */ 'z', '\0', |
| 642 | /* address size */ 8, |
| 643 | /* segment size */ 0x10, |
| 644 | /* code alignment factor */ 16, |
| 645 | /* data alignment factor */ 32, |
| 646 | /* return address register */ 10, |
| 647 | /* augment length */ 0x0}); |
| 648 | |
| 649 | std::vector<uint8_t> data{/* augment length */ 0x80, 0x3}; |
| 650 | SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data); |
| 651 | |
| 652 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200); |
| 653 | ASSERT_TRUE(fde != nullptr); |
| 654 | ASSERT_TRUE(fde->cie != nullptr); |
| 655 | EXPECT_EQ(4U, fde->cie->version); |
| 656 | EXPECT_EQ(0x5000U, fde->cie_offset); |
| 657 | EXPECT_EQ(0x53a2U, fde->cfa_instructions_offset); |
| 658 | EXPECT_EQ(0x5504U, fde->cfa_instructions_end); |
| 659 | EXPECT_EQ(0x4300U, fde->pc_start); |
| 660 | EXPECT_EQ(0x4600U, fde->pc_end); |
| 661 | EXPECT_EQ(0U, fde->lsda_address); |
| 662 | } |
| 663 | |
| 664 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_augment) { |
| 665 | SetCie64(&this->memory_, 0x5000, 0xfc, |
| 666 | std::vector<uint8_t>{/* version */ 4, |
| 667 | /* augment string */ 'z', '\0', |
| 668 | /* address size */ 8, |
| 669 | /* segment size */ 0x10, |
| 670 | /* code alignment factor */ 16, |
| 671 | /* data alignment factor */ 32, |
| 672 | /* return address register */ 10, |
| 673 | /* augment length */ 0x0}); |
| 674 | |
| 675 | std::vector<uint8_t> data{/* augment length */ 0x80, 0x3}; |
| 676 | SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0x10, &data); |
| 677 | |
| 678 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200); |
| 679 | ASSERT_TRUE(fde != nullptr); |
| 680 | ASSERT_TRUE(fde->cie != nullptr); |
| 681 | EXPECT_EQ(4U, fde->cie->version); |
| 682 | EXPECT_EQ(0x5000U, fde->cie_offset); |
| 683 | EXPECT_EQ(0x53b6U, fde->cfa_instructions_offset); |
| 684 | EXPECT_EQ(0x550cU, fde->cfa_instructions_end); |
| 685 | EXPECT_EQ(0x4300U, fde->pc_start); |
| 686 | EXPECT_EQ(0x4600U, fde->pc_end); |
| 687 | EXPECT_EQ(0U, fde->lsda_address); |
| 688 | } |
| 689 | |
| 690 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset32_lsda_address) { |
| 691 | SetCie32(&this->memory_, 0x5000, 0xfc, |
| 692 | std::vector<uint8_t>{/* version */ 1, |
| 693 | /* augment string */ 'z', 'L', '\0', |
| 694 | /* address size */ 8, |
| 695 | /* code alignment factor */ 16, |
| 696 | /* data alignment factor */ 32, |
| 697 | /* return address register */ 10, |
| 698 | /* augment length */ 0x2, |
| 699 | /* L data */ DW_EH_PE_udata2}); |
| 700 | |
| 701 | std::vector<uint8_t> data{/* augment length */ 0x80, 0x3, |
| 702 | /* lsda address */ 0x20, 0x45}; |
| 703 | SetFde32(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data); |
| 704 | |
| 705 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200); |
| 706 | ASSERT_TRUE(fde != nullptr); |
| 707 | ASSERT_TRUE(fde->cie != nullptr); |
| 708 | EXPECT_EQ(1U, fde->cie->version); |
| 709 | EXPECT_EQ(0x5000U, fde->cie_offset); |
| 710 | EXPECT_EQ(0x5392U, fde->cfa_instructions_offset); |
| 711 | EXPECT_EQ(0x5504U, fde->cfa_instructions_end); |
| 712 | EXPECT_EQ(0x4300U, fde->pc_start); |
| 713 | EXPECT_EQ(0x4600U, fde->pc_end); |
| 714 | EXPECT_EQ(0x4520U, fde->lsda_address); |
| 715 | } |
| 716 | |
| 717 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromOffset64_lsda_address) { |
| 718 | SetCie64(&this->memory_, 0x5000, 0xfc, |
| 719 | std::vector<uint8_t>{/* version */ 1, |
| 720 | /* augment string */ 'z', 'L', '\0', |
| 721 | /* address size */ 8, |
| 722 | /* code alignment factor */ 16, |
| 723 | /* data alignment factor */ 32, |
| 724 | /* return address register */ 10, |
| 725 | /* augment length */ 0x2, |
| 726 | /* L data */ DW_EH_PE_udata2}); |
| 727 | |
| 728 | std::vector<uint8_t> data{/* augment length */ 0x80, 0x3, |
| 729 | /* lsda address */ 0x20, 0x45}; |
| 730 | SetFde64(&this->memory_, 0x5200, 0x300, 0x5000, 0x4300, 0x300, 0, &data); |
| 731 | |
| 732 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x5200); |
| 733 | ASSERT_TRUE(fde != nullptr); |
| 734 | ASSERT_TRUE(fde->cie != nullptr); |
| 735 | EXPECT_EQ(1U, fde->cie->version); |
| 736 | EXPECT_EQ(0x5000U, fde->cie_offset); |
| 737 | EXPECT_EQ(0x53a6U, fde->cfa_instructions_offset); |
| 738 | EXPECT_EQ(0x550cU, fde->cfa_instructions_end); |
| 739 | EXPECT_EQ(0x4300U, fde->pc_start); |
| 740 | EXPECT_EQ(0x4600U, fde->pc_end); |
| 741 | EXPECT_EQ(0x4520U, fde->lsda_address); |
| 742 | } |
| 743 | |
| 744 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeFromPc_interleaved) { |
| 745 | SetCie32(&this->memory_, 0x5000, 0xfc, std::vector<uint8_t>{1, '\0', 0, 0, 1}); |
| 746 | |
| 747 | // FDE 0 (0x100 - 0x200) |
| 748 | SetFde32(&this->memory_, 0x5100, 0xfc, 0, 0x100, 0x100); |
| 749 | // FDE 1 (0x300 - 0x500) |
| 750 | SetFde32(&this->memory_, 0x5200, 0xfc, 0, 0x300, 0x200); |
| 751 | // FDE 2 (0x700 - 0x800) |
| 752 | SetFde32(&this->memory_, 0x5300, 0xfc, 0, 0x700, 0x100); |
| 753 | // FDE 3 (0xa00 - 0xb00) |
| 754 | SetFde32(&this->memory_, 0x5400, 0xfc, 0, 0xa00, 0x100); |
| 755 | // FDE 4 (0x100 - 0xb00) |
| 756 | SetFde32(&this->memory_, 0x5500, 0xfc, 0, 0x150, 0xa00); |
| 757 | // FDE 5 (0x0 - 0x50) |
| 758 | SetFde32(&this->memory_, 0x5600, 0xfc, 0, 0, 0x50); |
| 759 | |
| 760 | this->debug_frame_->Init(0x5000, 0x700, 0); |
| 761 | |
| 762 | // Force reading all entries so no entries are found. |
| 763 | const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0xfffff); |
| 764 | ASSERT_TRUE(fde == nullptr); |
| 765 | |
| 766 | // 0x0 - 0x50 FDE 5 |
| 767 | fde = this->debug_frame_->GetFdeFromPc(0x10); |
| 768 | ASSERT_TRUE(fde != nullptr); |
| 769 | EXPECT_EQ(0U, fde->pc_start); |
| 770 | EXPECT_EQ(0x50U, fde->pc_end); |
| 771 | |
| 772 | // 0x100 - 0x200 FDE 0 |
| 773 | fde = this->debug_frame_->GetFdeFromPc(0x170); |
| 774 | ASSERT_TRUE(fde != nullptr); |
| 775 | EXPECT_EQ(0x100U, fde->pc_start); |
| 776 | EXPECT_EQ(0x200U, fde->pc_end); |
| 777 | |
| 778 | // 0x200 - 0x300 FDE 4 |
| 779 | fde = this->debug_frame_->GetFdeFromPc(0x210); |
| 780 | ASSERT_TRUE(fde != nullptr); |
| 781 | EXPECT_EQ(0x150U, fde->pc_start); |
| 782 | EXPECT_EQ(0xb50U, fde->pc_end); |
| 783 | |
| 784 | // 0x300 - 0x500 FDE 1 |
| 785 | fde = this->debug_frame_->GetFdeFromPc(0x310); |
| 786 | ASSERT_TRUE(fde != nullptr); |
| 787 | EXPECT_EQ(0x300U, fde->pc_start); |
| 788 | EXPECT_EQ(0x500U, fde->pc_end); |
| 789 | |
| 790 | // 0x700 - 0x800 FDE 2 |
| 791 | fde = this->debug_frame_->GetFdeFromPc(0x790); |
| 792 | ASSERT_TRUE(fde != nullptr); |
| 793 | EXPECT_EQ(0x700U, fde->pc_start); |
| 794 | EXPECT_EQ(0x800U, fde->pc_end); |
| 795 | |
| 796 | // 0x800 - 0x900 FDE 4 |
| 797 | fde = this->debug_frame_->GetFdeFromPc(0x850); |
| 798 | ASSERT_TRUE(fde != nullptr); |
| 799 | EXPECT_EQ(0x150U, fde->pc_start); |
| 800 | EXPECT_EQ(0xb50U, fde->pc_end); |
| 801 | |
| 802 | // 0xa00 - 0xb00 FDE 3 |
| 803 | fde = this->debug_frame_->GetFdeFromPc(0xa35); |
| 804 | ASSERT_TRUE(fde != nullptr); |
| 805 | EXPECT_EQ(0xa00U, fde->pc_start); |
| 806 | EXPECT_EQ(0xb00U, fde->pc_end); |
| 807 | |
| 808 | // 0xb00 - 0xb50 FDE 4 |
| 809 | fde = this->debug_frame_->GetFdeFromPc(0xb20); |
| 810 | ASSERT_TRUE(fde != nullptr); |
| 811 | EXPECT_EQ(0x150U, fde->pc_start); |
| 812 | EXPECT_EQ(0xb50U, fde->pc_end); |
| 813 | } |
| 814 | |
Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 815 | REGISTER_TYPED_TEST_SUITE_P( |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 816 | DwarfDebugFrameTest, GetFdes32, GetFdes32_after_GetFdeFromPc, GetFdes32_not_in_section, |
| 817 | GetFdeFromPc32, GetFdeFromPc32_reverse, GetFdeFromPc32_not_in_section, GetFdes64, |
| 818 | GetFdes64_after_GetFdeFromPc, GetFdes64_not_in_section, GetFdeFromPc64, GetFdeFromPc64_reverse, |
| 819 | GetFdeFromPc64_not_in_section, GetCieFde32, GetCieFde64, GetCieFromOffset32_cie_cached, |
| 820 | GetCieFromOffset64_cie_cached, GetCieFromOffset32_version1, GetCieFromOffset64_version1, |
| 821 | GetCieFromOffset32_version3, GetCieFromOffset64_version3, GetCieFromOffset32_version4, |
Christopher Ferris | c312c9a | 2019-04-01 16:53:56 -0700 | [diff] [blame] | 822 | GetCieFromOffset64_version4, GetCieFromOffset32_version5, GetCieFromOffset64_version5, |
| 823 | GetCieFromOffset_version_invalid, GetCieFromOffset32_augment, GetCieFromOffset64_augment, |
| 824 | GetFdeFromOffset32_augment, GetFdeFromOffset64_augment, GetFdeFromOffset32_lsda_address, |
| 825 | GetFdeFromOffset64_lsda_address, GetFdeFromPc_interleaved); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 826 | |
| 827 | typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes; |
Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 828 | INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 829 | |
| 830 | } // namespace unwindstack |