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