1 /*
2 * Copyright (C) 2017 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 <ios>
20 #include <vector>
21
22 #include <gtest/gtest.h>
23
24 #include <unwindstack/DwarfMemory.h>
25
26 #include "MemoryFake.h"
27
28 namespace unwindstack {
29
30 class DwarfMemoryTest : public ::testing::Test {
31 protected:
SetUp()32 void SetUp() override {
33 memory_.Clear();
34 dwarf_mem_.reset(new DwarfMemory(&memory_));
35 }
36
37 template <typename AddressType>
38 void GetEncodedSizeTest(uint8_t value, size_t expected);
39 template <typename AddressType>
40 void ReadEncodedValue_omit();
41 template <typename AddressType>
42 void ReadEncodedValue_leb128();
43 template <typename AddressType>
44 void ReadEncodedValue_data1();
45 template <typename AddressType>
46 void ReadEncodedValue_data2();
47 template <typename AddressType>
48 void ReadEncodedValue_data4();
49 template <typename AddressType>
50 void ReadEncodedValue_data8();
51 template <typename AddressType>
52 void ReadEncodedValue_non_zero_adjust();
53 template <typename AddressType>
54 void ReadEncodedValue_overflow();
55 template <typename AddressType>
56 void ReadEncodedValue_high_bit_set();
57 template <typename AddressType>
58 void ReadEncodedValue_all();
59
60 MemoryFake memory_;
61 std::unique_ptr<DwarfMemory> dwarf_mem_;
62 };
63
TEST_F(DwarfMemoryTest,ReadBytes)64 TEST_F(DwarfMemoryTest, ReadBytes) {
65 memory_.SetMemory(0, std::vector<uint8_t>{0x10, 0x18, 0xff, 0xfe});
66
67 uint8_t byte;
68 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
69 ASSERT_EQ(0x10U, byte);
70 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
71 ASSERT_EQ(0x18U, byte);
72 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
73 ASSERT_EQ(0xffU, byte);
74 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
75 ASSERT_EQ(0xfeU, byte);
76 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
77
78 dwarf_mem_->set_cur_offset(2);
79 ASSERT_TRUE(dwarf_mem_->ReadBytes(&byte, 1));
80 ASSERT_EQ(0xffU, byte);
81 ASSERT_EQ(3U, dwarf_mem_->cur_offset());
82 }
83
TEST_F(DwarfMemoryTest,ReadSigned_check)84 TEST_F(DwarfMemoryTest, ReadSigned_check) {
85 uint64_t value;
86
87 // Signed 8 byte reads.
88 memory_.SetData8(0, static_cast<uint8_t>(-10));
89 memory_.SetData8(1, 200);
90 ASSERT_TRUE(dwarf_mem_->ReadSigned<int8_t>(&value));
91 ASSERT_EQ(static_cast<int8_t>(-10), static_cast<int8_t>(value));
92 ASSERT_TRUE(dwarf_mem_->ReadSigned<int8_t>(&value));
93 ASSERT_EQ(static_cast<int8_t>(200), static_cast<int8_t>(value));
94
95 // Signed 16 byte reads.
96 memory_.SetData16(0x10, static_cast<uint16_t>(-1000));
97 memory_.SetData16(0x12, 50100);
98 dwarf_mem_->set_cur_offset(0x10);
99 ASSERT_TRUE(dwarf_mem_->ReadSigned<int16_t>(&value));
100 ASSERT_EQ(static_cast<int16_t>(-1000), static_cast<int16_t>(value));
101 ASSERT_TRUE(dwarf_mem_->ReadSigned<int16_t>(&value));
102 ASSERT_EQ(static_cast<int16_t>(50100), static_cast<int16_t>(value));
103
104 // Signed 32 byte reads.
105 memory_.SetData32(0x100, static_cast<uint32_t>(-1000000000));
106 memory_.SetData32(0x104, 3000000000);
107 dwarf_mem_->set_cur_offset(0x100);
108 ASSERT_TRUE(dwarf_mem_->ReadSigned<int32_t>(&value));
109 ASSERT_EQ(static_cast<int32_t>(-1000000000), static_cast<int32_t>(value));
110 ASSERT_TRUE(dwarf_mem_->ReadSigned<int32_t>(&value));
111 ASSERT_EQ(static_cast<int32_t>(3000000000), static_cast<int32_t>(value));
112
113 // Signed 64 byte reads.
114 memory_.SetData64(0x200, static_cast<uint64_t>(-2000000000000LL));
115 memory_.SetData64(0x208, 5000000000000LL);
116 dwarf_mem_->set_cur_offset(0x200);
117 ASSERT_TRUE(dwarf_mem_->ReadSigned<int64_t>(&value));
118 ASSERT_EQ(static_cast<int64_t>(-2000000000000), static_cast<int64_t>(value));
119 ASSERT_TRUE(dwarf_mem_->ReadSigned<int64_t>(&value));
120 ASSERT_EQ(static_cast<int64_t>(5000000000000), static_cast<int64_t>(value));
121 }
122
TEST_F(DwarfMemoryTest,ReadULEB128)123 TEST_F(DwarfMemoryTest, ReadULEB128) {
124 memory_.SetMemory(0, std::vector<uint8_t>{0x01, 0x80, 0x24, 0xff, 0xc3, 0xff, 0x7f});
125
126 uint64_t value;
127 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
128 ASSERT_EQ(1U, dwarf_mem_->cur_offset());
129 ASSERT_EQ(1U, value);
130
131 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
132 ASSERT_EQ(3U, dwarf_mem_->cur_offset());
133 ASSERT_EQ(0x1200U, value);
134
135 ASSERT_TRUE(dwarf_mem_->ReadULEB128(&value));
136 ASSERT_EQ(7U, dwarf_mem_->cur_offset());
137 ASSERT_EQ(0xfffe1ffU, value);
138 }
139
TEST_F(DwarfMemoryTest,ReadSLEB128)140 TEST_F(DwarfMemoryTest, ReadSLEB128) {
141 memory_.SetMemory(0, std::vector<uint8_t>{0x06, 0x40, 0x82, 0x34, 0x89, 0x64, 0xf9, 0xc3, 0x8f,
142 0x2f, 0xbf, 0xc3, 0xf7, 0x5f});
143
144 int64_t value;
145 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
146 ASSERT_EQ(1U, dwarf_mem_->cur_offset());
147 ASSERT_EQ(6U, value);
148
149 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
150 ASSERT_EQ(2U, dwarf_mem_->cur_offset());
151 ASSERT_EQ(0xffffffffffffffc0ULL, static_cast<uint64_t>(value));
152
153 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
154 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
155 ASSERT_EQ(0x1a02U, value);
156
157 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
158 ASSERT_EQ(6U, dwarf_mem_->cur_offset());
159 ASSERT_EQ(0xfffffffffffff209ULL, static_cast<uint64_t>(value));
160
161 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
162 ASSERT_EQ(10U, dwarf_mem_->cur_offset());
163 ASSERT_EQ(0x5e3e1f9U, value);
164
165 ASSERT_TRUE(dwarf_mem_->ReadSLEB128(&value));
166 ASSERT_EQ(14U, dwarf_mem_->cur_offset());
167 ASSERT_EQ(0xfffffffffbfde1bfULL, static_cast<uint64_t>(value));
168 }
169
170 template <typename AddressType>
GetEncodedSizeTest(uint8_t value,size_t expected)171 void DwarfMemoryTest::GetEncodedSizeTest(uint8_t value, size_t expected) {
172 for (size_t i = 0; i < 16; i++) {
173 uint8_t encoding = (i << 4) | value;
174 ASSERT_EQ(expected, dwarf_mem_->GetEncodedSize<AddressType>(encoding))
175 << "encoding 0x" << std::hex << static_cast<uint32_t>(encoding) << " test value 0x"
176 << static_cast<size_t>(value);
177 }
178 }
179
TEST_F(DwarfMemoryTest,GetEncodedSize_absptr_uint32_t)180 TEST_F(DwarfMemoryTest, GetEncodedSize_absptr_uint32_t) {
181 GetEncodedSizeTest<uint32_t>(0, sizeof(uint32_t));
182 }
183
TEST_F(DwarfMemoryTest,GetEncodedSize_absptr_uint64_t)184 TEST_F(DwarfMemoryTest, GetEncodedSize_absptr_uint64_t) {
185 GetEncodedSizeTest<uint64_t>(0, sizeof(uint64_t));
186 }
187
TEST_F(DwarfMemoryTest,GetEncodedSize_data1)188 TEST_F(DwarfMemoryTest, GetEncodedSize_data1) {
189 // udata1
190 GetEncodedSizeTest<uint32_t>(0x0d, 1);
191 GetEncodedSizeTest<uint64_t>(0x0d, 1);
192
193 // sdata1
194 GetEncodedSizeTest<uint32_t>(0x0e, 1);
195 GetEncodedSizeTest<uint64_t>(0x0e, 1);
196 }
197
TEST_F(DwarfMemoryTest,GetEncodedSize_data2)198 TEST_F(DwarfMemoryTest, GetEncodedSize_data2) {
199 // udata2
200 GetEncodedSizeTest<uint32_t>(0x02, 2);
201 GetEncodedSizeTest<uint64_t>(0x02, 2);
202
203 // sdata2
204 GetEncodedSizeTest<uint32_t>(0x0a, 2);
205 GetEncodedSizeTest<uint64_t>(0x0a, 2);
206 }
207
TEST_F(DwarfMemoryTest,GetEncodedSize_data4)208 TEST_F(DwarfMemoryTest, GetEncodedSize_data4) {
209 // udata4
210 GetEncodedSizeTest<uint32_t>(0x03, 4);
211 GetEncodedSizeTest<uint64_t>(0x03, 4);
212
213 // sdata4
214 GetEncodedSizeTest<uint32_t>(0x0b, 4);
215 GetEncodedSizeTest<uint64_t>(0x0b, 4);
216 }
217
TEST_F(DwarfMemoryTest,GetEncodedSize_data8)218 TEST_F(DwarfMemoryTest, GetEncodedSize_data8) {
219 // udata8
220 GetEncodedSizeTest<uint32_t>(0x04, 8);
221 GetEncodedSizeTest<uint64_t>(0x04, 8);
222
223 // sdata8
224 GetEncodedSizeTest<uint32_t>(0x0c, 8);
225 GetEncodedSizeTest<uint64_t>(0x0c, 8);
226 }
227
TEST_F(DwarfMemoryTest,GetEncodedSize_unknown)228 TEST_F(DwarfMemoryTest, GetEncodedSize_unknown) {
229 GetEncodedSizeTest<uint32_t>(0x01, 0);
230 GetEncodedSizeTest<uint64_t>(0x01, 0);
231
232 GetEncodedSizeTest<uint32_t>(0x09, 0);
233 GetEncodedSizeTest<uint64_t>(0x09, 0);
234
235 GetEncodedSizeTest<uint32_t>(0x0f, 0);
236 GetEncodedSizeTest<uint64_t>(0x0f, 0);
237 }
238
239 template <typename AddressType>
ReadEncodedValue_omit()240 void DwarfMemoryTest::ReadEncodedValue_omit() {
241 uint64_t value = 123;
242 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0xff, &value));
243 ASSERT_EQ(0U, value);
244 }
245
TEST_F(DwarfMemoryTest,ReadEncodedValue_omit_uint32_t)246 TEST_F(DwarfMemoryTest, ReadEncodedValue_omit_uint32_t) {
247 ReadEncodedValue_omit<uint32_t>();
248 }
249
TEST_F(DwarfMemoryTest,ReadEncodedValue_omit_uint64_t)250 TEST_F(DwarfMemoryTest, ReadEncodedValue_omit_uint64_t) {
251 ReadEncodedValue_omit<uint64_t>();
252 }
253
TEST_F(DwarfMemoryTest,ReadEncodedValue_absptr_uint32_t)254 TEST_F(DwarfMemoryTest, ReadEncodedValue_absptr_uint32_t) {
255 uint64_t value = 100;
256 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x00, &value));
257
258 memory_.SetData32(0, 0x12345678);
259
260 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x00, &value));
261 ASSERT_EQ(4U, dwarf_mem_->cur_offset());
262 ASSERT_EQ(0x12345678U, value);
263 }
264
TEST_F(DwarfMemoryTest,ReadEncodedValue_absptr_uint64_t)265 TEST_F(DwarfMemoryTest, ReadEncodedValue_absptr_uint64_t) {
266 uint64_t value = 100;
267 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x00, &value));
268
269 memory_.SetData64(0, 0x12345678f1f2f3f4ULL);
270
271 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x00, &value));
272 ASSERT_EQ(8U, dwarf_mem_->cur_offset());
273 ASSERT_EQ(0x12345678f1f2f3f4ULL, value);
274 }
275
TEST_F(DwarfMemoryTest,ReadEncodedValue_aligned_uint32_t)276 TEST_F(DwarfMemoryTest, ReadEncodedValue_aligned_uint32_t) {
277 uint64_t value = 100;
278 dwarf_mem_->set_cur_offset(1);
279 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x50, &value));
280
281 memory_.SetData32(4, 0x12345678);
282
283 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint32_t>(0x50, &value));
284 ASSERT_EQ(8U, dwarf_mem_->cur_offset());
285 ASSERT_EQ(0x12345678U, value);
286 }
287
TEST_F(DwarfMemoryTest,ReadEncodedValue_aligned_uint64_t)288 TEST_F(DwarfMemoryTest, ReadEncodedValue_aligned_uint64_t) {
289 uint64_t value = 100;
290 dwarf_mem_->set_cur_offset(1);
291 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x50, &value));
292
293 memory_.SetData64(8, 0x12345678f1f2f3f4ULL);
294
295 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<uint64_t>(0x50, &value));
296 ASSERT_EQ(16U, dwarf_mem_->cur_offset());
297 ASSERT_EQ(0x12345678f1f2f3f4ULL, value);
298 }
299
300 template <typename AddressType>
ReadEncodedValue_leb128()301 void DwarfMemoryTest::ReadEncodedValue_leb128() {
302 memory_.SetMemory(0, std::vector<uint8_t>{0x80, 0x42});
303
304 uint64_t value = 100;
305 // uleb128
306 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x01, &value));
307 ASSERT_EQ(0x2100U, value);
308
309 dwarf_mem_->set_cur_offset(0);
310 // sleb128
311 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x09, &value));
312 ASSERT_EQ(0xffffffffffffe100ULL, value);
313 }
314
TEST_F(DwarfMemoryTest,ReadEncodedValue_leb128_uint32_t)315 TEST_F(DwarfMemoryTest, ReadEncodedValue_leb128_uint32_t) {
316 ReadEncodedValue_leb128<uint32_t>();
317 }
318
TEST_F(DwarfMemoryTest,ReadEncodedValue_leb128_uint64_t)319 TEST_F(DwarfMemoryTest, ReadEncodedValue_leb128_uint64_t) {
320 ReadEncodedValue_leb128<uint64_t>();
321 }
322
323 template <typename AddressType>
ReadEncodedValue_data1()324 void DwarfMemoryTest::ReadEncodedValue_data1() {
325 memory_.SetData8(0, 0xe0);
326
327 uint64_t value = 0;
328 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0d, &value));
329 ASSERT_EQ(0xe0U, value);
330
331 dwarf_mem_->set_cur_offset(0);
332 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0e, &value));
333 ASSERT_EQ(0xffffffffffffffe0ULL, value);
334 }
335
TEST_F(DwarfMemoryTest,ReadEncodedValue_data1_uint32_t)336 TEST_F(DwarfMemoryTest, ReadEncodedValue_data1_uint32_t) {
337 ReadEncodedValue_data1<uint32_t>();
338 }
339
TEST_F(DwarfMemoryTest,ReadEncodedValue_data1_uint64_t)340 TEST_F(DwarfMemoryTest, ReadEncodedValue_data1_uint64_t) {
341 ReadEncodedValue_data1<uint64_t>();
342 }
343
344 template <typename AddressType>
ReadEncodedValue_data2()345 void DwarfMemoryTest::ReadEncodedValue_data2() {
346 memory_.SetData16(0, 0xe000);
347
348 uint64_t value = 0;
349 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x02, &value));
350 ASSERT_EQ(0xe000U, value);
351
352 dwarf_mem_->set_cur_offset(0);
353 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0a, &value));
354 ASSERT_EQ(0xffffffffffffe000ULL, value);
355 }
356
TEST_F(DwarfMemoryTest,ReadEncodedValue_data2_uint32_t)357 TEST_F(DwarfMemoryTest, ReadEncodedValue_data2_uint32_t) {
358 ReadEncodedValue_data2<uint32_t>();
359 }
360
TEST_F(DwarfMemoryTest,ReadEncodedValue_data2_uint64_t)361 TEST_F(DwarfMemoryTest, ReadEncodedValue_data2_uint64_t) {
362 ReadEncodedValue_data2<uint64_t>();
363 }
364
365 template <typename AddressType>
ReadEncodedValue_data4()366 void DwarfMemoryTest::ReadEncodedValue_data4() {
367 memory_.SetData32(0, 0xe0000000);
368
369 uint64_t value = 0;
370 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x03, &value));
371 ASSERT_EQ(0xe0000000U, value);
372
373 dwarf_mem_->set_cur_offset(0);
374 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0b, &value));
375 ASSERT_EQ(0xffffffffe0000000ULL, value);
376 }
377
TEST_F(DwarfMemoryTest,ReadEncodedValue_data4_uint32_t)378 TEST_F(DwarfMemoryTest, ReadEncodedValue_data4_uint32_t) {
379 ReadEncodedValue_data4<uint32_t>();
380 }
381
TEST_F(DwarfMemoryTest,ReadEncodedValue_data4_uint64_t)382 TEST_F(DwarfMemoryTest, ReadEncodedValue_data4_uint64_t) {
383 ReadEncodedValue_data4<uint64_t>();
384 }
385
386 template <typename AddressType>
ReadEncodedValue_data8()387 void DwarfMemoryTest::ReadEncodedValue_data8() {
388 memory_.SetData64(0, 0xe000000000000000ULL);
389
390 uint64_t value = 0;
391 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x04, &value));
392 ASSERT_EQ(0xe000000000000000ULL, value);
393
394 dwarf_mem_->set_cur_offset(0);
395 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x0c, &value));
396 ASSERT_EQ(0xe000000000000000ULL, value);
397 }
398
TEST_F(DwarfMemoryTest,ReadEncodedValue_data8_uint32_t)399 TEST_F(DwarfMemoryTest, ReadEncodedValue_data8_uint32_t) {
400 ReadEncodedValue_data8<uint32_t>();
401 }
402
TEST_F(DwarfMemoryTest,ReadEncodedValue_data8_uint64_t)403 TEST_F(DwarfMemoryTest, ReadEncodedValue_data8_uint64_t) {
404 ReadEncodedValue_data8<uint64_t>();
405 }
406
407 template <typename AddressType>
ReadEncodedValue_non_zero_adjust()408 void DwarfMemoryTest::ReadEncodedValue_non_zero_adjust() {
409 memory_.SetData64(0, 0xe000000000000000ULL);
410
411 uint64_t value = 0;
412 dwarf_mem_->set_pc_offset(0x2000);
413 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0x14, &value));
414 ASSERT_EQ(0xe000000000002000ULL, value);
415 }
416
TEST_F(DwarfMemoryTest,ReadEncodedValue_non_zero_adjust_uint32_t)417 TEST_F(DwarfMemoryTest, ReadEncodedValue_non_zero_adjust_uint32_t) {
418 ReadEncodedValue_non_zero_adjust<uint32_t>();
419 }
420
TEST_F(DwarfMemoryTest,ReadEncodedValue_non_zero_adjust_uint64_t)421 TEST_F(DwarfMemoryTest, ReadEncodedValue_non_zero_adjust_uint64_t) {
422 ReadEncodedValue_non_zero_adjust<uint64_t>();
423 }
424
425 template <typename AddressType>
ReadEncodedValue_overflow()426 void DwarfMemoryTest::ReadEncodedValue_overflow() {
427 memory_.SetData64(0, 0);
428
429 uint64_t value = 0;
430 dwarf_mem_->set_cur_offset(UINT64_MAX);
431 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<AddressType>(0x50, &value));
432 }
433
TEST_F(DwarfMemoryTest,ReadEncodedValue_overflow_uint32_t)434 TEST_F(DwarfMemoryTest, ReadEncodedValue_overflow_uint32_t) {
435 ReadEncodedValue_overflow<uint32_t>();
436 }
437
TEST_F(DwarfMemoryTest,ReadEncodedValue_overflow_uint64_t)438 TEST_F(DwarfMemoryTest, ReadEncodedValue_overflow_uint64_t) {
439 ReadEncodedValue_overflow<uint64_t>();
440 }
441
442 template <typename AddressType>
ReadEncodedValue_high_bit_set()443 void DwarfMemoryTest::ReadEncodedValue_high_bit_set() {
444 uint64_t value;
445 memory_.SetData32(0, 0x15234);
446 ASSERT_FALSE(dwarf_mem_->ReadEncodedValue<AddressType>(0xc3, &value));
447
448 dwarf_mem_->set_func_offset(0x60000);
449 dwarf_mem_->set_cur_offset(0);
450 ASSERT_TRUE(dwarf_mem_->ReadEncodedValue<AddressType>(0xc3, &value));
451 ASSERT_EQ(0x75234U, value);
452 }
453
TEST_F(DwarfMemoryTest,ReadEncodedValue_high_bit_set_uint32_t)454 TEST_F(DwarfMemoryTest, ReadEncodedValue_high_bit_set_uint32_t) {
455 ReadEncodedValue_high_bit_set<uint32_t>();
456 }
457
TEST_F(DwarfMemoryTest,ReadEncodedValue_high_bit_set_uint64_t)458 TEST_F(DwarfMemoryTest, ReadEncodedValue_high_bit_set_uint64_t) {
459 ReadEncodedValue_high_bit_set<uint64_t>();
460 }
461
462 template <typename AddressType>
ReadEncodedValue_all()463 void DwarfMemoryTest::ReadEncodedValue_all() {
464 MemoryFakeAlwaysReadZero memory;
465 DwarfMemory dwarf_mem(&memory);
466
467 for (size_t i = 0; i <= 0xff; i++) {
468 uint64_t value;
469 if (dwarf_mem.ReadEncodedValue<AddressType>(static_cast<uint8_t>(i), &value)) {
470 ASSERT_EQ(0U, value);
471 }
472 }
473 }
474
TEST_F(DwarfMemoryTest,ReadEncodedValue_all_uint32_t)475 TEST_F(DwarfMemoryTest, ReadEncodedValue_all_uint32_t) {
476 ReadEncodedValue_all<uint32_t>();
477 }
478
TEST_F(DwarfMemoryTest,ReadEncodedValue_all_uint64_t)479 TEST_F(DwarfMemoryTest, ReadEncodedValue_all_uint64_t) {
480 ReadEncodedValue_all<uint64_t>();
481 }
482
TEST_F(DwarfMemoryTest,AdjustEncodedValue_absptr)483 TEST_F(DwarfMemoryTest, AdjustEncodedValue_absptr) {
484 uint64_t value = 0x1234;
485 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x00, &value));
486 ASSERT_EQ(0x1234U, value);
487 }
488
TEST_F(DwarfMemoryTest,AdjustEncodedValue_pcrel)489 TEST_F(DwarfMemoryTest, AdjustEncodedValue_pcrel) {
490 uint64_t value = 0x1234;
491 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
492
493 dwarf_mem_->set_pc_offset(0x2000);
494 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
495 ASSERT_EQ(0x3234U, value);
496
497 dwarf_mem_->set_pc_offset(static_cast<uint64_t>(-4));
498 value = 0x1234;
499 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x10, &value));
500 ASSERT_EQ(0x1230U, value);
501 }
502
TEST_F(DwarfMemoryTest,AdjustEncodedValue_textrel)503 TEST_F(DwarfMemoryTest, AdjustEncodedValue_textrel) {
504 uint64_t value = 0x8234;
505 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
506
507 dwarf_mem_->set_text_offset(0x1000);
508 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
509 ASSERT_EQ(0x9234U, value);
510
511 dwarf_mem_->set_text_offset(static_cast<uint64_t>(-16));
512 value = 0x8234;
513 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x20, &value));
514 ASSERT_EQ(0x8224U, value);
515 }
516
TEST_F(DwarfMemoryTest,AdjustEncodedValue_datarel)517 TEST_F(DwarfMemoryTest, AdjustEncodedValue_datarel) {
518 uint64_t value = 0xb234;
519 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
520
521 dwarf_mem_->set_data_offset(0x1200);
522 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
523 ASSERT_EQ(0xc434U, value);
524
525 dwarf_mem_->set_data_offset(static_cast<uint64_t>(-256));
526 value = 0xb234;
527 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x30, &value));
528 ASSERT_EQ(0xb134U, value);
529 }
530
TEST_F(DwarfMemoryTest,AdjustEncodedValue_funcrel)531 TEST_F(DwarfMemoryTest, AdjustEncodedValue_funcrel) {
532 uint64_t value = 0x15234;
533 ASSERT_FALSE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
534
535 dwarf_mem_->set_func_offset(0x60000);
536 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
537 ASSERT_EQ(0x75234U, value);
538
539 dwarf_mem_->set_func_offset(static_cast<uint64_t>(-4096));
540 value = 0x15234;
541 ASSERT_TRUE(dwarf_mem_->AdjustEncodedValue(0x40, &value));
542 ASSERT_EQ(0x14234U, value);
543 }
544
545 } // namespace unwindstack
546