summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'libunwindstack/tests/DwarfDebugFrameTest.cpp')
-rw-r--r--libunwindstack/tests/DwarfDebugFrameTest.cpp461
1 files changed, 461 insertions, 0 deletions
diff --git a/libunwindstack/tests/DwarfDebugFrameTest.cpp b/libunwindstack/tests/DwarfDebugFrameTest.cpp
new file mode 100644
index 000000000..69813e5ce
--- /dev/null
+++ b/libunwindstack/tests/DwarfDebugFrameTest.cpp
@@ -0,0 +1,461 @@
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 "DwarfDebugFrame.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 MockDwarfDebugFrame : public DwarfDebugFrame<TypeParam> {
34 public:
35 MockDwarfDebugFrame(Memory* memory) : DwarfDebugFrame<TypeParam>(memory) {}
36 ~MockDwarfDebugFrame() = default;
37
38 void TestSetFdeCount(uint64_t count) { this->fde_count_ = count; }
39 void TestSetOffset(uint64_t offset) { this->offset_ = offset; }
40 void TestSetEndOffset(uint64_t offset) { this->end_offset_ = offset; }
41 void TestPushFdeInfo(const typename DwarfDebugFrame<TypeParam>::FdeInfo& info) {
42 this->fdes_.push_back(info);
43 }
44
45 uint64_t TestGetFdeCount() { return this->fde_count_; }
46 uint8_t TestGetOffset() { return this->offset_; }
47 uint8_t TestGetEndOffset() { return this->end_offset_; }
48 void TestGetFdeInfo(size_t index, typename DwarfDebugFrame<TypeParam>::FdeInfo* info) {
49 *info = this->fdes_[index];
50 }
51};
52
53template <typename TypeParam>
54class DwarfDebugFrameTest : public ::testing::Test {
55 protected:
56 void SetUp() override {
57 memory_.Clear();
58 debug_frame_ = new MockDwarfDebugFrame<TypeParam>(&memory_);
59 ResetLogs();
60 }
61
62 void TearDown() override { delete debug_frame_; }
63
64 MemoryFake memory_;
65 MockDwarfDebugFrame<TypeParam>* debug_frame_ = nullptr;
66};
67TYPED_TEST_CASE_P(DwarfDebugFrameTest);
68
69// NOTE: All test class variables need to be referenced as this->.
70
71TYPED_TEST_P(DwarfDebugFrameTest, Init32) {
72 // CIE 32 information.
73 this->memory_.SetData32(0x5000, 0xfc);
74 this->memory_.SetData32(0x5004, 0xffffffff);
75 this->memory_.SetData8(0x5008, 1);
76 this->memory_.SetData8(0x5009, '\0');
77
78 // FDE 32 information.
79 this->memory_.SetData32(0x5100, 0xfc);
80 this->memory_.SetData32(0x5104, 0);
81 this->memory_.SetData32(0x5108, 0x1500);
82 this->memory_.SetData32(0x510c, 0x200);
83
84 this->memory_.SetData32(0x5200, 0xfc);
85 this->memory_.SetData32(0x5204, 0);
86 this->memory_.SetData32(0x5208, 0x2500);
87 this->memory_.SetData32(0x520c, 0x300);
88
89 // CIE 32 information.
90 this->memory_.SetData32(0x5300, 0xfc);
91 this->memory_.SetData32(0x5304, 0xffffffff);
92 this->memory_.SetData8(0x5308, 1);
93 this->memory_.SetData8(0x5309, '\0');
94
95 // FDE 32 information.
96 this->memory_.SetData32(0x5400, 0xfc);
97 this->memory_.SetData32(0x5404, 0x300);
98 this->memory_.SetData32(0x5408, 0x3500);
99 this->memory_.SetData32(0x540c, 0x400);
100
101 this->memory_.SetData32(0x5500, 0xfc);
102 this->memory_.SetData32(0x5504, 0x300);
103 this->memory_.SetData32(0x5508, 0x4500);
104 this->memory_.SetData32(0x550c, 0x500);
105
106 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
107 ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount());
108
109 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
110
111 this->debug_frame_->TestGetFdeInfo(0, &info);
112 EXPECT_EQ(0x5100U, info.offset);
113 EXPECT_EQ(0x1500U, info.start);
114 EXPECT_EQ(0x1700U, info.end);
115
116 this->debug_frame_->TestGetFdeInfo(1, &info);
117 EXPECT_EQ(0x5200U, info.offset);
118 EXPECT_EQ(0x2500U, info.start);
119 EXPECT_EQ(0x2800U, info.end);
120
121 this->debug_frame_->TestGetFdeInfo(2, &info);
122 EXPECT_EQ(0x5400U, info.offset);
123 EXPECT_EQ(0x3500U, info.start);
124 EXPECT_EQ(0x3900U, info.end);
125
126 this->debug_frame_->TestGetFdeInfo(3, &info);
127 EXPECT_EQ(0x5500U, info.offset);
128 EXPECT_EQ(0x4500U, info.start);
129 EXPECT_EQ(0x4a00U, info.end);
130}
131
132TYPED_TEST_P(DwarfDebugFrameTest, Init32_fde_not_following_cie) {
133 // CIE 32 information.
134 this->memory_.SetData32(0x5000, 0xfc);
135 this->memory_.SetData32(0x5004, 0xffffffff);
136 this->memory_.SetData8(0x5008, 1);
137 this->memory_.SetData8(0x5009, '\0');
138
139 // FDE 32 information.
140 this->memory_.SetData32(0x5100, 0xfc);
141 this->memory_.SetData32(0x5104, 0x1000);
142 this->memory_.SetData32(0x5108, 0x1500);
143 this->memory_.SetData32(0x510c, 0x200);
144
145 ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600));
146 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->last_error());
147}
148
149TYPED_TEST_P(DwarfDebugFrameTest, Init64) {
150 // CIE 64 information.
151 this->memory_.SetData32(0x5000, 0xffffffff);
152 this->memory_.SetData64(0x5004, 0xf4);
153 this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
154 this->memory_.SetData8(0x5014, 1);
155 this->memory_.SetData8(0x5015, '\0');
156
157 // FDE 64 information.
158 this->memory_.SetData32(0x5100, 0xffffffff);
159 this->memory_.SetData64(0x5104, 0xf4);
160 this->memory_.SetData64(0x510c, 0);
161 this->memory_.SetData64(0x5114, 0x1500);
162 this->memory_.SetData64(0x511c, 0x200);
163
164 this->memory_.SetData32(0x5200, 0xffffffff);
165 this->memory_.SetData64(0x5204, 0xf4);
166 this->memory_.SetData64(0x520c, 0);
167 this->memory_.SetData64(0x5214, 0x2500);
168 this->memory_.SetData64(0x521c, 0x300);
169
170 // CIE 64 information.
171 this->memory_.SetData32(0x5300, 0xffffffff);
172 this->memory_.SetData64(0x5304, 0xf4);
173 this->memory_.SetData64(0x530c, 0xffffffffffffffffULL);
174 this->memory_.SetData8(0x5314, 1);
175 this->memory_.SetData8(0x5315, '\0');
176
177 // FDE 64 information.
178 this->memory_.SetData32(0x5400, 0xffffffff);
179 this->memory_.SetData64(0x5404, 0xf4);
180 this->memory_.SetData64(0x540c, 0x300);
181 this->memory_.SetData64(0x5414, 0x3500);
182 this->memory_.SetData64(0x541c, 0x400);
183
184 this->memory_.SetData32(0x5500, 0xffffffff);
185 this->memory_.SetData64(0x5504, 0xf4);
186 this->memory_.SetData64(0x550c, 0x300);
187 this->memory_.SetData64(0x5514, 0x4500);
188 this->memory_.SetData64(0x551c, 0x500);
189
190 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600));
191 ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount());
192
193 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
194
195 this->debug_frame_->TestGetFdeInfo(0, &info);
196 EXPECT_EQ(0x5100U, info.offset);
197 EXPECT_EQ(0x1500U, info.start);
198 EXPECT_EQ(0x1700U, info.end);
199
200 this->debug_frame_->TestGetFdeInfo(1, &info);
201 EXPECT_EQ(0x5200U, info.offset);
202 EXPECT_EQ(0x2500U, info.start);
203 EXPECT_EQ(0x2800U, info.end);
204
205 this->debug_frame_->TestGetFdeInfo(2, &info);
206 EXPECT_EQ(0x5400U, info.offset);
207 EXPECT_EQ(0x3500U, info.start);
208 EXPECT_EQ(0x3900U, info.end);
209
210 this->debug_frame_->TestGetFdeInfo(3, &info);
211 EXPECT_EQ(0x5500U, info.offset);
212 EXPECT_EQ(0x4500U, info.start);
213 EXPECT_EQ(0x4a00U, info.end);
214}
215
216TYPED_TEST_P(DwarfDebugFrameTest, Init64_fde_not_following_cie) {
217 // CIE 64 information.
218 this->memory_.SetData32(0x5000, 0xffffffff);
219 this->memory_.SetData64(0x5004, 0xf4);
220 this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
221 this->memory_.SetData8(0x5014, 1);
222 this->memory_.SetData8(0x5015, '\0');
223
224 // FDE 64 information.
225 this->memory_.SetData32(0x5100, 0xffffffff);
226 this->memory_.SetData64(0x5104, 0xf4);
227 this->memory_.SetData64(0x510c, 0x1000);
228 this->memory_.SetData64(0x5114, 0x1500);
229 this->memory_.SetData64(0x511c, 0x200);
230
231 ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600));
232 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->last_error());
233}
234
235TYPED_TEST_P(DwarfDebugFrameTest, Init_version1) {
236 // CIE 32 information.
237 this->memory_.SetData32(0x5000, 0xfc);
238 this->memory_.SetData32(0x5004, 0xffffffff);
239 this->memory_.SetData8(0x5008, 1);
240 // Augment string.
241 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'});
242 // Code alignment factor.
243 this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00});
244 // Data alignment factor.
245 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
246 // Return address register
247 this->memory_.SetData8(0x5014, 0x84);
248 // Augmentation length
249 this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00});
250 // R data.
251 this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2);
252
253 // FDE 32 information.
254 this->memory_.SetData32(0x5100, 0xfc);
255 this->memory_.SetData32(0x5104, 0);
256 this->memory_.SetData16(0x5108, 0x1500);
257 this->memory_.SetData16(0x510a, 0x200);
258
259 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200));
260 ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
261
262 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
263 this->debug_frame_->TestGetFdeInfo(0, &info);
264 EXPECT_EQ(0x5100U, info.offset);
265 EXPECT_EQ(0x1500U, info.start);
266 EXPECT_EQ(0x1700U, info.end);
267}
268
269TYPED_TEST_P(DwarfDebugFrameTest, Init_version4) {
270 // CIE 32 information.
271 this->memory_.SetData32(0x5000, 0xfc);
272 this->memory_.SetData32(0x5004, 0xffffffff);
273 this->memory_.SetData8(0x5008, 4);
274 // Augment string.
275 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
276 // Address size.
277 this->memory_.SetData8(0x500e, 4);
278 // Segment size.
279 this->memory_.SetData8(0x500f, 0);
280 // Code alignment factor.
281 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00});
282 // Data alignment factor.
283 this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
284 // Return address register
285 this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10});
286 // Augmentation length
287 this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00});
288 // L data.
289 this->memory_.SetData8(0x501a, 0x10);
290 // P data.
291 this->memory_.SetData8(0x501b, DW_EH_PE_udata4);
292 this->memory_.SetData32(0x501c, 0x100);
293 // R data.
294 this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2);
295
296 // FDE 32 information.
297 this->memory_.SetData32(0x5100, 0xfc);
298 this->memory_.SetData32(0x5104, 0);
299 this->memory_.SetData16(0x5108, 0x1500);
300 this->memory_.SetData16(0x510a, 0x200);
301
302 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200));
303 ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
304
305 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
306 this->debug_frame_->TestGetFdeInfo(0, &info);
307 EXPECT_EQ(0x5100U, info.offset);
308 EXPECT_EQ(0x1500U, info.start);
309 EXPECT_EQ(0x1700U, info.end);
310}
311
312TYPED_TEST_P(DwarfDebugFrameTest, GetFdeOffsetFromPc) {
313 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
314 for (size_t i = 0; i < 9; i++) {
315 info.start = 0x1000 * (i + 1);
316 info.end = 0x1000 * (i + 2) - 0x10;
317 info.offset = 0x5000 + i * 0x20;
318 this->debug_frame_->TestPushFdeInfo(info);
319 }
320
321 this->debug_frame_->TestSetFdeCount(0);
322 uint64_t fde_offset;
323 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
324 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error());
325
326 this->debug_frame_->TestSetFdeCount(9);
327 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
328 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error());
329 // Odd number of elements.
330 for (size_t i = 0; i < 9; i++) {
331 TypeParam pc = 0x1000 * (i + 1);
332 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
333 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
334 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
335 << i;
336 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
337 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
338 << "Failed at index " << i;
339 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
340 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
341 << "Failed at index " << i;
342 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error());
343 }
344
345 // Even number of elements.
346 this->debug_frame_->TestSetFdeCount(10);
347 info.start = 0xa000;
348 info.end = 0xaff0;
349 info.offset = 0x5120;
350 this->debug_frame_->TestPushFdeInfo(info);
351
352 for (size_t i = 0; i < 10; i++) {
353 TypeParam pc = 0x1000 * (i + 1);
354 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
355 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
356 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
357 << i;
358 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
359 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
360 << "Failed at index " << i;
361 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
362 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
363 << "Failed at index " << i;
364 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->last_error());
365 }
366}
367
368TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) {
369 this->debug_frame_->TestSetOffset(0x4000);
370
371 // CIE 32 information.
372 this->memory_.SetData32(0xf000, 0x100);
373 this->memory_.SetData32(0xf004, 0xffffffff);
374 this->memory_.SetData8(0xf008, 0x1);
375 this->memory_.SetData8(0xf009, '\0');
376 this->memory_.SetData8(0xf00a, 4);
377 this->memory_.SetData8(0xf00b, 8);
378 this->memory_.SetData8(0xf00c, 0x20);
379
380 // FDE 32 information.
381 this->memory_.SetData32(0x14000, 0x20);
382 this->memory_.SetData32(0x14004, 0xb000);
383 this->memory_.SetData32(0x14008, 0x9000);
384 this->memory_.SetData32(0x1400c, 0x100);
385
386 const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000);
387 ASSERT_TRUE(fde != nullptr);
388 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
389 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
390 EXPECT_EQ(0x9000U, fde->pc_start);
391 EXPECT_EQ(0x9100U, fde->pc_end);
392 EXPECT_EQ(0xf000U, fde->cie_offset);
393 EXPECT_EQ(0U, fde->lsda_address);
394
395 ASSERT_TRUE(fde->cie != nullptr);
396 EXPECT_EQ(1U, fde->cie->version);
397 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
398 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
399 EXPECT_EQ(0U, fde->cie->segment_size);
400 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
401 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
402 EXPECT_EQ(0U, fde->cie->personality_handler);
403 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
404 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
405 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
406 EXPECT_EQ(8, fde->cie->data_alignment_factor);
407 EXPECT_EQ(0x20U, fde->cie->return_address_register);
408}
409
410TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) {
411 this->debug_frame_->TestSetOffset(0x2000);
412
413 // CIE 64 information.
414 this->memory_.SetData32(0x6000, 0xffffffff);
415 this->memory_.SetData64(0x6004, 0x100);
416 this->memory_.SetData64(0x600c, 0xffffffffffffffffULL);
417 this->memory_.SetData8(0x6014, 0x1);
418 this->memory_.SetData8(0x6015, '\0');
419 this->memory_.SetData8(0x6016, 4);
420 this->memory_.SetData8(0x6017, 8);
421 this->memory_.SetData8(0x6018, 0x20);
422
423 // FDE 64 information.
424 this->memory_.SetData32(0x8000, 0xffffffff);
425 this->memory_.SetData64(0x8004, 0x200);
426 this->memory_.SetData64(0x800c, 0x4000);
427 this->memory_.SetData64(0x8014, 0x5000);
428 this->memory_.SetData64(0x801c, 0x300);
429
430 const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000);
431 ASSERT_TRUE(fde != nullptr);
432 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
433 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
434 EXPECT_EQ(0x5000U, fde->pc_start);
435 EXPECT_EQ(0x5300U, fde->pc_end);
436 EXPECT_EQ(0x6000U, fde->cie_offset);
437 EXPECT_EQ(0U, fde->lsda_address);
438
439 ASSERT_TRUE(fde->cie != nullptr);
440 EXPECT_EQ(1U, fde->cie->version);
441 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
442 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
443 EXPECT_EQ(0U, fde->cie->segment_size);
444 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
445 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
446 EXPECT_EQ(0U, fde->cie->personality_handler);
447 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
448 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
449 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
450 EXPECT_EQ(8, fde->cie->data_alignment_factor);
451 EXPECT_EQ(0x20U, fde->cie->return_address_register);
452}
453
454REGISTER_TYPED_TEST_CASE_P(DwarfDebugFrameTest, Init32, Init32_fde_not_following_cie, Init64,
455 Init64_fde_not_following_cie, Init_version1, Init_version4,
456 GetFdeOffsetFromPc, GetCieFde32, GetCieFde64);
457
458typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes;
459INSTANTIATE_TYPED_TEST_CASE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
460
461} // namespace unwindstack