Lines
97.32 %
Functions
88.24 %
Branches
100 %
//! A malformed script must be refused with `Error::Resource`, never abort the
//! process by overflowing the native stack of a small compile thread.
use nomiscript::{Compiler, Error, Reader, SymbolTable};
const SELF_BUILDING_CLOSURE: &str = "(defun mk (a) (lambda (x) (let ((p4 4)) (let ((p3 3)) \
(let ((p2 2)) (let ((p1 1)) (let ((p0 0)) (let ((g (mk a))) (funcall g x))))))))) \
(funcall (mk 1) 2)";
fn compile_on_thread(stack_bytes: usize, src: &str) -> Result<Vec<u8>, Error> {
let src = src.to_string();
let worker = std::thread::Builder::new()
.stack_size(stack_bytes)
.spawn(move || {
let program = Reader::parse(&src)?;
let mut symbols = SymbolTable::with_builtins_for_wasm();
Compiler::new().compile(&program, &mut symbols)
})
.map_err(|e| Error::Compile(e.to_string()))?;
worker
.join()
.unwrap_or_else(|_| Err(Error::Compile("compile thread panicked".to_string())))
}
#[test]
fn every_stack_size_reaches_the_shared_ceiling() {
for kib in [512, 1024, 8192] {
let outcome = compile_on_thread(kib * 1024, SELF_BUILDING_CLOSURE);
assert!(
matches!(&outcome, Err(Error::Resource(m)) if m.contains("shared budget")),
"{kib} KiB stack: {outcome:?}"
);
fn nesting_macro(level: usize, width: usize, wrap: &str) -> String {
format!(
"(defmacro m (n) (if (= n 0) 0 (do ((i 0 (+ i 1)) (x (list 'm (- n 1)) {wrap})) \
((= i {width}) x)))) (m {level})"
)
fn nested_lets(depth: usize) -> String {
let opens: String = (0..depth).map(|i| format!("(let ((p{i} {i})) ")).collect();
format!("{opens}0{}", ")".repeat(depth))
fn let_chain_macro(width: usize) -> String {
let opens = "(let ((v 1)) ".repeat(width);
let closes = ")".repeat(width);
format!("(defmacro m (n) (if (= n 0) 0 `{opens}(m ,(- n 1)){closes})) ")
fn template_let_chain(width: usize, level: usize) -> String {
format!("{}(m {level})", let_chain_macro(width))
const COMPOSED_SCOPES: [(&str, &str); 4] = [
(
"s-do",
"(let ((s 0)) (do ((i 0 (+ i 1))) ((= i 2) s) (setf s (+ s 1)) (funcall (lambda () i)) (m 23)))",
),
"s-defun",
"(defun f (x) (setf x 2) (funcall (lambda () x)) (m 23)) (f 1)",
"s-lambdaparam",
"(funcall (lambda (x) (setf x 2) (funcall (lambda () x)) (m 23)) 1)",
("s-print", "(print (list 1 (m 23)))"),
];
fn abort_inputs() -> Vec<(String, String)> {
let mut inputs = vec![
("let60".to_string(), nested_lets(60)),
"macdeeplet".to_string(),
"(defmacro w () (do ((i 0 (+ i 1)) (x 0 (list 'let (list (list 'v i)) x))) \
((= i 120) x))) (w)"
.to_string(),
for level in [6, 23] {
inputs.push((
format!("mc-plus-L{level}-W120"),
nesting_macro(level, 120, "(list '+ 1 x)"),
));
format!("mc-if-L{level}-W120"),
nesting_macro(level, 120, "(list 'if 't x 0)"),
"mc-let-L23-W100".to_string(),
nesting_macro(23, 100, "(list 'let (list (list 'v i)) x)"),
inputs.push(("let57-top-L23".to_string(), template_let_chain(57, 23)));
for (name, scope) in COMPOSED_SCOPES {
format!("{name}-L23"),
format!("{}{scope}", let_chain_macro(57)),
"cf3-05".to_string(),
include_str!("acceptance/cf3-05-factory-lambda-binds-factory.nms").to_string(),
"5-let-factory".to_string(),
SELF_BUILDING_CLOSURE.to_string(),
inputs
fn deeply_nested_forms_never_abort_on_a_small_stack() {
for kib in [512, 1024, 2048] {
for (name, src) in &abort_inputs() {
let outcome = compile_on_thread(kib * 1024, src);
matches!(&outcome, Ok(_) | Err(Error::Resource(_))),
"{name} on {kib} KiB: {outcome:?}"
/// Macro expansions compose into one tree: two expansions whose summed own depth
/// stays at the ceiling compile, one more level each is a resource error.
fn composed_expansions_are_bounded_at_the_ceiling() {
for kib in [512, 2048] {
let at_cap = compile_on_thread(kib * 1024, &nesting_macro(2, 63, "(list '+ 1 x)"));
assert!(at_cap.is_ok(), "{kib} KiB at the ceiling: {at_cap:?}");
let over = compile_on_thread(kib * 1024, &nesting_macro(2, 64, "(list '+ 1 x)"));
matches!(&over, Err(Error::Resource(m)) if m.contains("nests deeper")),
"{kib} KiB over the ceiling: {over:?}"
const NESTING_MACRO: &str = "(defmacro w (n) (if (= n 0) 7 (do ((i 0 (+ i 1)) \
(y (list 'w (- n 1)) (list '+ 0 y))) ((= i 120) y))))";
const CAPTURE_NESTING_USE: &str = "(let ((g (lambda (x) (+ x (w 24))))) (funcall g 2))";
fn capture_function_chain() -> String {
let defs: String = (1..=25)
.map(|i| format!("(deff f{i} f{}) ", i + 1))
.rev()
.collect();
"(defmacro deff (name next) (list 'defun name '(x) (do ((i 0 (+ i 1)) \
(y (list next 'x) (list '+ 0 y))) ((= i 120) y)))) (defun f26 (x) x) {defs}\
(let ((g (lambda (x) (f1 x)))) (funcall g 2))"
fn refusal_message(stack_bytes: usize, src: &str) -> String {
match compile_on_thread(stack_bytes, src) {
Err(Error::Resource(message)) => message,
other => format!("not a resource refusal: {other:?}"),
/// The closure capture walk follows macro expansions and function bodies; the
/// composed depth must end in the walk's own structured refusal on every stack size.
fn the_capture_walk_refuses_composed_depth_on_a_small_stack() {
let bare = refusal_message(2 * 1024 * 1024, &format!("{NESTING_MACRO} (w 24)"));
assert!(bare.contains("nests deeper than 128 levels"), "{bare}");
for kib in [512, 1024, 1536, 2048] {
let size = kib * 1024;
let nested = refusal_message(size, &format!("{NESTING_MACRO} {CAPTURE_NESTING_USE}"));
assert_eq!(nested, bare, "capD24 on {kib} KiB");
let chain = refusal_message(size, &capture_function_chain());
chain.contains("shared budget of 24"),
"capfun24 on {kib} KiB: {chain}"