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 <gtest/gtest.h>
20
21 #include <unwindstack/DwarfError.h>
22
23 #include "DwarfEhFrame.h"
24 #include "DwarfEncoding.h"
25
26 #include "LogFake.h"
27 #include "MemoryFake.h"
28
29 namespace unwindstack {
30
31 template <typename TypeParam>
32 class DwarfEhFrameTest : public ::testing::Test {
33 protected:
SetUp()34 void SetUp() override {
35 memory_.Clear();
36 eh_frame_ = new DwarfEhFrame<TypeParam>(&memory_);
37 ResetLogs();
38 }
39
TearDown()40 void TearDown() override { delete eh_frame_; }
41
42 MemoryFake memory_;
43 DwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
44 };
45 TYPED_TEST_SUITE_P(DwarfEhFrameTest);
46
47 // NOTE: All test class variables need to be referenced as this->.
48
49 // Only verify different cie/fde format. All other DwarfSection corner
50 // cases are tested in DwarfDebugFrameTest.cpp.
51
TYPED_TEST_P(DwarfEhFrameTest,GetFdeCieFromOffset32)52 TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset32) {
53 // CIE 32 information.
54 this->memory_.SetData32(0x5000, 0xfc);
55 // Indicates this is a cie for eh_frame.
56 this->memory_.SetData32(0x5004, 0);
57 this->memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 16, 32, 1});
58
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);
64
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);
73
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);
87 }
88
TYPED_TEST_P(DwarfEhFrameTest,GetFdeCieFromOffset64)89 TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset64) {
90 // CIE 64 information.
91 this->memory_.SetData32(0x5000, 0xffffffff);
92 this->memory_.SetData64(0x5004, 0xfc);
93 // Indicates this is a cie for eh_frame.
94 this->memory_.SetData64(0x500c, 0);
95 this->memory_.SetMemory(0x5014, std::vector<uint8_t>{1, '\0', 16, 32, 1});
96
97 // FDE 64 information.
98 this->memory_.SetData32(0x5100, 0xffffffff);
99 this->memory_.SetData64(0x5104, 0xfc);
100 this->memory_.SetData64(0x510c, 0x10c);
101 this->memory_.SetData64(0x5114, 0x1500);
102 this->memory_.SetData64(0x511c, 0x200);
103
104 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100);
105 ASSERT_TRUE(fde != nullptr);
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);
111 EXPECT_EQ(0U, fde->lsda_address);
112
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);
126 }
127
128 REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64);
129
130 typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes;
131 INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfEhFrameTest, DwarfEhFrameTestTypes);
132
133 } // namespace unwindstack
134