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