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));
|
||||
}
|
||||
Reference in new issue
Block a user