Lines
100 %
Functions
Branches
// Skipped under Miri: spawns the built nms binary.
#![cfg(not(miri))]
#![cfg(unix)]
//! Compile-time evaluation that keeps re-nesting an accumulator must stop
//! with a structured error once the datum passes `MAX_ELAB_DEPTH`, never
//! overflow the compiler's native stack cloning it; a long flat list must
//! compile without one native frame per element. Each case runs in a
//! subprocess under a 2 MiB stack so a regression aborts only the child.
mod common;
use std::os::unix::process::ExitStatusExt;
use common::{
assert_fails_cleanly_with, assert_nms_source_prints_with_stack_limit, nms_with_stack_limit,
run_nms_with_stack_limit,
};
const DEPTH_ERROR: &str = "elaboration depth limit";
fn elements(n: usize) -> String {
(0..n).map(|i| i.to_string()).collect::<Vec<_>>().join(" ")
}
/// A macro whose DOLIST re-binds `acc` to `step` for each of `n` elements,
/// expanding to `result`, forced by a DEFVAR.
fn dolist_macro_source(n: usize, step: &str, result: &str) -> String {
format!(
"(defun wrap (&rest xs) xs)\n\
(defmacro m () (let ((acc nil)) (dolist (x (list {})) (setf acc {step})) {result}))\n\
(defvar *r* (m))\n",
elements(n)
)
fn assert_accumulation_is_refused(prefix: &str, n: usize, step: &str, result: &str) {
let out = run_nms_with_stack_limit(prefix, &dolist_macro_source(n, step, result), 2048);
assert_fails_cleanly_with(&out, DEPTH_ERROR);
#[test]
fn a_list_building_an_expansion_form_is_refused() {
let source = format!(
"(defmacro m () (let ((acc 0)) (dolist (x (list {})) (setf acc (list '+ acc 1))) acc))\n\
elements(1000)
);
let out = run_nms_with_stack_limit("nms_depth_list_plus_", &source, 2048);
fn a_list_nesting_the_accumulator_is_refused() {
assert_accumulation_is_refused("nms_depth_list_", 3000, "(list acc x)", "(list 'quote acc)");
fn a_list_quoting_the_accumulator_is_refused() {
assert_accumulation_is_refused("nms_depth_quote_", 3000, "(list 'quote acc)", "acc");
fn a_cons_nesting_the_accumulator_is_refused() {
assert_accumulation_is_refused(
"nms_depth_cons_",
3000,
"(cons acc nil)",
"(list 'quote acc)",
fn a_quasiquote_nesting_the_accumulator_is_refused() {
assert_accumulation_is_refused("nms_depth_qq_", 3000, "`(,acc)", "(list 'quote acc)");
fn a_map_nesting_the_accumulator_is_refused() {
"nms_depth_map_",
"(map (lambda (y) acc) (list 1))",
fn a_rest_parameter_nesting_the_accumulator_is_refused() {
assert_accumulation_is_refused("nms_depth_rest_", 3000, "(wrap acc)", "(list 'quote acc)");
fn a_fold_nesting_the_accumulator_is_refused() {
"(defmacro m () (list 'quote (fold (lambda (acc x) (list acc x)) nil (list {}))))\n\
elements(1500)
let out = run_nms_with_stack_limit("nms_depth_fold_", &source, 2048);
fn a_shallow_accumulation_still_expands() {
"{}\"ok\"\n",
dolist_macro_source(20, "(list acc x)", "(list 'quote acc)")
assert_nms_source_prints_with_stack_limit("nms_depth_shallow_", &source, "ok", 2048);
fn a_shallow_fold_accumulation_still_expands() {
(defvar *r* (m))\n\"ok\"\n",
elements(20)
assert_nms_source_prints_with_stack_limit("nms_depth_shallow_fold_", &source, "ok", 2048);
/// An instance exists at run time only, so a macro body that makes one is
/// refused at expansion time, however deep the accumulation.
fn a_struct_made_in_a_macro_body_is_refused() {
"(defstruct node next)\n\
(defmacro m () (let ((acc nil)) (dolist (x (list {})) (setf acc (make-node :next acc))) (list 'quote acc)))\n\
let out = run_nms_with_stack_limit("nms_struct_macro_", &source, 2048);
assert_fails_cleanly_with(
&out,
"a macro body cannot use a runtime value or effect at expansion time",
fn runtime_list(extra: usize) -> String {
format!("(list (entity-count) {})", "1 ".repeat(extra))
/// A long flat runtime `(list …)` is shallow source, so no depth guard
/// applies; building its cells must not recurse once per element. Compile
/// only: running a module this large is dominated by wasmtime's own code
/// generation, which is not what is under test.
fn a_five_thousand_element_list_compiles_under_a_small_stack() {
let out = nms_with_stack_limit(
"nms_flat_list_",
&format!("(length {})", runtime_list(5000)),
2048,
&["--compile"],
assert_eq!(out.status.signal(), None, "{:?}", out.status);
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
fn a_long_flat_list_still_builds_every_cell_in_order() {
assert_nms_source_prints_with_stack_limit(
"nms_flat_list_run_",
&format!(
"(let ((l {})) (list (length l) (car l) (car (cdr l))))",
runtime_list(300)
),
"(301 0 1)",