From efa4f29248601fd873bc9a11d7a850cd5d672132 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Tue, 19 Apr 2022 12:43:10 -0400 Subject: [PATCH] Implement validation canary --- wgpu-hal/src/dx12/instance.rs | 4 ++-- wgpu-hal/src/gles/egl.rs | 3 ++- wgpu-hal/src/lib.rs | 34 +++++++++++++++++++++++++++++++++ wgpu-hal/src/vulkan/instance.rs | 5 +++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/wgpu-hal/src/dx12/instance.rs b/wgpu-hal/src/dx12/instance.rs index aa9edbb2e..286f3e177 100644 --- a/wgpu-hal/src/dx12/instance.rs +++ b/wgpu-hal/src/dx12/instance.rs @@ -63,8 +63,8 @@ unsafe extern "system" fn output_debug_string_handler( }); if cfg!(debug_assertions) && level == log::Level::Error { - // Panicking behind FFI is UB, so we just exit. - std::process::exit(1); + // Set canary and continue + crate::VALIDATION_CANARY.set(); } excpt::EXCEPTION_CONTINUE_EXECUTION diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index d15c58805..5092205a2 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -259,7 +259,8 @@ fn gl_debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, m }); if cfg!(debug_assertions) && log_severity == log::Level::Error { - std::process::exit(1); + // Set canary and continue + crate::VALIDATION_CANARY.set(); } } diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 4b292a3b8..5d015ddfc 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -84,6 +84,7 @@ use std::{ num::{NonZeroU32, NonZeroU8}, ops::{Range, RangeInclusive}, ptr::NonNull, + sync::atomic::AtomicBool, }; use bitflags::bitflags; @@ -1143,6 +1144,39 @@ pub struct ComputePassDescriptor<'a> { pub label: Label<'a>, } +/// Stores if any API validation error has occurred in this process +/// since it was last reset. +/// +/// This is used for internal wgpu testing only and _must not_ be used +/// as a way to check for errors. +/// +/// This works as a static because `cargo nextest` runs all of our +/// tests in separate processes, so each test gets its own canary. +/// +/// This prevents the issue of one validation error terminating the +/// entire process. +pub static VALIDATION_CANARY: ValidationCanary = ValidationCanary { + inner: AtomicBool::new(false), +}; + +/// Flag for internal testing. +pub struct ValidationCanary { + inner: AtomicBool, +} + +impl ValidationCanary { + #[allow(dead_code)] // in some configurations this function is dead + fn set(&self) { + self.inner.store(true, std::sync::atomic::Ordering::SeqCst); + } + + /// Returns true if any API validation error has occurred in this process + /// since the last call to this function. + pub fn get_and_reset(&self) -> bool { + self.inner.swap(false, std::sync::atomic::Ordering::SeqCst) + } +} + #[test] fn test_default_limits() { let limits = wgt::Limits::default(); diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 05c05a92b..494711750 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -112,6 +112,11 @@ unsafe extern "system" fn debug_utils_messenger_callback( }); } + if cfg!(debug_assertions) && level == log::Level::Error { + // Set canary and continue + crate::VALIDATION_CANARY.set(); + } + vk::FALSE }