wasmerio / wasmer-js

Monorepo for Javascript WebAssembly packages by Wasmer

Home Page:https://wasmerio.github.io/wasmer-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[SDK-38] Providing `stdin` up front seems to always read the same bytes

Michael-F-Bryan opened this issue · comments

@theduke made a little demo where we load an image and pass it to runWasix() as stdin. The WASI program then does a std::io::stdin().read_to_end() so it can receive the image and resize it. The code looks something like this:

const stdin = await file.arrayBuffer();

const instance = await runWasix(MODULE, {
  args: ["resize", "--width", width.toString(), "--height", height.toString()],
  stdin: new Uint8Array(stdin),
});

However, no matter what image we passed in, we always seemed to trigger a panic/OOM in the instance, which would then abort with an unreachable instruction.

2023-12-14_20-30

After modifying the WASI program to read std::io::stdin() in chunks, I discovered that every read seems to get the same data rather than reading successive bytes from the input until EOF is reached.

Failed to convert image: Read 1048576 bytes (total: 1048576, first couple: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
Read 1048576 bytes (total: 2097152, first 16 bytes: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
Read 1048576 bytes (total: 3145728, first 16 bytes: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
Read 1048576 bytes (total: 4194304, first 16 bytes: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
...
Read 1048576 bytes (total: 1072693248, first 16 bytes: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
Read 1048576 bytes (total: 1073741824, first 16 bytes: [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82])
thread 'main' panicked at library/alloc/src/raw_vec.rs:545:5:
capacity overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I noticed this was broken around the same time TTY handling was fixed (maybe somewhere around 8b5d439), but I pretended the problem wasn't there by only writing tests that do instance.stdin.write()... Looks like that laziness has come back to bite me 😅

SDK-38

Looks like this is actually an issue with virtual_fs::StaticFile. See wasmerio/wasmer#4355 for the fix.

Oops, Linear automatically marked this as closed when wasmerio/wasmer#4355 was merged, but I still need to update this repo to pick up the fix.