From 53cdea232535505c3c8a7b71faab3c3d934b01ce Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 20 Nov 2026 13:54:18 +0800 Subject: [PATCH] fix(ci): build native plugin fixtures for the host target The macOS job failed with no_matching_artifact because the integration test copied the Linux-only example manifest and built a Linux-named .so. Tests now generate a manifest for the actual host triple, use the platform dynamic library name and compiler flags, and never rewrite the tracked example manifest. - add a cross-platform no_matching_artifact test that runs without a C compiler - add real tamper and ABI-major-mismatch rejection tests - make worker fixtures platform-native; Windows remains explicitly unsupported (no cc) rather than counted as a pass - example build scripts emit host-specific build/ fixtures (gitignored) - CI smoke points at the generated fixtures --- .github/workflows/ci.yml | 8 +- crates/cli/tests/integration_cli.rs | 203 ++++++++++++++++++++----- crates/cli/tests/plugin_worker.rs | 42 ++++- examples/plugin-demo/README.md | 6 +- examples/plugin-demo/build.sh | 54 +++++-- examples/plugin-multi-export/README.md | 6 +- examples/plugin-multi-export/build.sh | 51 +++++-- 7 files changed, 285 insertions(+), 85 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 08777ab..d1a9620 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,16 +61,16 @@ jobs: ./target/debug/marqdo benchmark run . ./target/debug/marqdo tool remove benchmark ./examples/plugin-demo/build.sh - ./target/debug/marqdo plugin inspect examples/plugin-demo + ./target/debug/marqdo plugin inspect examples/plugin-demo/build ./target/debug/marqdo run examples/plugin-demo/plugin-call.mq.md \ - --plugin examples/plugin-demo \ + --plugin examples/plugin-demo/build \ --trust-native-plugin \ --allow-plugin demo.solver ./examples/plugin-multi-export/build.sh ./target/debug/marqdo check examples/plugin-multi-export/plugin-call.mq.md \ - --plugin examples/plugin-multi-export + --plugin examples/plugin-multi-export/build ./target/debug/marqdo run examples/plugin-multi-export/plugin-call.mq.md \ - --plugin examples/plugin-multi-export \ + --plugin examples/plugin-multi-export/build \ --trust-native-plugin \ --allow-plugin demo.solver diff --git a/crates/cli/tests/integration_cli.rs b/crates/cli/tests/integration_cli.rs index 867e90b..1f31206 100644 --- a/crates/cli/tests/integration_cli.rs +++ b/crates/cli/tests/integration_cli.rs @@ -521,61 +521,83 @@ fn lock_and_sync_locked_detect_dependency_tampering() { assert_exit(&synced, 0, "sync --locked after re-lock"); } -/// Build the C demo plugin in a temporary copy and refresh the manifest hash in -/// that copy only. The repository copy is never touched and the `.so` is never -/// committed. +/// Host target and dynamic-library naming for a real Native ABI fixture. +/// +/// The manifest is generated from the actual host target instead of reusing +/// the Linux-only example manifest. This is what makes the integration test a +/// real test on macOS/Windows rather than an accidental "no matching artifact". +fn host_triple() -> String { + marqdo_plugin_abi::artifact::current_target_triple() + .as_str() + .to_string() +} + +fn dynamic_library_name() -> &'static str { + if cfg!(target_os = "windows") { + "solver.dll" + } else if cfg!(target_os = "macos") { + "solver.dylib" + } else { + "solver.so" + } +} + +fn shared_library_flags() -> &'static [&'static str] { + if cfg!(target_os = "windows") { + &["-shared"] + } else if cfg!(target_os = "macos") { + &["-dynamiclib", "-fPIC"] + } else { + &["-shared", "-fPIC"] + } +} + +/// Build the C demo plugin in a temporary directory and generate a manifest for +/// this host. The repository (including `examples/plugin-demo/plugin.yaml`) is +/// never modified, and no prebuilt artifact is reused. fn build_plugin_copy(tag: &str) -> Option<(TempDir, PathBuf)> { + build_plugin_copy_with(tag, &[]) +} + +/// Like [`build_plugin_copy`], with extra C compiler flags for negative fixtures. +fn build_plugin_copy_with(tag: &str, extra_flags: &[&str]) -> Option<(TempDir, PathBuf)> { if !cc_available() { - println!("skipping plugin test: `cc` is unavailable"); - return None; + // A real C ABI test needs a C toolchain. Windows MSVC does not provide + // `cc` in the plain runner PATH, so native ABI on Windows is reported + // as unsupported rather than counted as a pass. + if cfg!(windows) { + println!("skipping native plugin test on Windows: no `cc` toolchain"); + return None; + } + panic!("`cc` is required for the native plugin integration test on this platform"); } let dir = TempDir::new(tag); - let source = repo_root().join("examples").join("plugin-demo"); let target = dir.path().join("plugin-demo"); fs::create_dir_all(&target).expect("create plugin copy"); - for entry in fs::read_dir(&source).expect("read plugin-demo") { - let entry = entry.expect("plugin-demo entry"); - if !entry.path().is_file() { - continue; - } - // Copy manifest/source/readme but never a stale compiled artifact. - if entry - .path() - .extension() - .is_some_and(|extension| extension == "so") - { - continue; - } - fs::copy(entry.path(), target.join(entry.file_name())) - .unwrap_or_else(|error| panic!("copy plugin file: {error}")); - } + let source = repo_root().join("examples").join("plugin-demo"); + fs::copy(source.join("solver.c"), target.join("solver.c")).expect("copy plugin source"); - let include = repo_root().join("include"); + let artifact = target.join(dynamic_library_name()); let status = Command::new("cc") - .args([ - "-shared", - "-fPIC", - "-I", - include.to_str().expect("utf-8 include path"), - "-o", - target.join("solver.so").to_str().expect("utf-8 path"), - target.join("solver.c").to_str().expect("utf-8 path"), - ]) + .args(shared_library_flags()) + .args(extra_flags) + .arg("-I") + .arg(repo_root().join("include")) + .args(["-o", artifact.to_str().expect("utf-8 path")]) + .arg(target.join("solver.c")) .status() .unwrap_or_else(|error| panic!("cannot run cc: {error}")); assert!(status.success(), "building the demo plugin failed"); - let bytes = fs::read(target.join("solver.so")).expect("read solver.so"); + let bytes = fs::read(&artifact).expect("read built plugin"); let digest = hex::encode(sha2::Sha256::digest(&bytes)); - let manifest_path = target.join("plugin.yaml"); - let manifest = fs::read_to_string(&manifest_path).expect("read plugin.yaml"); - let placeholder = "0000000000000000000000000000000000000000000000000000000000000000"; - assert!( - manifest.contains(placeholder), - "manifest placeholder missing" + let manifest = format!( + "plugin:\n name: demo.solver\n version: 0.1.0\n abi: \"^1.0\"\n kind: native\n exports:\n - name: solver.add\n signature: \"($a: Int, $b: Int) -> Int\"\n requires:\n - math.compute\n artifacts:\n - target: {}\n file: {}\n sha256: \"{}\"\n", + host_triple(), + dynamic_library_name(), + digest ); - fs::write(&manifest_path, manifest.replace(placeholder, &digest)) - .expect("refresh plugin.yaml sha256"); + fs::write(target.join("plugin.yaml"), manifest).expect("write plugin.yaml"); Some((dir, target)) } @@ -764,3 +786,102 @@ fn platform_reports_strict_unavailable_until_security_gate_passes() { ); assert_eq!(value["platform"]["worker_processes"], true); } + +/// A manifest whose only artifact is for another target must be rejected with +/// `no_matching_artifact` *before* any library is loaded. This test needs no C +/// compiler and runs on every platform, including Windows. +#[test] +fn plugin_foreign_target_is_rejected_without_loading() { + let host = host_triple(); + let foreign = if host == "x86_64-unknown-linux-gnu" { + "aarch64-apple-darwin" + } else { + "x86_64-unknown-linux-gnu" + }; + let dir = TempDir::new("plugin-foreign-target"); + let file = dynamic_library_name(); + dir.write(file, "this is not a shared library"); + let digest = hex::encode(sha2::Sha256::digest(dir.read(file).as_bytes())); + dir.write( + "plugin.yaml", + &format!( + "plugin:\n name: demo.solver\n version: 0.1.0\n abi: \"^1.0\"\n kind: native\n exports:\n - name: solver.add\n signature: \"($a: Int, $b: Int) -> Int\"\n artifacts:\n - target: {foreign}\n file: {file}\n sha256: \"{digest}\"\n" + ), + ); + let output = cli( + dir.path(), + &["plugin", "inspect", dir.path().to_str().unwrap(), "--json"], + ); + assert_exit(&output, 1, "foreign artifact target must fail closed"); + let value = json_stdout(&output); + assert_eq!(value["success"], false); + assert_eq!(value["error"]["code"], "no_matching_artifact"); + let message = value["error"]["message"] + .as_str() + .expect("error message is a string"); + assert!( + message.contains(&host), + "the diagnostic must name the host target `{host}`: {message}" + ); +} + +/// A content-hash mismatch is detected by inspection and the library is never +/// loaded. The test mutates the built artifact after the manifest was sealed. +#[test] +fn plugin_artifact_tampering_is_rejected_before_load() { + let Some((_dir, plugin_dir)) = build_plugin_copy("plugin-tamper") else { + return; + }; + let artifact = plugin_dir.join(dynamic_library_name()); + let mut bytes = fs::read(&artifact).expect("read artifact"); + bytes.push(0); + fs::write(&artifact, bytes).expect("tamper with artifact"); + + let output = cli( + repo_root().as_path(), + &["plugin", "inspect", plugin_dir.to_str().unwrap(), "--json"], + ); + assert_exit(&output, 1, "tampered artifact must fail closed"); + let value = json_stdout(&output); + assert_eq!(value["success"], false); + assert_eq!(value["error"]["code"], "hash_mismatch"); + assert!(value["error"]["message"].as_str().is_some()); +} + +/// An ABI major mismatch reported by the native library is rejected by +/// negotiation before any export runs. +#[test] +fn plugin_abi_major_mismatch_is_rejected_before_execution() { + let Some((_dir, plugin_dir)) = + build_plugin_copy_with("plugin-abi-mismatch", &["-DMQ_SOLVER_REPORT_ABI_MAJOR=2"]) + else { + return; + }; + let dir = TempDir::new("plugin-abi-run"); + dir.write( + "doc.mq.md", + "## main\n\n**结果 = solver.add(a=20, b=22)**\n\n> 打印(结果)\n", + ); + let output = cli( + dir.path(), + &[ + "run", + "doc.mq.md", + "--plugin", + plugin_dir.to_str().unwrap(), + "--trust-native-plugin", + "--allow-plugin", + "demo.solver", + ], + ); + assert_exit(&output, 1, "ABI major mismatch must fail before execution"); + let stderr = stderr_text(&output); + assert!( + stderr.contains("abi_major_mismatch"), + "expected abi_major_mismatch, got: {stderr}" + ); + assert!( + !stdout_text(&output).contains("42"), + "plugin export must not run after an ABI mismatch" + ); +} diff --git a/crates/cli/tests/plugin_worker.rs b/crates/cli/tests/plugin_worker.rs index 2203569..142a770 100644 --- a/crates/cli/tests/plugin_worker.rs +++ b/crates/cli/tests/plugin_worker.rs @@ -42,6 +42,24 @@ fn repo() -> PathBuf { .to_path_buf() } +/// Platform dynamic-library name for the test fixture. +fn library_name() -> &'static str { + if cfg!(target_os = "macos") { + "solver.dylib" + } else { + "solver.so" + } +} + +/// Flags for building a shared library with the platform C compiler. +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") @@ -51,9 +69,10 @@ fn have_cc() -> bool { } fn compile(dir: &Path, extra: &[&str]) -> PathBuf { - let output = dir.join("solver.so"); + let output = dir.join(library_name()); let status = Command::new("cc") - .args(["-std=c11", "-O2", "-shared", "-fPIC"]) + .args(["-std=c11", "-O2"]) + .args(shared_flags()) .arg("-I") .arg(repo().join("include")) .args(extra) @@ -82,10 +101,11 @@ fn write_manifest(dir: &Path, sha: &str) { - math.compute artifacts: - target: {} - file: solver.so + file: {} sha256: "{}" "#, HostTarget::current().triple(), + library_name(), sha ), ) @@ -156,9 +176,14 @@ fn worker_crash_is_isolated_from_the_host() { } fn compile_capability(dir: &Path, extra: &[&str]) -> PathBuf { - let output = dir.join("capability.so"); + let output = dir.join(if cfg!(target_os = "macos") { + "capability.dylib" + } else { + "capability.so" + }); let status = Command::new("cc") - .args(["-std=c11", "-O2", "-shared", "-fPIC"]) + .args(["-std=c11", "-O2"]) + .args(shared_flags()) .arg("-I") .arg(repo().join("include")) .args(extra) @@ -187,10 +212,15 @@ fn write_capability_manifest(dir: &Path, sha: &str) { - fs.read artifacts: - target: {} - file: capability.so + file: {} sha256: "{}" "#, HostTarget::current().triple(), + if cfg!(target_os = "macos") { + "capability.dylib" + } else { + "capability.so" + }, sha ), ) diff --git a/examples/plugin-demo/README.md b/examples/plugin-demo/README.md index d79dce1..c852376 100644 --- a/examples/plugin-demo/README.md +++ b/examples/plugin-demo/README.md @@ -41,7 +41,7 @@ interchangeable. Update `artifacts[0].target` and `sha256` in [`plugin.yaml`](plugin.yaml) to match what you built: ```sh -sha256sum examples/plugin-demo/solver.so +sha256sum examples/plugin-demo/build/solver.so ``` ## Inspect @@ -51,8 +51,8 @@ Manifest validation and artifact verification are implemented by never loads plugin code): ```sh -./examples/plugin-demo/build.sh # builds solver.so and refreshes sha256 -marqdo plugin inspect examples/plugin-demo +./examples/plugin-demo/build.sh # writes build/solver. + manifest +marqdo plugin inspect examples/plugin-demo/build ``` A typical inspection flow is: diff --git a/examples/plugin-demo/build.sh b/examples/plugin-demo/build.sh index 0935707..13c599d 100755 --- a/examples/plugin-demo/build.sh +++ b/examples/plugin-demo/build.sh @@ -1,22 +1,44 @@ #!/usr/bin/env bash -# Build the example C plugin and refresh the sha256 in plugin.yaml. +# Build the example C plugin and generate a host-specific manifest. # -# The artifact is intentionally NOT committed: installing/inspecting a package -# never implies trusting or loading its native code, and the manifest must -# describe the artifact you actually build on your machine. +# The artifact and generated manifest go to `build/`, which is gitignored. The +# tracked `plugin.yaml` is never rewritten by a test or build: it documents the +# manifest shape, while the generated one matches the machine that built it. set -euo pipefail here="$(cd "$(dirname "$0")" && pwd)" repo="$(cd "$here/../.." && pwd)" cd "$repo" -cc -shared -fPIC -I include -o "$here/solver.so" "$here/solver.c" -hash="$(sha256sum "$here/solver.so" | cut -d' ' -f1)" -python3 - "$here/plugin.yaml" "$hash" <<'PY' -import sys, re, pathlib -path, digest = sys.argv[1], sys.argv[2] -p = pathlib.Path(path) -text = p.read_text() -text = re.sub(r'(?m)^(\s*sha256:\s*")[0-9a-fA-F]{64}(")', r'\g<1>' + digest + r'\g<2>', text) -p.write_text(text) -PY -echo "built $here/solver.so (sha256 $hash)" -echo "now run: marqdo plugin inspect $here" + +out="$here/build" +mkdir -p "$out" +host="$(rustc -vV | sed -n 's/^host: //p')" +case "$host" in + *apple-darwin*) ext="dylib"; flags=(-dynamiclib -fPIC) ;; + *windows*) ext="dll"; flags=(-shared) ;; + *) ext="so"; flags=(-shared -fPIC) ;; +esac +artifact="solver.$ext" +cc -std=c11 -O2 "${flags[@]}" -I include -o "$out/$artifact" "$here/solver.c" +if command -v sha256sum >/dev/null 2>&1; then + hash="$(sha256sum "$out/$artifact" | cut -d' ' -f1)" +else + hash="$(shasum -a 256 "$out/$artifact" | cut -d' ' -f1)" +fi +cat > "$out/plugin.yaml" < Int" + requires: + - math.compute + artifacts: + - target: $host + file: $artifact + sha256: "$hash" +YAML +echo "built $out/$artifact for $host (sha256 $hash)" +echo "now run: marqdo plugin inspect $out" diff --git a/examples/plugin-multi-export/README.md b/examples/plugin-multi-export/README.md index c1db83f..a72ca96 100644 --- a/examples/plugin-multi-export/README.md +++ b/examples/plugin-multi-export/README.md @@ -4,11 +4,11 @@ One native library exports two complete names: `solver.add` and `solver.neg`. Both are registered and callable; there is no "first prefix wins" namespace. ```sh -./examples/plugin-multi-export/build.sh +./examples/plugin-multi-export/build.sh # writes build/ for this host marqdo check examples/plugin-multi-export/plugin-call.mq.md \ - --plugin examples/plugin-multi-export + --plugin examples/plugin-multi-export/build marqdo run examples/plugin-multi-export/plugin-call.mq.md \ - --plugin examples/plugin-multi-export \ + --plugin examples/plugin-multi-export/build \ --trust-native-plugin --allow-plugin demo.solver ``` diff --git a/examples/plugin-multi-export/build.sh b/examples/plugin-multi-export/build.sh index 1700189..ca75f13 100755 --- a/examples/plugin-multi-export/build.sh +++ b/examples/plugin-multi-export/build.sh @@ -1,17 +1,44 @@ #!/usr/bin/env bash +# Build the multi-export example and generate a host-specific manifest under +# `build/` (gitignored). The tracked manifest is never rewritten. set -euo pipefail here="$(cd "$(dirname "$0")" && pwd)" repo="$(cd "$here/../.." && pwd)" cd "$repo" -cc -std=c11 -O2 -shared -fPIC -I include -DMQ_SOLVER_MULTI_EXPORT \ - -o "$here/solver.so" examples/plugin-demo/solver.c -hash="$(sha256sum "$here/solver.so" | cut -d' ' -f1)" -python3 - "$here/plugin.yaml" "$hash" <<'PY' -import pathlib, re, sys -path, digest = sys.argv[1], sys.argv[2] -p = pathlib.Path(path) -text = p.read_text() -text = re.sub(r'(?m)^(\s*sha256:\s*")[0-9a-fA-F]{64}(")', r'\g<1>' + digest + r'\g<2>', text) -p.write_text(text) -PY -echo "built $here/solver.so" + +out="$here/build" +mkdir -p "$out" +host="$(rustc -vV | sed -n 's/^host: //p')" +case "$host" in + *apple-darwin*) ext="dylib"; flags=(-dynamiclib -fPIC) ;; + *windows*) ext="dll"; flags=(-shared) ;; + *) ext="so"; flags=(-shared -fPIC) ;; +esac +artifact="solver.$ext" +cc -std=c11 -O2 "${flags[@]}" -I include -DMQ_SOLVER_MULTI_EXPORT \ + -o "$out/$artifact" "$repo/examples/plugin-demo/solver.c" +if command -v sha256sum >/dev/null 2>&1; then + hash="$(sha256sum "$out/$artifact" | cut -d' ' -f1)" +else + hash="$(shasum -a 256 "$out/$artifact" | cut -d' ' -f1)" +fi +cat > "$out/plugin.yaml" < Int" + - name: solver.neg + signature: "(\$a: Int) -> Int" + requires: + - math.compute + artifacts: + - target: $host + file: $artifact + sha256: "$hash" +YAML +echo "built $out/$artifact for $host (sha256 $hash)" +echo "now run: marqdo plugin inspect $out"