This is an archive of the discontinued LLVM Phabricator instance.

Bitcode: Change reader interface to take memory buffers.
ClosedPublic

Authored by pcc on Nov 1 2016, 3:53 PM.

Details

Summary

As proposed on llvm-dev:
http://lists.llvm.org/pipermail/llvm-dev/2016-October/106595.html

This change also fixes an API oddity where BitstreamCursor::Read() would
return zero for the first read past the end of the bitstream, but would
report_fatal_error for subsequent reads. Now we always report_fatal_error
for all reads past the end. Updated clients to check for the end of the
bitstream before reading from it.

I also needed to add padding to the invalid bitcode tests in
test/Bitcode/. This is because the streaming interface was not checking that
the file size is a multiple of 4.

Diff Detail

Repository
rL LLVM

Event Timeline

pcc updated this revision to Diff 76645.Nov 1 2016, 3:53 PM
pcc retitled this revision from to Bitcode, Support: Remove MemoryObject and DataStreamer interfaces; bitcode reader now takes memory buffers..
pcc updated this object.
pcc added reviewers: rafael, mehdi_amini, dexonsmith.
pcc added subscribers: llvm-commits, jordan_rose.

Sounds great to me, but it would be kind of nice to do it in two commits: one to add the new API and one to remove the old one.

pcc retitled this revision from Bitcode, Support: Remove MemoryObject and DataStreamer interfaces; bitcode reader now takes memory buffers. to Bitcode: Change reader interface to take memory buffers..Nov 1 2016, 4:23 PM
mehdi_amini edited edge metadata.Nov 1 2016, 4:24 PM
llvm/include/llvm/Bitcode/BitstreamReader.h
63 ↗(On Diff #76645)

= default; ?

69 ↗(On Diff #76645)

You can forward to the next ctor:

BitstreamReader(MemoryBufferRef BitcodeBytes)
      : BitstreamReader(BitcodeBytes.getBuffer()) {}
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
235 ↗(On Diff #76645)

Is this related to this commit?

402 ↗(On Diff #76645)

Is this related to this change or can you remove this as a pre-commit?

mehdi_amini added inline comments.Nov 1 2016, 4:25 PM
llvm/include/llvm/Bitcode/BitstreamReader.h
63 ↗(On Diff #76651)

The = default() was supposed to be here (commented while you updated the diff)

pcc updated this revision to Diff 76652.Nov 1 2016, 4:44 PM
pcc marked 2 inline comments as done.
pcc edited edge metadata.
  • Use = default and delegating ctor
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
235 ↗(On Diff #76645)

Yes, this ctor is currently needed elsewhere (marked below).

402 ↗(On Diff #76645)

Same here.

6691 ↗(On Diff #76651)

This is where we currently use the ctor.

mehdi_amini accepted this revision.Nov 1 2016, 5:05 PM
mehdi_amini edited edge metadata.

Alright, LGTM!

This revision is now accepted and ready to land.Nov 1 2016, 5:05 PM
This revision was automatically updated to reflect the committed changes.

Alright, LGTM!

llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
435

There is a semantic change here.

Previously, when at the end of the stream after calling Stream.advance(BitstreamCursor::AF_DontPopBlockAtEnd); we would call ReadCode() which would return 0 which is also bitc::END_BLOCK, and we would enter the condition below and return BitstreamEntry::getEndBlock();.
Now we return an error instead.

This is breaking the loop pattern in BitcodeReader::parseIdentificationBlock, which is conceptually the following:

// We expect a number of well-defined blocks, though we don't necessarily
// need to understand them all.
while (1) {
  BitstreamEntry Entry =
      Stream.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
  // Ignore other sub-blocks.
  if (Stream.SkipBlock())
    return error("Malformed block");
}
pcc added inline comments.Nov 9 2016, 2:26 PM
llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
435

As discussed on IRC, let's add a BitstreamEntry::EOF enum and change the clients to handle it.