Skip to content

Return an error instead of panicking on fixed-length-list size overflow - #2643

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
zikk090:fix/sizealign-fixed-length-list-overflow
Sep 8, 2026
Merged

Return an error instead of panicking on fixed-length-list size overflow#2643
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
zikk090:fix/sizealign-fixed-length-list-overflow

Conversation

@zikk090

@zikk090 zikk090 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

SizeAlign::fill (and its internal calculate helper) computed the size of a FixedLengthList as element_size * length via checked_mul(..).unwrap(), which panics when the multiplication overflows usize. Since list<T, N> places no upper bound on N in the WIT text format (unlike the component binary format, which bounds it to MAX_WASM_FIXED_LENGTH_LIST_ELEMENTS), a WIT document with two nested fixed-length lists of a non-trivial element type is enough to overflow the multiplication and abort the process, in a release build too.

This changes SizeAlign::fill/calculate to return anyhow::Result and propagates that outward through wit-dylib's Adapter::encode, create, and create_with_metadata, and their two call sites (the wasm-tools wit-dylib CLI subcommand and the wit-dylib test-programs harness). A oversized fixed-length list now surfaces as a normal error instead of aborting:

$ wasm-tools wit-dylib poc.wit
error: size of fixed-length list of 4294967295 elements overflows
the target architecture's address space

Adds a regression test covering both the overflow case (now returns Err) and a benign nested fixed-length list (still computes correctly).

Fixes #2637

`SizeAlign::fill` (and its internal `calculate` helper) computed the
size of a `FixedLengthList` as `element_size * length` via
`checked_mul(..).unwrap()`, which panics when the multiplication
overflows `usize`. Since `list<T, N>` places no upper bound on `N` in
the WIT text format (unlike the component binary format, which bounds
it to `MAX_WASM_FIXED_LENGTH_LIST_ELEMENTS`), a WIT document with two
nested fixed-length lists of a non-trivial element type is enough to
overflow the multiplication and abort the process, in a release build
too.

This changes `SizeAlign::fill`/`calculate` to return `anyhow::Result`
and propagates that outward through `wit-dylib`'s `Adapter::encode`,
`create`, and `create_with_metadata`, and their two call sites (the
`wasm-tools wit-dylib` CLI subcommand and the wit-dylib test-programs
harness). A oversized fixed-length list now surfaces as a normal
error instead of aborting:

    $ wasm-tools wit-dylib poc.wit
    error: size of fixed-length list of 4294967295 elements overflows
    the target architecture's address space

Adds a regression test covering both the overflow case (now returns
`Err`) and a benign nested fixed-length list (still computes
correctly).

Fixes bytecodealliance#2637
@zikk090
zikk090 requested a review from a team as a code owner September 8, 2026 19:47
@zikk090
zikk090 requested review from dicej and removed request for a team September 8, 2026 19:47

@pchickey pchickey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Please fix the error discovered by the clippy CI job. The other failure appears spurrious

find_signed_load's test helper still called super::create() as if it
returned Vec<u8> directly; it now returns anyhow::Result<Vec<u8>>.
Caught by CI's clippy job (which builds --all-targets, unlike my local
clippy run that only covered the lib). Did an exhaustive grep across
the whole workspace afterward for every other Adapter/SizeAlign call
site to confirm this was the only one missed.
@alexcrichton
alexcrichton added this pull request to the merge queue Sep 8, 2026
Merged via the queue into bytecodealliance:main with commit 138f801 Sep 8, 2026
37 checks passed
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.

Integer overflow panic in wit-parser SizeAlign for nested fixed-length lists

3 participants