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