blob: 3a5204463fbc49bbc63e880c094627581c1ce519 [file] [log] [blame]
Christopher Ferris61d40972017-06-12 19:14:20 -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 Ferris61d40972017-06-12 19:14:20 -070024#include "DwarfDebugFrame.h"
25#include "DwarfEncoding.h"
26
27#include "LogFake.h"
28#include "MemoryFake.h"
Christopher Ferris61d40972017-06-12 19:14:20 -070029
Christopher Ferrisd226a512017-07-14 10:37:19 -070030namespace unwindstack {
31
Christopher Ferris61d40972017-06-12 19:14:20 -070032template <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; }
Christopher Ferrisc9dee842017-11-03 14:50:27 -070039 void TestSetOffset(uint64_t offset) { this->entries_offset_ = offset; }
40 void TestSetEndOffset(uint64_t offset) { this->entries_end_ = offset; }
Christopher Ferris61d40972017-06-12 19:14:20 -070041 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
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700106 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris61d40972017-06-12 19:14:20 -0700107 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
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700145 ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800146 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700147}
148
Christopher Ferris1a141a02018-01-24 08:52:47 -0800149TYPED_TEST_P(DwarfDebugFrameTest, Init32_do_not_fail_on_bad_next_entry) {
150 // CIE 32 information.
151 this->memory_.SetData32(0x5000, 0xfc);
152 this->memory_.SetData32(0x5004, 0xffffffff);
153 this->memory_.SetData8(0x5008, 1);
154 this->memory_.SetData8(0x5009, '\0');
155
156 // FDE 32 information.
157 this->memory_.SetData32(0x5100, 0xfc);
158 this->memory_.SetData32(0x5104, 0);
159 this->memory_.SetData32(0x5108, 0x1500);
160 this->memory_.SetData32(0x510c, 0x200);
161
162 this->memory_.SetData32(0x5200, 0xfc);
163 this->memory_.SetData32(0x5204, 0);
164 this->memory_.SetData32(0x5208, 0x2500);
165 this->memory_.SetData32(0x520c, 0x300);
166
167 // CIE 32 information.
168 this->memory_.SetData32(0x5300, 0);
169 this->memory_.SetData32(0x5304, 0xffffffff);
170 this->memory_.SetData8(0x5308, 1);
171 this->memory_.SetData8(0x5309, '\0');
172
173 // FDE 32 information.
174 this->memory_.SetData32(0x5400, 0xfc);
175 this->memory_.SetData32(0x5404, 0x300);
176 this->memory_.SetData32(0x5408, 0x3500);
177 this->memory_.SetData32(0x540c, 0x400);
178
179 this->memory_.SetData32(0x5500, 0xfc);
180 this->memory_.SetData32(0x5504, 0x300);
181 this->memory_.SetData32(0x5508, 0x4500);
182 this->memory_.SetData32(0x550c, 0x500);
183
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700184 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris1a141a02018-01-24 08:52:47 -0800185 ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount());
186}
187
Christopher Ferris61d40972017-06-12 19:14:20 -0700188TYPED_TEST_P(DwarfDebugFrameTest, Init64) {
189 // CIE 64 information.
190 this->memory_.SetData32(0x5000, 0xffffffff);
191 this->memory_.SetData64(0x5004, 0xf4);
192 this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
193 this->memory_.SetData8(0x5014, 1);
194 this->memory_.SetData8(0x5015, '\0');
195
196 // FDE 64 information.
197 this->memory_.SetData32(0x5100, 0xffffffff);
198 this->memory_.SetData64(0x5104, 0xf4);
199 this->memory_.SetData64(0x510c, 0);
200 this->memory_.SetData64(0x5114, 0x1500);
201 this->memory_.SetData64(0x511c, 0x200);
202
203 this->memory_.SetData32(0x5200, 0xffffffff);
204 this->memory_.SetData64(0x5204, 0xf4);
205 this->memory_.SetData64(0x520c, 0);
206 this->memory_.SetData64(0x5214, 0x2500);
207 this->memory_.SetData64(0x521c, 0x300);
208
209 // CIE 64 information.
210 this->memory_.SetData32(0x5300, 0xffffffff);
211 this->memory_.SetData64(0x5304, 0xf4);
212 this->memory_.SetData64(0x530c, 0xffffffffffffffffULL);
213 this->memory_.SetData8(0x5314, 1);
214 this->memory_.SetData8(0x5315, '\0');
215
216 // FDE 64 information.
217 this->memory_.SetData32(0x5400, 0xffffffff);
218 this->memory_.SetData64(0x5404, 0xf4);
219 this->memory_.SetData64(0x540c, 0x300);
220 this->memory_.SetData64(0x5414, 0x3500);
221 this->memory_.SetData64(0x541c, 0x400);
222
223 this->memory_.SetData32(0x5500, 0xffffffff);
224 this->memory_.SetData64(0x5504, 0xf4);
225 this->memory_.SetData64(0x550c, 0x300);
226 this->memory_.SetData64(0x5514, 0x4500);
227 this->memory_.SetData64(0x551c, 0x500);
228
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700229 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris61d40972017-06-12 19:14:20 -0700230 ASSERT_EQ(4U, this->debug_frame_->TestGetFdeCount());
231
232 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
233
234 this->debug_frame_->TestGetFdeInfo(0, &info);
235 EXPECT_EQ(0x5100U, info.offset);
236 EXPECT_EQ(0x1500U, info.start);
237 EXPECT_EQ(0x1700U, info.end);
238
239 this->debug_frame_->TestGetFdeInfo(1, &info);
240 EXPECT_EQ(0x5200U, info.offset);
241 EXPECT_EQ(0x2500U, info.start);
242 EXPECT_EQ(0x2800U, info.end);
243
244 this->debug_frame_->TestGetFdeInfo(2, &info);
245 EXPECT_EQ(0x5400U, info.offset);
246 EXPECT_EQ(0x3500U, info.start);
247 EXPECT_EQ(0x3900U, info.end);
248
249 this->debug_frame_->TestGetFdeInfo(3, &info);
250 EXPECT_EQ(0x5500U, info.offset);
251 EXPECT_EQ(0x4500U, info.start);
252 EXPECT_EQ(0x4a00U, info.end);
253}
254
255TYPED_TEST_P(DwarfDebugFrameTest, Init64_fde_not_following_cie) {
256 // CIE 64 information.
257 this->memory_.SetData32(0x5000, 0xffffffff);
258 this->memory_.SetData64(0x5004, 0xf4);
259 this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
260 this->memory_.SetData8(0x5014, 1);
261 this->memory_.SetData8(0x5015, '\0');
262
263 // FDE 64 information.
264 this->memory_.SetData32(0x5100, 0xffffffff);
265 this->memory_.SetData64(0x5104, 0xf4);
266 this->memory_.SetData64(0x510c, 0x1000);
267 this->memory_.SetData64(0x5114, 0x1500);
268 this->memory_.SetData64(0x511c, 0x200);
269
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700270 ASSERT_FALSE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800271 ASSERT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700272}
273
Christopher Ferris1a141a02018-01-24 08:52:47 -0800274TYPED_TEST_P(DwarfDebugFrameTest, Init64_do_not_fail_on_bad_next_entry) {
275 // CIE 64 information.
276 this->memory_.SetData32(0x5000, 0xffffffff);
277 this->memory_.SetData64(0x5004, 0xf4);
278 this->memory_.SetData64(0x500c, 0xffffffffffffffffULL);
279 this->memory_.SetData8(0x5014, 1);
280 this->memory_.SetData8(0x5015, '\0');
281
282 // FDE 64 information.
283 this->memory_.SetData32(0x5100, 0xffffffff);
284 this->memory_.SetData64(0x5104, 0xf4);
285 this->memory_.SetData64(0x510c, 0);
286 this->memory_.SetData64(0x5114, 0x1500);
287 this->memory_.SetData64(0x511c, 0x200);
288
289 this->memory_.SetData32(0x5200, 0xffffffff);
290 this->memory_.SetData64(0x5204, 0xf4);
291 this->memory_.SetData64(0x520c, 0);
292 this->memory_.SetData64(0x5214, 0x2500);
293 this->memory_.SetData64(0x521c, 0x300);
294
295 // CIE 64 information.
296 this->memory_.SetData32(0x5300, 0xffffffff);
297 this->memory_.SetData64(0x5304, 0);
298 this->memory_.SetData64(0x530c, 0xffffffffffffffffULL);
299 this->memory_.SetData8(0x5314, 1);
300 this->memory_.SetData8(0x5315, '\0');
301
302 // FDE 64 information.
303 this->memory_.SetData32(0x5400, 0xffffffff);
304 this->memory_.SetData64(0x5404, 0xf4);
305 this->memory_.SetData64(0x540c, 0x300);
306 this->memory_.SetData64(0x5414, 0x3500);
307 this->memory_.SetData64(0x541c, 0x400);
308
309 this->memory_.SetData32(0x5500, 0xffffffff);
310 this->memory_.SetData64(0x5504, 0xf4);
311 this->memory_.SetData64(0x550c, 0x300);
312 this->memory_.SetData64(0x5514, 0x4500);
313 this->memory_.SetData64(0x551c, 0x500);
314
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700315 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x600, 0));
Christopher Ferris1a141a02018-01-24 08:52:47 -0800316 ASSERT_EQ(2U, this->debug_frame_->TestGetFdeCount());
317}
318
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700319TYPED_TEST_P(DwarfDebugFrameTest, Init_non_zero_load_bias) {
320 // CIE 32 information.
321 this->memory_.SetData32(0x5000, 0xfc);
322 this->memory_.SetData32(0x5004, 0xffffffff);
323 this->memory_.SetData8(0x5008, 1);
324 this->memory_.SetData8(0x5009, 'z');
325 this->memory_.SetData8(0x500a, 'R');
326 this->memory_.SetData8(0x500b, '\0');
327 this->memory_.SetData8(0x500c, 0);
328 this->memory_.SetData8(0x500d, 0);
329 this->memory_.SetData8(0x500e, 0);
330 this->memory_.SetData8(0x500f, 0);
331 this->memory_.SetData8(0x5010, 0x1b);
332
333 // FDE 32 information.
334 this->memory_.SetData32(0x5100, 0xfc);
335 this->memory_.SetData32(0x5104, 0);
336 this->memory_.SetData32(0x5108, 0x1500);
337 this->memory_.SetData32(0x510c, 0x200);
338 this->memory_.SetData8(0x5110, 0);
339 this->memory_.SetData8(0x5111, 0);
340
341 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200, 0x1000));
342 ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
343
344 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
345
346 this->debug_frame_->TestGetFdeInfo(0, &info);
347 EXPECT_EQ(0x5100U, info.offset);
348 EXPECT_EQ(0x2500U, info.start);
349 EXPECT_EQ(0x2700U, info.end);
350
351 const DwarfFde* fde = this->debug_frame_->GetFdeFromPc(0x2504);
352 ASSERT_TRUE(fde != nullptr);
353 EXPECT_EQ(0x2500U, fde->pc_start);
354 EXPECT_EQ(0x2700U, fde->pc_end);
355}
356
Christopher Ferris61d40972017-06-12 19:14:20 -0700357TYPED_TEST_P(DwarfDebugFrameTest, Init_version1) {
358 // CIE 32 information.
359 this->memory_.SetData32(0x5000, 0xfc);
360 this->memory_.SetData32(0x5004, 0xffffffff);
361 this->memory_.SetData8(0x5008, 1);
362 // Augment string.
363 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'R', 'P', 'L', '\0'});
364 // Code alignment factor.
365 this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x80, 0x00});
366 // Data alignment factor.
367 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
368 // Return address register
369 this->memory_.SetData8(0x5014, 0x84);
370 // Augmentation length
371 this->memory_.SetMemory(0x5015, std::vector<uint8_t>{0x84, 0x00});
372 // R data.
373 this->memory_.SetData8(0x5017, DW_EH_PE_pcrel | DW_EH_PE_udata2);
374
375 // FDE 32 information.
376 this->memory_.SetData32(0x5100, 0xfc);
377 this->memory_.SetData32(0x5104, 0);
378 this->memory_.SetData16(0x5108, 0x1500);
379 this->memory_.SetData16(0x510a, 0x200);
380
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700381 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200, 0));
Christopher Ferris61d40972017-06-12 19:14:20 -0700382 ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
383
384 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
385 this->debug_frame_->TestGetFdeInfo(0, &info);
386 EXPECT_EQ(0x5100U, info.offset);
387 EXPECT_EQ(0x1500U, info.start);
388 EXPECT_EQ(0x1700U, info.end);
389}
390
391TYPED_TEST_P(DwarfDebugFrameTest, Init_version4) {
392 // CIE 32 information.
393 this->memory_.SetData32(0x5000, 0xfc);
394 this->memory_.SetData32(0x5004, 0xffffffff);
395 this->memory_.SetData8(0x5008, 4);
396 // Augment string.
397 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
398 // Address size.
399 this->memory_.SetData8(0x500e, 4);
400 // Segment size.
401 this->memory_.SetData8(0x500f, 0);
402 // Code alignment factor.
403 this->memory_.SetMemory(0x5010, std::vector<uint8_t>{0x80, 0x00});
404 // Data alignment factor.
405 this->memory_.SetMemory(0x5012, std::vector<uint8_t>{0x81, 0x80, 0x80, 0x00});
406 // Return address register
407 this->memory_.SetMemory(0x5016, std::vector<uint8_t>{0x85, 0x10});
408 // Augmentation length
409 this->memory_.SetMemory(0x5018, std::vector<uint8_t>{0x84, 0x00});
410 // L data.
411 this->memory_.SetData8(0x501a, 0x10);
412 // P data.
413 this->memory_.SetData8(0x501b, DW_EH_PE_udata4);
414 this->memory_.SetData32(0x501c, 0x100);
415 // R data.
416 this->memory_.SetData8(0x5020, DW_EH_PE_pcrel | DW_EH_PE_udata2);
417
418 // FDE 32 information.
419 this->memory_.SetData32(0x5100, 0xfc);
420 this->memory_.SetData32(0x5104, 0);
421 this->memory_.SetData16(0x5108, 0x1500);
422 this->memory_.SetData16(0x510a, 0x200);
423
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700424 ASSERT_TRUE(this->debug_frame_->Init(0x5000, 0x200, 0));
Christopher Ferris61d40972017-06-12 19:14:20 -0700425 ASSERT_EQ(1U, this->debug_frame_->TestGetFdeCount());
426
427 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
428 this->debug_frame_->TestGetFdeInfo(0, &info);
429 EXPECT_EQ(0x5100U, info.offset);
430 EXPECT_EQ(0x1500U, info.start);
431 EXPECT_EQ(0x1700U, info.end);
432}
433
434TYPED_TEST_P(DwarfDebugFrameTest, GetFdeOffsetFromPc) {
435 typename DwarfDebugFrame<TypeParam>::FdeInfo info(0, 0, 0);
436 for (size_t i = 0; i < 9; i++) {
437 info.start = 0x1000 * (i + 1);
438 info.end = 0x1000 * (i + 2) - 0x10;
439 info.offset = 0x5000 + i * 0x20;
440 this->debug_frame_->TestPushFdeInfo(info);
441 }
442
443 this->debug_frame_->TestSetFdeCount(0);
444 uint64_t fde_offset;
445 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x1000, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800446 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700447
448 this->debug_frame_->TestSetFdeCount(9);
449 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(0x100, &fde_offset));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800450 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700451 // Odd number of elements.
452 for (size_t i = 0; i < 9; i++) {
453 TypeParam pc = 0x1000 * (i + 1);
454 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
455 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
456 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
457 << i;
458 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
459 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
460 << "Failed at index " << i;
461 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
462 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
463 << "Failed at index " << i;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800464 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700465 }
466
467 // Even number of elements.
468 this->debug_frame_->TestSetFdeCount(10);
469 info.start = 0xa000;
470 info.end = 0xaff0;
471 info.offset = 0x5120;
472 this->debug_frame_->TestPushFdeInfo(info);
473
474 for (size_t i = 0; i < 10; i++) {
475 TypeParam pc = 0x1000 * (i + 1);
476 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc, &fde_offset)) << "Failed at index " << i;
477 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
478 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 1, &fde_offset)) << "Failed at index "
479 << i;
480 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
481 ASSERT_TRUE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xeff, &fde_offset))
482 << "Failed at index " << i;
483 EXPECT_EQ(0x5000 + i * 0x20, fde_offset) << "Failed at index " << i;
484 ASSERT_FALSE(this->debug_frame_->GetFdeOffsetFromPc(pc + 0xfff, &fde_offset))
485 << "Failed at index " << i;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800486 ASSERT_EQ(DWARF_ERROR_NONE, this->debug_frame_->LastErrorCode());
Christopher Ferris61d40972017-06-12 19:14:20 -0700487 }
488}
489
490TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde32) {
491 this->debug_frame_->TestSetOffset(0x4000);
492
493 // CIE 32 information.
494 this->memory_.SetData32(0xf000, 0x100);
495 this->memory_.SetData32(0xf004, 0xffffffff);
496 this->memory_.SetData8(0xf008, 0x1);
497 this->memory_.SetData8(0xf009, '\0');
498 this->memory_.SetData8(0xf00a, 4);
499 this->memory_.SetData8(0xf00b, 8);
500 this->memory_.SetData8(0xf00c, 0x20);
501
502 // FDE 32 information.
503 this->memory_.SetData32(0x14000, 0x20);
504 this->memory_.SetData32(0x14004, 0xb000);
505 this->memory_.SetData32(0x14008, 0x9000);
506 this->memory_.SetData32(0x1400c, 0x100);
507
508 const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x14000);
509 ASSERT_TRUE(fde != nullptr);
510 EXPECT_EQ(0x14010U, fde->cfa_instructions_offset);
511 EXPECT_EQ(0x14024U, fde->cfa_instructions_end);
512 EXPECT_EQ(0x9000U, fde->pc_start);
513 EXPECT_EQ(0x9100U, fde->pc_end);
514 EXPECT_EQ(0xf000U, fde->cie_offset);
515 EXPECT_EQ(0U, fde->lsda_address);
516
517 ASSERT_TRUE(fde->cie != nullptr);
518 EXPECT_EQ(1U, fde->cie->version);
519 EXPECT_EQ(DW_EH_PE_sdata4, fde->cie->fde_address_encoding);
520 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
521 EXPECT_EQ(0U, fde->cie->segment_size);
522 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
523 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
524 EXPECT_EQ(0U, fde->cie->personality_handler);
525 EXPECT_EQ(0xf00dU, fde->cie->cfa_instructions_offset);
526 EXPECT_EQ(0xf104U, fde->cie->cfa_instructions_end);
527 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
528 EXPECT_EQ(8, fde->cie->data_alignment_factor);
529 EXPECT_EQ(0x20U, fde->cie->return_address_register);
530}
531
532TYPED_TEST_P(DwarfDebugFrameTest, GetCieFde64) {
533 this->debug_frame_->TestSetOffset(0x2000);
534
535 // CIE 64 information.
536 this->memory_.SetData32(0x6000, 0xffffffff);
537 this->memory_.SetData64(0x6004, 0x100);
538 this->memory_.SetData64(0x600c, 0xffffffffffffffffULL);
539 this->memory_.SetData8(0x6014, 0x1);
540 this->memory_.SetData8(0x6015, '\0');
541 this->memory_.SetData8(0x6016, 4);
542 this->memory_.SetData8(0x6017, 8);
543 this->memory_.SetData8(0x6018, 0x20);
544
545 // FDE 64 information.
546 this->memory_.SetData32(0x8000, 0xffffffff);
547 this->memory_.SetData64(0x8004, 0x200);
548 this->memory_.SetData64(0x800c, 0x4000);
549 this->memory_.SetData64(0x8014, 0x5000);
550 this->memory_.SetData64(0x801c, 0x300);
551
552 const DwarfFde* fde = this->debug_frame_->GetFdeFromOffset(0x8000);
553 ASSERT_TRUE(fde != nullptr);
554 EXPECT_EQ(0x8024U, fde->cfa_instructions_offset);
555 EXPECT_EQ(0x820cU, fde->cfa_instructions_end);
556 EXPECT_EQ(0x5000U, fde->pc_start);
557 EXPECT_EQ(0x5300U, fde->pc_end);
558 EXPECT_EQ(0x6000U, fde->cie_offset);
559 EXPECT_EQ(0U, fde->lsda_address);
560
561 ASSERT_TRUE(fde->cie != nullptr);
562 EXPECT_EQ(1U, fde->cie->version);
563 EXPECT_EQ(DW_EH_PE_sdata8, fde->cie->fde_address_encoding);
564 EXPECT_EQ(DW_EH_PE_omit, fde->cie->lsda_encoding);
565 EXPECT_EQ(0U, fde->cie->segment_size);
566 EXPECT_EQ(1U, fde->cie->augmentation_string.size());
567 EXPECT_EQ('\0', fde->cie->augmentation_string[0]);
568 EXPECT_EQ(0U, fde->cie->personality_handler);
569 EXPECT_EQ(0x6019U, fde->cie->cfa_instructions_offset);
570 EXPECT_EQ(0x610cU, fde->cie->cfa_instructions_end);
571 EXPECT_EQ(4U, fde->cie->code_alignment_factor);
572 EXPECT_EQ(8, fde->cie->data_alignment_factor);
573 EXPECT_EQ(0x20U, fde->cie->return_address_register);
574}
575
Christopher Ferris1a141a02018-01-24 08:52:47 -0800576REGISTER_TYPED_TEST_CASE_P(DwarfDebugFrameTest, Init32, Init32_fde_not_following_cie,
577 Init32_do_not_fail_on_bad_next_entry, Init64,
578 Init64_do_not_fail_on_bad_next_entry, Init64_fde_not_following_cie,
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700579 Init_non_zero_load_bias, Init_version1, Init_version4,
580 GetFdeOffsetFromPc, GetCieFde32, GetCieFde64);
Christopher Ferris61d40972017-06-12 19:14:20 -0700581
582typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes;
583INSTANTIATE_TYPED_TEST_CASE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700584
585} // namespace unwindstack