WasiCtx: default to empty/sink stdio files rather than throw
the test harness now uses the empty stdin file. I tested manually that the sink stdout & stderr files work, but theres no test in tree at the moment
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use wasi_cap_std_sync::WasiCtxBuilder;
|
use wasi_cap_std_sync::WasiCtxBuilder;
|
||||||
use wasi_common::pipe::{ReadPipe, WritePipe};
|
use wasi_common::pipe::WritePipe;
|
||||||
use wasmtime::{Linker, Module, Store};
|
use wasmtime::{Linker, Module, Store};
|
||||||
|
|
||||||
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
|
pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> anyhow::Result<()> {
|
||||||
@@ -18,7 +18,6 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
|||||||
builder = builder
|
builder = builder
|
||||||
.arg(bin_name)?
|
.arg(bin_name)?
|
||||||
.arg(".")?
|
.arg(".")?
|
||||||
.stdin(Box::new(ReadPipe::from(Vec::new())))
|
|
||||||
.stdout(Box::new(stdout.clone()))
|
.stdout(Box::new(stdout.clone()))
|
||||||
.stderr(Box::new(stderr.clone()));
|
.stderr(Box::new(stderr.clone()));
|
||||||
|
|
||||||
|
|||||||
@@ -65,16 +65,19 @@ pub struct WasiCtxBuilder(WasiCtx);
|
|||||||
impl WasiCtxBuilder {
|
impl WasiCtxBuilder {
|
||||||
pub fn build(self) -> Result<WasiCtx, Error> {
|
pub fn build(self) -> Result<WasiCtx, Error> {
|
||||||
use crate::file::TableFileExt;
|
use crate::file::TableFileExt;
|
||||||
let t = self.0.table();
|
// Default to an empty readpipe for stdin:
|
||||||
for (fd, name) in ["stdin", "stdout", "stderr"].iter().enumerate() {
|
if self.0.table().get_file(0).is_err() {
|
||||||
if t.get_file(fd as u32).is_err() {
|
let stdin = crate::pipe::ReadPipe::new(std::io::empty());
|
||||||
return Err(anyhow::anyhow!(
|
self.0.insert_file(0, Box::new(stdin), FileCaps::all());
|
||||||
"Cannot build WasiCtx: Missing required file `{}`",
|
}
|
||||||
name
|
// Default to a sink writepipe for stdout, stderr:
|
||||||
));
|
for stdio_write in &[1, 2] {
|
||||||
|
if self.0.table().get_file(*stdio_write).is_err() {
|
||||||
|
let output_file = crate::pipe::WritePipe::new(std::io::sink());
|
||||||
|
self.0
|
||||||
|
.insert_file(*stdio_write, Box::new(output_file), FileCaps::all());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drop(t);
|
|
||||||
Ok(self.0)
|
Ok(self.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,29 +92,17 @@ impl WasiCtxBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn stdin(self, f: Box<dyn WasiFile>) -> Self {
|
pub fn stdin(self, f: Box<dyn WasiFile>) -> Self {
|
||||||
self.0.insert_file(
|
self.0.insert_file(0, f, FileCaps::all());
|
||||||
0,
|
|
||||||
f,
|
|
||||||
FileCaps::READ | FileCaps::POLL_READWRITE, // XXX fixme: more rights are ok, but this is read-only
|
|
||||||
);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stdout(self, f: Box<dyn WasiFile>) -> Self {
|
pub fn stdout(self, f: Box<dyn WasiFile>) -> Self {
|
||||||
self.0.insert_file(
|
self.0.insert_file(1, f, FileCaps::all());
|
||||||
1,
|
|
||||||
f,
|
|
||||||
FileCaps::WRITE | FileCaps::POLL_READWRITE, // XXX fixme: more rights are ok, but this is append only
|
|
||||||
);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stderr(self, f: Box<dyn WasiFile>) -> Self {
|
pub fn stderr(self, f: Box<dyn WasiFile>) -> Self {
|
||||||
self.0.insert_file(
|
self.0.insert_file(2, f, FileCaps::all());
|
||||||
2,
|
|
||||||
f,
|
|
||||||
FileCaps::WRITE | FileCaps::POLL_READWRITE, // XXX fixme: more rights are ok, but this is append only
|
|
||||||
);
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user