| 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 | 61d4097 | 2017-06-12 19:14:20 -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> | 
|  | 22 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 23 | #include "DwarfEhFrame.h" | 
|  | 24 | #include "DwarfEncoding.h" | 
|  | 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> | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 32 | class DwarfEhFrameTest : public ::testing::Test { | 
|  | 33 | protected: | 
|  | 34 | void SetUp() override { | 
|  | 35 | memory_.Clear(); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 36 | eh_frame_ = new DwarfEhFrame<TypeParam>(&memory_); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 37 | ResetLogs(); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | void TearDown() override { delete eh_frame_; } | 
|  | 41 |  | 
|  | 42 | MemoryFake memory_; | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 43 | DwarfEhFrame<TypeParam>* eh_frame_ = nullptr; | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 44 | }; | 
| Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 45 | TYPED_TEST_SUITE_P(DwarfEhFrameTest); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | // NOTE: All test class variables need to be referenced as this->. | 
|  | 48 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 49 | // Only verify different cie/fde format. All other DwarfSection corner | 
|  | 50 | // cases are tested in DwarfDebugFrameTest.cpp. | 
|  | 51 |  | 
|  | 52 | TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset32) { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 53 | // CIE 32 information. | 
|  | 54 | this->memory_.SetData32(0x5000, 0xfc); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 55 | // Indicates this is a cie for eh_frame. | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 56 | this->memory_.SetData32(0x5004, 0); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 57 | this->memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 16, 32, 1}); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 58 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 59 | // FDE 32 information. | 
|  | 60 | this->memory_.SetData32(0x5100, 0xfc); | 
|  | 61 | this->memory_.SetData32(0x5104, 0x104); | 
|  | 62 | this->memory_.SetData32(0x5108, 0x1500); | 
|  | 63 | this->memory_.SetData32(0x510c, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 64 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 65 | const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100); | 
|  | 66 | ASSERT_TRUE(fde != nullptr); | 
|  | 67 | EXPECT_EQ(0x5000U, fde->cie_offset); | 
|  | 68 | EXPECT_EQ(0x5110U, fde->cfa_instructions_offset); | 
|  | 69 | EXPECT_EQ(0x5200U, fde->cfa_instructions_end); | 
|  | 70 | EXPECT_EQ(0x6608U, fde->pc_start); | 
|  | 71 | EXPECT_EQ(0x6808U, fde->pc_end); | 
|  | 72 | EXPECT_EQ(0U, fde->lsda_address); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 73 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 74 | const DwarfCie* cie = fde->cie; | 
|  | 75 | ASSERT_TRUE(cie != nullptr); | 
|  | 76 | EXPECT_EQ(1U, cie->version); | 
|  | 77 | EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding); | 
|  | 78 | EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding); | 
|  | 79 | EXPECT_EQ(0U, cie->segment_size); | 
|  | 80 | EXPECT_EQ('\0', cie->augmentation_string[0]); | 
|  | 81 | EXPECT_EQ(0U, cie->personality_handler); | 
|  | 82 | EXPECT_EQ(0x500dU, cie->cfa_instructions_offset); | 
|  | 83 | EXPECT_EQ(0x5100U, cie->cfa_instructions_end); | 
|  | 84 | EXPECT_EQ(16U, cie->code_alignment_factor); | 
|  | 85 | EXPECT_EQ(32U, cie->data_alignment_factor); | 
|  | 86 | EXPECT_EQ(1U, cie->return_address_register); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 89 | TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset64) { | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 90 | // CIE 64 information. | 
|  | 91 | this->memory_.SetData32(0x5000, 0xffffffff); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 92 | this->memory_.SetData64(0x5004, 0xfc); | 
|  | 93 | // Indicates this is a cie for eh_frame. | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 94 | this->memory_.SetData64(0x500c, 0); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 95 | this->memory_.SetMemory(0x5014, std::vector<uint8_t>{1, '\0', 16, 32, 1}); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 96 |  | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 97 | // FDE 64 information. | 
|  | 98 | this->memory_.SetData32(0x5100, 0xffffffff); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 99 | this->memory_.SetData64(0x5104, 0xfc); | 
| Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 100 | this->memory_.SetData64(0x510c, 0x10c); | 
|  | 101 | this->memory_.SetData64(0x5114, 0x1500); | 
|  | 102 | this->memory_.SetData64(0x511c, 0x200); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 103 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 104 | const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 105 | ASSERT_TRUE(fde != nullptr); | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 106 | EXPECT_EQ(0x5000U, fde->cie_offset); | 
|  | 107 | EXPECT_EQ(0x5124U, fde->cfa_instructions_offset); | 
|  | 108 | EXPECT_EQ(0x5208U, fde->cfa_instructions_end); | 
|  | 109 | EXPECT_EQ(0x6618U, fde->pc_start); | 
|  | 110 | EXPECT_EQ(0x6818U, fde->pc_end); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 111 | EXPECT_EQ(0U, fde->lsda_address); | 
|  | 112 |  | 
| Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 113 | const DwarfCie* cie = fde->cie; | 
|  | 114 | ASSERT_TRUE(cie != nullptr); | 
|  | 115 | EXPECT_EQ(1U, cie->version); | 
|  | 116 | EXPECT_EQ(DW_EH_PE_sdata8, cie->fde_address_encoding); | 
|  | 117 | EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding); | 
|  | 118 | EXPECT_EQ(0U, cie->segment_size); | 
|  | 119 | EXPECT_EQ('\0', cie->augmentation_string[0]); | 
|  | 120 | EXPECT_EQ(0U, cie->personality_handler); | 
|  | 121 | EXPECT_EQ(0x5019U, cie->cfa_instructions_offset); | 
|  | 122 | EXPECT_EQ(0x5108U, cie->cfa_instructions_end); | 
|  | 123 | EXPECT_EQ(16U, cie->code_alignment_factor); | 
|  | 124 | EXPECT_EQ(32U, cie->data_alignment_factor); | 
|  | 125 | EXPECT_EQ(1U, cie->return_address_register); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Christopher Ferris | 7e21eba | 2019-06-20 16:16:42 -0700 | [diff] [blame] | 128 | REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64); | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 129 |  | 
|  | 130 | typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes; | 
| Haibo Huang | 0c01bb6 | 2019-12-11 12:46:20 -0800 | [diff] [blame] | 131 | INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfEhFrameTest, DwarfEhFrameTestTypes); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | }  // namespace unwindstack |