1
// Skipped under Miri: spawns the built nms binary.
2
#![cfg(not(miri))]
3
#![cfg(unix)]
4

            
5
//! Compile-time evaluation that keeps re-nesting an accumulator must stop
6
//! with a structured error once the datum passes `MAX_ELAB_DEPTH`, never
7
//! overflow the compiler's native stack cloning it; a long flat list must
8
//! compile without one native frame per element. Each case runs in a
9
//! subprocess under a 2 MiB stack so a regression aborts only the child.
10

            
11
mod common;
12

            
13
use std::os::unix::process::ExitStatusExt;
14

            
15
use common::{
16
    assert_fails_cleanly_with, assert_nms_source_prints_with_stack_limit, nms_with_stack_limit,
17
    run_nms_with_stack_limit,
18
};
19

            
20
const DEPTH_ERROR: &str = "elaboration depth limit";
21

            
22
11
fn elements(n: usize) -> String {
23
20560
    (0..n).map(|i| i.to_string()).collect::<Vec<_>>().join(" ")
24
11
}
25

            
26
/// A macro whose DOLIST re-binds `acc` to `step` for each of `n` elements,
27
/// expanding to `result`, forced by a DEFVAR.
28
7
fn dolist_macro_source(n: usize, step: &str, result: &str) -> String {
29
7
    format!(
30
        "(defun wrap (&rest xs) xs)\n\
31
7
         (defmacro m () (let ((acc nil)) (dolist (x (list {})) (setf acc {step})) {result}))\n\
32
         (defvar *r* (m))\n",
33
7
        elements(n)
34
    )
35
7
}
36

            
37
6
fn assert_accumulation_is_refused(prefix: &str, n: usize, step: &str, result: &str) {
38
6
    let out = run_nms_with_stack_limit(prefix, &dolist_macro_source(n, step, result), 2048);
39
6
    assert_fails_cleanly_with(&out, DEPTH_ERROR);
40
6
}
41

            
42
#[test]
43
1
fn a_list_building_an_expansion_form_is_refused() {
44
1
    let source = format!(
45
        "(defmacro m () (let ((acc 0)) (dolist (x (list {})) (setf acc (list '+ acc 1))) acc))\n\
46
         (defvar *r* (m))\n",
47
1
        elements(1000)
48
    );
49
1
    let out = run_nms_with_stack_limit("nms_depth_list_plus_", &source, 2048);
50
1
    assert_fails_cleanly_with(&out, DEPTH_ERROR);
51
1
}
52

            
53
#[test]
54
1
fn a_list_nesting_the_accumulator_is_refused() {
55
1
    assert_accumulation_is_refused("nms_depth_list_", 3000, "(list acc x)", "(list 'quote acc)");
56
1
}
57

            
58
#[test]
59
1
fn a_list_quoting_the_accumulator_is_refused() {
60
1
    assert_accumulation_is_refused("nms_depth_quote_", 3000, "(list 'quote acc)", "acc");
61
1
}
62

            
63
#[test]
64
1
fn a_cons_nesting_the_accumulator_is_refused() {
65
1
    assert_accumulation_is_refused(
66
1
        "nms_depth_cons_",
67
1
        3000,
68
1
        "(cons acc nil)",
69
1
        "(list 'quote acc)",
70
1
    );
71
1
}
72

            
73
#[test]
74
1
fn a_quasiquote_nesting_the_accumulator_is_refused() {
75
1
    assert_accumulation_is_refused("nms_depth_qq_", 3000, "`(,acc)", "(list 'quote acc)");
76
1
}
77

            
78
#[test]
79
1
fn a_map_nesting_the_accumulator_is_refused() {
80
1
    assert_accumulation_is_refused(
81
1
        "nms_depth_map_",
82
1
        3000,
83
1
        "(map (lambda (y) acc) (list 1))",
84
1
        "(list 'quote acc)",
85
1
    );
86
1
}
87

            
88
#[test]
89
1
fn a_rest_parameter_nesting_the_accumulator_is_refused() {
90
1
    assert_accumulation_is_refused("nms_depth_rest_", 3000, "(wrap acc)", "(list 'quote acc)");
91
1
}
92

            
93
#[test]
94
1
fn a_fold_nesting_the_accumulator_is_refused() {
95
1
    let source = format!(
96
        "(defmacro m () (list 'quote (fold (lambda (acc x) (list acc x)) nil (list {}))))\n\
97
         (defvar *r* (m))\n",
98
1
        elements(1500)
99
    );
100
1
    let out = run_nms_with_stack_limit("nms_depth_fold_", &source, 2048);
101
1
    assert_fails_cleanly_with(&out, DEPTH_ERROR);
102
1
}
103

            
104
#[test]
105
1
fn a_shallow_accumulation_still_expands() {
106
1
    let source = format!(
107
        "{}\"ok\"\n",
108
1
        dolist_macro_source(20, "(list acc x)", "(list 'quote acc)")
109
    );
110
1
    assert_nms_source_prints_with_stack_limit("nms_depth_shallow_", &source, "ok", 2048);
111
1
}
112

            
113
#[test]
114
1
fn a_shallow_fold_accumulation_still_expands() {
115
1
    let source = format!(
116
        "(defmacro m () (list 'quote (fold (lambda (acc x) (list acc x)) nil (list {}))))\n\
117
         (defvar *r* (m))\n\"ok\"\n",
118
1
        elements(20)
119
    );
120
1
    assert_nms_source_prints_with_stack_limit("nms_depth_shallow_fold_", &source, "ok", 2048);
121
1
}
122

            
123
/// An instance exists at run time only, so a macro body that makes one is
124
/// refused at expansion time, however deep the accumulation.
125
#[test]
126
1
fn a_struct_made_in_a_macro_body_is_refused() {
127
1
    let source = format!(
128
        "(defstruct node next)\n\
129
         (defmacro m () (let ((acc nil)) (dolist (x (list {})) (setf acc (make-node :next acc))) (list 'quote acc)))\n\
130
         (defvar *r* (m))\n",
131
1
        elements(20)
132
    );
133
1
    let out = run_nms_with_stack_limit("nms_struct_macro_", &source, 2048);
134
1
    assert_fails_cleanly_with(
135
1
        &out,
136
1
        "a macro body cannot use a runtime value or effect at expansion time",
137
1
    );
138
1
}
139

            
140
2
fn runtime_list(extra: usize) -> String {
141
2
    format!("(list (entity-count) {})", "1 ".repeat(extra))
142
2
}
143

            
144
/// A long flat runtime `(list …)` is shallow source, so no depth guard
145
/// applies; building its cells must not recurse once per element. Compile
146
/// only: running a module this large is dominated by wasmtime's own code
147
/// generation, which is not what is under test.
148
#[test]
149
1
fn a_five_thousand_element_list_compiles_under_a_small_stack() {
150
1
    let out = nms_with_stack_limit(
151
1
        "nms_flat_list_",
152
1
        &format!("(length {})", runtime_list(5000)),
153
1
        2048,
154
1
        &["--compile"],
155
1
    );
156
1
    assert_eq!(out.status.signal(), None, "{:?}", out.status);
157
1
    assert!(
158
1
        out.status.success(),
159
        "stderr: {}",
160
        String::from_utf8_lossy(&out.stderr)
161
    );
162
1
}
163

            
164
#[test]
165
1
fn a_long_flat_list_still_builds_every_cell_in_order() {
166
1
    assert_nms_source_prints_with_stack_limit(
167
1
        "nms_flat_list_run_",
168
1
        &format!(
169
1
            "(let ((l {})) (list (length l) (car l) (car (cdr l))))",
170
1
            runtime_list(300)
171
1
        ),
172
1
        "(301 0 1)",
173
1
        2048,
174
1
    );
175
1
}