oxedyne/fe2o3/fe2o3_austenite/tests/eval_plugin.rs
6.4 KiB, 1 run
created by r1870400018:73461, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Austenite's limits on a WebAssembly plugin, each met by a hostile module from `tools/plugin/mkwasm.py`: |
| 2 | //! a loop in a call and in the module's `start`, which fuel stops, and a memory that grows without end or |
| 3 | //! is declared past the ceiling, which the limiter stops. Typst sets no such limits, so the oracle cannot |
| 4 | //! check these; each must end in a `Limit` diagnostic that names the limit, and never hang or abort. |
| 5 | //! `grow` really allocates up to the ceiling, so run this file under `rc-build 2G`. The memo of loads, |
| 6 | //! transitions and calls (W2) is checked over three compiles of one session. |
| 7 | |
| 8 | #![cfg(feature = "plugin")] |
| 9 | |
| 10 | use oxedyne_fe2o3_austenite::caches::Counters; |
| 11 | use oxedyne_fe2o3_austenite::compile::Session; |
| 12 | use oxedyne_fe2o3_austenite::diag::DiagnosticKind; |
| 13 | use oxedyne_fe2o3_austenite::emit::sinks::PdfSink; |
| 14 | use oxedyne_fe2o3_austenite::eval::eval::{ |
| 15 | eval_string, |
| 16 | EvalMode, |
| 17 | }; |
| 18 | use oxedyne_fe2o3_austenite::eval::plugin::{ |
| 19 | FUEL, |
| 20 | MEMORY, |
| 21 | }; |
| 22 | use oxedyne_fe2o3_austenite::eval::scope::Scope; |
| 23 | use oxedyne_fe2o3_austenite::eval::{ |
| 24 | Engine, |
| 25 | World, |
| 26 | }; |
| 27 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 28 | use oxedyne_fe2o3_austenite::syntax::Span; |
| 29 | |
| 30 | use oxedyne_fe2o3_core::prelude::*; |
| 31 | |
| 32 | use std::path::{ |
| 33 | Path, |
| 34 | PathBuf, |
| 35 | }; |
| 36 | use std::time::{ |
| 37 | Duration, |
| 38 | Instant, |
| 39 | }; |
| 40 | |
| 41 | const TEST_FUEL: u64 = 1_000_000_000; |
| 42 | |
| 43 | // Evaluates `code` from a source beside the fixture modules, answering the first error's kind and message. |
| 44 | fn first_error(code: &str) -> Outcome<(DiagnosticKind, String)> { |
| 45 | let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/plugin"); |
| 46 | let mut engine = Engine::new(World::new(dir.clone())); |
| 47 | // The default, FUEL, takes a loop about 26 minutes to spend; the mechanism is the same at any figure. |
| 48 | engine.plugin_fuel = TEST_FUEL; |
| 49 | let id = res!(engine.world.add_source(dir.join("case.typ"), String::new())); |
| 50 | let _ = eval_string(&mut engine, code, EvalMode::Code, Scope::new(), Span::new(id, 0, 0)); |
| 51 | match engine.diags.iter().find(|d| d.is_error()) { |
| 52 | Some(d) => Ok((d.kind, d.message.clone())), |
| 53 | None => Err(err!("{}: no error raised", code; Missing)), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn assert_limit(code: &str, word: &str) -> Outcome<()> { |
| 58 | let t = Instant::now(); |
| 59 | let (kind, msg) = res!(first_error(code)); |
| 60 | assert_eq!(kind, DiagnosticKind::Limit, "{}: kind {} ({})", code, kind, msg); |
| 61 | assert!(msg.contains(word), "{}: '{}' does not name the {}", code, msg, word); |
| 62 | // Fuel is counted, not timed, but a limit that took this long would not protect a compile. |
| 63 | assert!(t.elapsed() < Duration::from_secs(60), "{}: took {:?}", code, t.elapsed()); |
| 64 | Ok(()) |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn a_call_that_loops_forever_runs_out_of_fuel() -> Outcome<()> { |
| 69 | assert_limit("plugin(\"spin.wasm\").spin()", "fuel") |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn a_start_function_that_loops_forever_runs_out_of_fuel_while_loading() -> Outcome<()> { |
| 74 | // FIX-10: the load runs `start`, so the fuel must already be set. |
| 75 | assert_limit("plugin(\"spin_start.wasm\")", "fuel") |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn a_memory_that_grows_without_end_stops_at_the_ceiling() -> Outcome<()> { |
| 80 | assert_limit("plugin(\"grow.wasm\").grow()", "memory limit") |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn a_declared_memory_past_the_ceiling_fails_the_load() -> Outcome<()> { |
| 85 | // 32768 pages, 2 GiB, past the ceiling on every target. |
| 86 | assert!(32768usize << 16 > MEMORY); |
| 87 | assert_limit("plugin(\"huge_initial.wasm\")", "memory limit") |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn the_limits_are_named_in_the_message() -> Outcome<()> { |
| 92 | let (_, msg) = res!(first_error("plugin(\"spin.wasm\").spin()")); |
| 93 | assert!(msg.contains(&TEST_FUEL.to_string()), "'{}' does not give the fuel figure {}", msg, TEST_FUEL); |
| 94 | // No document Typst compiles may meet the default: the corpus's most is 84,908,412,112 (jogs 0.2.4). |
| 95 | assert!(FUEL >= 10 * 84_908_412_112); |
| 96 | Ok(()) |
| 97 | } |
| 98 | |
| 99 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 100 | // │ MEMO │ |
| 101 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 102 | |
| 103 | fn fixture(name: &str) -> PathBuf { |
| 104 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/plugin").join(name) |
| 105 | } |
| 106 | |
| 107 | // Two loads, a transition and three calls, one of them on the transitioned plugin; `arg` is the echo's. |
| 108 | fn memo_doc(arg: &str) -> String { |
| 109 | fmt!("#let p = plugin(\"echo.wasm\")\n#let c = plugin(\"counter.wasm\")\n#let one = plugin.transition(c.inc)\n\ |
| 110 | #str(p.echo(bytes(\"{}\"))) #array(c.get()).at(0) #array(one.get()).at(0)\n", arg) |
| 111 | } |
| 112 | |
| 113 | fn build(session: &mut Session, dir: &Path, text: &str) -> Outcome<(Vec<u8>, Counters)> { |
| 114 | let main = dir.join("main.typ"); |
| 115 | res!(std::fs::write(&main, text).map_err(|e| err!("Cannot write {}: {}", main.display(), e; IO, File, Write))); |
| 116 | let mut sink = res!(PdfSink::new()); |
| 117 | let done = res!(session.compile(&main, dir, &mut sink, None, None, false)); |
| 118 | if let Err(e) = &done.laid { |
| 119 | return Err(err!("the compile failed: {}", e.plain(); Invalid)); |
| 120 | } |
| 121 | let pdf = res!(sink.output().ok_or_else(|| err!("the compile finished no PDF"; Missing))); |
| 122 | Ok((pdf.to_vec(), done.counters)) |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn loads_transitions_and_calls_are_memoised_across_compiles() -> Outcome<()> { |
| 127 | let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_plugin_memo"); |
| 128 | let _ = std::fs::remove_dir_all(&dir); |
| 129 | res!(std::fs::create_dir_all(&dir).map_err(|e| err!("Cannot create {}: {}", dir.display(), e; IO, File, Write))); |
| 130 | for m in ["echo.wasm", "counter.wasm"] { |
| 131 | res!(std::fs::copy(fixture(m), dir.join(m)).map_err(|e| err!("Cannot copy {}: {}", m, e; IO, File, Write))); |
| 132 | } |
| 133 | let mut s = Session::new(FontStore::default()); |
| 134 | let (first, c1) = res!(build(&mut s, &dir, &memo_doc("one"))); |
| 135 | assert_eq!((c1.plugin_hits, c1.plugin_misses), (0, 6), "a first compile runs everything: {:?}", c1); |
| 136 | let (second, c2) = res!(build(&mut s, &dir, &memo_doc("one"))); |
| 137 | assert_eq!((c2.plugin_hits, c2.plugin_misses), (6, 0), "an unchanged recompile runs nothing: {:?}", c2); |
| 138 | assert!(first == second, "the recompile made different bytes"); |
| 139 | // Only the call whose argument changed runs again. |
| 140 | let (_, c3) = res!(build(&mut s, &dir, &memo_doc("two"))); |
| 141 | assert_eq!((c3.plugin_hits, c3.plugin_misses), (5, 1), "an edited argument ran more than its call: {:?}", c3); |
| 142 | assert_eq!(s.caches().plugins.len(), (2, 1, 4), "two loads, one transition, four calls held"); |
| 143 | Ok(()) |
| 144 | } |