Skip to content

wasmparser: usize may not be the most appropriate type for offsets #838

Closed

Description

@nagisa

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.

Activity

  1. alexcrichton commented on Nov 23, 2022

    @alexcrichton
    Member

    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 .

  2. added
    wasmparser
    on Dec 6, 2022
  3. WorldSEnder commented on Jul 19, 2026

    @WorldSEnder
    Contributor

    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.

  4. alexcrichton commented on Jul 20, 2026

    @alexcrichton
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasmparser

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions