feat(m0.6-r2): isolated plugin worker transport
- add a hidden __plugin-worker mode that loads one native library in a child process and serves length-prefixed JSON invoke frames - add host WorkerPlugin with ready handshake, export cross-check, per-call timeout and crash/EOF propagation - route kind: worker without in-process trust; native still requires explicit trust; wasm is rejected explicitly - report worker_processes=true while keeping strict OS isolation unavailable - test successful worker calls and worker crash isolation - update plugin ABI spec and R2 status
This commit is contained in:
1 parent
4ce37afb39
commit
44c2df2361
12 files changed
+661
-47
No files matched your search
@@ -883,9 +883,9 @@ fn load_plugin_into_host(
|
||||
manifest
|
||||
.validate()
|
||||
.map_err(|error| format!("{}: {}", error.code, error.message))?;
|
||||
if !trusted {
|
||||
if manifest.plugin.kind == marqdo_plugin_abi::PluginKind::Native && !trusted {
|
||||
return Err(format!(
|
||||
"loading `{}` in-process requires --trust-native-plugin: no isolated worker transport is implemented in this build",
|
||||
"loading `{}` in-process requires --trust-native-plugin; use `kind: worker` for crash isolation",
|
||||
manifest.plugin.name
|
||||
));
|
||||
}
|
||||
|
||||
@@ -58,6 +58,17 @@ enum Command {
|
||||
/// Unknown top-level command: dispatch to a registered external tool.
|
||||
#[command(external_subcommand)]
|
||||
External(Vec<OsString>),
|
||||
/// Internal isolated plugin worker (not a user-facing command).
|
||||
#[command(name = "__plugin-worker", hide = true)]
|
||||
PluginWorker(PluginWorkerArgs),
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
struct PluginWorkerArgs {
|
||||
/// plugin.yaml path.
|
||||
manifest: PathBuf,
|
||||
/// Plugin package directory.
|
||||
base_dir: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
@@ -386,6 +397,9 @@ fn main() {
|
||||
ToolCommand::Remove(args) => tool::remove(&args.command, args.json),
|
||||
},
|
||||
Command::External(args) => tool::dispatch(&args),
|
||||
Command::PluginWorker(args) => {
|
||||
marqdo_plugin_abi::worker::serve(&args.manifest, &args.base_dir)
|
||||
}
|
||||
};
|
||||
std::process::exit(code);
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
//! Isolated plugin worker transport (ADR-0010).
|
||||
#![cfg(unix)]
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Output};
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use marqdo_plugin_abi::{sha256_file, HostTarget};
|
||||
|
||||
static COUNTER: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new(tag: &str) -> Self {
|
||||
let id = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"marqdo-worker-test-{}-{id}-{tag}",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn repo() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn have_cc() -> bool {
|
||||
Command::new("cc")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|output| output.status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn compile(dir: &Path, extra: &[&str]) -> PathBuf {
|
||||
let output = dir.join("solver.so");
|
||||
let status = Command::new("cc")
|
||||
.args(["-std=c11", "-O2", "-shared", "-fPIC"])
|
||||
.arg("-I")
|
||||
.arg(repo().join("include"))
|
||||
.args(extra)
|
||||
.arg(repo().join("examples/plugin-demo/solver.c"))
|
||||
.arg("-o")
|
||||
.arg(&output)
|
||||
.status()
|
||||
.expect("cc");
|
||||
assert!(status.success(), "cc failed");
|
||||
output
|
||||
}
|
||||
|
||||
fn write_manifest(dir: &Path, sha: &str) {
|
||||
std::fs::write(
|
||||
dir.join("plugin.yaml"),
|
||||
format!(
|
||||
r#"plugin:
|
||||
name: demo.solver
|
||||
version: 0.1.0
|
||||
abi: "^1.0"
|
||||
kind: worker
|
||||
exports:
|
||||
- name: solver.add
|
||||
signature: "($a: Int, $b: Int) -> Int"
|
||||
requires:
|
||||
- math.compute
|
||||
artifacts:
|
||||
- target: {}
|
||||
file: solver.so
|
||||
sha256: "{}"
|
||||
"#,
|
||||
HostTarget::current().triple(),
|
||||
sha
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
dir.join("doc.mq.md"),
|
||||
"## main\n\n**结果 = solver.add(a=20, b=22)**\n\n> 打印(结果)\n",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn run(dir: &Path) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args([
|
||||
"run",
|
||||
dir.join("doc.mq.md").to_str().unwrap(),
|
||||
"--plugin",
|
||||
dir.to_str().unwrap(),
|
||||
"--allow-plugin",
|
||||
"demo.solver",
|
||||
])
|
||||
.output()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_transport_runs_without_in_process_trust() {
|
||||
if !have_cc() {
|
||||
eprintln!("skipping: cc is unavailable");
|
||||
return;
|
||||
}
|
||||
let dir = TempDir::new("ok");
|
||||
let artifact = compile(dir.path(), &[]);
|
||||
write_manifest(dir.path(), &sha256_file(&artifact).unwrap());
|
||||
let output = run(dir.path());
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(String::from_utf8_lossy(&output.stdout), "42\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_crash_is_isolated_from_the_host() {
|
||||
if !have_cc() {
|
||||
eprintln!("skipping: cc is unavailable");
|
||||
return;
|
||||
}
|
||||
let dir = TempDir::new("crash");
|
||||
let artifact = compile(dir.path(), &["-DMQ_SOLVER_CRASH"]);
|
||||
write_manifest(dir.path(), &sha256_file(&artifact).unwrap());
|
||||
let output = run(dir.path());
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(
|
||||
stderr.contains("plugin.crashed") || stderr.contains("plugin.failure"),
|
||||
"{stderr}"
|
||||
);
|
||||
// The host process survived and still runs a native Marqdo program.
|
||||
let survivor = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
||||
.args(["run", "examples/hello.mq.md"])
|
||||
.current_dir(repo())
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(survivor.status.code(), Some(0));
|
||||
}
|
||||
@@ -8,7 +8,9 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use marqdo_plugin_abi::{ErrorKind as PluginErrorKind, LoadedPlugin, PluginError, PluginManifest};
|
||||
use marqdo_plugin_abi::{
|
||||
ErrorKind as PluginErrorKind, LoadedPlugin, PluginError, PluginKind, PluginManifest,
|
||||
};
|
||||
use marqdo_span::Span;
|
||||
use marqdo_value::{ErrorCategory, RuntimeError, Value};
|
||||
|
||||
@@ -17,6 +19,7 @@ use crate::capability::{Capabilities, CapabilityAudit, CapabilityBroker};
|
||||
use crate::context::ExecutionContext;
|
||||
use crate::fs::ProjectFs;
|
||||
use crate::handles::{contains_plugin_handle, PluginHandleTable};
|
||||
use crate::plugin_worker::WorkerPlugin;
|
||||
use crate::trace::{EffectTrace, SemanticEvent, TraceEntry};
|
||||
use marqdo_sandbox::{PlatformCapabilities, SandboxMode};
|
||||
|
||||
@@ -150,7 +153,24 @@ struct PluginSlot {
|
||||
manifest_digest: String,
|
||||
/// Complete export name -> declared signature.
|
||||
exports: BTreeMap<String, String>,
|
||||
plugin: LoadedPlugin,
|
||||
plugin: PluginTransport,
|
||||
}
|
||||
|
||||
enum PluginTransport {
|
||||
Native(LoadedPlugin),
|
||||
Worker(WorkerPlugin),
|
||||
}
|
||||
|
||||
impl PluginTransport {
|
||||
fn invoke(
|
||||
&mut self,
|
||||
request: &marqdo_plugin_abi::WireRequest,
|
||||
) -> Result<marqdo_plugin_abi::WireResponse, PluginError> {
|
||||
match self {
|
||||
PluginTransport::Native(plugin) => plugin.invoke(request),
|
||||
PluginTransport::Worker(plugin) => plugin.invoke(request),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdHost {
|
||||
@@ -199,18 +219,6 @@ impl StdHost {
|
||||
base_dir: &std::path::Path,
|
||||
trusted: bool,
|
||||
) -> Result<Vec<String>, RuntimeError> {
|
||||
if !trusted {
|
||||
let mut error = RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
None,
|
||||
"loading an in-process native plugin requires explicit trust",
|
||||
);
|
||||
error.effect_trace.push(
|
||||
"hint: no isolated plugin worker transport is implemented in this build; in-process native code shares the host address space"
|
||||
.to_string(),
|
||||
);
|
||||
return Err(error);
|
||||
}
|
||||
self.capabilities
|
||||
.check_plugin(&manifest.plugin.name, None)?;
|
||||
manifest.validate().map_err(map_plugin_error)?;
|
||||
@@ -239,26 +247,56 @@ impl StdHost {
|
||||
.resolve_artifact(&marqdo_plugin_abi::HostTarget::current())
|
||||
.map_err(map_plugin_error)?;
|
||||
let manifest_digest = artifact.sha256.clone();
|
||||
let mut broker =
|
||||
CapabilityBroker::with_audit(self.capabilities.clone(), Arc::clone(&self.broker_audit));
|
||||
for capability in &self.vendor_capabilities {
|
||||
broker.allow_vendor(capability.clone());
|
||||
}
|
||||
let package = manifest.plugin.name.clone();
|
||||
let options = marqdo_plugin_abi::LoadOptions {
|
||||
host_target: marqdo_plugin_abi::HostTarget::current(),
|
||||
capability_handler: Some(Box::new(move |capability, request| {
|
||||
broker.authorize(&package, capability, request)
|
||||
})),
|
||||
let transport = match manifest.plugin.kind {
|
||||
PluginKind::Native => {
|
||||
if !trusted {
|
||||
let mut error = RuntimeError::new(
|
||||
ErrorCategory::PermissionDenied,
|
||||
None,
|
||||
"loading an in-process native plugin requires explicit trust",
|
||||
);
|
||||
error.effect_trace.push(
|
||||
"hint: use `kind: worker` for crash isolation, or pass --trust-native-plugin for in-process native code"
|
||||
.to_string(),
|
||||
);
|
||||
return Err(error);
|
||||
}
|
||||
let mut broker = CapabilityBroker::with_audit(
|
||||
self.capabilities.clone(),
|
||||
Arc::clone(&self.broker_audit),
|
||||
);
|
||||
for capability in &self.vendor_capabilities {
|
||||
broker.allow_vendor(capability.clone());
|
||||
}
|
||||
let package = manifest.plugin.name.clone();
|
||||
let options = marqdo_plugin_abi::LoadOptions {
|
||||
host_target: marqdo_plugin_abi::HostTarget::current(),
|
||||
capability_handler: Some(Box::new(move |capability, request| {
|
||||
broker.authorize(&package, capability, request)
|
||||
})),
|
||||
};
|
||||
PluginTransport::Native(
|
||||
LoadedPlugin::open_with(manifest, base_dir, options)
|
||||
.map_err(map_plugin_error)?,
|
||||
)
|
||||
}
|
||||
PluginKind::Worker => PluginTransport::Worker(
|
||||
WorkerPlugin::spawn(manifest, base_dir).map_err(map_plugin_error)?,
|
||||
),
|
||||
PluginKind::Wasm => {
|
||||
return Err(RuntimeError::new(
|
||||
ErrorCategory::KernelUnavailable,
|
||||
None,
|
||||
"plugin kind `wasm` is not supported in this build",
|
||||
))
|
||||
}
|
||||
};
|
||||
let loaded =
|
||||
LoadedPlugin::open_with(manifest, base_dir, options).map_err(map_plugin_error)?;
|
||||
let index = self.plugins.len();
|
||||
self.plugins.push(PluginSlot {
|
||||
package: manifest.plugin.name.clone(),
|
||||
manifest_digest,
|
||||
exports: exports.clone(),
|
||||
plugin: loaded,
|
||||
plugin: transport,
|
||||
});
|
||||
let names: Vec<String> = exports.keys().cloned().collect();
|
||||
for name in &names {
|
||||
|
||||
@@ -11,6 +11,7 @@ pub mod fs;
|
||||
pub mod handles;
|
||||
pub mod host;
|
||||
pub mod kernels;
|
||||
mod plugin_worker;
|
||||
pub mod trace;
|
||||
|
||||
pub use budget::{Budget, DEFAULT_FUEL, DEFAULT_OUTPUT_BYTES, DEFAULT_STACK_LIMIT};
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
//! Host side of the isolated plugin worker transport (ADR-0010).
|
||||
//!
|
||||
//! The worker is a child process running the same core binary with the hidden
|
||||
//! `__plugin-worker` command. It isolates crashes and keeps native code out of
|
||||
//! the interpreter process. A plain process is **not** an OS sandbox; strict
|
||||
//! mode still refuses without a real isolation adapter.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::process::{Child, ChildStdin, Command, Stdio};
|
||||
use std::sync::mpsc::{self, Receiver};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use marqdo_plugin_abi::worker::{read_frame, write_frame, WORKER_COMMAND};
|
||||
use marqdo_plugin_abi::{PluginError, PluginManifest, WireRequest, WireResponse};
|
||||
use serde_json::json;
|
||||
|
||||
/// A native plugin running in an isolated worker process.
|
||||
pub struct WorkerPlugin {
|
||||
child: Child,
|
||||
stdin: ChildStdin,
|
||||
frames: Receiver<Result<serde_json::Value, PluginError>>,
|
||||
exports: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
const CALL_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
impl std::fmt::Debug for WorkerPlugin {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("WorkerPlugin")
|
||||
.field("pid", &self.child.id())
|
||||
.field("exports", &self.exports.keys().collect::<Vec<_>>())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl WorkerPlugin {
|
||||
/// Spawn a worker for `manifest` and wait for its ready frame.
|
||||
pub fn spawn(manifest: &PluginManifest, base_dir: &Path) -> Result<Self, PluginError> {
|
||||
let manifest_path = if base_dir.join("plugin.yaml").is_file() {
|
||||
base_dir.join("plugin.yaml")
|
||||
} else {
|
||||
return Err(PluginError::load(
|
||||
"worker_manifest_missing",
|
||||
format!("no plugin.yaml in `{}`", base_dir.display()),
|
||||
));
|
||||
};
|
||||
let executable = std::env::current_exe().map_err(|error| {
|
||||
PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
format!("cannot find the current executable: {error}"),
|
||||
)
|
||||
})?;
|
||||
let mut child = Command::new(executable)
|
||||
.arg(WORKER_COMMAND)
|
||||
.arg(&manifest_path)
|
||||
.arg(base_dir)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()
|
||||
.map_err(|error| {
|
||||
PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
format!("cannot start plugin worker: {error}"),
|
||||
)
|
||||
})?;
|
||||
let Some(stdin) = child.stdin.take() else {
|
||||
let _ = child.kill();
|
||||
return Err(PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
"worker stdin unavailable",
|
||||
));
|
||||
};
|
||||
let Some(mut stdout) = child.stdout.take() else {
|
||||
let _ = child.kill();
|
||||
return Err(PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
"worker stdout unavailable",
|
||||
));
|
||||
};
|
||||
let (sender, frames) = mpsc::channel();
|
||||
thread::Builder::new()
|
||||
.name("marqdo-plugin-worker-reader".to_string())
|
||||
.spawn(move || loop {
|
||||
let frame = read_frame(&mut stdout);
|
||||
let failed = frame.is_err();
|
||||
if sender.send(frame).is_err() || failed {
|
||||
break;
|
||||
}
|
||||
})
|
||||
.map_err(|error| {
|
||||
let _ = child.kill();
|
||||
PluginError::load(
|
||||
"worker_spawn_failed",
|
||||
format!("cannot start worker reader: {error}"),
|
||||
)
|
||||
})?;
|
||||
let ready = frames.recv_timeout(HANDSHAKE_TIMEOUT).map_err(|_| {
|
||||
let _ = child.kill();
|
||||
PluginError::invoke("plugin.timeout", "plugin worker handshake timed out")
|
||||
})??;
|
||||
if ready.get("kind").and_then(serde_json::Value::as_str) != Some("ready") {
|
||||
let _ = child.kill();
|
||||
return Err(PluginError::load(
|
||||
"worker_handshake_failed",
|
||||
"worker did not report ready",
|
||||
));
|
||||
}
|
||||
let mut exports = BTreeMap::new();
|
||||
if let Some(entries) = ready.get("exports").and_then(serde_json::Value::as_array) {
|
||||
for entry in entries {
|
||||
let Some(name) = entry.get("name").and_then(serde_json::Value::as_str) else {
|
||||
continue;
|
||||
};
|
||||
let signature = entry
|
||||
.get("signature")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
exports.insert(name.to_string(), signature);
|
||||
}
|
||||
}
|
||||
let declared: std::collections::BTreeSet<&str> = manifest
|
||||
.plugin
|
||||
.exports
|
||||
.iter()
|
||||
.map(|export| export.name.as_str())
|
||||
.collect();
|
||||
let actual: std::collections::BTreeSet<&str> = exports.keys().map(String::as_str).collect();
|
||||
if declared != actual {
|
||||
let _ = child.kill();
|
||||
return Err(PluginError::manifest(
|
||||
"worker_export_mismatch",
|
||||
"worker exports do not match the manifest",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
child,
|
||||
stdin,
|
||||
frames,
|
||||
exports,
|
||||
})
|
||||
}
|
||||
|
||||
/// Invoke one export in the worker.
|
||||
pub fn invoke(&mut self, request: &WireRequest) -> Result<WireResponse, PluginError> {
|
||||
let request_value: serde_json::Value = serde_json::from_slice(&request.to_json_bytes()?)
|
||||
.map_err(|error| PluginError::wire("worker_encode_failed", error.to_string()))?;
|
||||
write_frame(
|
||||
&mut self.stdin,
|
||||
&json!({"kind": "invoke", "request": request_value}),
|
||||
)?;
|
||||
let frame = match self.frames.recv_timeout(CALL_TIMEOUT) {
|
||||
Ok(frame) => frame?,
|
||||
Err(_) => {
|
||||
let _ = self.child.kill();
|
||||
return Err(PluginError::invoke(
|
||||
"plugin.timeout",
|
||||
"plugin worker exceeded its invocation timeout",
|
||||
));
|
||||
}
|
||||
};
|
||||
match frame.get("kind").and_then(serde_json::Value::as_str) {
|
||||
Some("response") => marqdo_plugin_abi::worker::response_from_frame(&frame),
|
||||
Some("error") => Err(PluginError::invoke(
|
||||
frame
|
||||
.get("code")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or("plugin_call_failed"),
|
||||
frame
|
||||
.get("message")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.unwrap_or("worker reported an error"),
|
||||
)),
|
||||
_ => Err(PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
"plugin worker closed or violated the frame protocol",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WorkerPlugin {
|
||||
fn drop(&mut self) {
|
||||
let _ = write_frame(&mut self.stdin, &json!({"kind": "shutdown"}));
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ pub mod error;
|
||||
pub mod loader;
|
||||
pub mod manifest;
|
||||
pub mod wire;
|
||||
pub mod worker;
|
||||
|
||||
pub use abi::result as result_code;
|
||||
pub use abi::{
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
//! Isolated plugin worker transport (length-prefixed JSON frames).
|
||||
//!
|
||||
//! The worker is the same core binary invoked with the hidden
|
||||
//! `__plugin-worker` command. It loads one native plugin in a child process and
|
||||
//! serves invoke frames over stdin/stdout. This isolates crashes and provides a
|
||||
//! process boundary; it is **not** an OS sandbox and does not make arbitrary
|
||||
//! untrusted native code safe.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::error::PluginError;
|
||||
use crate::loader::LoadedPlugin;
|
||||
use crate::manifest::{PluginKind, PluginManifest};
|
||||
use crate::wire::{WireRequest, WireResponse};
|
||||
|
||||
/// Hidden CLI command name used to start a worker.
|
||||
pub const WORKER_COMMAND: &str = "__plugin-worker";
|
||||
/// Maximum frame size accepted by either side.
|
||||
pub const MAX_FRAME_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
/// Read one length-prefixed frame (4-byte big-endian length + JSON).
|
||||
pub fn read_frame<R: Read>(reader: &mut R) -> Result<Value, PluginError> {
|
||||
let mut length = [0_u8; 4];
|
||||
reader.read_exact(&mut length).map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
)
|
||||
})?;
|
||||
let length = u32::from_be_bytes(length) as usize;
|
||||
if length == 0 || length > MAX_FRAME_BYTES {
|
||||
return Err(PluginError::wire(
|
||||
"worker_frame_invalid",
|
||||
format!("worker frame length {length} is invalid"),
|
||||
));
|
||||
}
|
||||
let mut bytes = vec![0_u8; length];
|
||||
reader.read_exact(&mut bytes).map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame read failed: {error}"),
|
||||
)
|
||||
})?;
|
||||
serde_json::from_slice(&bytes).map_err(|error| {
|
||||
PluginError::wire(
|
||||
"worker_frame_invalid",
|
||||
format!("worker frame is not valid JSON: {error}"),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Write one length-prefixed frame.
|
||||
pub fn write_frame<W: Write>(writer: &mut W, value: &Value) -> Result<(), PluginError> {
|
||||
let bytes = serde_json::to_vec(value).map_err(|error| {
|
||||
PluginError::wire(
|
||||
"worker_encode_failed",
|
||||
format!("cannot encode worker frame: {error}"),
|
||||
)
|
||||
})?;
|
||||
if bytes.len() > MAX_FRAME_BYTES {
|
||||
return Err(PluginError::wire(
|
||||
"worker_frame_too_large",
|
||||
format!("worker frame is {} bytes", bytes.len()),
|
||||
));
|
||||
}
|
||||
writer
|
||||
.write_all(&(bytes.len() as u32).to_be_bytes())
|
||||
.and_then(|()| writer.write_all(&bytes))
|
||||
.and_then(|()| writer.flush())
|
||||
.map_err(|error| {
|
||||
PluginError::invoke(
|
||||
"plugin.crashed",
|
||||
format!("worker frame write failed: {error}"),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Run one worker until shutdown/EOF. Returns a process exit code.
|
||||
pub fn serve(manifest_path: &Path, base_dir: &Path) -> i32 {
|
||||
let manifest = match PluginManifest::load(manifest_path) {
|
||||
Ok(manifest) => manifest,
|
||||
Err(error) => {
|
||||
eprintln!("worker: {}: {}", error.code, error.message);
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
// The worker is the isolated transport; the library itself is loaded
|
||||
// natively inside this child process.
|
||||
let mut native_manifest = manifest.clone();
|
||||
native_manifest.plugin.kind = PluginKind::Native;
|
||||
let mut plugin = match LoadedPlugin::open(&native_manifest, base_dir) {
|
||||
Ok(plugin) => plugin,
|
||||
Err(error) => {
|
||||
eprintln!("worker: {}: {}", error.code, error.message);
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
let exports: Vec<Value> = plugin
|
||||
.exports()
|
||||
.map(|export| json!({"name": export.name(), "signature": export.signature()}))
|
||||
.collect();
|
||||
let mut stdin = std::io::stdin();
|
||||
let mut stdout = std::io::stdout();
|
||||
if let Err(error) = write_frame(&mut stdout, &json!({"kind": "ready", "exports": exports})) {
|
||||
eprintln!("worker: {}: {}", error.code, error.message);
|
||||
return 1;
|
||||
}
|
||||
loop {
|
||||
let frame = match read_frame(&mut stdin) {
|
||||
Ok(frame) => frame,
|
||||
Err(_) => return 0,
|
||||
};
|
||||
match frame.get("kind").and_then(Value::as_str) {
|
||||
Some("shutdown") => return 0,
|
||||
Some("invoke") => {
|
||||
let Some(request_value) = frame.get("request") else {
|
||||
let _ = write_frame(
|
||||
&mut stdout,
|
||||
&json!({"kind":"error","code":"worker_request_invalid","message":"missing request"}),
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let request_bytes = match serde_json::to_vec(request_value) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(error) => {
|
||||
let _ = write_frame(
|
||||
&mut stdout,
|
||||
&json!({"kind":"error","code":"worker_request_invalid","message":error.to_string()}),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let request = match WireRequest::from_json_bytes(&request_bytes) {
|
||||
Ok(request) => request,
|
||||
Err(error) => {
|
||||
let _ = write_frame(
|
||||
&mut stdout,
|
||||
&json!({"kind":"error","code":error.code,"message":error.message}),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match plugin.invoke(&request) {
|
||||
Ok(response) => {
|
||||
let body = response
|
||||
.to_json_bytes()
|
||||
.ok()
|
||||
.and_then(|bytes| serde_json::from_slice::<Value>(&bytes).ok())
|
||||
.unwrap_or_else(|| json!({"wire_version":1,"kind":"response"}));
|
||||
if write_frame(&mut stdout, &json!({"kind":"response","response":body})).is_err() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
if write_frame(
|
||||
&mut stdout,
|
||||
&json!({"kind":"error","code":error.code,"message":error.message,"error_kind":error.kind.as_str()}),
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if write_frame(
|
||||
&mut stdout,
|
||||
&json!({"kind":"error","code":"worker_frame_invalid","message":"unknown frame kind"}),
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a successful response from an embedded wire response value.
|
||||
pub fn response_from_frame(frame: &Value) -> Result<WireResponse, PluginError> {
|
||||
let body = frame
|
||||
.get("response")
|
||||
.ok_or_else(|| PluginError::wire("worker_response_invalid", "missing response body"))?;
|
||||
let bytes = serde_json::to_vec(body)
|
||||
.map_err(|error| PluginError::wire("worker_response_invalid", error.to_string()))?;
|
||||
WireResponse::from_json_bytes(&bytes)
|
||||
}
|
||||
@@ -98,10 +98,10 @@ pub struct PlatformCapabilities {
|
||||
impl PlatformCapabilities {
|
||||
/// Detect capabilities for the current build and platform.
|
||||
///
|
||||
/// M0.5 intentionally reports strict isolation as unavailable: no OS
|
||||
/// isolation adapter is implemented yet, and pretending otherwise would
|
||||
/// violate the security contract. The worker process transport is likewise
|
||||
/// not implemented in this build.
|
||||
/// Strict OS isolation remains intentionally unavailable: no OS isolation
|
||||
/// adapter is implemented, and pretending otherwise would violate the
|
||||
/// security contract. The isolated worker transport is implemented, but a
|
||||
/// plain child process is not an OS sandbox.
|
||||
pub fn detect() -> Self {
|
||||
let reason = if cfg!(target_os = "linux") {
|
||||
"no OS isolation adapter (namespaces/seccomp/cgroups) is implemented in this build"
|
||||
@@ -118,7 +118,7 @@ impl PlatformCapabilities {
|
||||
strict: StrictIsolation::Unavailable {
|
||||
reason: reason.to_string(),
|
||||
},
|
||||
worker_processes: false,
|
||||
worker_processes: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ providers).
|
||||
|---|---|---|
|
||||
| R0 baseline + ADRs | **Done** | this section; `docs/adr/0008`–`0012`; full workspace gate |
|
||||
| R1 package-aware identity + callable registry | **Done for statics** | `ModuleId { instance, relative_path }`; `PackageResolver` path sources; `lock_version: 2` with v1 migration; `CallableRegistry` for builtins/marqdo/declared ABI exports; external manifest wiring is R2 |
|
||||
| R2 ABI/Kernel/Broker | **In progress** | `KernelRegistry` external providers; complete-name plugin export routing; `CapabilityBroker` with vendor policy; static `check --plugin` signatures; multi-export C test; provider tests. Isolated worker and handle table still pending |
|
||||
| R2 ABI/Kernel/Broker | **Done (worker = crash isolation)** | `KernelRegistry` external providers; complete-name export routing; `CapabilityBroker`; static `check --plugin`; multi-export C test; isolated worker with timeout/crash tests; host handle table. Native/worker are not an OS sandbox |
|
||||
| R3 Tool Protocol | **Done** | `tool install/list/remove`; unknown-command fallback; atomic local install; SHA/tamper checks; black-box tests for arbitrary names, Unicode args, conflicts, traversal and PATH hijacking |
|
||||
| R4 Analysis/Trace v1 | **Done** | `check --emit` Analysis v1; `run/call --trace-file` Trace v1; deterministic schema tests; independent Python consumer; stale-artifact removal; value redaction |
|
||||
| R5 environment/isolation | **Partial** | Pinned Git fetch/cache, transitive `@alias` resolution, atomic `.marqdo/env` materialization, foreign process-group cleanup. Strict OS isolation remains explicitly unavailable (fail-closed) |
|
||||
|
||||
@@ -198,6 +198,11 @@ static int32_t solver_add(MqCallContext *ctx, MqBytes args_wire,
|
||||
}
|
||||
free(json);
|
||||
|
||||
#ifdef MQ_SOLVER_CRASH
|
||||
/* Test-only crash injection for isolated-worker crash tests. */
|
||||
abort();
|
||||
#endif
|
||||
|
||||
if (__builtin_add_overflow(a, b, &sum)) {
|
||||
return emit_error("integer_overflow", "solver.add overflowed i64",
|
||||
error_wire, MQ_ERR_INTERNAL);
|
||||
|
||||
+27
-11
@@ -7,9 +7,11 @@ C header is `include/marqdo_plugin_abi_v1.h`; the Rust mirrors are in
|
||||
|
||||
> **A C ABI is not a sandbox.** A native plugin is loaded into the host
|
||||
> process. It can read host memory, call arbitrary syscalls, bypass the
|
||||
> capability model and crash the host. Loading requires an explicit trust
|
||||
> decision. The isolated worker transport described by the blueprint is **not
|
||||
> implemented** in this build.
|
||||
> capability model and crash the host. In-process loading requires an explicit
|
||||
> trust decision. `kind: worker` runs the same native library in an isolated
|
||||
> child process with length-prefixed JSON frames; that isolates crashes and
|
||||
> enforces a handshake/call timeout, but a plain child process is **not** an OS
|
||||
> sandbox. `strict` still refuses without a real isolation adapter.
|
||||
|
||||
## 1. Independent versions
|
||||
|
||||
@@ -164,8 +166,9 @@ Validation (`crates/marqdo-plugin-abi/src/manifest.rs::PluginManifest::validate`
|
||||
* `abi`: caret-style requirement `^1`, `^1.0`, `1` or `1.0`. Caret semantics:
|
||||
`^M.m` means `>=M.m, <M+1.0`; the `0.x` special case pins the minor. It must
|
||||
be compatible with the host's `1.0`;
|
||||
* `kind`: `native`, `worker` or `wasm`; only `native` is loadable in-process in
|
||||
this build;
|
||||
* `kind`: `native`, `worker` or `wasm`. `native` loads in-process only with
|
||||
explicit trust; `worker` loads in a child process without
|
||||
`--trust-native-plugin`; `wasm` is explicitly unsupported;
|
||||
* `exports`: at least one, at most 256, unique, namespaced (must contain `.`),
|
||||
no reserved prefix (`marqdo.`, `builtin.`, `core.`, `std.`, `host.`),
|
||||
non-empty signature ≤ 4096 bytes, signature starts with `(` and contains
|
||||
@@ -351,9 +354,22 @@ Semantics:
|
||||
cannot grant `--allow-plugin`, so loading fails with `permission.denied`.
|
||||
|
||||
Trust boundary: `--trust-native-plugin` is an explicit unsafe decision; the
|
||||
process shares its address space with the plugin. In-process load is the only
|
||||
transport implemented — there is no worker process, Wasm component, hot
|
||||
unload, or plugin capability bridge. A native plugin crash is a host crash.
|
||||
process shares its address space with the plugin. `kind: worker` avoids sharing
|
||||
the address space: the core binary is re-invoked as `__plugin-worker`, loads
|
||||
one native library in the child, reports its complete export table, and serves
|
||||
length-prefixed invoke frames. Handshake and calls have timeouts; a crash/EOF
|
||||
becomes `plugin.crashed`/`plugin.timeout` in the host. This is crash isolation,
|
||||
not an OS sandbox: the worker still runs as the user. Wasm, hot unload and a
|
||||
full plugin capability broker for worker requests remain future work.
|
||||
|
||||
### 9.1 Worker frames
|
||||
|
||||
Frames are a 4-byte big-endian length followed by one JSON object (max 64 MiB).
|
||||
Worker → host: `{"kind":"ready","exports":[...]}`. Host → worker:
|
||||
`{"kind":"invoke","request":<wire request>}` and `{"kind":"shutdown"}`.
|
||||
Worker → host responses are `{"kind":"response","response":<wire response>}` or
|
||||
`{"kind":"error","code":...,"message":...}`. The worker validates that the
|
||||
declared manifest exports exactly match the library's registrations.
|
||||
|
||||
## 10. SDK usage (`crates/marqdo-plugin-sdk`)
|
||||
|
||||
@@ -410,9 +426,9 @@ when no C compiler is available.
|
||||
|
||||
| Item | Status |
|
||||
|---|---|
|
||||
| Isolated plugin worker transport (length-prefixed IPC) | Not implemented; `PlatformCapabilities::worker_processes = false`. |
|
||||
| Wasm Component + WIT | Not implemented. |
|
||||
| Hot unload/reload | Not supported by ABI v1 (no shutdown callback). |
|
||||
| Isolated plugin worker transport (length-prefixed IPC) | Implemented for crash isolation with handshake/call timeouts; a child process is not an OS sandbox. |
|
||||
| Wasm Component + WIT | Not implemented; `kind: wasm` is rejected explicitly. |
|
||||
| Hot unload/reload | Not supported by ABI v1; the worker is killed on unload. |
|
||||
| Plugin installation by package manager | Not implemented; `marqdo.yaml` `plugins:` is declarative only. |
|
||||
| Vendor capability handlers | The broker and vendor-name validation exist; no concrete vendor handler is shipped in this build. |
|
||||
| Static validation without an explicit manifest | `check` needs `--plugin <path>`; descriptors are not discovered from the network. |
|
||||
|
||||
Reference in new issue
Block a user