[obj2yaml] - Stop triggering UB when dumping corrupted strings.
[llvm-complete.git] / unittests / MC / DwarfLineTables.cpp
blob88e9565e1ca20dd10619d8f2b061d2625075b824
1 //===- llvm/unittest/MC/DwarfLineTables.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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/STLExtras.h"
10 #include "llvm/BinaryFormat/Dwarf.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCDwarf.h"
14 #include "llvm/MC/MCRegisterInfo.h"
15 #include "llvm/Support/TargetRegistry.h"
16 #include "llvm/Support/TargetSelect.h"
17 #include "gtest/gtest.h"
19 using namespace llvm;
21 namespace {
22 struct Context {
23 const char *Triple = "x86_64-pc-linux";
24 std::unique_ptr<MCRegisterInfo> MRI;
25 std::unique_ptr<MCAsmInfo> MAI;
26 std::unique_ptr<MCContext> Ctx;
28 Context() {
29 llvm::InitializeAllTargetInfos();
30 llvm::InitializeAllTargetMCs();
31 llvm::InitializeAllDisassemblers();
33 // If we didn't build x86, do not run the test.
34 std::string Error;
35 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
36 if (!TheTarget)
37 return;
39 MRI.reset(TheTarget->createMCRegInfo(Triple));
40 MAI.reset(TheTarget->createMCAsmInfo(*MRI, Triple));
41 Ctx = std::make_unique<MCContext>(MAI.get(), MRI.get(), nullptr);
44 operator bool() { return Ctx.get(); }
45 operator MCContext &() { return *Ctx; };
48 Context &getContext() {
49 static Context Ctxt;
50 return Ctxt;
54 void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta,
55 ArrayRef<uint8_t> ExpectedEncoding) {
56 SmallString<16> Buffer;
57 raw_svector_ostream EncodingOS(Buffer);
58 MCDwarfLineAddr::Encode(getContext(), Params, LineDelta, AddrDelta,
59 EncodingOS);
60 EXPECT_EQ(ExpectedEncoding, arrayRefFromStringRef(Buffer));
63 TEST(DwarfLineTables, TestDefaultParams) {
64 if (!getContext())
65 return;
67 MCDwarfLineTableParams Params;
69 // Minimal line offset expressible through extended opcode, 0 addr delta
70 const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5
71 verifyEncoding(Params, -5, 0, Encoding0);
73 // Maximal line offset expressible through extended opcode,
74 const uint8_t Encoding1[] = {26}; // Special opcode Addr += 0, Line += +8
75 verifyEncoding(Params, 8, 0, Encoding1);
77 // Random value in the middle of the special ocode range
78 const uint8_t Encoding2[] = {146}; // Special opcode Addr += 9, Line += 2
79 verifyEncoding(Params, 2, 9, Encoding2);
81 // Minimal line offset expressible through extended opcode, max addr delta
82 const uint8_t Encoding3[] = {251}; // Special opcode Addr += 17, Line += -5
83 verifyEncoding(Params, -5, 17, Encoding3);
85 // Biggest special opcode
86 const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1
87 verifyEncoding(Params, -1, 17, Encoding4);
89 // Line delta outside of the special opcode range, address delta in range
90 const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9,
91 158}; // Special opcode Addr += 10, Line += 0
92 verifyEncoding(Params, 9, 10, Encoding5);
94 // Address delta outside of the special opcode range, but small
95 // enough to do DW_LNS_const_add_pc + special opcode.
96 const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 17
97 62}; // Special opcode Addr += 3, Line += 2
98 verifyEncoding(Params, 2, 20, Encoding6);
100 // Address delta big enough to require the use of DW_LNS_advance_pc
101 // Line delta in special opcode range
102 const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100,
103 20}; // Special opcode Addr += 0, Line += 2
104 verifyEncoding(Params, 2, 100, Encoding7);
106 // No special opcode possible.
107 const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20,
108 dwarf::DW_LNS_advance_pc, 100,
109 dwarf::DW_LNS_copy};
110 verifyEncoding(Params, 20, 100, Encoding8);
113 TEST(DwarfLineTables, TestCustomParams) {
114 if (!getContext())
115 return;
117 // Some tests against the example values given in the standard.
118 MCDwarfLineTableParams Params;
119 Params.DWARF2LineOpcodeBase = 13;
120 Params.DWARF2LineBase = -3;
121 Params.DWARF2LineRange = 12;
123 // Minimal line offset expressible through extended opcode, 0 addr delta
124 const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5
125 verifyEncoding(Params, -3, 0, Encoding0);
127 // Maximal line offset expressible through extended opcode,
128 const uint8_t Encoding1[] = {24}; // Special opcode Addr += 0, Line += +8
129 verifyEncoding(Params, 8, 0, Encoding1);
131 // Random value in the middle of the special ocode range
132 const uint8_t Encoding2[] = {126}; // Special opcode Addr += 9, Line += 2
133 verifyEncoding(Params, 2, 9, Encoding2);
135 // Minimal line offset expressible through extended opcode, max addr delta
136 const uint8_t Encoding3[] = {253}; // Special opcode Addr += 20, Line += -3
137 verifyEncoding(Params, -3, 20, Encoding3);
139 // Biggest special opcode
140 const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1
141 verifyEncoding(Params, -1, 20, Encoding4);
143 // Line delta outside of the special opcode range, address delta in range
144 const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9,
145 136}; // Special opcode Addr += 10, Line += 0
146 verifyEncoding(Params, 9, 10, Encoding5);
148 // Address delta outside of the special opcode range, but small
149 // enough to do DW_LNS_const_add_pc + special opcode.
150 const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 20
151 138}; // Special opcode Addr += 10, Line += 2
152 verifyEncoding(Params, 2, 30, Encoding6);
154 // Address delta big enough to require the use of DW_LNS_advance_pc
155 // Line delta in special opcode range
156 const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100,
157 18}; // Special opcode Addr += 0, Line += 2
158 verifyEncoding(Params, 2, 100, Encoding7);
160 // No special opcode possible.
161 const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20,
162 dwarf::DW_LNS_advance_pc, 100,
163 dwarf::DW_LNS_copy};
164 verifyEncoding(Params, 20, 100, Encoding8);
167 TEST(DwarfLineTables, TestCustomParams2) {
168 if (!getContext())
169 return;
171 // Corner case param values.
172 MCDwarfLineTableParams Params;
173 Params.DWARF2LineOpcodeBase = 13;
174 Params.DWARF2LineBase = 1;
175 Params.DWARF2LineRange = 255;
177 const uint8_t Encoding0[] = {dwarf::DW_LNS_advance_line, 248, 1,
178 dwarf::DW_LNS_copy};
179 verifyEncoding(Params, 248, 0, Encoding0);