Lines
99.47 %
Functions
100 %
Branches
// Skipped under Miri: spawns the built nms binary (which runs wasm via
// wasmtime); subprocess execution and Cranelift can't run under Miri.
#![cfg(not(miri))]
//! Smoke tests for the `nms --test PATH` subcommand. Invokes the
//! built binary so the runner's argv/exit-code contract is what CI
//! actually checks: a non-zero exit code is the only signal an
//! external harness sees when a deftest fails.
mod common;
use std::path::PathBuf;
use std::process::Command;
#[cfg(unix)]
use common::{
assert_fails_cleanly_with, assert_nms_source_prints_with_stack_limit, run_nms_with_stack_limit,
};
use common::{nms_bin, tempfile_dir};
fn samples_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("nomiscript/tests/nms_runner_samples")
}
#[test]
fn test_subcommand_runs_all_files_in_dir() {
let out = Command::new(nms_bin())
.arg("--test")
.arg(samples_dir())
.output()
.expect("invoke nms");
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
out.status.success(),
"expected success, got {:?}\nstdout: {stdout}\nstderr: {stderr}",
out.status
);
assert!(stdout.contains("0 failed"), "stdout: {stdout}");
fn test_subcommand_accepts_single_file() {
let path = samples_dir().join("arithmetic.nms");
.arg(&path)
assert!(out.status.success(), "stderr: {:?}", out.stderr);
assert!(stdout.contains("5 passed"), "stdout: {stdout}");
fn long_and_or_chains_do_not_overflow_the_compiler_stack() {
// Regression: a naive recursive rewrite of `and`/`or` into nested IFs
// (`(and nil t t t ... 50000 times)`) used to recurse the compiler's
// native stack to build/compile the tree, aborting the WHOLE process
// with SIGABRT. Run as a genuine subprocess (not in-process) so a
// regression here crashes only the child — this test can then assert
// on that crash instead of taking the whole test binary down with it.
let path = samples_dir().join("long_and_or_chains.nms");
{
use std::os::unix::process::ExitStatusExt;
assert_eq!(
out.status.signal(),
None,
"nms subprocess was killed by a signal (status: {:?}) — likely a native \
stack overflow (SIGABRT) compiling a long AND/OR chain",
assert!(stdout.contains("4 passed"), "stdout: {stdout}");
/// Recursing once per clause overflows a 2 MiB stack from ~1000 clauses
/// (binding position) and ~500 (stack position); 2000 keeps a 2x margin.
const LONG_COND_CHAIN_CLAUSE_COUNT: usize = 2_000;
const LONG_COND_CHAIN_STACK_KIB: u32 = 2048;
fn long_cond_chain_clauses() -> String {
"(nil) ".repeat(LONG_COND_CHAIN_CLAUSE_COUNT)
fn long_cond_chain_in_binding_position_does_not_overflow_the_compiler_stack() {
// Regression: a naive recursive rewrite of a long flat COND's
// constant-false clause prefix (`(cond (nil) (nil) ... (t 7))`) used to
// self-recurse once per clause to build/compile the chain, aborting the
// WHOLE process with SIGABRT under a 2 MiB stack. This covers the eval
// path (a `let` binding's initializer).
let source = format!("(let ((x (cond {}(t 7)))) x)\n", long_cond_chain_clauses());
assert_nms_source_prints_with_stack_limit(
"nms_long_cond_chain_binding_",
&source,
"7",
LONG_COND_CHAIN_STACK_KIB,
fn long_cond_chain_in_stack_position_does_not_overflow_the_compiler_stack() {
// Same regression as above, covering the stack path (COND as a bare
// top-level value-producing form).
let source = format!("(cond {}(t 7))\n", long_cond_chain_clauses());
"nms_long_cond_chain_stack_",
fn cond_clauses_with_bodies_iterate_under_a_constrained_native_stack() {
// Codex regression: constant-false COND clauses WITH A BODY (not just
// bodyless ones) used to recurse through `if_form` once per clause —
// `(and t (cond (nil 8) (nil 8) ... (t 7)))` with 128 such clauses
// SIGABRTs under a 2 MiB stack (an 8 MiB stack survives it, masking the
// bug). Constant-test clauses, with or without a body, now iterate.
let clauses = "(nil 8) ".repeat(128);
let source = format!("(and t (cond {clauses}(t 7)))\n");
assert_nms_source_prints_with_stack_limit("nms_cond_body_clauses_2mib_", &source, "7", 2048);
/// The nomiscript source shape (a `(cond ...)` or `(and ...)`) that
/// exercises exactly `count` genuinely-runtime clauses/operands — each
/// built from a non-foldable `(primary-entity-idx)` call so the compiler
/// cannot constant-fold past the recursive runtime-guard path
/// `MAX_SHARED_RECURSION_DEPTH` protects.
fn runtime_chain_source(head: &str, count: usize) -> String {
match head {
"COND" => {
let clauses: String = (0..count)
.map(|i| format!("((= (primary-entity-idx) {}) {i}) ", i + 1))
.collect();
format!("(cond {clauses}(t 999))\n")
"AND" => {
let chain: String = (0..count)
.map(|i| format!("(= (primary-entity-idx) {}) ", i + 1000))
format!("(and {chain}999)\n")
_ => unreachable!("only COND/AND are exercised here"),
fn cond_runtime_chain_past_the_old_depth_ceiling_compiles_and_runs() {
// COND's runtime-clause chain is walked in a loop and charges no shared
// recursion budget, so 128 runtime clauses compile and run under a
// 2 MiB stack. AND/OR (below) still recurse and keep the ceiling.
let source = runtime_chain_source("COND", 128);
assert_nms_source_prints_with_stack_limit("nms_cond_depth_ceiling_", &source, "999", 2048);
fn and_runtime_chain_past_the_depth_ceiling_errors_not_crashes() {
// Same reasoning as the COND test above: `MAX_SHARED_RECURSION_DEPTH`
// is the sole ceiling.
let source = runtime_chain_source("AND", 128);
let out = run_nms_with_stack_limit("nms_and_depth_ceiling_", &source, 2048);
assert_fails_cleanly_with(&out, "shared budget");
fn cond_runtime_chain_under_the_depth_ceiling_still_runs_under_a_constrained_stack() {
// A realistic-depth genuinely-runtime chain (well under
// `MAX_SHARED_RECURSION_DEPTH`) still compiles and runs correctly
// under the same constrained 2 MiB stack.
let source = runtime_chain_source("COND", 16);
assert_nms_source_prints_with_stack_limit("nms_cond_depth_ok_", &source, "999", 2048);
/// A nomiscript source combining four INDEPENDENTLY-ceilinged compile-time
/// recursion families, each nested inside the last: `defun_depth` chained
/// defuns (f0 calls f1 calls f2 … the last calls `(m0)`), `macro_depth`
/// chained macro expansions (m0 expands to m1 expands to m2 … the last
/// expands to `9`), then a top-level form that runs an `and_operands`-long
/// genuinely-runtime AND chain followed by a `cond_clauses`-long
/// genuinely-runtime COND, whose final `(t (f0))` clause enters the
/// defun/macro chain. Each family alone can stay individually under its own
/// ceiling while the combination still exhausts a constrained native stack
/// (`primary-entity-idx` — a non-foldable entity accessor — is what forces
/// each AND/COND test to be GENUINELY runtime rather than constant-folded).
fn combined_recursion_source(
defun_depth: usize,
macro_depth: usize,
and_operands: usize,
cond_clauses: usize,
) -> String {
let mut lines = Vec::new();
for i in 0..defun_depth {
let body = if i + 1 < defun_depth {
format!("(f{})", i + 1)
} else {
"(m0)".to_string()
lines.push(format!("(defun f{i} () {body})"));
for i in 0..macro_depth {
let body = if i + 1 < macro_depth {
format!("(m{})", i + 1)
"9".to_string()
lines.push(format!("(defmacro m{i} () '{body})"));
let and_preds = "(= (primary-entity-idx) 0) ".repeat(and_operands);
let cond_clauses_src = "((= (primary-entity-idx) 1) 1) ".repeat(cond_clauses);
lines.push(format!(
"(and {and_preds}(cond {cond_clauses_src}(t (f0))))"
));
lines.join("\n") + "\n"
fn combined_recursion_families_do_not_overflow_the_compiler_stack() {
// Codex regression: 30 nested defuns + 60 nested macro expansions + a
// 31-operand runtime AND + a 31-clause runtime COND — every individual
// counter (defun inlining ≤ 32, macro expansion ≤ 64, AND/OR ≤ 32,
// COND ≤ 32) stays under its OWN ceiling, but nothing previously
// bounded the combination, and their native-stack costs add up. Must
// now fail with the STRUCTURED shared-budget error, never a crash.
let source = combined_recursion_source(30, 60, 31, 31);
let out = run_nms_with_stack_limit("nms_combined_recursion_", &source, 2048);
fn combined_recursion_families_under_the_shared_budget_still_run_under_a_constrained_stack() {
// A small, realistic mix of all four families together — well under
// the shared ceiling — still compiles and runs correctly under the
// same constrained 2 MiB stack.
let source = combined_recursion_source(2, 2, 2, 2);
assert_nms_source_prints_with_stack_limit("nms_combined_recursion_ok_", &source, "9", 2048);
fn test_subcommand_nonzero_exit_on_failure() {
let dir = tempfile_dir("nms_runner_failure_");
std::fs::write(
dir.join("a.nms"),
"(deftest will-fail (assert-equal 1 2))\n",
)
.unwrap();
.arg(&dir)
assert!(!out.status.success(), "expected non-zero exit on failure");
assert!(stdout.contains("1 failed"), "stdout: {stdout}");
fn coverage_flag_appends_coverage_section() {
.arg("--coverage")
assert!(stdout.contains("--- coverage ---"), "stdout: {stdout}");
// sandbox nms has no host fns registered, so the dump is the
// explanatory placeholder rather than a native-fn list. The rpc
// crate's unit tests assert the populated path.
stdout.contains("(no native fns referenced)"),
"stdout: {stdout}"
fn invalid_profile_strategy_errors_with_clear_message() {
.arg("--profile=bogus")
.arg("-e")
.arg("(+ 1 2)")
assert!(!out.status.success(), "expected non-zero exit");
stderr.contains("unknown --profile strategy"),
"stderr: {stderr}"
fn perfmap_profile_strategy_accepted() {
// PerfMap is the lighter Linux profiler agent. We don't validate
// the output file (path depends on PID); just confirm the flag is
// accepted and the eval completes.
.arg("--profile=perfmap")
.arg("(+ 2 2)")
assert!(stdout.contains('4'), "stdout: {stdout}");
fn test_subcommand_errors_on_empty_dir() {
let dir = tempfile_dir("nms_runner_empty_");
assert!(!out.status.success(), "expected non-zero exit on empty dir");