1
//! An index joining an AnyRef result is boxed where it lands, so the
2
//! INDEX-OVERFLOW that box can raise escapes every handler the value crosses
3
//! (0011 F2/S12).
4

            
5
use crate::ast::WasmType;
6
use crate::compiler::CompileMode;
7
use crate::compiler::behaviour_oracle::{Behaviour, Outcome, observe};
8
use crate::host_fn::HostFnSpec;
9

            
10
use super::lands_raw;
11

            
12
const OVERFLOW: &str = "INDEX-OVERFLOW";
13

            
14
4
fn run(source: &str) -> Vec<Behaviour> {
15
4
    let hosts = [
16
4
        HostFnSpec::new("rpc-protocol-version", "nomi", "rpc_protocol_version")
17
4
            .returns(WasmType::I32),
18
4
    ];
19
4
    observe(source, CompileMode::Eval, &hosts).0
20
4
}
21

            
22
1
fn overflows(behaviours: &[Behaviour]) -> bool {
23
1
    !behaviours.is_empty()
24
1
        && behaviours
25
1
            .iter()
26
2
            .all(|b| matches!(&b.outcome, Outcome::RuntimeError(msg) if msg.starts_with(OVERFLOW)))
27
1
}
28

            
29
#[test]
30
1
fn only_an_index_bound_for_anyref_lands_raw() {
31
1
    assert!(lands_raw(WasmType::I32, Some(WasmType::AnyRef)));
32
1
    assert!(!lands_raw(WasmType::Bool, Some(WasmType::AnyRef)));
33
1
    assert!(!lands_raw(WasmType::I32, Some(WasmType::I32)));
34
1
    assert!(!lands_raw(WasmType::I32, None));
35
1
}
36

            
37
#[test]
38
1
fn a_handler_case_does_not_catch_its_own_bodys_box() {
39
1
    let behaviours = run(
40
1
        r#"(handler-case (+ 1073741823 (+ 1 (rpc-protocol-version))) (index-overflow (e) "caught") (boom (e) "s"))"#,
41
1
    );
42
1
    assert!(overflows(&behaviours), "{behaviours:?}");
43
1
}
44

            
45
#[test]
46
1
fn an_exit_crossing_a_handler_is_boxed_outside_it() {
47
1
    let behaviours = run(
48
1
        r#"(block a (handler-case (if (> (rpc-protocol-version) 0) (return-from a (+ 1073741823 (+ 1 (rpc-protocol-version)))) "t") (index-overflow (e) "wrong")))"#,
49
1
    );
50
2
    let outcomes: Vec<&Outcome> = behaviours.iter().map(|b| &b.outcome).collect();
51
1
    assert!(
52
1
        matches!(
53
1
            outcomes.as_slice(),
54
1
            [Outcome::RuntimeError(empty), Outcome::Value(_)] if empty.starts_with(OVERFLOW)
55
        ),
56
        "{outcomes:?}"
57
    );
58
1
}
59

            
60
#[test]
61
1
fn a_cleanup_exit_supersedes_a_raw_payload_without_a_handler_seeing_it() {
62
1
    let behaviours = run(
63
1
        r#"(block b (unwind-protect (handler-case (unwind-protect (return-from b (+ 1073741823 (+ 1 (rpc-protocol-version)))) nil) (index-overflow (e) (print "wrong"))) (if (> (rpc-protocol-version) 0) (return-from b "s"))))"#,
64
1
    );
65
1
    let empty = behaviours
66
1
        .first()
67
1
        .map(|b| (&b.outcome, b.output.is_empty()));
68
1
    assert!(
69
1
        matches!(empty, Some((Outcome::Value(v), true)) if v.contains("I32(115)")),
70
        "{behaviours:?}"
71
    );
72
1
}
73

            
74
#[test]
75
1
fn a_raw_index_tail_falls_into_the_pad() {
76
1
    let behaviours = run(
77
1
        r#"(block a (if (> (rpc-protocol-version) 0) (return-from a "s")) (rpc-protocol-version))"#,
78
1
    );
79
1
    assert!(
80
1
        behaviours
81
1
            .iter()
82
2
            .all(|b| matches!(&b.outcome, Outcome::Value(v) if v.starts_with("Some(AnyRef)"))),
83
        "{behaviours:?}"
84
    );
85
1
}