| 1 |
//===- llvm/unittest/DebugInfo/DWARFFormValueTest.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/DWARFFormValue.h" |
| 10 |
#include "llvm/ADT/ArrayRef.h" |
| 11 |
#include "llvm/ADT/SmallString.h" |
| 12 |
#include "llvm/BinaryFormat/Dwarf.h" |
| 13 |
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" |
| 14 |
#include "llvm/Support/FormatVariadic.h" |
| 15 |
#include "llvm/Support/Host.h" |
| 16 |
#include "llvm/Support/LEB128.h" |
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include <climits> |
| 19 |
using namespace llvm; |
| 20 |
using namespace dwarf; |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
bool isFormClass(dwarf::Form Form, DWARFFormValue::FormClass FC) { |
| 25 |
return DWARFFormValue(Form).isFormClass(FC); |
| 26 |
} |
| 27 |
|
| 28 |
TEST(DWARFFormValue, FormClass) { |
| 29 |
EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address)); |
| 30 |
EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address)); |
| 31 |
EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant)); |
| 32 |
EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset)); |
| 33 |
EXPECT_TRUE( |
| 34 |
isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset)); |
| 35 |
EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String)); |
| 36 |
EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address)); |
| 37 |
EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address)); |
| 38 |
EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference)); |
| 39 |
EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference)); |
| 40 |
} |
| 41 |
|
| 42 |
template<typename RawTypeT> |
| 43 |
DWARFFormValue createDataXFormValue(dwarf::Form Form, RawTypeT Value) { |
| 44 |
char Raw[sizeof(RawTypeT)]; |
| 45 |
memcpy(Raw, &Value, sizeof(RawTypeT)); |
| 46 |
uint64_t Offset = 0; |
| 47 |
DWARFFormValue Result(Form); |
| 48 |
DWARFDataExtractor Data(StringRef(Raw, sizeof(RawTypeT)), |
| 49 |
sys::IsLittleEndianHost, sizeof(void *)); |
| 50 |
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32}); |
| 51 |
return Result; |
| 52 |
} |
| 53 |
|
| 54 |
DWARFFormValue createULEBFormValue(uint64_t Value) { |
| 55 |
SmallString<10> RawData; |
| 56 |
raw_svector_ostream OS(RawData); |
| 57 |
encodeULEB128(Value, OS); |
| 58 |
uint64_t Offset = 0; |
| 59 |
DWARFFormValue Result(DW_FORM_udata); |
| 60 |
DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *)); |
| 61 |
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32}); |
| 62 |
return Result; |
| 63 |
} |
| 64 |
|
| 65 |
DWARFFormValue createSLEBFormValue(int64_t Value) { |
| 66 |
SmallString<10> RawData; |
| 67 |
raw_svector_ostream OS(RawData); |
| 68 |
encodeSLEB128(Value, OS); |
| 69 |
uint64_t Offset = 0; |
| 70 |
DWARFFormValue Result(DW_FORM_sdata); |
| 71 |
DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *)); |
| 72 |
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32}); |
| 73 |
return Result; |
| 74 |
} |
| 75 |
|
| 76 |
TEST(DWARFFormValue, SignedConstantForms) { |
| 77 |
// Check that we correctly sign extend fixed size forms. |
| 78 |
auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123); |
| 79 |
auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345); |
| 80 |
auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789); |
| 81 |
auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1); |
| 82 |
EXPECT_EQ(*Sign1.getAsSignedConstant(), -123); |
| 83 |
EXPECT_EQ(*Sign2.getAsSignedConstant(), -12345); |
| 84 |
EXPECT_EQ(*Sign4.getAsSignedConstant(), -123456789); |
| 85 |
EXPECT_EQ(*Sign8.getAsSignedConstant(), -1); |
| 86 |
|
| 87 |
// Check that we can handle big positive values, but that we return |
| 88 |
// an error just over the limit. |
| 89 |
auto UMax = createULEBFormValue(LLONG_MAX); |
| 90 |
auto TooBig = createULEBFormValue(uint64_t(LLONG_MAX) + 1); |
| 91 |
EXPECT_EQ(*UMax.getAsSignedConstant(), LLONG_MAX); |
| 92 |
EXPECT_EQ(TooBig.getAsSignedConstant().has_value(), false); |
| 93 |
|
| 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(), 120); |
| 104 |
EXPECT_EQ(*Data2.getAsSignedConstant(), 32000); |
| 105 |
EXPECT_EQ(*Data4.getAsSignedConstant(), 2000000000); |
| 106 |
EXPECT_EQ(*Data8.getAsSignedConstant(), 0x1234567812345678LL); |
| 107 |
EXPECT_EQ(*LEBMin.getAsSignedConstant(), LLONG_MIN); |
| 108 |
EXPECT_EQ(*LEBMax.getAsSignedConstant(), LLONG_MAX); |
| 109 |
EXPECT_EQ(*LEB1.getAsSignedConstant(), -42); |
| 110 |
EXPECT_EQ(*LEB2.getAsSignedConstant(), 42); |
| 111 |
|
| 112 |
// Data16 is a little tricky. |
| 113 |
char Cksum[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
| 114 |
DWARFFormValue Data16(DW_FORM_data16); |
| 115 |
DWARFDataExtractor DE16(StringRef(Cksum, 16), sys::IsLittleEndianHost, |
| 116 |
sizeof(void *)); |
| 117 |
uint64_t Offset = 0; |
| 118 |
Data16.extractValue(DE16, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32}); |
| 119 |
SmallString<32> Str; |
| 120 |
raw_svector_ostream Res(Str); |
| 121 |
Data16.dump(Res, DIDumpOptions()); |
| 122 |
EXPECT_EQ(memcmp(Str.data(), "000102030405060708090a0b0c0d0e0f", 32), 0); |
| 123 |
} |
| 124 |
|
| 125 |
using ParamType = std::tuple<Form, uint16_t, uint8_t, DwarfFormat, |
| 126 |
ArrayRef<uint8_t>, uint64_t, bool>; |
| 127 |
|
| 128 |
struct FormSkipValueFixtureBase : public testing::TestWithParam<ParamType> { |
| 129 |
void SetUp() override { |
| 130 |
std::tie(Fm, Version, AddrSize, Dwarf, InitialData, ExpectedSkipped, |
| 131 |
ExpectedResult) = GetParam(); |
| 132 |
} |
| 133 |
|
| 134 |
void doSkipValueTest() { |
| 135 |
SCOPED_TRACE("Inputs: Form = " + std::to_string(Fm) + |
| 136 |
", Version = " + std::to_string(Version) + |
| 137 |
", AddrSize = " + std::to_string(uint32_t(AddrSize)) + |
| 138 |
", DwarfFormat = " + std::to_string(Dwarf)); |
| 139 |
std::vector<uint8_t> Buf(InitialData.data(), |
| 140 |
InitialData.data() + InitialData.size()); |
| 141 |
// The data extractor only adjusts the offset to the end of the buffer when |
| 142 |
// attempting to read past the end, so the buffer must be bigger than the |
| 143 |
// expected amount to be skipped to identify cases where more data than |
| 144 |
// expected is skipped. |
| 145 |
Buf.resize(ExpectedSkipped + 1); |
| 146 |
DWARFDataExtractor Data(Buf, sys::IsLittleEndianHost, AddrSize); |
| 147 |
uint64_t Offset = 0; |
| 148 |
EXPECT_EQ(DWARFFormValue::skipValue(Fm, Data, &Offset, |
| 149 |
{Version, AddrSize, Dwarf}), |
| 150 |
ExpectedResult); |
| 151 |
EXPECT_EQ(Offset, ExpectedSkipped); |
| 152 |
} |
| 153 |
|
| 154 |
Form Fm; |
| 155 |
uint16_t Version; |
| 156 |
uint8_t AddrSize; |
| 157 |
DwarfFormat Dwarf; |
| 158 |
ArrayRef<uint8_t> InitialData; |
| 159 |
uint64_t ExpectedSkipped; |
| 160 |
bool ExpectedResult; |
| 161 |
}; |
| 162 |
|
| 163 |
template <typename T> static ArrayRef<uint8_t> toBytes(const T &Input) { |
| 164 |
return ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&Input), |
| 165 |
sizeof(Input)); |
| 166 |
} |
| 167 |
|
| 168 |
const uint8_t LEBData[] = {0x80, 0x1}; |
| 169 |
ArrayRef<uint8_t> SampleLEB(LEBData, sizeof(LEBData)); |
| 170 |
const uint8_t SampleLength8 = 0x80; |
| 171 |
const uint16_t SampleLength16 = 0x80; |
| 172 |
const uint32_t SampleLength = 0x80; |
| 173 |
ArrayRef<uint8_t> SampleU8 = toBytes(SampleLength8); |
| 174 |
ArrayRef<uint8_t> SampleU16 = toBytes(SampleLength16); |
| 175 |
ArrayRef<uint8_t> SampleU32 = toBytes(SampleLength); |
| 176 |
const uint8_t StringData[] = "abcdef"; |
| 177 |
ArrayRef<uint8_t> SampleString(StringData, sizeof(StringData)); |
| 178 |
const uint8_t IndirectData8[] = {DW_FORM_data8}; |
| 179 |
const uint8_t IndirectData16[] = {DW_FORM_data16}; |
| 180 |
const uint8_t IndirectAddr[] = {DW_FORM_addr}; |
| 181 |
const uint8_t IndirectIndirectData1[] = {DW_FORM_indirect, DW_FORM_data1}; |
| 182 |
const uint8_t IndirectIndirectEnd[] = {DW_FORM_indirect}; |
| 183 |
|
| 184 |
// Gtest's paramterised tests only allow a maximum of 50 cases, so split the |
| 185 |
// test into multiple identical parts to share the cases. |
| 186 |
struct FormSkipValueFixture1 : FormSkipValueFixtureBase {}; |
| 187 |
struct FormSkipValueFixture2 : FormSkipValueFixtureBase {}; |
| 188 |
TEST_P(FormSkipValueFixture1, skipValuePart1) { doSkipValueTest(); } |
| 189 |
TEST_P(FormSkipValueFixture2, skipValuePart2) { doSkipValueTest(); } |
| 190 |
|
| 191 |
INSTANTIATE_TEST_SUITE_P( |
| 192 |
SkipValueTestParams1, FormSkipValueFixture1, |
| 193 |
testing::Values( |
| 194 |
// Form, Version, AddrSize, DwarfFormat, InitialData, ExpectedSize, |
| 195 |
// ExpectedResult. |
| 196 |
ParamType(DW_FORM_exprloc, 0, 0, DWARF32, SampleLEB, |
| 197 |
SampleLength + SampleLEB.size(), true), |
| 198 |
ParamType(DW_FORM_block, 0, 0, DWARF32, SampleLEB, |
| 199 |
SampleLength + SampleLEB.size(), true), |
| 200 |
ParamType(DW_FORM_block1, 0, 0, DWARF32, SampleU8, SampleLength8 + 1, |
| 201 |
true), |
| 202 |
ParamType(DW_FORM_block2, 0, 0, DWARF32, SampleU16, SampleLength16 + 2, |
| 203 |
true), |
| 204 |
ParamType(DW_FORM_block4, 0, 0, DWARF32, SampleU32, SampleLength + 4, |
| 205 |
true), |
| 206 |
ParamType(DW_FORM_string, 0, 0, DWARF32, SampleString, |
| 207 |
SampleString.size(), true), |
| 208 |
ParamType(DW_FORM_addr, 0, 42, DWARF32, SampleU32, 0, false), |
| 209 |
ParamType(DW_FORM_addr, 4, 0, DWARF32, SampleU32, 0, false), |
| 210 |
ParamType(DW_FORM_addr, 4, 42, DWARF32, SampleU32, 42, true), |
| 211 |
ParamType(DW_FORM_ref_addr, 0, 1, DWARF32, SampleU32, 0, false), |
| 212 |
ParamType(DW_FORM_ref_addr, 1, 0, DWARF32, SampleU32, 0, false), |
| 213 |
ParamType(DW_FORM_ref_addr, 1, 1, DWARF32, SampleU32, 4, true), |
| 214 |
ParamType(DW_FORM_ref_addr, 1, 1, DWARF64, SampleU32, 8, true), |
| 215 |
ParamType(DW_FORM_ref_addr, 2, 42, DWARF32, SampleU32, 42, true), |
| 216 |
ParamType(DW_FORM_ref_addr, 2, 42, DWARF64, SampleU32, 42, true), |
| 217 |
ParamType(DW_FORM_ref_addr, 3, 3, DWARF32, SampleU32, 4, true), |
| 218 |
ParamType(DW_FORM_ref_addr, 3, 3, DWARF64, SampleU32, 8, true), |
| 219 |
ParamType(DW_FORM_flag_present, 4, 4, DWARF32, SampleU32, 0, true), |
| 220 |
ParamType(DW_FORM_data1, 0, 0, DWARF32, SampleU32, 1, true), |
| 221 |
ParamType(DW_FORM_data2, 0, 0, DWARF32, SampleU32, 2, true), |
| 222 |
ParamType(DW_FORM_data4, 0, 0, DWARF32, SampleU32, 4, true), |
| 223 |
ParamType(DW_FORM_data8, 0, 0, DWARF32, SampleU32, 8, true), |
| 224 |
ParamType(DW_FORM_data16, 0, 0, DWARF32, SampleU32, 16, true), |
| 225 |
ParamType(DW_FORM_flag, 0, 0, DWARF32, SampleU32, 1, true), |
| 226 |
ParamType(DW_FORM_ref1, 0, 0, DWARF32, SampleU32, 1, true), |
| 227 |
ParamType(DW_FORM_ref2, 0, 0, DWARF32, SampleU32, 2, true), |
| 228 |
ParamType(DW_FORM_ref4, 0, 0, DWARF32, SampleU32, 4, true), |
| 229 |
ParamType(DW_FORM_ref8, 0, 0, DWARF32, SampleU32, 8, true), |
| 230 |
ParamType(DW_FORM_ref_sig8, 0, 0, DWARF32, SampleU32, 8, true), |
| 231 |
ParamType(DW_FORM_ref_sup4, 0, 0, DWARF32, SampleU32, 4, true), |
| 232 |
ParamType(DW_FORM_ref_sup8, 0, 0, DWARF32, SampleU32, 8, true), |
| 233 |
ParamType(DW_FORM_strx1, 0, 0, DWARF32, SampleU32, 1, true), |
| 234 |
ParamType(DW_FORM_strx2, 0, 0, DWARF32, SampleU32, 2, true), |
| 235 |
ParamType(DW_FORM_strx4, 0, 0, DWARF32, SampleU32, 4, true), |
| 236 |
ParamType(DW_FORM_addrx1, 0, 0, DWARF32, SampleU32, 1, true), |
| 237 |
ParamType(DW_FORM_addrx2, 0, 0, DWARF32, SampleU32, 2, true), |
| 238 |
ParamType(DW_FORM_addrx4, 0, 0, DWARF32, SampleU32, 4, true), |
| 239 |
ParamType(DW_FORM_sec_offset, 0, 1, DWARF32, SampleU32, 0, false), |
| 240 |
ParamType(DW_FORM_sec_offset, 1, 0, DWARF32, SampleU32, 0, false), |
| 241 |
ParamType(DW_FORM_sec_offset, 1, 1, DWARF32, SampleU32, 4, true), |
| 242 |
ParamType(DW_FORM_sec_offset, 1, 1, DWARF64, SampleU32, 8, true), |
| 243 |
ParamType(DW_FORM_strp, 0, 1, DWARF32, SampleU32, 0, false), |
| 244 |
ParamType(DW_FORM_strp, 1, 0, DWARF32, SampleU32, 0, false), |
| 245 |
ParamType(DW_FORM_strp, 1, 1, DWARF32, SampleU32, 4, true), |
| 246 |
ParamType(DW_FORM_strp, 1, 1, DWARF64, SampleU32, 8, true), |
| 247 |
ParamType(DW_FORM_strp_sup, 0, 1, DWARF32, SampleU32, 0, false), |
| 248 |
ParamType(DW_FORM_strp_sup, 1, 0, DWARF32, SampleU32, 0, false), |
| 249 |
ParamType(DW_FORM_strp_sup, 1, 1, DWARF32, SampleU32, 4, true), |
| 250 |
ParamType(DW_FORM_strp_sup, 1, 1, DWARF64, SampleU32, 8, true))); |
| 251 |
|
| 252 |
INSTANTIATE_TEST_SUITE_P( |
| 253 |
SkipValueTestParams2, FormSkipValueFixture2, |
| 254 |
testing::Values( |
| 255 |
ParamType(DW_FORM_line_strp, 0, 1, DWARF32, SampleU32, 0, false), |
| 256 |
ParamType(DW_FORM_line_strp, 1, 0, DWARF32, SampleU32, 0, false), |
| 257 |
ParamType(DW_FORM_line_strp, 1, 1, DWARF32, SampleU32, 4, true), |
| 258 |
ParamType(DW_FORM_line_strp, 1, 1, DWARF64, SampleU32, 8, true), |
| 259 |
ParamType(DW_FORM_GNU_ref_alt, 0, 1, DWARF32, SampleU32, 0, false), |
| 260 |
ParamType(DW_FORM_GNU_ref_alt, 1, 0, DWARF32, SampleU32, 0, false), |
| 261 |
ParamType(DW_FORM_GNU_ref_alt, 1, 1, DWARF32, SampleU32, 4, true), |
| 262 |
ParamType(DW_FORM_GNU_ref_alt, 1, 1, DWARF64, SampleU32, 8, true), |
| 263 |
ParamType(DW_FORM_GNU_strp_alt, 0, 1, DWARF32, SampleU32, 0, false), |
| 264 |
ParamType(DW_FORM_GNU_strp_alt, 1, 0, DWARF32, SampleU32, 0, false), |
| 265 |
ParamType(DW_FORM_GNU_strp_alt, 1, 1, DWARF32, SampleU32, 4, true), |
| 266 |
ParamType(DW_FORM_GNU_strp_alt, 1, 1, DWARF64, SampleU32, 8, true), |
| 267 |
ParamType(DW_FORM_sdata, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 268 |
true), |
| 269 |
ParamType(DW_FORM_udata, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 270 |
true), |
| 271 |
ParamType(DW_FORM_ref_udata, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 272 |
true), |
| 273 |
ParamType(DW_FORM_strx, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 274 |
true), |
| 275 |
ParamType(DW_FORM_addrx, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 276 |
true), |
| 277 |
ParamType(DW_FORM_loclistx, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 278 |
true), |
| 279 |
ParamType(DW_FORM_rnglistx, 0, 0, DWARF32, SampleLEB, SampleLEB.size(), |
| 280 |
true), |
| 281 |
ParamType(DW_FORM_GNU_addr_index, 0, 0, DWARF32, SampleLEB, |
| 282 |
SampleLEB.size(), true), |
| 283 |
ParamType(DW_FORM_GNU_str_index, 0, 0, DWARF32, SampleLEB, |
| 284 |
SampleLEB.size(), true), |
| 285 |
ParamType(DW_FORM_indirect, 0, 0, DWARF32, |
| 286 |
ArrayRef<uint8_t>(IndirectData8, sizeof(IndirectData8)), 9, |
| 287 |
true), |
| 288 |
ParamType(DW_FORM_indirect, 0, 0, DWARF32, |
| 289 |
ArrayRef<uint8_t>(IndirectData16, sizeof(IndirectData16)), 17, |
| 290 |
true), |
| 291 |
ParamType(DW_FORM_indirect, 4, 0, DWARF32, |
| 292 |
ArrayRef<uint8_t>(IndirectAddr, sizeof(IndirectAddr)), 1, |
| 293 |
false), |
| 294 |
ParamType(DW_FORM_indirect, 4, 4, DWARF32, |
| 295 |
ArrayRef<uint8_t>(IndirectAddr, sizeof(IndirectAddr)), 5, |
| 296 |
true), |
| 297 |
ParamType(DW_FORM_indirect, 4, 4, DWARF32, |
| 298 |
ArrayRef<uint8_t>(IndirectIndirectData1, |
| 299 |
sizeof(IndirectIndirectData1)), |
| 300 |
3, true), |
| 301 |
ParamType(DW_FORM_indirect, 4, 4, DWARF32, |
| 302 |
ArrayRef<uint8_t>(IndirectIndirectEnd, |
| 303 |
sizeof(IndirectIndirectEnd)), |
| 304 |
2, false), |
| 305 |
ParamType(/*Unknown=*/Form(0xff), 4, 4, DWARF32, SampleU32, 0, false))); |
| 306 |
|
| 307 |
using ErrorParams = std::tuple<Form, std::vector<uint8_t>>; |
| 308 |
struct ExtractValueErrorFixture : public testing::TestWithParam<ErrorParams> { |
| 309 |
void SetUp() override { std::tie(Fm, InitialData) = GetParam(); } |
| 310 |
|
| 311 |
Form Fm; |
| 312 |
ArrayRef<uint8_t> InitialData; |
| 313 |
}; |
| 314 |
|
| 315 |
TEST_P(ExtractValueErrorFixture, Test) { |
| 316 |
SCOPED_TRACE(formatv("Fm = {0}, InitialData = {1}", Fm, |
| 317 |
make_range(InitialData.begin(), InitialData.end())) |
| 318 |
.str()); |
| 319 |
|
| 320 |
DWARFDataExtractor Data(InitialData, sys::IsLittleEndianHost, 4); |
| 321 |
DWARFFormValue Form(Fm); |
| 322 |
uint64_t Offset = 0; |
| 323 |
EXPECT_FALSE(Form.extractValue(Data, &Offset, {0, 0, DWARF32})); |
| 324 |
} |
| 325 |
|
| 326 |
INSTANTIATE_TEST_SUITE_P( |
| 327 |
ExtractValueErrorParams, ExtractValueErrorFixture, |
| 328 |
testing::Values( |
| 329 |
ErrorParams{DW_FORM_ref_addr, {}}, ErrorParams{DW_FORM_block, {}}, |
| 330 |
ErrorParams{DW_FORM_block, {1}}, ErrorParams{DW_FORM_block, {2, 0}}, |
| 331 |
ErrorParams{DW_FORM_block1, {}}, ErrorParams{DW_FORM_block2, {}}, |
| 332 |
ErrorParams{DW_FORM_block4, {}}, ErrorParams{DW_FORM_data1, {}}, |
| 333 |
ErrorParams{DW_FORM_data2, {}}, ErrorParams{DW_FORM_strx3, {}}, |
| 334 |
ErrorParams{DW_FORM_data4, {}}, ErrorParams{DW_FORM_data8, {}}, |
| 335 |
ErrorParams{DW_FORM_data16, {}}, ErrorParams{DW_FORM_sdata, {}}, |
| 336 |
ErrorParams{DW_FORM_udata, {}}, ErrorParams{DW_FORM_string, {}}, |
| 337 |
ErrorParams{DW_FORM_indirect, {}}, |
| 338 |
ErrorParams{DW_FORM_indirect, {DW_FORM_data1}}, |
| 339 |
ErrorParams{DW_FORM_strp_sup, {}}, ErrorParams{DW_FORM_ref_sig8, {}})); |
| 340 |
|
| 341 |
using DumpValueParams = |
| 342 |
std::tuple<Form, ArrayRef<uint8_t>, DwarfFormat, StringRef>; |
| 343 |
struct DumpValueFixture : public testing::TestWithParam<DumpValueParams> { |
| 344 |
void SetUp() override { |
| 345 |
std::tie(Fm, InitialData, Format, ExpectedResult) = GetParam(); |
| 346 |
} |
| 347 |
|
| 348 |
Form Fm; |
| 349 |
ArrayRef<uint8_t> InitialData; |
| 350 |
DwarfFormat Format; |
| 351 |
StringRef ExpectedResult; |
| 352 |
}; |
| 353 |
|
| 354 |
TEST_P(DumpValueFixture, Test) { |
| 355 |
SCOPED_TRACE(formatv("Fm = {0}, InitialData = [{1}], Format = {2}", Fm, |
| 356 |
toHex(InitialData), |
| 357 |
Format == DWARF64 ? "DWARF64" : "DWARF32")); |
| 358 |
DWARFDataExtractor Data(InitialData, sys::IsLittleEndianHost, 8); |
| 359 |
DWARFFormValue Form(Fm); |
| 360 |
uint64_t Offset = 0; |
| 361 |
Form.extractValue(Data, &Offset, {0, 0, Format}); |
| 362 |
|
| 363 |
std::string Output; |
| 364 |
raw_string_ostream OS(Output); |
| 365 |
|
| 366 |
DIDumpOptions Opts; |
| 367 |
Opts.Verbose = true; |
| 368 |
Opts.ShowAddresses = true; |
| 369 |
|
| 370 |
Form.dump(OS, Opts); |
| 371 |
OS.flush(); |
| 372 |
|
| 373 |
EXPECT_EQ(Output, ExpectedResult); |
| 374 |
} |
| 375 |
|
| 376 |
const uint32_t DumpTestSample32Val = 0x112233; |
| 377 |
ArrayRef<uint8_t> DumpTestSample32 = toBytes(DumpTestSample32Val); |
| 378 |
const uint64_t DumpTestSample64Val = 0x11223344556677; |
| 379 |
ArrayRef<uint8_t> DumpTestSample64 = toBytes(DumpTestSample64Val); |
| 380 |
|
| 381 |
INSTANTIATE_TEST_SUITE_P( |
| 382 |
DumpValueParams, DumpValueFixture, |
| 383 |
testing::Values(DumpValueParams{DW_FORM_strp, DumpTestSample32, DWARF32, |
| 384 |
" .debug_str[0x00112233] = "}, |
| 385 |
DumpValueParams{DW_FORM_strp, DumpTestSample64, DWARF64, |
| 386 |
" .debug_str[0x0011223344556677] = "}, |
| 387 |
DumpValueParams{DW_FORM_line_strp, DumpTestSample32, |
| 388 |
DWARF32, " .debug_line_str[0x00112233] = "}, |
| 389 |
DumpValueParams{DW_FORM_line_strp, DumpTestSample64, |
| 390 |
DWARF64, |
| 391 |
" .debug_line_str[0x0011223344556677] = "}, |
| 392 |
DumpValueParams{DW_FORM_sec_offset, DumpTestSample32, |
| 393 |
DWARF32, "0x00112233"}, |
| 394 |
DumpValueParams{DW_FORM_sec_offset, DumpTestSample64, |
| 395 |
DWARF64, "0x0011223344556677"})); |
| 396 |
|
| 397 |
} // end anonymous namespace |