DWARFFormValueTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/Support/Dwarf.h"
  12. #include "llvm/Support/Host.h"
  13. #include "llvm/Support/LEB128.h"
  14. #include "gtest/gtest.h"
  15. #include <climits>
  16. using namespace llvm;
  17. using namespace dwarf;
  18. namespace {
  19. TEST(DWARFFormValue, FixedFormSizes) {
  20. // Size of DW_FORM_addr and DW_FORM_ref_addr are equal in DWARF2,
  21. // DW_FORM_ref_addr is always 4 bytes in DWARF32 starting from DWARF3.
  22. ArrayRef<uint8_t> sizes = DWARFFormValue::getFixedFormSizes(4, 2);
  23. EXPECT_EQ(sizes[DW_FORM_addr], sizes[DW_FORM_ref_addr]);
  24. sizes = DWARFFormValue::getFixedFormSizes(8, 2);
  25. EXPECT_EQ(sizes[DW_FORM_addr], sizes[DW_FORM_ref_addr]);
  26. sizes = DWARFFormValue::getFixedFormSizes(8, 3);
  27. EXPECT_EQ(4, sizes[DW_FORM_ref_addr]);
  28. // Check that we don't have fixed form sizes for weird address sizes.
  29. EXPECT_EQ(0U, DWARFFormValue::getFixedFormSizes(16, 2).size());
  30. }
  31. bool isFormClass(uint16_t Form, DWARFFormValue::FormClass FC) {
  32. return DWARFFormValue(Form).isFormClass(FC);
  33. }
  34. TEST(DWARFFormValue, FormClass) {
  35. EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address));
  36. EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address));
  37. EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant));
  38. EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset));
  39. EXPECT_TRUE(
  40. isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset));
  41. EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String));
  42. EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address));
  43. EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address));
  44. EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference));
  45. EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
  46. }
  47. template<typename RawTypeT>
  48. DWARFFormValue createDataXFormValue(uint16_t Form, RawTypeT Value) {
  49. char Raw[sizeof(RawTypeT)];
  50. memcpy(Raw, &Value, sizeof(RawTypeT));
  51. uint32_t Offset = 0;
  52. DWARFFormValue Result(Form);
  53. DataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
  54. sys::IsLittleEndianHost, sizeof(void*));
  55. Result.extractValue(Data, &Offset, nullptr);
  56. return Result;
  57. }
  58. DWARFFormValue createULEBFormValue(uint64_t Value) {
  59. SmallString<10> RawData;
  60. raw_svector_ostream OS(RawData);
  61. encodeULEB128(Value, OS);
  62. uint32_t Offset = 0;
  63. DWARFFormValue Result(DW_FORM_udata);
  64. DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
  65. Result.extractValue(Data, &Offset, nullptr);
  66. return Result;
  67. }
  68. DWARFFormValue createSLEBFormValue(int64_t Value) {
  69. SmallString<10> RawData;
  70. raw_svector_ostream OS(RawData);
  71. encodeSLEB128(Value, OS);
  72. uint32_t Offset = 0;
  73. DWARFFormValue Result(DW_FORM_sdata);
  74. DataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void*));
  75. Result.extractValue(Data, &Offset, nullptr);
  76. return Result;
  77. }
  78. TEST(DWARFFormValue, SignedConstantForms) {
  79. // Check that we correctly sign extend fixed size forms.
  80. auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
  81. auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
  82. auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
  83. auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
  84. EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
  85. EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
  86. EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
  87. EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
  88. // Check that we can handle big positive values, but that we return
  89. // an error just over the limit.
  90. auto UMax = createULEBFormValue(LLONG_MAX);
  91. auto TooBig = createULEBFormValue(uint64_t(LLONG_MAX) + 1);
  92. EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
  93. EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
  94. // Sanity check some other forms.
  95. auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
  96. auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
  97. auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
  98. auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
  99. auto LEBMin = createSLEBFormValue(LLONG_MIN);
  100. auto LEBMax = createSLEBFormValue(LLONG_MAX);
  101. auto LEB1 = createSLEBFormValue(-42);
  102. auto LEB2 = createSLEBFormValue(42);
  103. EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
  104. EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
  105. EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
  106. EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
  107. EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
  108. EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
  109. EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
  110. EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
  111. }
  112. } // end anonymous namespace