blob: 78608e3eec07483d6e5f60d400825abd638efe4f [file] [log] [blame]
Christopher Ferrisc9dee842017-11-03 14:50:27 -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
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080022#include <unwindstack/DwarfError.h>
23
Christopher Ferrisc9dee842017-11-03 14:50:27 -070024#include "DwarfEhFrameWithHdr.h"
25#include "DwarfEncoding.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070026
27#include "LogFake.h"
28#include "MemoryFake.h"
29
30namespace unwindstack {
31
32template <typename TypeParam>
Christopher Ferris92acaac2018-06-21 10:44:02 -070033class TestDwarfEhFrameWithHdr : public DwarfEhFrameWithHdr<TypeParam> {
Christopher Ferrisc9dee842017-11-03 14:50:27 -070034 public:
Christopher Ferris92acaac2018-06-21 10:44:02 -070035 TestDwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrameWithHdr<TypeParam>(memory) {}
36 ~TestDwarfEhFrameWithHdr() = default;
Christopher Ferrisc9dee842017-11-03 14:50:27 -070037
38 void TestSetTableEncoding(uint8_t encoding) { this->table_encoding_ = encoding; }
39 void TestSetEntriesOffset(uint64_t offset) { this->entries_offset_ = offset; }
40 void TestSetEntriesEnd(uint64_t end) { this->entries_end_ = end; }
41 void TestSetEntriesDataOffset(uint64_t offset) { this->entries_data_offset_ = offset; }
42 void TestSetCurEntriesOffset(uint64_t offset) { this->cur_entries_offset_ = offset; }
43 void TestSetTableEntrySize(size_t size) { this->table_entry_size_ = size; }
44
45 void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
46 void TestSetFdeInfo(uint64_t index, const typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo& info) {
47 this->fde_info_[index] = info;
48 }
49
50 uint8_t TestGetVersion() { return this->version_; }
51 uint8_t TestGetPtrEncoding() { return this->ptr_encoding_; }
52 uint64_t TestGetPtrOffset() { return this->ptr_offset_; }
53 uint8_t TestGetTableEncoding() { return this->table_encoding_; }
54 uint64_t TestGetTableEntrySize() { return this->table_entry_size_; }
55 uint64_t TestGetFdeCount() { return this->fde_count_; }
56 uint64_t TestGetEntriesOffset() { return this->entries_offset_; }
57 uint64_t TestGetEntriesEnd() { return this->entries_end_; }
58 uint64_t TestGetEntriesDataOffset() { return this->entries_data_offset_; }
59 uint64_t TestGetCurEntriesOffset() { return this->cur_entries_offset_; }
60};
61
62template <typename TypeParam>
63class DwarfEhFrameWithHdrTest : public ::testing::Test {
64 protected:
65 void SetUp() override {
66 memory_.Clear();
Christopher Ferris92acaac2018-06-21 10:44:02 -070067 eh_frame_ = new TestDwarfEhFrameWithHdr<TypeParam>(&memory_);
Christopher Ferrisc9dee842017-11-03 14:50:27 -070068 ResetLogs();
69 }
70
71 void TearDown() override { delete eh_frame_; }
72
73 MemoryFake memory_;
Christopher Ferris92acaac2018-06-21 10:44:02 -070074 TestDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
Christopher Ferrisc9dee842017-11-03 14:50:27 -070075};
Christopher Ferris7e21eba2019-06-20 16:16:42 -070076TYPED_TEST_SUITE_P(DwarfEhFrameWithHdrTest);
Christopher Ferrisc9dee842017-11-03 14:50:27 -070077
78// NOTE: All test class variables need to be referenced as this->.
79
80TYPED_TEST_P(DwarfEhFrameWithHdrTest, Init) {
81 this->memory_.SetMemory(
82 0x1000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_sdata4});
83 this->memory_.SetData16(0x1004, 0x500);
84 this->memory_.SetData32(0x1006, 126);
85
Christopher Ferris4cc36d22018-06-06 14:47:31 -070086 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferrisc9dee842017-11-03 14:50:27 -070087 EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
88 EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
89 EXPECT_EQ(DW_EH_PE_sdata4, this->eh_frame_->TestGetTableEncoding());
90 EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
91 EXPECT_EQ(126U, this->eh_frame_->TestGetFdeCount());
92 EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
93 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
94 EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
95 EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
96 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
97
Christopher Ferris22d8e8e2019-03-29 12:34:58 -070098 // Verify a zero table entry size fails to init.
99 this->memory_.SetData8(0x1003, 0x1);
100 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
101 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->eh_frame_->LastErrorCode());
102 // Reset the value back to the original.
103 this->memory_.SetData8(0x1003, DW_EH_PE_sdata4);
104
Christopher Ferris1a141a02018-01-24 08:52:47 -0800105 // Verify a zero fde count fails to init.
106 this->memory_.SetData32(0x1006, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700107 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800108 ASSERT_EQ(DWARF_ERROR_NO_FDES, this->eh_frame_->LastErrorCode());
Christopher Ferris1a141a02018-01-24 08:52:47 -0800109
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700110 // Verify an unexpected version will cause a fail.
Christopher Ferris1a141a02018-01-24 08:52:47 -0800111 this->memory_.SetData32(0x1006, 126);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700112 this->memory_.SetData8(0x1000, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700113 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800114 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700115 this->memory_.SetData8(0x1000, 2);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700116 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800117 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700118}
119
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700120TYPED_TEST_P(DwarfEhFrameWithHdrTest, Init_non_zero_load_bias) {
121 this->memory_.SetMemory(0x1000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata4,
122 DW_EH_PE_pcrel | DW_EH_PE_sdata4});
123 this->memory_.SetData16(0x1004, 0x500);
124 this->memory_.SetData32(0x1006, 1);
125 this->memory_.SetData32(0x100a, 0x2500);
126 this->memory_.SetData32(0x100e, 0x1400);
127
128 // CIE 32 information.
129 this->memory_.SetData32(0x1300, 0xfc);
130 this->memory_.SetData32(0x1304, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700131 this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, 'z', 'R', '\0', 0, 0, 0, 0, 0x1b});
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700132
133 // FDE 32 information.
134 this->memory_.SetData32(0x1400, 0xfc);
135 this->memory_.SetData32(0x1404, 0x104);
136 this->memory_.SetData32(0x1408, 0x10f8);
137 this->memory_.SetData32(0x140c, 0x200);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700138 this->memory_.SetData16(0x1410, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700139
140 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0x2000));
141 EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
142 EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
143 EXPECT_EQ(0x1b, this->eh_frame_->TestGetTableEncoding());
144 EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
145 EXPECT_EQ(1U, this->eh_frame_->TestGetFdeCount());
146 EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
147 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
148 EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
149 EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
150 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
151
152 const DwarfFde* fde = this->eh_frame_->GetFdeFromPc(0x4600);
153 ASSERT_TRUE(fde != nullptr);
154 EXPECT_EQ(0x4500U, fde->pc_start);
155 EXPECT_EQ(0x4700U, fde->pc_end);
156}
157
Christopher Ferris92acaac2018-06-21 10:44:02 -0700158TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdes) {
159 this->memory_.SetMemory(
160 0x1000, std::vector<uint8_t>{1, DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_sdata4});
161 this->memory_.SetData16(0x1004, 0x500);
162 this->memory_.SetData32(0x1006, 4);
163
164 // Header information.
165 this->memory_.SetData32(0x100a, 0x4600);
166 this->memory_.SetData32(0x100e, 0x1500);
167 this->memory_.SetData32(0x1012, 0x5500);
168 this->memory_.SetData32(0x1016, 0x1400);
169 this->memory_.SetData32(0x101a, 0x6800);
170 this->memory_.SetData32(0x101e, 0x1700);
171 this->memory_.SetData32(0x1022, 0x7700);
172 this->memory_.SetData32(0x1026, 0x1600);
173
174 // CIE 32 information.
175 this->memory_.SetData32(0x1300, 0xfc);
176 this->memory_.SetData32(0x1304, 0);
177 this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, '\0', 0, 0, 0});
178
179 // FDE 32 information.
180 // pc 0x5500 - 0x5700
181 this->memory_.SetData32(0x1400, 0xfc);
182 this->memory_.SetData32(0x1404, 0x104);
183 this->memory_.SetData32(0x1408, 0x40f8);
184 this->memory_.SetData32(0x140c, 0x200);
185
186 // pc 0x4600 - 0x4800
187 this->memory_.SetData32(0x1500, 0xfc);
188 this->memory_.SetData32(0x1504, 0x204);
189 this->memory_.SetData32(0x1508, 0x30f8);
190 this->memory_.SetData32(0x150c, 0x200);
191
192 // pc 0x7700 - 0x7900
193 this->memory_.SetData32(0x1600, 0xfc);
194 this->memory_.SetData32(0x1604, 0x304);
195 this->memory_.SetData32(0x1608, 0x60f8);
196 this->memory_.SetData32(0x160c, 0x200);
197
198 // pc 0x6800 - 0x6a00
199 this->memory_.SetData32(0x1700, 0xfc);
200 this->memory_.SetData32(0x1704, 0x404);
201 this->memory_.SetData32(0x1708, 0x50f8);
202 this->memory_.SetData32(0x170c, 0x200);
203
204 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0));
205
206 std::vector<const DwarfFde*> fdes;
207 this->eh_frame_->GetFdes(&fdes);
208 ASSERT_EQ(4U, fdes.size());
209
210 EXPECT_EQ(0x4600U, fdes[0]->pc_start);
211 EXPECT_EQ(0x4800U, fdes[0]->pc_end);
212 EXPECT_EQ(0x5500U, fdes[1]->pc_start);
213 EXPECT_EQ(0x5700U, fdes[1]->pc_end);
214 EXPECT_EQ(0x6800U, fdes[2]->pc_start);
215 EXPECT_EQ(0x6a00U, fdes[2]->pc_end);
216 EXPECT_EQ(0x7700U, fdes[3]->pc_start);
217 EXPECT_EQ(0x7900U, fdes[3]->pc_end);
218}
219
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700220TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_expect_cache_fail) {
221 this->eh_frame_->TestSetTableEntrySize(0x10);
222 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800223 this->eh_frame_->TestSetEntriesOffset(0x1000);
224
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700225 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800226 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
227 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700228 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800229 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
230 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700231}
232
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700233// We are assuming that pc rel, is really relative to the load_bias.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700234TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_pcrel) {
235 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_pcrel | DW_EH_PE_udata4);
236 this->eh_frame_->TestSetEntriesOffset(0x1000);
237 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
238 this->eh_frame_->TestSetTableEntrySize(0x10);
239
240 this->memory_.SetData32(0x1040, 0x340);
241 this->memory_.SetData32(0x1044, 0x500);
242
243 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
244 ASSERT_TRUE(info != nullptr);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700245 EXPECT_EQ(0x340U, info->pc);
246 EXPECT_EQ(0x500U, info->offset);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700247}
248
249TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_datarel) {
250 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_datarel | DW_EH_PE_udata4);
251 this->eh_frame_->TestSetEntriesOffset(0x1000);
252 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
253 this->eh_frame_->TestSetTableEntrySize(0x10);
254
255 this->memory_.SetData32(0x1040, 0x340);
256 this->memory_.SetData32(0x1044, 0x500);
257
258 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
259 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800260 EXPECT_EQ(0x3340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700261 EXPECT_EQ(0x3500U, info->offset);
262}
263
264TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_cached) {
265 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
266 this->eh_frame_->TestSetEntriesOffset(0x1000);
267 this->eh_frame_->TestSetTableEntrySize(0x10);
268
269 this->memory_.SetData32(0x1040, 0x340);
270 this->memory_.SetData32(0x1044, 0x500);
271
272 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
273 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800274 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700275 EXPECT_EQ(0x500U, info->offset);
276
277 // Clear the memory so that this will fail if it doesn't read cached data.
278 this->memory_.Clear();
279
280 info = this->eh_frame_->GetFdeInfoFromIndex(2);
281 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800282 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700283 EXPECT_EQ(0x500U, info->offset);
284}
285
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700286TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_verify) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700287 this->eh_frame_->TestSetTableEntrySize(0x10);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700288
289 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
290 for (size_t i = 0; i < 10; i++) {
291 info.pc = 0x1000 * (i + 1);
292 info.offset = 0x5000 + i * 0x20;
293 this->eh_frame_->TestSetFdeInfo(i, info);
294 }
295
296 uint64_t fde_offset;
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700297 this->eh_frame_->TestSetFdeCount(10);
298 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700299 // Not an error, just not found.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800300 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700301 // Even number of elements.
302 for (size_t i = 0; i < 10; i++) {
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700303 SCOPED_TRACE(testing::Message() << "Failed at index " << i);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700304 TypeParam pc = 0x1000 * (i + 1);
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700305 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset));
306 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
307 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset));
308 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
309 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset));
310 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700311 }
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700312
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700313 // Odd number of elements.
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700314 this->eh_frame_->TestSetFdeCount(9);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700315 for (size_t i = 0; i < 9; i++) {
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700316 SCOPED_TRACE(testing::Message() << "Failed at index " << i);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700317 TypeParam pc = 0x1000 * (i + 1);
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700318 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc, &fde_offset));
319 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
320 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset));
321 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
322 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset));
323 EXPECT_EQ(0x5000 + i * 0x20, fde_offset);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700324 }
325}
326
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700327TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_index_fail) {
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800328 this->eh_frame_->TestSetTableEntrySize(0x10);
329 this->eh_frame_->TestSetFdeCount(10);
330
331 uint64_t fde_offset;
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700332 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700333}
334
335TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_fail_fde_count) {
336 this->eh_frame_->TestSetFdeCount(0);
337
338 uint64_t fde_offset;
339 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800340 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700341}
342
Christopher Ferris22d8e8e2019-03-29 12:34:58 -0700343TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_search) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700344 this->eh_frame_->TestSetTableEntrySize(16);
345 this->eh_frame_->TestSetFdeCount(10);
346
347 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
348 info.pc = 0x550;
349 info.offset = 0x10500;
350 this->eh_frame_->TestSetFdeInfo(5, info);
351 info.pc = 0x750;
352 info.offset = 0x10700;
353 this->eh_frame_->TestSetFdeInfo(7, info);
354 info.pc = 0x850;
355 info.offset = 0x10800;
356 this->eh_frame_->TestSetFdeInfo(8, info);
357
358 uint64_t fde_offset;
359 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x800, &fde_offset));
360 EXPECT_EQ(0x10700U, fde_offset);
361}
362
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700363TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde32) {
364 // CIE 32 information.
365 this->memory_.SetData32(0xf000, 0x100);
366 this->memory_.SetData32(0xf004, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700367 this->memory_.SetMemory(0xf008, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700368
369 // FDE 32 information.
370 this->memory_.SetData32(0x14000, 0x20);
371 this->memory_.SetData32(0x14004, 0x5004);
372 this->memory_.SetData32(0x14008, 0x9000);
373 this->memory_.SetData32(0x1400c, 0x100);
374
375 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
376 ASSERT_TRUE(fde != nullptr);
377 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
378 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800379 EXPECT_EQ(0x1d008U, fde->pc_start);
380 EXPECT_EQ(0x1d108U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700381 EXPECT_EQ(0xf000U, fde->cie_offset);
382 EXPECT_EQ(0U, fde->lsda_address);
383
384 ASSERT_TRUE(fde->cie != nullptr);
385 EXPECT_EQ(1U, fde->cie->version);
386 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
387 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
388 EXPECT_EQ(0U, fde->cie->segment_size);
389 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
390 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
391 EXPECT_EQ(0U, fde->cie->personality_handler);
392 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
393 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
394 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
395 EXPECT_EQ(8, fde->cie->data_alignment_factor);
396 EXPECT_EQ(0x20U, fde->cie->return_address_register);
397}
398
399TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde64) {
400 // CIE 64 information.
401 this->memory_.SetData32(0x6000, 0xffffffff);
402 this->memory_.SetData64(0x6004, 0x100);
403 this->memory_.SetData64(0x600c, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700404 this->memory_.SetMemory(0x6014, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700405
406 // FDE 64 information.
407 this->memory_.SetData32(0x8000, 0xffffffff);
408 this->memory_.SetData64(0x8004, 0x200);
409 this->memory_.SetData64(0x800c, 0x200c);
410 this->memory_.SetData64(0x8014, 0x5000);
411 this->memory_.SetData64(0x801c, 0x300);
412
413 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
414 ASSERT_TRUE(fde != nullptr);
415 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
416 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800417 EXPECT_EQ(0xd018U, fde->pc_start);
418 EXPECT_EQ(0xd318U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700419 EXPECT_EQ(0x6000U, fde->cie_offset);
420 EXPECT_EQ(0U, fde->lsda_address);
421
422 ASSERT_TRUE(fde->cie != nullptr);
423 EXPECT_EQ(1U, fde->cie->version);
424 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
425 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
426 EXPECT_EQ(0U, fde->cie->segment_size);
427 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
428 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
429 EXPECT_EQ(0U, fde->cie->personality_handler);
430 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
431 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
432 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
433 EXPECT_EQ(8, fde->cie->data_alignment_factor);
434 EXPECT_EQ(0x20U, fde->cie->return_address_register);
435}
436
437TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeFromPc_fde_not_found) {
438 this->eh_frame_->TestSetTableEntrySize(16);
439 this->eh_frame_->TestSetFdeCount(1);
440
441 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
442 info.pc = 0x550;
443 info.offset = 0x10500;
444 this->eh_frame_->TestSetFdeInfo(0, info);
445
446 ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
447}
448
Christopher Ferris7e21eba2019-06-20 16:16:42 -0700449REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias, GetFdes,
450 GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
451 GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
452 GetFdeOffsetFromPc_verify, GetFdeOffsetFromPc_index_fail,
453 GetFdeOffsetFromPc_fail_fde_count, GetFdeOffsetFromPc_search,
454 GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700455
456typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
Christopher Ferris7e21eba2019-06-20 16:16:42 -0700457INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700458
459} // namespace unwindstack