blob: 4792fb50e24a193f605c32cf46bfffcc3126a7b1 [file] [log] [blame]
Christopher Ferris61d40972017-06-12 19:14:20 -07001/*
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 Ferris61d40972017-06-12 19:14:20 -070019#include <gtest/gtest.h>
20
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080021#include <unwindstack/DwarfError.h>
22
Christopher Ferris61d40972017-06-12 19:14:20 -070023#include "DwarfEhFrame.h"
24#include "DwarfEncoding.h"
25
26#include "LogFake.h"
27#include "MemoryFake.h"
Christopher Ferris61d40972017-06-12 19:14:20 -070028
Christopher Ferrisd226a512017-07-14 10:37:19 -070029namespace unwindstack {
30
Christopher Ferris61d40972017-06-12 19:14:20 -070031template <typename TypeParam>
Christopher Ferris61d40972017-06-12 19:14:20 -070032class DwarfEhFrameTest : public ::testing::Test {
33 protected:
34 void SetUp() override {
35 memory_.Clear();
Christopher Ferris92acaac2018-06-21 10:44:02 -070036 eh_frame_ = new DwarfEhFrame<TypeParam>(&memory_);
Christopher Ferris61d40972017-06-12 19:14:20 -070037 ResetLogs();
38 }
39
40 void TearDown() override { delete eh_frame_; }
41
42 MemoryFake memory_;
Christopher Ferris92acaac2018-06-21 10:44:02 -070043 DwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
Christopher Ferris61d40972017-06-12 19:14:20 -070044};
Christopher Ferris7e21eba2019-06-20 16:16:42 -070045TYPED_TEST_SUITE_P(DwarfEhFrameTest);
Christopher Ferris61d40972017-06-12 19:14:20 -070046
47// NOTE: All test class variables need to be referenced as this->.
48
Christopher Ferris92acaac2018-06-21 10:44:02 -070049// Only verify different cie/fde format. All other DwarfSection corner
50// cases are tested in DwarfDebugFrameTest.cpp.
51
52TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset32) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -070053 // CIE 32 information.
54 this->memory_.SetData32(0x5000, 0xfc);
Christopher Ferris92acaac2018-06-21 10:44:02 -070055 // Indicates this is a cie for eh_frame.
Christopher Ferrisc9dee842017-11-03 14:50:27 -070056 this->memory_.SetData32(0x5004, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -070057 this->memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 16, 32, 1});
Christopher Ferris61d40972017-06-12 19:14:20 -070058
Christopher Ferrisc9dee842017-11-03 14:50:27 -070059 // 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 Ferris61d40972017-06-12 19:14:20 -070064
Christopher Ferris92acaac2018-06-21 10:44:02 -070065 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 Ferrisc9dee842017-11-03 14:50:27 -070073
Christopher Ferris92acaac2018-06-21 10:44:02 -070074 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 Ferris61d40972017-06-12 19:14:20 -070087}
88
Christopher Ferris92acaac2018-06-21 10:44:02 -070089TYPED_TEST_P(DwarfEhFrameTest, GetFdeCieFromOffset64) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -070090 // CIE 64 information.
91 this->memory_.SetData32(0x5000, 0xffffffff);
Christopher Ferris92acaac2018-06-21 10:44:02 -070092 this->memory_.SetData64(0x5004, 0xfc);
93 // Indicates this is a cie for eh_frame.
Christopher Ferrisc9dee842017-11-03 14:50:27 -070094 this->memory_.SetData64(0x500c, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -070095 this->memory_.SetMemory(0x5014, std::vector<uint8_t>{1, '\0', 16, 32, 1});
Christopher Ferris61d40972017-06-12 19:14:20 -070096
Christopher Ferrisc9dee842017-11-03 14:50:27 -070097 // FDE 64 information.
98 this->memory_.SetData32(0x5100, 0xffffffff);
Christopher Ferris92acaac2018-06-21 10:44:02 -070099 this->memory_.SetData64(0x5104, 0xfc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700100 this->memory_.SetData64(0x510c, 0x10c);
101 this->memory_.SetData64(0x5114, 0x1500);
102 this->memory_.SetData64(0x511c, 0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700103
Christopher Ferris92acaac2018-06-21 10:44:02 -0700104 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x5100);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700105 ASSERT_TRUE(fde != nullptr);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700106 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 Ferris61d40972017-06-12 19:14:20 -0700111 EXPECT_EQ(0U, fde->lsda_address);
112
Christopher Ferris92acaac2018-06-21 10:44:02 -0700113 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 Ferris61d40972017-06-12 19:14:20 -0700126}
127
Christopher Ferris7e21eba2019-06-20 16:16:42 -0700128REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64);
Christopher Ferris61d40972017-06-12 19:14:20 -0700129
130typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes;
Christopher Ferris7e21eba2019-06-20 16:16:42 -0700131INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700132
133} // namespace unwindstack