open-standards/wai/wai-rs/tests/extension_duration_limits.rs
Transaction Science 161d147767
Some checks failed
wai-quantum WebGPU member / member · discrete GPU, Vulkan (jetson-hub) (pull_request) Successful in 9m59s
spec citations resolve / Code cites no section its SPEC lacks (pull_request) Successful in 4s
standards workspaces build + test / arl-rs (pull_request) Successful in 1m12s
standards workspaces build + test / eoc-rs (pull_request) Successful in 2m15s
standards workspaces build + test / jcp-rs (pull_request) Successful in 1m48s
standards workspaces build + test / joule-ui-rs (pull_request) Successful in 1m10s
standards workspaces build + test / joulehook-rs (pull_request) Successful in 1m13s
standards workspaces build + test / proof-rs (pull_request) Successful in 1m35s
standards workspaces build + test / sense-rs (pull_request) Successful in 1m14s
standards workspaces build + test / sandbox-rs (pull_request) Successful in 2m35s
no-vanity-metrics / Scan committed Markdown for vanity metrics (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no comparison (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no company or product (pull_request) Has been cancelled
no-vanity-metrics / No new orphan source files (pull_request) Has been cancelled
standards workspaces build + test / jouleclaw-rs (pull_request) Has been cancelled
standards workspaces build + test / aac-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-code-rs (pull_request) Has been cancelled
standards workspaces build + test / joulecontract-rs (pull_request) Has been cancelled
standards workspaces build + test / jouledesk-rs (pull_request) Has been cancelled
standards workspaces build + test / map-rs (pull_request) Has been cancelled
standards workspaces build + test / mesh-rs (pull_request) Has been cancelled
standards workspaces build + test / openpay (pull_request) Has been cancelled
standards workspaces build + test / macOS energy backends (pull_request) Has been cancelled
tests in CI / every test file is run by a job or listed with its reason (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · macos (pull_request) Has been cancelled
WAI conformance / quantum core and corpus under wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
WAI conformance / v2 vocabulary · manifests validate as stated (pull_request) Has been cancelled
WAI conformance / independent verifiers · claims, JWP, key discovery and integer payloads (Python) (pull_request) Has been cancelled
WAI conformance / delivery telemetry · CMCD and CMSD against an independent library (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · macos (pull_request) Has been cancelled
WAI conformance / codec wrappers · fuzz smoke, text decode limits, C entry points, provenance, the semantic path (pull_request) Has been cancelled
WAI conformance / C2PA manifest emission · signed, read back, validated (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux-arm64 (pull_request) Has been cancelled
WAI energy receipts / energy receipts · macos (pull_request) Has been cancelled
WAI HTTP delivery / WAI HTTP · does this change touch it (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify mutations 0/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify mutations 1/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify mutations 2/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify patches 0/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify patches 1/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · falsify patches 2/3 (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · every patch and mutation in exactly one shard (pull_request) Has been cancelled
WAI HTTP delivery / HTTP corpus · verified both ways, and the differential (pull_request) Has been cancelled
WAI HTTP delivery / cache rules · controls and labelled caches (pull_request) Has been cancelled
WAI JPEG-AI whole decode (pinned inputs, required) / fetch pinned JPEG-AI inputs → whole decode + pixel hash, required (pull_request) Has been cancelled
WAI neural sink gates / model-free refusals of the neural sink (pull_request) Has been cancelled
wai-pack / wai-pack · does this change touch it (pull_request) Has been cancelled
wai-pack / wai-pack · linux (pull_request) Has been cancelled
wai-pack / wai-pack · linux-arm64 (pull_request) Has been cancelled
wai-pack / wai-pack · macos (pull_request) Has been cancelled
wai-pack / wai-pack · independent verifier (Python) (pull_request) Has been cancelled
wai-pack / wai-pack · the floor through a media prober (pull_request) Has been cancelled
wai-pack / wai-pack · dependency licences (pull_request) Has been cancelled
wai-quantum WebGPU member / shader checks · browser build · wasip2 inertness · MSRV (pull_request) Has been cancelled
wai-quantum WebGPU member / member · software Vulkan (linux) (pull_request) Has been cancelled
wai-quantum WebGPU member / member · Metal (macos) (pull_request) Has been cancelled
wai-quantum WebGPU member / member · browser WebGPU (macos) (pull_request) Has been cancelled
WAI section references / cited sections exist (pull_request) Has been cancelled
WAI mbt2018_mean integer decode (regenerated vectors) / regenerate mbt2018_mean integer vectors → Rust decode, required (pull_request) Has been cancelled
wai-web reach builds / wai-web reach · does this change touch it (pull_request) Has been cancelled
wai-web reach builds / wai-web reach builds · the package of record, every output as recorded (pull_request) Has been cancelled
wai-web reach builds / wai-web reach builds · this tree's wai-web, every rung on its engine line (pull_request) Has been cancelled
wai-web wasm32 / wai-web wasm32 build · corpus, menu and writers (pull_request) Has been cancelled
workflow hygiene / every workflow supersedes its own queue (pull_request) Has been cancelled
lockfiles / every Cargo.lock is current (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux (pull_request) Has been cancelled
standards workspaces build + test / smart-byte-rs (pull_request) Has been cancelled
wai: reader limits for the extension formats' durations
A score, haptic signal, audio scene, splat4d clip or film declares its length in its
contract, and rendering allocates in proportion: a score's mixdown allocated `duration`
samples outright, a haptic channel `duration` × 2 bytes, a scene's stereo mix 20 bytes a
sample, and the splat4d and film receipts a hash a frame. Nothing bounded the
declaration, so a few bytes could ask a renderer for more memory than any machine has.
SECURITY.md recorded it as a gap when the fuzzing PR found the reader-side reservations.

Each extension's contract section (score, haptics, spatial-audio, volumetric,
linear-film and its reel) now has a reader limit shared with SPEC §2, through a new
always-on module `wai::limits`:
- score and haptics: `duration × channels` at most 2^27 samples;
- audio scene: `duration` at most 2^26, so the stereo floor is at most 2^27 samples;
- splat4d `n_frames`, film `frames`, a reel's shots together: at most 2^20 frames.
A reader may refuse past them (`duration_too_long`) when it reads the contract, before
anything is rendered; a conforming encoder writes none.

The film schedule's tick, `start_tick + f × ticks_per_frame`, wrapped in release builds
and panicked in debug ones. A reader must now refuse a schedule whose last tick passes
2^64 - 1 (a film's, and each shot's of a reel); `Film::frame_tick` saturates and
`Reel::frame_tick` uses checked arithmetic.

tests/extension_duration_limits.rs rewrites a committed vector of each format to declare
exactly its limit (it reads) and one past it (refused by name), and checks the film
schedule ending at 2^64 - 1 (reads) and past it (refused). Falsified: with the readers
as they were, all three tests fail. The world, audio, haptic, splat4d, score and film
corpora verify unchanged; the 275 lib tests with the container features pass. CI runs
the test beside container_reservations on every leg.

SECURITY.md drops the gap and keeps the one this exposes: replaying a world to a far
tick costs time in proportion (memory is bounded, time is not).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 11:07:16 -04:00

165 lines
7.4 KiB
Rust

//! 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<u8> {
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<u8>)> {
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<u8>)]) -> Vec<u8> {
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<String, serde_json::Value>)) -> Vec<u8> {
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<String, serde_json::Value>) {
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<E: std::fmt::Debug>(what: &str, r: Result<impl Sized, E>) {
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);
}