blob: 7c8fc6cc4b5747453f50b2694dcc6819aa840902 [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
22#include "DwarfEhFrameWithHdr.h"
23#include "DwarfEncoding.h"
24#include "DwarfError.h"
25
26#include "LogFake.h"
27#include "MemoryFake.h"
28
29namespace unwindstack {
30
31template <typename TypeParam>
32class MockDwarfEhFrameWithHdr : public DwarfEhFrameWithHdr<TypeParam> {
33 public:
34 MockDwarfEhFrameWithHdr(Memory* memory) : DwarfEhFrameWithHdr<TypeParam>(memory) {}
35 ~MockDwarfEhFrameWithHdr() = default;
36
37 void TestSetTableEncoding(uint8_t encoding) { this->table_encoding_ = encoding; }
38 void TestSetEntriesOffset(uint64_t offset) { this->entries_offset_ = offset; }
39 void TestSetEntriesEnd(uint64_t end) { this->entries_end_ = end; }
40 void TestSetEntriesDataOffset(uint64_t offset) { this->entries_data_offset_ = offset; }
41 void TestSetCurEntriesOffset(uint64_t offset) { this->cur_entries_offset_ = offset; }
42 void TestSetTableEntrySize(size_t size) { this->table_entry_size_ = size; }
43
44 void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
45 void TestSetFdeInfo(uint64_t index, const typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo& info) {
46 this->fde_info_[index] = info;
47 }
48
49 uint8_t TestGetVersion() { return this->version_; }
50 uint8_t TestGetPtrEncoding() { return this->ptr_encoding_; }
51 uint64_t TestGetPtrOffset() { return this->ptr_offset_; }
52 uint8_t TestGetTableEncoding() { return this->table_encoding_; }
53 uint64_t TestGetTableEntrySize() { return this->table_entry_size_; }
54 uint64_t TestGetFdeCount() { return this->fde_count_; }
55 uint64_t TestGetEntriesOffset() { return this->entries_offset_; }
56 uint64_t TestGetEntriesEnd() { return this->entries_end_; }
57 uint64_t TestGetEntriesDataOffset() { return this->entries_data_offset_; }
58 uint64_t TestGetCurEntriesOffset() { return this->cur_entries_offset_; }
59};
60
61template <typename TypeParam>
62class DwarfEhFrameWithHdrTest : public ::testing::Test {
63 protected:
64 void SetUp() override {
65 memory_.Clear();
66 eh_frame_ = new MockDwarfEhFrameWithHdr<TypeParam>(&memory_);
67 ResetLogs();
68 }
69
70 void TearDown() override { delete eh_frame_; }
71
72 MemoryFake memory_;
73 MockDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
74};
75TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest);
76
77// NOTE: All test class variables need to be referenced as this->.
78
79TYPED_TEST_P(DwarfEhFrameWithHdrTest, Init) {
80 this->memory_.SetMemory(
81 0x1000, std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_sdata4});
82 this->memory_.SetData16(0x1004, 0x500);
83 this->memory_.SetData32(0x1006, 126);
84
85 ASSERT_TRUE(this->eh_frame_->Init(0x1000, 0x100));
86 EXPECT_EQ(1U, this->eh_frame_->TestGetVersion());
87 EXPECT_EQ(DW_EH_PE_udata2, this->eh_frame_->TestGetPtrEncoding());
88 EXPECT_EQ(DW_EH_PE_sdata4, this->eh_frame_->TestGetTableEncoding());
89 EXPECT_EQ(4U, this->eh_frame_->TestGetTableEntrySize());
90 EXPECT_EQ(126U, this->eh_frame_->TestGetFdeCount());
91 EXPECT_EQ(0x500U, this->eh_frame_->TestGetPtrOffset());
92 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetEntriesOffset());
93 EXPECT_EQ(0x1100U, this->eh_frame_->TestGetEntriesEnd());
94 EXPECT_EQ(0x1000U, this->eh_frame_->TestGetEntriesDataOffset());
95 EXPECT_EQ(0x100aU, this->eh_frame_->TestGetCurEntriesOffset());
96
97 // Verify an unexpected version will cause a fail.
98 this->memory_.SetData8(0x1000, 0);
99 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100));
100 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->last_error());
101 this->memory_.SetData8(0x1000, 2);
102 ASSERT_FALSE(this->eh_frame_->Init(0x1000, 0x100));
103 ASSERT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->eh_frame_->last_error());
104}
105
106TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_expect_cache_fail) {
107 this->eh_frame_->TestSetTableEntrySize(0x10);
108 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
109 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
110 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->last_error());
111 ASSERT_TRUE(this->eh_frame_->GetFdeInfoFromIndex(0) == nullptr);
112 ASSERT_EQ(DWARF_ERROR_MEMORY_INVALID, this->eh_frame_->last_error());
113}
114
115TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_pcrel) {
116 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_pcrel | DW_EH_PE_udata4);
117 this->eh_frame_->TestSetEntriesOffset(0x1000);
118 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
119 this->eh_frame_->TestSetTableEntrySize(0x10);
120
121 this->memory_.SetData32(0x1040, 0x340);
122 this->memory_.SetData32(0x1044, 0x500);
123
124 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
125 ASSERT_TRUE(info != nullptr);
126 EXPECT_EQ(0x1384U, info->pc);
127 EXPECT_EQ(0x1540U, info->offset);
128}
129
130TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_read_datarel) {
131 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_datarel | DW_EH_PE_udata4);
132 this->eh_frame_->TestSetEntriesOffset(0x1000);
133 this->eh_frame_->TestSetEntriesDataOffset(0x3000);
134 this->eh_frame_->TestSetTableEntrySize(0x10);
135
136 this->memory_.SetData32(0x1040, 0x340);
137 this->memory_.SetData32(0x1044, 0x500);
138
139 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
140 ASSERT_TRUE(info != nullptr);
141 EXPECT_EQ(0x3344U, info->pc);
142 EXPECT_EQ(0x3500U, info->offset);
143}
144
145TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeInfoFromIndex_cached) {
146 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
147 this->eh_frame_->TestSetEntriesOffset(0x1000);
148 this->eh_frame_->TestSetTableEntrySize(0x10);
149
150 this->memory_.SetData32(0x1040, 0x340);
151 this->memory_.SetData32(0x1044, 0x500);
152
153 auto info = this->eh_frame_->GetFdeInfoFromIndex(2);
154 ASSERT_TRUE(info != nullptr);
155 EXPECT_EQ(0x344U, info->pc);
156 EXPECT_EQ(0x500U, info->offset);
157
158 // Clear the memory so that this will fail if it doesn't read cached data.
159 this->memory_.Clear();
160
161 info = this->eh_frame_->GetFdeInfoFromIndex(2);
162 ASSERT_TRUE(info != nullptr);
163 EXPECT_EQ(0x344U, info->pc);
164 EXPECT_EQ(0x500U, info->offset);
165}
166
167TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetBinary_verify) {
168 this->eh_frame_->TestSetTableEntrySize(0x10);
169 this->eh_frame_->TestSetFdeCount(10);
170
171 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
172 for (size_t i = 0; i < 10; i++) {
173 info.pc = 0x1000 * (i + 1);
174 info.offset = 0x5000 + i * 0x20;
175 this->eh_frame_->TestSetFdeInfo(i, info);
176 }
177
178 uint64_t fde_offset;
179 EXPECT_FALSE(this->eh_frame_->GetFdeOffsetBinary(0x100, &fde_offset, 10));
180 // Not an error, just not found.
181 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error());
182 // Even number of elements.
183 for (size_t i = 0; i < 10; i++) {
184 TypeParam pc = 0x1000 * (i + 1);
185 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 10)) << "Failed at index " << i;
186 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
187 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 10))
188 << "Failed at index " << i;
189 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
190 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 10))
191 << "Failed at index " << i;
192 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
193 }
194 // Odd number of elements.
195 for (size_t i = 0; i < 9; i++) {
196 TypeParam pc = 0x1000 * (i + 1);
197 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc, &fde_offset, 9)) << "Failed at index " << i;
198 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
199 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 1, &fde_offset, 9))
200 << "Failed at index " << i;
201 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
202 EXPECT_TRUE(this->eh_frame_->GetFdeOffsetBinary(pc + 0xfff, &fde_offset, 9))
203 << "Failed at index " << i;
204 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
205 }
206}
207
208TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential) {
209 this->eh_frame_->TestSetFdeCount(10);
210 this->eh_frame_->TestSetEntriesDataOffset(0x100);
211 this->eh_frame_->TestSetEntriesEnd(0x2000);
212 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
213
214 this->memory_.SetData32(0x1040, 0x340);
215 this->memory_.SetData32(0x1044, 0x500);
216
217 this->memory_.SetData32(0x1048, 0x440);
218 this->memory_.SetData32(0x104c, 0x600);
219
220 // Verify that if entries is zero, that it fails.
221 uint64_t fde_offset;
222 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
223 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
224
225 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x344, &fde_offset));
226 EXPECT_EQ(0x500U, fde_offset);
227
228 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
229 EXPECT_EQ(0x600U, fde_offset);
230
231 // Expect that the data is cached so no more memory reads will occur.
232 this->memory_.Clear();
233 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x444, &fde_offset));
234 EXPECT_EQ(0x600U, fde_offset);
235}
236
237TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_last_element) {
238 this->eh_frame_->TestSetFdeCount(2);
239 this->eh_frame_->TestSetEntriesDataOffset(0x100);
240 this->eh_frame_->TestSetEntriesEnd(0x2000);
241 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
242 this->eh_frame_->TestSetCurEntriesOffset(0x1040);
243
244 this->memory_.SetData32(0x1040, 0x340);
245 this->memory_.SetData32(0x1044, 0x500);
246
247 this->memory_.SetData32(0x1048, 0x440);
248 this->memory_.SetData32(0x104c, 0x600);
249
250 uint64_t fde_offset;
251 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
252 EXPECT_EQ(0x600U, fde_offset);
253}
254
255TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetSequential_end_check) {
256 this->eh_frame_->TestSetFdeCount(2);
257 this->eh_frame_->TestSetEntriesDataOffset(0x100);
258 this->eh_frame_->TestSetEntriesEnd(0x1048);
259 this->eh_frame_->TestSetTableEncoding(DW_EH_PE_udata4);
260
261 this->memory_.SetData32(0x1040, 0x340);
262 this->memory_.SetData32(0x1044, 0x500);
263
264 this->memory_.SetData32(0x1048, 0x440);
265 this->memory_.SetData32(0x104c, 0x600);
266
267 uint64_t fde_offset;
268 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetSequential(0x540, &fde_offset));
269 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error());
270}
271
272TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_fail_fde_count) {
273 this->eh_frame_->TestSetFdeCount(0);
274
275 uint64_t fde_offset;
276 ASSERT_FALSE(this->eh_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
277 ASSERT_EQ(DWARF_ERROR_NONE, this->eh_frame_->last_error());
278}
279
280TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_binary_search) {
281 this->eh_frame_->TestSetTableEntrySize(16);
282 this->eh_frame_->TestSetFdeCount(10);
283
284 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
285 info.pc = 0x550;
286 info.offset = 0x10500;
287 this->eh_frame_->TestSetFdeInfo(5, info);
288 info.pc = 0x750;
289 info.offset = 0x10700;
290 this->eh_frame_->TestSetFdeInfo(7, info);
291 info.pc = 0x850;
292 info.offset = 0x10800;
293 this->eh_frame_->TestSetFdeInfo(8, info);
294
295 uint64_t fde_offset;
296 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x800, &fde_offset));
297 EXPECT_EQ(0x10700U, fde_offset);
298}
299
300TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeOffsetFromPc_sequential_search) {
301 this->eh_frame_->TestSetFdeCount(10);
302 this->eh_frame_->TestSetTableEntrySize(0);
303
304 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
305 info.pc = 0x50;
306 info.offset = 0x10000;
307 this->eh_frame_->TestSetFdeInfo(0, info);
308 info.pc = 0x150;
309 info.offset = 0x10100;
310 this->eh_frame_->TestSetFdeInfo(1, info);
311 info.pc = 0x250;
312 info.offset = 0x10200;
313 this->eh_frame_->TestSetFdeInfo(2, info);
314
315 uint64_t fde_offset;
316 ASSERT_TRUE(this->eh_frame_->GetFdeOffsetFromPc(0x200, &fde_offset));
317 EXPECT_EQ(0x10100U, fde_offset);
318}
319
320TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde32) {
321 // CIE 32 information.
322 this->memory_.SetData32(0xf000, 0x100);
323 this->memory_.SetData32(0xf004, 0);
324 this->memory_.SetData8(0xf008, 0x1);
325 this->memory_.SetData8(0xf009, '\0');
326 this->memory_.SetData8(0xf00a, 4);
327 this->memory_.SetData8(0xf00b, 8);
328 this->memory_.SetData8(0xf00c, 0x20);
329
330 // FDE 32 information.
331 this->memory_.SetData32(0x14000, 0x20);
332 this->memory_.SetData32(0x14004, 0x5004);
333 this->memory_.SetData32(0x14008, 0x9000);
334 this->memory_.SetData32(0x1400c, 0x100);
335
336 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x14000);
337 ASSERT_TRUE(fde != nullptr);
338 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
339 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
340 EXPECT_EQ(0x1d00cU, fde->pc_start);
341 EXPECT_EQ(0x1d10cU, fde->pc_end);
342 EXPECT_EQ(0xf000U, fde->cie_offset);
343 EXPECT_EQ(0U, fde->lsda_address);
344
345 ASSERT_TRUE(fde->cie != nullptr);
346 EXPECT_EQ(1U, fde->cie->version);
347 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
348 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
349 EXPECT_EQ(0U, fde->cie->segment_size);
350 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
351 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
352 EXPECT_EQ(0U, fde->cie->personality_handler);
353 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
354 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
355 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
356 EXPECT_EQ(8, fde->cie->data_alignment_factor);
357 EXPECT_EQ(0x20U, fde->cie->return_address_register);
358}
359
360TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetCieFde64) {
361 // CIE 64 information.
362 this->memory_.SetData32(0x6000, 0xffffffff);
363 this->memory_.SetData64(0x6004, 0x100);
364 this->memory_.SetData64(0x600c, 0);
365 this->memory_.SetData8(0x6014, 0x1);
366 this->memory_.SetData8(0x6015, '\0');
367 this->memory_.SetData8(0x6016, 4);
368 this->memory_.SetData8(0x6017, 8);
369 this->memory_.SetData8(0x6018, 0x20);
370
371 // FDE 64 information.
372 this->memory_.SetData32(0x8000, 0xffffffff);
373 this->memory_.SetData64(0x8004, 0x200);
374 this->memory_.SetData64(0x800c, 0x200c);
375 this->memory_.SetData64(0x8014, 0x5000);
376 this->memory_.SetData64(0x801c, 0x300);
377
378 const DwarfFde* fde = this->eh_frame_->GetFdeFromOffset(0x8000);
379 ASSERT_TRUE(fde != nullptr);
380 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
381 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
382 EXPECT_EQ(0xd01cU, fde->pc_start);
383 EXPECT_EQ(0xd31cU, fde->pc_end);
384 EXPECT_EQ(0x6000U, fde->cie_offset);
385 EXPECT_EQ(0U, fde->lsda_address);
386
387 ASSERT_TRUE(fde->cie != nullptr);
388 EXPECT_EQ(1U, fde->cie->version);
389 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
390 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
391 EXPECT_EQ(0U, fde->cie->segment_size);
392 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
393 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
394 EXPECT_EQ(0U, fde->cie->personality_handler);
395 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
396 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
397 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
398 EXPECT_EQ(8, fde->cie->data_alignment_factor);
399 EXPECT_EQ(0x20U, fde->cie->return_address_register);
400}
401
402TYPED_TEST_P(DwarfEhFrameWithHdrTest, GetFdeFromPc_fde_not_found) {
403 this->eh_frame_->TestSetTableEntrySize(16);
404 this->eh_frame_->TestSetFdeCount(1);
405
406 typename DwarfEhFrameWithHdr<TypeParam>::FdeInfo info;
407 info.pc = 0x550;
408 info.offset = 0x10500;
409 this->eh_frame_->TestSetFdeInfo(0, info);
410
411 ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
412}
413
414REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, GetFdeInfoFromIndex_expect_cache_fail,
415 GetFdeInfoFromIndex_read_pcrel, GetFdeInfoFromIndex_read_datarel,
416 GetFdeInfoFromIndex_cached, GetFdeOffsetBinary_verify,
417 GetFdeOffsetSequential, GetFdeOffsetSequential_last_element,
418 GetFdeOffsetSequential_end_check, GetFdeOffsetFromPc_fail_fde_count,
419 GetFdeOffsetFromPc_binary_search, GetFdeOffsetFromPc_sequential_search,
420 GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
421
422typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
423INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
424
425} // namespace unwindstack