feat(plugin-abi,sdk): versioned C ABI v1, host bindings, Rust SDK and C example

Blueprint section 10: a first-class plugin ABI independent of the legacy
MARQDO_ABI_VERSION=2, with a C ABI that is explicitly not a sandbox.

- include/marqdo_plugin_abi_v1.h: frozen stable-layout C contract with
  documented buffer ownership/lifetimes, size limits, NULL/zero-length
  semantics, single-threaded invocation, no hot-unload policy, version
  negotiation and target-triple requirements.
- marqdo-plugin-abi (host side): repr(C) mirrors with compile-time layout
  assertions, major/minor negotiation (wrong major fails, minor clamps),
  plugin.yaml parsing/validation, target selection and sha256 artifact
  verification, wire_version 1 typed JSON request/response envelopes,
  structured PluginError, and a libloading-based loader with host alloc/free,
  export registration and runtime ABI re-negotiation.
- marqdo-plugin-sdk (plugin side): mq_plugin_entry! macro, panic containment,
  safe byte-buffer wrappers, capability/logging context and JSON helpers; it
  depends only on the shared ABI crate.
- examples/plugin-demo: dependency-free C plugin (solver.add), manifest,
  build.sh that refreshes the artifact hash, README explaining that install,
  inspect and load are three different trust decisions.
- Tests: C plugin cross-compiled with cc and loaded, plus wrong ABI major,
  missing symbol/artifact, hash mismatch and malformed manifest cases.
This commit is contained in:
cflmy committed 2026-11-20 10:43:59 +08:00
1 parent f6c3efa849
commit 6da206a906
20 files changed
+5171 -1

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
//! Rust SDK for writing Marqdo native plugins against plugin ABI v1.
//!
//! The SDK is the plugin-developer side of `include/marqdo_plugin_abi_v1.h`.
//! It hides the raw C layout behind safe wrappers and provides the
//! [`mq_plugin_entry!`] macro that exports the required
//! `mq_plugin_entry_v1` symbol:
//!
//! ```
//! use marqdo_plugin_sdk::{mq_plugin_entry, CallContext, PluginError, Value};
//!
//! fn add(_ctx: &mut CallContext, args: &[u8]) -> Result<Vec<u8>, PluginError> {
//! let request = marqdo_plugin_sdk::wire::decode_request(args)?;
//! let values = request.typed_args()?;
//! let a = marqdo_plugin_sdk::wire::int_arg(&values, "a")?;
//! let b = marqdo_plugin_sdk::wire::int_arg(&values, "b")?;
//! marqdo_plugin_sdk::wire::success(Some(Value::Int((a + b).into())))
//! }
//!
//! mq_plugin_entry! {
//! name: "demo.solver",
//! version: "0.1.0",
//! exports: {
//! "solver.add" => "($a: Int, $b: Int) -> Int" => add,
//! }
//! }
//! ```
//!
//! The SDK never depends on host-only crates: it only uses the shared ABI
//! types and the JSON wire envelope from `marqdo-plugin-abi`. Building a
//! plugin does not grant it any capability; authorization is a separate host
//! decision performed before loading.
#![deny(missing_docs)]
pub use marqdo_plugin_abi::abi::result as result_code;
pub use marqdo_plugin_abi::abi::{
MqBytes, MqCallContext, MqHostApi, MqOwnedBytes, MqPluginApi, MqPluginInvoke, ABI_MAJOR,
ABI_MINOR, ABI_VERSION_STRING, ENTRY_SYMBOL,
};
pub use marqdo_plugin_abi::{PluginError, Value, WireRequest, WireResponse, WIRE_VERSION};
pub mod bytes;
pub mod call;
pub mod entry;
pub mod wire;
pub use bytes::{BytesRef, HostBuffer};
pub use call::CallContext;
pub use wire::{
bool_arg, decode_request, error_response, int_arg, response_value, success, success_raw,
text_arg, value_arg,
};
/// Plugin handler signature.
///
/// A handler receives the per-call context (for capability requests and
/// logging) and the raw request envelope bytes, and returns the raw response
/// envelope bytes on success.
///
/// Use [`wire::decode_request`] and [`wire::success`] to build the response;
/// handlers should never panic: panics are caught by the SDK and converted
/// into a structured error, but the plugin process stays healthy only if the
/// panic does not corrupt plugin state.
pub type HandlerFn = fn(&mut CallContext, &[u8]) -> Result<Vec<u8>, PluginError>;
/// Declare a plugin and export `mq_plugin_entry_v1`.
///
/// ```ignore
/// marqdo_plugin_sdk::mq_plugin_entry! {
/// name: "demo.solver",
/// version: "0.1.0",
/// exports: {
/// "solver.add" => "($a: Int, $b: Int) -> Int" => add,
/// "solver.echo" => "($text: Text) -> Text" => echo,
/// }
/// }
/// ```
///
/// Each handler is a path (or non-capturing closure) with the [`HandlerFn`]
/// signature. The macro:
/// - validates the host ABI table (major version and `struct_size`),
/// - reports the plugin ABI version and a metadata envelope,
/// - registers every export and fails the load if registration fails.
#[macro_export]
macro_rules! mq_plugin_entry {
(
name: $name:literal,
version: $version:literal,
exports: {
$( $export_name:literal => $signature:literal => $handler:expr ),* $(,)?
}
) => {
/// Marqdo plugin ABI v1 entry point (generated by the SDK macro).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mq_plugin_entry_v1(
host: *const $crate::MqHostApi,
plugin: *mut $crate::MqPluginApi,
) -> i32 {
const METADATA: &[u8] = concat!(
"{\"name\":\"", $name, "\",\"version\":\"", $version, "\"}"
)
.as_bytes();
let metadata: &'static [u8] = METADATA;
// SAFETY: the host provides the ABI tables per the header contract.
let init = unsafe { $crate::entry::initialize(host, plugin, metadata) };
if init != 0 {
return init;
}
$(
{
extern "C" fn invoke(
ctx: *mut $crate::MqCallContext,
args: $crate::MqBytes,
result: *mut $crate::MqOwnedBytes,
error: *mut $crate::MqOwnedBytes,
) -> i32 {
// Expand the user's handler expression in a safe
// context (also required by `macro_metavars_in_unsafe`).
let handler: $crate::HandlerFn = $handler;
// SAFETY: the host owns the invocation buffers; the SDK
// validates them and never retains them.
unsafe {
$crate::entry::dispatch(handler, ctx, args, result, error)
}
}
let export_name: &str = $export_name;
let signature: &str = $signature;
// SAFETY: host table is valid for the plugin lifetime and
// was validated by `initialize`.
let code =
unsafe { $crate::entry::register_export(host, export_name, signature, invoke) };
if code != 0 {
return code;
}
}
)*
$crate::result_code::OK
}
};
}
#[cfg(test)]
mod tests;