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 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "DwarfDebugFrame.h" |
| 23 | #include "DwarfEncoding.h" |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 24 | #include "DwarfError.h" |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 25 | |
| 26 | #include "LogFake.h" |
| 27 | #include "MemoryFake.h" |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 28 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 29 | namespace unwindstack { |
| 30 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 31 | template <typename TypeParam> |
| 32 | class MockDwarfDebugFrame : public DwarfDebugFrame<TypeParam> { |
| 33 | public: |
| 34 | MockDwarfDebugFrame(Memory* memory) : DwarfDebugFrame<TypeParam>(memory) {} |
| 35 | ~MockDwarfDebugFrame() = default; |
| 36 | |
| 37 | void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 38 | void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; } |
| 39 | void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 40 | void TestPushFdeInfo(const typename DwarfDebugFrame<TypeParam>::FdeInfo& info) { |
| 41 | this->fdes_.push_back(info); |
| 42 | } |
| 43 | |
| 44 | uint64_t TestGetFdeCount() { return this->fde_count_; } |
| 45 | uint8_t TestGetOffset() { return this->offset_; } |
| 46 | uint8_t TestGetEndOffset() { return this->end_offset_; } |
| 47 | void TestGetFdeInfo(size_t index, typename DwarfDebugFrame<TypeParam>::FdeInfo* info) { |
| 48 | *info = this->fdes_[index]; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | template <typename TypeParam> |
| 53 | class DwarfDebugFrameTest : public ::testing::Test { |
| 54 | protected: |
| 55 | void SetUp() override { |
| 56 | memory_.Clear(); |
| 57 | debug_frame_ = new MockDwarfDebugFrame<TypeParam>(&memory_); |
| 58 | ResetLogs(); |
| 59 | } |
| 60 | |
| 61 | void TearDown() override { delete debug_frame_; } |
| 62 | |
| 63 | MemoryFake memory_; |
| 64 | MockDwarfDebugFrame<TypeParam>* debug_frame_ = nullptr; |
| 65 | }; |
| 66 | TYPED_TEST_CASE_P(DwarfDebugFrameTest); |
| 67 | |
| 68 | // NOTE: All test class variables need to be referenced as this->. |
| 69 | |
| 70 | TYPED_TEST_P(DwarfDebugFrameTest, Init32) { |
| 71 | // CIE 32 information. |
| 72 | this->memory_.SetData32(0x5000, 0xfc); |
| 73 | this->memory_.SetData32(0x5004, 0xffffffff); |
| 74 | this->memory_.SetData8(0x5008, 1); |
| 75 | this->memory_.SetData8(0x5009, '\0'); |
| 76 | |
| 77 | // FDE 32 information. |
| 78 | this->memory_.SetData32(0x5100, 0xfc); |
| 79 | this->memory_.SetData32(0x5104, 0); |
| 80 | this->memory_.SetData32(0x5108, 0x1500); |
| 81 | this->memory_.SetData32(0x510c, 0x200); |
| 82 | |
| 83 | this->memory_.SetData32(0x5200, 0xfc); |
| 84 | this->memory_.SetData32(0x5204, 0); |
| 85 | this->memory_.SetData32(0x5208, 0x2500); |
| 86 | this->memory_.SetData32(0x520c, 0x300); |
| 87 | |
| 88 | // CIE 32 information. |
| 89 | this->memory_.SetData32(0x5300, 0xfc); |
| 90 | this->memory_.SetData32(0x5304, 0xffffffff); |
| 91 | this->memory_.SetData8(0x5308, 1); |
| 92 | this->memory_.SetData8(0x5309, '\0'); |
| 93 | |
| 94 | // FDE 32 information. |
| 95 | this->memory_.SetData32(0x5400, 0xfc); |
| 96 | this->memory_.SetData32(0x5404, 0x300); |
| 97 | this->memory_.SetData32(0x5408, 0x3500); |
| 98 | this->memory_.SetData32(0x540c, 0x400); |
| 99 | |
| 100 | this->memory_.SetData32(0x5500, 0xfc); |
| 101 | this->memory_.SetData32(0x5504, 0x300); |
| 102 | this->memory_.SetData32(0x5508, 0x4500); |
| 103 | this->memory_.SetData32(0x550c, 0x500); |
| 104 | |
| 105 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600)); |
| 106 | ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount()); |
| 107 | |
| 108 | typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0); |
| 109 | |
| 110 | this->debug_frame_->TestGetFdeInfo(0, &info); |
| 111 | EXPECT_EQ(0x5100U, info.offset); |
| 112 | EXPECT_EQ(0x1500U, info.start); |
| 113 | EXPECT_EQ(0x1700U, info.end); |
| 114 | |
| 115 | this->debug_frame_->TestGetFdeInfo(1, &info); |
| 116 | EXPECT_EQ(0x5200U, info.offset); |
| 117 | EXPECT_EQ(0x2500U, info.start); |
| 118 | EXPECT_EQ(0x2800U, info.end); |
| 119 | |
| 120 | this->debug_frame_->TestGetFdeInfo(2, &info); |
| 121 | EXPECT_EQ(0x5400U, info.offset); |
| 122 | EXPECT_EQ(0x3500U, info.start); |
| 123 | EXPECT_EQ(0x3900U, info.end); |
| 124 | |
| 125 | this->debug_frame_->TestGetFdeInfo(3, &info); |
| 126 | EXPECT_EQ(0x5500U, info.offset); |
| 127 | EXPECT_EQ(0x4500U, info.start); |
| 128 | EXPECT_EQ(0x4a00U, info.end); |
| 129 | } |
| 130 | |
| 131 | TYPED_TEST_P(DwarfDebugFrameTest, Init32_fde_not_following_cie) { |
| 132 | // CIE 32 information. |
| 133 | this->memory_.SetData32(0x5000, 0xfc); |
| 134 | this->memory_.SetData32(0x5004, 0xffffffff); |
| 135 | this->memory_.SetData8(0x5008, 1); |
| 136 | this->memory_.SetData8(0x5009, '\0'); |
| 137 | |
| 138 | // FDE 32 information. |
| 139 | this->memory_.SetData32(0x5100, 0xfc); |
| 140 | this->memory_.SetData32(0x5104, 0x1000); |
| 141 | this->memory_.SetData32(0x5108, 0x1500); |
| 142 | this->memory_.SetData32(0x510c, 0x200); |
| 143 | |
| 144 | ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600)); |
| 145 | ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->last_error()); |
| 146 | } |
| 147 | |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame^] | 148 | TYPED_TEST_P(DwarfDebugFrameTest, Init32_do_not_fail_on_bad_next_entry) { |
| 149 | // CIE 32 information. |
| 150 | this->memory_.SetData32(0x5000, 0xfc); |
| 151 | this->memory_.SetData32(0x5004, 0xffffffff); |
| 152 | this->memory_.SetData8(0x5008, 1); |
| 153 | this->memory_.SetData8(0x5009, '\0'); |
| 154 | |
| 155 | // FDE 32 information. |
| 156 | this->memory_.SetData32(0x5100, 0xfc); |
| 157 | this->memory_.SetData32(0x5104, 0); |
| 158 | this->memory_.SetData32(0x5108, 0x1500); |
| 159 | this->memory_.SetData32(0x510c, 0x200); |
| 160 | |
| 161 | this->memory_.SetData32(0x5200, 0xfc); |
| 162 | this->memory_.SetData32(0x5204, 0); |
| 163 | this->memory_.SetData32(0x5208, 0x2500); |
| 164 | this->memory_.SetData32(0x520c, 0x300); |
| 165 | |
| 166 | // CIE 32 information. |
| 167 | this->memory_.SetData32(0x5300, 0); |
| 168 | this->memory_.SetData32(0x5304, 0xffffffff); |
| 169 | this->memory_.SetData8(0x5308, 1); |
| 170 | this->memory_.SetData8(0x5309, '\0'); |
| 171 | |
| 172 | // FDE 32 information. |
| 173 | this->memory_.SetData32(0x5400, 0xfc); |
| 174 | this->memory_.SetData32(0x5404, 0x300); |
| 175 | this->memory_.SetData32(0x5408, 0x3500); |
| 176 | this->memory_.SetData32(0x540c, 0x400); |
| 177 | |
| 178 | this->memory_.SetData32(0x5500, 0xfc); |
| 179 | this->memory_.SetData32(0x5504, 0x300); |
| 180 | this->memory_.SetData32(0x5508, 0x4500); |
| 181 | this->memory_.SetData32(0x550c, 0x500); |
| 182 | |
| 183 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600)); |
| 184 | ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount()); |
| 185 | } |
| 186 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 187 | TYPED_TEST_P(DwarfDebugFrameTest, Init64) { |
| 188 | // CIE 64 information. |
| 189 | this->memory_.SetData32(0x5000, 0xffffffff); |
| 190 | this->memory_.SetData64(0x5004, 0xf4); |
| 191 | this->memory_.SetData64(0x500c, 0xffffffffffffffffULL); |
| 192 | this->memory_.SetData8(0x5014, 1); |
| 193 | this->memory_.SetData8(0x5015, '\0'); |
| 194 | |
| 195 | // FDE 64 information. |
| 196 | this->memory_.SetData32(0x5100, 0xffffffff); |
| 197 | this->memory_.SetData64(0x5104, 0xf4); |
| 198 | this->memory_.SetData64(0x510c, 0); |
| 199 | this->memory_.SetData64(0x5114, 0x1500); |
| 200 | this->memory_.SetData64(0x511c, 0x200); |
| 201 | |
| 202 | this->memory_.SetData32(0x5200, 0xffffffff); |
| 203 | this->memory_.SetData64(0x5204, 0xf4); |
| 204 | this->memory_.SetData64(0x520c, 0); |
| 205 | this->memory_.SetData64(0x5214, 0x2500); |
| 206 | this->memory_.SetData64(0x521c, 0x300); |
| 207 | |
| 208 | // CIE 64 information. |
| 209 | this->memory_.SetData32(0x5300, 0xffffffff); |
| 210 | this->memory_.SetData64(0x5304, 0xf4); |
| 211 | this->memory_.SetData64(0x530c, 0xffffffffffffffffULL); |
| 212 | this->memory_.SetData8(0x5314, 1); |
| 213 | this->memory_.SetData8(0x5315, '\0'); |
| 214 | |
| 215 | // FDE 64 information. |
| 216 | this->memory_.SetData32(0x5400, 0xffffffff); |
| 217 | this->memory_.SetData64(0x5404, 0xf4); |
| 218 | this->memory_.SetData64(0x540c, 0x300); |
| 219 | this->memory_.SetData64(0x5414, 0x3500); |
| 220 | this->memory_.SetData64(0x541c, 0x400); |
| 221 | |
| 222 | this->memory_.SetData32(0x5500, 0xffffffff); |
| 223 | this->memory_.SetData64(0x5504, 0xf4); |
| 224 | this->memory_.SetData64(0x550c, 0x300); |
| 225 | this->memory_.SetData64(0x5514, 0x4500); |
| 226 | this->memory_.SetData64(0x551c, 0x500); |
| 227 | |
| 228 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600)); |
| 229 | ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount()); |
| 230 | |
| 231 | typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0); |
| 232 | |
| 233 | this->debug_frame_->TestGetFdeInfo(0, &info); |
| 234 | EXPECT_EQ(0x5100U, info.offset); |
| 235 | EXPECT_EQ(0x1500U, info.start); |
| 236 | EXPECT_EQ(0x1700U, info.end); |
| 237 | |
| 238 | this->debug_frame_->TestGetFdeInfo(1, &info); |
| 239 | EXPECT_EQ(0x5200U, info.offset); |
| 240 | EXPECT_EQ(0x2500U, info.start); |
| 241 | EXPECT_EQ(0x2800U, info.end); |
| 242 | |
| 243 | this->debug_frame_->TestGetFdeInfo(2, &info); |
| 244 | EXPECT_EQ(0x5400U, info.offset); |
| 245 | EXPECT_EQ(0x3500U, info.start); |
| 246 | EXPECT_EQ(0x3900U, info.end); |
| 247 | |
| 248 | this->debug_frame_->TestGetFdeInfo(3, &info); |
| 249 | EXPECT_EQ(0x5500U, info.offset); |
| 250 | EXPECT_EQ(0x4500U, info.start); |
| 251 | EXPECT_EQ(0x4a00U, info.end); |
| 252 | } |
| 253 | |
| 254 | TYPED_TEST_P(DwarfDebugFrameTest, Init64_fde_not_following_cie) { |
| 255 | // CIE 64 information. |
| 256 | this->memory_.SetData32(0x5000, 0xffffffff); |
| 257 | this->memory_.SetData64(0x5004, 0xf4); |
| 258 | this->memory_.SetData64(0x500c, 0xffffffffffffffffULL); |
| 259 | this->memory_.SetData8(0x5014, 1); |
| 260 | this->memory_.SetData8(0x5015, '\0'); |
| 261 | |
| 262 | // FDE 64 information. |
| 263 | this->memory_.SetData32(0x5100, 0xffffffff); |
| 264 | this->memory_.SetData64(0x5104, 0xf4); |
| 265 | this->memory_.SetData64(0x510c, 0x1000); |
| 266 | this->memory_.SetData64(0x5114, 0x1500); |
| 267 | this->memory_.SetData64(0x511c, 0x200); |
| 268 | |
| 269 | ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600)); |
| 270 | ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->last_error()); |
| 271 | } |
| 272 | |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame^] | 273 | TYPED_TEST_P(DwarfDebugFrameTest, Init64_do_not_fail_on_bad_next_entry) { |
| 274 | // CIE 64 information. |
| 275 | this->memory_.SetData32(0x5000, 0xffffffff); |
| 276 | this->memory_.SetData64(0x5004, 0xf4); |
| 277 | this->memory_.SetData64(0x500c, 0xffffffffffffffffULL); |
| 278 | this->memory_.SetData8(0x5014, 1); |
| 279 | this->memory_.SetData8(0x5015, '\0'); |
| 280 | |
| 281 | // FDE 64 information. |
| 282 | this->memory_.SetData32(0x5100, 0xffffffff); |
| 283 | this->memory_.SetData64(0x5104, 0xf4); |
| 284 | this->memory_.SetData64(0x510c, 0); |
| 285 | this->memory_.SetData64(0x5114, 0x1500); |
| 286 | this->memory_.SetData64(0x511c, 0x200); |
| 287 | |
| 288 | this->memory_.SetData32(0x5200, 0xffffffff); |
| 289 | this->memory_.SetData64(0x5204, 0xf4); |
| 290 | this->memory_.SetData64(0x520c, 0); |
| 291 | this->memory_.SetData64(0x5214, 0x2500); |
| 292 | this->memory_.SetData64(0x521c, 0x300); |
| 293 | |
| 294 | // CIE 64 information. |
| 295 | this->memory_.SetData32(0x5300, 0xffffffff); |
| 296 | this->memory_.SetData64(0x5304, 0); |
| 297 | this->memory_.SetData64(0x530c, 0xffffffffffffffffULL); |
| 298 | this->memory_.SetData8(0x5314, 1); |
| 299 | this->memory_.SetData8(0x5315, '\0'); |
| 300 | |
| 301 | // FDE 64 information. |
| 302 | this->memory_.SetData32(0x5400, 0xffffffff); |
| 303 | this->memory_.SetData64(0x5404, 0xf4); |
| 304 | this->memory_.SetData64(0x540c, 0x300); |
| 305 | this->memory_.SetData64(0x5414, 0x3500); |
| 306 | this->memory_.SetData64(0x541c, 0x400); |
| 307 | |
| 308 | this->memory_.SetData32(0x5500, 0xffffffff); |
| 309 | this->memory_.SetData64(0x5504, 0xf4); |
| 310 | this->memory_.SetData64(0x550c, 0x300); |
| 311 | this->memory_.SetData64(0x5514, 0x4500); |
| 312 | this->memory_.SetData64(0x551c, 0x500); |
| 313 | |
| 314 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600)); |
| 315 | ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount()); |
| 316 | } |
| 317 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 318 | TYPED_TEST_P(DwarfDebugFrameTest, Init_version1) { |
| 319 | // CIE 32 information. |
| 320 | this->memory_.SetData32(0x5000, 0xfc); |
| 321 | this->memory_.SetData32(0x5004, 0xffffffff); |
| 322 | this->memory_.SetData8(0x5008, 1); |
| 323 | // Augment string. |
| 324 | this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'}); |
| 325 | // Code alignment factor. |
| 326 | this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00}); |
| 327 | // Data alignment factor. |
| 328 | this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00}); |
| 329 | // Return address register |
| 330 | this->memory_.SetData8(0x5014, 0x84); |
| 331 | // Augmentation length |
| 332 | this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00}); |
| 333 | // R data. |
| 334 | this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2); |
| 335 | |
| 336 | // FDE 32 information. |
| 337 | this->memory_.SetData32(0x5100, 0xfc); |
| 338 | this->memory_.SetData32(0x5104, 0); |
| 339 | this->memory_.SetData16(0x5108, 0x1500); |
| 340 | this->memory_.SetData16(0x510a, 0x200); |
| 341 | |
| 342 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200)); |
| 343 | ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount()); |
| 344 | |
| 345 | typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0); |
| 346 | this->debug_frame_->TestGetFdeInfo(0, &info); |
| 347 | EXPECT_EQ(0x5100U, info.offset); |
| 348 | EXPECT_EQ(0x1500U, info.start); |
| 349 | EXPECT_EQ(0x1700U, info.end); |
| 350 | } |
| 351 | |
| 352 | TYPED_TEST_P(DwarfDebugFrameTest, Init_version4) { |
| 353 | // CIE 32 information. |
| 354 | this->memory_.SetData32(0x5000, 0xfc); |
| 355 | this->memory_.SetData32(0x5004, 0xffffffff); |
| 356 | this->memory_.SetData8(0x5008, 4); |
| 357 | // Augment string. |
| 358 | this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'}); |
| 359 | // Address size. |
| 360 | this->memory_.SetData8(0x500e, 4); |
| 361 | // Segment size. |
| 362 | this->memory_.SetData8(0x500f, 0); |
| 363 | // Code alignment factor. |
| 364 | this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00}); |
| 365 | // Data alignment factor. |
| 366 | this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00}); |
| 367 | // Return address register |
| 368 | this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10}); |
| 369 | // Augmentation length |
| 370 | this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00}); |
| 371 | // L data. |
| 372 | this->memory_.SetData8(0x501a, 0x10); |
| 373 | // P data. |
| 374 | this->memory_.SetData8(0x501b, DW_EH_PE_udata4); |
| 375 | this->memory_.SetData32(0x501c, 0x100); |
| 376 | // R data. |
| 377 | this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2); |
| 378 | |
| 379 | // FDE 32 information. |
| 380 | this->memory_.SetData32(0x5100, 0xfc); |
| 381 | this->memory_.SetData32(0x5104, 0); |
| 382 | this->memory_.SetData16(0x5108, 0x1500); |
| 383 | this->memory_.SetData16(0x510a, 0x200); |
| 384 | |
| 385 | ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200)); |
| 386 | ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount()); |
| 387 | |
| 388 | typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0); |
| 389 | this->debug_frame_->TestGetFdeInfo(0, &info); |
| 390 | EXPECT_EQ(0x5100U, info.offset); |
| 391 | EXPECT_EQ(0x1500U, info.start); |
| 392 | EXPECT_EQ(0x1700U, info.end); |
| 393 | } |
| 394 | |
| 395 | TYPED_TEST_P(DwarfDebugFrameTest, GetFdeOffsetFromPc) { |
| 396 | typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0); |
| 397 | for (size_t i = 0; i < 9; i++) { |
| 398 | info.start = 0x1000 * (i + 1); |
| 399 | info.end = 0x1000 * (i + 2) - 0x10; |
| 400 | info.offset = 0x5000 + i * 0x20; |
| 401 | this->debug_frame_->TestPushFdeInfo(info); |
| 402 | } |
| 403 | |
| 404 | this->debug_frame_->TestSetFdeCount(0); |
| 405 | uint64_t fde_offset; |
| 406 | ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset)); |
| 407 | ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error()); |
| 408 | |
| 409 | this->debug_frame_->TestSetFdeCount(9); |
| 410 | ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x100, &fde_offset)); |
| 411 | ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error()); |
| 412 | // Odd number of elements. |
| 413 | for (size_t i = 0; i < 9; i++) { |
| 414 | TypeParam pc = 0x1000 * (i + 1); |
| 415 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i; |
| 416 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 417 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " |
| 418 | << i; |
| 419 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 420 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset)) |
| 421 | << "Failed at index " << i; |
| 422 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 423 | ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset)) |
| 424 | << "Failed at index " << i; |
| 425 | ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error()); |
| 426 | } |
| 427 | |
| 428 | // Even number of elements. |
| 429 | this->debug_frame_->TestSetFdeCount(10); |
| 430 | info.start = 0xa000; |
| 431 | info.end = 0xaff0; |
| 432 | info.offset = 0x5120; |
| 433 | this->debug_frame_->TestPushFdeInfo(info); |
| 434 | |
| 435 | for (size_t i = 0; i < 10; i++) { |
| 436 | TypeParam pc = 0x1000 * (i + 1); |
| 437 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i; |
| 438 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 439 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index " |
| 440 | << i; |
| 441 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 442 | ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset)) |
| 443 | << "Failed at index " << i; |
| 444 | EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i; |
| 445 | ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset)) |
| 446 | << "Failed at index " << i; |
| 447 | ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error()); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) { |
| 452 | this->debug_frame_->TestSetOffset(0x4000); |
| 453 | |
| 454 | // CIE 32 information. |
| 455 | this->memory_.SetData32(0xf000, 0x100); |
| 456 | this->memory_.SetData32(0xf004, 0xffffffff); |
| 457 | this->memory_.SetData8(0xf008, 0x1); |
| 458 | this->memory_.SetData8(0xf009, '\0'); |
| 459 | this->memory_.SetData8(0xf00a, 4); |
| 460 | this->memory_.SetData8(0xf00b, 8); |
| 461 | this->memory_.SetData8(0xf00c, 0x20); |
| 462 | |
| 463 | // FDE 32 information. |
| 464 | this->memory_.SetData32(0x14000, 0x20); |
| 465 | this->memory_.SetData32(0x14004, 0xb000); |
| 466 | this->memory_.SetData32(0x14008, 0x9000); |
| 467 | this->memory_.SetData32(0x1400c, 0x100); |
| 468 | |
| 469 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000); |
| 470 | ASSERT_TRUE(fde != nullptr); |
| 471 | EXPECT_EQ(0x14010U, fde->cfa_instructions_offset); |
| 472 | EXPECT_EQ(0x14024U, fde->cfa_instructions_end); |
| 473 | EXPECT_EQ(0x9000U, fde->pc_start); |
| 474 | EXPECT_EQ(0x9100U, fde->pc_end); |
| 475 | EXPECT_EQ(0xf000U, fde->cie_offset); |
| 476 | EXPECT_EQ(0U, fde->lsda_address); |
| 477 | |
| 478 | ASSERT_TRUE(fde->cie != nullptr); |
| 479 | EXPECT_EQ(1U, fde->cie->version); |
| 480 | EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding); |
| 481 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); |
| 482 | EXPECT_EQ(0U, fde->cie->segment_size); |
| 483 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); |
| 484 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); |
| 485 | EXPECT_EQ(0U, fde->cie->personality_handler); |
| 486 | EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset); |
| 487 | EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end); |
| 488 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); |
| 489 | EXPECT_EQ(8, fde->cie->data_alignment_factor); |
| 490 | EXPECT_EQ(0x20U, fde->cie->return_address_register); |
| 491 | } |
| 492 | |
| 493 | TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) { |
| 494 | this->debug_frame_->TestSetOffset(0x2000); |
| 495 | |
| 496 | // CIE 64 information. |
| 497 | this->memory_.SetData32(0x6000, 0xffffffff); |
| 498 | this->memory_.SetData64(0x6004, 0x100); |
| 499 | this->memory_.SetData64(0x600c, 0xffffffffffffffffULL); |
| 500 | this->memory_.SetData8(0x6014, 0x1); |
| 501 | this->memory_.SetData8(0x6015, '\0'); |
| 502 | this->memory_.SetData8(0x6016, 4); |
| 503 | this->memory_.SetData8(0x6017, 8); |
| 504 | this->memory_.SetData8(0x6018, 0x20); |
| 505 | |
| 506 | // FDE 64 information. |
| 507 | this->memory_.SetData32(0x8000, 0xffffffff); |
| 508 | this->memory_.SetData64(0x8004, 0x200); |
| 509 | this->memory_.SetData64(0x800c, 0x4000); |
| 510 | this->memory_.SetData64(0x8014, 0x5000); |
| 511 | this->memory_.SetData64(0x801c, 0x300); |
| 512 | |
| 513 | const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000); |
| 514 | ASSERT_TRUE(fde != nullptr); |
| 515 | EXPECT_EQ(0x8024U, fde->cfa_instructions_offset); |
| 516 | EXPECT_EQ(0x820cU, fde->cfa_instructions_end); |
| 517 | EXPECT_EQ(0x5000U, fde->pc_start); |
| 518 | EXPECT_EQ(0x5300U, fde->pc_end); |
| 519 | EXPECT_EQ(0x6000U, fde->cie_offset); |
| 520 | EXPECT_EQ(0U, fde->lsda_address); |
| 521 | |
| 522 | ASSERT_TRUE(fde->cie != nullptr); |
| 523 | EXPECT_EQ(1U, fde->cie->version); |
| 524 | EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding); |
| 525 | EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding); |
| 526 | EXPECT_EQ(0U, fde->cie->segment_size); |
| 527 | EXPECT_EQ(1U, fde->cie->augmentation_string.size()); |
| 528 | EXPECT_EQ('\0', fde->cie->augmentation_string[0]); |
| 529 | EXPECT_EQ(0U, fde->cie->personality_handler); |
| 530 | EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset); |
| 531 | EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end); |
| 532 | EXPECT_EQ(4U, fde->cie->code_alignment_factor); |
| 533 | EXPECT_EQ(8, fde->cie->data_alignment_factor); |
| 534 | EXPECT_EQ(0x20U, fde->cie->return_address_register); |
| 535 | } |
| 536 | |
Christopher Ferris | 1a141a0 | 2018-01-24 08:52:47 -0800 | [diff] [blame^] | 537 | REGISTER_TYPED_TEST_CASE_P(DwarfDebugFrameTest, Init32, Init32_fde_not_following_cie, |
| 538 | Init32_do_not_fail_on_bad_next_entry, Init64, |
| 539 | Init64_do_not_fail_on_bad_next_entry, Init64_fde_not_following_cie, |
| 540 | Init_version1, Init_version4, GetFdeOffsetFromPc, GetCieFde32, |
| 541 | GetCieFde64); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 542 | |
| 543 | typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes; |
| 544 | INSTANTIATE_TYPED_TEST_CASE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 545 | |
| 546 | } // namespace unwindstack |