-
Notifications
You must be signed in to change notification settings - Fork 261
spirv: fix binary encoding/decoding on big-endian hosts #3658
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
Open
amaanq
wants to merge
1
commit into
KhronosGroup:main
Choose a base branch
from
amaanq:fix-big-endian-spirv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+21
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -98,10 +98,20 @@ class SPIRVNL { | |||||
| friend spv_ostream &operator<<(spv_ostream &O, const SPIRVNL &E); | ||||||
| }; | ||||||
|
|
||||||
| /// Read a single SPIR-V word from a binary stream, byte-swapping from | ||||||
| /// little-endian on big-endian hosts. | ||||||
| inline uint32_t readSPIRVWord(std::istream &IS) { | ||||||
| uint32_t W; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets fix this while we are here as with this patch this may become critical parser vulnerability for attacker-controlled input as we start to use decodeBinary/readSPIRVWord to parse the header
Suggested change
|
||||||
| IS.read(reinterpret_cast<char *>(&W), sizeof(W)); | ||||||
| #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||||
| W = __builtin_bswap32(W); | ||||||
| #endif | ||||||
| return W; | ||||||
| } | ||||||
|
|
||||||
| template <typename T> | ||||||
| const SPIRVDecoder &decodeBinary(const SPIRVDecoder &I, T &V) { | ||||||
| uint32_t W; | ||||||
| I.IS.read(reinterpret_cast<char *>(&W), sizeof(W)); | ||||||
| uint32_t W = readSPIRVWord(I.IS); | ||||||
| V = static_cast<T>(W); | ||||||
| SPIRVDBG(spvdbgs() << "Read word: W = " << W << " V = " << V << '\n'); | ||||||
| return I; | ||||||
|
|
@@ -186,6 +196,10 @@ const SPIRVEncoder &operator<<(const SPIRVEncoder &O, T V) { | |||||
| } | ||||||
| #endif | ||||||
| uint32_t W = static_cast<uint32_t>(V); | ||||||
| #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | ||||||
| // SPIR-V binary format is little-endian; byte-swap on big-endian hosts. | ||||||
| W = __builtin_bswap32(W); | ||||||
| #endif | ||||||
| O.OS.write(reinterpret_cast<char *>(&W), sizeof(W)); | ||||||
| return O; | ||||||
| } | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand the code correctly, as isSpirvBinary is public API - Img can be the whole SPIR-V module passed in a library call. If I'm right, I'd like to avoid unnecessary copy of Img. I'd suggest to avoid it either by memcopying only 4 bytes or byte checking first 4 bytes byte-by-byte.