Skip to content

wasi fd_read sets bogous n_bytes_read value / ignores buffer length on webassembly.shΒ #112

@jcaesar

Description

@jcaesar

The following Rust code panics in webassembly.sh when compiled with rustc β‰₯ 1.57 (but runs fine on desktop wasmer).

use std::io::Read;
fn main() {
    let mut buffer = String::new();
    std::io::stdin().read_to_string(&mut buffer).unwrap();
}

Panic

thread 'main' panicked at 'assertion failed: n <= buf.len()', library/std/src/io/mod.rs:398:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Quick repro:

  • open
  • enter echo putatleastthirtytwobyteshereandyouwillsee | ./repro

The failing assert is here. I haven't really looked at how the surrounding code changed, but change it did.

Adding some more debug output to wasi, I get the following with 1.56 on desktop

[1653482124.970 DEBUG wasmer_wasi::state] Initializing WASI filesystem
[1653482124.972 DEBUG wasmer_wasi::syscalls] wasi::environ_sizes_get
[1653482124.972 DEBUG wasmer_wasi::syscalls] env_var_count: 0, env_buf_size: 0
[1653482124.972 DEBUG wasmer_wasi::syscalls] wasi::fd_read: fd=0
[1653482124.972 DEBUG wasmer_wasi::syscalls] wasi::read_bytes: buf=0x00102910 buf_len=8192 bytes_read=42
[1653482124.972 DEBUG wasmer_wasi::syscalls] wasi::fd_read: fd=0
[1653482124.973 DEBUG wasmer_wasi::syscalls] wasi::read_bytes: buf=0x00102910 buf_len=8192 bytes_read=0

and the following on 1.57

[1653482131.932 DEBUG wasmer_wasi::state] Initializing WASI filesystem
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::environ_sizes_get
[1653482131.934 DEBUG wasmer_wasi::syscalls] env_var_count: 0, env_buf_size: 0
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::fd_read: fd=0
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::read_bytes: buf=0x001048b0 buf_len=32 bytes_read=32
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::fd_read: fd=0
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::read_bytes: buf=0x001048d0 buf_len=32 bytes_read=10
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::fd_read: fd=0
[1653482131.934 DEBUG wasmer_wasi::syscalls] wasi::read_bytes: buf=0x001048da buf_len=22 bytes_read=0

so I guess the initial buffer size changed and webassembly.sh somehow manages to return the wrong value for bytes_read (maybe 42?).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions