From 0c4aec391e625193b17012e9d2cd33bc0165336a Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Mon, 1 Feb 2021 14:43:32 -0800 Subject: [PATCH] actually empty ready bytes of stdin --- Cargo.lock | 1 + crates/test-programs/Cargo.toml | 1 + .../tests/wasm_tests/runtime/cap_std_sync.rs | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index e2b792cf94..9c6f008259 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2801,6 +2801,7 @@ dependencies = [ "cfg-if 1.0.0", "os_pipe", "pretty_env_logger", + "system-interface", "target-lexicon", "tempfile", "wasi-cap-std-sync", diff --git a/crates/test-programs/Cargo.toml b/crates/test-programs/Cargo.toml index 458864bf2b..3dc15366b3 100644 --- a/crates/test-programs/Cargo.toml +++ b/crates/test-programs/Cargo.toml @@ -23,6 +23,7 @@ os_pipe = "0.9" anyhow = "1.0.19" wat = "1.0.23" cap-std = "0.12" +system-interface = "0.5.4" [features] test_programs = [] diff --git a/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs b/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs index a19494d284..df641d23f8 100644 --- a/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs +++ b/crates/test-programs/tests/wasm_tests/runtime/cap_std_sync.rs @@ -1,4 +1,5 @@ use anyhow::Context; +use std::convert::TryInto; use std::path::Path; use wasi_cap_std_sync::WasiCtxBuilder; use wasi_common::pipe::{ReadPipe, WritePipe}; @@ -90,7 +91,18 @@ pub fn instantiate_inherit_stdio( // Tests assume that stdin does not have any bytes available to read. Make sure this is the // case, regardless of the test environment: - let _ = std::io::stdin().read_to_end().expect("read stdin to end"); + use std::io::Read; + use system_interface::io::ReadReady; + let nbytes = std::io::stdin() + .num_ready_bytes() + .expect("get stdin's ready bytes"); + if nbytes > 0 { + let mut stdin_contents = Vec::new(); + stdin_contents.resize(nbytes.try_into().expect("ready bytes fits in memory"), 0); + std::io::stdin() + .read(stdin_contents.as_mut_slice()) + .expect("read stdin to end"); + } // Create our wasi context. // Additionally register any preopened directories if we have them.