Debian Codegraph @

llvm-project/llvm/unittests/DebugInfo/DWARF/DWARFDebugFrameTest.cpp

raw
1 //===- llvm/unittest/DebugInfo/DWARFDebugFrameTest.cpp --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/DebugInfo/DIContext.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
16 #include "llvm/Testing/Support/Error.h"
17 #include "gtest/gtest.h"
18
19 using namespace llvm;
20
21 namespace {
22
23 dwarf::CIE createCIE(bool IsDWARF64, uint64_t Offset, uint64_t Length) {
24 return dwarf::CIE(IsDWARF64, Offset, Length,
25 /*Version=*/3,
26 /*Augmentation=*/StringRef(),
27 /*AddressSize=*/8,
28 /*SegmentDescriptorSize=*/0,
29 /*CodeAlignmentFactor=*/1,
30 /*DataAlignmentFactor=*/-8,
31 /*ReturnAddressRegister=*/16,
32 /*AugmentationData=*/StringRef(),
33 /*FDEPointerEncoding=*/dwarf::DW_EH_PE_absptr,
34 /*LSDAPointerEncoding=*/dwarf::DW_EH_PE_omit,
35 /*Personality=*/std::nullopt,
36 /*PersonalityEnc=*/std::nullopt,
37 /*Arch=*/Triple::x86_64);
38 }
39
40 void expectDumpResult(const dwarf::CIE &TestCIE, bool IsEH,
41 StringRef ExpectedFirstLine) {
42 std::string Output;
43 raw_string_ostream OS(Output);
44 auto DumpOpts = DIDumpOptions();
45 DumpOpts.IsEH = IsEH;
46 TestCIE.dump(OS, DumpOpts);
47 OS.flush();
48 StringRef FirstLine = StringRef(Output).split('\n').first;
49 EXPECT_EQ(FirstLine, ExpectedFirstLine);
50 }
51
52 void expectDumpResult(const dwarf::FDE &TestFDE, bool IsEH,
53 StringRef ExpectedFirstLine) {
54 std::string Output;
55 raw_string_ostream OS(Output);
56 auto DumpOpts = DIDumpOptions();
57 DumpOpts.IsEH = IsEH;
58 TestFDE.dump(OS, DumpOpts);
59 OS.flush();
60 StringRef FirstLine = StringRef(Output).split('\n').first;
61 EXPECT_EQ(FirstLine, ExpectedFirstLine);
62 }
63
64 TEST(DWARFDebugFrame, DumpDWARF32CIE) {
65 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
66 /*Offset=*/0x1111abcd,
67 /*Length=*/0x2222abcd);
68 expectDumpResult(TestCIE, /*IsEH=*/false, "1111abcd 2222abcd ffffffff CIE");
69 }
70
71 TEST(DWARFDebugFrame, DumpDWARF64CIE) {
72 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/true,
73 /*Offset=*/0x1111abcdabcd,
74 /*Length=*/0x2222abcdabcd);
75 expectDumpResult(TestCIE, /*IsEH=*/false,
76 "1111abcdabcd 00002222abcdabcd ffffffffffffffff CIE");
77 }
78
79 TEST(DWARFDebugFrame, DumpEHCIE) {
80 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
81 /*Offset=*/0x1000,
82 /*Length=*/0x20);
83 expectDumpResult(TestCIE, /*IsEH=*/true, "00001000 00000020 00000000 CIE");
84 }
85
86 TEST(DWARFDebugFrame, DumpEH64CIE) {
87 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/true,
88 /*Offset=*/0x1000,
89 /*Length=*/0x20);
90 expectDumpResult(TestCIE, /*IsEH=*/true,
91 "00001000 0000000000000020 00000000 CIE");
92 }
93
94 TEST(DWARFDebugFrame, DumpDWARF64FDE) {
95 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/true,
96 /*Offset=*/0x1111abcdabcd,
97 /*Length=*/0x2222abcdabcd);
98 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
99 /*Offset=*/0x3333abcdabcd,
100 /*Length=*/0x4444abcdabcd,
101 /*CIEPointer=*/0x1111abcdabcd,
102 /*InitialLocation=*/0x5555abcdabcd,
103 /*AddressRange=*/0x111111111111,
104 /*Cie=*/&TestCIE,
105 /*LSDAAddress=*/std::nullopt,
106 /*Arch=*/Triple::x86_64);
107 expectDumpResult(TestFDE, /*IsEH=*/false,
108 "3333abcdabcd 00004444abcdabcd 00001111abcdabcd FDE "
109 "cie=1111abcdabcd pc=5555abcdabcd...6666bcdebcde");
110 }
111
112 TEST(DWARFDebugFrame, DumpEH64FDE) {
113 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/true,
114 /*Offset=*/0x1111ab9a000c,
115 /*Length=*/0x20);
116 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
117 /*Offset=*/0x1111abcdabcd,
118 /*Length=*/0x2222abcdabcd,
119 /*CIEPointer=*/0x33abcd,
120 /*InitialLocation=*/0x4444abcdabcd,
121 /*AddressRange=*/0x111111111111,
122 /*Cie=*/&TestCIE,
123 /*LSDAAddress=*/std::nullopt,
124 /*Arch=*/Triple::x86_64);
125 expectDumpResult(TestFDE, /*IsEH=*/true,
126 "1111abcdabcd 00002222abcdabcd 0033abcd FDE "
127 "cie=1111ab9a000c pc=4444abcdabcd...5555bcdebcde");
128 }
129
130 static Error parseCFI(dwarf::CIE &C, ArrayRef<uint8_t> Instructions,
131 std::optional<uint64_t> Size = std::nullopt) {
132 DWARFDataExtractor Data(Instructions, /*IsLittleEndian=*/true,
133 /*AddressSize=*/8);
134 uint64_t Offset = 0;
135 const uint64_t EndOffset = Size ? *Size : (uint64_t)Instructions.size();
136 return C.cfis().parse(Data, &Offset, EndOffset);
137 }
138
139 static Error parseCFI(dwarf::FDE &FDE, ArrayRef<uint8_t> Instructions) {
140 DWARFDataExtractor Data(Instructions, /*IsLittleEndian=*/true,
141 /*AddressSize=*/8);
142 uint64_t Offset = 0;
143 return FDE.cfis().parse(Data, &Offset, Instructions.size());
144 }
145
146 TEST(DWARFDebugFrame, InvalidCFIOpcodesTest) {
147 llvm::DenseSet<uint8_t> ValidExtendedOpcodes = {
148 dwarf::DW_CFA_nop,
149 dwarf::DW_CFA_advance_loc,
150 dwarf::DW_CFA_offset,
151 dwarf::DW_CFA_restore,
152 dwarf::DW_CFA_set_loc,
153 dwarf::DW_CFA_advance_loc1,
154 dwarf::DW_CFA_advance_loc2,
155 dwarf::DW_CFA_advance_loc4,
156 dwarf::DW_CFA_offset_extended,
157 dwarf::DW_CFA_restore_extended,
158 dwarf::DW_CFA_undefined,
159 dwarf::DW_CFA_same_value,
160 dwarf::DW_CFA_register,
161 dwarf::DW_CFA_remember_state,
162 dwarf::DW_CFA_restore_state,
163 dwarf::DW_CFA_def_cfa,
164 dwarf::DW_CFA_def_cfa_register,
165 dwarf::DW_CFA_def_cfa_offset,
166 dwarf::DW_CFA_def_cfa_expression,
167 dwarf::DW_CFA_expression,
168 dwarf::DW_CFA_offset_extended_sf,
169 dwarf::DW_CFA_def_cfa_sf,
170 dwarf::DW_CFA_def_cfa_offset_sf,
171 dwarf::DW_CFA_LLVM_def_aspace_cfa,
172 dwarf::DW_CFA_LLVM_def_aspace_cfa_sf,
173 dwarf::DW_CFA_val_offset,
174 dwarf::DW_CFA_val_offset_sf,
175 dwarf::DW_CFA_val_expression,
176 dwarf::DW_CFA_MIPS_advance_loc8,
177 dwarf::DW_CFA_GNU_window_save,
178 dwarf::DW_CFA_AARCH64_negate_ra_state,
179 dwarf::DW_CFA_GNU_args_size};
180
181 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
182 /*Offset=*/0x0,
183 /*Length=*/0xff);
184
185 // See DWARF standard v3, section 7.23: low 6 bits are used to encode an
186 // extended opcode.
187 for (uint8_t Code = 0; Code <= 63; ++Code) {
188 if (ValidExtendedOpcodes.count(Code))
189 continue;
190
191 EXPECT_THAT_ERROR(parseCFI(TestCIE, Code),
192 FailedWithMessage(("invalid extended CFI opcode 0x" +
193 Twine::utohexstr(Code))
194 .str()
195 .c_str()));
196 }
197 }
198
199 // Here we test how truncated Call Frame Instructions are parsed.
200 TEST(DWARFDebugFrame, ParseTruncatedCFITest) {
201 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
202 /*Offset=*/0x0,
203 /*Length=*/0xff);
204
205 // Having an empty instructions list is fine.
206 EXPECT_THAT_ERROR(parseCFI(TestCIE, {}), Succeeded());
207
208 // Unable to read an opcode, because the instructions list is empty, but we
209 // say to the parser that it is not.
210 EXPECT_THAT_ERROR(
211 parseCFI(TestCIE, {}, /*Size=*/1),
212 FailedWithMessage(
213 "unexpected end of data at offset 0x0 while reading [0x0, 0x1)"));
214
215 // Unable to read a truncated DW_CFA_offset instruction.
216 EXPECT_THAT_ERROR(
217 parseCFI(TestCIE, {dwarf::DW_CFA_offset}),
218 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
219 "malformed uleb128, extends past end"));
220
221 // Unable to read a truncated DW_CFA_set_loc instruction.
222 EXPECT_THAT_ERROR(
223 parseCFI(TestCIE, {dwarf::DW_CFA_set_loc}),
224 FailedWithMessage(
225 "unexpected end of data at offset 0x1 while reading [0x1, 0x9)"));
226
227 // Unable to read a truncated DW_CFA_advance_loc1 instruction.
228 EXPECT_THAT_ERROR(
229 parseCFI(TestCIE, {dwarf::DW_CFA_advance_loc1}),
230 FailedWithMessage(
231 "unexpected end of data at offset 0x1 while reading [0x1, 0x2)"));
232
233 // Unable to read a truncated DW_CFA_advance_loc2 instruction.
234 EXPECT_THAT_ERROR(
235 parseCFI(TestCIE, {dwarf::DW_CFA_advance_loc2}),
236 FailedWithMessage(
237 "unexpected end of data at offset 0x1 while reading [0x1, 0x3)"));
238
239 // Unable to read a truncated DW_CFA_advance_loc4 instruction.
240 EXPECT_THAT_ERROR(
241 parseCFI(TestCIE, {dwarf::DW_CFA_advance_loc4}),
242 FailedWithMessage(
243 "unexpected end of data at offset 0x1 while reading [0x1, 0x5)"));
244
245 // A test for an instruction with a single ULEB128 operand.
246 auto CheckOp_ULEB128 = [&](uint8_t Inst) {
247 EXPECT_THAT_ERROR(
248 parseCFI(TestCIE, Inst),
249 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
250 "malformed uleb128, extends past end"));
251 };
252
253 for (uint8_t Inst :
254 {dwarf::DW_CFA_restore_extended, dwarf::DW_CFA_undefined,
255 dwarf::DW_CFA_same_value, dwarf::DW_CFA_def_cfa_register,
256 dwarf::DW_CFA_def_cfa_offset, dwarf::DW_CFA_GNU_args_size})
257 CheckOp_ULEB128(Inst);
258
259 // Unable to read a truncated DW_CFA_def_cfa_offset_sf instruction.
260 EXPECT_THAT_ERROR(
261 parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa_offset_sf}),
262 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
263 "malformed sleb128, extends past end"));
264
265 // A test for an instruction with two ULEB128 operands.
266 auto CheckOp_ULEB128_ULEB128 = [&](uint8_t Inst) {
267 EXPECT_THAT_ERROR(
268 parseCFI(TestCIE, Inst),
269 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
270 "malformed uleb128, extends past end"));
271
272 EXPECT_THAT_ERROR(
273 parseCFI(TestCIE, {Inst, /*Op1=*/0}),
274 FailedWithMessage("unable to decode LEB128 at offset 0x00000002: "
275 "malformed uleb128, extends past end"));
276 };
277
278 for (uint8_t Inst : {dwarf::DW_CFA_offset_extended, dwarf::DW_CFA_register,
279 dwarf::DW_CFA_def_cfa, dwarf::DW_CFA_LLVM_def_aspace_cfa,
280 dwarf::DW_CFA_val_offset})
281 CheckOp_ULEB128_ULEB128(Inst);
282
283 // A test for an instruction with two operands: ULEB128, SLEB128.
284 auto CheckOp_ULEB128_SLEB128 = [&](uint8_t Inst) {
285 EXPECT_THAT_ERROR(
286 parseCFI(TestCIE, Inst),
287 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
288 "malformed uleb128, extends past end"));
289
290 EXPECT_THAT_ERROR(
291 parseCFI(TestCIE, {Inst, /*Op1=*/0}),
292 FailedWithMessage("unable to decode LEB128 at offset 0x00000002: "
293 "malformed sleb128, extends past end"));
294 };
295
296 for (uint8_t Inst :
297 {dwarf::DW_CFA_offset_extended_sf, dwarf::DW_CFA_def_cfa_sf,
298 dwarf::DW_CFA_LLVM_def_aspace_cfa_sf, dwarf::DW_CFA_val_offset_sf})
299 CheckOp_ULEB128_SLEB128(Inst);
300
301 // Unable to read a truncated DW_CFA_def_cfa_expression instruction.
302 EXPECT_THAT_ERROR(
303 parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa_expression}),
304 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
305 "malformed uleb128, extends past end"));
306 EXPECT_THAT_ERROR(
307 parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa_expression,
308 /*expression length=*/0x1}),
309 FailedWithMessage(
310 "unexpected end of data at offset 0x2 while reading [0x2, 0x3)"));
311 // The DW_CFA_def_cfa_expression can contain a zero length expression.
312 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa_expression,
313 /*ExprLen=*/0}),
314 Succeeded());
315
316 // A test for an instruction with three operands: ULEB128, expression length
317 // (ULEB128) and expression bytes.
318 auto CheckOp_ULEB128_Expr = [&](uint8_t Inst) {
319 EXPECT_THAT_ERROR(
320 parseCFI(TestCIE, {Inst}),
321 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
322 "malformed uleb128, extends past end"));
323 EXPECT_THAT_ERROR(
324 parseCFI(TestCIE, {Inst, /*Op1=*/0}),
325 FailedWithMessage("unable to decode LEB128 at offset 0x00000002: "
326 "malformed uleb128, extends past end"));
327 // A zero length expression is fine
328 EXPECT_THAT_ERROR(parseCFI(TestCIE, {Inst,
329 /*Op1=*/0, /*ExprLen=*/0}),
330 Succeeded());
331 EXPECT_THAT_ERROR(
332 parseCFI(TestCIE, {Inst,
333 /*Op1=*/0, /*ExprLen=*/1}),
334 FailedWithMessage(
335 "unexpected end of data at offset 0x3 while reading [0x3, 0x4)"));
336 };
337
338 for (uint8_t Inst : {dwarf::DW_CFA_expression, dwarf::DW_CFA_val_expression})
339 CheckOp_ULEB128_Expr(Inst);
340 }
341
342 void expectDumpResult(const dwarf::UnwindLocation &Loc,
343 StringRef ExpectedFirstLine) {
344 std::string Output;
345 raw_string_ostream OS(Output);
346 OS << Loc;
347 OS.flush();
348 StringRef FirstLine = StringRef(Output).split('\n').first;
349 EXPECT_EQ(FirstLine, ExpectedFirstLine);
350 }
351
352 TEST(DWARFDebugFrame, DumpUnwindLocations) {
353 // Test constructing unwind locations and dumping each kind.
354 constexpr int32_t PlusOff = 8;
355 constexpr int32_t MinusOff = -8;
356 constexpr uint8_t RegNum = 12;
357 expectDumpResult(dwarf::UnwindLocation::createUnspecified(), "unspecified");
358 expectDumpResult(dwarf::UnwindLocation::createUndefined(), "undefined");
359 expectDumpResult(dwarf::UnwindLocation::createSame(), "same");
360 expectDumpResult(dwarf::UnwindLocation::createIsCFAPlusOffset(PlusOff),
361 "CFA+8");
362 expectDumpResult(dwarf::UnwindLocation::createIsCFAPlusOffset(MinusOff),
363 "CFA-8");
364 expectDumpResult(dwarf::UnwindLocation::createAtCFAPlusOffset(PlusOff),
365 "[CFA+8]");
366 expectDumpResult(dwarf::UnwindLocation::createAtCFAPlusOffset(MinusOff),
367 "[CFA-8]");
368
369 expectDumpResult(
370 dwarf::UnwindLocation::createIsRegisterPlusOffset(RegNum, PlusOff),
371 "reg12+8");
372 expectDumpResult(
373 dwarf::UnwindLocation::createIsRegisterPlusOffset(RegNum, MinusOff),
374 "reg12-8");
375 expectDumpResult(
376 dwarf::UnwindLocation::createAtRegisterPlusOffset(RegNum, PlusOff),
377 "[reg12+8]");
378 expectDumpResult(
379 dwarf::UnwindLocation::createAtRegisterPlusOffset(RegNum, MinusOff),
380 "[reg12-8]");
381 expectDumpResult(dwarf::UnwindLocation::createIsConstant(12), "12");
382 expectDumpResult(dwarf::UnwindLocation::createIsConstant(-32), "-32");
383 }
384
385 void expectDumpResult(const dwarf::RegisterLocations &Locs,
386 StringRef ExpectedFirstLine) {
387 std::string Output;
388 raw_string_ostream OS(Output);
389 OS << Locs;
390 OS.flush();
391 StringRef FirstLine = StringRef(Output).split('\n').first;
392 EXPECT_EQ(FirstLine, ExpectedFirstLine);
393 }
394
395 TEST(DWARFDebugFrame, RegisterLocations) {
396 // Test the functionality of the RegisterLocations class.
397 dwarf::RegisterLocations Locs;
398 expectDumpResult(Locs, "");
399 EXPECT_FALSE(Locs.hasLocations());
400 // Set a register location for reg12 to unspecified and verify it dumps
401 // correctly.
402 Locs.setRegisterLocation(12, dwarf::UnwindLocation::createUnspecified());
403 EXPECT_TRUE(Locs.hasLocations());
404 expectDumpResult(Locs, "reg12=unspecified");
405
406 // Replace the register location for reg12 to "same" and verify it dumps
407 // correctly after it is modified
408 Locs.setRegisterLocation(12, dwarf::UnwindLocation::createSame());
409 EXPECT_TRUE(Locs.hasLocations());
410 expectDumpResult(Locs, "reg12=same");
411
412 // Remove the register location for reg12 verify it dumps correctly after it
413 // is removed.
414 Locs.removeRegisterLocation(12);
415 EXPECT_FALSE(Locs.hasLocations());
416 expectDumpResult(Locs, "");
417
418 // Verify multiple registers added to the list dump correctly.
419 auto Reg12Loc = dwarf::UnwindLocation::createAtCFAPlusOffset(4);
420 auto Reg13Loc = dwarf::UnwindLocation::createAtCFAPlusOffset(8);
421 auto Reg14Loc = dwarf::UnwindLocation::createSame();
422 Locs.setRegisterLocation(12, Reg12Loc);
423 Locs.setRegisterLocation(13, Reg13Loc);
424 Locs.setRegisterLocation(14, Reg14Loc);
425 EXPECT_TRUE(Locs.hasLocations());
426 expectDumpResult(Locs, "reg12=[CFA+4], reg13=[CFA+8], reg14=same");
427
428 // Verify RegisterLocations::getRegisterLocation() works as expected.
429 std::optional<dwarf::UnwindLocation> OptionalLoc;
430 OptionalLoc = Locs.getRegisterLocation(0);
431 EXPECT_FALSE(OptionalLoc.has_value());
432
433 OptionalLoc = Locs.getRegisterLocation(12);
434 EXPECT_TRUE(OptionalLoc.has_value());
435 EXPECT_EQ(*OptionalLoc, Reg12Loc);
436
437 OptionalLoc = Locs.getRegisterLocation(13);
438 EXPECT_TRUE(OptionalLoc.has_value());
439 EXPECT_EQ(*OptionalLoc, Reg13Loc);
440
441 OptionalLoc = Locs.getRegisterLocation(14);
442 EXPECT_TRUE(OptionalLoc.has_value());
443 EXPECT_EQ(*OptionalLoc, Reg14Loc);
444
445 // Verify registers are correctly removed when multiple exist in the list.
446 Locs.removeRegisterLocation(13);
447 EXPECT_FALSE(Locs.getRegisterLocation(13).has_value());
448 EXPECT_TRUE(Locs.hasLocations());
449 expectDumpResult(Locs, "reg12=[CFA+4], reg14=same");
450 Locs.removeRegisterLocation(14);
451 EXPECT_FALSE(Locs.getRegisterLocation(14).has_value());
452 EXPECT_TRUE(Locs.hasLocations());
453 expectDumpResult(Locs, "reg12=[CFA+4]");
454 Locs.removeRegisterLocation(12);
455 EXPECT_FALSE(Locs.getRegisterLocation(12).has_value());
456 EXPECT_FALSE(Locs.hasLocations());
457 expectDumpResult(Locs, "");
458 }
459
460 // Test that empty rows are not added to UnwindTable when
461 // dwarf::CIE::CFIs or dwarf::FDE::CFIs is empty.
462 TEST(DWARFDebugFrame, UnwindTableEmptyRows) {
463 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
464 /*Offset=*/0x0,
465 /*Length=*/0xff);
466
467 // Having an empty instructions list is fine.
468 EXPECT_THAT_ERROR(parseCFI(TestCIE, {}), Succeeded());
469 EXPECT_TRUE(TestCIE.cfis().empty());
470
471 // Verify dwarf::UnwindTable::create() won't result in errors and
472 // and empty rows are not added to CIE UnwindTable.
473 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestCIE);
474 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
475 const size_t ExpectedNumOfRows = 0;
476 EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
477
478 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
479 /*Offset=*/0x3333abcdabcd,
480 /*Length=*/0x4444abcdabcd,
481 /*CIEPointer=*/0x1111abcdabcd,
482 /*InitialLocation=*/0x1000,
483 /*AddressRange=*/0x1000,
484 /*Cie=*/&TestCIE,
485 /*LSDAAddress=*/std::nullopt,
486 /*Arch=*/Triple::x86_64);
487
488 // Having an empty instructions list is fine.
489 EXPECT_THAT_ERROR(parseCFI(TestFDE, {}), Succeeded());
490 EXPECT_TRUE(TestFDE.cfis().empty());
491
492 // Verify dwarf::UnwindTable::create() won't result in errors and
493 // and empty rows are not added to FDE UnwindTable.
494 RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
495 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
496 EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
497 }
498
499 // Test that empty rows are not added to UnwindTable when dwarf::CIE::CFIs
500 // or dwarf::FDE::CFIs is not empty but has only DW_CFA_nop instructions.
501 TEST(DWARFDebugFrame, UnwindTableEmptyRows_NOPs) {
502 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
503 /*Offset=*/0x0,
504 /*Length=*/0xff);
505
506 // Make a CIE that has only DW_CFA_nop instructions.
507 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_nop}), Succeeded());
508 EXPECT_TRUE(!TestCIE.cfis().empty());
509
510 // Verify dwarf::UnwindTable::create() won't result in errors and
511 // and empty rows are not added to CIE UnwindTable.
512 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestCIE);
513 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
514 const size_t ExpectedNumOfRows = 0;
515 EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
516
517 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
518 /*Offset=*/0x3333abcdabcd,
519 /*Length=*/0x4444abcdabcd,
520 /*CIEPointer=*/0x1111abcdabcd,
521 /*InitialLocation=*/0x1000,
522 /*AddressRange=*/0x1000,
523 /*Cie=*/&TestCIE,
524 /*LSDAAddress=*/std::nullopt,
525 /*Arch=*/Triple::x86_64);
526
527 // Make an FDE that has only DW_CFA_nop instructions.
528 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_nop}), Succeeded());
529 EXPECT_TRUE(!TestFDE.cfis().empty());
530
531 // Verify dwarf::UnwindTable::create() won't result in errors and
532 // and empty rows are not added to FDE UnwindTable.
533 RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
534 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
535 EXPECT_EQ(RowsOrErr->size(), ExpectedNumOfRows);
536 }
537
538 TEST(DWARFDebugFrame, UnwindTableErrorNonAscendingFDERows) {
539 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
540 /*Offset=*/0x0,
541 /*Length=*/0xff);
542
543 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
544 /*Offset=*/0x3333abcdabcd,
545 /*Length=*/0x4444abcdabcd,
546 /*CIEPointer=*/0x1111abcdabcd,
547 /*InitialLocation=*/0x1000,
548 /*AddressRange=*/0x1000,
549 /*Cie=*/&TestCIE,
550 /*LSDAAddress=*/std::nullopt,
551 /*Arch=*/Triple::x86_64);
552
553 // Make a CIE that has a valid CFA definition.
554 constexpr uint8_t Reg = 12;
555 constexpr uint8_t Offset = 32;
556 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, Reg, Offset}),
557 Succeeded());
558
559 // Make a FDE with DWARF call frame instruction opcodes that have valid
560 // syntax, but will cause an error when we parse them into a UnwindTable.
561 // Here we encode two DW_CFA_set_loc opcodes:
562 // DW_CFA_set_loc(0x1100)
563 // DW_CFA_set_loc(0x1000)
564 // These opcodes cause a new row to be appended to the rows in a UnwindTable
565 // and the resulting rows are not in ascending address order and should cause
566 // a state machine error.
567 EXPECT_THAT_ERROR(
568 parseCFI(TestFDE, {dwarf::DW_CFA_set_loc, 0x00, 0x11, 0, 0, 0, 0, 0, 0,
569 dwarf::DW_CFA_set_loc, 0x00, 0x10, 0, 0, 0, 0, 0, 0}),
570 Succeeded());
571
572 // Verify we catch state machine error.
573 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
574 EXPECT_THAT_ERROR(RowsOrErr.takeError(),
575 FailedWithMessage("DW_CFA_set_loc with adrress 0x1000 which"
576 " must be greater than the current row "
577 "address 0x1100"));
578 }
579
580 TEST(DWARFDebugFrame, UnwindTableError_DW_CFA_restore_state) {
581 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
582 /*Offset=*/0x0,
583 /*Length=*/0xff);
584
585 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
586 /*Offset=*/0x3333abcdabcd,
587 /*Length=*/0x4444abcdabcd,
588 /*CIEPointer=*/0x1111abcdabcd,
589 /*InitialLocation=*/0x1000,
590 /*AddressRange=*/0x1000,
591 /*Cie=*/&TestCIE,
592 /*LSDAAddress=*/std::nullopt,
593 /*Arch=*/Triple::x86_64);
594
595 // Make a CIE that has a valid CFA definition.
596 constexpr uint8_t Reg = 12;
597 constexpr uint8_t Offset = 32;
598 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, Reg, Offset}),
599 Succeeded());
600
601 // Make a FDE with DWARF call frame instruction opcodes that have valid
602 // syntax, but will cause an error when we parse them into a UnwindTable.
603 // Here we encode a DW_CFA_restore_state opcode that was not preceded by a
604 // DW_CFA_remember_state, and an error should be returned.
605 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_restore_state}),
606 Succeeded());
607
608 // Verify we catch state machine error.
609 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
610 EXPECT_THAT_ERROR(RowsOrErr.takeError(),
611 FailedWithMessage("DW_CFA_restore_state without a matching "
612 "previous DW_CFA_remember_state"));
613 }
614
615 TEST(DWARFDebugFrame, UnwindTableError_DW_CFA_GNU_window_save) {
616 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
617 /*Offset=*/0x0,
618 /*Length=*/0xff);
619
620 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
621 /*Offset=*/0x3333abcdabcd,
622 /*Length=*/0x4444abcdabcd,
623 /*CIEPointer=*/0x1111abcdabcd,
624 /*InitialLocation=*/0x1000,
625 /*AddressRange=*/0x1000,
626 /*Cie=*/&TestCIE,
627 /*LSDAAddress=*/std::nullopt,
628 /*Arch=*/Triple::x86_64);
629
630 // Make a CIE that has a valid CFA definition.
631 constexpr uint8_t Reg = 12;
632 constexpr uint8_t Offset = 32;
633 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, Reg, Offset}),
634 Succeeded());
635
636 // Make a FDE with DWARF call frame instruction opcodes that have valid
637 // syntax, but will cause an error when we parse them into a UnwindTable.
638 // Here we encode a DW_CFA_GNU_window_save that is not supported. I have not
639 // found any documentation that describes what this does after some brief
640 // searching.
641 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_GNU_window_save}),
642 Succeeded());
643
644 // Verify we catch state machine error.
645 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
646 EXPECT_THAT_ERROR(RowsOrErr.takeError(),
647 FailedWithMessage("DW_CFA opcode 0x2d is not supported for "
648 "architecture x86_64"));
649 }
650
651 TEST(DWARFDebugFrame, UnwindTableError_DW_CFA_def_cfa_offset) {
652 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
653 /*Offset=*/0x0,
654 /*Length=*/0xff);
655
656 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
657 /*Offset=*/0x3333abcdabcd,
658 /*Length=*/0x4444abcdabcd,
659 /*CIEPointer=*/0x1111abcdabcd,
660 /*InitialLocation=*/0x1000,
661 /*AddressRange=*/0x1000,
662 /*Cie=*/&TestCIE,
663 /*LSDAAddress=*/std::nullopt,
664 /*Arch=*/Triple::x86_64);
665
666 // Make a CIE that has an invalid CFA definition. We do this so we can try
667 // and use a DW_CFA_def_cfa_register opcode in the FDE and get an appropriate
668 // error back.
669 EXPECT_THAT_ERROR(parseCFI(TestCIE, {}), Succeeded());
670
671 // Make a FDE with DWARF call frame instruction opcodes that have valid
672 // syntax, but will cause an error when we parse them into a UnwindTable.
673 // Here we encode a DW_CFA_def_cfa_offset with a offset of 16, but our CIE
674 // didn't define the CFA in terms of a register plus offset, so this should
675 // cause an error.
676 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_def_cfa_offset, 16}),
677 Succeeded());
678
679 // Verify we catch state machine error.
680 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
681 EXPECT_THAT_ERROR(RowsOrErr.takeError(),
682 FailedWithMessage("DW_CFA_def_cfa_offset found when CFA "
683 "rule was not RegPlusOffset"));
684 }
685
686 TEST(DWARFDebugFrame, UnwindTableDefCFAOffsetSFCFAError) {
687 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
688 /*Offset=*/0x0,
689 /*Length=*/0xff);
690
691 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
692 /*Offset=*/0x3333abcdabcd,
693 /*Length=*/0x4444abcdabcd,
694 /*CIEPointer=*/0x1111abcdabcd,
695 /*InitialLocation=*/0x1000,
696 /*AddressRange=*/0x1000,
697 /*Cie=*/&TestCIE,
698 /*LSDAAddress=*/std::nullopt,
699 /*Arch=*/Triple::x86_64);
700
701 // Make a CIE that has an invalid CFA definition. We do this so we can try
702 // and use a DW_CFA_def_cfa_offset_sf opcode in the FDE and get an
703 // appropriate error back.
704 EXPECT_THAT_ERROR(parseCFI(TestCIE, {}), Succeeded());
705
706 // Make a FDE with DWARF call frame instruction opcodes that have valid
707 // syntax, but will cause an error when we parse them into a UnwindTable.
708 // Here we encode a DW_CFA_def_cfa_offset_sf with a offset of 4, but our CIE
709 // didn't define the CFA in terms of a register plus offset, so this should
710 // cause an error.
711 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_def_cfa_offset_sf, 4}),
712 Succeeded());
713
714 // Verify we catch state machine error.
715 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
716 EXPECT_THAT_ERROR(RowsOrErr.takeError(),
717 FailedWithMessage("DW_CFA_def_cfa_offset_sf found when CFA "
718 "rule was not RegPlusOffset"));
719 }
720
721 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_def_cfa_register) {
722 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
723 /*Offset=*/0x0,
724 /*Length=*/0xff);
725
726 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
727 /*Offset=*/0x3333abcdabcd,
728 /*Length=*/0x4444abcdabcd,
729 /*CIEPointer=*/0x1111abcdabcd,
730 /*InitialLocation=*/0x1000,
731 /*AddressRange=*/0x1000,
732 /*Cie=*/&TestCIE,
733 /*LSDAAddress=*/std::nullopt,
734 /*Arch=*/Triple::x86_64);
735
736 // Make a CIE that has only defines the CFA register with no offset. Some
737 // architectures do this and we must ensure that we set the CFA value to be
738 // equal to that register with no offset.
739 constexpr uint8_t CFAReg = 12;
740 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa_register, CFAReg}),
741 Succeeded());
742
743 // Make a FDE with DWARF call frame instruction opcodes that have valid
744 // syntax, but will cause an error when we parse them into a UnwindTable.
745 // Here we encode a DW_CFA_def_cfa_register with a register number of 12, but
746 // our CIE didn't define the CFA in terms of a register plus offset, so this
747 // should cause an error.
748 EXPECT_THAT_ERROR(parseCFI(TestFDE, {}), Succeeded());
749
750 // Verify we catch state machine error.
751 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
752 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
753 const dwarf::UnwindTable &Rows = RowsOrErr.get();
754 EXPECT_EQ(Rows.size(), 1u);
755 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
756 EXPECT_EQ(Rows[0].getCFAValue(),
757 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg, 0));
758 }
759
760 TEST(DWARFDebugFrame, UnwindTableRowPushingOpcodes) {
761 // Test all opcodes that should end up pushing a UnwindRow into a UnwindTable.
762 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
763 /*Offset=*/0x0,
764 /*Length=*/0xff);
765
766 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
767 /*Offset=*/0x3333abcdabcd,
768 /*Length=*/0x4444abcdabcd,
769 /*CIEPointer=*/0x1111abcdabcd,
770 /*InitialLocation=*/0x1000,
771 /*AddressRange=*/0x1000,
772 /*Cie=*/&TestCIE,
773 /*LSDAAddress=*/std::nullopt,
774 /*Arch=*/Triple::x86_64);
775
776 // Make a CIE that has a valid CFA definition and a single register unwind
777 // rule for register that we will verify is in all of the pushed rows.
778 constexpr uint8_t CFAReg = 12;
779 constexpr uint8_t CFAOffset = 32;
780 constexpr uint8_t Reg = 13;
781 constexpr uint8_t InReg = 14;
782
783 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, CFAReg, CFAOffset,
784 dwarf::DW_CFA_register, Reg, InReg}),
785 Succeeded());
786
787 // Make a FDE with DWARF call frame instruction opcodes that use all of the
788 // row pushing opcodes. This will verify that all opcodes that should create
789 // a row are correctly working. Each opcode will push a row prior to
790 // advancing the address, and then a row will be automatically pushed at the
791 // end of the parsing, so we should end up with 6 rows starting at address
792 // 0x1000 (from the FDE) and incrementing each one by 4 * CodeAlignmentFactor
793 // from the CIE.
794 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_advance_loc | 4,
795 dwarf::DW_CFA_advance_loc1,
796 4,
797 dwarf::DW_CFA_advance_loc2,
798 4,
799 0,
800 dwarf::DW_CFA_advance_loc4,
801 4,
802 0,
803 0,
804 0,
805 dwarf::DW_CFA_set_loc,
806 0x14,
807 0x10,
808 0,
809 0,
810 0,
811 0,
812 0,
813 0}),
814 Succeeded());
815
816 // Create locations that we expect the UnwindRow objects to contain after
817 // parsing the DWARF call frame instructions.
818 dwarf::RegisterLocations VerifyLocs;
819 VerifyLocs.setRegisterLocation(
820 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
821
822 // Verify we catch state machine error.
823 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
824 ASSERT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
825 const dwarf::UnwindTable &Rows = RowsOrErr.get();
826 EXPECT_EQ(Rows.size(), 6u);
827 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
828 EXPECT_EQ(Rows[0].getRegisterLocations().size(), 1u);
829 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
830 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
831 EXPECT_EQ(Rows[1].getRegisterLocations().size(), 1u);
832 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs);
833 EXPECT_EQ(Rows[2].getAddress(), 0x1008u);
834 EXPECT_EQ(Rows[2].getRegisterLocations().size(), 1u);
835 EXPECT_EQ(Rows[2].getRegisterLocations(), VerifyLocs);
836 EXPECT_EQ(Rows[3].getAddress(), 0x100cu);
837 EXPECT_EQ(Rows[3].getRegisterLocations().size(), 1u);
838 EXPECT_EQ(Rows[3].getRegisterLocations(), VerifyLocs);
839 EXPECT_EQ(Rows[4].getAddress(), 0x1010u);
840 EXPECT_EQ(Rows[4].getRegisterLocations().size(), 1u);
841 EXPECT_EQ(Rows[4].getRegisterLocations(), VerifyLocs);
842 EXPECT_EQ(Rows[5].getAddress(), 0x1014u);
843 EXPECT_EQ(Rows[5].getRegisterLocations().size(), 1u);
844 EXPECT_EQ(Rows[5].getRegisterLocations(), VerifyLocs);
845 }
846
847 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_restore) {
848 // Test that DW_CFA_restore works as expected when parsed in the state
849 // machine.
850 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
851 /*Offset=*/0x0,
852 /*Length=*/0xff);
853
854 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
855 /*Offset=*/0x3333abcdabcd,
856 /*Length=*/0x4444abcdabcd,
857 /*CIEPointer=*/0x1111abcdabcd,
858 /*InitialLocation=*/0x1000,
859 /*AddressRange=*/0x1000,
860 /*Cie=*/&TestCIE,
861 /*LSDAAddress=*/std::nullopt,
862 /*Arch=*/Triple::x86_64);
863
864 // Make a CIE that has a valid CFA definition and a single register unwind
865 // rule for register that we will verify is in all of the pushed rows.
866 constexpr uint8_t CFAReg = 12;
867 constexpr uint8_t CFAOffset = 32;
868 constexpr uint8_t Reg = 13;
869 constexpr uint8_t InReg = 14;
870 constexpr int32_t RegCFAOffset = -8;
871
872 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, CFAReg, CFAOffset,
873 dwarf::DW_CFA_register, Reg, InReg}),
874 Succeeded());
875
876 // Make a FDE with DWARF call frame instruction opcodes that changes the rule
877 // for register "Reg" to be [CFA-8], then push a row, and then restore the
878 // register unwind rule for "Reg" using DW_CFA_restore. We should end up with
879 // two rows:
880 // - one with Reg = [CFA-8]
881 // - one with Reg = InReg
882 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_offset | Reg, 1,
883 dwarf::DW_CFA_advance_loc | 4,
884 dwarf::DW_CFA_restore | Reg}),
885 Succeeded());
886
887 // Create locations that we expect the UnwindRow objects to contain after
888 // parsing the DWARF call frame instructions.
889 dwarf::RegisterLocations VerifyLocs1;
890 VerifyLocs1.setRegisterLocation(
891 Reg, dwarf::UnwindLocation::createAtCFAPlusOffset(RegCFAOffset));
892
893 dwarf::RegisterLocations VerifyLocs2;
894 VerifyLocs2.setRegisterLocation(
895 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
896
897 // Verify we catch state machine error.
898 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
899 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
900 const dwarf::UnwindTable &Rows = RowsOrErr.get();
901 EXPECT_EQ(Rows.size(), 2u);
902 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
903 EXPECT_EQ(Rows[0].getRegisterLocations().size(), 1u);
904 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs1);
905 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
906 EXPECT_EQ(Rows[1].getRegisterLocations().size(), 1u);
907 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs2);
908 }
909
910 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_restore_extended) {
911 // Test that DW_CFA_restore works as expected when parsed in the state
912 // machine.
913 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
914 /*Offset=*/0x0,
915 /*Length=*/0xff);
916
917 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
918 /*Offset=*/0x3333abcdabcd,
919 /*Length=*/0x4444abcdabcd,
920 /*CIEPointer=*/0x1111abcdabcd,
921 /*InitialLocation=*/0x1000,
922 /*AddressRange=*/0x1000,
923 /*Cie=*/&TestCIE,
924 /*LSDAAddress=*/std::nullopt,
925 /*Arch=*/Triple::x86_64);
926
927 // Make a CIE that has a valid CFA definition and a single register unwind
928 // rule for register that we will verify is in all of the pushed rows.
929 constexpr uint8_t CFAReg = 12;
930 constexpr uint8_t CFAOffset = 32;
931 constexpr uint8_t Reg = 13;
932 constexpr uint8_t InReg = 14;
933 constexpr int32_t RegCFAOffset = -8;
934
935 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, CFAReg, CFAOffset,
936 dwarf::DW_CFA_register, Reg, InReg}),
937 Succeeded());
938
939 // Make a FDE with DWARF call frame instruction opcodes that changes the rule
940 // for register "Reg" to be [CFA-8], then push a row, and then restore the
941 // register unwind rule for "Reg" using DW_CFA_restore_extended. We should
942 // end up with two rows:
943 // - one with Reg = [CFA-8]
944 // - one with Reg = InReg
945 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_offset | Reg, 1,
946 dwarf::DW_CFA_advance_loc | 4,
947 dwarf::DW_CFA_restore_extended, Reg}),
948 Succeeded());
949
950 // Create locations that we expect the UnwindRow objects to contain after
951 // parsing the DWARF call frame instructions.
952 dwarf::RegisterLocations VerifyLocs1;
953 VerifyLocs1.setRegisterLocation(
954 Reg, dwarf::UnwindLocation::createAtCFAPlusOffset(RegCFAOffset));
955
956 dwarf::RegisterLocations VerifyLocs2;
957 VerifyLocs2.setRegisterLocation(
958 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
959
960 // Verify we catch state machine error.
961 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
962 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
963 const dwarf::UnwindTable &Rows = RowsOrErr.get();
964 EXPECT_EQ(Rows.size(), 2u);
965 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
966 EXPECT_EQ(Rows[0].getRegisterLocations().size(), 1u);
967 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs1);
968 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
969 EXPECT_EQ(Rows[1].getRegisterLocations().size(), 1u);
970 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs2);
971 }
972
973 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_offset) {
974 // Test that DW_CFA_offset, DW_CFA_offset_extended and
975 // DW_CFA_offset_extended_sf work as expected when parsed in the state
976 // machine.
977 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
978 /*Offset=*/0x0,
979 /*Length=*/0xff);
980
981 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
982 /*Offset=*/0x3333abcdabcd,
983 /*Length=*/0x4444abcdabcd,
984 /*CIEPointer=*/0x1111abcdabcd,
985 /*InitialLocation=*/0x1000,
986 /*AddressRange=*/0x1000,
987 /*Cie=*/&TestCIE,
988 /*LSDAAddress=*/std::nullopt,
989 /*Arch=*/Triple::x86_64);
990
991 // Make a CIE that has a valid CFA definition and a single register unwind
992 // rule for register that we will verify is in all of the pushed rows.
993 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
994 Succeeded());
995
996 // Make a FDE with DWARF call frame instruction opcodes that changes the
997 // unwind rules for the follwing registers:
998 // Reg1 = [CFA-8]
999 // Reg2 = [CFA-16]
1000 // Reg3 = [CFA+8]
1001 constexpr uint8_t Reg1 = 14;
1002 constexpr uint8_t Reg2 = 15;
1003 constexpr uint8_t Reg3 = 16;
1004 constexpr uint8_t Neg1SLEB = 0x7f;
1005 EXPECT_THAT_ERROR(
1006 parseCFI(TestFDE,
1007 {dwarf::DW_CFA_offset | Reg1, 1, dwarf::DW_CFA_offset_extended,
1008 Reg2, 2, dwarf::DW_CFA_offset_extended_sf, Reg3, Neg1SLEB}),
1009 Succeeded());
1010
1011 // Create locations that we expect the UnwindRow objects to contain after
1012 // parsing the DWARF call frame instructions.
1013 dwarf::RegisterLocations VerifyLocs;
1014 VerifyLocs.setRegisterLocation(
1015 Reg1, dwarf::UnwindLocation::createAtCFAPlusOffset(-8));
1016 VerifyLocs.setRegisterLocation(
1017 Reg2, dwarf::UnwindLocation::createAtCFAPlusOffset(-16));
1018 VerifyLocs.setRegisterLocation(
1019 Reg3, dwarf::UnwindLocation::createAtCFAPlusOffset(8));
1020
1021 // Verify we catch state machine error.
1022 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1023 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1024 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1025 EXPECT_EQ(Rows.size(), 1u);
1026 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1027 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1028 }
1029
1030 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_val_offset) {
1031 // Test that DW_CFA_val_offset and DW_CFA_val_offset_sf work as expected when
1032 // parsed in the state machine.
1033 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1034 /*Offset=*/0x0,
1035 /*Length=*/0xff);
1036
1037 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1038 /*Offset=*/0x3333abcdabcd,
1039 /*Length=*/0x4444abcdabcd,
1040 /*CIEPointer=*/0x1111abcdabcd,
1041 /*InitialLocation=*/0x1000,
1042 /*AddressRange=*/0x1000,
1043 /*Cie=*/&TestCIE,
1044 /*LSDAAddress=*/std::nullopt,
1045 /*Arch=*/Triple::x86_64);
1046
1047 // Make a CIE that has a valid CFA definition and a single register unwind
1048 // rule for register that we will verify is in all of the pushed rows.
1049 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1050 Succeeded());
1051
1052 // Make a FDE with DWARF call frame instruction opcodes that changes the
1053 // unwind rules for the follwing registers:
1054 // Reg1 = [CFA-8]
1055 // Reg2 = [CFA-16]
1056 // Reg3 = [CFA+8]
1057 constexpr uint8_t Reg1 = 14;
1058 constexpr uint8_t Reg2 = 15;
1059 constexpr uint8_t Neg1SLEB = 0x7f;
1060 EXPECT_THAT_ERROR(
1061 parseCFI(TestFDE, {dwarf::DW_CFA_val_offset, Reg1, 1,
1062 dwarf::DW_CFA_val_offset_sf, Reg2, Neg1SLEB}),
1063 Succeeded());
1064
1065 // Create locations that we expect the UnwindRow objects to contain after
1066 // parsing the DWARF call frame instructions.
1067 dwarf::RegisterLocations VerifyLocs;
1068 VerifyLocs.setRegisterLocation(
1069 Reg1, dwarf::UnwindLocation::createIsCFAPlusOffset(-8));
1070 VerifyLocs.setRegisterLocation(
1071 Reg2, dwarf::UnwindLocation::createIsCFAPlusOffset(8));
1072
1073 // Verify we catch state machine error.
1074 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1075 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1076 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1077 EXPECT_EQ(Rows.size(), 1u);
1078 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1079 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1080 }
1081
1082 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_nop) {
1083 // Test that DW_CFA_nop works as expected when parsed in the state machine.
1084 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1085 /*Offset=*/0x0,
1086 /*Length=*/0xff);
1087
1088 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1089 /*Offset=*/0x3333abcdabcd,
1090 /*Length=*/0x4444abcdabcd,
1091 /*CIEPointer=*/0x1111abcdabcd,
1092 /*InitialLocation=*/0x1000,
1093 /*AddressRange=*/0x1000,
1094 /*Cie=*/&TestCIE,
1095 /*LSDAAddress=*/std::nullopt,
1096 /*Arch=*/Triple::x86_64);
1097
1098 // Make a CIE that has a valid CFA definition and a single register unwind
1099 // rule for register that we will verify is in all of the pushed rows.
1100 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1101 Succeeded());
1102
1103 // Make a FDE with DWARF call frame instruction opcodes that changes the
1104 // unwind rules for the follwing registers:
1105 // Reg1 = [CFA-8]
1106 // The opcodes for setting Reg1 are preceded by a DW_CFA_nop.
1107 constexpr uint8_t Reg1 = 14;
1108 EXPECT_THAT_ERROR(
1109 parseCFI(TestFDE, {dwarf::DW_CFA_nop, dwarf::DW_CFA_offset | Reg1, 1}),
1110 Succeeded());
1111
1112 // Create locations that we expect the UnwindRow objects to contain after
1113 // parsing the DWARF call frame instructions.
1114 dwarf::RegisterLocations VerifyLocs;
1115 VerifyLocs.setRegisterLocation(
1116 Reg1, dwarf::UnwindLocation::createAtCFAPlusOffset(-8));
1117
1118 // Verify we catch state machine error.
1119 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1120 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1121 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1122 EXPECT_EQ(Rows.size(), 1u);
1123 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1124 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1125 }
1126
1127 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_remember_state) {
1128 // Test that DW_CFA_remember_state and DW_CFA_restore_state work as expected
1129 // when parsed in the state machine.
1130 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1131 /*Offset=*/0x0,
1132 /*Length=*/0xff);
1133
1134 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1135 /*Offset=*/0x3333abcdabcd,
1136 /*Length=*/0x4444abcdabcd,
1137 /*CIEPointer=*/0x1111abcdabcd,
1138 /*InitialLocation=*/0x1000,
1139 /*AddressRange=*/0x1000,
1140 /*Cie=*/&TestCIE,
1141 /*LSDAAddress=*/std::nullopt,
1142 /*Arch=*/Triple::x86_64);
1143
1144 // Make a CIE that has a valid CFA definition and a single register unwind
1145 // rule for register that we will verify is in all of the pushed rows.
1146 constexpr uint8_t CFAOff1 = 32;
1147 constexpr uint8_t CFAOff2 = 16;
1148 constexpr uint8_t Reg1 = 14;
1149 constexpr uint8_t Reg2 = 15;
1150 constexpr uint8_t Reg3 = 16;
1151
1152 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, CFAOff1}),
1153 Succeeded());
1154
1155 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1156 // follwing rows:
1157 // 0x1000: CFA=reg12+CFAOff1: Reg1=[CFA-8]
1158 // 0x1004: CFA=reg12+CFAOff1: Reg1=[CFA-8] Reg2=[CFA-16]
1159 // 0x1008: CFA=reg12+CFAOff2: Reg1=[CFA-8] Reg2=[CFA-16] Reg3=[CFA-24]
1160 // 0x100C: CFA=reg12+CFAOff1: Reg1=[CFA-8] Reg2=[CFA-16]
1161 // 0x1010: CFA=reg12+CFAOff1: Reg1=[CFA-8]
1162 // This state machine will:
1163 // - set Reg1 location
1164 // - push a row (from DW_CFA_advance_loc)
1165 // - remember the state
1166 // - set Reg2 location
1167 // - push a row (from DW_CFA_advance_loc)
1168 // - remember the state
1169 // - set CFA offset to CFAOff2
1170 // - set Reg3 location
1171 // - push a row (from DW_CFA_advance_loc)
1172 // - remember the state where Reg1 and Reg2 were set
1173 // - push a row (from DW_CFA_advance_loc)
1174 // - remember the state where only Reg1 was set
1175 // - push a row (automatically at the end of instruction parsing)
1176 // Then we verify that all registers are correct in all generated rows.
1177 EXPECT_THAT_ERROR(
1178 parseCFI(TestFDE,
1179 {dwarf::DW_CFA_offset | Reg1, 1, dwarf::DW_CFA_advance_loc | 4,
1180 dwarf::DW_CFA_remember_state, dwarf::DW_CFA_offset | Reg2, 2,
1181 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_remember_state,
1182 dwarf::DW_CFA_def_cfa_offset, CFAOff2,
1183 dwarf::DW_CFA_offset | Reg3, 3, dwarf::DW_CFA_advance_loc | 4,
1184 dwarf::DW_CFA_restore_state, dwarf::DW_CFA_advance_loc | 4,
1185 dwarf::DW_CFA_restore_state}),
1186 Succeeded());
1187
1188 // Create locations that we expect the UnwindRow objects to contain after
1189 // parsing the DWARF call frame instructions.
1190 dwarf::RegisterLocations VerifyLocs1;
1191 VerifyLocs1.setRegisterLocation(
1192 Reg1, dwarf::UnwindLocation::createAtCFAPlusOffset(-8));
1193
1194 dwarf::RegisterLocations VerifyLocs2;
1195 VerifyLocs2.setRegisterLocation(
1196 Reg1, dwarf::UnwindLocation::createAtCFAPlusOffset(-8));
1197 VerifyLocs2.setRegisterLocation(
1198 Reg2, dwarf::UnwindLocation::createAtCFAPlusOffset(-16));
1199
1200 dwarf::RegisterLocations VerifyLocs3;
1201 VerifyLocs3.setRegisterLocation(
1202 Reg1, dwarf::UnwindLocation::createAtCFAPlusOffset(-8));
1203 VerifyLocs3.setRegisterLocation(
1204 Reg2, dwarf::UnwindLocation::createAtCFAPlusOffset(-16));
1205 VerifyLocs3.setRegisterLocation(
1206 Reg3, dwarf::UnwindLocation::createAtCFAPlusOffset(-24));
1207
1208 // Verify we catch state machine error.
1209 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1210 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1211 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1212 EXPECT_EQ(Rows.size(), 5u);
1213 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1214 EXPECT_EQ(Rows[0].getCFAValue(),
1215 dwarf::UnwindLocation::createIsRegisterPlusOffset(12, CFAOff1));
1216 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs1);
1217
1218 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
1219 EXPECT_EQ(Rows[1].getCFAValue(),
1220 dwarf::UnwindLocation::createIsRegisterPlusOffset(12, CFAOff1));
1221 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs2);
1222
1223 EXPECT_EQ(Rows[2].getAddress(), 0x1008u);
1224 EXPECT_EQ(Rows[2].getCFAValue(),
1225 dwarf::UnwindLocation::createIsRegisterPlusOffset(12, CFAOff2));
1226 EXPECT_EQ(Rows[2].getRegisterLocations(), VerifyLocs3);
1227
1228 EXPECT_EQ(Rows[3].getAddress(), 0x100Cu);
1229 EXPECT_EQ(Rows[3].getCFAValue(),
1230 dwarf::UnwindLocation::createIsRegisterPlusOffset(12, CFAOff1));
1231 EXPECT_EQ(Rows[3].getRegisterLocations(), VerifyLocs2);
1232
1233 EXPECT_EQ(Rows[4].getAddress(), 0x1010u);
1234 EXPECT_EQ(Rows[4].getCFAValue(),
1235 dwarf::UnwindLocation::createIsRegisterPlusOffset(12, CFAOff1));
1236 EXPECT_EQ(Rows[4].getRegisterLocations(), VerifyLocs1);
1237 }
1238
1239 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_undefined) {
1240 // Test that DW_CFA_undefined works as expected when parsed in the state
1241 // machine.
1242 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1243 /*Offset=*/0x0,
1244 /*Length=*/0xff);
1245
1246 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1247 /*Offset=*/0x3333abcdabcd,
1248 /*Length=*/0x4444abcdabcd,
1249 /*CIEPointer=*/0x1111abcdabcd,
1250 /*InitialLocation=*/0x1000,
1251 /*AddressRange=*/0x1000,
1252 /*Cie=*/&TestCIE,
1253 /*LSDAAddress=*/std::nullopt,
1254 /*Arch=*/Triple::x86_64);
1255
1256 // Make a CIE that has a valid CFA definition and a single register unwind
1257 // rule for register that we will verify is in all of the pushed rows.
1258 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1259 Succeeded());
1260
1261 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1262 // follwing rows:
1263 // 0x1000: CFA=reg12+32: Reg1=undefined
1264 // Then we verify that all registers are correct in all generated rows.
1265 constexpr uint8_t Reg1 = 14;
1266 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_undefined, Reg1}),
1267 Succeeded());
1268
1269 // Create locations that we expect the UnwindRow objects to contain after
1270 // parsing the DWARF call frame instructions.
1271 dwarf::RegisterLocations VerifyLocs;
1272 VerifyLocs.setRegisterLocation(Reg1,
1273 dwarf::UnwindLocation::createUndefined());
1274
1275 // Verify we catch state machine error.
1276 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1277 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1278 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1279 EXPECT_EQ(Rows.size(), 1u);
1280 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1281 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1282 }
1283
1284 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_same_value) {
1285 // Test that DW_CFA_same_value works as expected when parsed in the state
1286 // machine.
1287 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1288 /*Offset=*/0x0,
1289 /*Length=*/0xff);
1290
1291 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1292 /*Offset=*/0x3333abcdabcd,
1293 /*Length=*/0x4444abcdabcd,
1294 /*CIEPointer=*/0x1111abcdabcd,
1295 /*InitialLocation=*/0x1000,
1296 /*AddressRange=*/0x1000,
1297 /*Cie=*/&TestCIE,
1298 /*LSDAAddress=*/std::nullopt,
1299 /*Arch=*/Triple::x86_64);
1300
1301 // Make a CIE that has a valid CFA definition and a single register unwind
1302 // rule for register that we will verify is in all of the pushed rows.
1303 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1304 Succeeded());
1305
1306 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1307 // follwing rows:
1308 // 0x1000: CFA=reg12+32: Reg1=same
1309 // Then we verify that all registers are correct in all generated rows.
1310 constexpr uint8_t Reg1 = 14;
1311 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_same_value, Reg1}),
1312 Succeeded());
1313
1314 // Create locations that we expect the UnwindRow objects to contain after
1315 // parsing the DWARF call frame instructions.
1316 dwarf::RegisterLocations VerifyLocs;
1317 VerifyLocs.setRegisterLocation(Reg1, dwarf::UnwindLocation::createSame());
1318
1319 // Verify we catch state machine error.
1320 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1321 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1322 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1323 EXPECT_EQ(Rows.size(), 1u);
1324 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1325 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1326 }
1327
1328 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_register) {
1329 // Test that DW_CFA_register works as expected when parsed in the state
1330 // machine.
1331 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1332 /*Offset=*/0x0,
1333 /*Length=*/0xff);
1334
1335 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1336 /*Offset=*/0x3333abcdabcd,
1337 /*Length=*/0x4444abcdabcd,
1338 /*CIEPointer=*/0x1111abcdabcd,
1339 /*InitialLocation=*/0x1000,
1340 /*AddressRange=*/0x1000,
1341 /*Cie=*/&TestCIE,
1342 /*LSDAAddress=*/std::nullopt,
1343 /*Arch=*/Triple::x86_64);
1344
1345 // Make a CIE that has a valid CFA definition and a single register unwind
1346 // rule for register that we will verify is in all of the pushed rows.
1347 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1348 Succeeded());
1349
1350 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1351 // follwing rows:
1352 // 0x1000: CFA=reg12+32: Reg1=same
1353 // Then we verify that all registers are correct in all generated rows.
1354 constexpr uint8_t Reg = 13;
1355 constexpr uint8_t InReg = 14;
1356 EXPECT_THAT_ERROR(parseCFI(TestFDE, {dwarf::DW_CFA_register, Reg, InReg}),
1357 Succeeded());
1358
1359 // Create locations that we expect the UnwindRow objects to contain after
1360 // parsing the DWARF call frame instructions.
1361 dwarf::RegisterLocations VerifyLocs;
1362 VerifyLocs.setRegisterLocation(
1363 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
1364
1365 // Verify we catch state machine error.
1366 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1367 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1368 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1369 EXPECT_EQ(Rows.size(), 1u);
1370 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1371 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1372 }
1373
1374 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_expression) {
1375 // Test that DW_CFA_expression works as expected when parsed in the state
1376 // machine.
1377 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1378 /*Offset=*/0x0,
1379 /*Length=*/0xff);
1380
1381 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1382 /*Offset=*/0x3333abcdabcd,
1383 /*Length=*/0x4444abcdabcd,
1384 /*CIEPointer=*/0x1111abcdabcd,
1385 /*InitialLocation=*/0x1000,
1386 /*AddressRange=*/0x1000,
1387 /*Cie=*/&TestCIE,
1388 /*LSDAAddress=*/std::nullopt,
1389 /*Arch=*/Triple::x86_64);
1390
1391 // Make a CIE that has a valid CFA definition and a single register unwind
1392 // rule for register that we will verify is in all of the pushed rows.
1393 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1394 Succeeded());
1395
1396 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1397 // follwing rows:
1398 // 0x1000: CFA=reg12+32: Reg1=DWARFExpr(DW_OP_reg12)
1399 // Then we verify that all registers are correct in all generated rows.
1400 constexpr uint8_t Reg = 13;
1401 constexpr uint8_t AddrSize = 8;
1402 std::vector<uint8_t> CFIBytes = {dwarf::DW_CFA_expression, Reg, 1,
1403 dwarf::DW_OP_reg12};
1404
1405 EXPECT_THAT_ERROR(parseCFI(TestFDE, CFIBytes), Succeeded());
1406
1407 // Create locations that we expect the UnwindRow objects to contain after
1408 // parsing the DWARF call frame instructions.
1409 dwarf::RegisterLocations VerifyLocs;
1410
1411 std::vector<uint8_t> ExprBytes = {dwarf::DW_OP_reg12};
1412 DataExtractor ExprData(ExprBytes, true, AddrSize);
1413 DWARFExpression Expr(ExprData, AddrSize);
1414 VerifyLocs.setRegisterLocation(
1415 Reg, dwarf::UnwindLocation::createAtDWARFExpression(Expr));
1416
1417 // Verify we catch state machine error.
1418 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1419 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1420 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1421 EXPECT_EQ(Rows.size(), 1u);
1422 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1423 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1424 }
1425
1426 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_val_expression) {
1427 // Test that DW_CFA_val_expression works as expected when parsed in the state
1428 // machine.
1429 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1430 /*Offset=*/0x0,
1431 /*Length=*/0xff);
1432
1433 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1434 /*Offset=*/0x3333abcdabcd,
1435 /*Length=*/0x4444abcdabcd,
1436 /*CIEPointer=*/0x1111abcdabcd,
1437 /*InitialLocation=*/0x1000,
1438 /*AddressRange=*/0x1000,
1439 /*Cie=*/&TestCIE,
1440 /*LSDAAddress=*/std::nullopt,
1441 /*Arch=*/Triple::x86_64);
1442
1443 // Make a CIE that has a valid CFA definition and a single register unwind
1444 // rule for register that we will verify is in all of the pushed rows.
1445 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, 12, 32}),
1446 Succeeded());
1447
1448 // Make a FDE with DWARF call frame instruction opcodes that encodes the
1449 // follwing rows:
1450 // 0x1000: CFA=reg12+32: Reg1=DWARFExpr(DW_OP_reg12)
1451 // Then we verify that all registers are correct in all generated rows.
1452 constexpr uint8_t Reg = 13;
1453 constexpr uint8_t AddrSize = 8;
1454 std::vector<uint8_t> CFIBytes = {dwarf::DW_CFA_val_expression, Reg, 1,
1455 dwarf::DW_OP_reg12};
1456
1457 EXPECT_THAT_ERROR(parseCFI(TestFDE, CFIBytes), Succeeded());
1458
1459 // Create locations that we expect the UnwindRow objects to contain after
1460 // parsing the DWARF call frame instructions.
1461 dwarf::RegisterLocations VerifyLocs;
1462
1463 std::vector<uint8_t> ExprBytes = {dwarf::DW_OP_reg12};
1464 DataExtractor ExprData(ExprBytes, true, AddrSize);
1465 DWARFExpression Expr(ExprData, AddrSize);
1466 VerifyLocs.setRegisterLocation(
1467 Reg, dwarf::UnwindLocation::createIsDWARFExpression(Expr));
1468
1469 // Verify we catch state machine error.
1470 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1471 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1472 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1473 EXPECT_EQ(Rows.size(), 1u);
1474 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1475 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1476 }
1477
1478 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_def_cfa) {
1479 // Test that DW_CFA_def_cfa, DW_CFA_def_cfa_sf, DW_CFA_def_cfa_register,
1480 // DW_CFA_def_cfa_offset, and DW_CFA_def_cfa_offset_sf works as expected when
1481 // parsed in the state machine.
1482 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1483 /*Offset=*/0x0,
1484 /*Length=*/0xff);
1485
1486 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1487 /*Offset=*/0x3333abcdabcd,
1488 /*Length=*/0x4444abcdabcd,
1489 /*CIEPointer=*/0x1111abcdabcd,
1490 /*InitialLocation=*/0x1000,
1491 /*AddressRange=*/0x1000,
1492 /*Cie=*/&TestCIE,
1493 /*LSDAAddress=*/std::nullopt,
1494 /*Arch=*/Triple::x86_64);
1495
1496 // Make a CIE that has a valid CFA definition and a single register unwind
1497 // rule for register that we will verify is in all of the pushed rows.
1498 constexpr uint8_t CFAReg1 = 12;
1499 constexpr uint8_t CFAOff1 = 32;
1500 constexpr uint8_t CFAReg2 = 13;
1501 constexpr uint8_t CFAOff2 = 48;
1502 constexpr uint8_t Reg = 13;
1503 constexpr uint8_t InReg = 14;
1504
1505 EXPECT_THAT_ERROR(parseCFI(TestCIE, {dwarf::DW_CFA_def_cfa, CFAReg1, CFAOff1,
1506 dwarf::DW_CFA_register, Reg, InReg}),
1507 Succeeded());
1508
1509 // Make a FDE with DWARF call frame instruction opcodes that use all of the
1510 // DW_CFA_def_cfa* opcodes. This will verify that all opcodes that should
1511 // create a row are correctly working.
1512 EXPECT_THAT_ERROR(
1513 parseCFI(
1514 TestFDE,
1515 {
1516 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_register,
1517 CFAReg2, dwarf::DW_CFA_advance_loc | 4,
1518 dwarf::DW_CFA_def_cfa_offset, CFAOff2,
1519 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_offset_sf,
1520 0x7c, // -4 SLEB to make offset = 32 (CFAOff1)
1521 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_sf, CFAReg1,
1522 0x7a, // -6 SLEB to make CFA offset 48 (CFAOff2)
1523 }),
1524 Succeeded());
1525
1526 // Create locations that we expect the UnwindRow objects to contain after
1527 // parsing the DWARF call frame instructions.
1528 dwarf::RegisterLocations VerifyLocs;
1529 VerifyLocs.setRegisterLocation(
1530 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
1531
1532 // Verify we catch state machine error.
1533 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1534 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1535 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1536 EXPECT_EQ(Rows.size(), 5u);
1537 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1538 EXPECT_EQ(
1539 Rows[0].getCFAValue(),
1540 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg1, CFAOff1));
1541 EXPECT_EQ(Rows[0].getRegisterLocations().size(), 1u);
1542 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1543
1544 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
1545 EXPECT_EQ(
1546 Rows[1].getCFAValue(),
1547 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff1));
1548 EXPECT_EQ(Rows[1].getRegisterLocations().size(), 1u);
1549 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs);
1550
1551 EXPECT_EQ(Rows[2].getAddress(), 0x1008u);
1552 EXPECT_EQ(
1553 Rows[2].getCFAValue(),
1554 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff2));
1555 EXPECT_EQ(Rows[2].getRegisterLocations().size(), 1u);
1556 EXPECT_EQ(Rows[2].getRegisterLocations(), VerifyLocs);
1557
1558 EXPECT_EQ(Rows[3].getAddress(), 0x100cu);
1559 EXPECT_EQ(
1560 Rows[3].getCFAValue(),
1561 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff1));
1562 EXPECT_EQ(Rows[3].getRegisterLocations().size(), 1u);
1563 EXPECT_EQ(Rows[3].getRegisterLocations(), VerifyLocs);
1564
1565 EXPECT_EQ(Rows[4].getAddress(), 0x1010u);
1566 EXPECT_EQ(
1567 Rows[4].getCFAValue(),
1568 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg1, CFAOff2));
1569 EXPECT_EQ(Rows[4].getRegisterLocations().size(), 1u);
1570 EXPECT_EQ(Rows[4].getRegisterLocations(), VerifyLocs);
1571 }
1572
1573 TEST(DWARFDebugFrame, UnwindTable_DW_CFA_LLVM_def_aspace_cfa) {
1574 // Test that DW_CFA_LLVM_def_aspace_cfa, DW_CFA_LLVM_def_aspace_cfa_sf,
1575 // DW_CFA_def_cfa_register, DW_CFA_def_cfa_offset, and
1576 // DW_CFA_def_cfa_offset_sf works as expected when parsed in the state
1577 // machine.
1578 dwarf::CIE TestCIE = createCIE(/*IsDWARF64=*/false,
1579 /*Offset=*/0x0,
1580 /*Length=*/0xff);
1581
1582 dwarf::FDE TestFDE(/*IsDWARF64=*/true,
1583 /*Offset=*/0x3333abcdabcd,
1584 /*Length=*/0x4444abcdabcd,
1585 /*CIEPointer=*/0x1111abcdabcd,
1586 /*InitialLocation=*/0x1000,
1587 /*AddressRange=*/0x1000,
1588 /*Cie=*/&TestCIE,
1589 /*LSDAAddress=*/std::nullopt,
1590 /*Arch=*/Triple::x86_64);
1591
1592 // Make a CIE that has a valid CFA definition and a single register unwind
1593 // rule for register that we will verify is in all of the pushed rows.
1594 constexpr uint8_t CFAReg1 = 12;
1595 constexpr uint8_t CFAOff1 = 32;
1596 constexpr uint8_t CFAReg2 = 13;
1597 constexpr uint8_t CFAOff2 = 48;
1598 constexpr uint8_t Reg = 13;
1599 constexpr uint8_t InReg = 14;
1600 constexpr uint8_t AddrSpace = 2;
1601
1602 EXPECT_THAT_ERROR(
1603 parseCFI(TestCIE, {dwarf::DW_CFA_LLVM_def_aspace_cfa, CFAReg1, CFAOff1,
1604 AddrSpace, dwarf::DW_CFA_register, Reg, InReg}),
1605 Succeeded());
1606
1607 // Make a FDE with DWARF call frame instruction opcodes that use all of the
1608 // DW_CFA_def_cfa* opcodes. This will verify that all opcodes that should
1609 // create a row are correctly working.
1610 EXPECT_THAT_ERROR(
1611 parseCFI(
1612 TestFDE,
1613 {
1614 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_register,
1615 CFAReg2, dwarf::DW_CFA_advance_loc | 4,
1616 dwarf::DW_CFA_def_cfa_offset, CFAOff2,
1617 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_offset_sf,
1618 0x7c, // -4 SLEB to make offset = 32 (CFAOff1)
1619 dwarf::DW_CFA_advance_loc | 4, dwarf::DW_CFA_def_cfa_sf, CFAReg1,
1620 0x7a, // -6 SLEB to make CFA offset 48 (CFAOff2)
1621 }),
1622 Succeeded());
1623
1624 // Create locations that we expect the UnwindRow objects to contain after
1625 // parsing the DWARF call frame instructions.
1626 dwarf::RegisterLocations VerifyLocs;
1627 VerifyLocs.setRegisterLocation(
1628 Reg, dwarf::UnwindLocation::createIsRegisterPlusOffset(InReg, 0));
1629
1630 // Verify we catch state machine error.
1631 Expected<dwarf::UnwindTable> RowsOrErr = dwarf::UnwindTable::create(&TestFDE);
1632 EXPECT_THAT_ERROR(RowsOrErr.takeError(), Succeeded());
1633 const dwarf::UnwindTable &Rows = RowsOrErr.get();
1634 EXPECT_EQ(Rows.size(), 5u);
1635 EXPECT_EQ(Rows[0].getAddress(), 0x1000u);
1636 EXPECT_EQ(Rows[0].getCFAValue(),
1637 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg1, CFAOff1,
1638 AddrSpace));
1639 EXPECT_EQ(Rows[0].getRegisterLocations().size(), 1u);
1640 EXPECT_EQ(Rows[0].getRegisterLocations(), VerifyLocs);
1641
1642 EXPECT_EQ(Rows[1].getAddress(), 0x1004u);
1643 EXPECT_EQ(Rows[1].getCFAValue(),
1644 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff1,
1645 AddrSpace));
1646 EXPECT_EQ(Rows[1].getRegisterLocations().size(), 1u);
1647 EXPECT_EQ(Rows[1].getRegisterLocations(), VerifyLocs);
1648
1649 EXPECT_EQ(Rows[2].getAddress(), 0x1008u);
1650 EXPECT_EQ(Rows[2].getCFAValue(),
1651 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff2,
1652 AddrSpace));
1653 EXPECT_EQ(Rows[2].getRegisterLocations().size(), 1u);
1654 EXPECT_EQ(Rows[2].getRegisterLocations(), VerifyLocs);
1655
1656 EXPECT_EQ(Rows[3].getAddress(), 0x100cu);
1657 EXPECT_EQ(Rows[3].getCFAValue(),
1658 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg2, CFAOff1,
1659 AddrSpace));
1660 EXPECT_EQ(Rows[3].getRegisterLocations().size(), 1u);
1661 EXPECT_EQ(Rows[3].getRegisterLocations(), VerifyLocs);
1662
1663 EXPECT_EQ(Rows[4].getAddress(), 0x1010u);
1664 EXPECT_EQ(Rows[4].getCFAValue(),
1665 dwarf::UnwindLocation::createIsRegisterPlusOffset(CFAReg1, CFAOff2,
1666 AddrSpace));
1667 EXPECT_EQ(Rows[4].getRegisterLocations().size(), 1u);
1668 EXPECT_EQ(Rows[4].getRegisterLocations(), VerifyLocs);
1669 }
1670
1671 } // end anonymous namespace