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