Skip to content

[BitstreamReader] Fix 32-bit overflow #117363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/lib/Bitstream/Reader/BitstreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ Expected<unsigned> BitstreamCursor::readRecord(unsigned AbbrevID,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this raises a question of whether we want to support writing 4GB+ blobs in bitcode files. We could use ReadVBR64(6) above and EmitVBR64(6) on the writer side, and I think that would be backwards compatible, it just widens the maximum encodable value.

Copy link
Contributor

@mingmingl-llvm mingmingl-llvm Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to {Read,Emit}VBR64(6) looks backward compatible to me as well, and https://llvm.org/docs/BitCodeFormat.html#define-abbrev-encoding needs an update around Blob (code 5): This field is emitted as a vbr6, followed by padding to a 32-bit boundary (for alignment) and an array of 8-bit objects It's not clear to me (from the doc and code comment around) how 32-bit alignment is related with value width (32 vs 64) and whether alignment needs an update for 4GB+ blobs.

If we decide not to support 4GB blob soon, I think emitting a release-build visible warning (e.g., by LLVMContext's diagnostic handler) when blob size is larger than 4GB around

EmitVBR(static_cast<uint32_t>(Bytes.size()), 6);
is generally a good change.

// Figure out where the end of this blob will be including tail padding.
size_t CurBitPos = GetCurrentBitNo();
const size_t NewEnd = CurBitPos + alignTo(NumElts, 4) * 8;
const size_t NewEnd =
CurBitPos + static_cast<uint64_t>(alignTo(NumElts, 4)) * 8;

// Make sure the bitstream is large enough to contain the blob.
if (!canSkipToPos(NewEnd/8))
Expand Down
Loading