Skip to content
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

Handle non-utf8 hosts in the adapter #8749

Conversation

alexcrichton
Copy link
Member

This commit is a fix to the WASIp1 adapter for components to better handle the case where the host does not use a utf-8 string encoding. This is never the case for wasmtime-the-crate since it's a Rust-based host but this adapter is used outside of Wasmtime in jco, for example, where JS is not utf-8-based. When transcoding from utf-16 to utf-8 hosts may make an overlarge allocation and then shrink to a smaller allocation. This shrinking step has never been supported by the adapter and it's always aborted in this case.

Aside: why is this only a problem now? This hasn't been an issue before now because jco bindings never actually shrank. In doing so however this violated the canonical ABI because allocations are guaranteed to be precisely sized. New debug assertions in newer versions of Rust caught this mistake. This means that when jco tried to add downsizing of the allocation it quickly hit this panic in the adapter.

The fix in this commit is to handle the specific case of shrinking memory. The specific fix is to simply ignore the shrinking of memory. This is pretty subtle though why it seems to work out well enough for now (and it's probably still buggy). For now though this is enough to get jco's test suite passing with a shrinking allocation.

Unfortunately I don't know of a way to test this in this repository. Wasmtime does not support multiple encodings of host strings, only guest strings. This means that there's no wasmtime-based way to pass a non-utf-8 string into a guest.

This commit is a fix to the WASIp1 adapter for components to better
handle the case where the host does not use a utf-8 string encoding.
This is never the case for `wasmtime`-the-crate since it's a Rust-based
host but this adapter is used outside of Wasmtime in jco, for example,
where JS is not utf-8-based. When transcoding from utf-16 to utf-8 hosts
may make an overlarge allocation and then shrink to a smaller
allocation. This shrinking step has never been supported by the adapter
and it's always aborted in this case.

Aside: why is this only a problem now? This hasn't been an issue before
now because jco bindings never actually shrank. In doing so however this
violated the canonical ABI because allocations are guaranteed to be
precisely sized. New debug assertions in newer versions of Rust caught
this mistake. This means that when jco tried to add downsizing of the
allocation it quickly hit this panic in the adapter.

The fix in this commit is to handle the specific case of shrinking
memory. The specific fix is to simply ignore the shrinking of memory.
This is pretty subtle though why it seems to work out well enough for
now (and it's probably still buggy). For now though this is enough to
get jco's test suite passing with a shrinking allocation.

Unfortunately I don't know of a way to test this in this repository.
Wasmtime does not support multiple encodings of host strings, only guest
strings. This means that there's no wasmtime-based way to pass a
non-utf-8 string into a guest.
@alexcrichton alexcrichton requested a review from a team as a code owner June 5, 2024 21:56
@alexcrichton alexcrichton requested review from pchickey and removed request for a team June 5, 2024 21:56
@guybedford
Copy link
Contributor

//cc @kateinoigakukun

@alexcrichton alexcrichton added this pull request to the merge queue Jun 5, 2024
Merged via the queue into bytecodealliance:main with commit 59ae63b Jun 5, 2024
36 checks passed
@alexcrichton alexcrichton deleted the fix-adapter-shrinking-memory branch June 5, 2024 23:59
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