diff --git a/crates/media/src/exr_planes/tests.rs b/crates/media/src/exr_planes/tests.rs index a08dc87fc..c3a831250 100644 --- a/crates/media/src/exr_planes/tests.rs +++ b/crates/media/src/exr_planes/tests.rs @@ -1724,6 +1724,50 @@ fn a_plane_the_system_cannot_give_is_refused_not_allocated() { assert!(matches!(&over, ReadError::TooLarge(why) if why.contains("4096 bytes")), "{over:?}"); } +/// A source that counts the bytes read from it. +struct Counted { + inner: R, + read: usize, +} + +impl std::io::Read for Counted { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let n = self.inner.read(buf)?; + self.read += n; + Ok(n) + } +} + +impl std::io::Seek for Counted { + fn seek(&mut self, to: std::io::SeekFrom) -> std::io::Result { + self.inner.seek(to) + } +} + +/// #3749 review: one decompressed block may take 8 GiB at most, whatever memory the read may +/// use: a file whose block is larger (a data window a billion pixels wide over one large chunk, +/// so that the chunk could hold it) is refused for its size before anything is read, also with a +/// decode limit far above the block. +#[test] +fn a_block_over_the_fixed_limit_is_refused_whatever_the_decode_limit() { + let _only = LIMIT_TESTS.lock().unwrap_or_else(PoisonError::into_inner); + let f32s = [("R", false), ("G", false), ("B", false), ("A", false)]; + let small = write(vec![part(None, &f32s, (1, 1), Compression::ZIP1)], (1, 1)); + // One line of 2^30 - 2 pixels of four f32 channels: a block of 16 GiB... + let mut bytes = with_data_window_x_max(small, (1 << 30) - 3); + // ...over a chunk long enough to hold it at craft-exr's ratio (2^16 bytes a byte). + bytes.extend(std::iter::repeat_n(0u8, 300 << 10)); + let index = crate::exr_index::build(&mut std::io::Cursor::new(bytes.as_slice()), FileStamp::Virtual).expect("indexed: the chunk can hold the block"); + // The read may use 64 GiB (the floor is tiny, the system's figure high). + effectcraft_render::memory::set_decode_limit(Some(64 << 30)); + let request = ReadRequest { floor: 8, ..ReadRequest::full(vec![0, 1, 2, 3]) }; + let mut src = Counted { inner: std::io::Cursor::new(bytes.as_slice()), read: 0 }; + let (read, _) = crate::exr_read::read_planes(&mut src, &index, &request); + effectcraft_render::memory::set_decode_limit(None); + assert!(matches!(&read, Err(crate::exr_read::ReadError::TooLarge(why)) if why.contains("one block of this read may use")), "{read:?}"); + assert_eq!(src.read, 0, "refused before anything was read"); +} + /// Reads of `path` fail for lack of memory when `fails` says so for the channels read; the reads /// asked for are counted. fn no_memory_when(path: &str, fails: impl Fn(&[usize]) -> bool + Send + Sync + 'static) -> Arc { diff --git a/crates/media/src/exr_read.rs b/crates/media/src/exr_read.rs index 9a11dea3e..60175806e 100644 --- a/crates/media/src/exr_read.rs +++ b/crates/media/src/exr_read.rs @@ -244,6 +244,13 @@ pub(crate) fn read_error(e: seek::Error) -> ReadError { } } +/// The most bytes one decompressed block of a read may take, whatever the read's limit +/// ([`MAX_DECODE_BYTES`]: 8 GiB, 512 MiB in the browser): the decompressor allocates a block +/// whole before it looks at the data, and a block is one strip of the file, so a larger one is a +/// damaged or hostile header, not a file with more channels (#3749). A refusal for it doesn't +/// depend on the limit: a request asking at least as much, refused for it before, would be again. +pub(crate) const MAX_BLOCK_BYTES: usize = MAX_DECODE_BYTES; + /// The most threads one read decompresses on (beside the caller's), and the size of a file /// whose chunks are decompressed on the calling thread alone. const READ_WORKERS: usize = 4; @@ -293,7 +300,8 @@ fn read(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> Result<( let n = ow.checked_mul(oh).ok_or_else(|| ReadError::Unreadable("display window".into()))?; let need = n.saturating_mul(req.channels.len()).saturating_mul(std::mem::size_of::()); // (the limit is decided here and handed to `craft-exr` as the read's `max_bytes`: the planes - // it reserves, fallibly, and the largest block it decompresses follow it) + // it reserves, fallibly, follow it; one decompressed block stays at the fixed floor however + // much memory the machine has, see `MAX_BLOCK_BYTES`) let most = req.max_bytes.min(effectcraft_render::memory::decode_limit(req.floor, need)); if need > most { return Err(too_large(w, h, need, most)); @@ -309,6 +317,7 @@ fn read(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> Result<( request.fill = &fill; request.rows = req.rows.clone(); request.level = req.level; + request.max_block_bytes = Some(MAX_BLOCK_BYTES); // craft-exr decompresses on threads of its own, started for this read, beside the decode // pool the caller runs on: a few at most, and none for a small file (its chunks are // decompressed on the calling thread).