You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
wasmparser: usize may not be the most appropriate type for offsets #838
The wasmparser implements an incremental parser for the binary webassembly encoding. In practice this means that tooling can work with files larger the size of the virtual memory. This is not unlike the concept of 32-bit systems still being capable of working with filesystem files larger than 4GiB (i.e. off_t > uint32_t).
While wasmparser does impose a limit on the number of entities within module contents, these limits are not sufficiently low to limit modules to under-4GiB on 32-bit (or 64-kiB on 16-bit) targets. That is, I believe, quite intentional. However it still uses usize to represent the offset while parsing, which seems like a foot gun. We probably should replace the offset with a u64.
This issue has not been written with any particular use-case in mind, so it isn’t something that needs to be fixed ASAP, but it still seems like something we probably should look into fixing when the opportunity presents itself.
I definitely agree that usize probably isn't the best, and I think that it should switch to using u64. The offsets I think are pretty pervasively used for in-memory indexing though so I think some care will be required to avoid adding lots of as casts all over the place, but it may also not be as bad as I think .
During #2573 I find that a lot of the consumers (even only in this repository) load the full wasm file into memory first and then expect to be able to index into that Vec with the parsed ranges and offsets.
Two strategies come to mind:
either convert from u64 to usize in every consumer where that indexing happens and leave a comment about the fact that indices are small enough
make the parsers and structures generic over the offset type used (allowing both u64 and usize, maybe others) and using usize when the whole file is loaded into memory in the first place.
The former would be more readable imo, but less ergonomics. The latter needs a bunch of adjustments, especially since Operator<'_>, the visitor traits and their accompanying helper macros etc need to also become generic over the offset.
Let me know if you favour one approach over the other.
Thanks for the PR! Personally I think I'd favor the former case as well. W.r.t. the exact specifics of your PR I'd personally go the route of using u64 directly instead of a LogicalOffset typedef, and then I'd additionally skip the MemOffset abstraction as that seems like it's probably overkill. The main thing seems like it would be to validate in the constructor of BinaryReader that the provided u64 offset added to the length of the blob provided is in-range for u64. The position field would remain as usize, and then conversions would all be known to be infallible.
Activity
alexcrichton commentedon Nov 23, 2022
I definitely agree that
usizeprobably isn't the best, and I think that it should switch to usingu64. The offsets I think are pretty pervasively used for in-memory indexing though so I think some care will be required to avoid adding lots ofascasts all over the place, but it may also not be as bad as I think .WorldSEnder commentedon Jul 19, 2026
During #2573 I find that a lot of the consumers (even only in this repository) load the full wasm file into memory first and then expect to be able to index into that
Vecwith the parsed ranges and offsets.Two strategies come to mind:
u64andusize, maybe others) and usingusizewhen the whole file is loaded into memory in the first place.The former would be more readable imo, but less ergonomics. The latter needs a bunch of adjustments, especially since
Operator<'_>, the visitor traits and their accompanying helper macros etc need to also become generic over the offset.Let me know if you favour one approach over the other.
alexcrichton commentedon Jul 20, 2026
Thanks for the PR! Personally I think I'd favor the former case as well. W.r.t. the exact specifics of your PR I'd personally go the route of using
u64directly instead of aLogicalOffsettypedef, and then I'd additionally skip theMemOffsetabstraction as that seems like it's probably overkill. The main thing seems like it would be to validate in the constructor ofBinaryReaderthat the providedu64offset added to the length of the blob provided is in-range foru64. Thepositionfield would remain asusize, and then conversions would all be known to be infallible.