From f266431f24342a7e06eec64a31f83165a7e7a9a9 Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Mon, 24 Aug 2020 21:14:07 +0530 Subject: [PATCH 1/2] Improve validation in b2b copies and RenderCommands --- wgpu-core/src/command/draw.rs | 4 ++++ wgpu-core/src/command/render.rs | 22 ++++++++++++++++++++++ wgpu-core/src/command/transfer.rs | 15 ++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/wgpu-core/src/command/draw.rs b/wgpu-core/src/command/draw.rs index 1682a141b..d5f13a2ff 100644 --- a/wgpu-core/src/command/draw.rs +++ b/wgpu-core/src/command/draw.rs @@ -71,6 +71,10 @@ pub enum RenderCommandError { MissingTextureUsage(#[from] MissingTextureUsageError), #[error(transparent)] PushConstants(#[from] PushConstantUploadError), + #[error("Invalid Viewport parameters")] + InvalidViewport, + #[error("Invalid ScissorRect parameters")] + InvalidScissorRect, } #[derive(Clone, Copy, Debug, Default)] diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 6dc994d99..83b67d58d 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -466,6 +466,9 @@ impl Global { } let mut output_attachments = AttachmentDataVec::::new(); + let mut attachment_width = std::u32::MAX; + let mut attachment_height = std::u32::MAX; + let context = { use hal::device::Device as _; @@ -568,6 +571,9 @@ impl Global { .map_err(|_| RenderPassError::InvalidAttachment(at.attachment))?; add_view(view)?; + attachment_width = attachment_width.min(view.extent.width); + attachment_height = attachment_height.min(view.extent.height); + let layouts = match view.inner { TextureViewInner::Native { ref source_id, .. } => { let previous_use = base_trackers @@ -1198,6 +1204,15 @@ impl Global { depth_max, } => { use std::{convert::TryFrom, i16}; + if rect.w <= 0.0 + || rect.h <= 0.0 + || depth_min < 0.0 + || depth_min > 1.0 + || depth_max < 0.0 + || depth_max > 1.0 + { + Err(RenderCommandError::InvalidViewport)? + } let r = hal::pso::Rect { x: i16::try_from(rect.x.round() as i64).unwrap_or(0), y: i16::try_from(rect.y.round() as i64).unwrap_or(0), @@ -1249,6 +1264,13 @@ impl Global { } RenderCommand::SetScissor(ref rect) => { use std::{convert::TryFrom, i16}; + if rect.w == 0 + || rect.h == 0 + || rect.x + rect.w > attachment_width + || rect.y + rect.h > attachment_height + { + Err(RenderCommandError::InvalidScissorRect)? + } let r = hal::pso::Rect { x: i16::try_from(rect.x).unwrap_or(0), y: i16::try_from(rect.y).unwrap_or(0), diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 0a778f2e7..3b315c629 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -46,6 +46,8 @@ pub enum TransferError { InvalidBuffer(BufferId), #[error("texture {0:?} is invalid")] InvalidTexture(TextureId), + #[error("Source and destination cannot be the same buffer")] + AttemptToCopyWithinBuffer, #[error("source buffer/texture is missing the `COPY_SRC` usage flag")] MissingCopySrcUsageFlag, #[error("destination buffer/texture is missing the `COPY_DST` usage flag")] @@ -302,6 +304,9 @@ impl Global { ) -> Result<(), CopyError> { span!(_guard, INFO, "CommandEncoder::copy_buffer_to_buffer"); + if source == destination { + Err(TransferError::AttemptToCopyWithinBuffer)? + } let hub = B::hub(self); let mut token = Token::root(); @@ -324,11 +329,6 @@ impl Global { None => (), } - if size == 0 { - tracing::trace!("Ignoring copy_buffer_to_buffer of size 0"); - return Ok(()); - } - let (src_buffer, src_pending) = cmd_buf .trackers .buffers @@ -378,6 +378,11 @@ impl Global { })? } + if size == 0 { + tracing::trace!("Ignoring copy_buffer_to_buffer of size 0"); + return Ok(()); + } + let region = hal::command::BufferCopy { src: source_offset, dst: destination_offset, From a10e5cdb53922380ad383b2a3f557fc4c5656beb Mon Sep 17 00:00:00 2001 From: Kunal Mohan Date: Mon, 24 Aug 2020 23:02:04 +0530 Subject: [PATCH 2/2] Check whether attachments are of same size or not --- wgpu-core/src/command/render.rs | 21 +++++++++++++++------ wgpu-core/src/command/transfer.rs | 4 ++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 83b67d58d..957737238 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -316,6 +316,8 @@ pub enum RenderPassError { Encoder(#[from] CommandEncoderError), #[error("attachment texture view {0:?} is invalid")] InvalidAttachment(id::TextureViewId), + #[error("attachments have different sizes")] + MismatchAttachments, #[error("attachment's sample count {0} is invalid")] InvalidSampleCount(u8), #[error("attachment with resolve target must be multi-sampled")] @@ -466,8 +468,9 @@ impl Global { } let mut output_attachments = AttachmentDataVec::::new(); - let mut attachment_width = std::u32::MAX; - let mut attachment_height = std::u32::MAX; + let mut attachment_width = None; + let mut attachment_height = None; + let mut valid_attachment = true; let context = { use hal::device::Device as _; @@ -571,8 +574,10 @@ impl Global { .map_err(|_| RenderPassError::InvalidAttachment(at.attachment))?; add_view(view)?; - attachment_width = attachment_width.min(view.extent.width); - attachment_height = attachment_height.min(view.extent.height); + valid_attachment &= *attachment_width.get_or_insert(view.extent.width) + == view.extent.width + && *attachment_height.get_or_insert(view.extent.height) + == view.extent.height; let layouts = match view.inner { TextureViewInner::Native { ref source_id, .. } => { @@ -629,6 +634,10 @@ impl Global { colors.push((color_at, hal::image::Layout::ColorAttachmentOptimal)); } + if !valid_attachment { + Err(RenderPassError::MismatchAttachments)? + } + for resolve_target in color_attachments.iter().flat_map(|at| at.resolve_target) { let view = trackers .views @@ -1266,8 +1275,8 @@ impl Global { use std::{convert::TryFrom, i16}; if rect.w == 0 || rect.h == 0 - || rect.x + rect.w > attachment_width - || rect.y + rect.h > attachment_height + || rect.x + rect.w > attachment_width.unwrap() + || rect.y + rect.h > attachment_height.unwrap() { Err(RenderCommandError::InvalidScissorRect)? } diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 3b315c629..661b8d21f 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -47,7 +47,7 @@ pub enum TransferError { #[error("texture {0:?} is invalid")] InvalidTexture(TextureId), #[error("Source and destination cannot be the same buffer")] - AttemptToCopyWithinBuffer, + SameSourceDestinationBuffer, #[error("source buffer/texture is missing the `COPY_SRC` usage flag")] MissingCopySrcUsageFlag, #[error("destination buffer/texture is missing the `COPY_DST` usage flag")] @@ -305,7 +305,7 @@ impl Global { span!(_guard, INFO, "CommandEncoder::copy_buffer_to_buffer"); if source == destination { - Err(TransferError::AttemptToCopyWithinBuffer)? + Err(TransferError::SameSourceDestinationBuffer)? } let hub = B::hub(self); let mut token = Token::root();