OpenEXR limit: one decompressed block stays at 8 GiB whatever memory a read may use (#3749 review)

The read's limit (the floor or the memory budget) bounds the planes it
allocates; one block the decompressor allocates whole is capped apart, at
the fixed floor (8 GiB, 512 MiB in the browser), with craft-exr's
max_block_bytes. A larger block is a damaged or hostile header, refused
for its size before anything is read. That refusal doesn't depend on the
limit, so remembering it for requests asking at least as much stays
right; after a limit change it is only decided once more.
This commit is contained in:
vangel76
2026-11-16 23:38:19 +01:00
parent c0c53353fe
commit ba2cb5f759
2 changed files with 54 additions and 1 deletions
+44
View File
@@ -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<R> {
inner: R,
read: usize,
}
impl<R: std::io::Read> std::io::Read for Counted<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
self.read += n;
Ok(n)
}
}
impl<R: std::io::Seek> std::io::Seek for Counted<R> {
fn seek(&mut self, to: std::io::SeekFrom) -> std::io::Result<u64> {
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<AtomicU64> {
+10 -1
View File
@@ -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::<f32>());
// (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).