Skip to content

Commit

Permalink
Change default PosixIo to use an empty vec for stdin (risc0#1148)
Browse files Browse the repository at this point in the history
This PR changes the default `PosixIo` struct to hook the guest `stdin`
to an empty vector, instead of the current default which is to use the
host process `stdin`. This resolves issue risc0#1016, where it is easy to
accidentally cause a difficult to debug hand on the process by
forgetting to add an `ExectorEnvBuilder::write` call, resulting in the
executor performing a blocking read on stdin.

Note that if a developer does indeed want the host process `stdin` to be
used as guest `stdin`, they can accomplish this by calling
`builder.stdin(std::io::stdin())` while building the `ExectorEnv`.

Related: risc0#1050
Resolves: risc0#1016
  • Loading branch information
nategraf authored Nov 15, 2023
1 parent 9d24766 commit ea88d8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion risc0/r0vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{fs, path::PathBuf, rc::Rc};
use std::{fs, io, path::PathBuf, rc::Rc};

use clap::{Args, Parser, ValueEnum};
use risc0_zkvm::{
Expand Down Expand Up @@ -45,6 +45,8 @@ struct Cli {
prove_guest_errors: bool,

/// File to read initial input from.
///
/// Reads input from stdin if an initial input file is not provided.
#[arg(long)]
initial_input: Option<PathBuf>,

Expand Down Expand Up @@ -122,6 +124,8 @@ pub fn main() {

if let Some(input) = args.initial_input.as_ref() {
builder.stdin(fs::File::open(input).unwrap());
} else {
builder.stdin(io::stdin());
}

#[cfg(feature = "profiler")]
Expand Down
4 changes: 2 additions & 2 deletions risc0/zkvm/src/host/client/posix_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::{
cell::RefCell,
collections::BTreeMap,
io::{stderr, stdin, stdout, BufRead, BufReader, Write},
io::{stderr, stdout, BufRead, Cursor, Write},
rc::Rc,
};

Expand All @@ -34,7 +34,7 @@ impl<'a> Default for PosixIo<'a> {
read_fds: Default::default(),
write_fds: Default::default(),
};
new.with_read_fd(fileno::STDIN, BufReader::new(stdin()))
new.with_read_fd(fileno::STDIN, Cursor::new(vec![]))
.with_write_fd(fileno::STDOUT, stdout())
.with_write_fd(fileno::STDERR, stderr());
new
Expand Down

0 comments on commit ea88d8f

Please sign in to comment.