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