1 //===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/BinaryFormat/Dwarf.h"
14 #include "llvm/Support/Host.h"
15 #include "llvm/Support/LEB128.h"
16 #include "gtest/gtest.h"
17 #include <climits>
18 using namespace llvm;
19 using namespace dwarf;
20
21 namespace {
22
isFormClass(dwarf::Form Form,DWARFFormValue::FormClass FC)23 bool isFormClass(dwarf::Form Form, DWARFFormValue::FormClass FC) {
24 return DWARFFormValue(Form).isFormClass(FC);
25 }
26
TEST(DWARFFormValue,FormClass)27 TEST(DWARFFormValue, FormClass) {
28 EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address));
29 EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address));
30 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant));
31 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset));
32 EXPECT_TRUE(
33 isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset));
34 EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String));
35 EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address));
36 EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address));
37 EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference));
38 EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
39 }
40
41 template<typename RawTypeT>
createDataXFormValue(dwarf::Form Form,RawTypeT Value)42 DWARFFormValue createDataXFormValue(dwarf::Form Form, RawTypeT Value) {
43 char Raw[sizeof(RawTypeT)];
44 memcpy(Raw, &Value, sizeof(RawTypeT));
45 uint32_t Offset = 0;
46 DWARFFormValue Result(Form);
47 DWARFDataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
48 sys::IsLittleEndianHost, sizeof(void *));
49 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
50 return Result;
51 }
52
createULEBFormValue(uint64_t Value)53 DWARFFormValue createULEBFormValue(uint64_t Value) {
54 SmallString<10> RawData;
55 raw_svector_ostream OS(RawData);
56 encodeULEB128(Value, OS);
57 uint32_t Offset = 0;
58 DWARFFormValue Result(DW_FORM_udata);
59 DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
60 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
61 return Result;
62 }
63
createSLEBFormValue(int64_t Value)64 DWARFFormValue createSLEBFormValue(int64_t Value) {
65 SmallString<10> RawData;
66 raw_svector_ostream OS(RawData);
67 encodeSLEB128(Value, OS);
68 uint32_t Offset = 0;
69 DWARFFormValue Result(DW_FORM_sdata);
70 DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
71 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
72 return Result;
73 }
74
TEST(DWARFFormValue,SignedConstantForms)75 TEST(DWARFFormValue, SignedConstantForms) {
76 // Check that we correctly sign extend fixed size forms.
77 auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
78 auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
79 auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
80 auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
81 EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
82 EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
83 EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
84 EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
85
86 // Check that we can handle big positive values, but that we return
87 // an error just over the limit.
88 auto UMax = createULEBFormValue(LLONG_MAX);
89 auto TooBig = createULEBFormValue(uint64_t(LLONG_MAX) + 1);
90 EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
91 EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
92
93 // Sanity check some other forms.
94 auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
95 auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
96 auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
97 auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
98 auto LEBMin = createSLEBFormValue(LLONG_MIN);
99 auto LEBMax = createSLEBFormValue(LLONG_MAX);
100 auto LEB1 = createSLEBFormValue(-42);
101 auto LEB2 = createSLEBFormValue(42);
102 EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
103 EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
104 EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
105 EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
106 EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
107 EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
108 EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
109 EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
110
111 // Data16 is a little tricky.
112 char Cksum[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
113 DWARFFormValue Data16(DW_FORM_data16);
114 DWARFDataExtractor DE16(StringRef(Cksum, 16), sys::IsLittleEndianHost,
115 sizeof(void *));
116 uint32_t Offset = 0;
117 Data16.extractValue(DE16, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
118 SmallString<32> Str;
119 raw_svector_ostream Res(Str);
120 Data16.dump(Res, DIDumpOptions());
121 EXPECT_EQ(memcmp(Str.data(), "000102030405060708090a0b0c0d0e0f", 32), 0);
122 }
123
124 } // end anonymous namespace
125