craft-gpu-yuv: an input larger than the largest buffer is refused before it is read

The provider checked the read-back frame against max_buffer_size but not
the RGBA8 input. A job whose input exceeds the limit is now
GpuFault::InvalidJob before the context reads it, on every context. The
BufferLimit message now names any buffer, not only the read-back one.
This commit is contained in:
dotCooCoo committed 2026-11-23 23:54:49 -08:00
1 parent f3e24c3846
commit dfeb20ddcf
4 files changed
+27 -7

No files matched your search

+2 -2
View File
@@ -47,8 +47,8 @@ are decimal digits.
The provider checks the job against the context's `DeviceCaps` on every context, the null one
included: each source and output side must be at most `max_texture_dimension_2d`, and the
read-back layout with its padded rows (`FrameLayout::buffer_size`) at most `max_buffer_size`
bytes. It checks the input's length before reading it. A
read-back layout with its padded rows (`FrameLayout::buffer_size`) and the RGBA8 input at most
`max_buffer_size` bytes. It checks the input's length before reading it. A
malformed, missing, repeated or unknown parameter, a wrong input count or length, and a size the
caps or the conversion do not allow are `GpuFault::InvalidJob`. With a device, the provider also
checks the device's own limits, and an error the device reports during the job is
+7 -3
View File
@@ -4,9 +4,9 @@
//! A job has one input, an RGBA8 buffer of `src_width × src_height × 4` bytes in the context, and
//! [`YuvJob::params`] as its parameters. Its outcome has one output: a new buffer that holds the
//! packed frame in [`FrameLayout`] order. Every side must be no larger than the context's
//! [`DeviceCaps::max_texture_dimension_2d`](craft_gpu::DeviceCaps), and the frame no larger than its
//! `max_buffer_size`, on every context, so that a job that runs on the null context also runs on a
//! device with the same caps.
//! [`DeviceCaps::max_texture_dimension_2d`](craft_gpu::DeviceCaps), and the input and the frame no
//! larger than its `max_buffer_size`, on every context, so that a job that runs on the null context
//! also runs on a device with the same caps.
use craft_gpu::{GpuContext, GpuFault, GpuJob, GpuJobOutcome, GpuJobParams, GpuProvider, HandleId};
@@ -224,6 +224,10 @@ impl GpuProvider for YuvProvider {
if src_width == 0 || src_height == 0 || expected != Some(bytes) {
return Err(fault(Error::Source { width: src_width, height: src_height, bytes }));
}
let input_bytes = bytes as u64;
if input_bytes > caps.max_buffer_size {
return Err(fault(Error::BufferLimit { needed: input_bytes, limit: caps.max_buffer_size }));
}
let rgba = ctx.read_buffer(*input)?;
let frame = self.convert(&rgba, &spec).map_err(fault)?;
let output = ctx.upload_buffer("craft.yuv.frame", &frame)?;
+15
View File
@@ -186,6 +186,21 @@ fn jobs_past_the_context_caps_are_refused() {
assert!(dispatch(&r, &mut ctx, &YuvJob::new(c, 64, 4, 64, 42).job(small)).is_ok());
}
#[test]
fn an_input_past_the_buffer_limit_is_refused_before_it_is_read() {
let r = registry(YuvProvider::new());
let caps = DeviceCaps { max_texture_dimension_2d: 64, max_buffer_size: 16128, ..DeviceCaps::default() };
let mut ctx = Capped::new(caps);
let c = Conversion::new(Format::Nv12, Matrix::Bt709, Range::Limited);
// 64 × 64 × 4 = 16384 bytes, more than the 16128-byte limit; 64 × 63 × 4 is exactly 16128.
let large = ctx.upload_buffer("large", &tests::noise(64, 64)).unwrap();
let at_limit = ctx.upload_buffer("at limit", &tests::noise(64, 63)).unwrap();
let refused = invalid(r.dispatch(&mut ctx, &YuvJob::new(c, 64, 64, 64, 4).job(large)));
assert!(refused.contains("16384 bytes is larger than the largest buffer, 16128"), "{refused}");
assert_eq!(ctx.reads, 0, "the refused input is not read");
assert!(dispatch(&r, &mut ctx, &YuvJob::new(c, 64, 63, 64, 4).job(at_limit)).is_ok());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_provider_with_a_device_matches_the_cpu_through_the_registry() {
+3 -2
View File
@@ -151,7 +151,8 @@ pub enum Error {
TooLarge { width: u32, height: u32, limit: u32 },
/// The frame's byte sizes do not fit in the integer types that hold them.
Overflow { width: u32, height: u32 },
/// A read-back buffer for the frame would be larger than the device's largest buffer.
/// A buffer the job uses (the RGBA8 input, or the read-back buffer for the frame) would be
/// larger than the device's largest buffer.
BufferLimit { needed: u64, limit: u64 },
/// A source side is zero, or the pixels are not `width × height × 4` bytes.
Source { width: u32, height: u32, bytes: usize },
@@ -177,7 +178,7 @@ impl fmt::Display for Error {
Error::OutputSize { width, height } => write!(f, "a 4:2:0 picture of {width}×{height} needs both sides even and not zero"),
Error::TooLarge { width, height, limit } => write!(f, "{width}×{height} is larger than the largest texture side, {limit}"),
Error::Overflow { width, height } => write!(f, "a frame of {width}×{height} is too large to hold"),
Error::BufferLimit { needed, limit } => write!(f, "a read-back buffer of {needed} bytes is larger than the largest buffer, {limit}"),
Error::BufferLimit { needed, limit } => write!(f, "a buffer of {needed} bytes is larger than the largest buffer, {limit}"),
Error::Source { width, height, bytes } => write!(f, "{bytes} bytes are not an RGBA picture of {width}×{height}"),
Error::BufferSize { needed, actual } => write!(f, "{actual} bytes where {needed} are needed"),
Error::Offset(offset) => write!(f, "buffer offset {offset} is not a multiple of {}", wgpu::COPY_BYTES_PER_ROW_ALIGNMENT),