1 // Copyright (c) 2010, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31 
32 // bytereader_unittest.cc: Unit tests for dwarf2reader::ByteReader
33 
34 #include <string>
35 
36 #include "breakpad_googletest_includes.h"
37 #include "common/dwarf/bytereader.h"
38 #include "common/dwarf/bytereader-inl.h"
39 #include "common/dwarf/cfi_assembler.h"
40 #include "common/using_std_string.h"
41 
42 using dwarf2reader::ByteReader;
43 using dwarf2reader::DwarfPointerEncoding;
44 using dwarf2reader::ENDIANNESS_BIG;
45 using dwarf2reader::ENDIANNESS_LITTLE;
46 using google_breakpad::CFISection;
47 using google_breakpad::test_assembler::Label;
48 using google_breakpad::test_assembler::kBigEndian;
49 using google_breakpad::test_assembler::kLittleEndian;
50 using google_breakpad::test_assembler::Section;
51 using testing::Test;
52 
53 struct ReaderFixture {
54   string contents;
55   size_t pointer_size;
56 };
57 
58 class Reader: public ReaderFixture, public Test { };
59 class ReaderDeathTest: public ReaderFixture, public Test { };
60 
TEST_F(Reader,SimpleConstructor)61 TEST_F(Reader, SimpleConstructor) {
62   ByteReader reader(ENDIANNESS_BIG);
63   reader.SetAddressSize(4);
64   CFISection section(kBigEndian, 4);
65   section
66     .D8(0xc0)
67     .D16(0xcf0d)
68     .D32(0x96fdd219)
69     .D64(0xbbf55fef0825f117ULL)
70     .ULEB128(0xa0927048ba8121afULL)
71     .LEB128(-0x4f337badf4483f83LL)
72     .D32(0xfec319c9);
73   ASSERT_TRUE(section.GetContents(&contents));
74   const char *data = contents.data();
75   EXPECT_EQ(0xc0U, reader.ReadOneByte(data));
76   EXPECT_EQ(0xcf0dU, reader.ReadTwoBytes(data + 1));
77   EXPECT_EQ(0x96fdd219U, reader.ReadFourBytes(data + 3));
78   EXPECT_EQ(0xbbf55fef0825f117ULL, reader.ReadEightBytes(data + 7));
79   size_t leb128_size;
80   EXPECT_EQ(0xa0927048ba8121afULL,
81             reader.ReadUnsignedLEB128(data + 15, &leb128_size));
82   EXPECT_EQ(10U, leb128_size);
83   EXPECT_EQ(-0x4f337badf4483f83LL,
84             reader.ReadSignedLEB128(data + 25, &leb128_size));
85   EXPECT_EQ(10U, leb128_size);
86   EXPECT_EQ(0xfec319c9, reader.ReadAddress(data + 35));
87 }
88 
TEST_F(Reader,ValidEncodings)89 TEST_F(Reader, ValidEncodings) {
90   ByteReader reader(ENDIANNESS_LITTLE);
91   EXPECT_TRUE(reader.ValidEncoding(
92       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_absptr)));
93   EXPECT_TRUE(reader.ValidEncoding(
94       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_omit)));
95   EXPECT_TRUE(reader.ValidEncoding(
96       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_aligned)));
97   EXPECT_TRUE(reader.ValidEncoding(
98       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_uleb128)));
99   EXPECT_TRUE(reader.ValidEncoding(
100       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata2)));
101   EXPECT_TRUE(reader.ValidEncoding(
102       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata4)));
103   EXPECT_TRUE(reader.ValidEncoding(
104       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata8)));
105   EXPECT_TRUE(reader.ValidEncoding(
106       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sleb128)));
107   EXPECT_TRUE(reader.ValidEncoding(
108       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata2)));
109   EXPECT_TRUE(reader.ValidEncoding(
110       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata4)));
111   EXPECT_TRUE(reader.ValidEncoding(
112       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata8)));
113   EXPECT_TRUE(reader.ValidEncoding(
114       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_absptr |
115                            dwarf2reader::DW_EH_PE_pcrel)));
116   EXPECT_TRUE(reader.ValidEncoding(
117       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_uleb128 |
118                            dwarf2reader::DW_EH_PE_pcrel)));
119   EXPECT_TRUE(reader.ValidEncoding(
120       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata2 |
121                            dwarf2reader::DW_EH_PE_pcrel)));
122   EXPECT_TRUE(reader.ValidEncoding(
123       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata4 |
124                            dwarf2reader::DW_EH_PE_pcrel)));
125   EXPECT_TRUE(reader.ValidEncoding(
126       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata8 |
127                            dwarf2reader::DW_EH_PE_pcrel)));
128   EXPECT_TRUE(reader.ValidEncoding(
129       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sleb128 |
130                            dwarf2reader::DW_EH_PE_pcrel)));
131   EXPECT_TRUE(reader.ValidEncoding(
132       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata2 |
133                            dwarf2reader::DW_EH_PE_pcrel)));
134   EXPECT_TRUE(reader.ValidEncoding(
135       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata4 |
136                            dwarf2reader::DW_EH_PE_pcrel)));
137   EXPECT_TRUE(reader.ValidEncoding(
138       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata8 |
139                            dwarf2reader::DW_EH_PE_pcrel)));
140   EXPECT_TRUE(reader.ValidEncoding(
141       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_absptr |
142                            dwarf2reader::DW_EH_PE_textrel)));
143   EXPECT_TRUE(reader.ValidEncoding(
144       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_uleb128 |
145                            dwarf2reader::DW_EH_PE_textrel)));
146   EXPECT_TRUE(reader.ValidEncoding(
147       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata2 |
148                            dwarf2reader::DW_EH_PE_textrel)));
149   EXPECT_TRUE(reader.ValidEncoding(
150       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata4 |
151                            dwarf2reader::DW_EH_PE_textrel)));
152   EXPECT_TRUE(reader.ValidEncoding(
153       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata8 |
154                            dwarf2reader::DW_EH_PE_textrel)));
155   EXPECT_TRUE(reader.ValidEncoding(
156       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sleb128 |
157                            dwarf2reader::DW_EH_PE_textrel)));
158   EXPECT_TRUE(reader.ValidEncoding(
159       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata2 |
160                            dwarf2reader::DW_EH_PE_textrel)));
161   EXPECT_TRUE(reader.ValidEncoding(
162       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata4 |
163                            dwarf2reader::DW_EH_PE_textrel)));
164   EXPECT_TRUE(reader.ValidEncoding(
165       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata8 |
166                            dwarf2reader::DW_EH_PE_textrel)));
167   EXPECT_TRUE(reader.ValidEncoding(
168       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_absptr |
169                            dwarf2reader::DW_EH_PE_datarel)));
170   EXPECT_TRUE(reader.ValidEncoding(
171       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_uleb128 |
172                            dwarf2reader::DW_EH_PE_datarel)));
173   EXPECT_TRUE(reader.ValidEncoding(
174       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata2 |
175                            dwarf2reader::DW_EH_PE_datarel)));
176   EXPECT_TRUE(reader.ValidEncoding(
177       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata4 |
178                            dwarf2reader::DW_EH_PE_datarel)));
179   EXPECT_TRUE(reader.ValidEncoding(
180       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata8 |
181                            dwarf2reader::DW_EH_PE_datarel)));
182   EXPECT_TRUE(reader.ValidEncoding(
183       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sleb128 |
184                            dwarf2reader::DW_EH_PE_datarel)));
185   EXPECT_TRUE(reader.ValidEncoding(
186       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata2 |
187                            dwarf2reader::DW_EH_PE_datarel)));
188   EXPECT_TRUE(reader.ValidEncoding(
189       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata4 |
190                            dwarf2reader::DW_EH_PE_datarel)));
191   EXPECT_TRUE(reader.ValidEncoding(
192       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata8 |
193                            dwarf2reader::DW_EH_PE_datarel)));
194   EXPECT_TRUE(reader.ValidEncoding(
195       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_absptr |
196                            dwarf2reader::DW_EH_PE_funcrel)));
197   EXPECT_TRUE(reader.ValidEncoding(
198       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_uleb128 |
199                            dwarf2reader::DW_EH_PE_funcrel)));
200   EXPECT_TRUE(reader.ValidEncoding(
201       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata2 |
202                            dwarf2reader::DW_EH_PE_funcrel)));
203   EXPECT_TRUE(reader.ValidEncoding(
204       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata4 |
205                            dwarf2reader::DW_EH_PE_funcrel)));
206   EXPECT_TRUE(reader.ValidEncoding(
207       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_udata8 |
208                            dwarf2reader::DW_EH_PE_funcrel)));
209   EXPECT_TRUE(reader.ValidEncoding(
210       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sleb128 |
211                            dwarf2reader::DW_EH_PE_funcrel)));
212   EXPECT_TRUE(reader.ValidEncoding(
213       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata2 |
214                            dwarf2reader::DW_EH_PE_funcrel)));
215   EXPECT_TRUE(reader.ValidEncoding(
216       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata4 |
217                            dwarf2reader::DW_EH_PE_funcrel)));
218   EXPECT_TRUE(reader.ValidEncoding(
219       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_sdata8 |
220                            dwarf2reader::DW_EH_PE_funcrel)));
221   EXPECT_TRUE(reader.ValidEncoding(
222       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
223                            dwarf2reader::DW_EH_PE_absptr |
224                            dwarf2reader::DW_EH_PE_pcrel)));
225   EXPECT_TRUE(reader.ValidEncoding(
226       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
227                            dwarf2reader::DW_EH_PE_uleb128 |
228                            dwarf2reader::DW_EH_PE_pcrel)));
229   EXPECT_TRUE(reader.ValidEncoding(
230       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
231                            dwarf2reader::DW_EH_PE_udata2 |
232                            dwarf2reader::DW_EH_PE_pcrel)));
233   EXPECT_TRUE(reader.ValidEncoding(
234       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
235                            dwarf2reader::DW_EH_PE_udata4 |
236                            dwarf2reader::DW_EH_PE_pcrel)));
237   EXPECT_TRUE(reader.ValidEncoding(
238       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
239                            dwarf2reader::DW_EH_PE_udata8 |
240                            dwarf2reader::DW_EH_PE_pcrel)));
241   EXPECT_TRUE(reader.ValidEncoding(
242       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
243                            dwarf2reader::DW_EH_PE_sleb128 |
244                            dwarf2reader::DW_EH_PE_pcrel)));
245   EXPECT_TRUE(reader.ValidEncoding(
246       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
247                            dwarf2reader::DW_EH_PE_sdata2 |
248                            dwarf2reader::DW_EH_PE_pcrel)));
249   EXPECT_TRUE(reader.ValidEncoding(
250       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
251                            dwarf2reader::DW_EH_PE_sdata4 |
252                            dwarf2reader::DW_EH_PE_pcrel)));
253   EXPECT_TRUE(reader.ValidEncoding(
254       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
255                            dwarf2reader::DW_EH_PE_sdata8 |
256                            dwarf2reader::DW_EH_PE_pcrel)));
257   EXPECT_TRUE(reader.ValidEncoding(
258       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
259                            dwarf2reader::DW_EH_PE_absptr |
260                            dwarf2reader::DW_EH_PE_textrel)));
261   EXPECT_TRUE(reader.ValidEncoding(
262       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
263                            dwarf2reader::DW_EH_PE_uleb128 |
264                            dwarf2reader::DW_EH_PE_textrel)));
265   EXPECT_TRUE(reader.ValidEncoding(
266       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
267                            dwarf2reader::DW_EH_PE_udata2 |
268                            dwarf2reader::DW_EH_PE_textrel)));
269   EXPECT_TRUE(reader.ValidEncoding(
270       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
271                            dwarf2reader::DW_EH_PE_udata4 |
272                            dwarf2reader::DW_EH_PE_textrel)));
273   EXPECT_TRUE(reader.ValidEncoding(
274       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
275                            dwarf2reader::DW_EH_PE_udata8 |
276                            dwarf2reader::DW_EH_PE_textrel)));
277   EXPECT_TRUE(reader.ValidEncoding(
278       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
279                            dwarf2reader::DW_EH_PE_sleb128 |
280                            dwarf2reader::DW_EH_PE_textrel)));
281   EXPECT_TRUE(reader.ValidEncoding(
282       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
283                            dwarf2reader::DW_EH_PE_sdata2 |
284                            dwarf2reader::DW_EH_PE_textrel)));
285   EXPECT_TRUE(reader.ValidEncoding(
286       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
287                            dwarf2reader::DW_EH_PE_sdata4 |
288                            dwarf2reader::DW_EH_PE_textrel)));
289   EXPECT_TRUE(reader.ValidEncoding(
290       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
291                            dwarf2reader::DW_EH_PE_sdata8 |
292                            dwarf2reader::DW_EH_PE_textrel)));
293   EXPECT_TRUE(reader.ValidEncoding(
294       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
295                            dwarf2reader::DW_EH_PE_absptr |
296                            dwarf2reader::DW_EH_PE_datarel)));
297   EXPECT_TRUE(reader.ValidEncoding(
298       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
299                            dwarf2reader::DW_EH_PE_uleb128 |
300                            dwarf2reader::DW_EH_PE_datarel)));
301   EXPECT_TRUE(reader.ValidEncoding(
302       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
303                            dwarf2reader::DW_EH_PE_udata2 |
304                            dwarf2reader::DW_EH_PE_datarel)));
305   EXPECT_TRUE(reader.ValidEncoding(
306       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
307                            dwarf2reader::DW_EH_PE_udata4 |
308                            dwarf2reader::DW_EH_PE_datarel)));
309   EXPECT_TRUE(reader.ValidEncoding(
310       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
311                            dwarf2reader::DW_EH_PE_udata8 |
312                            dwarf2reader::DW_EH_PE_datarel)));
313   EXPECT_TRUE(reader.ValidEncoding(
314       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
315                            dwarf2reader::DW_EH_PE_sleb128 |
316                            dwarf2reader::DW_EH_PE_datarel)));
317   EXPECT_TRUE(reader.ValidEncoding(
318       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
319                            dwarf2reader::DW_EH_PE_sdata2 |
320                            dwarf2reader::DW_EH_PE_datarel)));
321   EXPECT_TRUE(reader.ValidEncoding(
322       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
323                            dwarf2reader::DW_EH_PE_sdata4 |
324                            dwarf2reader::DW_EH_PE_datarel)));
325   EXPECT_TRUE(reader.ValidEncoding(
326       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
327                            dwarf2reader::DW_EH_PE_sdata8 |
328                            dwarf2reader::DW_EH_PE_datarel)));
329   EXPECT_TRUE(reader.ValidEncoding(
330       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
331                            dwarf2reader::DW_EH_PE_absptr |
332                            dwarf2reader::DW_EH_PE_funcrel)));
333   EXPECT_TRUE(reader.ValidEncoding(
334       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
335                            dwarf2reader::DW_EH_PE_uleb128 |
336                            dwarf2reader::DW_EH_PE_funcrel)));
337   EXPECT_TRUE(reader.ValidEncoding(
338       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
339                            dwarf2reader::DW_EH_PE_udata2 |
340                            dwarf2reader::DW_EH_PE_funcrel)));
341   EXPECT_TRUE(reader.ValidEncoding(
342       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
343                            dwarf2reader::DW_EH_PE_udata4 |
344                            dwarf2reader::DW_EH_PE_funcrel)));
345   EXPECT_TRUE(reader.ValidEncoding(
346       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
347                            dwarf2reader::DW_EH_PE_udata8 |
348                            dwarf2reader::DW_EH_PE_funcrel)));
349   EXPECT_TRUE(reader.ValidEncoding(
350       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
351                            dwarf2reader::DW_EH_PE_sleb128 |
352                            dwarf2reader::DW_EH_PE_funcrel)));
353   EXPECT_TRUE(reader.ValidEncoding(
354       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
355                            dwarf2reader::DW_EH_PE_sdata2 |
356                            dwarf2reader::DW_EH_PE_funcrel)));
357   EXPECT_TRUE(reader.ValidEncoding(
358       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
359                            dwarf2reader::DW_EH_PE_sdata4 |
360                            dwarf2reader::DW_EH_PE_funcrel)));
361   EXPECT_TRUE(reader.ValidEncoding(
362       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_indirect |
363                            dwarf2reader::DW_EH_PE_sdata8 |
364                            dwarf2reader::DW_EH_PE_funcrel)));
365 
366   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x05)));
367   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x07)));
368   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x0d)));
369   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x0f)));
370   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x51)));
371   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x60)));
372   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0x70)));
373   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0xf0)));
374   EXPECT_FALSE(reader.ValidEncoding(DwarfPointerEncoding(0xd0)));
375 }
376 
TEST_F(ReaderDeathTest,DW_EH_PE_omit)377 TEST_F(ReaderDeathTest, DW_EH_PE_omit) {
378   static const char data[1] = { 42 };
379   ByteReader reader(ENDIANNESS_BIG);
380   reader.SetAddressSize(4);
381   EXPECT_DEATH(reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_omit,
382                                          &pointer_size),
383                "encoding != DW_EH_PE_omit");
384 }
385 
TEST_F(Reader,DW_EH_PE_absptr4)386 TEST_F(Reader, DW_EH_PE_absptr4) {
387   static const char data[] = { 0x27, 0x57, 0xea, 0x40 };
388   ByteReader reader(ENDIANNESS_LITTLE);
389   reader.SetAddressSize(4);
390   EXPECT_EQ(0x40ea5727U,
391             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_absptr,
392                                       &pointer_size));
393   EXPECT_EQ(4U, pointer_size);
394 }
395 
TEST_F(Reader,DW_EH_PE_absptr8)396 TEST_F(Reader, DW_EH_PE_absptr8) {
397   static const char data[] = {
398     0x60, 0x27, 0x57, 0xea, 0x40, 0xc2, 0x98, 0x05, 0x01, 0x50
399   };
400   ByteReader reader(ENDIANNESS_LITTLE);
401   reader.SetAddressSize(8);
402   EXPECT_EQ(0x010598c240ea5727ULL,
403             reader.ReadEncodedPointer(data + 1, dwarf2reader::DW_EH_PE_absptr,
404                                       &pointer_size));
405   EXPECT_EQ(8U, pointer_size);
406 }
407 
TEST_F(Reader,DW_EH_PE_uleb128)408 TEST_F(Reader, DW_EH_PE_uleb128) {
409   static const char data[] = { 0x81, 0x84, 0x4c };
410   ByteReader reader(ENDIANNESS_LITTLE);
411   reader.SetAddressSize(4);
412   EXPECT_EQ(0x130201U,
413             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_uleb128,
414                                       &pointer_size));
415   EXPECT_EQ(3U, pointer_size);
416 }
417 
TEST_F(Reader,DW_EH_PE_udata2)418 TEST_F(Reader, DW_EH_PE_udata2) {
419   static const char data[] = { 0xf4, 0x8d };
420   ByteReader reader(ENDIANNESS_BIG);
421   reader.SetAddressSize(4);
422   EXPECT_EQ(0xf48dU,
423             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_udata2,
424                                       &pointer_size));
425   EXPECT_EQ(2U, pointer_size);
426 }
427 
TEST_F(Reader,DW_EH_PE_udata4)428 TEST_F(Reader, DW_EH_PE_udata4) {
429   static const char data[] = { 0xb2, 0x68, 0xa5, 0x62, 0x8f, 0x8b };
430   ByteReader reader(ENDIANNESS_BIG);
431   reader.SetAddressSize(8);
432   EXPECT_EQ(0xa5628f8b,
433             reader.ReadEncodedPointer(data + 2, dwarf2reader::DW_EH_PE_udata4,
434                                       &pointer_size));
435   EXPECT_EQ(4U, pointer_size);
436 }
437 
TEST_F(Reader,DW_EH_PE_udata8Addr8)438 TEST_F(Reader, DW_EH_PE_udata8Addr8) {
439   static const char data[] = {
440     0x27, 0x04, 0x73, 0x04, 0x69, 0x9f, 0x19, 0xed, 0x8f, 0xfe
441   };
442   ByteReader reader(ENDIANNESS_LITTLE);
443   reader.SetAddressSize(8);
444   EXPECT_EQ(0x8fed199f69047304ULL,
445             reader.ReadEncodedPointer(data + 1, dwarf2reader::DW_EH_PE_udata8,
446                                         &pointer_size));
447   EXPECT_EQ(8U, pointer_size);
448 }
449 
TEST_F(Reader,DW_EH_PE_udata8Addr4)450 TEST_F(Reader, DW_EH_PE_udata8Addr4) {
451   static const char data[] = {
452     0x27, 0x04, 0x73, 0x04, 0x69, 0x9f, 0x19, 0xed, 0x8f, 0xfe
453   };
454   ByteReader reader(ENDIANNESS_LITTLE);
455   reader.SetAddressSize(4);
456   EXPECT_EQ(0x69047304ULL,
457             reader.ReadEncodedPointer(data + 1, dwarf2reader::DW_EH_PE_udata8,
458                                         &pointer_size));
459   EXPECT_EQ(8U, pointer_size);
460 }
461 
TEST_F(Reader,DW_EH_PE_sleb128)462 TEST_F(Reader, DW_EH_PE_sleb128) {
463   static const char data[] = { 0x42, 0xff, 0xfb, 0x73 };
464   ByteReader reader(ENDIANNESS_BIG);
465   reader.SetAddressSize(4);
466   EXPECT_EQ(-0x030201U & 0xffffffff,
467             reader.ReadEncodedPointer(data + 1, dwarf2reader::DW_EH_PE_sleb128,
468                                         &pointer_size));
469   EXPECT_EQ(3U, pointer_size);
470 }
471 
TEST_F(Reader,DW_EH_PE_sdata2)472 TEST_F(Reader, DW_EH_PE_sdata2) {
473   static const char data[] = { 0xb9, 0xbf };
474   ByteReader reader(ENDIANNESS_LITTLE);
475   reader.SetAddressSize(8);
476   EXPECT_EQ(0xffffffffffffbfb9ULL,
477             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_sdata2,
478                                         &pointer_size));
479   EXPECT_EQ(2U, pointer_size);
480 }
481 
TEST_F(Reader,DW_EH_PE_sdata4)482 TEST_F(Reader, DW_EH_PE_sdata4) {
483   static const char data[] = { 0xa0, 0xca, 0xf2, 0xb8, 0xc2, 0xad };
484   ByteReader reader(ENDIANNESS_LITTLE);
485   reader.SetAddressSize(8);
486   EXPECT_EQ(0xffffffffadc2b8f2ULL,
487             reader.ReadEncodedPointer(data + 2, dwarf2reader::DW_EH_PE_sdata4,
488                                         &pointer_size));
489   EXPECT_EQ(4U, pointer_size);
490 }
491 
TEST_F(Reader,DW_EH_PE_sdata8)492 TEST_F(Reader, DW_EH_PE_sdata8) {
493   static const char data[] = {
494     0xf6, 0x66, 0x57, 0x79, 0xe0, 0x0c, 0x9b, 0x26, 0x87
495   };
496   ByteReader reader(ENDIANNESS_LITTLE);
497   reader.SetAddressSize(8);
498   EXPECT_EQ(0x87269b0ce0795766ULL,
499             reader.ReadEncodedPointer(data + 1, dwarf2reader::DW_EH_PE_sdata8,
500                                         &pointer_size));
501   EXPECT_EQ(8U, pointer_size);
502 }
503 
TEST_F(Reader,DW_EH_PE_pcrel)504 TEST_F(Reader, DW_EH_PE_pcrel) {
505   static const char data[] = { 0x4a, 0x8b, 0x1b, 0x14, 0xc8, 0xc4, 0x02, 0xce };
506   ByteReader reader(ENDIANNESS_BIG);
507   reader.SetAddressSize(4);
508   DwarfPointerEncoding encoding =
509       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_pcrel
510                            | dwarf2reader::DW_EH_PE_absptr);
511   reader.SetCFIDataBase(0x89951377, data);
512   EXPECT_EQ(0x89951377 + 3 + 0x14c8c402,
513             reader.ReadEncodedPointer(data + 3, encoding, &pointer_size));
514   EXPECT_EQ(4U, pointer_size);
515 }
516 
TEST_F(Reader,DW_EH_PE_textrel)517 TEST_F(Reader, DW_EH_PE_textrel) {
518   static const char data[] = { 0xd9, 0x0d, 0x05, 0x17, 0xc9, 0x7a, 0x42, 0x1e };
519   ByteReader reader(ENDIANNESS_LITTLE);
520   reader.SetAddressSize(4);
521   reader.SetTextBase(0xb91beaf0);
522   DwarfPointerEncoding encoding =
523       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_textrel
524                            | dwarf2reader::DW_EH_PE_sdata2);
525   EXPECT_EQ((0xb91beaf0 + 0xffffc917) & 0xffffffff,
526             reader.ReadEncodedPointer(data + 3, encoding, &pointer_size));
527   EXPECT_EQ(2U, pointer_size);
528 }
529 
TEST_F(Reader,DW_EH_PE_datarel)530 TEST_F(Reader, DW_EH_PE_datarel) {
531   static const char data[] = { 0x16, 0xf2, 0xbb, 0x82, 0x68, 0xa7, 0xbc, 0x39 };
532   ByteReader reader(ENDIANNESS_BIG);
533   reader.SetAddressSize(8);
534   reader.SetDataBase(0xbef308bd25ce74f0ULL);
535   DwarfPointerEncoding encoding =
536       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_datarel
537                            | dwarf2reader::DW_EH_PE_sleb128);
538   EXPECT_EQ(0xbef308bd25ce74f0ULL + 0xfffffffffffa013bULL,
539             reader.ReadEncodedPointer(data + 2, encoding, &pointer_size));
540   EXPECT_EQ(3U, pointer_size);
541 }
542 
TEST_F(Reader,DW_EH_PE_funcrel)543 TEST_F(Reader, DW_EH_PE_funcrel) {
544   static const char data[] = { 0x84, 0xf8, 0x14, 0x01, 0x61, 0xd1, 0x48, 0xc9 };
545   ByteReader reader(ENDIANNESS_BIG);
546   reader.SetAddressSize(4);
547   reader.SetFunctionBase(0x823c3520);
548   DwarfPointerEncoding encoding =
549       DwarfPointerEncoding(dwarf2reader::DW_EH_PE_funcrel
550                            | dwarf2reader::DW_EH_PE_udata2);
551   EXPECT_EQ(0x823c3520 + 0xd148,
552             reader.ReadEncodedPointer(data + 5, encoding, &pointer_size));
553   EXPECT_EQ(2U, pointer_size);
554 }
555 
TEST(UsableBase,CFI)556 TEST(UsableBase, CFI) {
557   static const char data[1] = { 0x42 };
558   ByteReader reader(ENDIANNESS_BIG);
559   reader.SetCFIDataBase(0xb31cbd20, data);
560   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_absptr));
561   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_pcrel));
562   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_textrel));
563   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_datarel));
564   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_funcrel));
565   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_omit));
566   EXPECT_FALSE(reader.UsableEncoding(DwarfPointerEncoding(0x60)));
567 }
568 
TEST(UsableBase,Text)569 TEST(UsableBase, Text) {
570   ByteReader reader(ENDIANNESS_BIG);
571   reader.SetTextBase(0xa899ccb9);
572   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_absptr));
573   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_pcrel));
574   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_textrel));
575   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_datarel));
576   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_funcrel));
577   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_omit));
578   EXPECT_FALSE(reader.UsableEncoding(DwarfPointerEncoding(0x60)));
579 }
580 
TEST(UsableBase,Data)581 TEST(UsableBase, Data) {
582   ByteReader reader(ENDIANNESS_BIG);
583   reader.SetDataBase(0xf7b10bcd);
584   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_absptr));
585   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_pcrel));
586   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_textrel));
587   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_datarel));
588   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_funcrel));
589   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_omit));
590   EXPECT_FALSE(reader.UsableEncoding(DwarfPointerEncoding(0x60)));
591 }
592 
TEST(UsableBase,Function)593 TEST(UsableBase, Function) {
594   ByteReader reader(ENDIANNESS_BIG);
595   reader.SetFunctionBase(0xc2c0ed81);
596   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_absptr));
597   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_pcrel));
598   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_textrel));
599   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_datarel));
600   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_funcrel));
601   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_omit));
602   EXPECT_FALSE(reader.UsableEncoding(DwarfPointerEncoding(0x60)));
603 }
604 
TEST(UsableBase,ClearFunction)605 TEST(UsableBase, ClearFunction) {
606   ByteReader reader(ENDIANNESS_BIG);
607   reader.SetFunctionBase(0xc2c0ed81);
608   reader.ClearFunctionBase();
609   EXPECT_TRUE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_absptr));
610   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_pcrel));
611   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_textrel));
612   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_datarel));
613   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_funcrel));
614   EXPECT_FALSE(reader.UsableEncoding(dwarf2reader::DW_EH_PE_omit));
615   EXPECT_FALSE(reader.UsableEncoding(DwarfPointerEncoding(0x60)));
616 }
617 
618 struct AlignedFixture {
AlignedFixtureAlignedFixture619   AlignedFixture() : reader(ENDIANNESS_BIG) { reader.SetAddressSize(4); }
620   static const char data[10];
621   ByteReader reader;
622   size_t pointer_size;
623 };
624 
625 const char AlignedFixture::data[10] = {
626   0xfe, 0x6e, 0x93, 0xd8, 0x34, 0xd5, 0x1c, 0xd3, 0xac, 0x2b
627 };
628 
629 class Aligned: public AlignedFixture, public Test { };
630 
TEST_F(Aligned,DW_EH_PE_aligned0)631 TEST_F(Aligned, DW_EH_PE_aligned0) {
632   reader.SetCFIDataBase(0xb440305c, data);
633   EXPECT_EQ(0xfe6e93d8U,
634             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_aligned,
635                                       &pointer_size));
636   EXPECT_EQ(4U, pointer_size);
637 }
638 
TEST_F(Aligned,DW_EH_PE_aligned1)639 TEST_F(Aligned, DW_EH_PE_aligned1) {
640   reader.SetCFIDataBase(0xb440305d, data);
641   EXPECT_EQ(0xd834d51cU,
642             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_aligned,
643                                       &pointer_size));
644   EXPECT_EQ(7U, pointer_size);
645 }
646 
TEST_F(Aligned,DW_EH_PE_aligned2)647 TEST_F(Aligned, DW_EH_PE_aligned2) {
648   reader.SetCFIDataBase(0xb440305e, data);
649   EXPECT_EQ(0x93d834d5U,
650             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_aligned,
651                                       &pointer_size));
652   EXPECT_EQ(6U, pointer_size);
653 }
654 
TEST_F(Aligned,DW_EH_PE_aligned3)655 TEST_F(Aligned, DW_EH_PE_aligned3) {
656   reader.SetCFIDataBase(0xb440305f, data);
657   EXPECT_EQ(0x6e93d834U,
658             reader.ReadEncodedPointer(data, dwarf2reader::DW_EH_PE_aligned,
659                                       &pointer_size));
660   EXPECT_EQ(5U, pointer_size);
661 }
662 
TEST_F(Aligned,DW_EH_PE_aligned11)663 TEST_F(Aligned, DW_EH_PE_aligned11) {
664   reader.SetCFIDataBase(0xb4403061, data);
665   EXPECT_EQ(0xd834d51cU,
666             reader.ReadEncodedPointer(data + 1,
667                                       dwarf2reader::DW_EH_PE_aligned,
668                                       &pointer_size));
669   EXPECT_EQ(6U, pointer_size);
670 }
671 
TEST_F(Aligned,DW_EH_PE_aligned30)672 TEST_F(Aligned, DW_EH_PE_aligned30) {
673   reader.SetCFIDataBase(0xb4403063, data);
674   EXPECT_EQ(0x6e93d834U,
675             reader.ReadEncodedPointer(data + 1,
676                                       dwarf2reader::DW_EH_PE_aligned,
677                                       &pointer_size));
678   EXPECT_EQ(4U, pointer_size);
679 }
680 
TEST_F(Aligned,DW_EH_PE_aligned23)681 TEST_F(Aligned, DW_EH_PE_aligned23) {
682   reader.SetCFIDataBase(0xb4403062, data);
683   EXPECT_EQ(0x1cd3ac2bU,
684             reader.ReadEncodedPointer(data + 3,
685                                       dwarf2reader::DW_EH_PE_aligned,
686                                       &pointer_size));
687   EXPECT_EQ(7U, pointer_size);
688 }
689 
TEST_F(Aligned,DW_EH_PE_aligned03)690 TEST_F(Aligned, DW_EH_PE_aligned03) {
691   reader.SetCFIDataBase(0xb4403064, data);
692   EXPECT_EQ(0x34d51cd3U,
693             reader.ReadEncodedPointer(data + 3,
694                                       dwarf2reader::DW_EH_PE_aligned,
695                                       &pointer_size));
696   EXPECT_EQ(5U, pointer_size);
697 }
698