This is an archive of the discontinued LLVM Phabricator instance.

[DX] Improve parse error messages
ClosedPublic

Authored by beanz on Dec 8 2022, 4:26 PM.

Details

Summary

This change refactors the parte parsing logic to operate on StringRefs
of the part data rather than starting from an offset and splicing down.
It also improves some of the error reporting around part layout.

Specifically, this code now reports a distinct error if there isn't
enough data in the buffer to store the part size and it reports an
error if the parts overlap.

Diff Detail

Event Timeline

beanz created this revision.Dec 8 2022, 4:26 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 4:26 PM
beanz requested review of this revision.Dec 8 2022, 4:26 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 4:26 PM
bob80905 added inline comments.Dec 8 2022, 4:55 PM
llvm/lib/Object/DXContainer.cpp
108–109

This comment blocks seems pretty irrelevant now, so maybe it should be removed / updated.
I am also concerned this might change some error text behavior, since I don't see the PartOffset variable ever getting the header size value subtracted from it.
Although it's possible to exit early when PartOffset < LastOffset, what happens if, say, PartOffset = Data.getBufferSize() - sizeof(dxbc::PartHeader) + 1?
In the old code, it returns, but in this new code, it might not enter the if block, right?

beanz added inline comments.Dec 9 2022, 8:53 AM
llvm/lib/Object/DXContainer.cpp
108–109

Yep! I need to update that.

The PartOffset field is read from the buffer, so it shouldn't need to be modified. The offset values in the file _should_ point to valid offsets in the file from which we can read at least a part header (4 bytes for the part name and 4 bytes for the part size).

The ParsePartNoSize test below should cover the case where a part offset is too close to the end of the file to have a size field, but I think you're right that there is a missing case where the part size can't handle the part name field.

I'll update the patch.

Thanks!

beanz updated this revision to Diff 481675.Dec 9 2022, 9:41 AM

Updating based on PR feedback from bob80905. Thank you!

bob80905 accepted this revision.Dec 22 2022, 11:46 AM

Looks good to me.
Only minor nit at llvm/lib/Object/DXContainer.cpp:114.
Changing the condition from if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name)) to if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name) && PartOffset < Data.getBufferSize()))
would allow the parsed error message to be more precise, by explicitly stating the error is due to the partOffset being unable to read the part header name.
Then, another condition, PartOffset >= Data.getBufferSize())), would be able to definitively state that the part offset is beyond the file boundary.

This revision is now accepted and ready to land.Dec 22 2022, 11:46 AM
This revision was landed with ongoing or failed builds.Jan 3 2023, 10:50 AM
This revision was automatically updated to reflect the committed changes.