blob: 19c7b98a2de45ebcdb204241bf48a9bc085fd69c [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>
33class MockDwarfEhFrameWithHdr : public DwarfEhFrameWithHdr<TypeParam> {
34 public:
35 MockDwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrameWithHdr<TypeParam>(memory) {}
36 ~MockDwarfEhFrameWithHdr() = default;
37
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();
67 eh_frame_ = new MockDwarfEhFrameWithHdr<TypeParam>(&memory_);
68 ResetLogs();
69 }
70
71 void TearDown() override { delete eh_frame_; }
72
73 MemoryFake memory_;
74 MockDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
75};
76TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest);
77
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 Ferris1a141a02018-01-24 08:52:47 -080098 // Verify a zero fde count fails to init.
99 this->memory_.SetData32(0x1006, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700100 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800101 ASSERT_EQ(DWARF_ERROR_NO_FDES, this->eh_frame_->LastErrorCode());
Christopher Ferris1a141a02018-01-24 08:52:47 -0800102
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700103 // Verify an unexpected version will cause a fail.
Christopher Ferris1a141a02018-01-24 08:52:47 -0800104 this->memory_.SetData32(0x1006, 126);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700105 this->memory_.SetData8(0x1000, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700106 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800107 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700108 this->memory_.SetData8(0x1000, 2);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700109 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800110 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700111}
112
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700113TYPED_TEST_P(DwarfEhFrameWithHdrTest, Init_non_zero_load_bias) {
114 this->memory_.SetMemory(0x1000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata4,
115 DW_EH_PE_pcrel | DW_EH_PE_sdata4});
116 this->memory_.SetData16(0x1004, 0x500);
117 this->memory_.SetData32(0x1006, 1);
118 this->memory_.SetData32(0x100a, 0x2500);
119 this->memory_.SetData32(0x100e, 0x1400);
120
121 // CIE 32 information.
122 this->memory_.SetData32(0x1300, 0xfc);
123 this->memory_.SetData32(0x1304, 0);
124 this->memory_.SetData8(0x1308, 1);
125 this->memory_.SetData8(0x1309, 'z');
126 this->memory_.SetData8(0x130a, 'R');
127 this->memory_.SetData8(0x130b, '\0');
128 this->memory_.SetData8(0x130c, 0);
129 this->memory_.SetData8(0x130d, 0);
130 this->memory_.SetData8(0x130e, 0);
131 this->memory_.SetData8(0x130f, 0);
132 this->memory_.SetData8(0x1310, 0x1b);
133
134 // FDE 32 information.
135 this->memory_.SetData32(0x1400, 0xfc);
136 this->memory_.SetData32(0x1404, 0x104);
137 this->memory_.SetData32(0x1408, 0x10f8);
138 this->memory_.SetData32(0x140c, 0x200);
139 this->memory_.SetData8(0x1410, 0);
140 this->memory_.SetData8(0x1411, 0);
141
142 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0x2000));
143 EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
144 EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
145 EXPECT_EQ(0x1b, this->eh_frame_->TestGetTableEncoding());
146 EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
147 EXPECT_EQ(1U, this->eh_frame_->TestGetFdeCount());
148 EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
149 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
150 EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
151 EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
152 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
153
154 const DwarfFde* fde = this->eh_frame_->GetFdeFromPc(0x4600);
155 ASSERT_TRUE(fde != nullptr);
156 EXPECT_EQ(0x4500U, fde->pc_start);
157 EXPECT_EQ(0x4700U, fde->pc_end);
158}
159
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700160TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_expect_cache_fail) {
161 this->eh_frame_->TestSetTableEntrySize(0x10);
162 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800163 this->eh_frame_->TestSetEntriesOffset(0x1000);
164
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700165 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800166 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
167 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700168 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800169 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
170 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700171}
172
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700173// We are assuming that pc rel, is really relative to the load_bias.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700174TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_pcrel) {
175 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_pcrel | DW_EH_PE_udata4);
176 this->eh_frame_->TestSetEntriesOffset(0x1000);
177 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
178 this->eh_frame_->TestSetTableEntrySize(0x10);
179
180 this->memory_.SetData32(0x1040, 0x340);
181 this->memory_.SetData32(0x1044, 0x500);
182
183 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
184 ASSERT_TRUE(info != nullptr);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700185 EXPECT_EQ(0x340U, info->pc);
186 EXPECT_EQ(0x500U, info->offset);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700187}
188
189TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_datarel) {
190 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_datarel | DW_EH_PE_udata4);
191 this->eh_frame_->TestSetEntriesOffset(0x1000);
192 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
193 this->eh_frame_->TestSetTableEntrySize(0x10);
194
195 this->memory_.SetData32(0x1040, 0x340);
196 this->memory_.SetData32(0x1044, 0x500);
197
198 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
199 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800200 EXPECT_EQ(0x3340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700201 EXPECT_EQ(0x3500U, info->offset);
202}
203
204TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_cached) {
205 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
206 this->eh_frame_->TestSetEntriesOffset(0x1000);
207 this->eh_frame_->TestSetTableEntrySize(0x10);
208
209 this->memory_.SetData32(0x1040, 0x340);
210 this->memory_.SetData32(0x1044, 0x500);
211
212 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
213 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800214 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700215 EXPECT_EQ(0x500U, info->offset);
216
217 // Clear the memory so that this will fail if it doesn't read cached data.
218 this->memory_.Clear();
219
220 info = this->eh_frame_->GetFdeInfoFromIndex(2);
221 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800222 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700223 EXPECT_EQ(0x500U, info->offset);
224}
225
226TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetBinary_verify) {
227 this->eh_frame_->TestSetTableEntrySize(0x10);
228 this->eh_frame_->TestSetFdeCount(10);
229
230 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
231 for (size_t i = 0; i < 10; i++) {
232 info.pc = 0x1000 * (i + 1);
233 info.offset = 0x5000 + i * 0x20;
234 this->eh_frame_->TestSetFdeInfo(i, info);
235 }
236
237 uint64_t fde_offset;
238 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetBinary(0x100, &fde_offset, 10));
239 // Not an error, just not found.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800240 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700241 // Even number of elements.
242 for (size_t i = 0; i < 10; i++) {
243 TypeParam pc = 0x1000 * (i + 1);
244 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 10)) << "Failed at index " << i;
245 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
246 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 10))
247 << "Failed at index " << i;
248 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
249 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 10))
250 << "Failed at index " << i;
251 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
252 }
253 // Odd number of elements.
254 for (size_t i = 0; i < 9; i++) {
255 TypeParam pc = 0x1000 * (i + 1);
256 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 9)) << "Failed at index " << i;
257 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
258 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 9))
259 << "Failed at index " << i;
260 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
261 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 9))
262 << "Failed at index " << i;
263 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
264 }
265}
266
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800267TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetBinary_index_fail) {
268 this->eh_frame_->TestSetTableEntrySize(0x10);
269 this->eh_frame_->TestSetFdeCount(10);
270
271 uint64_t fde_offset;
272 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetBinary(0x1000, &fde_offset, 10));
273}
274
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700275TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential) {
276 this->eh_frame_->TestSetFdeCount(10);
277 this->eh_frame_->TestSetEntriesDataOffset(0x100);
278 this->eh_frame_->TestSetEntriesEnd(0x2000);
279 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
280
281 this->memory_.SetData32(0x1040, 0x340);
282 this->memory_.SetData32(0x1044, 0x500);
283
284 this->memory_.SetData32(0x1048, 0x440);
285 this->memory_.SetData32(0x104c, 0x600);
286
287 // Verify that if entries is zero, that it fails.
288 uint64_t fde_offset;
289 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
290 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
291
292 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
293 EXPECT_EQ(0x500U, fde_offset);
294
295 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
296 EXPECT_EQ(0x600U, fde_offset);
297
298 // Expect that the data is cached so no more memory reads will occur.
299 this->memory_.Clear();
300 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
301 EXPECT_EQ(0x600U, fde_offset);
302}
303
304TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_last_element) {
305 this->eh_frame_->TestSetFdeCount(2);
306 this->eh_frame_->TestSetEntriesDataOffset(0x100);
307 this->eh_frame_->TestSetEntriesEnd(0x2000);
308 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
309 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
310
311 this->memory_.SetData32(0x1040, 0x340);
312 this->memory_.SetData32(0x1044, 0x500);
313
314 this->memory_.SetData32(0x1048, 0x440);
315 this->memory_.SetData32(0x104c, 0x600);
316
317 uint64_t fde_offset;
318 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
319 EXPECT_EQ(0x600U, fde_offset);
320}
321
322TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_end_check) {
323 this->eh_frame_->TestSetFdeCount(2);
324 this->eh_frame_->TestSetEntriesDataOffset(0x100);
325 this->eh_frame_->TestSetEntriesEnd(0x1048);
326 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
327
328 this->memory_.SetData32(0x1040, 0x340);
329 this->memory_.SetData32(0x1044, 0x500);
330
331 this->memory_.SetData32(0x1048, 0x440);
332 this->memory_.SetData32(0x104c, 0x600);
333
334 uint64_t fde_offset;
335 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800336 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700337}
338
339TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_fail_fde_count) {
340 this->eh_frame_->TestSetFdeCount(0);
341
342 uint64_t fde_offset;
343 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800344 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700345}
346
347TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_binary_search) {
348 this->eh_frame_->TestSetTableEntrySize(16);
349 this->eh_frame_->TestSetFdeCount(10);
350
351 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
352 info.pc = 0x550;
353 info.offset = 0x10500;
354 this->eh_frame_->TestSetFdeInfo(5, info);
355 info.pc = 0x750;
356 info.offset = 0x10700;
357 this->eh_frame_->TestSetFdeInfo(7, info);
358 info.pc = 0x850;
359 info.offset = 0x10800;
360 this->eh_frame_->TestSetFdeInfo(8, info);
361
362 uint64_t fde_offset;
363 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x800, &fde_offset));
364 EXPECT_EQ(0x10700U, fde_offset);
365}
366
367TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_sequential_search) {
368 this->eh_frame_->TestSetFdeCount(10);
369 this->eh_frame_->TestSetTableEntrySize(0);
370
371 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
372 info.pc = 0x50;
373 info.offset = 0x10000;
374 this->eh_frame_->TestSetFdeInfo(0, info);
375 info.pc = 0x150;
376 info.offset = 0x10100;
377 this->eh_frame_->TestSetFdeInfo(1, info);
378 info.pc = 0x250;
379 info.offset = 0x10200;
380 this->eh_frame_->TestSetFdeInfo(2, info);
381
382 uint64_t fde_offset;
383 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x200, &fde_offset));
384 EXPECT_EQ(0x10100U, fde_offset);
385}
386
387TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde32) {
388 // CIE 32 information.
389 this->memory_.SetData32(0xf000, 0x100);
390 this->memory_.SetData32(0xf004, 0);
391 this->memory_.SetData8(0xf008, 0x1);
392 this->memory_.SetData8(0xf009, '\0');
393 this->memory_.SetData8(0xf00a, 4);
394 this->memory_.SetData8(0xf00b, 8);
395 this->memory_.SetData8(0xf00c, 0x20);
396
397 // FDE 32 information.
398 this->memory_.SetData32(0x14000, 0x20);
399 this->memory_.SetData32(0x14004, 0x5004);
400 this->memory_.SetData32(0x14008, 0x9000);
401 this->memory_.SetData32(0x1400c, 0x100);
402
403 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
404 ASSERT_TRUE(fde != nullptr);
405 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
406 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800407 EXPECT_EQ(0x1d008U, fde->pc_start);
408 EXPECT_EQ(0x1d108U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700409 EXPECT_EQ(0xf000U, fde->cie_offset);
410 EXPECT_EQ(0U, fde->lsda_address);
411
412 ASSERT_TRUE(fde->cie != nullptr);
413 EXPECT_EQ(1U, fde->cie->version);
414 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
415 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
416 EXPECT_EQ(0U, fde->cie->segment_size);
417 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
418 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
419 EXPECT_EQ(0U, fde->cie->personality_handler);
420 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
421 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
422 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
423 EXPECT_EQ(8, fde->cie->data_alignment_factor);
424 EXPECT_EQ(0x20U, fde->cie->return_address_register);
425}
426
427TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde64) {
428 // CIE 64 information.
429 this->memory_.SetData32(0x6000, 0xffffffff);
430 this->memory_.SetData64(0x6004, 0x100);
431 this->memory_.SetData64(0x600c, 0);
432 this->memory_.SetData8(0x6014, 0x1);
433 this->memory_.SetData8(0x6015, '\0');
434 this->memory_.SetData8(0x6016, 4);
435 this->memory_.SetData8(0x6017, 8);
436 this->memory_.SetData8(0x6018, 0x20);
437
438 // FDE 64 information.
439 this->memory_.SetData32(0x8000, 0xffffffff);
440 this->memory_.SetData64(0x8004, 0x200);
441 this->memory_.SetData64(0x800c, 0x200c);
442 this->memory_.SetData64(0x8014, 0x5000);
443 this->memory_.SetData64(0x801c, 0x300);
444
445 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
446 ASSERT_TRUE(fde != nullptr);
447 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
448 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800449 EXPECT_EQ(0xd018U, fde->pc_start);
450 EXPECT_EQ(0xd318U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700451 EXPECT_EQ(0x6000U, fde->cie_offset);
452 EXPECT_EQ(0U, fde->lsda_address);
453
454 ASSERT_TRUE(fde->cie != nullptr);
455 EXPECT_EQ(1U, fde->cie->version);
456 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
457 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
458 EXPECT_EQ(0U, fde->cie->segment_size);
459 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
460 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
461 EXPECT_EQ(0U, fde->cie->personality_handler);
462 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
463 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
464 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
465 EXPECT_EQ(8, fde->cie->data_alignment_factor);
466 EXPECT_EQ(0x20U, fde->cie->return_address_register);
467}
468
469TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeFromPc_fde_not_found) {
470 this->eh_frame_->TestSetTableEntrySize(16);
471 this->eh_frame_->TestSetFdeCount(1);
472
473 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
474 info.pc = 0x550;
475 info.offset = 0x10500;
476 this->eh_frame_->TestSetFdeInfo(0, info);
477
478 ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
479}
480
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700481REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias,
482 GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
483 GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
484 GetFdeOffsetBinary_verify, GetFdeOffsetBinary_index_fail,
485 GetFdeOffsetSequential, GetFdeOffsetSequential_last_element,
486 GetFdeOffsetSequential_end_check, GetFdeOffsetFromPc_fail_fde_count,
487 GetFdeOffsetFromPc_binary_search, GetFdeOffsetFromPc_sequential_search,
488 GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700489
490typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
491INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
492
493} // namespace unwindstack