Skip to content

Conversation

@pchickey
Copy link
Contributor

@pchickey pchickey commented Aug 27, 2025

This patch is motivated by wasm-ld's concatenation of custom sections with the same name. wit-component and related stores component binaries in a module's custom section, and if these sections are unintentionally duplicated and then concatenated by wasm-ld, we get a strange error when parsing the corrupted binary.

There is no way to detect that a wasm module or component contains more information than its original length, because the wasm binary format is a fixed header and then any number of sections.

If two valid wasm modules or components are concatenated into a single binary, the start of the second module will be parsed as another section. The wasm magic number \0asm where \0 is parsed as the marker for a custom section, a will be parsed as an leb section length of 97, s will parse as an leb length 115 for the custom section's string name. Because that string does not fit in the section, this "section" will always fail to parse. Currently, it gives the error message unexpected end-of-file, which is somewhat misleading - it wasn't actually the end-of-file encountered, that ends up just being an artifact of how wasmparser represents sections to readers.

So, we now do a 4-byte lookahead at the start of parsing each section and give this error case a more useful message.

@pchickey pchickey requested a review from a team as a code owner August 27, 2025 00:34
@pchickey pchickey requested review from alexcrichton and dicej and removed request for a team and dicej August 27, 2025 00:34
@pchickey pchickey force-pushed the pch/parse_corrupted branch 2 times, most recently from 98b416a to a153800 Compare August 27, 2025 01:18
@pchickey pchickey force-pushed the pch/parse_corrupted branch from a153800 to 4a663ba Compare August 27, 2025 01:21
@alexcrichton alexcrichton added this pull request to the merge queue Aug 27, 2025
Merged via the queue into main with commit 4c16e95 Aug 27, 2025
34 checks passed
@alexcrichton alexcrichton deleted the pch/parse_corrupted branch August 27, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants