Skip to content

Commit

Permalink
fix(core): validate bounds of timestamp write indices in compute passes
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Nov 21, 2024
1 parent be2d5d3 commit e35afc7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
- Replace potentially unsound usage of `PreHashedMap` with `FastHashMap`. By @jamienicol in [#6541](https://github.com/gfx-rs/wgpu/pull/6541).
- Add missing validation for timestamp writes in compute and render passes (by @ErichDonGubler in #????):
- Check the status of the `TIMESTAMP_QUERY` feature before other validation.
- Check that indices are in-bounds for the query set.

#### Naga

Expand Down
14 changes: 12 additions & 2 deletions wgpu-core/src/command/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ impl Global {
arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
let &PassTimestampWrites {
query_set,
beginning_of_pass_write_index: _,
end_of_pass_write_index: _,
beginning_of_pass_write_index,
end_of_pass_write_index,
} = tw;

match cmd_buf
Expand All @@ -335,6 +335,16 @@ impl Global {
Err(e) => return make_err(e.into(), arc_desc),
}

for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
.into_iter()
.flatten()
{
match query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None) {
Ok(()) => (),
Err(e) => return make_err(e.into(), arc_desc),
}
}

Some(ArcPassTimestampWrites {
query_set,
beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ pub enum CommandEncoderError {
InvalidResource(#[from] InvalidResourceError),
#[error(transparent)]
MissingFeatures(#[from] MissingFeatures),
#[error(transparent)]
TimestampWritesInvalid(#[from] QueryUseError),
}

impl Global {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum ResolveError {
}

impl QuerySet {
fn validate_query(
pub(crate) fn validate_query(
self: &Arc<Self>,
query_type: SimplifiedQueryType,
query_index: u32,
Expand Down
12 changes: 10 additions & 2 deletions wgpu-core/src/command/render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::binding_model::BindGroup;
use crate::command::{
validate_and_begin_occlusion_query, validate_and_begin_pipeline_statistics_query,
SimplifiedQueryType,
};
use crate::init_tracker::BufferInitTrackerAction;
use crate::pipeline::RenderPipeline;
Expand Down Expand Up @@ -1394,8 +1395,8 @@ impl Global {
arc_desc.timestamp_writes = if let Some(tw) = desc.timestamp_writes {
let &PassTimestampWrites {
query_set,
beginning_of_pass_write_index: _,
end_of_pass_write_index: _,
beginning_of_pass_write_index,
end_of_pass_write_index,
} = tw;

let query_set = query_sets.get(query_set).get()?;
Expand All @@ -1404,6 +1405,13 @@ impl Global {

query_set.same_device(device)?;

for idx in [beginning_of_pass_write_index, end_of_pass_write_index]
.into_iter()
.flatten()
{
query_set.validate_query(SimplifiedQueryType::Timestamp, idx, None)?;
}

Some(ArcPassTimestampWrites {
query_set,
beginning_of_pass_write_index: tw.beginning_of_pass_write_index,
Expand Down

0 comments on commit e35afc7

Please sign in to comment.