Repository navigation
fix(ipc-runtime): treat wasm pointers above 2 GiB as unsigned in the WASI shim - #25864
Open
alejoamiras wants to merge 1 commit into
Open
alejoamiras wants to merge 1 commit into
alejoamiras wants to merge 1 commit into
Conversation
…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.
Collaborator
Flakey Tests🤖 says: This CI run detected 1 tests that failed, but were tolerated due to a .test_patterns.yml entry. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
DataViewthrows aRangeErroron a negative offset, sorandom_get,fd_write, the clock calls and the args/environ calls fail.fd_fdstat_getis worse:Uint8Array.fillcounts 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_writeand 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
>>> 0before 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_writeandfd_fdstat_get. The other imports do not dereference a pointer, andhost.tsalready reads the thread-spawn argument, allocator results and response slots as unsigned.Tests
src/wasm_wasi_shim.test.tscalls 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. Thefd_fdstat_getcase 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_getwith a length of -1 (2^32 - 1) must throw instead of returning success with nothing written.On
nextall seven new tests fail: five withRangeError, thefd_fdstat_getcase on the zeroed bytes at the wrong address, and the length case becauserandom_getreturns 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:
fd_fdstat_getreports a character device instead of BADF.fd_writedoes not bounds-check the iovec table up front, andslice/fillclamp out-of-range ranges, so an invalid call can report success.