- Add the frozen marqdo_resource_provider.h ABI (version + open/read/write/ close/last_error) and a libloading-based host adapter with version/symbol checks and callsite-attached host errors (ADR-0036). - Add StdHost::register_c_resource_provider using the same kind/name validation and project-mode name grant as the in-process/worker providers. - Compile a C shared-library test provider; cover round-trip and fail-closed missing-library behavior. Trusted native code, not a sandbox; plugin-Worker binding remains open. - Update ADRs/spec/status/README/changelog and evidence. Gate: fmt, clippy -D warnings, 435 tests passed.
149 lines
4.3 KiB
Rust
149 lines
4.3 KiB
Rust
#![cfg(unix)]
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
use marqdo_host::{Capabilities, Host, StdHost};
|
|
use marqdo_sandbox::SandboxMode;
|
|
use marqdo_value::ErrorCategory;
|
|
|
|
fn context() -> marqdo_host::ExecutionContext {
|
|
marqdo_host::ExecutionContext::new("test".to_string(), None)
|
|
}
|
|
|
|
fn handle(value: marqdo_value::Value) -> marqdo_value::ResourceHandle {
|
|
match value {
|
|
marqdo_value::Value::Resource(handle) => handle,
|
|
other => panic!("expected resource, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
fn cc_available() -> bool {
|
|
Command::new("cc")
|
|
.arg("--version")
|
|
.output()
|
|
.map(|output| output.status.success())
|
|
.unwrap_or(false)
|
|
}
|
|
|
|
fn build_provider(dir: &Path) -> PathBuf {
|
|
let source = dir.join("provider.c");
|
|
std::fs::write(
|
|
&source,
|
|
r#"
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
|
|
static const char DATA[] = "c-abi";
|
|
static size_t position = 0;
|
|
static uint64_t next_token = 1;
|
|
|
|
uint32_t marqdo_resource_provider_abi_version(void) { return 1u; }
|
|
|
|
int32_t marqdo_resource_open(const char *kind, const char *descriptor,
|
|
const char *rights_csv, uint64_t *out_token) {
|
|
(void)kind; (void)descriptor; (void)rights_csv;
|
|
position = 0;
|
|
*out_token = next_token++;
|
|
return 0;
|
|
}
|
|
|
|
int32_t marqdo_resource_read(uint64_t token, uint8_t *buffer,
|
|
uint32_t capacity, uint32_t *out_read,
|
|
int32_t *out_eof) {
|
|
(void)token;
|
|
size_t remaining = sizeof(DATA) - 1 - position;
|
|
size_t count = remaining < capacity ? remaining : capacity;
|
|
memcpy(buffer, DATA + position, count);
|
|
position += count;
|
|
*out_read = (uint32_t)count;
|
|
*out_eof = position >= sizeof(DATA) - 1 ? 1 : 0;
|
|
return 0;
|
|
}
|
|
|
|
int32_t marqdo_resource_write(uint64_t token, const uint8_t *bytes,
|
|
uint32_t length, uint32_t *out_written) {
|
|
(void)token; (void)bytes;
|
|
*out_written = length;
|
|
return 0;
|
|
}
|
|
|
|
int32_t marqdo_resource_close(uint64_t token) { (void)token; return 0; }
|
|
|
|
int32_t marqdo_resource_last_error(char *buffer, uint32_t capacity) {
|
|
if (capacity > 0) buffer[0] = 0;
|
|
return 0;
|
|
}
|
|
"#,
|
|
)
|
|
.unwrap();
|
|
let extension = if cfg!(target_os = "macos") {
|
|
"dylib"
|
|
} else {
|
|
"so"
|
|
};
|
|
let library = dir.join(format!("libecho_provider.{extension}"));
|
|
let status = Command::new("cc")
|
|
.args(["-shared", "-fPIC", "-O2", "-o"])
|
|
.arg(&library)
|
|
.arg(&source)
|
|
.status()
|
|
.expect("run cc");
|
|
assert!(status.success(), "C provider build failed");
|
|
library
|
|
}
|
|
|
|
#[test]
|
|
fn c_abi_provider_round_trips() {
|
|
if !cc_available() {
|
|
eprintln!("skipping C provider test: cc is unavailable");
|
|
return;
|
|
}
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let library = build_provider(dir.path());
|
|
let project = tempfile::tempdir().unwrap();
|
|
let mut host = StdHost::new(
|
|
SandboxMode::Project,
|
|
Capabilities::project().allow_resource_provider("cabi"),
|
|
project.path(),
|
|
);
|
|
host.register_c_resource_provider("cabi", &["cabi.sensor"], &library)
|
|
.unwrap();
|
|
let ctx = context();
|
|
let handle = handle(
|
|
host.resource_open("cabi.sensor", "sensor-1", &["read".to_string()], &ctx)
|
|
.unwrap(),
|
|
);
|
|
let first = host.resource_read(handle, 2, &ctx).unwrap();
|
|
assert_eq!(first.bytes, b"c-");
|
|
assert!(!first.eof);
|
|
let rest = host.resource_read(handle, 16, &ctx).unwrap();
|
|
assert_eq!(rest.bytes, b"abi");
|
|
assert!(rest.eof);
|
|
assert_eq!(host.resource_write(handle, b"xyz", &ctx).unwrap(), 3);
|
|
host.resource_close(handle, &ctx).unwrap();
|
|
assert_eq!(
|
|
host.resource_close(handle, &ctx).unwrap_err().code(),
|
|
"resource.closed"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn c_abi_provider_loading_fails_closed() {
|
|
let project = tempfile::tempdir().unwrap();
|
|
let mut host = StdHost::new(
|
|
SandboxMode::Project,
|
|
Capabilities::project().allow_resource_provider("cabi"),
|
|
project.path(),
|
|
);
|
|
let error = host
|
|
.register_c_resource_provider(
|
|
"cabi",
|
|
&["cabi.sensor"],
|
|
Path::new("/definitely/missing/marqdo-provider.so"),
|
|
)
|
|
.unwrap_err();
|
|
assert_eq!(error.category, ErrorCategory::KernelUnavailable);
|
|
}
|