From b898c419feee27db34e8e283d6a87a6543965554 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sat, 21 Nov 2026 18:35:33 +0800 Subject: [PATCH] test(portable): fix native stack and process-fixture assumptions macOS arm64 aborted in nesting_depth_limit_is_enforced because the test-harness thread stack is smaller than a production main thread. Run that contract test on an explicit 8 MiB stack; the 128-level parser bound itself is unchanged. The stdlib process product test hard-coded /bin/echo, which does not exist on Windows. Use the test CLI itself as the granted program: the fixture reads MARQDO_TEST_PROCESS_PROGRAM from the granted env, runs it with --version, and the harness compares against the same invocation. process.run now forwards SYSTEMROOT/WINDIR on Windows (process creation/DLL loading, not a capability), and the bundled stdlib digest is regenerated for the changed test fixture. --- crates/cli/tests/stdlib_product.rs | 96 +++++++++++++++++--------- crates/marqdo-expression/src/parser.rs | 21 ++++++ crates/marqdo-host/src/process.rs | 17 ++++- stdlib/stdlib.json | 2 +- stdlib/tests/process.mq.md | 5 +- 5 files changed, 102 insertions(+), 39 deletions(-) diff --git a/crates/cli/tests/stdlib_product.rs b/crates/cli/tests/stdlib_product.rs index 468599a..be201c5 100644 --- a/crates/cli/tests/stdlib_product.rs +++ b/crates/cli/tests/stdlib_product.rs @@ -37,8 +37,8 @@ struct TestFile { name: String, stdin: &'static str, stdout: &'static str, - args: &'static [&'static str], - env: &'static [(&'static str, &'static str)], + args: Vec, + env: Vec<(String, String)>, } fn product_tests() -> Vec { @@ -52,8 +52,8 @@ fn product_tests() -> Vec { name: String::new(), stdin: "", stdout: "", - args: &[], - env: &[], + args: Vec::new(), + env: Vec::new(), }; } let name = path.file_name().unwrap().to_string_lossy().to_string(); @@ -63,35 +63,50 @@ fn product_tests() -> Vec { "stream.mq.md" => ("", "stream\n"), _ => ("", ""), }; - let args = if name == "fs.mq.md" { - &[ - "--allow-read", - "out", - "--allow-read", - "out/**", - "--allow-write", - "out", - "--allow-write", - "out/**", - ][..] - } else if name == "env.mq.md" { - &[ - "--allow-env", - "MARQDO_TEST_ENV", - "--allow-env", - "MARQDO_MISSING_ENV", - ][..] - } else if name == "process.mq.md" { - &["--allow-process", "/bin/echo"][..] - } else if name == "random.mq.md" { - &["--allow-random"][..] - } else { - &[][..] + let text_args = |values: &[&str]| -> Vec { + values.iter().map(|value| value.to_string()).collect() }; - let env = if name == "env.mq.md" { - &[("MARQDO_TEST_ENV", "env-value")][..] + let (args, env) = if name == "fs.mq.md" { + ( + text_args(&[ + "--allow-read", + "out", + "--allow-read", + "out/**", + "--allow-write", + "out", + "--allow-write", + "out/**", + ]), + Vec::new(), + ) + } else if name == "env.mq.md" { + ( + text_args(&[ + "--allow-env", + "MARQDO_TEST_ENV", + "--allow-env", + "MARQDO_MISSING_ENV", + ]), + vec![("MARQDO_TEST_ENV".to_string(), "env-value".to_string())], + ) + } else if name == "process.mq.md" { + // Use the test binary itself as the granted program: it exists + // on every native platform, unlike the old `/bin/echo` fixture. + let program = env!("CARGO_BIN_EXE_marqdo").to_string(); + ( + vec![ + "--allow-env".to_string(), + "MARQDO_TEST_PROCESS_PROGRAM".to_string(), + "--allow-process".to_string(), + program.clone(), + ], + vec![("MARQDO_TEST_PROCESS_PROGRAM".to_string(), program)], + ) + } else if name == "random.mq.md" { + (text_args(&["--allow-random"]), Vec::new()) } else { - &[][..] + (Vec::new(), Vec::new()) }; TestFile { name, @@ -114,11 +129,15 @@ fn run_product_test(file: &TestFile) -> serde_json::Value { let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo")) .args(["run"]) .arg(stdlib_dir().join("tests").join(&file.name)) - .args(file.args) + .args(file.args.iter()) .args(["--json"]) .current_dir(stdlib_dir()) .env("MARQDO_STDLIB_DIR", stdlib_dir()) - .envs(file.env.iter().copied()) + .envs( + file.env + .iter() + .map(|(key, value)| (key.as_str(), value.as_str())), + ) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) @@ -380,7 +399,16 @@ fn process_test_runs_an_explicitly_granted_program() { let items = report["value"]["items"].as_array().expect("list"); assert_eq!(items[0]["value"], true); assert_eq!(items[1]["decimal"], "0"); - assert_eq!(items[2]["value"], "hello\n"); + let expected = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .arg("--version") + .output() + .expect("run marqdo --version"); + let expected_stdout = String::from_utf8_lossy(&expected.stdout); + assert_eq!( + items[2]["value"].as_str().expect("process stdout"), + expected_stdout.as_ref(), + "process stdout must match the granted program" + ); assert_eq!(items[3]["value"], ""); } diff --git a/crates/marqdo-expression/src/parser.rs b/crates/marqdo-expression/src/parser.rs index 042ee7f..7c49e0c 100644 --- a/crates/marqdo-expression/src/parser.rs +++ b/crates/marqdo-expression/src/parser.rs @@ -1377,8 +1377,29 @@ mod tests { format!("{}1{}", "(".repeat(depth), ")".repeat(depth)) } + /// Run a deeply recursive parser test on an explicit stack. + /// + /// The test harness gives each test thread a small stack on some native + /// platforms (macOS arm64 aborted with a stack overflow at the 128-level + /// nesting check). Production parsing happens on the main thread; pinning + /// this test to 8 MiB keeps the contract test meaningful without depending + /// on the harness default. + fn with_parser_stack(body: F) { + std::thread::Builder::new() + .name("parser-nesting-depth".to_string()) + .stack_size(8 * 1024 * 1024) + .spawn(body) + .expect("spawn parser nesting thread") + .join() + .expect("parser nesting thread panicked"); + } + #[test] fn nesting_depth_limit_is_enforced() { + with_parser_stack(nesting_depth_limit_is_enforced_body); + } + + fn nesting_depth_limit_is_enforced_body() { assert!(parse_expression(&nested_parens(MAX_NESTING_DEPTH)).is_ok()); let err = parse_expression(&nested_parens(MAX_NESTING_DEPTH + 1)).unwrap_err(); assert_eq!(err.code, "expr.parse"); diff --git a/crates/marqdo-host/src/process.rs b/crates/marqdo-host/src/process.rs index 320a487..0e57b80 100644 --- a/crates/marqdo-host/src/process.rs +++ b/crates/marqdo-host/src/process.rs @@ -27,14 +27,25 @@ pub(crate) fn run_process( ctx: &ExecutionContext, ) -> Result { const LIMIT: usize = 1024 * 1024; - let child = Command::new(program) + let mut command = Command::new(program); + command .args(args) .current_dir(root) .env_clear() .stdin(Stdio::null()) .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn(); + .stderr(Stdio::piped()); + #[cfg(windows)] + { + // Windows process creation and DLL loading require the system root; + // this is not a package-resolution path or a capability grant. + for name in ["SYSTEMROOT", "WINDIR"] { + if let Some(value) = std::env::var_os(name) { + command.env(name, value); + } + } + } + let child = command.spawn(); let child = match child { Ok(child) => child, Err(error) if error.kind() == std::io::ErrorKind::NotFound => { diff --git a/stdlib/stdlib.json b/stdlib/stdlib.json index 6ee8cfe..a2e8d2a 100644 --- a/stdlib/stdlib.json +++ b/stdlib/stdlib.json @@ -1,5 +1,5 @@ { "name": "marqdo-stdlib", "version": "0.1.0", - "digest": "sha256:c3fca0cc3bd8f89f0f45d52418113613dd0613711cfdba76b514e5e6244409d2" + "digest": "sha256:b94f1a48ca6619689584aca3af7befac64ac9fdb7ea7c0a9ccb491df8df226b8" } diff --git a/stdlib/tests/process.mq.md b/stdlib/tests/process.mq.md index c4731ee..6976962 100644 --- a/stdlib/tests/process.mq.md +++ b/stdlib/tests/process.mq.md @@ -1,5 +1,6 @@ --- import: + env: "std:env" process: "std:process" --- @@ -7,6 +8,8 @@ import: ## main -**结果 = process.运行("/bin/echo", ["hello"])** +**程序 = env.读取("MARQDO_TEST_PROCESS_PROGRAM")** + +**结果 = process.运行(程序.value, ["--version"])** `return [结果.ok, 结果.value.status, 结果.value.stdout, 结果.value.stderr]`