feat(ext): single callable binding, provider v2 CLI, capability handlers, stdlib aliases

Implements RFC-EXT-0001 PR-00..PR-07 first pass:
- CallSiteKey/CallBindingTable + one frontend binding pass; runtime dispatch by identity
- prepare composition root shared by check/run/inspect/Analysis; exact plugin reconciliation
- provider.json v2 --provider/--allow-provider worker CLI with fail-closed verification
- capability handler registry with real fs.read and audit
- verified stdlib public aliases replace print_override/AST scan
- docs: specs, ADR-0054/0055/0057/0058, README, CI/release gates, evidence

PR-04 extension selection/lock v4 and final frozen-binary evidence remain.
This commit is contained in:
cflmy committed 2026-11-22 00:17:36 +08:00
1 parent 9aa536c2c9
commit 1f6fd644a0
54 files changed
+8710 -828

No files matched your search

+23 -31
View File
@@ -536,7 +536,7 @@ fn expr_calls(
) {
match expr {
Expr::Call { callee, args, span } => {
let (state, target) = resolve_callee(project, module, callee);
let (state, target) = resolve_callee(project, module, function, *span);
calls.push(json!({
"function": function.id.to_string(),
"span": span_json(*span),
@@ -572,42 +572,34 @@ fn expr_calls(
}
}
/// Project the authoritative call binding into the Analysis `state`/`target`
/// fields.
///
/// Analysis is a read-only projection of the same prepared binding the runtime
/// consumes (RFC-EXT-0001 §4/§9): there is no second name-resolution algorithm
/// here. `state` keeps its v1 vocabulary (`function`, `builtin`, `plugin`,
/// `unresolved`); official stdlib aliases such as `打印` therefore appear as
/// `function` targets because that is the implementation that actually runs.
fn resolve_callee(
project: &LoadedProject,
module: &Module,
callee: &Expr,
_module: &Module,
function: &Function,
span: Span,
) -> (&'static str, Value) {
match callee {
Expr::Ident(ident) => {
if marqdo_ir::builtins::find(&ident.name).is_some() {
("builtin", json!(ident.name))
} else if let Some(function) = module.function(&ident.name) {
("function", json!(function.id.to_string()))
} else {
("unresolved", Value::Null)
}
let key = marqdo_ir::call_binding::CallSiteKey::new(function.id.clone(), span);
match project.program.call_bindings.get(&key) {
Some(marqdo_ir::call_binding::BoundTarget::Builtin { canonical_name }) => {
("builtin", json!(canonical_name))
}
Expr::Member { base, name, .. } => {
if let Expr::Ident(alias) = base.as_ref() {
let qualified = format!("{}.{}", alias.name, name);
if marqdo_ir::builtins::find(&qualified).is_some() {
return ("builtin", json!(qualified));
}
if let Some(target_module) = project.program.resolve_import(&module.id, &alias.name)
{
if let Some(target) = project.program.module(target_module) {
if let Some(function) = target.function(name) {
return ("function", json!(function.id.to_string()));
}
}
}
if project.program.external_callable(&qualified).is_some() {
return ("plugin", json!(qualified));
}
}
Some(marqdo_ir::call_binding::BoundTarget::Marqdo { function }) => {
("function", json!(function.to_string()))
}
Some(marqdo_ir::call_binding::BoundTarget::AbiPlugin { export, .. }) => {
("plugin", json!(export))
}
Some(marqdo_ir::call_binding::BoundTarget::DynamicValue) | None => {
("unresolved", Value::Null)
}
_ => ("unresolved", Value::Null),
}
}
+26 -132
View File
@@ -27,148 +27,42 @@ pub use sandbox::SandboxSettings;
pub use tool::{fmt, plugin_inspect};
use crate::args;
use crate::loader::{self, FileDiagnostic, LoadedProject};
use crate::extension_descriptors::{self, StaticPluginSet};
use crate::loader::{FileDiagnostic, LoadedProject};
use crate::output;
use crate::project_prepare::{self, PrepareOptions, PreparedProgram};
fn load_project(file: Option<&Path>, json: bool) -> Result<LoadedProject, i32> {
let file = file.unwrap_or_else(|| Path::new("index.mq.md"));
match loader::load(file) {
Ok(project) => Ok(project),
/// Prepare one document through the single composition-root step.
///
/// `check`/`run`/`call`/`inspect` must all use this function so they share the
/// same call bindings and diagnostics (RFC-EXT-0001 §4.4, §18.2).
pub(super) fn prepare_project(
file: Option<&Path>,
json: bool,
plugins: &[PathBuf],
) -> Result<PreparedProgram, i32> {
let options = PrepareOptions {
plugins: plugins.to_vec(),
};
match project_prepare::prepare(file, &options) {
Ok(prepared) => Ok(prepared),
Err(error) => {
if json {
output::print_json(&json!({"success": false, "error": error}));
output::print_json(&json!({
"success": false,
"error": {
"code": error.code,
"message": error.message,
}
}));
} else {
eprintln!("error: {error}");
}
if error.contains("marqdo.lock") || error.contains("lockfile") {
Err(1)
} else {
Err(2)
eprintln!("error[{}]: {}", error.code, error.message);
}
Err(error.exit_code)
}
}
}
/// Add plugin export descriptors from manifests without loading a library.
fn add_static_plugin_descriptors(
project: &mut LoadedProject,
paths: &[PathBuf],
) -> Result<(), String> {
for path in paths {
let manifest_path = if path.is_dir() {
path.join("plugin.yaml")
} else {
path.clone()
};
let manifest = marqdo_plugin_abi::PluginManifest::load(&manifest_path)
.map_err(|error| format!("{}: {}", error.code, error.message))?;
manifest
.validate()
.map_err(|error| format!("{}: {}", error.code, error.message))?;
let digest = manifest
.resolve_artifact(&marqdo_plugin_abi::HostTarget::current())
.map_err(|error| format!("{}: {}", error.code, error.message))?
.sha256
.clone();
for export in &manifest.plugin.exports {
if project
.program
.external_callables
.iter()
.any(|callable| callable.public_name == export.name)
{
return Err(format!(
"plugin export `{}` is declared more than once",
export.name
));
}
let signature = plugin_signature(&export.signature).map_err(|error| {
format!(
"plugin export `{}` has an invalid signature: {error}",
export.name
)
})?;
project
.program
.external_callables
.push(marqdo_ir::callable::CallableDescriptor {
id: marqdo_ir::callable::CallableId(format!(
"plugin:{}:{}",
manifest.plugin.name, export.name
)),
public_name: export.name.clone(),
owner_package: manifest.plugin.name.clone(),
signature,
effects_requested: manifest.plugin.requires.clone(),
origin: marqdo_ir::callable::CallableOrigin::AbiPlugin {
package: manifest.plugin.name.clone(),
export: export.name.clone(),
},
implementation_digest: Some(digest.clone()),
source_span: None,
});
}
}
if paths.is_empty() {
return Ok(());
}
let existing: Vec<(Option<marqdo_span::Span>, String)> = project
.diagnostics
.iter()
.map(|file| {
(
file.diagnostic.span,
format!("{}:{}", file.module_id, file.diagnostic.message),
)
})
.collect();
for (module_id, diagnostic) in marqdo_frontend::calls::validate_program(&project.program) {
let key = format!("{}:{}", module_id, diagnostic.message);
if existing.contains(&(diagnostic.span, key)) {
continue;
}
if let Some(file) = project.files.get(&module_id) {
project.diagnostics.push(FileDiagnostic {
module_id,
path: file.path.clone(),
source: file.source.clone(),
diagnostic,
});
}
}
Ok(())
}
/// Convert a plugin manifest signature (`($a: Int) -> Int`) to the language
/// parameter list, dropping the display-only return annotation and `$` sigils.
fn plugin_signature(raw: &str) -> Result<marqdo_ir::callable::CallableSignature, String> {
let before_return = raw.split("->").next().unwrap_or(raw).trim();
let open = before_return
.find('(')
.ok_or_else(|| "missing `(`".to_string())?;
let close = before_return
.rfind(')')
.ok_or_else(|| "missing `)`".to_string())?;
if close < open {
return Err("malformed parameter list".to_string());
}
let params = before_return[open + 1..close].replace('$', "");
let parsed =
marqdo_frontend::signature::parse(&format!("({params})"), marqdo_span::Span::default());
if parsed.has_errors() {
return Err(parsed
.diagnostics
.iter()
.map(|diagnostic| diagnostic.message.clone())
.collect::<Vec<_>>()
.join("; "));
}
Ok(marqdo_ir::callable::CallableSignature {
params: parsed.params,
kind: marqdo_ir::SignatureKind::Explicit,
})
}
/// `marqdo check`.
fn validate_refs(project: &LoadedProject) -> Vec<FileDiagnostic> {
let mut diagnostics = Vec::new();
+3 -10
View File
@@ -9,18 +9,11 @@ pub fn check(
plugins: &[PathBuf],
emit: Option<&Path>,
) -> i32 {
let mut project = match load_project(file, json) {
Ok(project) => project,
let prepared = match prepare_project(file, json, plugins) {
Ok(prepared) => prepared,
Err(code) => return code,
};
if let Err(error) = add_static_plugin_descriptors(&mut project, plugins) {
if json {
output::print_json(&json!({"success": false, "error": error}));
} else {
eprintln!("error: {error}");
}
return 2;
}
let mut project = prepared.loaded;
if refs {
let ref_diagnostics = validate_refs(&project);
project.diagnostics.extend(ref_diagnostics);
+3 -2
View File
@@ -20,10 +20,11 @@ pub struct InspectFlags {
}
pub fn inspect(file: Option<&Path>, flags: &InspectFlags, json: bool) -> i32 {
let project = match load_project(file, json) {
Ok(project) => project,
let prepared = match prepare_project(file, json, &[]) {
Ok(prepared) => prepared,
Err(code) => return code,
};
let project = prepared.loaded;
let entry = project.entry_file();
let module = &entry.analysis.module;
let selected_any =
+253 -42
View File
@@ -4,6 +4,7 @@
//! runtime helpers, but keeps the run session out of the command table.
use super::*;
use crate::provider_manifest;
use marqdo_sandbox::StrictChildState;
pub fn execute(
@@ -15,18 +16,12 @@ pub fn execute(
trace: bool,
) -> i32 {
let command_name = if function.is_some() { "call" } else { "run" };
let mut project = match load_project(file, json) {
Ok(project) => project,
let prepared = match prepare_project(file, json, &settings.plugins) {
Ok(prepared) => prepared,
Err(code) => return code,
};
if let Err(error) = add_static_plugin_descriptors(&mut project, &settings.plugins) {
if json {
output::print_json(&json!({"success": false, "error": error}));
} else {
eprintln!("error: {error}");
}
return 2;
}
let project = prepared.loaded;
let static_plugins = prepared.plugins;
if project.has_errors() {
if json {
output::print_json(&json!({
@@ -136,10 +131,80 @@ pub fn execute(
return 2;
}
}
for plugin_path in &settings.plugins {
if let Err(message) =
load_plugin_into_host(&mut host, plugin_path, settings.trust_native_plugin)
// T6: verify every selected artifact/path/hash before starting a worker or
// loading native code. Static path/signature checks happened in `prepare`.
//
// Provider selection is host assembly, not callable semantics: `check`
// never sees it. Parse and verify every `--provider` manifest, resolve the
// current target artifact, check the exact grant and reject duplicate
// names/kinds *before* any provider or plugin worker process is spawned
// (RFC-EXT-0001 §5.4.6/§18.2 T6–T7).
let providers = match provider_manifest::read_provider_manifests(&settings.providers) {
Ok(providers) => providers,
Err(error) => {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
};
// `--provider` alone never authorizes; the exact `--allow-provider NAME`
// is required before the helper process may be started. An ungranted
// provider fails closed instead of being silently ignored.
for provider in &providers {
if !settings
.allow_provider
.iter()
.any(|grant| grant == &provider.name)
{
let error = extension_descriptors::PrepareError::load(
"permission.denied",
format!(
"resource provider `{}` was selected with --provider but is not granted; \
add --allow-provider {}",
provider.name, provider.name
),
);
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
}
// A grant that matches no selected provider is a deterministic option
// error: providers are never discovered from PATH, the working directory or
// the network.
for grant in &settings.allow_provider {
if !providers.iter().any(|provider| &provider.name == grant) {
let error = extension_descriptors::PrepareError::option(
"ext.provider_grant_unmatched",
format!(
"--allow-provider `{grant}` matches no selected --provider manifest; \
providers are never discovered automatically"
),
);
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
}
for plugin in &static_plugins.plugins {
if let Err(error) = extension_descriptors::verify_bundle_regular_file(
&plugin.base_dir,
&plugin.artifact_relative,
) {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
let artifact = plugin.artifact_path();
if let Err(error) = extension_descriptors::verify_sha256(&artifact, &plugin.artifact_sha256)
{
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
}
// T7: start only selected, granted and verified extensions.
for plugin in &static_plugins.plugins {
if let Err(message) = load_plugin_into_host(
&mut host,
&plugin.manifest_path,
settings.trust_native_plugin,
) {
if json {
output::print_json(&json!({"success": false, "error": message}));
} else {
@@ -148,36 +213,24 @@ pub fn execute(
return 1;
}
}
// Expose the complete export names loaded from trusted manifests so the
// runtime resolves exactly those names instead of probing namespaces.
for (name, _signature, digest, package) in host.plugin_exports() {
if project
.program
.external_callables
.iter()
.any(|callable| callable.public_name == name)
{
continue;
// T8: exact one-to-one reconciliation with the static descriptors. Never
// synthesise a missing signature (`params: Vec::new()`) or guess an export.
if let Err(message) = reconcile_plugin_exports(&static_plugins, &host) {
emit_error(json, command_name, "ext.plugin_binding_mismatch", &message);
return 1;
}
// T7b: start only granted and verified provider helpers. Every manifest,
// artifact, target, grant and duplicate-kind check above already passed, so
// no worker is spawned for a provider that failed verification.
for provider in &providers {
if let Err(error) = provider_manifest::load_resource_provider_into_host(
&mut host,
provider,
&settings.allow_provider,
) {
emit_prepare_error(json, command_name, &error);
return error.exit_code;
}
project
.program
.external_callables
.push(marqdo_ir::callable::CallableDescriptor {
id: marqdo_ir::callable::CallableId(format!("plugin:{package}:{name}")),
public_name: name.clone(),
owner_package: package.clone(),
signature: marqdo_ir::callable::CallableSignature {
params: Vec::new(),
kind: marqdo_ir::SignatureKind::Explicit,
},
effects_requested: vec!["host".to_string()],
origin: marqdo_ir::callable::CallableOrigin::AbiPlugin {
package,
export: name,
},
implementation_digest: Some(digest),
source_span: None,
});
}
let function_name = function.unwrap_or("main").to_string();
let entry_module = &project.program.modules[&project.entry];
@@ -292,6 +345,80 @@ pub fn execute(
}
}
fn emit_prepare_error(
json: bool,
command_name: &str,
error: &crate::extension_descriptors::PrepareError,
) {
if json {
output::print_json(&json!({
"command": command_name,
"success": false,
"error": {"code": error.code, "message": error.message},
}));
} else {
eprintln!("error[{}]: {}", error.code, error.message);
}
}
/// Compare the Host's actual plugin exports with the static manifest set.
///
/// Names, raw signatures, owners and verified artifact digests must match
/// exactly; extra, missing or altered exports fail closed with
/// `ext.plugin_binding_mismatch` and the main function never runs.
fn reconcile_plugin_exports(
static_plugins: &StaticPluginSet,
host: &StdHost,
) -> Result<(), String> {
let mut expected: std::collections::BTreeMap<String, (String, String, String)> =
std::collections::BTreeMap::new();
for plugin in &static_plugins.plugins {
for export in &plugin.exports {
expected.insert(
export.name.clone(),
(
export.signature.clone(),
plugin.artifact_sha256.clone(),
plugin.package.clone(),
),
);
}
}
let loaded = host.plugin_exports();
if loaded.len() != expected.len() {
return Err(format!(
"static plugin descriptors declare {} export(s) but the runtime loaded {}; \
refusing to execute",
expected.len(),
loaded.len()
));
}
for (name, signature, digest, package) in loaded {
let Some((expected_signature, expected_digest, expected_package)) = expected.get(&name)
else {
return Err(format!(
"runtime export `{name}` was not declared by any selected plugin manifest"
));
};
if &signature != expected_signature {
return Err(format!(
"export `{name}` signature mismatch: manifest `{expected_signature}`, runtime `{signature}`"
));
}
if &digest != expected_digest {
return Err(format!(
"export `{name}` artifact digest mismatch: manifest `{expected_digest}`, runtime `{digest}`"
));
}
if &package != expected_package {
return Err(format!(
"export `{name}` owner mismatch: manifest `{expected_package}`, runtime `{package}`"
));
}
}
Ok(())
}
/// Read piped stdin once at the composition boundary.
///
/// A terminal stdin is not read, so `marqdo run` never blocks waiting for an
@@ -310,3 +437,87 @@ fn read_stdin() -> Vec<u8> {
}
bytes
}
#[cfg(test)]
mod tests {
use super::*;
fn empty_host() -> StdHost {
StdHost::new(
SandboxMode::Project,
marqdo_host::Capabilities::pure(),
std::env::temp_dir(),
)
}
fn static_plugin_with_export() -> StaticPluginSet {
let descriptor = marqdo_ir::callable::CallableDescriptor {
id: marqdo_ir::callable::CallableId("plugin:demo.ext:solver.add".into()),
public_name: "solver.add".into(),
owner_package: "demo.ext".into(),
signature: marqdo_ir::callable::CallableSignature {
params: vec![
marqdo_ir::Param {
name: "a".into(),
span: Span::default(),
type_annotation: None,
default: None,
},
marqdo_ir::Param {
name: "b".into(),
span: Span::default(),
type_annotation: None,
default: None,
},
],
kind: marqdo_ir::SignatureKind::Explicit,
},
effects_requested: vec!["math.compute".into()],
origin: marqdo_ir::callable::CallableOrigin::AbiPlugin {
package: "demo.ext".into(),
export: "solver.add".into(),
},
implementation_digest: Some("a".repeat(64)),
source_span: None,
};
StaticPluginSet {
plugins: vec![crate::extension_descriptors::StaticPlugin {
manifest_path: PathBuf::from("/fixtures/demo/plugin.yaml"),
base_dir: PathBuf::from("/fixtures/demo"),
package: "demo.ext".into(),
version: "0.1.0".into(),
kind: marqdo_plugin_abi::PluginKind::Worker,
manifest_sha256: format!("sha256:{}", "b".repeat(64)),
artifact_target: marqdo_plugin_abi::HostTarget::current()
.triple()
.to_string(),
artifact_sha256: "a".repeat(64),
artifact_relative: "build/plugin.so".into(),
exports: vec![crate::extension_descriptors::StaticPluginExport {
name: "solver.add".into(),
signature: "($a: Int, $b: Int) -> Int".into(),
descriptor,
}],
requires: Vec::new(),
}],
}
}
#[test]
fn reconcile_accepts_matching_empty_sets() {
assert!(
reconcile_plugin_exports(&StaticPluginSet::default(), &empty_host()).is_ok(),
"no selected plugin and no loaded plugin must reconcile"
);
}
#[test]
fn reconcile_rejects_missing_runtime_exports() {
let error = reconcile_plugin_exports(&static_plugin_with_export(), &empty_host())
.expect_err("a declared export that the runtime did not load must fail closed");
assert!(
error.contains("declare 1 export(s)") || error.contains("not declared"),
"{error}"
);
}
}
+45 -5
View File
@@ -43,6 +43,10 @@ pub struct SandboxSettings {
pub timeout: Option<u64>,
/// Explicitly trusted plugin manifests to load.
pub plugins: Vec<PathBuf>,
/// Explicitly selected resource provider v2 manifests (RFC-EXT-0001 §5.4).
pub providers: Vec<PathBuf>,
/// Exact resource provider name grants (declaration is not a grant).
pub allow_provider: Vec<String>,
/// Explicit trust decision for in-process native plugin loading.
pub trust_native_plugin: bool,
/// Optional Trace v2 output path.
@@ -68,8 +72,8 @@ impl SandboxSettings {
/// Reject strict-mode requests that this L1 isolation level does not
/// support. Strict currently covers pure Marqdo and native Math only;
/// granting Foreign, ABI plugins, tools or trace files would require
/// extending the supervisor contract first.
/// granting Foreign, ABI plugins, resource providers, tools or trace files
/// would require extending the supervisor contract first.
pub fn strict_preflight(&self) -> Result<(), String> {
let requested = self.allow_foreign
|| !self.allow_read.is_empty()
@@ -80,6 +84,8 @@ impl SandboxSettings {
|| self.allow_random
|| !self.allow_plugin.is_empty()
|| !self.plugins.is_empty()
|| !self.providers.is_empty()
|| !self.allow_provider.is_empty()
|| self.trust_native_plugin
|| !self.kernels.is_empty()
|| self.trace_file.is_some();
@@ -87,8 +93,9 @@ impl SandboxSettings {
return Err(
"strict mode currently supports pure Marqdo and native Math only; \
--allow-read/--allow-write/--allow-env/--allow-process/--allow-random/\
--allow-foreign/--allow-plugin/--plugin/--trust-native-plugin/--kernel/--trace-file are
rejected. Use --mode project for those capabilities."
--allow-foreign/--allow-plugin/--plugin/--provider/--allow-provider/\
--trust-native-plugin/--kernel/--trace-file are rejected. \
Use --mode project for those capabilities."
.to_string(),
);
}
@@ -108,9 +115,13 @@ impl SandboxSettings {
|| self.allow_random
|| self.allow_foreign
|| !self.allow_plugin.is_empty()
|| !self.providers.is_empty()
|| !self.allow_provider.is_empty()
{
return Err(
"pure mode prohibits external I/O grants; use --mode project".to_string(),
"pure mode prohibits external I/O grants and extension providers; \
use --mode project"
.to_string(),
);
}
Ok(Capabilities::pure())
@@ -141,6 +152,10 @@ impl SandboxSettings {
for plugin in &self.allow_plugin {
capabilities = capabilities.allow_plugin(plugin.clone());
}
for provider in &self.allow_provider {
validate_provider_grant(provider)?;
capabilities = capabilities.allow_resource_provider(provider.clone());
}
Ok(capabilities)
}
SandboxMode::Strict => {
@@ -150,3 +165,28 @@ impl SandboxSettings {
}
}
}
/// Reject non-exact `--allow-provider` grants.
///
/// Provider grants are exact stable names (RFC-EXT-0001 §5.2): there is no `*`
/// wildcard, no prefix matching and no implicit discovery, so a grant either
/// names a selected manifest or the run fails closed.
fn validate_provider_grant(name: &str) -> Result<(), String> {
if name.is_empty() {
return Err("--allow-provider requires a non-empty provider name".to_string());
}
if name
.chars()
.any(|character| matches!(character, '*' | '?' | '[' | ']'))
{
return Err(format!(
"--allow-provider `{name}`: wildcards are not supported; grants are exact provider names"
));
}
if name.chars().any(char::is_control) {
return Err(format!(
"--allow-provider `{name}`: provider names must not contain control characters"
));
}
Ok(())
}
+414
View File
@@ -0,0 +1,414 @@
//! Static extension descriptors for the CLI composition root
//! (RFC-EXT-0001 §4.4/§6, ADR-0054/0056).
//!
//! This module only *reads* manifests. It never `dlopen`s a library, never
//! spawns a worker, never opens a network connection and never executes a
//! side-effect script. The descriptors produced here feed the single
//! [`marqdo_ir::callable::CallableRegistry`] and binding pass; `run` later
//! verifies the real artifacts against these recorded identities.
use std::collections::BTreeSet;
use std::path::{Component, Path, PathBuf};
use marqdo_ir::callable::{CallableDescriptor, CallableId, CallableOrigin, CallableSignature};
use marqdo_ir::SignatureKind;
use marqdo_plugin_abi::{HostTarget, PluginKind, PluginManifest};
use marqdo_span::Span;
use sha2::{Digest, Sha256};
/// A structured prepare-stage failure.
#[derive(Debug, Clone)]
pub struct PrepareError {
/// Stable diagnostic code (`ext.*` unless reusing an existing event code).
pub code: String,
/// Human-readable message; never contains secrets or binary content.
pub message: String,
/// CLI exit code (2 for manifest/option errors, 1 for load/verify errors).
pub exit_code: i32,
}
impl PrepareError {
/// Construct a manifest/option error (exit code 2).
pub fn option(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
exit_code: 2,
}
}
/// Construct a loading/verification error (exit code 1).
pub fn load(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
exit_code: 1,
}
}
}
/// One statically declared export.
#[derive(Debug, Clone)]
pub struct StaticPluginExport {
/// Complete dotted export name.
pub name: String,
/// Raw manifest signature string, compared exactly at run time.
pub signature: String,
/// Parsed static descriptor used by the binding pass.
pub descriptor: CallableDescriptor,
}
/// One statically read plugin package.
///
/// The identity fields are recorded for `inspect --extensions`, evidence and
/// reconciliation; they are not consulted for name resolution (the binding
/// table is).
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct StaticPlugin {
/// Canonical manifest path.
pub manifest_path: PathBuf,
/// Package directory (artifact root).
pub base_dir: PathBuf,
/// Declared package name.
pub package: String,
/// Declared package version.
pub version: String,
/// Deployment kind (`native`, `worker`, `wasm`).
pub kind: PluginKind,
/// `sha256:<hex>` of the manifest file bytes.
pub manifest_sha256: String,
/// Target triple selected for this host.
pub artifact_target: String,
/// Declared (not yet verified) artifact sha256, lowercase hex.
pub artifact_sha256: String,
/// Bundle-relative artifact path.
pub artifact_relative: String,
/// Exports in manifest order.
pub exports: Vec<StaticPluginExport>,
/// Declared capability requests (metadata only; never grants).
pub requires: Vec<String>,
}
impl StaticPlugin {
/// Absolute artifact path inside the package bundle.
pub fn artifact_path(&self) -> PathBuf {
self.base_dir.join(&self.artifact_relative)
}
}
/// Static plugin selection read from explicit CLI `--plugin` flags.
#[derive(Debug, Clone, Default)]
pub struct StaticPluginSet {
/// Packages in deterministic manifest order.
pub plugins: Vec<StaticPlugin>,
}
impl StaticPluginSet {
/// All export descriptors in deterministic order.
pub fn descriptors(&self) -> Vec<CallableDescriptor> {
let mut descriptors = Vec::new();
for plugin in &self.plugins {
for export in &plugin.exports {
descriptors.push(export.descriptor.clone());
}
}
descriptors
}
}
/// Read and validate all explicitly selected `--plugin` manifests.
///
/// No binary is opened or executed. Duplicate package identities, duplicate
/// full export names, reserved names and malformed signatures are rejected
/// here, before any binding is produced.
pub fn read_plugin_descriptors(paths: &[PathBuf]) -> Result<StaticPluginSet, PrepareError> {
let mut set = StaticPluginSet::default();
let mut export_names: BTreeSet<String> = BTreeSet::new();
let mut packages: BTreeSet<(String, String)> = BTreeSet::new();
for path in paths {
let manifest_path = if path.is_dir() {
path.join("plugin.yaml")
} else {
path.clone()
};
let manifest_path = manifest_path.canonicalize().map_err(|error| {
PrepareError::option(
"ext.manifest_unreadable",
format!(
"cannot open plugin manifest `{}`: {error}",
manifest_path.display()
),
)
})?;
let base_dir = manifest_path
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."));
let base_dir = base_dir.canonicalize().unwrap_or(base_dir);
let manifest_bytes = std::fs::read(&manifest_path).map_err(|error| {
PrepareError::option(
"ext.manifest_unreadable",
format!(
"cannot read plugin manifest `{}`: {error}",
manifest_path.display()
),
)
})?;
let manifest = PluginManifest::load(&manifest_path)
.map_err(|error| PrepareError::option(error.code.clone(), error.message.clone()))?;
manifest
.validate()
.map_err(|error| PrepareError::option(error.code.clone(), error.message.clone()))?;
let host = HostTarget::current();
let artifact = manifest.resolve_artifact(&host).map_err(|error| {
PrepareError::option("ext.no_matching_artifact", error.message.clone())
})?;
let artifact_relative = artifact.file.clone();
ensure_bundle_relative(&artifact_relative).map_err(|error| {
PrepareError::option(
"ext.artifact_integrity",
format!(
"plugin `{}` artifact path `{artifact_relative}` is invalid: {error}",
manifest.plugin.name
),
)
})?;
let manifest_sha256 = format!("sha256:{}", hex(&Sha256::digest(&manifest_bytes)));
let identity = (manifest.plugin.name.clone(), manifest_sha256.clone());
if !packages.insert(identity.clone()) {
// The exact same package declaration twice is idempotent.
continue;
}
if set
.plugins
.iter()
.any(|plugin| plugin.package == identity.0)
{
return Err(PrepareError::option(
"ext.duplicate_callable",
format!(
"plugin package `{}` is selected with two different manifests; \
explicit aliases are not implemented, so this is rejected",
identity.0
),
));
}
let mut exports = Vec::new();
for export in &manifest.plugin.exports {
if !export_names.insert(export.name.clone()) {
return Err(PrepareError::option(
"ext.duplicate_callable",
format!(
"plugin export `{}` is declared by more than one selected package",
export.name
),
));
}
let signature = plugin_signature(&export.signature).map_err(|error| {
PrepareError::option(
"ext.plugin_invalid_signature",
format!(
"plugin export `{}` has an invalid signature: {error}",
export.name
),
)
})?;
let descriptor = CallableDescriptor {
id: CallableId(format!("plugin:{}:{}", manifest.plugin.name, export.name)),
public_name: export.name.clone(),
owner_package: manifest.plugin.name.clone(),
signature,
effects_requested: manifest.plugin.requires.clone(),
origin: CallableOrigin::AbiPlugin {
package: manifest.plugin.name.clone(),
export: export.name.clone(),
},
implementation_digest: Some(artifact.sha256.clone()),
source_span: None,
};
exports.push(StaticPluginExport {
name: export.name.clone(),
signature: export.signature.clone(),
descriptor,
});
}
set.plugins.push(StaticPlugin {
manifest_path,
base_dir,
package: manifest.plugin.name.clone(),
version: manifest.plugin.version.clone(),
kind: manifest.plugin.kind,
manifest_sha256,
artifact_target: host.triple().to_string(),
artifact_sha256: artifact.sha256.clone(),
artifact_relative,
exports,
requires: manifest.plugin.requires.clone(),
});
}
set.plugins.sort_by(|left, right| {
(&left.package, &left.manifest_sha256).cmp(&(&right.package, &right.manifest_sha256))
});
Ok(set)
}
/// Convert a plugin manifest signature (`($a: Int, $b: Int) -> Int`) to the
/// language parameter list.
///
/// ABI v1 transports named arguments as a map, so positional arguments are a
/// static-validated convenience: the binding pass normalises them to the
/// manifest order. Default values are rejected until the ABI wire semantics for
/// them are versioned (RFC-EXT-0001 §4.5.3); silently injecting defaults at run
/// time is forbidden.
pub fn plugin_signature(raw: &str) -> Result<CallableSignature, String> {
let before_return = raw.split("->").next().unwrap_or(raw).trim();
let open = before_return
.find('(')
.ok_or_else(|| "missing `(`".to_string())?;
let close = before_return
.rfind(')')
.ok_or_else(|| "missing `)`".to_string())?;
if close < open {
return Err("malformed parameter list".to_string());
}
let params = before_return[open + 1..close].replace('$', "");
let parsed = marqdo_frontend::signature::parse(&format!("({params})"), Span::default());
if parsed.has_errors() {
return Err(parsed
.diagnostics
.iter()
.map(|diagnostic| diagnostic.message.clone())
.collect::<Vec<_>>()
.join("; "));
}
if parsed.params.iter().any(|param| param.default.is_some()) {
return Err(
"ABI v1 plugin signatures must not declare default values; make parameters required"
.to_string(),
);
}
Ok(CallableSignature {
params: parsed.params,
kind: SignatureKind::Explicit,
})
}
/// Reject absolute paths, `..` components and empty paths.
pub fn ensure_bundle_relative(path: &str) -> Result<(), String> {
let mut normalised = PathBuf::new();
for component in Path::new(path).components() {
match component {
Component::Normal(part) => normalised.push(part),
Component::CurDir => {}
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
return Err("must be a relative path inside the package".to_string())
}
}
}
if normalised.as_os_str().is_empty() {
return Err("must not be empty".to_string());
}
Ok(())
}
/// Verify a bundle file is a regular file below `bundle_root`, travelling
/// through symlinks only if the final canonical target stays inside the bundle.
pub fn verify_bundle_regular_file(
bundle_root: &Path,
relative: &str,
) -> Result<PathBuf, PrepareError> {
ensure_bundle_relative(relative).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!("artifact path `{relative}` is invalid: {error}"),
)
})?;
let root = bundle_root.canonicalize().map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!(
"cannot resolve package root `{}`: {error}",
bundle_root.display()
),
)
})?;
let candidate = root.join(relative);
let canonical = candidate.canonicalize().map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!("cannot open artifact `{}`: {error}", candidate.display()),
)
})?;
if !canonical.starts_with(&root) {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"artifact `{relative}` resolves outside its package bundle (`{}`)",
canonical.display()
),
));
}
let metadata = std::fs::metadata(&canonical).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!("cannot stat artifact `{}`: {error}", canonical.display()),
)
})?;
if !metadata.is_file() {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!("artifact `{}` is not a regular file", canonical.display()),
));
}
Ok(canonical)
}
/// Verify a file's lowercase sha256 against a declared value.
pub fn verify_sha256(path: &Path, declared: &str) -> Result<(), PrepareError> {
let bytes = std::fs::read(path).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!("cannot read artifact `{}`: {error}", path.display()),
)
})?;
let actual = hex(&Sha256::digest(&bytes));
if actual != declared.to_ascii_lowercase() {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"artifact `{}` sha256 mismatch: declared {declared}, actual {actual}",
path.display()
),
));
}
Ok(())
}
fn hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for byte in bytes {
out.push_str(&format!("{byte:02x}"));
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plugin_signature_rejects_defaults_and_parses_positional() {
assert!(plugin_signature("($a: Int, $b: Int) -> Int").is_ok());
let error = plugin_signature("($a: Int = 1) -> Int").unwrap_err();
assert!(error.contains("default"), "{error}");
}
#[test]
fn bundle_relative_paths_reject_traversal() {
assert!(ensure_bundle_relative("lib/demo.so").is_ok());
assert!(ensure_bundle_relative("../demo.so").is_err());
assert!(ensure_bundle_relative("/etc/passwd").is_err());
assert!(ensure_bundle_relative("").is_err());
}
}
+171 -72
View File
@@ -14,7 +14,8 @@ use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use marqdo_expression::Expr;
use marqdo_frontend::{analyze_source, AnalysisResult};
use marqdo_frontend::AnalysisResult;
use marqdo_ir::callable::OfficialAlias;
use marqdo_ir::{ModuleId, PlaceTarget, Program, ResolvedImport, Stmt};
use marqdo_package::{
discover_root, LockFile, Manifest, PackageResolver, ResolvedPackage, LOCK_FILE, MANIFEST_FILE,
@@ -157,6 +158,11 @@ pub struct LoadedProject {
pub diagnostics: Vec<FileDiagnostic>,
/// Verified bundled stdlib identity, when one was available.
pub stdlib: Option<StdlibInfo>,
/// Official stdlib public aliases referenced by this program (ADR-0058).
///
/// These are ordinary `Marqdo` bindings; the runtime has no print special
/// case and no `Program.print_override`.
pub official_aliases: Vec<marqdo_ir::callable::OfficialAlias>,
}
impl LoadedProject {
@@ -280,32 +286,11 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
let entry_id = loader.load_module(&entry_module.package, &entry_module.relative_path)?;
loader.program.main_module = Some(entry_id.clone());
// Compat-B (ADR-0030): only programs that actually call 打印/print need the
// stdlib print module; injecting it for every program would change the
// module graph, analysis output and startup cost for no benefit.
if program_uses_print(&loader.program) {
if let Some(stdlib) = loader.stdlib.clone() {
let package = stdlib.package();
let io = loader.load_module(&package, "io.mq.md")?;
let Some(module) = loader.program.module(&io) else {
return Err("stdlib print module was not loaded".to_string());
};
let Some(function) = module.function("写值") else {
return Err("stdlib print module has no `写值` function".to_string());
};
loader.program.print_override = Some(function.id.clone());
}
}
for (module_id, diagnostic) in marqdo_frontend::calls::validate_program(&loader.program) {
if let Some(file) = loader.files.get(&module_id) {
loader.diagnostics.push(FileDiagnostic {
module_id,
path: file.path.clone(),
source: file.source.clone(),
diagnostic,
});
}
}
// Official stdlib public aliases (ADR-0058): the verified metadata names
// the target module/function; only aliases the program actually references
// load their module, and the runtime binds them as ordinary Marqdo calls.
let official_aliases = loader.load_official_aliases()?;
let stdlib_info = loader.stdlib.as_ref().map(|bundle| StdlibInfo {
version: bundle.version().to_string(),
digest: bundle.digest().to_string(),
@@ -328,6 +313,7 @@ pub fn load(entry: &Path) -> Result<LoadedProject, String> {
packages: loader.packages,
diagnostics: loader.diagnostics,
stdlib: stdlib_info,
official_aliases,
})
}
@@ -378,7 +364,7 @@ impl Loader {
.or_insert_with(|| package_root.clone());
let source = std::fs::read_to_string(&path)
.map_err(|error| format!("cannot read `{}`: {error}", path.display()))?;
let analysis = analyze_source(&module_id.to_string(), &source);
let analysis = marqdo_frontend::analyze_source_for_program(&module_id.to_string(), &source);
let imports: Vec<(String, String, marqdo_span::Span)> = analysis
.document
.frontmatter()
@@ -516,57 +502,170 @@ impl Loader {
}
Ok(module_id)
}
}
/// Whether the loaded program calls the legacy `打印`/`print` builtin.
fn program_uses_print(program: &Program) -> bool {
program.modules.values().any(|module| {
module
.functions
.iter()
.any(|function| stmts_use_print(&function.body))
})
}
fn stmts_use_print(stmts: &[Stmt]) -> bool {
stmts.iter().any(|stmt| match stmt {
Stmt::Assign { target, value, .. } => {
let target_uses = match target {
PlaceTarget::Local { .. } => false,
PlaceTarget::Index { base, index, .. } => {
expr_uses_print(base) || expr_uses_print(index)
}
/// Resolve the official stdlib public aliases referenced by the program.
///
/// Only the verified `stdlib.json` metadata can declare an official alias;
/// a third-party package cannot claim `打印`/`print` or `std.*`. Modules are
/// loaded only when the program references one of the alias names, so an
/// ordinary program does not grow its module graph or startup cost.
fn load_official_aliases(&mut self) -> Result<Vec<OfficialAlias>, String> {
let Some(stdlib) = self.stdlib.clone() else {
return Ok(Vec::new());
};
if stdlib.public_aliases().is_empty() {
return Ok(Vec::new());
}
let referenced = referenced_identifiers(&self.program);
let mut aliases = Vec::new();
for alias in stdlib.public_aliases() {
if !alias
.public_names
.iter()
.any(|name| referenced.contains(name))
{
continue;
}
let resolved = stdlib.resolve(&alias.target_module)?;
let module_id = self.load_module(&resolved.package, &resolved.relative_path)?;
let Some(module) = self.program.module(&module_id) else {
return Err(format!(
"stdlib public alias target module `{}` was not loaded",
alias.target_module
));
};
target_uses || expr_uses_print(value)
let Some(function) = module.function(&alias.target_function) else {
return Err(format!(
"stdlib public alias `{}` target `{}::{}` does not exist",
alias.public_names.join("/"),
alias.target_module,
alias.target_function
));
};
if let Some(expected) = &alias.expected_signature {
let parsed =
marqdo_frontend::signature::parse(expected, marqdo_span::Span::default());
if parsed.has_errors() {
return Err(format!(
"stdlib public alias `{}` has an invalid expected_signature `{expected}`",
alias.public_names.join("/")
));
}
let expected_names: Vec<&str> = parsed
.params
.iter()
.map(|param| param.name.as_str())
.collect();
let actual_names: Vec<&str> = function
.params
.iter()
.map(|param| param.name.as_str())
.collect();
if expected_names != actual_names
|| function.signature != marqdo_ir::SignatureKind::Explicit
{
return Err(format!(
"stdlib public alias `{}` expected signature `{expected}` does not match \
target `{}::{}` (params {:?}, kind {:?})",
alias.public_names.join("/"),
alias.target_module,
alias.target_function,
actual_names,
function.signature
));
}
}
aliases.push(OfficialAlias {
public_names: alias.public_names.clone(),
function: function.id.clone(),
signature: marqdo_ir::callable::CallableSignature {
params: function.params.clone(),
kind: function.signature,
},
owner_package: module_id.instance.clone(),
effects_requested: alias.effects.clone(),
});
}
Stmt::Eval { expr, .. } | Stmt::Return { value: expr, .. } => expr_uses_print(expr),
Stmt::Branch { arms, .. } => arms
.iter()
.any(|arm| expr_uses_print(&arm.condition) || stmts_use_print(&arm.body)),
Stmt::Loop {
condition, body, ..
} => expr_uses_print(condition) || stmts_use_print(body),
})
Ok(aliases)
}
}
/// Collect every identifier referenced by a program.
///
/// This is a conservative "is this alias used at all?" scan; it only decides
/// whether an official alias target module should be loaded. It does not decide
/// call targets (that is the single binding pass).
fn referenced_identifiers(program: &Program) -> std::collections::BTreeSet<String> {
let mut names = std::collections::BTreeSet::new();
for module in program.modules.values() {
for function in &module.functions {
for param in &function.params {
if let Some(default) = &param.default {
collect_identifiers_expr(default, &mut names);
}
}
collect_identifiers_stmts(&function.body, &mut names);
}
}
names
}
fn collect_identifiers_stmts(stmts: &[Stmt], names: &mut std::collections::BTreeSet<String>) {
for stmt in stmts {
match stmt {
Stmt::Assign { target, value, .. } => {
if let PlaceTarget::Index { base, index, .. } = target {
collect_identifiers_expr(base, names);
collect_identifiers_expr(index, names);
}
collect_identifiers_expr(value, names);
}
Stmt::Eval { expr, .. } | Stmt::Return { value: expr, .. } => {
collect_identifiers_expr(expr, names)
}
Stmt::Branch { arms, .. } => {
for arm in arms {
collect_identifiers_expr(&arm.condition, names);
collect_identifiers_stmts(&arm.body, names);
}
}
Stmt::Loop {
condition, body, ..
} => {
collect_identifiers_expr(condition, names);
collect_identifiers_stmts(body, names);
}
}
}
}
fn expr_uses_print(expr: &Expr) -> bool {
fn collect_identifiers_expr(expr: &Expr, names: &mut std::collections::BTreeSet<String>) {
match expr {
Expr::Ident(ident) => {
names.insert(ident.name.clone());
}
Expr::List(items) => items
.iter()
.for_each(|item| collect_identifiers_expr(item, names)),
Expr::Record(entries) => entries
.iter()
.for_each(|entry| collect_identifiers_expr(&entry.value, names)),
Expr::Unary { operand, .. } => collect_identifiers_expr(operand, names),
Expr::Binary { left, right, .. } => {
collect_identifiers_expr(left, names);
collect_identifiers_expr(right, names);
}
Expr::Member { base, .. } => collect_identifiers_expr(base, names),
Expr::Index { base, index, .. } => {
collect_identifiers_expr(base, names);
collect_identifiers_expr(index, names);
}
Expr::Call { callee, args, .. } => {
matches!(callee.as_ref(), Expr::Ident(ident) if matches!(ident.name.as_str(), "打印" | "print"))
|| expr_uses_print(callee)
|| args.iter().any(|arg| expr_uses_print(&arg.value))
collect_identifiers_expr(callee, names);
for arg in args {
collect_identifiers_expr(&arg.value, names);
}
}
Expr::List(items) => items.iter().any(expr_uses_print),
Expr::Record(entries) => entries.iter().any(|entry| expr_uses_print(&entry.value)),
Expr::Unary { operand, .. } => expr_uses_print(operand),
Expr::Binary { left, right, .. } => expr_uses_print(left) || expr_uses_print(right),
Expr::Member { base, .. } => expr_uses_print(base),
Expr::Index { base, index, .. } => expr_uses_print(base) || expr_uses_print(index),
Expr::Int(_)
| Expr::Float(_)
| Expr::Text(_)
| Expr::Bool(_)
| Expr::None
| Expr::Ident(_) => false,
Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => {}
}
}
+15
View File
@@ -7,8 +7,11 @@
mod analysis;
mod args;
mod commands;
mod extension_descriptors;
mod loader;
mod output;
mod project_prepare;
mod provider_manifest;
mod stdlib;
mod tool;
mod trace;
@@ -128,6 +131,16 @@ struct SandboxArgs {
/// Explicitly trusted plugin manifest/directory to load (repeatable).
#[arg(long = "plugin", value_name = "PATH")]
plugin: Vec<PathBuf>,
/// Explicitly selected resource provider v2 manifest (repeatable).
///
/// Selecting a provider never authorizes it; `--allow-provider NAME` is
/// required as well. `check`/`inspect` do not load providers.
#[arg(long = "provider", value_name = "PATH")]
provider: Vec<PathBuf>,
/// Grant one selected resource provider by its exact manifest name
/// (repeatable; wildcards are not supported).
#[arg(long = "allow-provider", value_name = "NAME")]
allow_provider: Vec<String>,
/// Write a versioned Trace v2 file for this run.
#[arg(long = "trace-file", value_name = "PATH")]
trace_file: Option<PathBuf>,
@@ -164,6 +177,8 @@ impl SandboxArgs {
max_region_memory: self.max_region_memory,
timeout: self.timeout,
plugins: self.plugin.clone(),
providers: self.provider.clone(),
allow_provider: self.allow_provider.clone(),
trust_native_plugin: self.trust_native_plugin,
trace_file: self.trace_file.clone(),
trace_max_events: self.trace_max_events,
+138
View File
@@ -0,0 +1,138 @@
//! The single `prepare` composition-root step (RFC-EXT-0001 §4.4, §18.2).
//!
//! Order of operations (T1–T5 of the RFC):
//!
//! 1. load the Markdown/package graph (read only, no execution);
//! 2. read static extension descriptors (never `dlopen`/spawn);
//! 3. build the unique [`CallableRegistry`] and reject static conflicts;
//! 4. run the single program-wide call binding pass;
//! 5. produce a [`PreparedProgram`] consumed by `check`, `run`, `call`,
//! `inspect` and Analysis.
//!
//! `run` may only assemble the Host and start workers after this returns.
use std::path::PathBuf;
use marqdo_ir::callable::CallableRegistry;
use crate::extension_descriptors::{read_plugin_descriptors, PrepareError, StaticPluginSet};
use crate::loader::{self, FileDiagnostic, LoadedProject};
/// Options that influence static preparation.
#[derive(Debug, Clone, Default)]
pub struct PrepareOptions {
/// Explicit `--plugin` manifests (repeatable, no implicit discovery).
pub plugins: Vec<PathBuf>,
}
/// A statically prepared program: everything `check`/`run`/`inspect` share.
pub struct PreparedProgram {
/// Loaded project graph with authoritative diagnostics.
pub loaded: LoadedProject,
/// Static plugin descriptors (identity only; no binary loaded).
pub plugins: StaticPluginSet,
/// Unique callable registry used by the binding pass.
///
/// Kept on the prepared value so later consumers (`inspect --extensions`,
/// evidence) can report the same static table without rebuilding it.
#[allow(dead_code)]
pub registry: CallableRegistry,
}
/// Prepare one document without executing any project or extension code.
pub fn prepare(
file: Option<&std::path::Path>,
options: &PrepareOptions,
) -> Result<PreparedProgram, PrepareError> {
// T1: static module/package loading. No call diagnostics are produced by
// the loader; the single binding pass below owns them.
let mut loaded = loader::load(file.unwrap_or_else(|| std::path::Path::new("index.mq.md")))
.map_err(|message| default_load_error(&message))?;
// T2: extension descriptors are read from manifests only.
let plugins = read_plugin_descriptors(&options.plugins)?;
// T3: unique static table. Duplicate exports/builtin shadowing are rejected
// before any call site can be bound.
for descriptor in plugins.descriptors() {
if loaded
.program
.external_callable(&descriptor.public_name)
.is_some()
{
return Err(PrepareError::option(
"ext.duplicate_callable",
format!(
"plugin export `{}` is declared more than once",
descriptor.public_name
),
));
}
if marqdo_ir::builtins::find(&descriptor.public_name).is_some() {
return Err(PrepareError::option(
"ext.duplicate_callable",
format!(
"plugin export `{}` conflicts with a builtin; plugins may not shadow builtins",
descriptor.public_name
),
));
}
loaded.program.external_callables.push(descriptor);
}
let mut registry = CallableRegistry::try_from_program(&loaded.program).map_err(|conflict| {
PrepareError::option(
"ext.duplicate_callable",
format!("static callable table conflict: {conflict}"),
)
})?;
// Official stdlib public aliases (ADR-0058) are ordinary Marqdo bindings.
for alias in &loaded.official_aliases {
registry
.insert_official_alias(alias.clone())
.map_err(|error| {
PrepareError::option(
"ext.duplicate_callable",
format!("stdlib public alias: {error}"),
)
})?;
}
// T4: exactly one call binding pass for the whole program.
let bound = marqdo_frontend::calls::bind_program_calls(&loaded.program, &registry);
for (module_id, diagnostic) in bound.diagnostics {
let Some(file) = loaded.files.get(&module_id) else {
continue;
};
let (path, source) = (file.path.clone(), file.source.clone());
let duplicate = loaded.diagnostics.iter().any(|existing| {
existing.module_id == module_id
&& existing.diagnostic.code == diagnostic.code
&& existing.diagnostic.span == diagnostic.span
&& existing.diagnostic.message == diagnostic.message
});
if !duplicate {
loaded.diagnostics.push(FileDiagnostic {
module_id,
path,
source,
diagnostic,
});
}
}
loaded.program.set_call_bindings(bound.bindings);
Ok(PreparedProgram {
loaded,
plugins,
registry,
})
}
fn default_load_error(message: &str) -> PrepareError {
if message.contains("marqdo.lock") || message.contains("lockfile") {
PrepareError::load("project.lock", message)
} else {
PrepareError::option("project.load", message)
}
}
+952
View File
@@ -0,0 +1,952 @@
//! Strict third-party Resource Provider v2 manifests for the CLI composition
//! root (RFC-EXT-0001 §5.3, ADR-0055).
//!
//! This module *reads* `provider.json` schema version 2 and verifies the
//! selected artifact. It never `dlopen`s a library, never spawns a helper and
//! never authorizes a provider: parsing a manifest is not a grant. The v1
//! `marqdo.provider` manifest stays a host-embedding path
//! (`StdHost::register_provider_package`, ADR-0037) and is rejected here with
//! `ext.unsupported_provider_schema`; there is no silent migration.
//!
//! The CLI only accepts the `c-abi-1` + `worker` transport combination and
//! resolves the helper as a sibling of the running `marqdo` executable; PATH
//! lookup is never used. Integrity hashes are content-integrity checks, not
//! publisher authentication.
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use marqdo_host::StdHost;
use marqdo_plugin_abi::{HostTarget, TargetTriple};
use serde::Deserialize;
use sha2::{Digest, Sha256};
use crate::extension_descriptors::{
ensure_bundle_relative, verify_bundle_regular_file, verify_sha256, PrepareError,
};
/// Provider manifest file name used by the CLI (`--provider` accepts this file).
pub const PROVIDER_MANIFEST_FILE: &str = "provider.json";
/// Provider manifest schema name.
pub const PROVIDER_SCHEMA: &str = "marqdo.provider";
/// Provider manifest schema version accepted by the CLI.
pub const PROVIDER_SCHEMA_VERSION: u64 = 2;
/// Provider ABI accepted by the CLI (ADR-0036/0038).
pub const PROVIDER_ABI: &str = "c-abi-1";
/// Provider transport accepted by the CLI (ADR-0035/0038).
pub const PROVIDER_TRANSPORT: &str = "worker";
/// Helper executable shipped next to the `marqdo` binary (ADR-0038).
pub const PROVIDER_WORKER_BINARY: &str = "marqdo-provider-worker";
/// Upper bounds for a single provider manifest; these bound parser work only.
pub const MAX_PROVIDER_NAME_BYTES: usize = 128;
pub const MAX_PROVIDER_KINDS: usize = 64;
pub const MAX_PROVIDER_ARTIFACTS: usize = 32;
pub const MAX_PROVIDER_PERMISSIONS: usize = 64;
/// Maximum selected artifact size (256 MiB).
pub const MAX_PROVIDER_ARTIFACT_BYTES: u64 = 256 * 1024 * 1024;
/// Maximum manifest size (1 MiB); a manifest is metadata, not an artifact.
pub const MAX_PROVIDER_MANIFEST_BYTES: u64 = 1024 * 1024;
/// Core resource kinds owned by the host; third parties may never override
/// them (RFC-EXT-0001 §5.3.1). `stdio`/`file`/`tcp`/`tls`/`udp`/`tcp-listener`
/// are dispatched by `marqdo-host` before the provider registry is consulted.
pub const RESERVED_RESOURCE_KINDS: &[&str] =
&["stdio", "file", "tcp", "tls", "udp", "tcp-listener"];
/// Raw `provider.json` schema version 2 document.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ProviderManifest {
schema: String,
schema_version: u64,
name: String,
version: String,
abi: String,
transport: String,
kinds: Vec<String>,
artifacts: Vec<ManifestArtifact>,
#[serde(default)]
permissions_requested: Vec<String>,
/// Free-form, non-executable metadata. Ignored for execution and never used
/// to select artifacts or grant permissions.
#[serde(rename = "metadata", default)]
_metadata: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ManifestArtifact {
target: String,
path: String,
sha256: String,
}
/// A manifest whose selected artifact has been resolved and verified.
///
/// Producing one requires reading the manifest, checking the schema rules,
/// selecting the artifact for the current host target and verifying the
/// content hash. It still does not grant access and no worker has been started.
#[derive(Debug, Clone)]
pub struct VerifiedProvider {
/// Stable grant name (`--allow-provider NAME` must match exactly).
pub name: String,
/// Strict SemVer version declared by the manifest.
pub version: String,
/// Non-reserved resource kinds served by this provider.
pub kinds: Vec<String>,
/// Canonical manifest path.
pub manifest_path: PathBuf,
/// Canonical bundle directory (manifest parent).
pub base_dir: PathBuf,
/// `sha256:<hex>` of the manifest bytes at verification time.
pub manifest_digest: String,
/// Selected transport (`worker` in v2).
pub transport: String,
/// Canonical path of the selected, hash-verified artifact.
pub selected_artifact_path: PathBuf,
/// Bundle-relative artifact path exactly as declared.
pub selected_artifact_relative: String,
/// Lowercase hex sha256 declared by the manifest.
pub selected_artifact_sha256: String,
/// Target triple the selected artifact was built for.
pub selected_target: String,
}
impl VerifiedProvider {
/// Re-verify the manifest bytes and the selected artifact immediately
/// before the provider worker is spawned.
///
/// `run` must never trust a hash it checked earlier: another process can
/// replace the manifest or the artifact between selection and use.
pub fn reverify(&self) -> Result<(), PrepareError> {
let bytes = std::fs::read(&self.manifest_path).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!(
"provider manifest `{}` could not be re-read before use: {error}",
self.manifest_path.display()
),
)
})?;
if bytes.len() as u64 > MAX_PROVIDER_MANIFEST_BYTES {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"provider manifest `{}` grew beyond the {MAX_PROVIDER_MANIFEST_BYTES}-byte limit before use",
self.manifest_path.display()
),
));
}
let digest = manifest_digest(&bytes);
if digest != self.manifest_digest {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"provider manifest `{}` changed after verification (expected {}, actual {}); refusing to start its worker",
self.manifest_path.display(),
self.manifest_digest,
digest
),
));
}
let artifact =
verify_bundle_regular_file(&self.base_dir, &self.selected_artifact_relative)?;
if artifact != self.selected_artifact_path {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"provider `{}` artifact `{}` now resolves to `{}` instead of the verified `{}`",
self.name,
self.selected_artifact_relative,
artifact.display(),
self.selected_artifact_path.display()
),
));
}
check_artifact_size(&artifact)?;
verify_sha256(&artifact, &self.selected_artifact_sha256)?;
Ok(())
}
}
/// Read, validate and verify every explicitly selected `--provider` manifest.
///
/// No worker is spawned and no artifact is executed. Duplicate provider names
/// and duplicate resource kinds across providers are rejected deterministically
/// (the returned list is sorted by provider name) so a later registration can
/// never overwrite an earlier one.
pub fn read_provider_manifests(paths: &[PathBuf]) -> Result<Vec<VerifiedProvider>, PrepareError> {
let mut manifest_paths: BTreeSet<PathBuf> = BTreeSet::new();
for path in paths {
if path.is_dir() {
return Err(PrepareError::option(
"ext.manifest_unreadable",
format!(
"--provider `{}` must name a `{PROVIDER_MANIFEST_FILE}` file, not a directory",
path.display()
),
));
}
let canonical = path.canonicalize().map_err(|error| {
PrepareError::option(
"ext.manifest_unreadable",
format!(
"cannot open provider manifest `{}`: {error}",
path.display()
),
)
})?;
manifest_paths.insert(canonical);
}
let mut providers = Vec::with_capacity(manifest_paths.len());
for path in manifest_paths {
providers.push(read_provider_manifest(&path)?);
}
// Deterministic conflict resolution independent of CLI argument order.
providers.sort_by(|left, right| {
(&left.name, &left.manifest_digest).cmp(&(&right.name, &right.manifest_digest))
});
let mut unique: Vec<VerifiedProvider> = Vec::with_capacity(providers.len());
for provider in providers {
if let Some(existing) = unique.iter().find(|item| item.name == provider.name) {
if existing.manifest_digest != provider.manifest_digest {
return Err(PrepareError::option(
"ext.duplicate_provider",
format!(
"provider name `{}` is selected with two different manifests (`{}` and `{}`); \
explicit aliases are not implemented, so this is rejected",
provider.name,
existing.manifest_path.display(),
provider.manifest_path.display()
),
));
}
// The exact same declaration selected twice is idempotent.
continue;
}
unique.push(provider);
}
let mut claimed_kinds: BTreeMap<String, String> = BTreeMap::new();
for provider in &unique {
for kind in &provider.kinds {
if let Some(owner) = claimed_kinds.get(kind) {
return Err(PrepareError::option(
"ext.duplicate_provider_kind",
format!(
"resource kind `{kind}` is declared by both provider `{owner}` and provider `{}`; \
duplicate kinds are rejected before any worker starts",
provider.name
),
));
}
claimed_kinds.insert(kind.clone(), provider.name.clone());
}
}
Ok(unique)
}
fn read_provider_manifest(canonical_path: &Path) -> Result<VerifiedProvider, PrepareError> {
let metadata = std::fs::metadata(canonical_path).map_err(|error| {
PrepareError::option(
"ext.manifest_unreadable",
format!(
"cannot stat provider manifest `{}`: {error}",
canonical_path.display()
),
)
})?;
if metadata.len() > MAX_PROVIDER_MANIFEST_BYTES {
return Err(PrepareError::option(
"ext.manifest_invalid",
format!(
"provider manifest `{}` is {} bytes, larger than the {MAX_PROVIDER_MANIFEST_BYTES}-byte limit",
canonical_path.display(),
metadata.len()
),
));
}
let bytes = std::fs::read(canonical_path).map_err(|error| {
PrepareError::option(
"ext.manifest_unreadable",
format!(
"cannot read provider manifest `{}`: {error}",
canonical_path.display()
),
)
})?;
let digest = manifest_digest(&bytes);
let value: serde_json::Value = serde_json::from_slice(&bytes).map_err(|error| {
PrepareError::option(
"ext.manifest_invalid",
format!(
"provider manifest `{}` is not valid UTF-8 JSON: {error}",
canonical_path.display()
),
)
})?;
// Schema/version gate runs before structural parsing so a v1 (or future)
// manifest gets the stable `ext.unsupported_provider_schema` code instead
// of a generic field error.
let schema = value.get("schema").and_then(serde_json::Value::as_str);
if schema != Some(PROVIDER_SCHEMA) {
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider manifest `{}` does not declare `schema: \"{PROVIDER_SCHEMA}\"`; \
the CLI accepts schema version {PROVIDER_SCHEMA_VERSION} only",
canonical_path.display()
),
));
}
let schema_version = value
.get("schema_version")
.and_then(serde_json::Value::as_u64);
if schema_version != Some(PROVIDER_SCHEMA_VERSION) {
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider manifest `{}` declares schema_version {}; the CLI accepts {PROVIDER_SCHEMA_VERSION} only \
(v1 and future versions are rejected, there is no silent migration)",
canonical_path.display(),
schema_version
.map(|version| version.to_string())
.unwrap_or_else(|| "<missing>".to_string())
),
));
}
let manifest: ProviderManifest = serde_json::from_value(value).map_err(|error| {
PrepareError::option(
"ext.manifest_invalid",
format!(
"provider manifest `{}` is not a valid {PROVIDER_SCHEMA} v{PROVIDER_SCHEMA_VERSION} document: {error}",
canonical_path.display()
),
)
})?;
if manifest.schema != PROVIDER_SCHEMA || manifest.schema_version != PROVIDER_SCHEMA_VERSION {
// Unreachable after the Value-level gate; kept fail-closed.
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider manifest `{}` uses an unsupported schema",
canonical_path.display()
),
));
}
validate_manifest(&manifest, canonical_path)?;
let base_dir = canonical_path
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| PathBuf::from("."));
let selected_target = HostTarget::current().triple().to_string();
let artifact = manifest
.artifacts
.iter()
.find(|artifact| artifact.target == selected_target)
.ok_or_else(|| {
PrepareError::option(
"ext.no_matching_artifact",
format!(
"provider `{}` has no artifact for the current target `{selected_target}`; \
other platforms are never substituted",
manifest.name
),
)
})?;
let artifact_relative = normalise_artifact_path(&manifest.name, &artifact.path)?;
let artifact_path = verify_bundle_regular_file(&base_dir, &artifact_relative)?;
check_artifact_size(&artifact_path)?;
verify_sha256(&artifact_path, &artifact.sha256)?;
Ok(VerifiedProvider {
name: manifest.name,
version: manifest.version,
kinds: manifest.kinds,
manifest_path: canonical_path.to_path_buf(),
base_dir,
manifest_digest: digest,
transport: manifest.transport,
selected_artifact_path: artifact_path,
selected_artifact_relative: artifact_relative,
selected_artifact_sha256: artifact.sha256.to_ascii_lowercase(),
selected_target,
})
}
fn validate_manifest(
manifest: &ProviderManifest,
manifest_path: &Path,
) -> Result<(), PrepareError> {
let invalid = |message: String| {
PrepareError::option(
"ext.manifest_invalid",
format!("provider manifest `{}`: {message}", manifest_path.display()),
)
};
validate_provider_name(&manifest.name)
.map_err(|error| invalid(format!("invalid provider name: {error}")))?;
if RESERVED_RESOURCE_KINDS.contains(&manifest.name.as_str()) {
return Err(invalid(format!(
"provider name `{}` is a reserved core resource kind",
manifest.name
)));
}
semver::Version::parse(&manifest.version).map_err(|error| {
invalid(format!(
"version `{}` is not strict SemVer: {error}",
manifest.version
))
})?;
if manifest.abi != PROVIDER_ABI {
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider `{}` declares abi `{}`; schema v{PROVIDER_SCHEMA_VERSION} accepts `{PROVIDER_ABI}` only",
manifest.name, manifest.abi
),
));
}
if manifest.transport != PROVIDER_TRANSPORT {
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider `{}` declares transport `{}`; schema v{PROVIDER_SCHEMA_VERSION} accepts `{PROVIDER_TRANSPORT}` only",
manifest.name, manifest.transport
),
));
}
if manifest.kinds.is_empty() {
return Err(invalid(
"kinds must declare at least one resource kind".to_string(),
));
}
if manifest.kinds.len() > MAX_PROVIDER_KINDS {
return Err(invalid(format!(
"kinds declares more than {MAX_PROVIDER_KINDS} entries"
)));
}
let mut kinds = BTreeSet::new();
for kind in &manifest.kinds {
validate_provider_name(kind)
.map_err(|error| invalid(format!("invalid resource kind `{kind}`: {error}")))?;
if RESERVED_RESOURCE_KINDS.contains(&kind.as_str()) {
return Err(invalid(format!(
"resource kind `{kind}` is reserved by the host and cannot be overridden by a provider"
)));
}
if !kinds.insert(kind.clone()) {
return Err(invalid(format!(
"resource kind `{kind}` is declared more than once"
)));
}
}
if manifest.artifacts.is_empty() {
return Err(invalid(
"artifacts must declare at least one target artifact".to_string(),
));
}
if manifest.artifacts.len() > MAX_PROVIDER_ARTIFACTS {
return Err(invalid(format!(
"artifacts declares more than {MAX_PROVIDER_ARTIFACTS} entries"
)));
}
let mut targets = BTreeSet::new();
for artifact in &manifest.artifacts {
let triple = TargetTriple::parse(&artifact.target).map_err(|error| {
invalid(format!(
"artifact target `{}` is not a target triple: {error}",
artifact.target
))
})?;
if !targets.insert(triple.to_string()) {
return Err(invalid(format!(
"artifact target `{triple}` is declared more than once"
)));
}
if !is_lowercase_sha256(&artifact.sha256) {
return Err(invalid(format!(
"artifact for target `{:?}` must declare a 64-character lowercase hex sha256",
artifact.target
)));
}
}
if manifest.permissions_requested.len() > MAX_PROVIDER_PERMISSIONS {
return Err(invalid(format!(
"permissions_requested declares more than {MAX_PROVIDER_PERMISSIONS} entries"
)));
}
let mut permissions = BTreeSet::new();
for permission in &manifest.permissions_requested {
if !is_known_capability(permission) && !is_vendor_capability(permission) {
return Err(invalid(format!(
"permissions_requested entry `{permission}` is not a known capability or a dotted vendor capability"
)));
}
if !permissions.insert(permission.clone()) {
return Err(invalid(format!(
"permission `{permission}` is declared more than once"
)));
}
}
// `metadata` is intentionally not validated: it has no execution meaning.
Ok(())
}
/// Validate a dotted provider/kind name using the plugin name grammar
/// (ASCII lowercase segment start, then lowercase/digits/`-`/`_`).
pub fn validate_provider_name(name: &str) -> Result<(), String> {
if name.is_empty() {
return Err("must not be empty".to_string());
}
if name.len() > MAX_PROVIDER_NAME_BYTES {
return Err(format!(
"must not be longer than {MAX_PROVIDER_NAME_BYTES} bytes"
));
}
for segment in name.split('.') {
if segment.is_empty() {
return Err(format!("`{name}` contains an empty segment"));
}
let mut bytes = segment.bytes();
let first = bytes.next().unwrap_or(b'a');
if !first.is_ascii_lowercase() {
return Err(format!(
"`{name}` must start every `.`-separated segment with a lowercase ASCII letter"
));
}
if !bytes.all(|byte| {
byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-' || byte == b'_'
}) {
return Err(format!(
"`{name}` may only contain lowercase ASCII letters, digits, `-` and `_`"
));
}
}
Ok(())
}
/// Reject absolute paths and `..` traversal in declared artifact paths.
fn normalise_artifact_path(provider: &str, path: &str) -> Result<String, PrepareError> {
ensure_bundle_relative(path).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!("provider `{provider}` artifact path `{path}` is invalid: {error}"),
)
})?;
Ok(path.to_string())
}
fn check_artifact_size(path: &Path) -> Result<(), PrepareError> {
let metadata = std::fs::metadata(path).map_err(|error| {
PrepareError::load(
"ext.artifact_integrity",
format!(
"cannot stat provider artifact `{}`: {error}",
path.display()
),
)
})?;
if metadata.len() > MAX_PROVIDER_ARTIFACT_BYTES {
return Err(PrepareError::load(
"ext.artifact_integrity",
format!(
"provider artifact `{}` is {} bytes, larger than the {MAX_PROVIDER_ARTIFACT_BYTES}-byte limit",
path.display(),
metadata.len()
),
));
}
Ok(())
}
fn is_lowercase_sha256(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}
fn is_known_capability(name: &str) -> bool {
marqdo_plugin_abi::KNOWN_CAPABILITIES.contains(&name)
}
/// A dotted vendor capability proposal (validation only, never a grant).
fn is_vendor_capability(name: &str) -> bool {
let segments: Vec<&str> = name.split('.').collect();
if segments.len() < 3 {
return false;
}
if matches!(
segments[0],
"console" | "math" | "fs" | "net" | "process" | "log" | "clock" | "random"
) {
return false;
}
segments.iter().all(|segment| {
!segment.is_empty()
&& segment.bytes().enumerate().all(|(index, byte)| {
byte.is_ascii_lowercase() || byte.is_ascii_digit() || (byte == b'-' && index > 0)
})
})
}
fn manifest_digest(bytes: &[u8]) -> String {
let mut hasher = Sha256::new();
hasher.update(bytes);
let digest = hasher.finalize();
let mut hex = String::with_capacity(digest.len() * 2);
for byte in digest {
hex.push_str(&format!("{byte:02x}"));
}
format!("sha256:{hex}")
}
/// Whether the provider `worker` transport is supported on this build.
///
/// RFC-EXT-0001 §5.6 makes Linux x86_64 the mandatory lane. Other targets stay
/// compilable but must fail closed with `ext.platform_unsupported` instead of
/// pretending a helper works.
pub fn provider_worker_platform_supported() -> bool {
cfg!(all(target_os = "linux", target_arch = "x86_64"))
}
/// Resolve the provider helper shipped next to the running `marqdo` executable.
///
/// Resolution is *only* the sibling file: there is no PATH lookup and no
/// directory search (RFC-EXT-0001 §5.4.5). A missing helper is
/// `ext.worker_unavailable`, never a fallback to in-process native loading.
pub fn provider_worker_executable() -> Result<PathBuf, PrepareError> {
if !provider_worker_platform_supported() {
return Err(PrepareError::load(
"ext.platform_unsupported",
format!(
"the resource provider worker transport is supported on linux/x86_64 only in this release; \
this build targets {}/{}",
std::env::consts::OS,
std::env::consts::ARCH
),
));
}
let executable = std::env::current_exe().map_err(|error| {
PrepareError::load(
"ext.worker_unavailable",
format!(
"cannot locate the marqdo executable to find `{PROVIDER_WORKER_BINARY}`: {error}"
),
)
})?;
let directory = executable.parent().ok_or_else(|| {
PrepareError::load(
"ext.worker_unavailable",
format!(
"the marqdo executable `{}` has no parent directory",
executable.display()
),
)
})?;
let helper = directory.join(format!(
"{PROVIDER_WORKER_BINARY}{}",
std::env::consts::EXE_SUFFIX
));
match std::fs::metadata(&helper) {
Ok(metadata) if metadata.is_file() => Ok(helper),
_ => Err(PrepareError::load(
"ext.worker_unavailable",
format!(
"the resource provider worker helper `{}` is missing; it must be shipped next to the \
marqdo executable (PATH lookup is not allowed)",
helper.display()
),
)),
}
}
/// Check the exact `--allow-provider` grant, re-verify the manifest and
/// artifact, then register the existing worker resource-provider transport.
///
/// The grant check is repeated here (it was already checked during pre-spawn
/// verification) so no caller can register an ungranted provider. The provider
/// is always registered through
/// [`StdHost::register_worker_resource_provider`]; this path never falls back
/// to the trusted in-process C ABI or a native library.
pub fn load_resource_provider_into_host(
host: &mut StdHost,
provider: &VerifiedProvider,
allow_provider: &[String],
) -> Result<(), PrepareError> {
if !allow_provider.iter().any(|grant| grant == &provider.name) {
return Err(PrepareError::load(
"permission.denied",
format!(
"resource provider `{}` was selected but not granted; add --allow-provider {}",
provider.name, provider.name
),
));
}
if provider.transport != PROVIDER_TRANSPORT {
return Err(PrepareError::option(
"ext.unsupported_provider_schema",
format!(
"provider `{}` transport `{}` cannot be loaded by the CLI",
provider.name, provider.transport
),
));
}
// Re-verify immediately before use; never trust an earlier hash.
provider.reverify()?;
let worker = provider_worker_executable()?;
let kinds: Vec<&str> = provider.kinds.iter().map(String::as_str).collect();
let args = vec![provider
.selected_artifact_path
.to_string_lossy()
.into_owned()];
host.register_worker_resource_provider(&provider.name, &kinds, &worker, &args)
.map_err(|error| {
PrepareError::load(
"ext.worker_unavailable",
format!(
"cannot start the worker for resource provider `{}` v{} (target {}) using `{}`: {}",
provider.name,
provider.version,
provider.selected_target,
worker.display(),
error.message
),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
fn tempdir(tag: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!(
"marqdo-provider-manifest-{}-{tag}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_nanos())
.unwrap_or(0)
));
std::fs::create_dir_all(&path).unwrap();
path
}
fn write_manifest(dir: &Path, manifest: &str) -> PathBuf {
let path = dir.join(PROVIDER_MANIFEST_FILE);
std::fs::write(&path, manifest).unwrap();
path
}
fn artifact(dir: &Path, name: &str, bytes: &[u8]) -> (String, String) {
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
let mut hasher = Sha256::new();
hasher.update(bytes);
let mut hex = String::new();
for byte in hasher.finalize() {
hex.push_str(&format!("{byte:02x}"));
}
(name.to_string(), hex)
}
fn manifest_text(name: &str, kinds: &[&str], path: &str, sha: &str) -> String {
let kinds = kinds
.iter()
.map(|kind| format!("\"{kind}\""))
.collect::<Vec<_>>()
.join(", ");
format!(
r#"{{
"schema": "marqdo.provider",
"schema_version": 2,
"name": "{name}",
"version": "0.1.0",
"abi": "c-abi-1",
"transport": "worker",
"kinds": [{kinds}],
"artifacts": [
{{"target": "{}", "path": "{path}", "sha256": "{sha}"}}
],
"permissions_requested": []
}}"#,
HostTarget::current().triple()
)
}
#[test]
fn valid_v2_manifest_verifies_artifact() {
let dir = tempdir("valid");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let manifest = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &sha),
);
let providers = read_provider_manifests(&[manifest]).unwrap();
assert_eq!(providers.len(), 1);
assert_eq!(providers[0].name, "demo.sensor");
assert_eq!(
providers[0].selected_target,
HostTarget::current().triple().to_string()
);
assert!(providers[0].manifest_digest.starts_with("sha256:"));
providers[0].reverify().unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn v1_manifest_is_rejected_without_migration() {
let dir = tempdir("v1");
let manifest = write_manifest(
&dir,
r#"{"schema":"marqdo.provider","schema_version":1,"name":"cabi","kinds":["cabi.sensor"],"abi":"c-abi-1","library":{"linux":"libcabi.so"}}"#,
);
let error = read_provider_manifests(&[manifest]).unwrap_err();
assert_eq!(error.code, "ext.unsupported_provider_schema");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn missing_target_is_no_matching_artifact() {
let dir = tempdir("target");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let manifest = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &sha).replace(
&HostTarget::current().triple().to_string(),
"aarch64-unknown-linux-gnu",
),
);
let error = read_provider_manifests(&[manifest]).unwrap_err();
assert_eq!(error.code, "ext.no_matching_artifact");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn reserved_kind_is_rejected() {
let dir = tempdir("reserved");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let manifest = write_manifest(&dir, &manifest_text("demo.sensor", &["file"], &path, &sha));
let error = read_provider_manifests(&[manifest]).unwrap_err();
assert_eq!(error.code, "ext.manifest_invalid");
assert!(error.message.contains("reserved"), "{}", error.message);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn traversal_and_digest_mismatch_are_artifact_integrity() {
let dir = tempdir("integrity");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let traversal = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], "../libdemo.so", &sha),
);
let error = read_provider_manifests(&[traversal]).unwrap_err();
assert_eq!(error.code, "ext.artifact_integrity");
let mismatch = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &"0".repeat(64)),
);
let error = read_provider_manifests(&[mismatch]).unwrap_err();
assert_eq!(error.code, "ext.artifact_integrity");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn duplicate_kinds_across_providers_are_rejected() {
let dir = tempdir("duplicate-kind");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let first = write_manifest(
&dir,
&manifest_text("demo.one", &["demo.sensor"], &path, &sha),
);
let second_dir = dir.join("second");
std::fs::create_dir_all(&second_dir).unwrap();
std::fs::copy(dir.join(&path), second_dir.join(&path)).unwrap();
let second = write_manifest(
&second_dir,
&manifest_text("demo.two", &["demo.sensor"], &path, &sha),
);
let error = read_provider_manifests(&[first, second]).unwrap_err();
assert_eq!(error.code, "ext.duplicate_provider_kind");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn duplicate_provider_name_with_different_manifests_is_rejected() {
let dir = tempdir("duplicate-name");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let first = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &sha),
);
let second_dir = dir.join("second");
std::fs::create_dir_all(&second_dir).unwrap();
std::fs::copy(dir.join(&path), second_dir.join(&path)).unwrap();
// Same name, different version => different manifest digest.
let second = write_manifest(
&second_dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &sha)
.replace("\"version\": \"0.1.0\"", "\"version\": \"0.2.0\""),
);
let error = read_provider_manifests(&[first.clone(), second]).unwrap_err();
assert_eq!(error.code, "ext.duplicate_provider");
// Selecting the exact same manifest twice is idempotent.
let providers = read_provider_manifests(&[first.clone(), first]).unwrap();
assert_eq!(providers.len(), 1);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn unknown_fields_and_bad_names_are_manifest_invalid() {
let dir = tempdir("invalid");
let (path, sha) = artifact(&dir, "libdemo.so", b"provider-bytes");
let unknown = write_manifest(
&dir,
&manifest_text("demo.sensor", &["demo.sensor"], &path, &sha).replace(
"\"permissions_requested\": []",
"\"permissions_requested\": [], \"exec\": true",
),
);
let error = read_provider_manifests(&[unknown]).unwrap_err();
assert_eq!(error.code, "ext.manifest_invalid");
let bad_name = write_manifest(
&dir,
&manifest_text("Demo.Sensor", &["demo.sensor"], &path, &sha),
);
let error = read_provider_manifests(&[bad_name]).unwrap_err();
assert_eq!(error.code, "ext.manifest_invalid");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn worker_helper_resolution_only_uses_the_executable_sibling() {
// The real helper is installed next to the CLI binary by the
// integration fixture; this test only asserts the platform/absence
// contract and that a path is never derived from PATH.
if provider_worker_platform_supported() {
let helper = provider_worker_executable();
// In a unit-test binary `current_exe()` is the test binary, so the
// sibling helper is normally absent: the error must be structured.
match helper {
Ok(path) => assert!(path.is_file()),
Err(error) => assert_eq!(error.code, "ext.worker_unavailable"),
}
} else {
let error = provider_worker_executable().unwrap_err();
assert_eq!(error.code, "ext.platform_unsupported");
}
}
}
+209
View File
@@ -13,6 +13,25 @@ use sha2::{Digest, Sha256};
/// Environment override used by tests and relocatable installations.
pub const STDLIB_DIR_ENV: &str = "MARQDO_STDLIB_DIR";
/// One official bundled-stdlib public alias (ADR-0058).
///
/// The alias table lives in the verified `stdlib.json` metadata, not in Rust
/// source: `打印`/`print` are ordinary `Marqdo` bindings to `io.mq.md::写值`
/// and the runtime no longer special-cases print names.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublicAlias {
/// Public names that resolve to the target function.
pub public_names: Vec<String>,
/// Package-relative target module (for example `io.mq.md`).
pub target_module: String,
/// Canonical/local target function name.
pub target_function: String,
/// Declared parameter names, used to verify the target signature.
pub expected_signature: Option<String>,
/// Conservative effect request summary.
pub effects: Vec<String>,
}
/// Verified bundled standard library.
#[derive(Debug, Clone)]
pub struct StdlibBundle {
@@ -20,6 +39,7 @@ pub struct StdlibBundle {
name: String,
version: String,
digest: String,
aliases: Vec<PublicAlias>,
}
impl StdlibBundle {
@@ -81,14 +101,42 @@ impl StdlibBundle {
"stdlib digest mismatch: declared {declared}, actual {actual}"
));
}
let aliases = parse_public_aliases(&manifest, &manifest_path)?;
let declared_alias_digest = manifest
.get("aliases_digest")
.and_then(serde_json::Value::as_str);
let actual_alias_digest = aliases_digest(&aliases);
match (declared_alias_digest, aliases.is_empty()) {
(Some(declared), _) => {
if declared != actual_alias_digest {
return Err(format!(
"stdlib public-alias metadata digest mismatch: declared {declared}, \
actual {actual_alias_digest}"
));
}
}
(None, false) => {
return Err(format!(
"`{}` declares public_aliases without aliases_digest",
manifest_path.display()
));
}
(None, true) => {}
}
Ok(Self {
root,
name,
version,
digest: actual,
aliases,
})
}
/// Verified official public aliases.
pub fn public_aliases(&self) -> &[PublicAlias] {
&self.aliases
}
/// Bundle root.
pub fn root(&self) -> &Path {
&self.root
@@ -227,6 +275,167 @@ fn hex(bytes: &[u8]) -> String {
out
}
fn parse_public_aliases(
manifest: &serde_json::Value,
manifest_path: &Path,
) -> Result<Vec<PublicAlias>, String> {
let Some(entries) = manifest.get("public_aliases") else {
return Ok(Vec::new());
};
let entries = entries.as_array().ok_or_else(|| {
format!(
"`{}` public_aliases must be an array",
manifest_path.display()
)
})?;
let mut aliases = Vec::new();
let mut seen_names = std::collections::BTreeSet::new();
for entry in entries {
let public_names = entry
.get("public_names")
.and_then(serde_json::Value::as_array)
.ok_or_else(|| {
format!(
"`{}` public alias has no public_names array",
manifest_path.display()
)
})?
.iter()
.map(|name| {
name.as_str().map(ToString::to_string).ok_or_else(|| {
format!(
"`{}` public alias name must be a string",
manifest_path.display()
)
})
})
.collect::<Result<Vec<_>, _>>()?;
if public_names.is_empty() {
return Err(format!(
"`{}` public alias declares no names",
manifest_path.display()
));
}
for name in &public_names {
if name.is_empty() {
return Err(format!(
"`{}` public alias name must not be empty",
manifest_path.display()
));
}
if !seen_names.insert(name.clone()) {
return Err(format!(
"`{}` public alias name `{name}` is declared more than once",
manifest_path.display()
));
}
}
let target_module = entry
.get("target_module")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| {
format!(
"`{}` public alias has no target_module",
manifest_path.display()
)
})?
.to_string();
validate_bundle_relative(&target_module).map_err(|error| {
format!(
"`{}` public alias target_module `{target_module}` is invalid: {error}",
manifest_path.display()
)
})?;
let target_function = entry
.get("target_function")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| {
format!(
"`{}` public alias has no target_function",
manifest_path.display()
)
})?
.to_string();
if target_function.is_empty() {
return Err(format!(
"`{}` public alias target_function must not be empty",
manifest_path.display()
));
}
let expected_signature = entry
.get("expected_signature")
.and_then(serde_json::Value::as_str)
.map(ToString::to_string);
let effects = entry
.get("effects")
.and_then(serde_json::Value::as_array)
.map(|effects| {
effects
.iter()
.filter_map(serde_json::Value::as_str)
.map(ToString::to_string)
.collect()
})
.unwrap_or_default();
aliases.push(PublicAlias {
public_names,
target_module,
target_function,
expected_signature,
effects,
});
}
aliases.sort_by(|left, right| {
(&left.target_module, &left.target_function)
.cmp(&(&right.target_module, &right.target_function))
});
Ok(aliases)
}
fn validate_bundle_relative(path: &str) -> Result<(), String> {
let mut normalised = PathBuf::new();
for component in Path::new(path).components() {
match component {
Component::Normal(part) => normalised.push(part),
Component::CurDir => {}
Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
return Err("must be a relative path inside the bundle".to_string())
}
}
}
if normalised.as_os_str().is_empty() {
return Err("must not be empty".to_string());
}
Ok(())
}
/// Canonical digest over the official public-alias table.
///
/// Format (identical to `tools/pack-stdlib.py`): for every alias sorted by
/// `(target_module, target_function)`: module, NUL, function, NUL, then every
/// public name sorted, NUL. A fixed binary format avoids JSON formatting drift.
pub fn aliases_digest(aliases: &[PublicAlias]) -> String {
let mut sorted = aliases.to_vec();
sorted.sort_by(|left, right| {
(&left.target_module, &left.target_function)
.cmp(&(&right.target_module, &right.target_function))
});
let mut hasher = Sha256::new();
for alias in &sorted {
hasher.update(alias.target_module.as_bytes());
hasher.update([0]);
hasher.update(alias.target_function.as_bytes());
hasher.update([0]);
let mut names = alias.public_names.clone();
names.sort();
for name in names {
hasher.update(name.as_bytes());
hasher.update([0]);
}
}
format!("sha256:{}", hex(&hasher.finalize()))
}
#[cfg(test)]
mod tests {
use super::*;
+122
View File
@@ -272,3 +272,125 @@ fn walk_rs(root: &Path) -> Vec<PathBuf> {
}
files
}
/// RFC-EXT-0001 §4/§14: the runtime must dispatch by the prepared binding and
/// never re-resolve a name or special-case a print function.
#[test]
fn callable_binding_is_the_only_static_resolution_path() {
let root = repo_root();
let loader = read(&root.join("crates/cli/src/loader.rs"));
let interpreter = read(&root.join("crates/marqdo-runtime/src/interpreter.rs"));
let run = read(&root.join("crates/cli/src/commands/run.rs"));
let frontend = read(&root.join("crates/marqdo-frontend/src/calls.rs"));
assert!(
!loader.contains("program_uses_print"),
"loader must not scan for per-function names (ADR-0058)"
);
assert!(
!interpreter.contains("print_override"),
"the runtime must not special-case print (ADR-0054/0058)"
);
assert!(
!interpreter.contains("external_callables.is_empty"),
"the runtime must not fall back to plugin guessing (ADR-0054)"
);
assert!(
interpreter.contains("call_bindings.get"),
"the runtime must dispatch by the prepared call binding table"
);
assert!(
!interpreter.contains("builtins::find(&qualified)"),
"a qualified name must not be re-resolved at run time"
);
assert!(
!run.contains("external_callables")
|| !run.contains("external_callables")
|| !run.contains("push(marqdo_ir::callable::CallableDescriptor"),
"run must not synthesise plugin descriptors (ADR-0054/§4.5)"
);
assert!(
frontend.contains("bind_program_calls"),
"the frontend must expose the single binding entry point"
);
}
/// RFC-EXT-0001 §14.2/S4: no business-domain builtin or host-op branch may be
/// added for an ecosystem feature. These must be implemented as `.mq.md`,
/// plugin exports or provider streams.
#[test]
fn no_business_domain_builtins_or_host_ops() {
let root = repo_root();
let builtins = read(&root.join("crates/marqdo-ir/src/builtins.rs"));
let host = read(&root.join("crates/marqdo-host/src/host.rs"));
for forbidden in ["quantum.", "ai.chat", "vendor.", "demo.sensor", "marqdo."] {
assert!(
!builtins.contains(forbidden),
"business builtin namespace `{forbidden}` must not exist"
);
}
assert!(
!host.contains("\"demo.sensor\""),
"provider kinds must be data, not host-op string branches"
);
}
/// RFC-EXT-0001 §8: official stdlib public aliases are verified metadata, not
/// hard-coded runtime names. Recompute the canonical metadata digest.
#[test]
fn official_stdlib_public_aliases_are_verified_metadata() {
let root = repo_root().join("stdlib");
let manifest: serde_json::Value =
serde_json::from_slice(&std::fs::read(root.join("stdlib.json")).unwrap()).unwrap();
let aliases = manifest["public_aliases"]
.as_array()
.expect("stdlib.json must declare public_aliases");
assert!(!aliases.is_empty());
let mut hasher = Sha256::new();
let mut entries: Vec<&serde_json::Value> = aliases.iter().collect();
entries.sort_by_key(|alias| {
(
alias["target_module"].as_str().unwrap_or("").to_string(),
alias["target_function"].as_str().unwrap_or("").to_string(),
)
});
for alias in &entries {
hasher.update(alias["target_module"].as_str().unwrap().as_bytes());
hasher.update([0]);
hasher.update(alias["target_function"].as_str().unwrap().as_bytes());
hasher.update([0]);
let mut names: Vec<&str> = alias["public_names"]
.as_array()
.unwrap()
.iter()
.filter_map(serde_json::Value::as_str)
.collect();
names.sort();
for name in names {
hasher.update(name.as_bytes());
hasher.update([0]);
}
}
let actual = format!("sha256:{}", hex(&hasher.finalize()));
assert_eq!(
manifest["aliases_digest"], actual,
"stdlib.json aliases_digest must cover the public alias table"
);
for alias in aliases {
let target_module = alias["target_module"].as_str().unwrap();
let target_function = alias["target_function"].as_str().unwrap();
let module = read(&root.join(target_module));
assert!(
module.contains(&format!("## {target_function}")),
"public alias target `{target_module}::{target_function}` must exist"
);
}
}
fn hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for byte in bytes {
out.push_str(&format!("{byte:02x}"));
}
out
}
+653
View File
@@ -0,0 +1,653 @@
//! Black-box callable resolution / binding conformance (RFC-EXT-0001 §4.6).
//!
//! Every test drives the real `marqdo` binary through the public CLI. The
//! point is to prove that `check` and `run` share one static resolution and
//! that the runtime never guesses a plugin from a dotted string prefix.
#![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-callable-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 cli(args: &[&str], dir: &Path) -> Output {
Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(args)
.current_dir(dir)
.output()
.unwrap()
}
fn write(path: &Path, text: &str) {
std::fs::write(path, text).unwrap();
}
fn library_name() -> &'static str {
if cfg!(target_os = "macos") {
"solver.dylib"
} else {
"solver.so"
}
}
fn shared_flags() -> &'static [&'static str] {
if cfg!(target_os = "macos") {
&["-dynamiclib", "-fPIC"]
} else {
&["-shared", "-fPIC"]
}
}
fn have_cc() -> bool {
Command::new("cc")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn compile_plugin(dir: &Path, extra: &[&str]) -> PathBuf {
let output = dir.join(library_name());
let status = Command::new("cc")
.args(["-std=c11", "-O2"])
.args(shared_flags())
.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_worker_manifest(dir: &Path, artifact: &Path, exports: &str) {
let sha = sha256_file(artifact).unwrap();
write(
&dir.join("plugin.yaml"),
&format!(
r#"plugin:
name: demo.solver
version: 0.1.0
abi: "^1.0"
kind: worker
exports:
{exports} requires:
- math.compute
artifacts:
- target: {}
file: {}
sha256: "{sha}"
"#,
HostTarget::current().triple(),
artifact.file_name().unwrap().to_string_lossy()
),
);
}
fn add_export(name: &str, signature: &str) -> String {
format!(" - name: {name}\n signature: \"{signature}\"\n")
}
fn stderr(output: &Output) -> String {
String::from_utf8_lossy(&output.stderr).to_string()
}
fn stdout(output: &Output) -> String {
String::from_utf8_lossy(&output.stdout).to_string()
}
// CALL-01
#[test]
fn import_alias_conflicting_with_builtin_namespace_is_rejected() {
let dir = TempDir::new("call01");
write(
&dir.path().join("math.mq.md"),
"## @exec eval\n\n`params(a: Int)`\n\n`return a + 100`\n",
);
write(
&dir.path().join("index.mq.md"),
"---\nimport:\n math: \"./math.mq.md\"\n---\n\n## @exec main\n\n`return math.eval(a=1)`\n",
);
let check = cli(&["check", "index.mq.md"], dir.path());
assert_eq!(check.status.code(), Some(1), "{}", stderr(&check));
assert!(
stderr(&check).contains("ext.import_namespace_conflict"),
"{}",
stderr(&check)
);
let run = cli(&["run", "index.mq.md"], dir.path());
assert_eq!(run.status.code(), Some(1));
assert!(stdout(&run).is_empty());
assert!(
stderr(&run).contains("ext.import_namespace_conflict"),
"{}",
stderr(&run)
);
}
// CALL-02
#[test]
fn dotted_builtin_call_binds_without_an_import() {
let output = cli(&["check", "examples/math.mq.md"], &repo());
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
let run = cli(&["call", "examples/math.mq.md", "计算圆面积"], &repo());
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
}
// CALL-03
#[test]
fn renamed_import_alias_binds_the_module_function() {
let dir = TempDir::new("call03");
write(
&dir.path().join("math.mq.md"),
"## @exec eval\n\n`params(a: Int)`\n\n`return a + 100`\n",
);
write(
&dir.path().join("index.mq.md"),
"---\nimport:\n mymath: \"./math.mq.md\"\n---\n\n## @exec main\n\n`return mymath.eval(a=1)`\n",
);
let check = cli(&["check", "index.mq.md"], dir.path());
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
let run = cli(&["run", "index.mq.md", "--json"], dir.path());
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
let json: serde_json::Value = serde_json::from_str(&stdout(&run)).unwrap();
assert_eq!(
json["value"],
serde_json::json!({"type": "int", "decimal": "101"})
);
}
// CALL-04 / CALL-05
#[test]
fn unknown_plugin_export_is_a_static_symbol_error() {
let dir = TempDir::new("call04");
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`return solver.add(a=1, b=2)`\n",
);
let check = cli(&["check", "index.mq.md"], dir.path());
assert_eq!(check.status.code(), Some(1));
assert!(
stderr(&check).contains("symbol.unknown"),
"{}",
stderr(&check)
);
let run = cli(&["run", "index.mq.md"], dir.path());
assert_eq!(run.status.code(), Some(1));
assert!(stderr(&run).contains("symbol.unknown"), "{}", stderr(&run));
assert!(stdout(&run).is_empty());
// CALL-05: the diagnostic does not change when an unrelated plugin is
// loaded, because the static export table is still the only source.
if !have_cc() {
return;
}
let plugin_dir = TempDir::new("call05-plugin");
let artifact = compile_plugin(plugin_dir.path(), &[]);
write_worker_manifest(
plugin_dir.path(),
&artifact,
&add_export("solver.add", "($a: Int, $b: Int) -> Int"),
);
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`return solver.subtract(a=1, b=2)`\n",
);
let with_plugin = cli(
&[
"run",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--allow-plugin",
"demo.solver",
],
dir.path(),
);
assert_eq!(with_plugin.status.code(), Some(1));
assert!(
stderr(&with_plugin).contains("symbol.unknown"),
"{}",
stderr(&with_plugin)
);
}
// CALL-06
#[test]
fn plugin_with_two_prefixes_binds_both_exports() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("call06");
let plugin_dir = TempDir::new("call06-plugin");
let artifact = compile_plugin(plugin_dir.path(), &["-DMQ_SOLVER_MULTI_EXPORT"]);
write_worker_manifest(
plugin_dir.path(),
&artifact,
&(add_export("solver.add", "($a: Int, $b: Int) -> Int")
+ &add_export("solver.neg", "($a: Int) -> Int")),
);
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`和 = solver.add(20, b=22)`\n\n`相反数 = solver.neg(7)`\n\n`打印(和)`\n\n`打印(相反数)`\n",
);
let output = cli(
&[
"run",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--allow-plugin",
"demo.solver",
],
dir.path(),
);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
assert_eq!(stdout(&output), "42\n-7\n");
}
// CALL-07
#[test]
fn duplicate_complete_export_across_plugins_is_rejected_at_prepare() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("call07");
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`return solver.add(a=1, b=2)`\n",
);
let mut plugins = Vec::new();
for tag in ["a", "b"] {
let plugin_dir = TempDir::new(tag);
let artifact = compile_plugin(plugin_dir.path(), &[]);
let sha = sha256_file(&artifact).unwrap();
write(
&plugin_dir.path().join("plugin.yaml"),
&format!(
r#"plugin:
name: demo.{tag}.solver
version: 0.1.0
abi: "^1.0"
kind: worker
exports:
- name: solver.add
signature: "($a: Int, $b: Int) -> Int"
artifacts:
- target: {}
file: {}
sha256: "{sha}"
"#,
HostTarget::current().triple(),
artifact.file_name().unwrap().to_string_lossy()
),
);
plugins.push(plugin_dir);
}
let check = cli(
&[
"check",
"index.mq.md",
"--plugin",
plugins[0].path().to_str().unwrap(),
"--plugin",
plugins[1].path().to_str().unwrap(),
],
dir.path(),
);
assert_eq!(check.status.code(), Some(2), "{}", stderr(&check));
assert!(
stderr(&check).contains("ext.duplicate_callable"),
"{}",
stderr(&check)
);
}
// CALL-08
#[test]
fn bad_plugin_arguments_fail_in_check_and_run_identically() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("call08");
let plugin_dir = TempDir::new("call08-plugin");
let artifact = compile_plugin(plugin_dir.path(), &[]);
write_worker_manifest(
plugin_dir.path(),
&artifact,
&add_export("solver.add", "($a: Int, $b: Int) -> Int"),
);
for (tag, call) in [
("missing", "solver.add(a=1)"),
("unknown", "solver.add(a=1, b=2, c=3)"),
("duplicate", "solver.add(a=1, a=2, b=3)"),
("too-many-positional", "solver.add(1, 2, 3)"),
] {
write(
&dir.path().join("index.mq.md"),
&format!("## @exec main\n\n`return {call}`\n"),
);
let check = cli(
&[
"check",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
],
dir.path(),
);
assert_eq!(check.status.code(), Some(1), "{tag}: {}", stderr(&check));
let expected = if tag == "duplicate" {
// The expression grammar rejects duplicate named arguments before
// call binding; either way the failure is static.
"sem.syntax"
} else {
"call.arity"
};
assert!(
stderr(&check).contains(expected),
"{tag}: {}",
stderr(&check)
);
let run = cli(
&[
"run",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--allow-plugin",
"demo.solver",
],
dir.path(),
);
assert_eq!(run.status.code(), Some(1), "{tag}: {}", stderr(&run));
assert!(stdout(&run).is_empty(), "{tag}");
// `check` appends a summary line and `run` a refusal line, but the
// structured call.arity diagnostic itself must be identical.
let arity = |text: &str| {
text.lines()
.filter(|line| line.contains("call.arity") || line.contains("sem.syntax"))
.map(ToString::to_string)
.collect::<Vec<_>>()
};
assert!(!arity(&stderr(&check)).is_empty(), "{tag}");
assert_eq!(
arity(&stderr(&check)),
arity(&stderr(&run)),
"{tag}: check != run"
);
}
}
// CALL-09
#[test]
fn changed_artifact_digest_is_rejected_before_main_runs() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("call09");
let plugin_dir = TempDir::new("call09-plugin");
let artifact = compile_plugin(plugin_dir.path(), &[]);
write_worker_manifest(
plugin_dir.path(),
&artifact,
&add_export("solver.add", "($a: Int, $b: Int) -> Int"),
);
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`打印(\"must-not-run\")`\n\n`return solver.add(a=1, b=2)`\n",
);
// Static check succeeds: it never opens the artifact.
let check = cli(
&[
"check",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
],
dir.path(),
);
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
// Corrupt the artifact bytes; run must reject before the entry function.
let mut bytes = std::fs::read(&artifact).unwrap();
bytes.push(0);
std::fs::write(&artifact, bytes).unwrap();
let run = cli(
&[
"run",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--allow-plugin",
"demo.solver",
],
dir.path(),
);
assert_eq!(run.status.code(), Some(1), "{}", stderr(&run));
assert!(
stderr(&run).contains("ext.artifact_integrity"),
"{}",
stderr(&run)
);
assert!(stdout(&run).is_empty(), "{}", stdout(&run));
}
// CALL-11
#[test]
fn local_function_reference_is_a_dynamic_language_call() {
let dir = TempDir::new("call11");
write(
&dir.path().join("index.mq.md"),
"## @exec 平方\n\n`params(n: Int)`\n\n`return n * n`\n\n## @exec 主\n\n`f = ref(\"#平方\")`\n\n`return f(n=4)`\n",
);
let run = cli(&["call", "index.mq.md", "主", "--json"], dir.path());
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
let json: serde_json::Value = serde_json::from_str(&stdout(&run)).unwrap();
assert_eq!(
json["value"],
serde_json::json!({"type": "int", "decimal": "16"})
);
}
// CALL-12
#[test]
fn calling_a_text_value_is_not_callable_and_never_reaches_the_host() {
let dir = TempDir::new("call12");
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`f = \"text\"`\n\n`return f()`\n",
);
let check = cli(&["check", "index.mq.md"], dir.path());
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
let run = cli(&["run", "index.mq.md"], dir.path());
assert_eq!(run.status.code(), Some(1), "{}", stderr(&run));
assert!(
stderr(&run).contains("call.not_callable") || stderr(&run).contains("not_callable"),
"{}",
stderr(&run)
);
}
// CALL-13
#[test]
fn explicit_import_aliases_bind_same_named_functions_independently() {
let dir = TempDir::new("call13");
write(
&dir.path().join("a.mq.md"),
"## @exec value\n\n`params()`\n\n`return 10`\n",
);
write(
&dir.path().join("b.mq.md"),
"## @exec value\n\n`params()`\n\n`return 20`\n",
);
write(
&dir.path().join("index.mq.md"),
"---\nimport:\n aa: \"./a.mq.md\"\n bb: \"./b.mq.md\"\n---\n\n## @exec main\n\n`return aa.value() + bb.value()`\n",
);
let run = cli(&["run", "index.mq.md", "--json"], dir.path());
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
let json: serde_json::Value = serde_json::from_str(&stdout(&run)).unwrap();
assert_eq!(
json["value"],
serde_json::json!({"type": "int", "decimal": "30"})
);
}
// CALL-14
#[test]
fn check_never_executes_a_plugin_artifact_or_side_effect_script() {
let dir = TempDir::new("call14");
let plugin_dir = TempDir::new("call14-plugin");
let marker = plugin_dir.path().join("executed.marker");
let artifact = plugin_dir.path().join(library_name());
write(
&artifact,
&format!("#!/bin/sh\ntouch '{}'\nexit 0\n", marker.display()),
);
let sha = sha256_file(&artifact).unwrap();
write(
&plugin_dir.path().join("plugin.yaml"),
&format!(
r#"plugin:
name: demo.script
version: 0.1.0
abi: "^1.0"
kind: worker
exports:
- name: demo.run
signature: "() -> Int"
artifacts:
- target: {}
file: {}
sha256: "{sha}"
"#,
HostTarget::current().triple(),
artifact.file_name().unwrap().to_string_lossy()
),
);
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`return demo.run()`\n",
);
let check = cli(
&[
"check",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
],
dir.path(),
);
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
assert!(!marker.exists(), "check executed the artifact");
}
// CALL-15
#[test]
fn check_emit_and_run_share_the_same_static_binding_source() {
if !have_cc() {
eprintln!("skipping: cc is unavailable");
return;
}
let dir = TempDir::new("call15");
let plugin_dir = TempDir::new("call15-plugin");
let artifact = compile_plugin(plugin_dir.path(), &[]);
write_worker_manifest(
plugin_dir.path(),
&artifact,
&add_export("solver.add", "($a: Int, $b: Int) -> Int"),
);
write(
&dir.path().join("index.mq.md"),
"## @exec main\n\n`return solver.add(a=1, b=2)`\n",
);
let analysis_path = dir.path().join("analysis.json");
let check = cli(
&[
"check",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--emit",
analysis_path.to_str().unwrap(),
],
dir.path(),
);
assert_eq!(check.status.code(), Some(0), "{}", stderr(&check));
let analysis: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&analysis_path).unwrap()).unwrap();
let functions = analysis["modules"]
.as_array()
.and_then(|modules| modules.first())
.and_then(|module| module["functions"].as_array())
.unwrap_or_else(|| panic!("analysis has no module functions: {analysis}"));
assert!(!functions.is_empty());
let run = cli(
&[
"run",
"index.mq.md",
"--plugin",
plugin_dir.path().to_str().unwrap(),
"--allow-plugin",
"demo.solver",
"--json",
],
dir.path(),
);
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
// Without the descriptor the same call is statically unknown: run/check
// consume the same prepared binding, not a runtime name probe.
let without = cli(&["check", "index.mq.md"], dir.path());
assert_eq!(without.status.code(), Some(1));
assert!(stderr(&without).contains("symbol.unknown"));
}
// CALL-16
#[test]
fn print_alias_keeps_the_existing_output_bytes() {
let run = cli(&["run", "examples/hello.mq.md"], &repo());
assert_eq!(run.status.code(), Some(0), "{}", stderr(&run));
assert_eq!(stdout(&run), "你好,Marqdo\n");
}
+80 -18
View File
@@ -166,8 +166,8 @@ fn semantic_goldens_match_end_to_end_execution() {
let mut program = marqdo_ir::Program::default();
program.insert(analysis.module.clone());
program.main_module = Some(module_id.clone());
install_stdlib_print(&mut program);
install_entry_stdlib_imports(&mut program, &module_id, &analysis);
bind_calls_with_stdlib_aliases(&mut program);
let mut capabilities = Capabilities::pure();
if grant_foreign {
@@ -284,26 +284,88 @@ fn normalise_relative(path: &Path) -> String {
.join("/")
}
/// Load the bundled stdlib print modules for direct-runtime fixtures.
/// Bind direct-runtime fixtures exactly like the CLI composition root.
///
/// The CLI loader normally injects these for Compat-B. The semantic harness
/// builds a Program directly, so it mirrors the same resolution here; without
/// it, fixtures that call `打印` would hit the Compat-C "no bundle" error.
fn install_stdlib_print(program: &mut Program) {
/// The semantic harness builds a `Program` directly, so it mirrors the
/// production path: load the official stdlib public-alias targets, build the
/// unique registry, run the single binding pass and install its table. There is
/// no `print_override` and no runtime name guessing.
fn bind_calls_with_stdlib_aliases(program: &mut Program) {
let root = repo_root().join("stdlib");
if !root.join("stdlib.json").is_file() {
return;
}
let mut loaded = std::collections::BTreeSet::new();
let io = match load_stdlib_module(&root, "io.mq.md", program, &mut loaded) {
Ok(id) => id,
Err(error) => panic!("cannot load bundled stdlib: {error}"),
};
let function = program
.module(&io)
.and_then(|module| module.function("写值"))
.unwrap_or_else(|| panic!("stdlib io has no 写值 function"));
program.print_override = Some(function.id.clone());
let mut official = Vec::new();
if root.join("stdlib.json").is_file() {
let text = fs::read_to_string(root.join("stdlib.json"))
.unwrap_or_else(|error| panic!("cannot read stdlib.json: {error}"));
let manifest: serde_json::Value = serde_json::from_str(&text)
.unwrap_or_else(|error| panic!("invalid stdlib.json: {error}"));
if let Some(aliases) = manifest
.get("public_aliases")
.and_then(serde_json::Value::as_array)
{
for entry in aliases {
let public_names: Vec<String> = entry
.get("public_names")
.and_then(serde_json::Value::as_array)
.unwrap_or_else(|| panic!("public alias has no public_names"))
.iter()
.filter_map(serde_json::Value::as_str)
.map(ToString::to_string)
.collect();
let target_module = entry
.get("target_module")
.and_then(serde_json::Value::as_str)
.unwrap_or_else(|| panic!("public alias has no target_module"));
let target_function = entry
.get("target_function")
.and_then(serde_json::Value::as_str)
.unwrap_or_else(|| panic!("public alias has no target_function"));
let module_id = load_stdlib_module(&root, target_module, program, &mut loaded)
.unwrap_or_else(|error| panic!("cannot load stdlib alias module: {error}"));
let function = program
.module(&module_id)
.and_then(|module| module.function(target_function))
.unwrap_or_else(|| {
panic!("stdlib alias target `{target_function}` is missing")
});
let effects = entry
.get("effects")
.and_then(serde_json::Value::as_array)
.map(|effects| {
effects
.iter()
.filter_map(serde_json::Value::as_str)
.map(ToString::to_string)
.collect()
})
.unwrap_or_default();
official.push(marqdo_ir::callable::OfficialAlias {
public_names,
function: function.id.clone(),
signature: marqdo_ir::callable::CallableSignature {
params: function.params.clone(),
kind: function.signature,
},
owner_package: module_id.instance.clone(),
effects_requested: effects,
});
}
}
}
let mut registry = program.callable_registry();
for alias in official {
registry
.insert_official_alias(alias)
.unwrap_or_else(|error| panic!("cannot register stdlib alias: {error}"));
}
let bound = marqdo_frontend::calls::bind_program_calls(program, &registry);
if !bound.diagnostics.is_empty() {
panic!(
"fixture failed static call binding: {:#?}",
bound.diagnostics
);
}
program.set_call_bindings(bound.bindings);
}
fn load_stdlib_module(
+13
View File
@@ -224,6 +224,7 @@ fn runtime_negative_goldens() {
let mut program = marqdo_ir::Program::default();
program.insert(analysis.module.clone());
program.main_module = Some(module_id.clone());
bind_program_calls(&mut program);
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::pure(),
@@ -284,3 +285,15 @@ fn runtime_negative_goldens() {
}
let _ = fs::remove_dir_all(&project_root);
}
/// Bind direct-runtime fixtures exactly like the CLI composition root.
fn bind_program_calls(program: &mut marqdo_ir::Program) {
let registry = program.callable_registry();
let bound = marqdo_frontend::calls::bind_program_calls(program, &registry);
assert!(
bound.diagnostics.is_empty(),
"fixture failed static call binding: {:#?}",
bound.diagnostics
);
program.set_call_bindings(bound.bindings);
}
+8
View File
@@ -395,6 +395,14 @@ fn narration_removal_preserves_execution() {
let mut program = marqdo_ir::Program::default();
program.insert(analysis.module.clone());
program.main_module = Some(module_id.clone());
let registry = program.callable_registry();
let bound = marqdo_frontend::calls::bind_program_calls(&program, &registry);
assert!(
bound.diagnostics.is_empty(),
"property fixture failed binding: {:#?}",
bound.diagnostics
);
program.set_call_bindings(bound.bindings);
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::pure(),
File diff suppressed because it is too large. Load diff
+260
View File
@@ -0,0 +1,260 @@
//! Official stdlib public alias routing (RFC-EXT-0001 §8, ADR-0058).
//!
//! `打印`/`print` are ordinary `Marqdo` bindings to `io.mq.md::写值` declared
//! in the verified `stdlib.json` metadata. There is no `print_override`, no
//! runtime name special case and no hard-coded per-function scan.
#![cfg(unix)]
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicU32, Ordering};
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-stdlib-alias-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 cli(args: &[&str], dir: &Path, env: &[(&str, &Path)]) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_marqdo"));
command.args(args).current_dir(dir);
for (key, value) in env {
command.env(key, value);
}
command.output().unwrap()
}
fn stderr(output: &Output) -> String {
String::from_utf8_lossy(&output.stderr).to_string()
}
fn stdout(output: &Output) -> String {
String::from_utf8_lossy(&output.stdout).to_string()
}
fn copy_stdlib(target: &Path) {
let source = repo().join("stdlib");
std::fs::create_dir_all(target).unwrap();
for entry in std::fs::read_dir(&source).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
copy_dir(&path, &target.join(entry.file_name()));
} else {
std::fs::copy(&path, target.join(entry.file_name())).unwrap();
}
}
}
fn copy_dir(source: &Path, target: &Path) {
std::fs::create_dir_all(target).unwrap();
for entry in std::fs::read_dir(source).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
copy_dir(&path, &target.join(entry.file_name()));
} else {
std::fs::copy(&path, target.join(entry.file_name())).unwrap();
}
}
}
#[test]
fn print_and_its_alias_are_bound_to_the_official_module() {
let dir = TempDir::new("print");
std::fs::write(
dir.path().join("index.mq.md"),
"## @exec main\n\n`打印(1)`\n\n`print(\"two\")`\n\n`return None`\n",
)
.unwrap();
let output = cli(&["run", "index.mq.md"], dir.path(), &[]);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
assert_eq!(stdout(&output), "1\ntwo\n");
}
#[test]
fn program_without_print_does_not_load_the_stdlib_module_graph() {
let dir = TempDir::new("no-print");
std::fs::write(
dir.path().join("index.mq.md"),
"## @exec main\n\n`params()`\n\n`return 1 + 2`\n",
)
.unwrap();
let analysis_path = dir.path().join("factorial.json");
let output = cli(
&[
"check",
"index.mq.md",
"--emit",
analysis_path.to_str().unwrap(),
],
dir.path(),
&[],
);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
let analysis: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&analysis_path).unwrap()).unwrap();
let modules = analysis["modules"].as_array().unwrap();
assert!(
modules
.iter()
.all(|module| !module["id"].as_str().unwrap_or("").starts_with("std:")),
"a program without print must not load the stdlib: {analysis}"
);
}
#[test]
fn print_program_loads_exactly_the_declared_alias_target() {
let dir = TempDir::new("with-print");
std::fs::write(
dir.path().join("index.mq.md"),
"## @exec main\n\n`打印(\"x\")`\n\n`return None`\n",
)
.unwrap();
let analysis_path = dir.path().join("hello.json");
let output = cli(
&[
"check",
"index.mq.md",
"--emit",
analysis_path.to_str().unwrap(),
],
dir.path(),
&[],
);
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
let analysis: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&analysis_path).unwrap()).unwrap();
let ids: Vec<&str> = analysis["modules"]
.as_array()
.unwrap()
.iter()
.filter_map(|module| module["id"].as_str())
.collect();
assert!(
ids.iter().any(|id| id.ends_with("::io.mq.md")),
"print must load io.mq.md: {ids:?}"
);
assert!(
ids.iter().any(|id| id.ends_with("::format.mq.md")),
"io.mq.md imports format.mq.md: {ids:?}"
);
}
#[test]
fn tampered_public_alias_metadata_digest_is_rejected() {
let dir = TempDir::new("alias-digest");
let stdlib = dir.path().join("stdlib");
copy_stdlib(&stdlib);
let manifest_path = stdlib.join("stdlib.json");
let text = std::fs::read_to_string(&manifest_path).unwrap();
let mut manifest: serde_json::Value = serde_json::from_str(&text).unwrap();
manifest["aliases_digest"] = serde_json::json!("sha256:".to_string() + &"0".repeat(64));
std::fs::write(
&manifest_path,
serde_json::to_string_pretty(&manifest).unwrap() + "\n",
)
.unwrap();
let doc = dir.path().join("index.mq.md");
std::fs::write(&doc, "## @exec main\n\n`打印(1)`\n\n`return None`\n").unwrap();
let output = cli(
&["run", doc.to_str().unwrap()],
dir.path(),
&[("MARQDO_STDLIB_DIR", &stdlib)],
);
assert!(!output.status.success(), "{}", stderr(&output));
assert!(
stderr(&output).contains("public-alias metadata digest mismatch"),
"{}",
stderr(&output)
);
}
#[test]
fn tampered_alias_target_signature_is_rejected() {
let dir = TempDir::new("alias-target");
let stdlib = dir.path().join("stdlib");
copy_stdlib(&stdlib);
let manifest_path = stdlib.join("stdlib.json");
let text = std::fs::read_to_string(&manifest_path).unwrap();
let mut manifest: serde_json::Value = serde_json::from_str(&text).unwrap();
manifest["public_aliases"][0]["expected_signature"] = serde_json::json!("params(值, 额外)");
// Recompute the declared metadata digest so only the target check can fail.
let canonical = manifest["public_aliases"][0].clone();
let module = canonical["target_module"].as_str().unwrap();
let function = canonical["target_function"].as_str().unwrap();
let mut names: Vec<&str> = canonical["public_names"]
.as_array()
.unwrap()
.iter()
.filter_map(serde_json::Value::as_str)
.collect();
names.sort();
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(module.as_bytes());
hasher.update([0]);
hasher.update(function.as_bytes());
hasher.update([0]);
for name in names {
hasher.update(name.as_bytes());
hasher.update([0]);
}
let digest = format!("sha256:{}", hex(&hasher.finalize()));
manifest["aliases_digest"] = serde_json::json!(digest);
std::fs::write(
&manifest_path,
serde_json::to_string_pretty(&manifest).unwrap() + "\n",
)
.unwrap();
let doc = dir.path().join("index.mq.md");
std::fs::write(&doc, "## @exec main\n\n`打印(1)`\n\n`return None`\n").unwrap();
let output = cli(
&["run", doc.to_str().unwrap()],
dir.path(),
&[("MARQDO_STDLIB_DIR", &stdlib)],
);
assert!(!output.status.success(), "{}", stderr(&output));
assert!(
stderr(&output).contains("expected signature") || stderr(&output).contains("alias"),
"{}",
stderr(&output)
);
}
fn hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for byte in bytes {
out.push_str(&format!("{byte:02x}"));
}
out
}