blob: 910ae363b03c9d5f6ff50bbd844f4eb9018d440f [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};
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);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700124 this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, 'z', 'R', '\0', 0, 0, 0, 0, 0x1b});
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700125
126 // FDE 32 information.
127 this->memory_.SetData32(0x1400, 0xfc);
128 this->memory_.SetData32(0x1404, 0x104);
129 this->memory_.SetData32(0x1408, 0x10f8);
130 this->memory_.SetData32(0x140c, 0x200);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700131 this->memory_.SetData16(0x1410, 0);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700132
133 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0x2000));
134 EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
135 EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
136 EXPECT_EQ(0x1b, this->eh_frame_->TestGetTableEncoding());
137 EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
138 EXPECT_EQ(1U, this->eh_frame_->TestGetFdeCount());
139 EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
140 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
141 EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
142 EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
143 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
144
145 const DwarfFde* fde = this->eh_frame_->GetFdeFromPc(0x4600);
146 ASSERT_TRUE(fde != nullptr);
147 EXPECT_EQ(0x4500U, fde->pc_start);
148 EXPECT_EQ(0x4700U, fde->pc_end);
149}
150
Christopher Ferris92acaac2018-06-21 10:44:02 -0700151TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdes) {
152 this->memory_.SetMemory(
153 0x1000, std::vector<uint8_t>{1, DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_sdata4});
154 this->memory_.SetData16(0x1004, 0x500);
155 this->memory_.SetData32(0x1006, 4);
156
157 // Header information.
158 this->memory_.SetData32(0x100a, 0x4600);
159 this->memory_.SetData32(0x100e, 0x1500);
160 this->memory_.SetData32(0x1012, 0x5500);
161 this->memory_.SetData32(0x1016, 0x1400);
162 this->memory_.SetData32(0x101a, 0x6800);
163 this->memory_.SetData32(0x101e, 0x1700);
164 this->memory_.SetData32(0x1022, 0x7700);
165 this->memory_.SetData32(0x1026, 0x1600);
166
167 // CIE 32 information.
168 this->memory_.SetData32(0x1300, 0xfc);
169 this->memory_.SetData32(0x1304, 0);
170 this->memory_.SetMemory(0x1308, std::vector<uint8_t>{1, '\0', 0, 0, 0});
171
172 // FDE 32 information.
173 // pc 0x5500 - 0x5700
174 this->memory_.SetData32(0x1400, 0xfc);
175 this->memory_.SetData32(0x1404, 0x104);
176 this->memory_.SetData32(0x1408, 0x40f8);
177 this->memory_.SetData32(0x140c, 0x200);
178
179 // pc 0x4600 - 0x4800
180 this->memory_.SetData32(0x1500, 0xfc);
181 this->memory_.SetData32(0x1504, 0x204);
182 this->memory_.SetData32(0x1508, 0x30f8);
183 this->memory_.SetData32(0x150c, 0x200);
184
185 // pc 0x7700 - 0x7900
186 this->memory_.SetData32(0x1600, 0xfc);
187 this->memory_.SetData32(0x1604, 0x304);
188 this->memory_.SetData32(0x1608, 0x60f8);
189 this->memory_.SetData32(0x160c, 0x200);
190
191 // pc 0x6800 - 0x6a00
192 this->memory_.SetData32(0x1700, 0xfc);
193 this->memory_.SetData32(0x1704, 0x404);
194 this->memory_.SetData32(0x1708, 0x50f8);
195 this->memory_.SetData32(0x170c, 0x200);
196
197 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100, 0));
198
199 std::vector<const DwarfFde*> fdes;
200 this->eh_frame_->GetFdes(&fdes);
201 ASSERT_EQ(4U, fdes.size());
202
203 EXPECT_EQ(0x4600U, fdes[0]->pc_start);
204 EXPECT_EQ(0x4800U, fdes[0]->pc_end);
205 EXPECT_EQ(0x5500U, fdes[1]->pc_start);
206 EXPECT_EQ(0x5700U, fdes[1]->pc_end);
207 EXPECT_EQ(0x6800U, fdes[2]->pc_start);
208 EXPECT_EQ(0x6a00U, fdes[2]->pc_end);
209 EXPECT_EQ(0x7700U, fdes[3]->pc_start);
210 EXPECT_EQ(0x7900U, fdes[3]->pc_end);
211}
212
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700213TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_expect_cache_fail) {
214 this->eh_frame_->TestSetTableEntrySize(0x10);
215 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800216 this->eh_frame_->TestSetEntriesOffset(0x1000);
217
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700218 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800219 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
220 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700221 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800222 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->LastErrorCode());
223 EXPECT_EQ(0x1000U, this->eh_frame_->LastErrorAddress());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700224}
225
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700226// We are assuming that pc rel, is really relative to the load_bias.
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700227TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_pcrel) {
228 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_pcrel | DW_EH_PE_udata4);
229 this->eh_frame_->TestSetEntriesOffset(0x1000);
230 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
231 this->eh_frame_->TestSetTableEntrySize(0x10);
232
233 this->memory_.SetData32(0x1040, 0x340);
234 this->memory_.SetData32(0x1044, 0x500);
235
236 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
237 ASSERT_TRUE(info != nullptr);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700238 EXPECT_EQ(0x340U, info->pc);
239 EXPECT_EQ(0x500U, info->offset);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700240}
241
242TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_datarel) {
243 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_datarel | DW_EH_PE_udata4);
244 this->eh_frame_->TestSetEntriesOffset(0x1000);
245 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
246 this->eh_frame_->TestSetTableEntrySize(0x10);
247
248 this->memory_.SetData32(0x1040, 0x340);
249 this->memory_.SetData32(0x1044, 0x500);
250
251 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
252 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800253 EXPECT_EQ(0x3340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700254 EXPECT_EQ(0x3500U, info->offset);
255}
256
257TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_cached) {
258 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
259 this->eh_frame_->TestSetEntriesOffset(0x1000);
260 this->eh_frame_->TestSetTableEntrySize(0x10);
261
262 this->memory_.SetData32(0x1040, 0x340);
263 this->memory_.SetData32(0x1044, 0x500);
264
265 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
266 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800267 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700268 EXPECT_EQ(0x500U, info->offset);
269
270 // Clear the memory so that this will fail if it doesn't read cached data.
271 this->memory_.Clear();
272
273 info = this->eh_frame_->GetFdeInfoFromIndex(2);
274 ASSERT_TRUE(info != nullptr);
Christopher Ferrise37e2d02018-02-09 15:57:39 -0800275 EXPECT_EQ(0x340U, info->pc);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700276 EXPECT_EQ(0x500U, info->offset);
277}
278
279TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetBinary_verify) {
280 this->eh_frame_->TestSetTableEntrySize(0x10);
281 this->eh_frame_->TestSetFdeCount(10);
282
283 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
284 for (size_t i = 0; i < 10; i++) {
285 info.pc = 0x1000 * (i + 1);
286 info.offset = 0x5000 + i * 0x20;
287 this->eh_frame_->TestSetFdeInfo(i, info);
288 }
289
290 uint64_t fde_offset;
291 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetBinary(0x100, &fde_offset, 10));
292 // Not an error, just not found.
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800293 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700294 // Even number of elements.
295 for (size_t i = 0; i < 10; i++) {
296 TypeParam pc = 0x1000 * (i + 1);
297 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 10)) << "Failed at index " << i;
298 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
299 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 10))
300 << "Failed at index " << i;
301 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
302 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 10))
303 << "Failed at index " << i;
304 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
305 }
306 // Odd number of elements.
307 for (size_t i = 0; i < 9; i++) {
308 TypeParam pc = 0x1000 * (i + 1);
309 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 9)) << "Failed at index " << i;
310 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
311 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 9))
312 << "Failed at index " << i;
313 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
314 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 9))
315 << "Failed at index " << i;
316 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
317 }
318}
319
Christopher Ferrisd96cbae2017-11-08 11:01:18 -0800320TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetBinary_index_fail) {
321 this->eh_frame_->TestSetTableEntrySize(0x10);
322 this->eh_frame_->TestSetFdeCount(10);
323
324 uint64_t fde_offset;
325 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetBinary(0x1000, &fde_offset, 10));
326}
327
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700328TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential) {
329 this->eh_frame_->TestSetFdeCount(10);
330 this->eh_frame_->TestSetEntriesDataOffset(0x100);
331 this->eh_frame_->TestSetEntriesEnd(0x2000);
332 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
333
334 this->memory_.SetData32(0x1040, 0x340);
335 this->memory_.SetData32(0x1044, 0x500);
336
337 this->memory_.SetData32(0x1048, 0x440);
338 this->memory_.SetData32(0x104c, 0x600);
339
340 // Verify that if entries is zero, that it fails.
341 uint64_t fde_offset;
342 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
343 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
344
345 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
346 EXPECT_EQ(0x500U, fde_offset);
347
348 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
349 EXPECT_EQ(0x600U, fde_offset);
350
351 // Expect that the data is cached so no more memory reads will occur.
352 this->memory_.Clear();
353 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
354 EXPECT_EQ(0x600U, fde_offset);
355}
356
357TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_last_element) {
358 this->eh_frame_->TestSetFdeCount(2);
359 this->eh_frame_->TestSetEntriesDataOffset(0x100);
360 this->eh_frame_->TestSetEntriesEnd(0x2000);
361 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
362 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
363
364 this->memory_.SetData32(0x1040, 0x340);
365 this->memory_.SetData32(0x1044, 0x500);
366
367 this->memory_.SetData32(0x1048, 0x440);
368 this->memory_.SetData32(0x104c, 0x600);
369
370 uint64_t fde_offset;
371 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
372 EXPECT_EQ(0x600U, fde_offset);
373}
374
375TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_end_check) {
376 this->eh_frame_->TestSetFdeCount(2);
377 this->eh_frame_->TestSetEntriesDataOffset(0x100);
378 this->eh_frame_->TestSetEntriesEnd(0x1048);
379 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
380
381 this->memory_.SetData32(0x1040, 0x340);
382 this->memory_.SetData32(0x1044, 0x500);
383
384 this->memory_.SetData32(0x1048, 0x440);
385 this->memory_.SetData32(0x104c, 0x600);
386
387 uint64_t fde_offset;
388 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800389 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700390}
391
392TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_fail_fde_count) {
393 this->eh_frame_->TestSetFdeCount(0);
394
395 uint64_t fde_offset;
396 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800397 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->LastErrorCode());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700398}
399
400TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_binary_search) {
401 this->eh_frame_->TestSetTableEntrySize(16);
402 this->eh_frame_->TestSetFdeCount(10);
403
404 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
405 info.pc = 0x550;
406 info.offset = 0x10500;
407 this->eh_frame_->TestSetFdeInfo(5, info);
408 info.pc = 0x750;
409 info.offset = 0x10700;
410 this->eh_frame_->TestSetFdeInfo(7, info);
411 info.pc = 0x850;
412 info.offset = 0x10800;
413 this->eh_frame_->TestSetFdeInfo(8, info);
414
415 uint64_t fde_offset;
416 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x800, &fde_offset));
417 EXPECT_EQ(0x10700U, fde_offset);
418}
419
420TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_sequential_search) {
421 this->eh_frame_->TestSetFdeCount(10);
422 this->eh_frame_->TestSetTableEntrySize(0);
423
424 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
425 info.pc = 0x50;
426 info.offset = 0x10000;
427 this->eh_frame_->TestSetFdeInfo(0, info);
428 info.pc = 0x150;
429 info.offset = 0x10100;
430 this->eh_frame_->TestSetFdeInfo(1, info);
431 info.pc = 0x250;
432 info.offset = 0x10200;
433 this->eh_frame_->TestSetFdeInfo(2, info);
434
435 uint64_t fde_offset;
436 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x200, &fde_offset));
437 EXPECT_EQ(0x10100U, fde_offset);
438}
439
440TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde32) {
441 // CIE 32 information.
442 this->memory_.SetData32(0xf000, 0x100);
443 this->memory_.SetData32(0xf004, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700444 this->memory_.SetMemory(0xf008, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700445
446 // FDE 32 information.
447 this->memory_.SetData32(0x14000, 0x20);
448 this->memory_.SetData32(0x14004, 0x5004);
449 this->memory_.SetData32(0x14008, 0x9000);
450 this->memory_.SetData32(0x1400c, 0x100);
451
452 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
453 ASSERT_TRUE(fde != nullptr);
454 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
455 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800456 EXPECT_EQ(0x1d008U, fde->pc_start);
457 EXPECT_EQ(0x1d108U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700458 EXPECT_EQ(0xf000U, fde->cie_offset);
459 EXPECT_EQ(0U, fde->lsda_address);
460
461 ASSERT_TRUE(fde->cie != nullptr);
462 EXPECT_EQ(1U, fde->cie->version);
463 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
464 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
465 EXPECT_EQ(0U, fde->cie->segment_size);
466 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
467 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
468 EXPECT_EQ(0U, fde->cie->personality_handler);
469 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
470 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
471 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
472 EXPECT_EQ(8, fde->cie->data_alignment_factor);
473 EXPECT_EQ(0x20U, fde->cie->return_address_register);
474}
475
476TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde64) {
477 // CIE 64 information.
478 this->memory_.SetData32(0x6000, 0xffffffff);
479 this->memory_.SetData64(0x6004, 0x100);
480 this->memory_.SetData64(0x600c, 0);
Christopher Ferris92acaac2018-06-21 10:44:02 -0700481 this->memory_.SetMemory(0x6014, std::vector<uint8_t>{1, '\0', 4, 8, 0x20});
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700482
483 // FDE 64 information.
484 this->memory_.SetData32(0x8000, 0xffffffff);
485 this->memory_.SetData64(0x8004, 0x200);
486 this->memory_.SetData64(0x800c, 0x200c);
487 this->memory_.SetData64(0x8014, 0x5000);
488 this->memory_.SetData64(0x801c, 0x300);
489
490 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
491 ASSERT_TRUE(fde != nullptr);
492 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
493 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800494 EXPECT_EQ(0xd018U, fde->pc_start);
495 EXPECT_EQ(0xd318U, fde->pc_end);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700496 EXPECT_EQ(0x6000U, fde->cie_offset);
497 EXPECT_EQ(0U, fde->lsda_address);
498
499 ASSERT_TRUE(fde->cie != nullptr);
500 EXPECT_EQ(1U, fde->cie->version);
501 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
502 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
503 EXPECT_EQ(0U, fde->cie->segment_size);
504 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
505 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
506 EXPECT_EQ(0U, fde->cie->personality_handler);
507 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
508 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
509 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
510 EXPECT_EQ(8, fde->cie->data_alignment_factor);
511 EXPECT_EQ(0x20U, fde->cie->return_address_register);
512}
513
514TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeFromPc_fde_not_found) {
515 this->eh_frame_->TestSetTableEntrySize(16);
516 this->eh_frame_->TestSetFdeCount(1);
517
518 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
519 info.pc = 0x550;
520 info.offset = 0x10500;
521 this->eh_frame_->TestSetFdeInfo(0, info);
522
523 ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
524}
525
Christopher Ferris92acaac2018-06-21 10:44:02 -0700526REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias, GetFdes,
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700527 GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
528 GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
529 GetFdeOffsetBinary_verify, GetFdeOffsetBinary_index_fail,
530 GetFdeOffsetSequential, GetFdeOffsetSequential_last_element,
531 GetFdeOffsetSequential_end_check, GetFdeOffsetFromPc_fail_fde_count,
532 GetFdeOffsetFromPc_binary_search, GetFdeOffsetFromPc_sequential_search,
533 GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700534
535typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
536INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
537
538} // namespace unwindstack