//! Each extension format's reader holds its declared duration to the reader limits it //! shares with SPEC ยง2 (`wai::limits`): samples, counting every channel, for a score, //! a haptic signal and an audio scene's stereo floor; frames for a splat4d clip, a film //! and a reel's shots together. Rendering allocates in proportion to the declaration, //! and nothing bounded it: a few bytes could ask a renderer for more memory than any //! machine has. //! //! For each, a committed vector's contract is rewritten to declare exactly the limit, //! which must still read, and one past it, which must be refused by name //! (`duration_too_long`). A film whose last tick passes 2^64 - 1 is refused too: its //! tick arithmetic used to wrap in release builds and panic in debug ones. #![cfg(all( feature = "world", feature = "audio_scene", feature = "film", feature = "score", feature = "haptic", feature = "splat4d" ))] use std::path::PathBuf; use wai::limits::{RENDER_MAX_FRAMES, RENDER_MAX_SAMPLES}; fn vector(rel: &str) -> Vec { let p = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..").join(rel); std::fs::read(&p).unwrap_or_else(|e| panic!("read {}: {e}", p.display())) } /// The sections of a container in the layout these formats share: a magic, a `u16` /// count, a table of `(kind u8, offset u32, length u32)`, then the bodies. fn sections(b: &[u8]) -> Vec<(u8, Vec)> { let n = u16::from_le_bytes([b[4], b[5]]) as usize; let body = &b[6 + 9 * n..]; (0..n) .map(|i| { let e = &b[6 + 9 * i..6 + 9 * (i + 1)]; let off = u32::from_le_bytes(e[1..5].try_into().unwrap()) as usize; let len = u32::from_le_bytes(e[5..9].try_into().unwrap()) as usize; (e[0], body[off..off + len].to_vec()) }) .collect() } fn build(magic: &[u8], secs: &[(u8, Vec)]) -> Vec { let mut out = magic[..4].to_vec(); out.extend_from_slice(&(secs.len() as u16).to_le_bytes()); let mut off = 0u32; for (k, body) in secs { out.push(*k); out.extend_from_slice(&off.to_le_bytes()); out.extend_from_slice(&(body.len() as u32).to_le_bytes()); off += body.len().div_ceil(8) as u32 * 8; } for (_, body) in secs { out.extend_from_slice(body); out.resize(out.len() + (body.len().div_ceil(8) * 8 - body.len()), 0); } out } /// The container with its contract (section 0x01) rewritten by `edit`. fn with_contract(b: &[u8], edit: impl Fn(&mut serde_json::Map)) -> Vec { let mut secs = sections(b); let c = secs.iter_mut().find(|(k, _)| *k == 0x01).expect("a contract section"); let mut v: serde_json::Value = serde_json::from_slice(&c.1).expect("the contract parses"); edit(v.as_object_mut().unwrap()); c.1 = serde_json::to_vec(&v).unwrap(); build(b, &secs) } fn set(key: &'static str, value: u64) -> impl Fn(&mut serde_json::Map) { move |o| { o.insert(key.into(), value.into()); } } fn channels(b: &[u8]) -> u64 { let secs = sections(b); let c = &secs.iter().find(|(k, _)| *k == 0x01).unwrap().1; let v: serde_json::Value = serde_json::from_slice(c).unwrap(); v["channels"].as_u64().unwrap_or(1).max(1) } fn refused(what: &str, r: Result) { let e = format!("{:?}", r.err().unwrap_or_else(|| panic!("{what}: past the limit, and read"))); assert!(e.contains("duration_too_long:"), "{what}: refused for another reason: {e}"); } #[test] fn audio_formats_are_held_to_the_sample_limit_counting_every_channel() { let score = vector("score-conformance/vectors/arp.wscr"); let at = RENDER_MAX_SAMPLES / channels(&score); wai::score::Score::from_bytes(&with_contract(&score, set("duration", at))).expect("a score at the limit reads"); refused("score", wai::score::Score::from_bytes(&with_contract(&score, set("duration", at + 1)))); let haptic = vector("haptic-conformance/vectors/ramp.whap"); let at = RENDER_MAX_SAMPLES / channels(&haptic); wai::haptic::HapticSignal::from_bytes(&with_contract(&haptic, set("duration", at))).expect("a signal at the limit reads"); refused("haptic", wai::haptic::HapticSignal::from_bytes(&with_contract(&haptic, set("duration", at + 1)))); // A scene's floor is stereo: two samples for every one of `duration`. let scene = vector("wai-web/demo/audio/moving.wscn"); let at = RENDER_MAX_SAMPLES / 2; wai::audio_scene::SpatialScene::from_bytes(&with_contract(&scene, set("duration", at))).expect("a scene at the limit reads"); refused("audio scene", wai::audio_scene::SpatialScene::from_bytes(&with_contract(&scene, set("duration", at + 1)))); } #[test] fn frame_formats_are_held_to_the_frame_limit() { let clip = vector("volumetric-conformance/vectors/morph.w4ds"); wai::splat4d::Splat4dClipData::from_bytes(&with_contract(&clip, set("n_frames", RENDER_MAX_FRAMES))) .expect("a clip at the limit reads"); refused("splat4d", wai::splat4d::Splat4dClipData::from_bytes(&with_contract(&clip, set("n_frames", RENDER_MAX_FRAMES + 1)))); let film = vector("film-conformance/vectors/rise.wflm"); wai::film::Film::from_bytes(&with_contract(&film, set("frames", RENDER_MAX_FRAMES))).expect("a film at the limit reads"); refused("film", wai::film::Film::from_bytes(&with_contract(&film, set("frames", RENDER_MAX_FRAMES + 1)))); // The reel: the first shot's frames (the first u64 after the shot count) set so the // shots together are exactly at the limit, then one past it. let reel = vector("film-conformance/vectors/coverage.wrel"); let secs = sections(&reel); let shots = &secs.iter().find(|(k, _)| *k == 0x03).expect("a shots section").1; let n = u32::from_le_bytes(shots[..4].try_into().unwrap()) as usize; let mut others = 0u64; let mut p = 4; let mut firsts = Vec::new(); for i in 0..n { let frames = u64::from_le_bytes(shots[p..p + 8].try_into().unwrap()); firsts.push(p); if i > 0 { others += frames; } let keys = u32::from_le_bytes(shots[p + 24..p + 28].try_into().unwrap()) as usize; p += 24 + 4 + keys * 72; } let with_first = |frames: u64| { let mut s = secs.clone(); let body = &mut s.iter_mut().find(|(k, _)| *k == 0x03).unwrap().1; body[firsts[0]..firsts[0] + 8].copy_from_slice(&frames.to_le_bytes()); build(&reel, &s) }; wai::film::Reel::from_bytes(&with_first(RENDER_MAX_FRAMES - others)).expect("a reel at the limit reads"); refused("reel", wai::film::Reel::from_bytes(&with_first(RENDER_MAX_FRAMES - others + 1))); } #[test] fn a_film_whose_last_tick_passes_u64_is_refused() { let film = vector("film-conformance/vectors/rise.wflm"); let past = with_contract(&film, |o| { o.insert("frames".into(), 3u64.into()); o.insert("start_tick".into(), (u64::MAX - 1).into()); o.insert("ticks_per_frame".into(), 1u64.into()); }); let e = format!("{:?}", wai::film::Film::from_bytes(&past).err().expect("refused")); assert!(e.contains("last tick passes"), "{e}"); let at = with_contract(&film, |o| { o.insert("frames".into(), 2u64.into()); o.insert("start_tick".into(), (u64::MAX - 1).into()); o.insert("ticks_per_frame".into(), 1u64.into()); }); let f = wai::film::Film::from_bytes(&at).expect("a schedule ending at 2^64 - 1 reads"); assert_eq!(f.frame_tick(1), u64::MAX); }