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