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