From c04925c5803e0cf1678d22eb091aee0d5a88cf85 Mon Sep 17 00:00:00 2001 From: Ken Mantle Date: Wed, 11 Nov 2026 02:56:34 -0500 Subject: [PATCH] fix(cos): a clone that edits an object stream or a /Length stops reading what other clones cached (#6910) * fix(cos): a clone that edits an object stream or a /Length stops reading what other clones cached Clones share the object cache and the parsed object streams. #5802 keeps results that read a clone's edits out of those shared maps, but a clone could still read entries cached before its edit, by another clone or by itself: after replacing object stream 4, a member of it still read as the file's copy, and a stream or object stream framed by an indirect /Length kept the file's framing after the length was edited. Each cached load now records the other objects it read (a member's object stream, an indirect /Length, and what those read in turn), and a document whose edits include one of them reads the object again. Documents with no edits read the shared entries as before. This also replaces forgetting a stream's cached member failures on every edit, which the same check covers for the editing document without costing other clones their entries. Co-Authored-By: Claude Opus 5.5 * fix(cos): count a reference read as null in what a load read A reference whose generation doesn't match the file reads as null, but an edit that sets that generation makes it match. A load that read null there (an object stream's /DecodeParms by reference, say) now counts the object as read, so a clone that sets it decodes the stream again instead of taking the one another clone cached. Reads keep a shared slice, and the read bound has its own test. Co-Authored-By: Claude Opus 5.5 * test(optimize): edit the object stream without breaking it space_audit_shares_an_object_stream_..._edited_members set the stream's /First to 999999 and still expected its members to read, which only held while a stale shared-cache entry answered for them. Mark the stream with an unrelated key instead: it is still edited, so its members are sized on their own, and they stay readable. Co-Authored-By: Claude Opus 5.5 * fix(cos): keep an object stream decoded by edited parameters out of the shared cache Whether an object stream is the loading document's own was settled before its indirect /DecodeParms were read. A clone that edited them decoded the stream by its edit and put the result in the cache its clones share, so the document it came from read the clone's data. The check now runs after decoding. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: KenM76 Co-authored-by: Claude Opus 5.5 --- crates/cos/src/document/objects.rs | 60 ++++++---- crates/cos/src/document/state.rs | 118 ++++++++++++++++--- crates/cos/src/document/tests.rs | 181 ++++++++++++++++++++++++++++- crates/optimize/src/audit.rs | 3 +- 4 files changed, 320 insertions(+), 42 deletions(-) diff --git a/crates/cos/src/document/objects.rs b/crates/cos/src/document/objects.rs index 925a7c879d..42aef4af0f 100644 --- a/crates/cos/src/document/objects.rs +++ b/crates/cos/src/document/objects.rs @@ -2,8 +2,8 @@ use std::collections::HashSet; use super::reader::find; use super::state::{ - LENGTHS, LoadGuard, ObjStm, ObjectReader, ReadState, ScanLengths, Slot, SourceIdentity, loaded_bytes, loading, note_overlay_read, objstm_member, - overlay_reads, + LENGTHS, LoadGuard, ObjStm, ObjectReader, ReadFrame, ReadState, ScanLengths, Slot, SourceIdentity, loaded_bytes, loading, note_overlay_read, + note_read, objstm_member, overlay_reads, }; use super::xref::{Revision, XrefEntry, XrefIndex}; use super::{ @@ -244,6 +244,12 @@ impl Document { pub(super) fn try_get_reference(&self, r: ObjRef) -> Result, CosError> { if !self.reference_matches(r) { + // An edit of `r.num` can make it match, so a load that read null here read it too; + // with `r.num` edited (freed, or set at another generation) the null is the edit's. + if self.overlay.contains_key(&r.num) { + note_overlay_read(); + } + note_read(r.num, None); return Ok(Arc::new(Object::Null)); } self.try_get(r.num) @@ -554,22 +560,32 @@ impl Document { pub fn try_get(&self, num: u32) -> Result, CosError> { if let Some(slot) = self.overlay.get(&num) { + note_read(num, None); note_overlay_read(); return Ok(match slot { Slot::Set(_, o) | Slot::RepairedSet(_, _, o) => o.clone(), Slot::Freed(_) => Arc::new(Object::Null), }); } - if let Some(loaded) = self.cache.lock().map_err(|_| CosError::Poisoned)?.get(&num) { + // Clones share the cache: what a load read that this document has edited since (the + // member's object stream, a `/Length`) makes the entry another document's. + if let Some((loaded, reads)) = self.cache.lock().map_err(|_| CosError::Poisoned)?.get(&num) + && !reads.touch(&self.overlay) + { + note_read(num, Some(reads)); return loaded.clone(); } + note_read(num, None); // Loading can re-enter (indirect stream `/Length`, object streams); damaged files make // cycles such as `6 0 obj << /Length 6 0 R >>`. Bound the nesting on every path. let _guard = LoadGuard::enter(num).ok_or_else(|| CosError::Syntax { offset: 0, detail: format!("reference cycle while loading object {num}") })?; let _scope = crate::security::FollowScope::enter(); let edits_read = overlay_reads(); + let frame = ReadFrame::enter(); let loaded = self.load(num, 0).map(Arc::new); + let reads = frame.reads(); + drop(frame); if let Err(e) = &loaded && !matches!(e, CosError::Poisoned) { @@ -581,7 +597,7 @@ impl Document { if overlay_reads() == edits_read && !matches!(loaded, Err(CosError::Poisoned)) { // The editing cache is bounded like the scan reader's: re-parsing a file-backed view // is cheap, and returned objects outlive eviction. - self.cache.lock().map_err(|_| CosError::Poisoned)?.insert(num, loaded.clone(), loaded_bytes(&loaded, &self.data)); + self.cache.lock().map_err(|_| CosError::Poisoned)?.insert(num, loaded.clone(), loaded_bytes(&loaded, &self.data), reads); self.evict_editor_cache_if_over_budget(num)?; } loaded @@ -724,9 +740,14 @@ impl Document { pub(super) fn objstm(&self, num: u32) -> Result, CosError> { // An edited or freed stream is this document's alone: clones share the parsed streams. let edited = self.overlay.contains_key(&num); - if !edited && let Some(s) = self.objstms.lock().map_err(|_| CosError::Poisoned)?.get(&num) { + if !edited + && let Some(s) = self.objstms.lock().map_err(|_| CosError::Poisoned)?.get(&num) + && !s.reads.touch(&self.overlay) + { + note_read(num, Some(&s.reads)); return Ok(s.clone()); } + let frame = ReadFrame::enter(); // A file entry that cannot place the stream (one that says it is itself compressed // inside another stream, as in #2367, or points nowhere) is tried by the stream's own // header, as a plain object with a wrong offset is. An edited or freed stream, in the @@ -740,8 +761,6 @@ impl Document { } _ => Arc::new(self.scan_for(num).map(|(id, o)| self.decrypted(id, o)).ok_or(CosError::MissingObject(num))?), }; - // A stream whose load read this document's edits (its `/Length`) is as much its own as an edited one. - let own = edited || overlay_reads() != edits_read; let Object::Stream(s) = &*loaded else { return Err(CosError::MissingObject(num)) }; let n = s.dict.int(b"N").unwrap_or(0).clamp(0, 1_000_000) as usize; let first = usize::try_from(s.dict.int(b"First").unwrap_or(0).max(0)).ok(); @@ -758,8 +777,10 @@ impl Document { let (Some(Object::Int(onum)), Some(Object::Int(off))) = (lx.object().ok(), lx.object().ok()) else { break }; index.push(objstm_member(onum, off, first, data.len())); } - let stm = Arc::new(ObjStm { index, data: Arc::new(data) }); - if own { + let stm = Arc::new(ObjStm { index, data: Arc::new(data), reads: frame.reads() }); + // A stream whose load or decoding read this document's edits (its `/Length`, its + // `/DecodeParms`) is as much its own as an edited one. + if edited || overlay_reads() != edits_read { return Ok(stm); } // Same bound as the object cache: decoded object streams can be megabytes each. @@ -777,17 +798,21 @@ impl Document { note_overlay_read(); } let cached = loading(); - // A length resolved through this document's edits counts as an edit read on every reuse. - if cached && let Some((known, edits)) = LENGTHS.with(|m| m.borrow().get(&r).copied()) { + // Each reuse counts as a read of the length and of what resolving it read, and as an edit + // read when that went through this document's edits. + if cached && let Some((known, edits, reads)) = LENGTHS.with(|m| m.borrow().get(&r).cloned()) { if edits { note_overlay_read(); } + note_read(r.num, Some(&reads)); return known; } + note_read(r.num, None); let edits_read = overlay_reads(); + let frame = ReadFrame::enter(); let length = self.try_get_reference(r).ok().and_then(|o| o.as_int()); if cached { - LENGTHS.with(|m| m.borrow_mut().insert(r, (length, overlay_reads() != edits_read))); + LENGTHS.with(|m| m.borrow_mut().insert(r, (length, overlay_reads() != edits_read, frame.reads()))); } length } @@ -847,7 +872,6 @@ impl Document { pub fn set(&mut self, r: ObjRef, obj: impl Into) { let obj = self.adopt(obj.into()); self.overlay.insert(r.num, Slot::Set(r.generation, Arc::new(obj))); - self.forget_member_failures(r.num); if r.num >= self.next_num { self.next_num = r.num.saturating_add(1); } @@ -907,16 +931,6 @@ impl Document { /// Delete an object (its number becomes free with the next generation). pub fn free(&mut self, r: ObjRef) { self.overlay.insert(r.num, Slot::Freed(r.generation.saturating_add(1))); - self.forget_member_failures(r.num); - } - - /// Forget cached failures of the members `stream` held in the file: they were failures of - /// the file's copy, and an edit of `stream` may read. Clones share the cache, so theirs are - /// searched again too, which costs time only. A poisoned lock leaves the cache as it is. - fn forget_member_failures(&self, stream: u32) { - if let Ok(mut cache) = self.cache.lock() { - cache.forget_failures(|n| matches!(self.entries.lookup(*n, &self.data), Some(XrefEntry::InStream { stream: s, .. }) if s == stream)); - } } /// Free what was added since the file was read and nothing refers to any more: the objects diff --git a/crates/cos/src/document/state.rs b/crates/cos/src/document/state.rs index c5bd85c065..7359ce715a 100644 --- a/crates/cos/src/document/state.rs +++ b/crates/cos/src/document/state.rs @@ -8,7 +8,10 @@ thread_local! { /// retries a parse several ways, and each retry would resolve the same lengths again /// (exponential in the chain of streams whose lengths point at further streams). Each is /// kept with whether resolving it read unsaved edits. - pub(super) static LENGTHS: std::cell::RefCell, bool)>> = std::cell::RefCell::new(HashMap::new()); + pub(super) static LENGTHS: std::cell::RefCell> = std::cell::RefCell::new(HashMap::new()); + /// What the loads running on this thread have read ([`ReadFrame`]): how many frames are + /// open, the object numbers read inside them, and how often that list was full. + static READS: std::cell::RefCell<(u32, Vec, u64)> = const { std::cell::RefCell::new((0, Vec::new(), 0)) }; /// Unsaved edits read on this thread (`Document::try_get`, and a `/Length` in `Document::load`). static OVERLAY_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; /// How deep `Document::scan_for` is nested on this thread, and how many indirect `/Length` @@ -59,6 +62,94 @@ impl Drop for ScanLengths { } } +/// A resolved `/Length` in [`LENGTHS`]: its value, whether resolving it read unsaved edits, and +/// what it read. +pub(super) type Length = (Option, bool, Reads); + +/// The other objects a cached load read: an object stream for a member, an indirect `/Length`, +/// and what those read in turn. Clones share the caches, so a document whose edits include one of +/// them reads the object again instead (`Document::try_get`); `None` is a load that read more +/// than [`Reads::MAX`], which such a document always reads again. +#[derive(Clone, Debug, Default)] +pub(super) struct Reads(Option>); + +impl Reads { + /// Objects one entry keeps: a member's stream and a length or two, and what they read. + const MAX: usize = 64; + /// Object numbers the open frames keep between them. + const HELD: usize = 4096; + + /// Whether `overlay` (a document's edits) holds any of these objects. + pub(super) fn touch(&self, overlay: &BTreeMap) -> bool { + !overlay.is_empty() && self.0.as_ref().is_none_or(|nums| nums.iter().any(|n| overlay.contains_key(n))) + } + + fn bytes(&self) -> usize { + self.0.as_ref().map_or(0, |nums| nums.len().saturating_mul(std::mem::size_of::())) + } +} + +/// Counts `num` as read by the loads open on this thread, with what loading it read (`reads`). +pub(super) fn note_read(num: u32, reads: Option<&Reads>) { + READS.with(|r| { + let (depth, held, full) = &mut *r.borrow_mut(); + if *depth == 0 { + return; + } + let more = reads.map_or(Some(&[][..]), |r| r.0.as_deref()); + match more { + Some(more) if held.len().saturating_add(more.len()) < Reads::HELD => { + held.push(num); + held.extend_from_slice(more); + } + _ => *full = full.wrapping_add(1), + } + }); +} + +/// Collects what one load reads, for [`Reads`]; frames nest, and each read counts for every +/// frame open around it. +pub(super) struct ReadFrame { + start: usize, + full: u64, +} + +impl ReadFrame { + pub(super) fn enter() -> Self { + READS.with(|r| { + let (depth, held, full) = &mut *r.borrow_mut(); + *depth = depth.saturating_add(1); + ReadFrame { start: held.len(), full: *full } + }) + } + + /// What was read since the frame opened. + pub(super) fn reads(&self) -> Reads { + READS.with(|r| { + let (_, held, full) = &*r.borrow(); + if *full != self.full { + return Reads(None); + } + let mut nums = held.get(self.start..).unwrap_or_default().to_vec(); + nums.sort_unstable(); + nums.dedup(); + Reads((nums.len() <= Reads::MAX).then(|| Arc::from(nums))) + }) + } +} + +impl Drop for ReadFrame { + fn drop(&mut self) { + READS.with(|r| { + let (depth, held, _) = &mut *r.borrow_mut(); + *depth = depth.saturating_sub(1); + if *depth == 0 { + held.clear(); + } + }); + } +} + /// Unsaved edits read so far on this thread: a load during which this changes depended on one. pub(super) fn overlay_reads() -> u64 { OVERLAY_READS.with(std::cell::Cell::get) @@ -122,6 +213,8 @@ pub(super) struct ObjStm { /// or offset is unusable ([`objstm_member`]). pub(super) index: Vec>, pub(super) data: Arc>, + /// What parsing it read: the stream itself and its `/Length`, `/DecodeParms` and so on. + pub(super) reads: Reads, } /// Where member `num` of an object stream starts: its `offset` past `/First` (`first`), when the @@ -317,13 +410,14 @@ thread_local! { /// (#5873). #[derive(Default)] pub(crate) struct ObjectCache { - entries: HashMap, + entries: HashMap, owned: usize, } impl ObjectCache { - pub(crate) fn get(&self, num: &u32) -> Option<&Loaded> { - self.entries.get(num).map(|(object, _)| object) + /// What loading `num` gave, and what that load read. + pub(super) fn get(&self, num: &u32) -> Option<(&Loaded, &Reads)> { + self.entries.get(num).map(|(object, _, reads)| (object, reads)) } #[cfg(test)] @@ -331,9 +425,10 @@ impl ObjectCache { self.entries.contains_key(num) } - /// Keep what loading `num` gave, which holds `owned` bytes ([`loaded_bytes`]). - pub(crate) fn insert(&mut self, num: u32, object: Loaded, owned: usize) { - if let Some((_, before)) = self.entries.insert(num, (object, owned)) { + /// Keep what loading `num` gave, which holds `owned` bytes ([`loaded_bytes`]), and what it read. + pub(super) fn insert(&mut self, num: u32, object: Loaded, owned: usize, reads: Reads) { + let owned = owned.saturating_add(reads.bytes()); + if let Some((_, before, _)) = self.entries.insert(num, (object, owned, reads)) { self.owned = self.owned.saturating_sub(before); } self.owned = self.owned.saturating_add(owned); @@ -358,19 +453,14 @@ impl ObjectCache { self.owned = 0; } - /// Drop the cached failures `stale` picks. A failure holds no bytes, so the total stands. - pub(crate) fn forget_failures(&mut self, mut stale: impl FnMut(&u32) -> bool) { - self.entries.retain(|num, (loaded, _)| loaded.is_ok() || !stale(num)); - } - pub(crate) fn retain(&mut self, mut keep: impl FnMut(&u32) -> bool) { self.entries.retain(|num, _| keep(num)); - self.owned = self.entries.values().fold(0usize, |n, (_, owned)| n.saturating_add(*owned)); + self.owned = self.entries.values().fold(0usize, |n, (_, owned, _)| n.saturating_add(*owned)); } #[cfg(test)] pub(crate) fn values(&self) -> impl Iterator> { - self.entries.values().filter_map(|(object, _)| object.as_ref().ok()) + self.entries.values().filter_map(|(object, _, _)| object.as_ref().ok()) } } diff --git a/crates/cos/src/document/tests.rs b/crates/cos/src/document/tests.rs index c0e36cdc1c..814d4ef8eb 100644 --- a/crates/cos/src/document/tests.rs +++ b/crates/cos/src/document/tests.rs @@ -883,6 +883,179 @@ fn an_edited_object_stream_leaves_other_clones_members_alone() { assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes"), "the replacement reached another clone"); } +/// A member another clone read from the file's object stream is not what a clone that replaced +/// the stream reads, nor is one the document read itself before replacing it. +#[test] +fn a_replaced_object_stream_hides_the_members_read_before() { + let replacement = || { + let mut dict = Dict::new(); + dict.set(b"Type".to_vec(), Object::name("ObjStm")); + dict.set(b"N".to_vec(), Object::Int(1)); + dict.set(b"First".to_vec(), Object::Int(4)); + crate::Stream::from_raw(dict, b"3 0 << /Hidden (no) >>".to_vec()) + }; + let file = Arc::new(object_stream_file("3 0 ", "<< /Hidden (yes) >>", 0)); + let doc = Document::open(file.clone()).unwrap(); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes")); + let mut replaced = doc.clone(); + replaced.set(ObjRef::new(4, 0), replacement()); + assert_eq!(text_of(&replaced, 3, b"Hidden").as_deref(), Some("no"), "another clone's read of the file's stream"); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes"), "the replacement reached another clone"); + let mut doc = Document::open(file.clone()).unwrap(); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes")); + doc.set(ObjRef::new(4, 0), replacement()); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("no"), "the document's own read before the edit"); + let doc = Document::open(file).unwrap(); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("yes")); + let mut freed = doc.clone(); + freed.free(ObjRef::new(4, 0)); + assert_eq!(freed.try_get(3), Err(CosError::MissingObject(4)), "another clone's read before the free"); +} + +/// An object stream another clone framed by the file's indirect `/Length` is not what a clone +/// that edited that length reads. +#[test] +fn an_edited_object_stream_length_hides_the_members_read_before() { + let file = Arc::new(object_stream_file_with("3 0 ", "<< /Hidden (cut endstream here) >>", 0, true)); + let doc = Document::open(file).unwrap(); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("cut endstream here")); + let mut edited = doc.clone(); + edited.set(ObjRef::new(6, 0), Object::Int("3 0 << /Hidden (cut".len() as i64)); + assert_ne!(text_of(&edited, 3, b"Hidden").as_deref(), Some("cut endstream here"), "another clone's framing of the stream"); + assert_eq!(text_of(&doc, 3, b"Hidden").as_deref(), Some("cut endstream here"), "the edited length reached another clone"); +} + +/// A stream another clone read by the file's indirect `/Length` is not what a clone that edited +/// that length reads. +#[test] +fn an_edited_stream_length_hides_the_stream_read_before() { + let bodies = [ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [] /Count 0 >>", + "<< /Length 4 0 R >>\nstream\nshort\nendstream long\nendstream", + "20", + ]; + let mut out = b"%PDF-1.4\n".to_vec(); + let mut table = String::from("xref\n0 5\n0000000000 65535 f \n"); + for (n, body) in bodies.iter().enumerate() { + table.push_str(&format!("{:010} 00000 n \n", out.len())); + out.extend_from_slice(format!("{} 0 obj\n{body}\nendobj\n", n + 1).as_bytes()); + } + let start = out.len(); + out.extend_from_slice(format!("{table}trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n{start}\n%%EOF\n").as_bytes()); + let data = |doc: &Document| match &*doc.get(ObjRef::new(3, 0)) { + Object::Stream(s) => s.raw.to_vec(), + other => panic!("{other:?}"), + }; + let doc = Document::open(Arc::new(out)).unwrap(); + assert_eq!(data(&doc), b"short\nendstream long"); + let mut edited = doc.clone(); + edited.set(ObjRef::new(4, 0), Object::Int(5)); + assert_eq!(data(&edited), b"short", "another clone's read by the file's length"); + assert_eq!(data(&doc), b"short\nendstream long", "the edited length reached another clone"); +} + +/// A file whose object 3 sits in an object stream PNG-predicted by `/DecodeParms 6 1 R`, a +/// generation the file doesn't have, and the parameters that make it decode. +fn predicted_object_stream() -> (Vec, Dict) { + let members = b"3 0 << /Hidden (yes) >>"; + // PNG Sub, one row. + let mut rows = vec![1u8]; + let mut before = 0u8; + for b in members { + rows.push(b.wrapping_sub(before)); + before = *b; + } + let coded = pdfcraft_filters::encode_flate(&rows); + let mut out = b"%PDF-1.5\n".to_vec(); + let mut offs = [0usize; 7]; + let mut obj = |out: &mut Vec, n: usize, body: &[u8]| { + offs[n] = out.len(); + out.extend_from_slice(format!("{n} 0 obj\n").as_bytes()); + out.extend_from_slice(body); + out.extend_from_slice(b"\nendobj\n"); + }; + obj(&mut out, 1, b"<< /Type /Catalog /Pages 2 0 R >>"); + obj(&mut out, 2, b"<< /Type /Pages /Kids [] /Count 0 /Extra 3 0 R >>"); + let mut stm = format!("<< /Type /ObjStm /N 1 /First 4 /Filter /FlateDecode /DecodeParms 6 1 R /Length {} >>\nstream\n", coded.len()).into_bytes(); + stm.extend_from_slice(&coded); + stm.extend_from_slice(b"\nendstream"); + obj(&mut out, 4, &stm); + let data = [2u8, 0, 4, 0]; + let mut xs = format!("<< /Type /XRef /Size 7 /W [1 2 1] /Index [3 1] /Length {} >>\nstream\n", data.len()).into_bytes(); + xs.extend_from_slice(&data); + xs.extend_from_slice(b"\nendstream"); + obj(&mut out, 5, &xs); + obj(&mut out, 6, b"null"); + let table = out.len(); + out.extend_from_slice(b"xref\n0 7\n0000000000 65535 f \n"); + for (n, off) in offs.iter().enumerate().skip(1) { + let line = if n == 3 { "0000000000 65535 f \n".to_string() } else { format!("{off:010} 00000 n \n") }; + out.extend_from_slice(line.as_bytes()); + } + out.extend_from_slice(format!("trailer << /Size 7 /Root 1 0 R /XRefStm {} >>\nstartxref\n{table}\n%%EOF\n", offs[5]).as_bytes()); + let mut parms = Dict::new(); + parms.set(b"Predictor".to_vec(), Object::Int(11)); + parms.set(b"Columns".to_vec(), Object::Int(members.len() as i64)); + (out, parms) +} + +fn hidden(doc: &Document) -> Option> { + doc.get(ObjRef::new(3, 0)).as_dict().and_then(|d| d.get(b"Hidden")).and_then(Object::as_string).map(|s| s.bytes.clone()) +} + +/// An object stream whose `/DecodeParms` names a generation the file doesn't have decodes without +/// them; a clone that sets that generation decodes with them, not by the stream another clone read. +#[test] +fn parameters_an_edit_makes_match_hide_the_object_stream_read_before() { + let (file, parms) = predicted_object_stream(); + let doc = Document::open(Arc::new(file)).unwrap(); + assert_eq!(hidden(&doc), None, "no object 6 1: no predictor undone"); + let mut edited = doc.clone(); + edited.set(ObjRef::new(6, 1), Object::Dict(parms)); + assert_eq!(hidden(&edited), Some(b"yes".to_vec()), "another clone's stream, decoded without them"); +} + +/// The other way round: an object stream a clone decoded by its edited parameters stays that +/// clone's, so the document it was cloned from still decodes by the file's. +#[test] +fn an_object_stream_decoded_by_edited_parameters_is_not_shared() { + let (file, parms) = predicted_object_stream(); + let doc = Document::open(Arc::new(file)).unwrap(); + let mut edited = doc.clone(); + edited.set(ObjRef::new(6, 1), Object::Dict(parms)); + assert_eq!(hidden(&edited), Some(b"yes".to_vec())); + assert_eq!(hidden(&doc), None, "the clone's decoding, read from the shared streams"); +} + +/// What a load read is kept only up to a bound; past it the load counts as having read every +/// object, so a document with any edit reads it again. +#[test] +fn a_load_that_read_too_much_counts_as_reading_everything() { + use super::state::{ReadFrame, note_read}; + let mut overlay = BTreeMap::new(); + overlay.insert(999_999, Slot::Freed(1)); + let frame = ReadFrame::enter(); + for n in 0..8 { + note_read(n, None); + } + assert!(!frame.reads().touch(&overlay), "a few reads, none edited"); + assert!(!frame.reads().touch(&BTreeMap::new()), "nothing is edited"); + for n in 8..100 { + note_read(n, None); + } + assert!(frame.reads().touch(&overlay), "more objects than an entry keeps"); + drop(frame); + let outer = ReadFrame::enter(); + let inner = ReadFrame::enter(); + for n in 0..5_000 { + note_read(n % 4, None); + } + assert!(inner.reads().touch(&overlay), "more reads than the thread keeps"); + drop(inner); + assert!(outer.reads().touch(&overlay), "an outer frame missed them too"); +} + /// A member that failed to load from the file's object stream is kept as a failure, but replacing /// or freeing that stream forgets it: the member then reads from the edit. #[test] @@ -1500,7 +1673,7 @@ fn temporary_reader_keeps_file_views_and_evicts_data_of_its_own() { // cleared after the next read, including a failed one. let owned = Object::Stream(crate::Stream::from_raw(Dict::new(), vec![b'y'; ObjectReader::STREAM_BYTES + 1])); let size = held_bytes(&owned, &reader.document.data, 0); - reader.document.cache.lock().unwrap().insert(3, Ok(Arc::new(owned)), size); + reader.document.cache.lock().unwrap().insert(3, Ok(Arc::new(owned)), size, Default::default()); assert_eq!(*reader.get(ObjRef::new(99, 0)), Object::Null); assert!(reader.document.cache.lock().unwrap().is_empty(), "owned data over budget is evicted"); } @@ -2408,10 +2581,10 @@ fn the_object_cache_keeps_its_total_as_objects_come_and_go() { let two = Arc::new(Object::Array(vec![Object::Int(1); 10])); let (a, b) = (held_bytes(&one, &data, 0), held_bytes(&two, &data, 0)); let mut cache = super::state::ObjectCache::default(); - cache.insert(1, Ok(one.clone()), a); - cache.insert(2, Ok(two), b); + cache.insert(1, Ok(one.clone()), a, Default::default()); + cache.insert(2, Ok(two), b, Default::default()); assert_eq!(cache.owned(), a + b); - cache.insert(2, Ok(one), a); + cache.insert(2, Ok(one), a, Default::default()); assert_eq!(cache.owned(), a + a, "a replaced object no longer counts"); cache.retain(|n| *n == 1); assert_eq!((cache.len(), cache.owned()), (1, a)); diff --git a/crates/optimize/src/audit.rs b/crates/optimize/src/audit.rs index f5da17c45d..789bf2e89d 100644 --- a/crates/optimize/src/audit.rs +++ b/crates/optimize/src/audit.rs @@ -495,9 +495,10 @@ mod tests { assert_eq!(bytes_of(&after_self_ref, SpaceCategory::StructureInfo), struct_before); // Editing the object stream itself detaches its members from compressed stream sizing. + // The edit leaves the stream readable: one that broke `/First` would leave no members. edited .update_dict(ObjRef::new(stm_num, 0), |d| { - d.set(b"First".to_vec(), Object::Int(999_999)); + d.set(b"Edited".to_vec(), Object::Bool(true)); }) .unwrap(); let after_stream_edit = audit_space(&edited, 0);