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

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

Closed
Michael-F-Bryan opened this issue Dec 14, 2023 · 2 comments · Fixed by wasmerio/wasmer#4355, #368 or #365
Closed
Assignees
Labels
bug Something isn't working linear Created by Linear-GitHub Sync

Comments

@Michael-F-Bryan
Copy link
Contributor

Michael-F-Bryan commented Dec 14, 2023

@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

@Michael-F-Bryan Michael-F-Bryan added bug Something isn't working linear Created by Linear-GitHub Sync labels Dec 14, 2023
@Michael-F-Bryan Michael-F-Bryan self-assigned this Dec 14, 2023
@Michael-F-Bryan Michael-F-Bryan added linear Created by Linear-GitHub Sync and removed linear Created by Linear-GitHub Sync labels Dec 14, 2023
@Michael-F-Bryan Michael-F-Bryan changed the title Providing stdin up front seems to always read the same bytes [SDK-38] Providing stdin up front seems to always read the same bytes Dec 14, 2023
@Michael-F-Bryan
Copy link
Contributor Author

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

@Michael-F-Bryan
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working linear Created by Linear-GitHub Sync
Projects
None yet
1 participant