Lines
100 %
Functions
85 %
Branches
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! Output that does not fit the 64 KiB output buffer raises `OUTPUT-OVERFLOW`
//! rather than writing past it. After the 24-byte output header 65512 bytes
//! remain: a debug value takes 56 of them, a tag 48 plus its name and value
//! bytes (which grow down from the buffer end), a delete record 32.
use nms::interpreter::Interpreter;
use scripting::{EntityHeaderArgs, MemorySerializer, ScriptExecutor, TransactionArgs};
use scripting_format::{ContextType, EntityType};
const OUTPUT_SIZE: u32 = 64 * 1024;
/// Runs `source` against one input transaction; the output entity count.
fn run(source: &str) -> Result<usize, String> {
let _interp = Interpreter::new(false).map_err(|e| e.to_string())?;
let wasm = rpc::compile_script(source).map_err(|e| e.to_string())?;
let mut ser = MemorySerializer::new();
ser.set_context(ContextType::BatchProcess, EntityType::Transaction);
let tx = ser.add_transaction(
EntityHeaderArgs {
id: [1; 16],
parent_idx: -1,
is_primary: true,
is_context: false,
},
TransactionArgs {
post_date: 0,
enter_date: 0,
split_count: 0,
tag_count: 0,
is_multi_currency: false,
);
ser.set_primary(tx);
let input = ser.finalize(OUTPUT_SIZE);
let executor = ScriptExecutor::try_new().map_err(|e| e.to_string())?;
executor
.execute(&wasm, &input, Some(OUTPUT_SIZE))
.map(|entities| entities.len())
.map_err(|e| format!("{e:?}"))
}
/// A runtime list of `len` numbers: `len` + 2 debug values.
fn list_of(len: usize) -> String {
format!(
"(let ((r nil)) (do ((i 0 (+ i 1))) ((= i {len})) (setf r (cons (+ i (entity-count)) r))) r)"
)
/// `count` tags on the input transaction in a runtime loop, then `nil`.
fn tags(count: usize, value: &str) -> String {
"(do ((i 0 (+ i 1))) ((= i (+ {count} (* 0 (entity-count))))) (create-tag 0 \"k\" \"{value}\"))"
/// `count` deletes of the input transaction in a runtime loop, then `nil`.
fn deletes(count: usize) -> String {
format!("(do ((i 0 (+ i 1))) ((= i (+ {count} (* 0 (entity-count))))) (delete-entity 0))")
fn assert_overflows(source: &str) {
let err = run(source).expect_err("the output overflows the buffer");
assert!(err.contains("__nomi_raise:OUTPUT-OVERFLOW"), "{err}");
#[test]
fn a_result_that_just_fits_is_written_whole() {
assert_eq!(run(&list_of(1167)), Ok(1169));
fn a_result_one_entity_too_large_raises_output_overflow() {
assert_overflows(&list_of(1168));
assert_overflows(&list_of(3000));
fn tags_that_just_fit_are_all_written() {
assert_eq!(run(&tags(1309, "v")), Ok(1310));
fn a_tag_past_the_buffer_raises_output_overflow() {
assert_overflows(&tags(1311, "v"));
/// 70-byte tags: 935 fit, and the 62 bytes left still hold the `nil` result.
fn a_tag_overflow_is_catchable_inside_the_script() {
let source = format!(
"(handler-case {} (output-overflow (e) nil))",
tags(2000, "vvvvvvvvvvvvvvvvvvvvv")
assert_eq!(run(&source), Ok(936));
/// 2045 records and the `nil` result fill 65496 of 65512 bytes.
fn deletes_that_just_fit_are_all_written() {
assert_eq!(run(&deletes(2045)), Ok(2046));
/// 2046 records still fit, so the `nil` result write overflows; the 2048th
/// record itself does not fit, so the delete writer overflows.
fn a_delete_past_the_buffer_raises_output_overflow() {
assert_overflows(&deletes(2046));
assert_overflows(&deletes(2048));
/// A PRINT longer than the guest's staging area below the input used to
/// overwrite the input header's output size, lifting the limit.
fn after_long_print(source: &str) -> String {
format!("(print \"{}\") {source}", "A".repeat(4200))
fn a_long_print_does_not_lift_the_output_limit() {
assert_eq!(run(&after_long_print(&list_of(1167))), Ok(1169));
assert_overflows(&after_long_print(&list_of(1168)));
fn a_long_print_does_not_lift_the_interpreter_output_limit() {
let mut interp = Interpreter::new(false).expect("interpreter");
let err = interp.eval(&after_long_print(&list_of(1200)));
assert!(err.is_err(), "{err:?}");