diff --git a/js/net/src/group.test.ts b/js/net/src/group.test.ts index fe1e558ed50..6954384cc95 100644 --- a/js/net/src/group.test.ts +++ b/js/net/src/group.test.ts @@ -1,7 +1,7 @@ import { expect, test } from "bun:test"; import { Manual, Milli, Nano, yieldNow } from "@moq/clock"; import { Signal } from "@moq/signals"; -import { FrameTooLarge, StreamCode, StreamError } from "./error.ts"; +import { FrameTooLarge, ProtocolViolation, StreamCode, StreamError } from "./error.ts"; import { encodable } from "./flate/group.ts"; import { Producer } from "./group.ts"; import { hooks, type Replacement } from "./internal.ts"; @@ -511,6 +511,58 @@ test("done ignores replayed frames an adopted copy buffers", async () => { producer.finish(); }); +test("a resumed read refuses a replacement that ends before the frames already read", async () => { + for (const repeated of [2, 1]) { + const producer = new Producer(0n); + const consumer = producer.mirror(); + const replacement = new Producer(0n); + for (const text of ["zero", "one"].slice(0, repeated)) replacement.writeFrame(textFrame(text)); + replacement.finish(); + const failed = new Error("route failed"); + hooks.recoverGroup(consumer, { + changed: new Signal(false), + wants: (error) => error === failed, + replace: async () => ({ + group: replacement.mirror(), + expiry: { expired: () => false, changed: [] }, + adopt: () => {}, + discard: () => {}, + }), + }); + producer.writeFrame(textFrame("zero")); + producer.writeFrame(textFrame("one")); + expect(await consumer.readString()).toBe("zero"); + expect(await consumer.readString()).toBe("one"); + producer.abort(failed); + if (repeated === 2) expect(await consumer.readFrame()).toBeUndefined(); + else await expect(consumer.readFrame()).rejects.toBeInstanceOf(ProtocolViolation); + } +}); + +test("a resumed read of a copy that held nothing takes the replacement's end", async () => { + // The copy starts past the group's end, so it proves no frame there. + const producer = new Producer(0n); + hooks.startGroup(producer, 3); + const consumer = producer.mirror(); + const replacement = new Producer(0n); + replacement.writeFrame(textFrame("zero")); + replacement.writeFrame(textFrame("one")); + replacement.finish(); + const failed = new Error("route failed"); + hooks.recoverGroup(consumer, { + changed: new Signal(false), + wants: (error) => error === failed, + replace: async () => ({ + group: replacement.mirror(), + expiry: { expired: () => false, changed: [] }, + adopt: () => {}, + discard: () => {}, + }), + }); + producer.abort(failed); + expect(await consumer.readFrame()).toBeUndefined(); +}); + test("buffered frames are still readable after the group closes", async () => { const { producer, consumer } = pair(0n); producer.writeFrame(textFrame("a")); diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 4785a3df392..31ae4758b39 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -1111,6 +1111,8 @@ export class Consumer { #attempt?: Attempt; // The frame a replacement copy resumes at: frames below it were read from an earlier copy. #skip = 0; + // How far the copies read before held frames, which the copy read now must not end short of. + #held = 0; // Sticky failure, set once, including a budget that gave up on the group. #terminal?: Error; #verdict = new Once(); @@ -1397,6 +1399,9 @@ export class Consumer { return; } replacement.adopt(); + // Only a frame it holds proves where it reaches; an empty copy proves nothing past its start. + const total = this.#state.total.peek(); + if (total > this.#state.offset) this.#held = Math.max(this.#held, total); // The copy left behind stops teeing into the state this consumer read. if (this.#state.closed.peek() === undefined) this.#state.closed.set(null); this.#arrived ??= this.#state.arrived; @@ -1598,11 +1603,19 @@ export class Consumer { if (closed !== undefined) { // The copy finished the group: a replacement asked for meanwhile is moot. this.#abandon(); + // Unless it finished before frames an earlier copy delivered, so the copies + // disagree on where the group ends. + if (this.#state.total.peek() < this.#held) { + this.#terminal = new ProtocolViolation(`group ${this.sequence} ended before frames already read`); + if (this.#verdict.peek() === undefined) this.#verdict.set(this.#terminal); + this.#closed?.refresh(); + throw this.#terminal; + } this.#closed?.refresh(); return; } - // Nothing buffered and the group is still open: this wait is the stall the + // Nothing buffered and the group is still open: this wait is the // drift budget bounds, and the only place it applies. if (this.#expire()) continue; // A stall while a newer route serves looks there too. diff --git a/quest/m1/README.md b/quest/m1/README.md index 903693c5f1a..cefdf2d36ab 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -57,7 +57,6 @@ moves what remains of them once their claims merge. - [moq-mux reads only the base layer's H.265 SPS](/quest/m1/h265-base-layer-sps.md) - a layered MV-HEVC or SHVC stream imports as its base layer instead of misreading an enhancement-layer SPS, which still reaches the track - [A cancelled dispatch lane stops its phase promptly](/quest/m1/dispatch-cancel-race.md) - a TERM landing just before a lane forks its phase's command still stops that command, instead of waiting out the phase - [Lifecycles are explicit state](/quest/m1/explicit-state/README.md) - every lifecycle the 2026-11-07 audit found implicit is one enum or union, fixing the bugs in the 11 lifecycles it redesigns, with review and agent rules against new ones -- [Copies of one group identity must agree beyond their bytes](/quest/m1/copy-identity.md) - copies that differ in timestamp, object fields, or frame count fail as a protocol violation - [Session auth grants and replies stay consistent and bounded](/quest/m1/auth-stream-bounds.md) - an AUTH_ERROR revokes its grant, queued grants and retiring tokens are bounded, and dropping queued requests cannot deadlock - [Relay settings and admin input that cannot work are refused](/quest/m1/relay-config-checks.md) - clustered stats without a node name, pin without workers, repeated filter keys, and unknown transports fail loud - [Track drop runs are forgotten below the floor](/quest/m1/drop-runs-prune.md) - a track's dropped-group record stays proportional to its window in Rust and JS diff --git a/quest/m1/copy-identity.md b/quest/m1/copy-identity.md deleted file mode 100644 index 0cbbba322fd..00000000000 --- a/quest/m1/copy-identity.md +++ /dev/null @@ -1,13 +0,0 @@ -# [M] Copies of one group identity must agree beyond their bytes - -## Goal - -Two copies of the same broadcast+track+group (another route, a FETCH answer, a re-offer, a mid-frame recovery) that disagree on anything a reader sees fail the later copy with a protocol violation, instead of readers of each copy seeing different content under one identity. Today `rs/moq-net/src/model/group/content.rs` `row()`, `write_chunk` and the recovery in `frame.rs` compare only size, `inflated` and overlapping bytes; nothing compares a frame's timestamp, its IETF object fields, or the group's final frame count, so a copy with one more frame can raise `largest` after `finish_largest` declared it settled. - -## Plan - -Found by the 2026-11-07 invariant audit (model-1). Put the timestamp and a digest of the object fields in the shared row, and the group's fin in `Content`; a copy that disagrees, commits past the fin, or finishes short of it gets `ProtocolViolation`. Tests: two copies with differing timestamps, differing fields, and an extra frame. Check the JS counterpart (`js/net/src/group.ts`) and match it in the same PR. - -## Related - -- [A reader on a failed copy continues on the held re-offer](/quest/m1/copy-recover.md) - also stitches frames across copies diff --git a/rs/moq-net/src/model/frame.rs b/rs/moq-net/src/model/frame.rs index 10287fb8891..b151a5e29bf 100644 --- a/rs/moq-net/src/model/frame.rs +++ b/rs/moq-net/src/model/frame.rs @@ -1529,6 +1529,31 @@ pub struct Consumer { recover: Option>, } +/// One copy's read of a frame, as [`agree`] compares it. +type Read<'a> = ( + &'a kio::Consumer, + &'a Info, + Option<&'a super::object::Fields>, +); + +/// Whether two copies' reads of one frame carry the same timestamp and object properties, +/// where both copies hold them: timestamps that may be send times differ per copy, and only +/// a copy whose Object IDs arrive with its frames says a frame without fields has none. +fn agree(a: Read<'_>, b: Read<'_>) -> bool { + fn properties(fields: Option<&super::object::Fields>) -> Option<&super::object::Properties> { + fields + .map(|fields| &fields.properties) + .filter(|properties| **properties != super::object::Properties::default()) + } + fn copy(state: &kio::Consumer) -> (bool, bool) { + let state = state.read(); + (state.unchecked, state.wire().is_some()) + } + let ((a_unchecked, a_wire), (b_unchecked, b_wire)) = (copy(a.0), copy(b.0)); + (a_unchecked || b_unchecked || a.1.timestamp == b.1.timestamp) + && (!a_wire || !b_wire || properties(a.2) == properties(b.2)) +} + impl std::ops::Deref for Consumer { type Target = Info; @@ -1623,8 +1648,13 @@ impl Consumer { if frame.fields.as_ref().is_some_and(|fields| fields.id != *index) { return Poll::Ready(Err(Error::Unsupported)); } - // Same name, same content: a different size is the routes disagreeing. - if frame.info.size != self.info.size || frame.inflated != self.inflated { + // Same name, same content: anything a reader sees differing is the routes disagreeing. + if frame.info.size != self.info.size + || frame.inflated != self.inflated + || !agree( + (&self.state, &self.info, self.fields.as_deref()), + (&frame.state, &frame.info, frame.fields.as_deref()), + ) { return Poll::Ready(Err(Error::ProtocolViolation)); } recover.adopt(&replacement); diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 9eef88faa4f..fe01d9835c6 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -285,6 +285,10 @@ pub(super) struct GroupState { // How far readers got, which each frame entering the log is measured against. pace: Arc, + + // The track's timestamps may be send times ([`track::Info::unchecked`]), so copies of + // a frame need not agree on them. + pub(super) unchecked: bool, } /// The frames a copy of a group holds, from where it starts, which a successor copy of @@ -1261,6 +1265,8 @@ impl GroupState { key, size: entry.frame.payload.len() as u64, inflated: self.flate.as_ref().map(|_| entry.inflated), + timestamp: self.checked_time(entry.frame.timestamp), + fields: entry.fields.as_deref(), }, input, &self.charge, @@ -1271,6 +1277,11 @@ impl GroupState { Ok(()) } + /// A frame's timestamp as copies compare it: none where they may differ. + fn checked_time(&self, timestamp: Option) -> Option { + timestamp.filter(|_| !self.unchecked) + } + /// Whether Object IDs left the frame ordinals, so ordinal recovery cannot stitch. pub(super) fn sparse(&self) -> bool { self.wire().is_some_and(|wire| wire.sparse) @@ -1307,6 +1318,15 @@ impl GroupState { .add(slots + if self.content.is_none() { written } else { 0 }) } + /// Where this copy declared the whole group ends, as a frame position its copies share: + /// not a copy a transport cut short, nor one whose ordinals left its Object IDs. Exact + /// only when the copy holds the frame before it, or starts the group: an empty copy + /// starting later says only that nothing lies past its start. + fn declared_fin(&self) -> Option<(u64, bool)> { + let fin = self.fin.filter(|_| self.range_end.is_none() && !self.sparse())?; + Some((fin as u64, fin == 0 || fin > self.offset)) + } + /// Whether the frames held start at the group's first object. fn first_object(&self) -> bool { self.wire().map_or(self.offset == 0, |wire| wire.first_object) @@ -1618,6 +1638,7 @@ impl Producer { ))); } state.charge = charge; + state.unchecked = track.unchecked; (state.log.reader(), state.pace.clone()) }; let alive = Arc::new(Alive { @@ -2024,6 +2045,8 @@ impl Producer { key, size: frame.size, inflated: header.inflated, + timestamp: state.checked_time(timestamp), + fields: header.fields.as_deref(), }, Input::Stream(None), &state.charge, @@ -2197,6 +2220,14 @@ impl Producer { debug_assert!(state.partial.is_none(), "a frame is already open"); let fin = state.next_index; state.fin = Some(fin); + // Another copy of the group finished elsewhere, or holds a frame past this end. + if let Some(content) = &state.content + && let Some((fin, exact)) = state.declared_fin() + && let Err(err) = content.finish(fin, exact) + { + state.fin = None; + return Err(self.abort_conflict(state, err)); + } // No frame follows to compress. let original_fin = state.range_end.is_none().then_some(fin); if let Some(flate) = &mut state.flate { @@ -2408,6 +2439,8 @@ impl Handle { key, size: entry.frame.payload.len() as u64, inflated: state.flate.as_ref().map(|_| entry.inflated), + timestamp: state.checked_time(entry.frame.timestamp), + fields: entry.fields.as_deref(), }, Input::Whole(entry.frame.payload), &state.charge, @@ -2424,6 +2457,7 @@ impl Handle { let partial = state.partial.as_ref().expect("checked above"); let key = Content::key(state.next_index - 1, partial.fields.as_deref(), state.wire().is_some()); let (buf, inflated, charged) = (partial.buf.clone(), partial.inflated, partial.charged); + let (timestamp, fields) = (state.checked_time(partial.timestamp), partial.fields.clone()); let transferred = state.charge.transfer(charged); buf.bill_view(state.charge.extra()); content.row( @@ -2431,12 +2465,18 @@ impl Handle { key, size: buf.size() as u64, inflated, + timestamp, + fields: fields.as_deref(), }, Input::Stream(Some(buf)), &state.charge, Some(transferred), )?; } + // A copy that already finished the whole group says where it ends. + if let Some((fin, exact)) = state.declared_fin() { + content.finish(fin, exact)?; + } Ok(()) })(); if let Err(err) = loaded { @@ -3426,12 +3466,15 @@ struct Carry { recover: Option, // Boarding or riding a flight, once the copy read shed the frame read next. ride: Option, + // Frames the copies read before held, which the copy read now must not end short of. + committed: usize, } impl Clone for Carry { fn clone(&self) -> Self { Self { recover: self.recover.clone(), + committed: self.committed, // A clone reads in parallel, so it boards for itself. ride: self.ride.as_ref().map(|ride| Ride { boarding: None, @@ -3693,7 +3736,12 @@ impl Consumer { return res; } let index = self.cursor.index; - let Some(recover) = self.carry.as_mut().and_then(|carry| carry.recover.as_mut()) else { + let Some(Carry { + recover: Some(recover), + committed, + .. + }) = self.carry.as_deref_mut() + else { return res; }; let failed = match &res { @@ -3713,6 +3761,12 @@ impl Consumer { } // Read to its end: no route is needed for it any more. Poll::Ready(Ok(None)) => { + // A copy read before held frames past where this one ends. + if self.cursor.state.read().fin.is_some_and(|fin| fin < *committed) { + let err = Error::ProtocolViolation; + recover.failed(index, &err); + return Poll::Ready(Err(err)); + } if let Some(carry) = &mut self.carry { carry.recover = None; } @@ -3743,8 +3797,14 @@ impl Consumer { } Poll::Pending => return Poll::Pending, }; - let recover = (self.carry.as_mut().and_then(|carry| carry.recover.as_mut())) - .expect("a replacement comes from recovery"); + // Only a frame it holds proves where it reaches; an empty copy proves nothing past its start. + let committed = { + let state = self.cursor.state.read(); + Some(state.committed()).filter(|committed| *committed > state.offset) + }; + let carry = self.carry.as_mut().expect("a replacement comes from recovery"); + carry.committed = carry.committed.max(committed.unwrap_or(0)); + let recover = carry.recover.as_mut().expect("a replacement comes from recovery"); // Same frames by index, so only the channel changes; read progress and the // cap stay this cursor's. let (index, end, raw) = (self.cursor.index, self.cursor.end, self.cursor.raw); @@ -4183,8 +4243,8 @@ impl Consumer { (Some(carry), ride) => carry.ride = ride, (None, Some(ride)) => { self.carry = Some(Box::new(Carry { - recover: None, ride: Some(ride), + ..Default::default() })) } (None, None) => {} @@ -6954,6 +7014,149 @@ mod test { } } + /// A group's copies sharing content, the first finished with two frames if `finished`. + fn shared_copies(info: track::Info, finished: bool) -> (Producer, impl Fn() -> Producer) { + let cache = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); + let mut first = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + first.write_frame(None, verbose(0)).unwrap(); + first.write_frame(None, verbose(1)).unwrap(); + if finished { + first.finish().unwrap(); + } + let content = first.handle.activate_content().unwrap().unwrap(); + let copy = move || { + let copy = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + copy.handle.share_content(content.clone()); + copy + }; + (first, copy) + } + + /// Copies of a frame with the same bytes still disagree on another timestamp, except on + /// a track whose timestamps may be send times. + #[test] + fn copies_disagreeing_on_a_timestamp_fail() { + let ms = |ms| Timestamp::from_millis(ms).unwrap(); + for unchecked in [false, true] { + let cache = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); + let mut info = track::Info::default().with_timescale(Timescale::MILLI); + info.unchecked = unchecked; + let mut first = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + first.write_frame(ms(1), verbose(0)).unwrap(); + let content = first.handle.activate_content().unwrap().unwrap(); + let mut copy = Producer::new(Info::from(0u64), info, cache); + copy.handle.share_content(content); + let res = copy.write_frame(ms(2), verbose(0)); + match unchecked { + false => assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"), + true => assert!(res.is_ok(), "{res:?}"), + } + assert_eq!(copy.handle.is_aborted(), !unchecked); + } + } + + /// Copies of an object with the same bytes still disagree on other properties. + #[test] + fn copies_disagreeing_on_object_properties_fail() { + let write = |group: Producer, properties: &'static [u8]| -> Result { + let fields = super::super::object::Fields { + id: 0, + properties: super::super::object::Properties { + sealed_time: None, + block: Some(super::super::object::Block { + bytes: Bytes::from_static(properties), + immutable: false, + }), + }, + }; + let payload = verbose(0); + let mut frame = group.create_object(frame::Header { + info: untimed(payload.len()), + fields: Some(Arc::new(fields)), + inflated: None, + })?; + frame.write(payload)?; + frame.finish() + }; + let cache = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); + let producer = || Producer::new(Info::from(0u64), track::Info::default(), cache.clone()); + let first = write(producer(), b"\x01\x02").unwrap(); + let content = first.handle.activate_content().unwrap().unwrap(); + let copy = || { + let copy = producer(); + copy.handle.share_content(content.clone()); + copy + }; + assert!(write(copy(), b"\x01\x02").is_ok()); + let differing = copy(); + let handle = differing.handle(); + assert!(matches!(write(differing, b"\x01\x03"), Err(Error::ProtocolViolation))); + assert!(handle.is_aborted()); + } + + /// A copy holding a frame past the end another declared fails, whether it writes the + /// frame whole or streams it, and whether the end was declared before the copies shared + /// content or after. + #[test] + fn a_copy_past_the_declared_end_fails() { + for finished in [true, false] { + let (mut first, copy) = shared_copies(track::Info::default(), finished); + if !finished { + first.finish().unwrap(); + } + let mut same = copy(); + same.write_frame(None, verbose(0)).unwrap(); + same.write_frame(None, verbose(1)).unwrap(); + same.finish().unwrap(); + + let mut whole = copy(); + whole.write_frame(None, verbose(0)).unwrap(); + whole.write_frame(None, verbose(1)).unwrap(); + let res = whole.write_frame(None, verbose(2)); + assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"); + assert!(whole.handle.is_aborted()); + + let mut streamed = copy(); + streamed.write_frame(None, verbose(0)).unwrap(); + streamed.write_frame(None, verbose(1)).unwrap(); + let handle = streamed.handle(); + assert!(matches!( + streamed.create_frame(untimed(1)), + Err(Error::ProtocolViolation) + )); + assert!(handle.is_aborted()); + } + } + + /// A copy finishing short of a frame another copy holds fails, as does one finishing + /// short of another's declared end. A copy its transport cut short declares no end. + #[test] + fn a_copy_finishing_short_fails() { + for finished in [false, true] { + let (_first, copy) = shared_copies(track::Info::default(), finished); + let mut short = copy(); + short.write_frame(None, verbose(0)).unwrap(); + assert!(matches!(short.finish(), Err(Error::ProtocolViolation))); + assert!(short.handle.is_aborted()); + assert!(!short.handle.is_finished()); + + let mut cut = copy(); + cut.bound(Bounds { start: 0, end: Some(1) }).unwrap(); + cut.write_frame(None, verbose(0)).unwrap(); + cut.finish().unwrap(); + + // An empty copy starting at or past the end says only that nothing lies past it. + for start in [2, 3] { + let mut past = copy(); + past.start_at(start).unwrap(); + past.finish().unwrap(); + } + let mut within = copy(); + within.start_at(1).unwrap(); + assert!(matches!(within.finish(), Err(Error::ProtocolViolation))); + } + } + /// A received slice that inflates to other than its declared size fails the read. #[test] fn a_received_size_mismatch_fails_the_read() { diff --git a/rs/moq-net/src/model/group/content.rs b/rs/moq-net/src/model/group/content.rs index 4e0353744a6..038d207ed0c 100644 --- a/rs/moq-net/src/model/group/content.rs +++ b/rs/moq-net/src/model/group/content.rs @@ -1,7 +1,9 @@ //! Weak indexed identity; delivery records and streaming readers own the bytes. +use super::super::object; use super::*; use std::collections::hash_map::RandomState; -use std::hash::Hash; +use std::hash::{BuildHasher, Hash}; +use std::sync::LazyLock; use std::sync::atomic::AtomicU64; type Map = hashbrown::HashMap; @@ -28,25 +30,62 @@ pub(in crate::model) enum Key { Object(u64), } +impl Key { + /// Where the frame sits in the group: its Object ID, which a frame without one shares + /// with its ordinal, so every copy of one track counts in the same space. + fn position(self) -> u64 { + match self { + Self::Frame(index) | Self::Object(index) => index, + } + } +} + +/// Keyed per process, so a peer cannot pick properties that collide with another copy's. +static DIGEST: LazyLock = LazyLock::new(RandomState::new); + +/// A frame's object properties, as one value copies compare. A frame without fields holds +/// none, the same as an empty block; its Object ID is already in its [`Key`]. +fn digest(fields: Option<&object::Fields>) -> u64 { + match fields.map(|fields| &fields.properties) { + Some(properties) if *properties != object::Properties::default() => DIGEST.hash_one(properties), + _ => 0, + } +} + pub(super) enum Input { Whole(Payload), Stream(Option), Adopt(frame::Shared), } -pub(super) struct Spec { +pub(super) struct Spec<'a> { pub key: Key, pub size: u64, pub inflated: Option, + /// The frame's timestamp, or `None` on a track whose timestamps may be send times + /// ([`track::Info::unchecked`]), which every copy of that track shares. + pub timestamp: Option, + pub fields: Option<&'a object::Fields>, } struct Row { inflated: Option, + timestamp: Option, + fields: u64, content: frame::SharedWeak, } +#[derive(Default)] +struct State { + rows: Map, + // One past the last frame position a copy declared complete with its finish. + fin: Option, + // One past the furthest frame position any copy opened. + reached: u64, +} + pub(in crate::model) struct Content { - state: Mutex>, + state: Mutex, frame_charge: AtomicU64, bill: cache::Extra, plain: Mutex>>, @@ -68,7 +107,7 @@ impl Content { }) } - pub(super) fn key(index: usize, fields: Option<&super::super::object::Fields>, wire: bool) -> Key { + pub(super) fn key(index: usize, fields: Option<&object::Fields>, wire: bool) -> Key { if wire || fields.is_some() { Key::Object(fields.map_or(index as u64, |fields| fields.id)) } else { @@ -78,16 +117,31 @@ impl Content { pub(super) fn row( self: &Arc, - spec: Spec, + spec: Spec<'_>, input: Input, charge: &cache::Charge, transferred: Option, ) -> Result { - let Spec { key, size, inflated } = spec; + let Spec { + key, + size, + inflated, + timestamp, + fields, + } = spec; + let fields = digest(fields); + let position = key.position(); let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - if let Some(row) = state.get(&key) { + // A copy finished before this position, so the frame does not exist. + if state.fin.is_some_and(|fin| position >= fin) { + return Err(Error::ProtocolViolation); + } + if let Some(row) = state.rows.get(&key) { if let Some(shared) = row.content.upgrade() { - let matches = shared.size() as u64 == size && row.inflated == inflated; + let matches = shared.size() as u64 == size + && row.inflated == inflated + && row.timestamp == timestamp + && row.fields == fields; // A temporary strong owner must never drop under this mutex: its last // release removes its weak record through `forget_frame`. drop(state); @@ -110,7 +164,7 @@ impl Content { } return Ok(shared); } - state.remove(&key); + state.rows.remove(&key); } // Retained bytes/frame limits remain per delivery. Combining valid disjoint // ranges must not introduce a stricter aggregate admission policy. @@ -129,18 +183,40 @@ impl Content { } shared.bill_content(transferred.unwrap_or_else(|| charge.extra())); insert( - &mut state, + &mut state.rows, key, Row { inflated, + timestamp, + fields, content: shared.downgrade(), }, &self.bill, ); + state.reached = state.reached.max(position + 1); drop(state); Ok(shared) } + /// Declare that the group ends at frame position `fin`, or at most there unless `exact`, + /// refusing a copy that disagrees: one finished elsewhere at another position, or one + /// short of a frame another opened. + pub(super) fn finish(&self, fin: u64, exact: bool) -> Result<()> { + let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); + let agrees = match state.fin { + Some(prior) if exact => prior == fin, + Some(prior) => prior <= fin, + None => true, + }; + if !agrees || state.reached > fin { + return Err(Error::ProtocolViolation); + } + if exact { + state.fin = Some(fin); + } + Ok(()) + } + pub(in crate::model) fn charge_frame(&self, bytes: u64) { self.frame_charge.fetch_add(bytes, Ordering::Relaxed); } @@ -150,8 +226,8 @@ impl Content { .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |total| total.checked_sub(charge)) .expect("canonical frame charge underflow"); let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - if state.get(&key).is_some_and(|row| row.content.address() == address) { - remove(&mut state, &key, &self.bill); + if state.rows.get(&key).is_some_and(|row| row.content.address() == address) { + remove(&mut state.rows, &key, &self.bill); } } @@ -178,6 +254,7 @@ impl Content { self.state .lock() .unwrap_or_else(PoisonError::into_inner) + .rows .values() .filter_map(|row| row.content.upgrade()) .collect() @@ -306,6 +383,8 @@ impl StorageBench { key: Key::Frame(index as u64), size: 64, inflated: None, + timestamp: None, + fields: None, }, Input::Whole(Bytes::from(vec![0; 64]).into()), &charge, @@ -353,6 +432,8 @@ mod tests { key: Key::Frame(index), size: 0, inflated: None, + timestamp: None, + fields: None, }, Input::Whole(Bytes::new().into()), &charge, @@ -369,7 +450,7 @@ mod tests { assert_eq!(current.cost().0, 512); drop(frames); assert_eq!(current.cost().0, 0); - assert_eq!(current.state.lock().unwrap().allocation_size(), 0); + assert_eq!(current.state.lock().unwrap().rows.allocation_size(), 0); drop(current); assert!(contents.get(7).is_none()); assert_eq!( diff --git a/rs/moq-net/src/model/object.rs b/rs/moq-net/src/model/object.rs index 8e08bc7b05e..266a4e2e868 100644 --- a/rs/moq-net/src/model/object.rs +++ b/rs/moq-net/src/model/object.rs @@ -19,7 +19,7 @@ pub(crate) struct Property { /// /// On a timed track, the mutable Timestamp is never held: it is the frame's, which egress /// restates. On an untimed one it has no units, so it stays in the block like any other. -#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] pub(crate) struct Properties { /// The Timestamp the block's Immutable Properties hold on a timed track, which keeps /// its ingress units. @@ -32,7 +32,7 @@ pub(crate) struct Properties { /// /// Kept encoded because a decoded list costs many times its wire size, and a relay holds /// one per cached object. Egress decodes it only to re-encode for another draft. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] pub(crate) struct Block { /// The properties in ascending ID order, serialized as the track's /// [`TrackProperties::version`] does. Never empty. diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 5dcd5c43f23..a2b83a8e911 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -3496,6 +3496,75 @@ mod test { assert!(chunks.read_chunk().now_or_never().unwrap().unwrap().is_none()); } + /// A half-read frame refuses a replacement that stamps it with another time. + #[test] + #[cfg(feature = "async")] + fn a_half_read_frame_refuses_a_replacement_with_another_timestamp() { + let routes = Producer::new(); + let logical = logical(&routes); + let a = copy(); + routes.serve(a.consume()); + let mut sub = subscribe(&logical, Duration::from_secs(10)); + + let open = a.create_group(group::Info { sequence: 0 }).unwrap(); + let mut frame = open + .create_frame(crate::frame::Info { + size: 4, + timestamp: Some(ts(0)), + }) + .unwrap(); + frame.write(b"ab".as_ref()).unwrap(); + let mut reading = recv(&mut sub); + let mut chunks = reading.next_frame().now_or_never().unwrap().unwrap().unwrap(); + assert!(chunks.read_chunk().now_or_never().unwrap().unwrap().is_some()); + + drop(frame); + a.clone().abort(Error::Dropped).unwrap(); + let b = copy(); + let mut whole = b.create_group(group::Info { sequence: 0 }).unwrap(); + whole.write_frame(ts(1), b"abcd".as_ref()).unwrap(); + routes.serve(b.consume()); + + let res = chunks.read_chunk().now_or_never().unwrap(); + assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"); + } + + /// A group read past where the replacement finishes it refuses that end, rather than + /// ending cleanly with frames the replacement says do not exist. + #[test] + #[cfg(feature = "async")] + fn a_resumed_group_refuses_a_replacement_ending_before_what_was_read() { + for frames in [2, 1] { + let routes = Producer::new(); + let logical = logical(&routes); + let a = copy(); + routes.serve(a.consume()); + let mut sub = subscribe(&logical, Duration::from_secs(10)); + + let mut open = a.create_group(group::Info { sequence: 0 }).unwrap(); + open.write_frame(ts(0), b"a".as_ref()).unwrap(); + open.write_frame(ts(1), b"b".as_ref()).unwrap(); + let mut reading = recv(&mut sub); + assert_eq!(read(&mut reading), Some(b"a".to_vec())); + assert_eq!(read(&mut reading), Some(b"b".to_vec())); + a.clone().abort(Error::Dropped).unwrap(); + + let b = copy(); + let mut replacement = b.create_group(group::Info { sequence: 0 }).unwrap(); + for (frame, payload) in [b"a", b"b"].into_iter().enumerate().take(frames) { + replacement.write_frame(ts(frame as u64), payload.as_ref()).unwrap(); + } + replacement.finish().unwrap(); + routes.serve(b.consume()); + + let res = reading.read_frame().now_or_never().expect("a result"); + match frames { + 2 => assert!(matches!(res, Ok(None)), "{res:?}"), + _ => assert!(matches!(res, Err(Error::ProtocolViolation)), "{res:?}"), + } + } + } + /// A resumed group that the serving route gives up or aborts ends with that route's /// error, even with no successor or later writes and its fetch still unanswered. #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0ee302943ad..726fe2b39fe 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -18035,9 +18035,9 @@ mod test { // Serving it replaces the too-narrow entry rather than colliding with it. let mut whole = request.accept(Info::timed()).unwrap(); - whole - .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"head")) - .unwrap(); + for payload in [b"head".as_ref(), b"mid1", b"mid2", b"tail"] { + whole.write_frame(Timestamp::ZERO, payload).unwrap(); + } whole.finish().unwrap(); let mut served = fetch.await.unwrap(); @@ -18242,6 +18242,7 @@ mod test { for request in requests { let mut head = request.accept(Info::default()).unwrap(); head.write_frame(None, bytes::Bytes::from_static(b"snapshot")).unwrap(); + head.write_frame(None, bytes::Bytes::from_static(b"delta")).unwrap(); head.finish().unwrap(); } @@ -18280,6 +18281,7 @@ mod test { sub.update(replay().with_range(1..2)).unwrap(); let mut head = request.accept(Info::default()).unwrap(); head.write_frame(None, bytes::Bytes::from_static(b"snapshot")).unwrap(); + head.write_frame(None, bytes::Bytes::from_static(b"delta")).unwrap(); head.finish().unwrap(); assert!( matches!(sub.recv_group().now_or_never(), Some(Ok(None))), @@ -18638,6 +18640,8 @@ mod test { .unwrap(); head.write_frame(Timestamp::ZERO, bytes::Bytes::from(vec![0u8; 10_000])) .unwrap(); + head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) + .unwrap(); head.finish().unwrap(); drop(fetch.await.unwrap()); if abort_live { @@ -18726,6 +18730,9 @@ mod test { .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"from0")) .unwrap(); assert_eq!(widest.await.unwrap().index(), 0); + for payload in [b"from1".as_ref(), b"from2"] { + group.write_frame(Timestamp::ZERO, payload).unwrap(); + } group.finish().unwrap(); }