Skip to content

Commit 218a6eb

Browse files
Merge pull request #8491 from rastogishubham/FixBinaryStream6
Cherry-pick #8482 to swift/release/6.0
2 parents e675022 + 82fea75 commit 218a6eb

File tree

5 files changed

+105
-77
lines changed

5 files changed

+105
-77
lines changed

llvm/include/llvm/MCCAS/MCCASDebugV1.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,26 @@ struct DataWriter {
8989

9090
/// Use a more efficient format for storing 4-byte wide form data.
9191
uint64_t convertFourByteFormDataToULEB(ArrayRef<char> FormData,
92-
DataWriter &Writer);
92+
DataWriter &Writer, bool IsLittleEndian,
93+
uint8_t AddressSize);
9394

9495
// Helper class to write a DIE's abbreviation contents to a buffer.
9596
struct AbbrevEntryWriter : DataWriter {
9697
void writeAbbrevEntry(DWARFDie DIE);
9798
};
9899

99100
struct AbbrevEntryReader {
100-
AbbrevEntryReader(StringRef Data)
101-
: DataStream(Data, support::endianness::little) {}
101+
AbbrevEntryReader(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
102+
: Extractor(Data, IsLittleEndian, AddressSize), Cursor(0) {}
102103
Expected<dwarf::Tag> readTag();
103104
Expected<bool> readHasChildren();
104105

105106
Expected<dwarf::Attribute> readAttr();
106107
Expected<dwarf::Form> readForm();
107108

108109
private:
109-
BinaryStreamReader DataStream;
110+
DataExtractor Extractor;
111+
DataExtractor::Cursor Cursor;
110112
};
111113

112114
/// Given a sequence of AbbrevEntries, as written by an AbbrevEntryWriter,
@@ -115,7 +117,8 @@ struct AbbrevEntryReader {
115117
/// Returns the number of bytes written to OS.
116118
uint64_t reconstructAbbrevSection(raw_ostream &OS,
117119
ArrayRef<StringRef> AbbrevEntries,
118-
uint64_t &MaxDIEAbbrevCount);
120+
uint64_t &MaxDIEAbbrevCount,
121+
bool IsLittleEndian, uint8_t AddressSize);
119122
} // namespace v1
120123
} // namespace mccasformats
121124
} // namespace llvm

llvm/include/llvm/MCCAS/MCCASObjectV1.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ class MCCASReader {
630630
return Target.isLittleEndian() ? support::little : support::big;
631631
}
632632

633+
bool isLittleEndian() { return Target.isLittleEndian(); }
634+
633635
Expected<MCObjectProxy> getObjectProxy(cas::ObjectRef ID) {
634636
auto Node = MCObjectProxy::get(Schema, Schema.CAS.getProxy(ID));
635637
if (!Node)
@@ -742,7 +744,8 @@ Error visitDebugInfo(
742744
std::function<void(dwarf::Tag, uint64_t)> StartTagCallback,
743745
std::function<void(dwarf::Attribute, dwarf::Form, StringRef, bool)>
744746
AttrCallback,
745-
std::function<void(bool)> EndTagCallback, uint8_t AddressSize,
747+
std::function<void(bool)> EndTagCallback, bool IsLittleEndian,
748+
uint8_t AddressSize,
746749
std::function<void(StringRef)> NewBlockCallback = [](StringRef) {});
747750

748751
} // namespace v1

llvm/lib/MCCAS/MCCASDebugV1.cpp

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,17 @@ bool mccasformats::v1::doesntDedup(dwarf::Form Form, dwarf::Attribute Attr) {
233233
return llvm::is_contained(it->second, Attr);
234234
}
235235

236-
uint64_t
237-
mccasformats::v1::convertFourByteFormDataToULEB(ArrayRef<char> FormData,
238-
DataWriter &Writer) {
236+
uint64_t mccasformats::v1::convertFourByteFormDataToULEB(
237+
ArrayRef<char> FormData, DataWriter &Writer, bool IsLittleEndian,
238+
uint8_t AddressSize) {
239239
assert(FormData.size() == 4);
240-
auto Reader =
241-
BinaryStreamReader(toStringRef(FormData), support::endianness::little);
240+
StringRef FormDataStringRef = StringRef(FormData.begin(), FormData.size());
241+
DataExtractor Extractor(FormDataStringRef, IsLittleEndian, AddressSize);
242+
DataExtractor::Cursor Cursor(0);
242243

243-
uint32_t IntegerData;
244-
if (auto Err = Reader.readInteger(IntegerData))
245-
handleAllErrors(std::move(Err)); // this should never fail
244+
uint32_t IntegerData = Extractor.getU32(Cursor);
245+
if (!Cursor)
246+
handleAllErrors(Cursor.takeError()); // this should never fail
246247
Writer.writeULEB128(IntegerData);
247248
return getULEB128Size(IntegerData);
248249
}
@@ -271,39 +272,41 @@ void AbbrevEntryWriter::writeAbbrevEntry(DWARFDie DIE) {
271272
}
272273

273274
Expected<dwarf::Tag> AbbrevEntryReader::readTag() {
274-
uint64_t TagAsInt;
275-
if (auto E = DataStream.readULEB128(TagAsInt))
276-
return std::move(E);
275+
uint64_t TagAsInt = Extractor.getULEB128(Cursor);
276+
if (!Cursor)
277+
return Cursor.takeError();
277278
return static_cast<dwarf::Tag>(TagAsInt);
278279
}
279280

280281
Expected<bool> AbbrevEntryReader::readHasChildren() {
281-
char HasChildren;
282-
if (auto E = DataStream.readInteger(HasChildren))
283-
return std::move(E);
282+
char HasChildren = Extractor.getU8(Cursor);
283+
if (!Cursor)
284+
return Cursor.takeError();
284285
return HasChildren;
285286
}
286287

287288
Expected<dwarf::Attribute> AbbrevEntryReader::readAttr() {
288-
if (DataStream.bytesRemaining() == 0)
289+
if (Extractor.eof(Cursor))
289290
return static_cast<dwarf::Attribute>(getEndOfAttributesMarker());
290-
uint64_t AttrAsInt;
291-
if (auto E = DataStream.readULEB128(AttrAsInt))
292-
return std::move(E);
291+
uint64_t AttrAsInt = Extractor.getULEB128(Cursor);
292+
if (!Cursor)
293+
return Cursor.takeError();
293294
return static_cast<dwarf::Attribute>(AttrAsInt);
294295
}
295296

296-
static Expected<int64_t> handleImplicitConst(BinaryStreamReader &Reader) {
297-
int64_t ImplicitVal;
298-
if (auto E = Reader.readSLEB128(ImplicitVal))
299-
return std::move(E);
297+
static Expected<int64_t> handleImplicitConst(DataExtractor &Extractor,
298+
DataExtractor::Cursor &Cursor) {
299+
int64_t ImplicitVal = Extractor.getSLEB128(Cursor);
300+
301+
if (!Cursor)
302+
return Cursor.takeError();
300303
return ImplicitVal;
301304
}
302305

303306
Expected<dwarf::Form> AbbrevEntryReader::readForm() {
304-
uint64_t FormAsInt;
305-
if (auto E = DataStream.readULEB128(FormAsInt))
306-
return std::move(E);
307+
uint64_t FormAsInt = Extractor.getULEB128(Cursor);
308+
if (!Cursor)
309+
return Cursor.takeError();
307310
auto Form = static_cast<dwarf::Form>(FormAsInt);
308311

309312
// Dwarf 5: Section 7.4:
@@ -314,45 +317,45 @@ Expected<dwarf::Form> AbbrevEntryReader::readForm() {
314317

315318
// Advance reader to beyond the implicit_const value, to read Forms correctly.
316319
if (Form == dwarf::Form::DW_FORM_implicit_const) {
317-
auto ImplicitVal = handleImplicitConst(DataStream);
320+
auto ImplicitVal = handleImplicitConst(Extractor, Cursor);
318321
if (!ImplicitVal)
319322
return ImplicitVal.takeError();
320323
}
321324
return Form;
322325
}
323326

324-
uint64_t
325-
mccasformats::v1::reconstructAbbrevSection(raw_ostream &OS,
326-
ArrayRef<StringRef> AbbrevEntries,
327-
uint64_t &MaxDIEAbbrevCount) {
327+
uint64_t mccasformats::v1::reconstructAbbrevSection(
328+
raw_ostream &OS, ArrayRef<StringRef> AbbrevEntries,
329+
uint64_t &MaxDIEAbbrevCount, bool IsLittleEndian, uint8_t AddressSize) {
328330
uint64_t WrittenSize = 0;
329331
for (auto EntryData : AbbrevEntries) {
330332
// Dwarf 5: Section 7.5.3:
331333
// Each declaration begins with an unsigned LEB128 number representing the
332334
// abbreviation code itself. [...] The abbreviation code 0 is reserved for
333335
// null debugging information entries.
334336
WrittenSize += encodeULEB128(MaxDIEAbbrevCount, OS);
335-
BinaryStreamReader Reader(EntryData, support::endianness::little);
337+
DataExtractor Extractor(EntryData, IsLittleEndian, AddressSize);
338+
DataExtractor::Cursor Cursor(0);
336339
// [uleb(Tag), has_children]
337-
uint64_t TagAsInt;
338-
uint8_t HasChildren;
339-
if (auto Err = Reader.readULEB128(TagAsInt))
340-
handleAllErrors(std::move(Err));
341-
if (auto Err = Reader.readInteger(HasChildren))
342-
handleAllErrors(std::move(Err));
340+
uint64_t TagAsInt = Extractor.getULEB128(Cursor);
341+
if (!Cursor)
342+
handleAllErrors(Cursor.takeError());
343+
uint8_t HasChildren = Extractor.getU8(Cursor);
344+
if (!Cursor)
345+
handleAllErrors(Cursor.takeError());
343346
WrittenSize += encodeULEB128(TagAsInt, OS);
344347
OS << HasChildren;
345348
WrittenSize += 1;
346349
assert(HasChildren == 0 || HasChildren == 1);
347350

348351
// [uleb(Attr), uleb(Form)]*
349-
while (!Reader.empty()) {
350-
uint64_t AttrAsInt;
351-
uint64_t FormAsInt;
352-
if (auto Err = Reader.readULEB128(AttrAsInt))
353-
handleAllErrors(std::move(Err));
354-
if (auto Err = Reader.readULEB128(FormAsInt))
355-
handleAllErrors(std::move(Err));
352+
while (!Extractor.eof(Cursor)) {
353+
uint64_t AttrAsInt = Extractor.getULEB128(Cursor);
354+
if (!Cursor)
355+
handleAllErrors(Cursor.takeError());
356+
uint64_t FormAsInt = Extractor.getULEB128(Cursor);
357+
if (!Cursor)
358+
handleAllErrors(Cursor.takeError());
356359

357360
WrittenSize += encodeULEB128(AttrAsInt, OS);
358361

@@ -370,7 +373,7 @@ mccasformats::v1::reconstructAbbrevSection(raw_ostream &OS,
370373
// This number is used as the value of the attribute with the
371374
// aformentioned form and nothing is stored in the .debug_info section.
372375
if (Form == dwarf::Form::DW_FORM_implicit_const) {
373-
auto ImplicitVal = handleImplicitConst(Reader);
376+
auto ImplicitVal = handleImplicitConst(Extractor, Cursor);
374377
if (!ImplicitVal)
375378
handleAllErrors(ImplicitVal.takeError());
376379
WrittenSize += encodeSLEB128(*ImplicitVal, OS);

0 commit comments

Comments
 (0)