Skip to content

fix(core): Use more checked arithmetic - #9357

Merged
andyleiserson merged 2 commits into
gfx-rs:trunkfrom
andyleiserson:jj-push-klwn
Apr 1, 2026
Merged

fix(core): Use more checked arithmetic#9357
andyleiserson merged 2 commits into
gfx-rs:trunkfrom
andyleiserson:jj-push-klwn

Conversation

@andyleiserson

@andyleiserson andyleiserson commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Use checked arithmetic in more places.

Testing
In an ideal world there would be tests for this, but time is finite.

Squash or Rebase? Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

@ErichDonGubler ErichDonGubler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely an improvement, woot!

return Err(ComputePassErrorInner::IndirectBufferOverrun {
offset,
end_offset,
end_offset: offset + args_size,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: We should not be trying to compute an end offset if it's possibly bad. We can fix this as follow-up, though.

suggestion: Let's store the size instead of the end offset, like with other bounds checking errors that we've been changing recently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nitpick also applies to other diagnostics that have an end offset that may not be in bounds, e.g., BuildAccelerationStructureError::InsufficientBufferSize.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will include this with the next round of changes.

Comment on lines +1891 to +1909
#[derive(Clone, Debug, Error)]
pub enum InvalidWorkgroupSizeError {
#[error(
"Workgroup size {dimensions:?} ({total} total invocations) must be less or equal to \
the per-dimension limit `Limits::{per_dimension_limits_desc}` of {per_dimension_limits:?} \
and the total invocation limit `Limits::{total_limit_desc}` of {total_limit}"
)]
LimitExceeded {
dimensions: [u32; 3],
per_dimension_limits: [u32; 3],
per_dimension_limits_desc: &'static str,
total: u32,
total_limit: u32,
total_limit_desc: &'static str,
},
#[error("Workgroup sizes {dimensions:?} must be positive")]
Zero { dimensions: [u32; 3] },
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue(non-blocking): We should note this new API surface in a CHANGELOG entry.

Comment on lines -395 to +396
#[error(
"Shader entry point's workgroup size {dimensions:?} ({total} total invocations) must be \
less or equal to the per-dimension limit `Limits::{per_dimension_limits_desc}` of \
{per_dimension_limits:?} and the total invocation limit `Limits::{total_limit_desc}` of \
{total_limit}"
)]
InvalidWorkgroupSize {
dimensions: [u32; 3],
per_dimension_limits: [u32; 3],
per_dimension_limits_desc: &'static str,
total: u32,
total_limit: u32,
total_limit_desc: &'static str,
},
#[error(transparent)]
InvalidWorkgroupSize(#[from] InvalidWorkgroupSizeError),

@ErichDonGubler ErichDonGubler Apr 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This is technically a breaking change, and we should note that in a CHANGELOG entry. This could be follow-up, I suppose.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feedback also applies to DrawError::InvalidGroupSize.

Comment on lines -395 to +396
#[error(
"Shader entry point's workgroup size {dimensions:?} ({total} total invocations) must be \
less or equal to the per-dimension limit `Limits::{per_dimension_limits_desc}` of \
{per_dimension_limits:?} and the total invocation limit `Limits::{total_limit_desc}` of \
{total_limit}"
)]
InvalidWorkgroupSize {
dimensions: [u32; 3],
per_dimension_limits: [u32; 3],
per_dimension_limits_desc: &'static str,
total: u32,
total_limit: u32,
total_limit_desc: &'static str,
},
#[error(transparent)]
InvalidWorkgroupSize(#[from] InvalidWorkgroupSizeError),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feedback also applies to DrawError::InvalidGroupSize.

let vertex_limits = super::VertexLimits::new(state.vertex_buffer_sizes(), &pipeline.steps);

let stride = super::get_src_stride_of_indirect_args(family);
assert!(offset <= wgt::BufferAddress::MAX - stride);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Doesn't the user control offset here? Shouldn't this be a validation error instead (maybe as follow-up)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed unlikely enough to me that this would actually occur that it didn't seem worth introducing an error for it. This is multi_draw_indirect, so not standardized functionality, and I don't think we will even accept numbers from JavaScript beyond the range that consecutive integers can be represented exactly (2^52 or so).

@ErichDonGubler ErichDonGubler self-assigned this Apr 1, 2026
@ErichDonGubler ErichDonGubler added type: bug Something isn't working area: correctness We're behaving incorrectly labels Apr 1, 2026
@andyleiserson
andyleiserson merged commit 11db97c into gfx-rs:trunk Apr 1, 2026
59 checks passed
@andyleiserson
andyleiserson deleted the jj-push-klwn branch April 1, 2026 22:23
slyedoc pushed a commit to slyedoc/wgpu that referenced this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: correctness We're behaving incorrectly type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants