feat(resource): add stdin and stderr stdio endpoints
This commit is contained in:
1 parent
7fbe069196
commit
aed6a80ba7
6 files changed
+377
-18
No files matched your search
@@ -815,7 +815,8 @@ pub fn execute(
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
let mut host = StdHost::new(mode, capabilities, project.root.clone());
|
||||
let stdin = read_stdin();
|
||||
let mut host = StdHost::new(mode, capabilities, project.root.clone()).with_stdin(stdin);
|
||||
if trace || json || settings.trace_file.is_some() {
|
||||
host = host
|
||||
.with_trace()
|
||||
@@ -935,6 +936,8 @@ pub fn execute(
|
||||
}
|
||||
let stdout = host.take_output();
|
||||
let stdout_text = String::from_utf8_lossy(&stdout).to_string();
|
||||
let stderr = host.take_error();
|
||||
let stderr_text = String::from_utf8_lossy(&stderr).to_string();
|
||||
let trace_entries = host.trace().to_vec();
|
||||
match result {
|
||||
Ok(outcome) => {
|
||||
@@ -944,11 +947,15 @@ pub fn execute(
|
||||
"success": true,
|
||||
"value": marqdo_value::to_wire(&outcome.value),
|
||||
"stdout": stdout_text,
|
||||
"stderr": stderr_text,
|
||||
"diagnostics": output::diagnostics_json(&project.diagnostics),
|
||||
"trace": trace_entries,
|
||||
}));
|
||||
} else {
|
||||
use std::io::Write;
|
||||
if !stderr.is_empty() {
|
||||
let _ = std::io::stderr().write_all(&stderr);
|
||||
}
|
||||
let _ = std::io::stdout().write_all(&stdout);
|
||||
if function.is_some() && outcome.value != Value::Unit {
|
||||
println!("{}", outcome.value.display());
|
||||
@@ -970,11 +977,15 @@ pub fn execute(
|
||||
"command": command_name,
|
||||
"success": false,
|
||||
"stdout": stdout_text,
|
||||
"stderr": stderr_text,
|
||||
"error": runtime_error_json(&error, &project),
|
||||
"trace": trace_entries,
|
||||
}));
|
||||
} else {
|
||||
use std::io::Write;
|
||||
if !stderr.is_empty() {
|
||||
let _ = std::io::stderr().write_all(&stderr);
|
||||
}
|
||||
let _ = std::io::stdout().write_all(&stdout);
|
||||
eprint!("{}", render_runtime_error(&error, &project));
|
||||
}
|
||||
@@ -983,6 +994,25 @@ pub fn execute(
|
||||
}
|
||||
}
|
||||
|
||||
/// Read piped stdin once at the composition boundary.
|
||||
///
|
||||
/// A terminal stdin is not read, so `marqdo run` never blocks waiting for an
|
||||
/// interactive EOF. Non-terminal input is a finite pipe/file and is copied into
|
||||
/// the run-scoped `stdio/stdin` resource.
|
||||
fn read_stdin() -> Vec<u8> {
|
||||
use std::io::{IsTerminal, Read};
|
||||
let mut stdin = std::io::stdin();
|
||||
if stdin.is_terminal() {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
if let Err(error) = stdin.read_to_end(&mut bytes) {
|
||||
eprintln!("error: cannot read stdin: {error}");
|
||||
return Vec::new();
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
fn load_kernel_descriptor(host: &mut StdHost, path: &Path) -> Result<(), String> {
|
||||
let text = std::fs::read_to_string(path).map_err(|error| {
|
||||
format!(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! End-to-end resource stream tests through the CLI.
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
use std::process::{Command, Output, Stdio};
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
@@ -33,6 +34,24 @@ fn cli(dir: &Path, args: &[&str]) -> Output {
|
||||
.expect("run marqdo")
|
||||
}
|
||||
|
||||
fn cli_with_stdin(dir: &Path, args: &[&str], input: &[u8]) -> Output {
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("spawn marqdo");
|
||||
child
|
||||
.stdin
|
||||
.take()
|
||||
.expect("stdin pipe")
|
||||
.write_all(input)
|
||||
.expect("write stdin");
|
||||
child.wait_with_output().expect("wait for marqdo")
|
||||
}
|
||||
|
||||
fn write_file(path: &Path, contents: &str) {
|
||||
std::fs::write(path, contents).unwrap();
|
||||
}
|
||||
@@ -295,3 +314,95 @@ fn file_stream_requires_a_read_grant() {
|
||||
assert_eq!(report["success"], false);
|
||||
assert_eq!(report["error"]["code"], "permission.denied");
|
||||
}
|
||||
|
||||
const STDIN_READER: &str = r#"## main
|
||||
|
||||
**输入 = resource.open("stdio", "stdin", ["read"])**
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**第一 = resource.read(输入, 内存, 0, 2)**
|
||||
|
||||
**第二 = resource.read(输入, 内存, 2, 8)**
|
||||
|
||||
**第三 = resource.read(输入, 内存, 0, 1)**
|
||||
|
||||
*[第一.value.count, 第一.value.eof, 第二.value.count, 第二.value.eof, 第三.value.eof, 内存[0], 内存[1], 内存[2], 内存[3]]*
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn piped_stdin_is_chunked_and_reaches_eof() {
|
||||
let dir = TempDir::new("stdin");
|
||||
write_file(&dir.path().join("index.mq.md"), STDIN_READER);
|
||||
let output = cli_with_stdin(dir.path(), &["run", "index.mq.md", "--json"], b"abcd");
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
|
||||
assert_eq!(report["success"], true);
|
||||
let values = report["value"]["items"].as_array().expect("list result");
|
||||
assert_eq!(values[0]["decimal"], "2");
|
||||
assert_eq!(values[1]["value"], false);
|
||||
assert_eq!(values[2]["decimal"], "2");
|
||||
assert_eq!(values[3]["value"], true);
|
||||
assert_eq!(values[4]["value"], true);
|
||||
for (index, expected) in ["97", "98", "99", "100"].into_iter().enumerate() {
|
||||
assert_eq!(values[5 + index]["decimal"], expected);
|
||||
}
|
||||
}
|
||||
|
||||
const STDERR_WRITER: &str = r#"## main
|
||||
|
||||
**错误 = resource.open("stdio", "stderr", ["write"])**
|
||||
|
||||
**输出 = resource.open("stdio", "stdout", ["write"])**
|
||||
|
||||
**内存 = memory.new(1, 1)**
|
||||
|
||||
**`内存[0] = 101`**
|
||||
|
||||
**`内存[1] = 114`**
|
||||
|
||||
**`内存[2] = 114`**
|
||||
|
||||
**写错误 = resource.write(错误, 内存, 0, 3)**
|
||||
|
||||
**`内存[0] = 111`**
|
||||
|
||||
**`内存[1] = 117`**
|
||||
|
||||
**`内存[2] = 116`**
|
||||
|
||||
**写输出 = resource.write(输出, 内存, 0, 3)**
|
||||
|
||||
**关闭错误 = resource.close(错误)**
|
||||
|
||||
**关闭输出 = resource.close(输出)**
|
||||
|
||||
*[写错误.value.count, 写输出.value.count, 关闭错误.ok, 关闭输出.ok]*
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn program_stderr_is_separate_from_stdout_and_diagnostics() {
|
||||
let dir = TempDir::new("stderr");
|
||||
write_file(&dir.path().join("index.mq.md"), STDERR_WRITER);
|
||||
let output = cli(dir.path(), &["run", "index.mq.md", "--json"]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("json output");
|
||||
assert_eq!(report["success"], true);
|
||||
assert_eq!(report["stdout"], "out");
|
||||
assert_eq!(report["stderr"], "err");
|
||||
let values = report["value"]["items"].as_array().expect("list result");
|
||||
assert_eq!(values[0]["decimal"], "3");
|
||||
assert_eq!(values[1]["decimal"], "3");
|
||||
assert_eq!(values[2]["value"], true);
|
||||
assert_eq!(values[3]["value"], true);
|
||||
}
|
||||
@@ -203,6 +203,10 @@ pub struct StdHost {
|
||||
broker_audit: Arc<Mutex<Vec<CapabilityAudit>>>,
|
||||
/// Host-issued opaque plugin handles.
|
||||
pub handles: PluginHandleTable,
|
||||
/// Run-scoped stdin bytes supplied by the composition root.
|
||||
stdin: Vec<u8>,
|
||||
/// Program stderr captured separately from stdout and diagnostics.
|
||||
stderr: Vec<u8>,
|
||||
/// Host-owned resource/stream table (ADR-0025).
|
||||
resources: ResourceTable,
|
||||
/// Registered in-process providers keyed by resource kind (ADR-0033).
|
||||
@@ -267,6 +271,8 @@ impl StdHost {
|
||||
vendor_capabilities: BTreeSet::new(),
|
||||
broker_audit: Arc::new(Mutex::new(Vec::new())),
|
||||
handles: PluginHandleTable::default(),
|
||||
stdin: Vec::new(),
|
||||
stderr: Vec::new(),
|
||||
resources: ResourceTable::new(),
|
||||
resource_providers: BTreeMap::new(),
|
||||
semantic_events: Vec::new(),
|
||||
@@ -476,6 +482,25 @@ impl StdHost {
|
||||
&self.output
|
||||
}
|
||||
|
||||
/// Attach run-scoped stdin bytes at assembly time (composition root).
|
||||
///
|
||||
/// This does not start a thread, spawn a process or read a hidden second
|
||||
/// channel. A program reads these bytes only through `stdio/stdin`.
|
||||
pub fn with_stdin(mut self, bytes: impl Into<Vec<u8>>) -> Self {
|
||||
self.stdin = bytes.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Buffered program stderr without consuming it.
|
||||
pub fn error(&self) -> &[u8] {
|
||||
&self.stderr
|
||||
}
|
||||
|
||||
/// Take buffered program stderr.
|
||||
pub fn take_error(&mut self) -> Vec<u8> {
|
||||
std::mem::take(&mut self.stderr)
|
||||
}
|
||||
|
||||
fn dispatch(
|
||||
&mut self,
|
||||
op: HostOp,
|
||||
@@ -728,7 +753,8 @@ impl StdHost {
|
||||
let requested: BTreeSet<&str> = rights.iter().map(String::as_str).collect();
|
||||
if self.mode == SandboxMode::Strict {
|
||||
// stdio/stdout is the existing console channel already permitted by
|
||||
// strict L1 (CM-A11); file/network/process providers are not.
|
||||
// strict L1 (CM-A11); stdin, stderr, file/network/process providers
|
||||
// are not, because strict isolation has not been proven for them.
|
||||
let stdio_console =
|
||||
kind == "stdio" && descriptor == "stdout" && requested == BTreeSet::from(["write"]);
|
||||
if !stdio_console {
|
||||
@@ -749,27 +775,37 @@ impl StdHost {
|
||||
}
|
||||
match kind {
|
||||
"stdio" => {
|
||||
if descriptor != "stdout" || requested != BTreeSet::from(["write"]) {
|
||||
let allowed = match descriptor {
|
||||
"stdout" | "stderr" => requested == BTreeSet::from(["write"]),
|
||||
"stdin" => requested == BTreeSet::from(["read"]),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
ctx.span,
|
||||
"the first stream slice supports only stdio/stdout with rights [write]",
|
||||
"stdio supports stdout/stderr with rights [write] and stdin with rights [read]",
|
||||
));
|
||||
}
|
||||
if !self.capabilities.console {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
ctx.span,
|
||||
"the console capability is required for stdio/stdout",
|
||||
"the console capability is required for stdio resources",
|
||||
));
|
||||
}
|
||||
let handle = self.resources.open_stdout();
|
||||
let handle = match descriptor {
|
||||
"stdin" => self.resources.open_stdin(self.stdin.clone()),
|
||||
"stderr" => self.resources.open_stderr(),
|
||||
"stdout" => self.resources.open_stdout(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.record_semantic(
|
||||
"resource.open",
|
||||
None,
|
||||
Some(&ctx.module_id),
|
||||
ctx.span,
|
||||
Some("kind=stdio descriptor=stdout"),
|
||||
Some(&format!("kind=stdio descriptor={descriptor}")),
|
||||
);
|
||||
self.record_resource_effect("resource.open", ctx, started, "ok");
|
||||
Ok(Value::Resource(handle))
|
||||
@@ -999,10 +1035,15 @@ impl Host for StdHost {
|
||||
) -> Result<usize, RuntimeError> {
|
||||
let started = Instant::now();
|
||||
let kind = self.resources.kind(handle, ctx.span)?;
|
||||
let stdio = self.resources.stdio_name(handle, ctx.span)?;
|
||||
let count = self.resources.write(handle, bytes, ctx.span)?;
|
||||
if kind == "stdio" {
|
||||
self.budget.consume_output(count, ctx.span)?;
|
||||
self.output.extend_from_slice(bytes);
|
||||
if stdio == Some("stderr") {
|
||||
self.stderr.extend_from_slice(&bytes[..count]);
|
||||
} else {
|
||||
self.output.extend_from_slice(&bytes[..count]);
|
||||
}
|
||||
}
|
||||
self.record_semantic(
|
||||
"resource.write",
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
//! Host-owned resource/stream table (ADR-0025).
|
||||
//!
|
||||
//! The first slice deliberately supports only stdout writes and bounded
|
||||
//! project-file reads loaded at open. Handles are run-scoped and opaque; the
|
||||
//! table validates run identity, slot identity and generation on every call.
|
||||
//! The stream table owns run-scoped stdio endpoints, bounded project-file
|
||||
//! read/write streams, explicitly registered provider tokens and IP-literal TCP
|
||||
//! streams. Handles are opaque and the table validates run identity, slot
|
||||
//! identity and generation on every call. Bytes move only through `Memory`.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{Read, Write};
|
||||
@@ -76,9 +77,17 @@ pub struct ResourceRead {
|
||||
}
|
||||
|
||||
enum Stream {
|
||||
Stdin {
|
||||
bytes: Vec<u8>,
|
||||
position: usize,
|
||||
closed: bool,
|
||||
},
|
||||
Stdout {
|
||||
closed: bool,
|
||||
},
|
||||
Stderr {
|
||||
closed: bool,
|
||||
},
|
||||
File {
|
||||
relative: String,
|
||||
bytes: Vec<u8>,
|
||||
@@ -110,7 +119,9 @@ struct Slot {
|
||||
impl Slot {
|
||||
fn closed(&self) -> bool {
|
||||
match &self.stream {
|
||||
Stream::Stdin { closed, .. } => *closed,
|
||||
Stream::Stdout { closed } => *closed,
|
||||
Stream::Stderr { closed } => *closed,
|
||||
Stream::File { closed, .. } => *closed,
|
||||
Stream::FileWriter { closed, .. } => *closed,
|
||||
Stream::Provider { closed, .. } => *closed,
|
||||
@@ -152,6 +163,39 @@ impl ResourceTable {
|
||||
self.insert(Stream::Stdout { closed: false })
|
||||
}
|
||||
|
||||
/// Register the pre-opened stderr stream.
|
||||
pub fn open_stderr(&mut self) -> ResourceHandle {
|
||||
self.insert(Stream::Stderr { closed: false })
|
||||
}
|
||||
|
||||
/// Register a run-scoped stdin stream over already supplied bytes.
|
||||
///
|
||||
/// The host supplies bytes at assembly time; `read` chunks them with EOF.
|
||||
/// There is no blocking OS read and no hidden second input channel here.
|
||||
pub fn open_stdin(&mut self, bytes: Vec<u8>) -> ResourceHandle {
|
||||
self.insert(Stream::Stdin {
|
||||
bytes,
|
||||
position: 0,
|
||||
closed: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the stdio descriptor name for a handle, if it is a stdio stream.
|
||||
pub fn stdio_name(
|
||||
&self,
|
||||
handle: ResourceHandle,
|
||||
span: Option<Span>,
|
||||
) -> Result<Option<&'static str>, RuntimeError> {
|
||||
let slot = self.slot(handle, span)?;
|
||||
Ok(match &slot.stream {
|
||||
Stream::Stdin { .. } => Some("stdin"),
|
||||
Stream::Stdout { .. } => Some("stdout"),
|
||||
Stream::Stderr { .. } => Some("stderr"),
|
||||
Stream::File { .. } | Stream::FileWriter { .. } => None,
|
||||
Stream::Provider { .. } | Stream::Tcp { .. } => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Register a project-file read stream opened from already-bounded bytes.
|
||||
pub fn open_file(&mut self, relative: impl Into<String>, bytes: Vec<u8>) -> ResourceHandle {
|
||||
self.insert(Stream::File {
|
||||
@@ -268,7 +312,10 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::File {
|
||||
Stream::Stdin {
|
||||
bytes, position, ..
|
||||
}
|
||||
| Stream::File {
|
||||
bytes, position, ..
|
||||
} => {
|
||||
let start = *position;
|
||||
@@ -285,6 +332,11 @@ impl ResourceTable {
|
||||
span,
|
||||
"stdout does not support resource.read",
|
||||
)),
|
||||
Stream::Stderr { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
"stderr does not support resource.read",
|
||||
)),
|
||||
Stream::FileWriter { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
@@ -333,7 +385,12 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::Stdout { .. } => Ok(bytes.len()),
|
||||
Stream::Stdout { .. } | Stream::Stderr { .. } => Ok(bytes.len()),
|
||||
Stream::Stdin { .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
"stdin is a read-only resource stream",
|
||||
)),
|
||||
Stream::File { relative, .. } => Err(RuntimeError::new(
|
||||
ErrorCategory::ResourceUnsupported,
|
||||
span,
|
||||
@@ -396,7 +453,9 @@ impl ResourceTable {
|
||||
));
|
||||
}
|
||||
match &mut slot.stream {
|
||||
Stream::Stdin { closed, .. } => *closed = true,
|
||||
Stream::Stdout { closed } => *closed = true,
|
||||
Stream::Stderr { closed } => *closed = true,
|
||||
Stream::File { closed, .. } => *closed = true,
|
||||
Stream::FileWriter { closed, .. } => *closed = true,
|
||||
Stream::Provider {
|
||||
@@ -447,7 +506,9 @@ impl ResourceTable {
|
||||
pub fn kind(&self, handle: ResourceHandle, span: Option<Span>) -> Result<String, RuntimeError> {
|
||||
let slot = self.slot(handle, span)?;
|
||||
Ok(match &slot.stream {
|
||||
Stream::Stdout { .. } => "stdio".to_string(),
|
||||
Stream::Stdin { .. } | Stream::Stdout { .. } | Stream::Stderr { .. } => {
|
||||
"stdio".to_string()
|
||||
}
|
||||
Stream::File { .. } | Stream::FileWriter { .. } => "file".to_string(),
|
||||
Stream::Provider { kind, .. } => kind.clone(),
|
||||
Stream::Tcp { .. } => "tcp".to_string(),
|
||||
@@ -536,4 +597,57 @@ mod tests {
|
||||
"resource.unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdin_reads_are_chunked_and_end_at_eof() {
|
||||
let mut table = ResourceTable::new();
|
||||
let stdin = table.open_stdin(b"ab".to_vec());
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: b"a".to_vec(),
|
||||
eof: false
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.read(stdin, 8, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: b"b".to_vec(),
|
||||
eof: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap(),
|
||||
ResourceRead {
|
||||
bytes: Vec::new(),
|
||||
eof: true
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
table.write(stdin, b"x", None).unwrap_err().code(),
|
||||
"resource.unsupported"
|
||||
);
|
||||
table.close(stdin, None).unwrap();
|
||||
assert_eq!(
|
||||
table.read(stdin, 1, None).unwrap_err().code(),
|
||||
"resource.closed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stderr_is_write_only() {
|
||||
let mut table = ResourceTable::new();
|
||||
let stderr = table.open_stderr();
|
||||
assert_eq!(table.write(stderr, b"diagnostic", None).unwrap(), 10);
|
||||
assert_eq!(
|
||||
table.read(stderr, 1, None).unwrap_err().code(),
|
||||
"resource.unsupported"
|
||||
);
|
||||
assert_eq!(table.stdio_name(stderr, None).unwrap(), Some("stderr"));
|
||||
table.close(stderr, None).unwrap();
|
||||
assert_eq!(
|
||||
table.write(stderr, b"x", None).unwrap_err().code(),
|
||||
"resource.closed"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,63 @@ fn stdout_write_obeys_the_output_budget_atomically() {
|
||||
assert!(host.take_output().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pure_mode_reads_stdin_through_a_resource() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut host = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path())
|
||||
.with_stdin(b"abcd".to_vec());
|
||||
let ctx = context();
|
||||
let handle = handle(
|
||||
host.resource_open("stdio", "stdin", &["read".to_string()], &ctx)
|
||||
.unwrap(),
|
||||
);
|
||||
let first = host.resource_read(handle, 2, &ctx).unwrap();
|
||||
assert_eq!(first.bytes, b"ab");
|
||||
assert!(!first.eof);
|
||||
let rest = host.resource_read(handle, 8, &ctx).unwrap();
|
||||
assert_eq!(rest.bytes, b"cd");
|
||||
assert!(rest.eof);
|
||||
assert_eq!(host.resource_read(handle, 1, &ctx).unwrap().bytes, b"");
|
||||
|
||||
let write = host.resource_write(handle, b"x", &ctx).unwrap_err();
|
||||
assert_eq!(write.code(), "resource.unsupported");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdout_and_stderr_are_captured_separately() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut host = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
|
||||
let ctx = context();
|
||||
let stdout = handle(
|
||||
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
|
||||
.unwrap(),
|
||||
);
|
||||
let stderr = handle(
|
||||
host.resource_open("stdio", "stderr", &["write".to_string()], &ctx)
|
||||
.unwrap(),
|
||||
);
|
||||
assert_eq!(host.resource_write(stdout, b"out", &ctx).unwrap(), 3);
|
||||
assert_eq!(host.resource_write(stderr, b"err", &ctx).unwrap(), 3);
|
||||
assert_eq!(host.output(), b"out");
|
||||
assert_eq!(host.error(), b"err");
|
||||
assert_eq!(host.take_output(), b"out");
|
||||
assert_eq!(host.take_error(), b"err");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_mode_refuses_stdin_and_stderr() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut host = StdHost::new(SandboxMode::Strict, Capabilities::pure(), dir.path())
|
||||
.with_stdin(b"x".to_vec());
|
||||
let ctx = context();
|
||||
for (descriptor, rights) in [("stdin", "read"), ("stderr", "write")] {
|
||||
let error = host
|
||||
.resource_open("stdio", descriptor, &[rights.to_string()], &ctx)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.category, ErrorCategory::PermissionDenied);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_mode_allows_only_the_existing_stdio_console_channel() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -430,14 +430,20 @@ pub(crate) fn dispatch(
|
||||
)?;
|
||||
let available = interpreter.memory().byte_len(memory, span)?;
|
||||
let start = non_negative_usize(&offset_value, "resource.read offset", span)?;
|
||||
if start >= available {
|
||||
if start > available {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::IndexOutOfBounds,
|
||||
span,
|
||||
format!("resource.read offset {start} is at or beyond the Memory length {available}"),
|
||||
format!("resource.read offset {start} is beyond the Memory length {available}"),
|
||||
));
|
||||
}
|
||||
let limit = max_bytes.min(available - start);
|
||||
// Offset == length is a valid EOF probe. Ask for one byte so a
|
||||
// provider can report EOF without a zero-length read ambiguity.
|
||||
let limit = if start == available {
|
||||
1
|
||||
} else {
|
||||
max_bytes.min(available - start)
|
||||
};
|
||||
let result = interpreter.resource_read(handle, limit, &module, span);
|
||||
match result {
|
||||
Ok(read) => {
|
||||
|
||||
Reference in new issue
Block a user