diff --git a/.cargo/config.toml b/.cargo/config.toml index b97153d60..608e3de54 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,7 @@ [alias] xtask = "run -p wgpu-xtask --" + +[build] +# Note that any rustflags set here will be overridden by the `RUSTFLAGS` +# environment variable, if set (e.g. in CI). +rustflags = ["--cfg", "wgpu_validate_locks_debug"] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f10f3195d..f41bd0651 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,7 +34,7 @@ env: RUST_LOG: debug,wasm_bindgen_wasm_interpreter=warn,wasm_bindgen_cli_support=warn,walrus=warn,naga=info RUST_BACKTRACE: full PKG_CONFIG_ALLOW_CROSS: 1 # allow android to work - RUSTFLAGS: -D warnings + RUSTFLAGS: -D warnings --cfg wgpu_validate_locks_debug RUSTDOCFLAGS: -D warnings WASM_BINDGEN_TEST_TIMEOUT: 300 # 5 minutes CACHE_SUFFIX: e # cache busting diff --git a/.github/workflows/cts.yml b/.github/workflows/cts.yml index 5d8c541fb..c565957b3 100644 --- a/.github/workflows/cts.yml +++ b/.github/workflows/cts.yml @@ -16,7 +16,7 @@ env: CARGO_INCREMENTAL: false CARGO_TERM_COLOR: always RUST_BACKTRACE: full - RUSTFLAGS: -D warnings + RUSTFLAGS: -D warnings --cfg wgpu_validate_locks_debug RUSTDOCFLAGS: -D warnings # diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fa96de28..e06e76cd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -184,12 +184,11 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Zero-initialize padding (if any) at the end of a buffer allocation. This was application-visible in rare cases on Vulkan when a shader read beyond the valid range of a vertex buffer. By @andyleiserson in [#9791](https://github.com/gfx-rs/wgpu/pull/9791). - Fix required immediate slots calculation and remove `naga::valid::FunctionInfo::immediate_slots_used`. By @beicause in [#9725](https://github.com/gfx-rs/wgpu/pull/9725). - Fix a spurious assertion failure in `Device::maintain` when multiple threads race polling the same device. By @AdrianEddy in [#9958](https://github.com/gfx-rs/wgpu/pull/9958). -- Fix `PendingSubmission` releasing its lock guards out of stacking order, which tripped `--cfg wgpu_validate_locks` on any submission. By @AdrianEddy in [#9960](https://github.com/gfx-rs/wgpu/pull/9960). - Fix `Buffer::unmap` failing with `NotMapped` when it races a `Buffer::map` running on another thread. By @AdrianEddy in [#9959](https://github.com/gfx-rs/wgpu/pull/9959). - Fix separate depth/stencil read-only state and `SYNC-HAZARD-WRITE-AFTER-WRITE` Vulkan validation error. By @beicause in [#9763](https://github.com/gfx-rs/wgpu/pull/9763). - In `Queue::submit`, validate all command buffers before making any updates to the init trackers. This is not expected to affect performance or behavior when handling successful submission. By @andyleiserson in [#10003](https://github.com/gfx-rs/wgpu/pull/10003). - Fix initialization tracking for some cases of array textures, 3d textures, and depth/stencil textures with divergent usage in a render pass. By @andyleiserson in [#10002](https://github.com/gfx-rs/wgpu/pull/10002) and [#10060](https://github.com/gfx-rs/wgpu/pull/10060). -- Fixed a deadlock between `Queue::compact_blas` and `Queue::submit`, which acquired `Device::command_indices` and `Queue::pending_writes` in opposite orders. By @mstampfli in [#10118](https://github.com/gfx-rs/wgpu/pull/10118). +- Fix various deadlocks due to inconsistent ordering of lock acquisitions. `wgpu` is now tested with lock rank checking (`--cfg wgpu_validate_locks`) in CI, which should prevent many deadlocks in the future. By @AdrianEddy in [#9960](https://github.com/gfx-rs/wgpu/pull/9960), @mstampfli in [#10118](https://github.com/gfx-rs/wgpu/pull/10118), and @andyleiserson in [#9728](https://github.com/gfx-rs/wgpu/pull/9728) and [#9479](https://github.com/gfx-rs/wgpu/pull/9479). - Fixed some cases of passing object labels to platform APIs despite `InstanceFlags::DISCARD_HAL_LABELS` being set. By @andyleiserson in [#10121](https://github.com/gfx-rs/wgpu/pull/10121) and [#10123](https://github.com/gfx-rs/wgpu/pull/10123). - Clean up resources properly when `Device::new` fails, to avoid a leak or panic. By @andyleiserson in [#10160](https://github.com/gfx-rs/wgpu/pull/10160). - Fix pending buffer mappings incorrectly succeeding when the device is lost before they are processed. By @jinleili in [#10301](https://github.com/gfx-rs/wgpu/pull/10301). diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index c315d077b..cf502d7fe 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -30,7 +30,10 @@ ignored = ["cfg_aliases"] [lints.rust] missing_debug_implementations = "warn" -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(wgpu_validate_locks)'] } +unexpected_cfgs = { level = "warn", check-cfg = [ + 'cfg(wgpu_validate_locks)', + 'cfg(wgpu_validate_locks_debug)', +] } [lib] diff --git a/wgpu-core/build.rs b/wgpu-core/build.rs index 355a8a806..baef29356 100644 --- a/wgpu-core/build.rs +++ b/wgpu-core/build.rs @@ -25,5 +25,15 @@ fn main() { any(target_os = "linux", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd") ) }, metal: { all(target_vendor = "apple", feature = "metal") }, + + // `wgpu_validate_locks` can also be set directly, if desired. + // See `wgpu-core/src/lock/mod.rs`. + wgpu_validate_locks: { + all( + debug_assertions, + wgpu_validate_locks_debug, + not(target_family = "wasm") + ) + } } } diff --git a/wgpu-core/src/lock/mod.rs b/wgpu-core/src/lock/mod.rs index dac314f35..79c3e75a8 100644 --- a/wgpu-core/src/lock/mod.rs +++ b/wgpu-core/src/lock/mod.rs @@ -17,10 +17,14 @@ //! uninstrumented, no-overhead wrappers around the standard lock //! types. //! -//! If the `wgpu_validate_locks` config is set (for example, with -//! `RUSTFLAGS='--cfg wgpu_validate_locks'`), `wgpu-core` uses the -//! [`ranked`] module's locks. We hope to make this the default for -//! debug builds soon. +//! If the `wgpu_validate_locks` config is set (either directly, i.e. with +//! `RUSTFLAGS='--cfg wgpu_validate_locks'`, or via the +//! `wgpu_validate_locks_debug` `cfg` alias in `build.rs` that conditionally +//! enables it when debug assertions are also enabled), `wgpu-core` uses the +//! [`ranked`] module's locks. `.config/cargo.toml` in the `wgpu` tree specifies +//! `wgpu_validate_locks_debug`, so lock validation is active by default in +//! `wgpu` CI and `wgpu` development trees, but not in `wgpu`-using applications +//! unless specifically requested. //! //! If the `observe_locks` feature is enabled, `wgpu-core` uses the //! [`observing`] module's locks. @@ -38,6 +42,7 @@ pub mod rank; mod ranked; #[cfg(feature = "observe_locks")] +#[cfg_attr(wgpu_validate_locks, allow(dead_code))] mod observing; #[cfg_attr(any(wgpu_validate_locks, feature = "observe_locks"), allow(dead_code))] @@ -46,7 +51,7 @@ mod vanilla; #[cfg(wgpu_validate_locks)] use ranked as chosen; -#[cfg(feature = "observe_locks")] +#[cfg(all(not(wgpu_validate_locks), feature = "observe_locks"))] use observing as chosen; #[cfg(not(any(wgpu_validate_locks, feature = "observe_locks")))] diff --git a/wgpu-core/src/lock/ranked.rs b/wgpu-core/src/lock/ranked.rs index b59a1de3a..f734bcc91 100644 --- a/wgpu-core/src/lock/ranked.rs +++ b/wgpu-core/src/lock/ranked.rs @@ -289,6 +289,7 @@ pub struct RwLockReadGuard<'a, T> { /// For details, see [the module documentation][self]. pub struct RwLockWriteGuard<'a, T> { inner: wgpu_sync::RwLockWriteGuard<'a, T>, + #[cfg_attr(not(miri), expect(unused))] // but `Drop` has important side effects saved: LockStateGuard, }