beamr 0.13.2

A Rust runtime with the BEAM's execution model, targeting Gleam
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! The round-trip ratchet for the `.beam` encoder (feature `encode`).
//!
//! For every committed `.beam` fixture: `decode(x) -> encode -> decode` and
//! assert the two `ParsedModule` contents are equal. Where the fixture's
//! imports resolve, the re-encoded module is also run through
//! `validate_module`, held to the same verdict the original earns.
#![cfg(feature = "encode")]

use std::path::{Path, PathBuf};

use beamr::atom::AtomTable;
use beamr::loader::decode::{Instruction, Literal, Operand, TypeTestOp};
use beamr::loader::encode::encode_module;
use beamr::loader::load::{ParsedModule, resolve_imports};
use beamr::loader::validate::validate_module;
use beamr::loader::{ExportEntry, load_beam_chunks};
use beamr::module::ModuleRegistry;
use beamr::native::{AllCapabilitiesPolicy, BifRegistry, NativeEntry};

struct NoBifs;

impl BifRegistry for NoBifs {
    fn lookup(
        &self,
        _module: beamr::atom::Atom,
        _function: beamr::atom::Atom,
        _arity: u8,
    ) -> Option<NativeEntry> {
        None
    }
}

/// Recursively collects every `*.beam` file under `root`.
fn collect_beams(root: &Path, out: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(root) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            collect_beams(&path, out);
        } else if path.extension().is_some_and(|ext| ext == "beam") {
            out.push(path);
        }
    }
}

fn all_fixtures() -> Vec<PathBuf> {
    let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
    let mut roots = vec![manifest.join("tests/fixtures")];
    // `test-workflows/` sits at the repository root, two levels above the crate.
    if let Some(repo_root) = manifest.parent().and_then(Path::parent) {
        roots.push(repo_root.join("test-workflows"));
    }
    let mut beams = Vec::new();
    for root in roots {
        collect_beams(&root, &mut beams);
    }
    beams.sort();
    beams
}

/// The result of driving one `.beam` file through the encoder.
enum Outcome {
    /// `decode -> encode -> decode` produced a structurally identical module.
    Passed,
    /// The original bytes could not even be decoded — the decoder, not the
    /// encoder, declined them (e.g. an opcode outside beamr's supported set).
    /// The encoder is never exercised, so this is an exclusion, not a bug.
    Excluded(String),
    /// The encoder was exercised and misbehaved: it errored, produced bytes the
    /// loader rejects, or produced a module that differs from the original.
    /// Every one of these is an encoder bug to fix.
    Failed(String),
}

/// Drives one `.beam` payload through `decode -> encode -> decode` and reports
/// whether the encoder round-tripped it, was never reached (undecodable input),
/// or misbehaved.
fn drive(bytes: &[u8]) -> Outcome {
    let table = AtomTable::with_common_atoms();
    let original = match load_beam_chunks(bytes, &table) {
        Ok(module) => module,
        Err(error) => return Outcome::Excluded(format!("original does not decode: {error}")),
    };
    let encoded = match encode_module(&original, &table) {
        Ok(bytes) => bytes,
        Err(error) => return Outcome::Failed(format!("encode failed: {error}")),
    };
    let reloaded = match load_beam_chunks(&encoded, &table) {
        Ok(module) => module,
        Err(error) => return Outcome::Failed(format!("re-encoded bytes do not decode: {error}")),
    };
    if let Some(detail) = first_mismatch(&original, &reloaded) {
        return Outcome::Failed(format!("round-trip mismatch: {detail}"));
    }
    // The re-encoded module must earn the same validation verdict as the
    // original — encoding neither introduces nor masks a validation fault.
    if validates(&original) != validates(&reloaded) {
        return Outcome::Failed("validation verdict changed after re-encode".to_string());
    }
    Outcome::Passed
}

/// Returns a human-readable description of the first field in which two parsed
/// modules differ, or `None` when they are structurally equal. The instruction
/// stream is diffed element-by-element so a mismatch names the exact index.
fn first_mismatch(original: &ParsedModule, reloaded: &ParsedModule) -> Option<String> {
    if original.name != reloaded.name {
        return Some("name".to_string());
    }
    if original.atoms != reloaded.atoms {
        return Some("atoms".to_string());
    }
    if original.instructions != reloaded.instructions {
        if original.instructions.len() != reloaded.instructions.len() {
            return Some(format!(
                "instruction count {} != {}",
                original.instructions.len(),
                reloaded.instructions.len()
            ));
        }
        for (index, (a, b)) in original
            .instructions
            .iter()
            .zip(reloaded.instructions.iter())
            .enumerate()
        {
            if a != b {
                return Some(format!(
                    "instruction[{index}]:\n    original: {a:?}\n    reloaded: {b:?}"
                ));
            }
        }
        return Some("instructions".to_string());
    }
    if original.imports != reloaded.imports {
        return Some("imports".to_string());
    }
    if original.exports != reloaded.exports {
        return Some("exports".to_string());
    }
    if original.lambdas != reloaded.lambdas {
        return Some(format!(
            "lambdas:\n    original: {:?}\n    reloaded: {:?}",
            original.lambdas, reloaded.lambdas
        ));
    }
    if original.literals != reloaded.literals {
        if original.literals.len() != reloaded.literals.len() {
            return Some(format!(
                "literal count {} != {}",
                original.literals.len(),
                reloaded.literals.len()
            ));
        }
        for (index, (a, b)) in original
            .literals
            .iter()
            .zip(reloaded.literals.iter())
            .enumerate()
        {
            if a != b {
                return Some(format!(
                    "literal[{index}]:\n    original: {a:?}\n    reloaded: {b:?}"
                ));
            }
        }
        return Some("literals".to_string());
    }
    if original.string_table != reloaded.string_table {
        return Some("string_table".to_string());
    }
    if original.line_info != reloaded.line_info {
        return Some("line_info".to_string());
    }
    None
}

/// `validate_module` verdict for a parsed module, resolving imports against an
/// empty registry (external imports become deferred, which validate accepts).
fn validates(module: &ParsedModule) -> bool {
    let registry = ModuleRegistry::new();
    let (resolved, _report) = resolve_imports(module, &registry, &NoBifs, &AllCapabilitiesPolicy);
    validate_module(module, &resolved).is_ok()
}

#[test]
fn every_fixture_round_trips_through_encode() {
    let fixtures = all_fixtures();
    assert!(
        fixtures.len() >= 70,
        "expected the full fixture corpus, found {}",
        fixtures.len()
    );

    let mut failures = Vec::new();
    for path in &fixtures {
        let bytes = std::fs::read(path).expect("fixture readable");
        match drive(&bytes) {
            Outcome::Passed => {}
            Outcome::Excluded(reason) => {
                // A committed fixture that the decoder itself rejects is a
                // regression in the fixture corpus, not an acceptable skip.
                failures.push(format!(
                    "{}: decoder rejected fixture: {reason}",
                    path.display()
                ));
            }
            Outcome::Failed(detail) => {
                failures.push(format!("{}: {detail}", path.display()));
            }
        }
    }

    assert!(
        failures.is_empty(),
        "{} of {} committed fixtures failed to round-trip:\n{}",
        failures.len(),
        fixtures.len(),
        failures.join("\n")
    );
}

/// Full-corpus ratchet: when `BEAMR_RT_CORPUS_DIR` names a directory, every
/// `*.beam` beneath it is driven through `decode -> encode -> decode`. Failures
/// are collected with their paths (the walk never stops at the first), and the
/// module counts are printed loudly so a green run still proves what it covered.
///
/// Undecodable inputs are *excluded* (the encoder is never reached) and reported
/// separately — they are not encoder bugs. Every module the decoder accepts must
/// round-trip through the encoder byte-for-structure identical, or the test
/// fails naming each offender.
#[test]
fn corpus_round_trips_when_env_set() {
    let Ok(dir) = std::env::var("BEAMR_RT_CORPUS_DIR") else {
        println!(
            "BEAMR_RT_CORPUS_DIR unset — corpus ratchet skipped \
             (committed-fixture ratchet still runs in the sibling test)"
        );
        return;
    };

    let root = PathBuf::from(&dir);
    let mut beams = Vec::new();
    collect_beams(&root, &mut beams);
    beams.sort();

    assert!(
        !beams.is_empty(),
        "BEAMR_RT_CORPUS_DIR={dir} contained no *.beam files"
    );

    let mut passed = 0_usize;
    let mut failures = Vec::new();
    let mut excluded = Vec::new();
    for path in &beams {
        let bytes = match std::fs::read(path) {
            Ok(bytes) => bytes,
            Err(error) => {
                failures.push(format!("{}: unreadable: {error}", path.display()));
                continue;
            }
        };
        match drive(&bytes) {
            Outcome::Passed => passed += 1,
            Outcome::Excluded(reason) => excluded.push(format!("{}: {reason}", path.display())),
            Outcome::Failed(detail) => failures.push(format!("{}\n  {detail}", path.display())),
        }
    }

    println!("=== corpus round-trip ratchet: {dir} ===");
    println!("  discovered .beam files : {}", beams.len());
    println!("  encoder round-tripped  : {passed}");
    println!("  excluded (undecodable) : {}", excluded.len());
    println!("  encoder failures       : {}", failures.len());
    if !excluded.is_empty() {
        println!("--- excluded (decoder declined; encoder not exercised) ---");
        for line in &excluded {
            println!("  EXCLUDED {line}");
        }
    }
    if !failures.is_empty() {
        println!("--- encoder failures ---");
        for line in &failures {
            println!("  FAILED {line}");
        }
    }

    assert!(
        failures.is_empty(),
        "{} of {} decodable corpus modules failed to round-trip through the encoder",
        failures.len(),
        passed + failures.len()
    );
}

/// A hand-built module carrying the nasty operand and literal shapes: negative
/// and bignum integers, atom-index width boundaries, an empty string table, and
/// a compressed multi-entry `LitT`.
#[test]
fn hand_built_edge_cases_round_trip() {
    let table = AtomTable::with_common_atoms();
    let module_name = table.intern("edge_module");
    let start = table.intern("start");
    let wide = table.intern("wide_atom");

    // A big-integer literal beyond i64 (magnitude 2^70), sign byte + LE bytes.
    let mut big_magnitude = vec![0_u8; 9];
    big_magnitude[8] = 0x40; // 2^70 == 1 << 70 -> byte 8 (bit 6) set.
    let mut big_integer_bytes = vec![0_u8]; // positive sign
    big_integer_bytes.extend_from_slice(&big_magnitude);

    let literals = vec![
        Literal::Integer(-1),
        Literal::Integer(i64::MIN),
        Literal::BigInteger(big_integer_bytes),
        Literal::Tuple(vec![
            Literal::Atom(start),
            Literal::String(b"hello".to_vec()),
            Literal::Float(3.5),
        ]),
        Literal::Nil,
        Literal::Binary(vec![1, 2, 3, 4]),
    ];

    let instructions = vec![
        Instruction::Label { label: 1 },
        Instruction::FuncInfo {
            module: Operand::Atom(Some(module_name)),
            function: Operand::Atom(Some(start)),
            arity: Operand::Unsigned(0),
        },
        Instruction::Label { label: 2 },
        // Negative and large inline integer operands.
        Instruction::Move {
            source: Operand::Integer(-42),
            destination: Operand::X(0),
        },
        Instruction::Move {
            source: Operand::Integer(40000),
            destination: Operand::X(1),
        },
        // A literal reference into the compressed LitT.
        Instruction::Move {
            source: Operand::Literal(2),
            destination: Operand::X(2),
        },
        // Wide atom operand + a synthetic is_function2 (list-wrapped operands).
        Instruction::Move {
            source: Operand::Atom(Some(wide)),
            destination: Operand::X(3),
        },
        Instruction::TypeTest {
            op: TypeTestOp::IsFunction2,
            fail: Operand::Label(2),
            value: Operand::List(vec![Operand::X(3), Operand::Integer(2)]),
        },
        Instruction::Return,
    ];

    let module = ParsedModule {
        name: module_name,
        atoms: vec![module_name, start, wide],
        instructions,
        imports: Vec::new(),
        exports: vec![ExportEntry {
            function: start,
            arity: 0,
            label: 1,
        }],
        lambdas: Vec::new(),
        literals,
        string_table: Vec::new(),
        line_info: Vec::new(),
    };

    let encoded = encode_module(&module, &table).expect("edge module encodes");
    let reloaded = load_beam_chunks(&encoded, &table).expect("edge module decodes");
    assert_eq!(module, reloaded);
}

/// An empty `LitT` (no literals) omits the chunk entirely and still round-trips
/// to an empty literal table.
#[test]
fn empty_optional_chunks_round_trip() {
    let table = AtomTable::with_common_atoms();
    let name = table.intern("bare_module");
    let module = ParsedModule {
        name,
        atoms: vec![name],
        instructions: vec![Instruction::Label { label: 1 }, Instruction::Return],
        imports: Vec::new(),
        exports: Vec::new(),
        lambdas: Vec::new(),
        literals: Vec::new(),
        string_table: Vec::new(),
        line_info: Vec::new(),
    };

    let encoded = encode_module(&module, &table).expect("bare module encodes");
    let reloaded = load_beam_chunks(&encoded, &table).expect("bare module decodes");
    assert_eq!(module, reloaded);
    assert!(reloaded.literals.is_empty());
}