diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 06b35f8208..de03bbbb63 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -114,7 +114,8 @@ pub struct LoadOptions { pub max_manifest_bytes: usize, /// Maximum size of one decompressed blob. pub max_blob_bytes: usize, - /// Maximum total decompressed tile + blob bytes. + /// Maximum total decompressed tile + blob bytes. The default follows the machine + /// ([`total_budget`]). pub max_total_bytes: u64, /// Keep document and layer ids from the file (the id counter is advanced /// past them). When `false`, fresh ids are assigned. @@ -148,6 +149,24 @@ pub const BROWSER_MAX_BLOB_BYTES: usize = 512 << 20; #[cfg(target_arch = "wasm32")] pub const BROWSER_MAX_TOTAL_BYTES: u64 = 1 << 30; +/// The least [`total_budget`] gives a machine whose memory is known, so a load isn't refused +/// because RAM is briefly short. +pub const MIN_TOTAL_BUDGET: u64 = 1 << 30; + +/// The default [`LoadOptions::max_total_bytes`] (#8595): `cap` (the format's own limit), lowered to +/// what this machine can hold, the available RAM plus the room left on the scratch disk, but not +/// below [`MIN_TOTAL_BUDGET`]. Decoded tiles start in RAM and spill to the scratch disk as memory +/// fills, so both count. A bundle names each tile's decoded size, so a small file of nearly empty, +/// highly compressed tiles can ask for far more than the machine has; past this budget the load +/// stops with an error before decoding more, instead of the process running out of memory. With +/// neither figure known (the browser, an unknown platform), `cap` stands. +pub fn total_budget(cap: u64, available_ram: Option, scratch_room: Option) -> u64 { + match (available_ram, scratch_room) { + (None, None) => cap, + (ram, scratch) => ram.unwrap_or(0).saturating_add(scratch.unwrap_or(0)).max(MIN_TOTAL_BUDGET).min(cap), + } +} + impl Default for LoadOptions { fn default() -> Self { #[cfg_attr(not(target_arch = "wasm32"), allow(unused_mut))] @@ -157,10 +176,11 @@ impl Default for LoadOptions { limits.max_blob_bytes = limits.max_blob_bytes.min(BROWSER_MAX_BLOB_BYTES); limits.max_total_bytes = limits.max_total_bytes.min(BROWSER_MAX_TOTAL_BYTES); } + let ram = photocraft_raster::memory::system::current().available_ram; LoadOptions { max_manifest_bytes: limits.max_manifest_bytes, max_blob_bytes: limits.max_blob_bytes, - max_total_bytes: limits.max_total_bytes, + max_total_bytes: total_budget(limits.max_total_bytes, ram, photocraft_raster::spill::scratch_capacity()), preserve_ids: true, max_canvas_side: MAX_CANVAS_SIDE, max_canvas_pixels: MAX_CANVAS_PIXELS, diff --git a/crates/format/tests/it/corruption.rs b/crates/format/tests/it/corruption.rs index 1a69a60055..35d8ac5799 100644 --- a/crates/format/tests/it/corruption.rs +++ b/crates/format/tests/it/corruption.rs @@ -523,3 +523,22 @@ fn declared_canvas_outside_the_budget_is_refused() { let opts = LoadOptions { max_canvas_side: 65_536, ..Default::default() }; assert!(matches!(load_from_bytes_with(&with_size(65_537, 1), &opts), Err(FormatError::LimitExceeded(_)))); } + +/// #8595: the default decode budget follows the machine: the format's cap, lowered to the +/// available RAM plus the scratch disk's room, never below 1 GiB. A bundle that expands past the +/// budget a machine gets stops with an error instead of running it out of memory. +#[test] +fn the_default_decode_budget_follows_the_machines_memory() { + let cap = 16 << 30; + assert_eq!(total_budget(cap, None, None), cap, "nothing known: the format's cap"); + assert_eq!(total_budget(cap, Some(4 << 30), None), 4 << 30); + assert_eq!(total_budget(cap, Some(4 << 30), Some(8 << 30)), 12 << 30, "spilled tiles count too"); + assert_eq!(total_budget(cap, Some(64 << 30), None), cap); + assert_eq!(total_budget(cap, Some(0), None), MIN_TOTAL_BUDGET, "a brief shortage doesn't refuse every load"); + assert_eq!(total_budget(1 << 20, Some(4 << 30), None), 1 << 20, "never above the cap"); + // The budget a machine gets is enforced: a bundle that expands past it is refused. + let b = sample(); + assert!(load_from_bytes(&b).is_ok()); + let opts = LoadOptions { max_total_bytes: total_budget(1000, Some(4 << 30), Some(4 << 30)), ..Default::default() }; + assert!(matches!(load_from_bytes_with(&b, &opts), Err(FormatError::LimitExceeded(_)))); +}