Skip to content

fix(ipc-runtime): treat wasm pointers above 2 GiB as unsigned in the WASI shim - #25864

Open
alejoamiras wants to merge 1 commit into
nextfrom
worktree-wasi-shim-high-pointers
Open

alejoamiras wants to merge 1 commit into
nextfrom
worktree-wasi-shim-high-pointers

Conversation

@alejoamiras

Copy link
Copy Markdown
Contributor

What was wrong

A wasm32 module hands its pointers and sizes to imports as i32. Once the module's memory grows past 2 GiB, any address above that line reaches the WASI shim as a negative number, and the shim used it as it came:

  • DataView throws a RangeError on a negative offset, so random_get, fd_write, the clock calls and the args/environ calls fail.
  • fd_fdstat_get is worse: Uint8Array.fill counts a negative start back from the end of the buffer, so it zeroes 24 bytes at the wrong address (memory length + pointer - 4 GiB) before the next line throws.

Start-up calls (args, environ, fdstat) usually see a small heap. random_get, fd_write and the clocks are the ones a large module reaches in practice. I found this while reading the shim during other bb.js host work and reproduced it with a small script; I have not seen it reported from a real run.

The fix

Every pointer and size argument is converted with >>> 0 before use in the nine imports that dereference one: args_sizes_get, args_get, environ_sizes_get, environ_get, clock_res_get, clock_time_get, random_get, fd_write and fd_fdstat_get. The other imports do not dereference a pointer, and host.ts already reads the thread-spawn argument, allocator results and response slots as unsigned.

Tests

src/wasm_wasi_shim.test.ts calls each of the nine imports on a 2 GiB + 64 KiB memory with an address just above 2 GiB and checks that the value lands there. The fd_fdstat_get case also checks that the 24 bytes at the address the old code zeroed are left alone. A seventh case, on a one-page memory, checks that sizes are read unsigned: random_get with a length of -1 (2^32 - 1) must throw instead of returning success with nothing written.

On next all seven new tests fail: five with RangeError, the fd_fdstat_get case on the zeroed bytes at the wrong address, and the length case because random_get returns success. With the fix the package's 64 tests pass. The memory is reserved but barely touched, so the file runs in well under a second at about 50 MB RSS, and the high-address cases skip themselves if the engine cannot reserve that much (checked under a 3.5 GB address-space limit: six skipped, the length case still passes).

Left for a follow-up

These are robustness gaps for invalid calls, not failures for valid high pointers, so they are not in this PR:

  • A descriptor of 2^31 or more still arrives negative, so fd_fdstat_get reports a character device instead of BADF.
  • fd_write does not bounds-check the iovec table up front, and slice/fill clamp out-of-range ranges, so an invalid call can report success.

…WASI shim

wasm passes its u32 pointers and sizes to imports as i32, so an address at or
above 2 GiB reaches the shim as a negative number. DataView then throws a
RangeError, and fd_fdstat_get's Uint8Array.fill counts the negative start back
from the end of memory, zeroing 24 bytes at the wrong address (memory length +
pointer - 4 GiB) before it throws.

Convert every pointer and size argument with >>> 0 in args_sizes_get, args_get,
environ_sizes_get, environ_get, clock_res_get, clock_time_get, random_get,
fd_write and fd_fdstat_get.
@AztecBot

Copy link
Copy Markdown
Collaborator

Flakey Tests

🤖 says: This CI run detected 1 tests that failed, but were tolerated due to a .test_patterns.yml entry.

\033FLAKED\033 (8;;http://ci.aztec-labs.com/a48fefa0dffb9d62�a48fefa0dffb9d628;;�): cd labs && export root= ci3= &&  yarn-project/end-to-end/scripts/run_test.sh simple src/p2p/preferred_gossip_network.test.ts (123s) (code: 0) group:e2e-p2p-epoch-flakes

This branch has not been deployed

No deployments
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.

2 participants