Implements issue #3 (RFC-EXT-0003) slices on the post-3.0.0-beta.1 tree. PR-00/01/H-06: reproduce and fix sibling heading mode inheritance. build_units now pops the current and same-or-higher-level headings from the ancestor stack before deriving the inherited mode, so an unmarked heading can never inherit the previous sibling. Covered by the units matrix, conformance fixtures 22/23, and black-box heading_inheritance through check --emit/run/call. PR-02/H-08: the deterministic release archive now ships the complete verified stdlib/ tree. package.py and smoke.py recompute the runtime tree/alias digests, record them in RELEASE_INFO.json.stdlib, and fail on mismatch; smoke removes the bundle and requires a fail-closed run. Release discovery no longer falls back to the compile-time source tree (debug builds keep that development convenience). PR-03/H-04: one marqdo_host::CORE_RESOURCE_KINDS registry is used by both the CLI provider-manifest validator and every trusted host registration path; all six core kinds are rejected as provider name or declared kind before registration. H-03: docs/architecture/resource-operations.md records every reviewed resource_request operation and architecture_boundaries pins the exact allowlist; new domain operations are forbidden in core (ADR-0062). PR-04/H-01: remove math.parse/eval/substitute/simplify/differentiate from BUILTINS, HostOp, the runtime, the host kernel and the workspace (crates/marqdo-math deleted). Formula stays inert; Int/Float arithmetic stays. Math algorithms are a pure examples/math-library package plus an optional examples/math-backend ABI plugin that requests the math.compute capability (ADR-0061). Removed calls are static symbol.unknown; the math import namespace is released again. Tests/examples/specs migrated; new math_library black-box test. PR-05/06 Step A: ADR-0060 classifies @exec/@knowledge as the closed Core Modes and specifies the Proposed metadata-only Heading Annotations v1 (target 3.1, sem.invalid_mode_marker unchanged, Analysis v2 unchanged). ADR-0063 records the foreign F-A retention and deferred F-B removal. Docs/status/compatibility/changelog and the RFC-EXT-0003 evidence file are updated. Full local gate: cargo fmt PASS, clippy -D warnings PASS, workspace tests 586 passed/0 failed/2 ignored, python tools/ci 28 passed.
943 lines
30 KiB
Rust
943 lines
30 KiB
Rust
//! Host-owned resource/stream table (ADR-0025).
|
|
//!
|
|
//! 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};
|
|
use std::net::{TcpListener, TcpStream, UdpSocket};
|
|
|
|
use rustls::{ClientConnection, StreamOwned};
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use marqdo_span::Span;
|
|
use marqdo_value::{ErrorCategory, ResourceHandle, RuntimeError};
|
|
|
|
static NEXT_RUN_ID: AtomicU64 = AtomicU64::new(1);
|
|
|
|
/// Core resource kinds owned by the host itself.
|
|
///
|
|
/// A third-party [`ResourceProvider`] may neither use one of these names as its
|
|
/// provider name nor declare one of them as a served kind: the host dispatches
|
|
/// these kinds before consulting the provider table. This is the single source of
|
|
/// truth used by both host registration and the CLI provider-manifest validator
|
|
/// ([`spec/resource-provider-v2.md`], ADR-0033/0062).
|
|
pub const CORE_RESOURCE_KINDS: &[&str] = &["stdio", "file", "tcp", "tls", "udp", "tcp-listener"];
|
|
|
|
/// Whether `name` is a host-owned core resource kind.
|
|
pub fn is_core_resource_kind(name: &str) -> bool {
|
|
CORE_RESOURCE_KINDS.contains(&name)
|
|
}
|
|
|
|
/// A versioned trusted provider of additional resource kinds (ADR-0033).
|
|
///
|
|
/// In-process providers share the host address space and are **not** a
|
|
/// sandbox; only explicitly trusted code may be registered.
|
|
pub trait ResourceProvider: Send {
|
|
/// Stable grant name.
|
|
fn name(&self) -> &str;
|
|
/// Protocol version; must be 1 in this build.
|
|
fn protocol_version(&self) -> u32 {
|
|
1
|
|
}
|
|
/// Resource kinds served by this provider.
|
|
fn kinds(&self) -> &[&str];
|
|
|
|
/// Owned kind list used by registration. Providers with dynamically owned
|
|
/// kind strings override this.
|
|
fn kind_strings(&self) -> Vec<String> {
|
|
self.kinds()
|
|
.iter()
|
|
.map(|kind| (*kind).to_string())
|
|
.collect()
|
|
}
|
|
/// Open one descriptor, returning a provider-private token.
|
|
fn open(
|
|
&mut self,
|
|
kind: &str,
|
|
descriptor: &str,
|
|
rights: &[String],
|
|
span: Option<Span>,
|
|
) -> Result<u64, RuntimeError>;
|
|
/// Read up to `max_bytes` from a token.
|
|
fn read(
|
|
&mut self,
|
|
token: u64,
|
|
max_bytes: usize,
|
|
span: Option<Span>,
|
|
) -> Result<ResourceRead, RuntimeError>;
|
|
/// Write bytes to a token; return the accepted count.
|
|
fn write(
|
|
&mut self,
|
|
token: u64,
|
|
bytes: &[u8],
|
|
span: Option<Span>,
|
|
) -> Result<usize, RuntimeError>;
|
|
/// Close a token.
|
|
fn close(&mut self, token: u64, span: Option<Span>) -> Result<(), RuntimeError>;
|
|
}
|
|
|
|
/// Shared provider handle registered on the host.
|
|
pub type SharedResourceProvider = Arc<Mutex<Box<dyn ResourceProvider>>>;
|
|
|
|
/// Bytes returned by one stream read.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct ResourceRead {
|
|
/// Bytes actually read (may be empty at EOF).
|
|
pub bytes: Vec<u8>,
|
|
/// Whether the stream has reached EOF.
|
|
pub eof: bool,
|
|
}
|
|
|
|
struct StdinCursor {
|
|
bytes: Vec<u8>,
|
|
position: usize,
|
|
}
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
enum Stream {
|
|
Stdin {
|
|
cursor: Arc<Mutex<StdinCursor>>,
|
|
closed: bool,
|
|
},
|
|
Stdout {
|
|
closed: bool,
|
|
},
|
|
Stderr {
|
|
closed: bool,
|
|
},
|
|
File {
|
|
relative: String,
|
|
bytes: Vec<u8>,
|
|
position: usize,
|
|
closed: bool,
|
|
},
|
|
FileWriter {
|
|
relative: String,
|
|
bytes: Vec<u8>,
|
|
closed: bool,
|
|
},
|
|
Provider {
|
|
kind: String,
|
|
provider: SharedResourceProvider,
|
|
token: u64,
|
|
closed: bool,
|
|
},
|
|
Tcp {
|
|
stream: TcpStream,
|
|
closed: bool,
|
|
},
|
|
Udp {
|
|
socket: UdpSocket,
|
|
closed: bool,
|
|
},
|
|
TcpListener {
|
|
listener: TcpListener,
|
|
closed: bool,
|
|
},
|
|
Tls {
|
|
stream: StreamOwned<ClientConnection, TcpStream>,
|
|
closed: bool,
|
|
},
|
|
}
|
|
|
|
struct Slot {
|
|
generation: u64,
|
|
stream: Stream,
|
|
}
|
|
|
|
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,
|
|
Stream::Tcp { closed, .. } => *closed,
|
|
Stream::Udp { closed, .. } => *closed,
|
|
Stream::TcpListener { closed, .. } => *closed,
|
|
Stream::Tls { closed, .. } => *closed,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Host resource table for one run.
|
|
pub struct ResourceTable {
|
|
run_id: u64,
|
|
next_resource: u64,
|
|
slots: BTreeMap<u64, Slot>,
|
|
stdin: Arc<Mutex<StdinCursor>>,
|
|
}
|
|
|
|
impl Default for ResourceTable {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl ResourceTable {
|
|
/// Create an empty table with a fresh run identity.
|
|
pub fn new() -> Self {
|
|
Self {
|
|
run_id: NEXT_RUN_ID.fetch_add(1, Ordering::Relaxed),
|
|
next_resource: 1,
|
|
slots: BTreeMap::new(),
|
|
stdin: Arc::new(Mutex::new(StdinCursor {
|
|
bytes: Vec::new(),
|
|
position: 0,
|
|
})),
|
|
}
|
|
}
|
|
|
|
/// Number of live (not necessarily open) slots.
|
|
pub fn slot_count(&self) -> usize {
|
|
self.slots.len()
|
|
}
|
|
|
|
/// Register the pre-opened stdout stream.
|
|
pub fn open_stdout(&mut self) -> ResourceHandle {
|
|
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 })
|
|
}
|
|
|
|
/// Set the run-scoped stdin bytes at assembly time.
|
|
///
|
|
/// All `stdio/stdin` handles share this cursor so repeated console reads
|
|
/// continue through the input instead of restarting at byte zero.
|
|
pub fn set_stdin(&mut self, bytes: Vec<u8>) {
|
|
self.stdin = Arc::new(Mutex::new(StdinCursor { bytes, position: 0 }));
|
|
}
|
|
|
|
/// Register a read-only handle over the run-scoped stdin cursor.
|
|
pub fn open_stdin(&mut self) -> ResourceHandle {
|
|
self.insert(Stream::Stdin {
|
|
cursor: Arc::clone(&self.stdin),
|
|
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 { .. }
|
|
| Stream::Udp { .. }
|
|
| Stream::TcpListener { .. }
|
|
| Stream::Tls { .. } => 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 {
|
|
relative: relative.into(),
|
|
bytes,
|
|
position: 0,
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Register an empty buffered project-file write stream.
|
|
pub fn open_file_writer(&mut self, relative: impl Into<String>) -> ResourceHandle {
|
|
self.insert(Stream::FileWriter {
|
|
relative: relative.into(),
|
|
bytes: Vec::new(),
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Register a connected TCP stream.
|
|
pub fn open_tcp(&mut self, stream: TcpStream) -> ResourceHandle {
|
|
self.insert(Stream::Tcp {
|
|
stream,
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Register a connected UDP socket.
|
|
pub fn open_udp(&mut self, socket: UdpSocket) -> ResourceHandle {
|
|
self.insert(Stream::Udp {
|
|
socket,
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Register a bound TCP listener.
|
|
pub fn open_tcp_listener(&mut self, listener: TcpListener) -> ResourceHandle {
|
|
self.insert(Stream::TcpListener {
|
|
listener,
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Register a TLS client stream over a TCP socket.
|
|
pub fn open_tls(&mut self, stream: StreamOwned<ClientConnection, TcpStream>) -> ResourceHandle {
|
|
self.insert(Stream::Tls {
|
|
stream,
|
|
closed: false,
|
|
})
|
|
}
|
|
|
|
/// Accept one inbound TCP connection from a listener, with a 5 s deadline.
|
|
pub fn accept(
|
|
&mut self,
|
|
handle: ResourceHandle,
|
|
span: Option<Span>,
|
|
) -> Result<ResourceHandle, RuntimeError> {
|
|
let slot = self.slot_mut(handle, span)?;
|
|
if slot.closed() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceClosed,
|
|
span,
|
|
"Resource handle is closed",
|
|
));
|
|
}
|
|
let Stream::TcpListener { listener, .. } = &mut slot.stream else {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceUnsupported,
|
|
span,
|
|
"resource.accept requires a tcp-listener handle",
|
|
));
|
|
};
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
|
loop {
|
|
match listener.accept() {
|
|
Ok((stream, _)) => {
|
|
let _ = stream.set_read_timeout(Some(std::time::Duration::from_secs(5)));
|
|
let _ = stream.set_write_timeout(Some(std::time::Duration::from_secs(5)));
|
|
return Ok(self.insert(Stream::Tcp {
|
|
stream,
|
|
closed: false,
|
|
}));
|
|
}
|
|
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
|
|
if std::time::Instant::now() >= deadline {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
"tcp-listener accept timed out after 5 seconds",
|
|
));
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
}
|
|
Err(error) => {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("tcp-listener accept failed: {error}"),
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Open a provider token and wrap it in a host-issued handle.
|
|
pub fn open_provider(
|
|
&mut self,
|
|
provider: SharedResourceProvider,
|
|
kind: &str,
|
|
descriptor: &str,
|
|
rights: &[String],
|
|
span: Option<Span>,
|
|
) -> Result<ResourceHandle, RuntimeError> {
|
|
let token = {
|
|
let mut provider = provider.lock().map_err(|_| provider_lock_error(span))?;
|
|
provider.open(kind, descriptor, rights, span)?
|
|
};
|
|
Ok(self.insert(Stream::Provider {
|
|
kind: kind.to_string(),
|
|
provider,
|
|
token,
|
|
closed: false,
|
|
}))
|
|
}
|
|
|
|
fn insert(&mut self, stream: Stream) -> ResourceHandle {
|
|
let id = self.next_resource;
|
|
self.next_resource += 1;
|
|
self.slots.insert(
|
|
id,
|
|
Slot {
|
|
generation: 1,
|
|
stream,
|
|
},
|
|
);
|
|
ResourceHandle::from_host(self.run_id, id, 1)
|
|
}
|
|
|
|
fn slot(&self, handle: ResourceHandle, span: Option<Span>) -> Result<&Slot, RuntimeError> {
|
|
if handle.run_id() != self.run_id {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
"Resource handle belongs to a different host run",
|
|
));
|
|
}
|
|
let Some(slot) = self.slots.get(&handle.resource_id()) else {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
"unknown Resource handle",
|
|
));
|
|
};
|
|
if slot.generation != handle.generation() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
"stale Resource handle",
|
|
));
|
|
}
|
|
Ok(slot)
|
|
}
|
|
|
|
fn slot_mut(
|
|
&mut self,
|
|
handle: ResourceHandle,
|
|
span: Option<Span>,
|
|
) -> Result<&mut Slot, RuntimeError> {
|
|
self.slot(handle, span)?;
|
|
self.slots
|
|
.get_mut(&handle.resource_id())
|
|
.ok_or_else(|| unknown_handle(span))
|
|
}
|
|
|
|
/// Read up to `max_bytes` from a file stream.
|
|
///
|
|
/// Reads at EOF return `count=0, eof=true`. A read on a closed stream is
|
|
/// `resource.closed`; stdout is not readable.
|
|
pub fn read(
|
|
&mut self,
|
|
handle: ResourceHandle,
|
|
max_bytes: usize,
|
|
span: Option<Span>,
|
|
) -> Result<ResourceRead, RuntimeError> {
|
|
let slot = self.slot_mut(handle, span)?;
|
|
if slot.closed() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceClosed,
|
|
span,
|
|
"Resource handle is closed",
|
|
));
|
|
}
|
|
match &mut slot.stream {
|
|
Stream::Stdin { cursor, .. } => {
|
|
let mut cursor = cursor.lock().map_err(|_| stdin_lock_error(span))?;
|
|
let start = cursor.position;
|
|
let end = start.saturating_add(max_bytes).min(cursor.bytes.len());
|
|
let chunk = cursor.bytes[start..end].to_vec();
|
|
cursor.position = end;
|
|
Ok(ResourceRead {
|
|
bytes: chunk,
|
|
eof: end >= cursor.bytes.len(),
|
|
})
|
|
}
|
|
Stream::File {
|
|
bytes, position, ..
|
|
} => {
|
|
let start = *position;
|
|
let end = start.saturating_add(max_bytes).min(bytes.len());
|
|
let chunk = bytes[start..end].to_vec();
|
|
*position = end;
|
|
Ok(ResourceRead {
|
|
bytes: chunk,
|
|
eof: end >= bytes.len(),
|
|
})
|
|
}
|
|
Stream::Stdout { .. } => Err(RuntimeError::new(
|
|
ErrorCategory::ResourceUnsupported,
|
|
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,
|
|
"a file write stream does not support resource.read",
|
|
)),
|
|
Stream::Provider {
|
|
provider, token, ..
|
|
} => {
|
|
let mut provider = provider.lock().map_err(|_| provider_lock_error(span))?;
|
|
let read = provider.read(*token, max_bytes, span)?;
|
|
if read.bytes.len() > max_bytes {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
format!(
|
|
"resource provider returned {} bytes for a {max_bytes}-byte read",
|
|
read.bytes.len()
|
|
),
|
|
));
|
|
}
|
|
Ok(read)
|
|
}
|
|
Stream::Tcp { stream, .. } => {
|
|
let mut buffer = vec![0u8; max_bytes];
|
|
let count = stream.read(&mut buffer).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("tcp read failed: {error}"),
|
|
)
|
|
})?;
|
|
buffer.truncate(count);
|
|
Ok(ResourceRead {
|
|
bytes: buffer,
|
|
eof: count == 0,
|
|
})
|
|
}
|
|
Stream::TcpListener { .. } => Err(RuntimeError::new(
|
|
ErrorCategory::ResourceUnsupported,
|
|
span,
|
|
"a tcp-listener does not support resource.read; use resource.accept",
|
|
)),
|
|
Stream::Tls { stream, .. } => {
|
|
let mut buffer = vec![0u8; max_bytes];
|
|
let count = stream.read(&mut buffer).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("tls read failed: {error}"),
|
|
)
|
|
})?;
|
|
buffer.truncate(count);
|
|
Ok(ResourceRead {
|
|
bytes: buffer,
|
|
eof: count == 0,
|
|
})
|
|
}
|
|
Stream::Udp { socket, .. } => {
|
|
let mut buffer = vec![0u8; max_bytes];
|
|
let count = socket.recv(&mut buffer).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("udp read failed: {error}"),
|
|
)
|
|
})?;
|
|
buffer.truncate(count);
|
|
Ok(ResourceRead {
|
|
bytes: buffer,
|
|
eof: false,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Validate a stdout write and return the accepted byte count.
|
|
///
|
|
/// The caller performs the output-budget accounting and buffer append; this
|
|
/// method only checks handle identity, closed state and stream kind.
|
|
pub fn write(
|
|
&mut self,
|
|
handle: ResourceHandle,
|
|
bytes: &[u8],
|
|
span: Option<Span>,
|
|
) -> Result<usize, RuntimeError> {
|
|
let slot = self.slot_mut(handle, span)?;
|
|
if slot.closed() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceClosed,
|
|
span,
|
|
"Resource handle is closed",
|
|
));
|
|
}
|
|
match &mut slot.stream {
|
|
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,
|
|
format!("project file `{relative}` was opened read-only"),
|
|
)),
|
|
Stream::FileWriter {
|
|
relative,
|
|
bytes: buffer,
|
|
..
|
|
} => {
|
|
let next = buffer.len().checked_add(bytes.len());
|
|
let Some(next) = next else {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceExhausted,
|
|
span,
|
|
format!("file write buffer for `{relative}` overflows"),
|
|
));
|
|
};
|
|
if next > crate::fs::MAX_READ_BYTES as usize {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceExhausted,
|
|
span,
|
|
format!(
|
|
"file write buffer for `{relative}` exceeds the {} byte limit",
|
|
crate::fs::MAX_READ_BYTES
|
|
),
|
|
));
|
|
}
|
|
buffer.extend_from_slice(bytes);
|
|
Ok(bytes.len())
|
|
}
|
|
Stream::Provider {
|
|
provider, token, ..
|
|
} => {
|
|
let mut provider = provider.lock().map_err(|_| provider_lock_error(span))?;
|
|
let count = provider.write(*token, bytes, span)?;
|
|
if count > bytes.len() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
format!(
|
|
"resource provider reported {count} bytes written for a {}-byte request",
|
|
bytes.len()
|
|
),
|
|
));
|
|
}
|
|
Ok(count)
|
|
}
|
|
Stream::Tcp { stream, .. } => stream.write(bytes).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("tcp write failed: {error}"),
|
|
)
|
|
}),
|
|
Stream::TcpListener { .. } => Err(RuntimeError::new(
|
|
ErrorCategory::ResourceUnsupported,
|
|
span,
|
|
"a tcp-listener does not support resource.write; use resource.accept",
|
|
)),
|
|
Stream::Tls { stream, .. } => stream.write(bytes).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("tls write failed: {error}"),
|
|
)
|
|
}),
|
|
Stream::Udp { socket, .. } => socket.send(bytes).map_err(|error| {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
format!("udp write failed: {error}"),
|
|
)
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Close a stream. A second close is `resource.closed`.
|
|
pub fn close(
|
|
&mut self,
|
|
handle: ResourceHandle,
|
|
span: Option<Span>,
|
|
) -> Result<(), RuntimeError> {
|
|
let slot = self.slot_mut(handle, span)?;
|
|
if slot.closed() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceClosed,
|
|
span,
|
|
"Resource handle is already closed",
|
|
));
|
|
}
|
|
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 {
|
|
provider,
|
|
token,
|
|
closed,
|
|
..
|
|
} => {
|
|
let mut provider = provider.lock().map_err(|_| provider_lock_error(span))?;
|
|
provider.close(*token, span)?;
|
|
*closed = true;
|
|
}
|
|
Stream::Tcp { stream, closed } => {
|
|
let _ = stream.shutdown(std::net::Shutdown::Both);
|
|
*closed = true;
|
|
}
|
|
Stream::Udp { closed, .. } => *closed = true,
|
|
Stream::TcpListener { closed, .. } => *closed = true,
|
|
Stream::Tls { closed, .. } => *closed = true,
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Return the buffered bytes of a file write stream without closing it.
|
|
///
|
|
/// `None` means the handle is not a file writer (stdout or a reader). The
|
|
/// caller must call [`Self::close`] only after the flush succeeds, so a
|
|
/// failed flush leaves the writer open with its buffer intact.
|
|
pub fn pending_file_write(
|
|
&self,
|
|
handle: ResourceHandle,
|
|
span: Option<Span>,
|
|
) -> Result<Option<(String, Vec<u8>)>, RuntimeError> {
|
|
let slot = self.slot(handle, span)?;
|
|
if slot.closed() {
|
|
return Err(RuntimeError::new(
|
|
ErrorCategory::ResourceClosed,
|
|
span,
|
|
"Resource handle is closed",
|
|
));
|
|
}
|
|
match &slot.stream {
|
|
Stream::FileWriter {
|
|
relative, bytes, ..
|
|
} => Ok(Some((relative.clone(), bytes.clone()))),
|
|
_ => Ok(None),
|
|
}
|
|
}
|
|
|
|
/// Descriptor kind for diagnostics/trace, if the handle is valid.
|
|
pub fn kind(&self, handle: ResourceHandle, span: Option<Span>) -> Result<String, RuntimeError> {
|
|
let slot = self.slot(handle, span)?;
|
|
Ok(match &slot.stream {
|
|
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(),
|
|
Stream::Udp { .. } => "udp".to_string(),
|
|
Stream::TcpListener { .. } => "tcp-listener".to_string(),
|
|
Stream::Tls { .. } => "tls".to_string(),
|
|
})
|
|
}
|
|
}
|
|
|
|
fn provider_lock_error(span: Option<Span>) -> RuntimeError {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
"resource provider lock is poisoned",
|
|
)
|
|
}
|
|
|
|
fn stdin_lock_error(span: Option<Span>) -> RuntimeError {
|
|
RuntimeError::new(
|
|
ErrorCategory::HostError,
|
|
span,
|
|
"stdin cursor lock is poisoned",
|
|
)
|
|
}
|
|
|
|
fn unknown_handle(span: Option<Span>) -> RuntimeError {
|
|
RuntimeError::new(
|
|
ErrorCategory::ResourceFault,
|
|
span,
|
|
"unknown Resource handle",
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn file_reads_are_chunked_with_eof() {
|
|
let mut table = ResourceTable::new();
|
|
let handle = table.open_file("data.txt", b"abcdef".to_vec());
|
|
assert_eq!(
|
|
table.read(handle, 2, None).unwrap(),
|
|
ResourceRead {
|
|
bytes: b"ab".to_vec(),
|
|
eof: false
|
|
}
|
|
);
|
|
assert_eq!(
|
|
table.read(handle, 8, None).unwrap(),
|
|
ResourceRead {
|
|
bytes: b"cdef".to_vec(),
|
|
eof: true
|
|
}
|
|
);
|
|
assert_eq!(
|
|
table.read(handle, 1, None).unwrap(),
|
|
ResourceRead {
|
|
bytes: Vec::new(),
|
|
eof: true
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn forged_and_closed_handles_are_rejected() {
|
|
let mut table = ResourceTable::new();
|
|
let handle = table.open_stdout();
|
|
let forged = ResourceHandle::from_host(handle.run_id() + 1, handle.resource_id(), 1);
|
|
assert_eq!(
|
|
table.write(forged, b"x", None).unwrap_err().code(),
|
|
"runtime.resource"
|
|
);
|
|
table.close(handle, None).unwrap();
|
|
assert_eq!(
|
|
table.write(handle, b"x", None).unwrap_err().code(),
|
|
"resource.closed"
|
|
);
|
|
assert_eq!(
|
|
table.close(handle, None).unwrap_err().code(),
|
|
"resource.closed"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn stdout_is_not_readable_and_file_is_not_writable() {
|
|
let mut table = ResourceTable::new();
|
|
let stdout = table.open_stdout();
|
|
assert_eq!(
|
|
table.read(stdout, 1, None).unwrap_err().code(),
|
|
"resource.unsupported"
|
|
);
|
|
let file = table.open_file("x.txt", Vec::new());
|
|
assert_eq!(
|
|
table.write(file, b"x", None).unwrap_err().code(),
|
|
"resource.unsupported"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn stdin_reads_are_chunked_and_end_at_eof() {
|
|
let mut table = ResourceTable::new();
|
|
table.set_stdin(b"ab".to_vec());
|
|
let stdin = table.open_stdin();
|
|
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"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod provider_tests {
|
|
use super::*;
|
|
|
|
struct OverReporting;
|
|
|
|
impl ResourceProvider for OverReporting {
|
|
fn name(&self) -> &str {
|
|
"over-reporting"
|
|
}
|
|
fn kinds(&self) -> &[&str] {
|
|
&["bad"]
|
|
}
|
|
fn open(
|
|
&mut self,
|
|
_kind: &str,
|
|
_descriptor: &str,
|
|
_rights: &[String],
|
|
_span: Option<Span>,
|
|
) -> Result<u64, RuntimeError> {
|
|
Ok(1)
|
|
}
|
|
fn read(
|
|
&mut self,
|
|
_token: u64,
|
|
max_bytes: usize,
|
|
_span: Option<Span>,
|
|
) -> Result<ResourceRead, RuntimeError> {
|
|
Ok(ResourceRead {
|
|
bytes: vec![0; max_bytes + 1],
|
|
eof: false,
|
|
})
|
|
}
|
|
fn write(
|
|
&mut self,
|
|
_token: u64,
|
|
bytes: &[u8],
|
|
_span: Option<Span>,
|
|
) -> Result<usize, RuntimeError> {
|
|
Ok(bytes.len() + 1)
|
|
}
|
|
fn close(&mut self, _token: u64, _span: Option<Span>) -> Result<(), RuntimeError> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn provider_counts_are_bounded_by_the_request() {
|
|
let mut table = ResourceTable::new();
|
|
let provider: SharedResourceProvider = Arc::new(Mutex::new(Box::new(OverReporting)));
|
|
let handle = table
|
|
.open_provider(provider, "bad", "descriptor", &["read".to_string()], None)
|
|
.unwrap();
|
|
assert_eq!(
|
|
table.read(handle, 2, None).unwrap_err().code(),
|
|
"runtime.resource"
|
|
);
|
|
assert_eq!(
|
|
table.write(handle, b"x", None).unwrap_err().code(),
|
|
"runtime.resource"
|
|
);
|
|
}
|
|
}
|