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