diff --git a/crates/metal2vulkan-validation/src/candidate.rs b/crates/metal2vulkan-validation/src/candidate.rs index 893bf1f3a..689fbc0e9 100644 --- a/crates/metal2vulkan-validation/src/candidate.rs +++ b/crates/metal2vulkan-validation/src/candidate.rs @@ -105,6 +105,9 @@ mod platform; any(target_os = "linux", target_os = "macos") ))] pub(crate) use platform::validated; +/// The replay stand-in's device keeps its loader word as the candidate's context does. +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub(crate) use platform::LoaderWord; #[cfg(test)] pub(crate) mod tests; diff --git a/crates/metal2vulkan-validation/src/candidate/platform.rs b/crates/metal2vulkan-validation/src/candidate/platform.rs index f97edf2e1..09650b015 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform.rs @@ -21,6 +21,7 @@ pub use acceptance::create_acceptance_pipeline; #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub use acceptance::opened_worker_device; pub use device::{capture_formats, echo_validation_messages, venue_check}; +pub(crate) use loader_word::LoaderWord; pub use profiles::{requested_restriction, restrict_devices_to}; pub use runtime::execute_runtime; #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] diff --git a/crates/metal2vulkan-validation/src/replay/new_runtime/standin.rs b/crates/metal2vulkan-validation/src/replay/new_runtime/standin.rs index 7d615c276..9eaa4357a 100644 --- a/crates/metal2vulkan-validation/src/replay/new_runtime/standin.rs +++ b/crates/metal2vulkan-validation/src/replay/new_runtime/standin.rs @@ -16,8 +16,15 @@ //! where the driver requires one; a dma-buf, into a memory type its fd's query lists, dedicated to //! the buffer. The device holds a handle of the runtime's device, so the instance it lives on //! outlives it, and frees its imports and itself before that handle goes. +//! +//! On Linux and macOS the device keeps its loader word as the candidate's context does +//! (`candidate`'s `LoaderWord`): recorded as `vkCreateDevice` returns it, and put back before each +//! call the loader serves itself (`vkGetDeviceQueue`, `vkAllocateCommandBuffers`, +//! `vkDestroyDevice`), which a driver that writes over it while reporting a message would fault. use super::videos::VideoHandle; +#[cfg(any(target_os = "linux", target_os = "macos"))] +use crate::candidate::LoaderWord; use crate::gpu::wait::{self, SubmissionBackend}; use ash::vk; use metal2vulkan_runtime as rt; @@ -51,6 +58,9 @@ struct Staging { /// The stand-in's device, queue and imports. pub(super) struct StandIn { device: ash::Device, + /// The device's loader word (the module's docs). + #[cfg(any(target_os = "linux", target_os = "macos"))] + word: LoaderWord, queue: vk::Queue, family: u32, pool: vk::CommandPool, @@ -84,6 +94,11 @@ impl StandIn { /// first family that copies, with the external-memory extensions of the host's handles that /// the physical device offers. Err where it offers no fd's (an NT handle's on Windows) or the /// device cannot be made. + #[expect( + clippy::disallowed_methods, + reason = "the stand-in's own device: its queue as vkCreateDevice returns it, its command \ + buffer and its destruction each after its loader word is put back" + )] pub(super) fn new(runtime: &rt::Device, backend: SubmissionBackend) -> Result { let raw = runtime.raw(); let (instance, physical) = (raw.instance, raw.physical_device); @@ -113,7 +128,7 @@ impl StandIn { return Err(format!("the device offers no {needed:?}")); } let names: Vec<*const std::ffi::c_char> = (std::iter::once(needed)) - .chain(optional.into_iter().filter(|name| offers(*name))) + .chain(optional.into_iter().filter(|name| offers(name))) .map(std::ffi::CStr::as_ptr) .collect(); let priorities = [1.0]; @@ -126,7 +141,16 @@ impl StandIn { // SAFETY: the extensions are the physical device's, and the family one of its families. let device = unsafe { instance.create_device(physical, &info, None) } .map_err(|error| format!("vkCreateDevice: {error}"))?; - // SAFETY: the device is new, and its one queue of `family` exists. + // SAFETY: the loader just returned the handle; the stand-in destroys it last. + #[cfg(any(target_os = "linux", target_os = "macos"))] + let word = unsafe { LoaderWord::of_device(device.handle()) }; + let word_back = || { + // SAFETY: the device is live, and only this thread calls the loader with it. + #[cfg(any(target_os = "linux", target_os = "macos"))] + let _ = unsafe { word.restore() }; + }; + // SAFETY: the device is new, and its one queue of `family` exists; nothing has reported + // on the device since its word was recorded. let queue = unsafe { device.get_device_queue(family, 0) }; let made = (|| { let pool_info = vk::CommandPoolCreateInfo::default() @@ -138,6 +162,7 @@ impl StandIn { .command_pool(pool) .level(vk::CommandBufferLevel::PRIMARY) .command_buffer_count(1); + word_back(); // SAFETY: the pool is the device's. let command = match unsafe { device.allocate_command_buffers(&allocate) } { Ok(buffers) => buffers[0], @@ -160,6 +185,7 @@ impl StandIn { let (pool, command, fence) = match made { Ok(made) => made, Err(error) => { + word_back(); // SAFETY: nothing of the device is in use. unsafe { device.destroy_device(None) }; return Err(format!("the stand-in's command pool or fence: {error}")); @@ -171,6 +197,8 @@ impl StandIn { #[cfg(unix)] fds: ash::khr::external_memory_fd::Device::new(instance, &device), device, + #[cfg(any(target_os = "linux", target_os = "macos"))] + word, queue, family, pool, @@ -527,6 +555,10 @@ impl StandIn { } impl Drop for StandIn { + #[expect( + clippy::disallowed_methods, + reason = "the stand-in's one vkDestroyDevice, after its loader word is put back" + )] fn drop(&mut self) { // Each write waited for its fence, so the queue is idle; the drain bounds the wait all the // same. A device whose drain fails is leaked rather than destroyed. @@ -545,6 +577,8 @@ impl Drop for StandIn { } self.device.destroy_fence(self.fence, None); self.device.destroy_command_pool(self.pool, None); + #[cfg(any(target_os = "linux", target_os = "macos"))] + let _ = self.word.restore(); self.device.destroy_device(None); } } diff --git a/crates/metal2vulkan-validation/src/replay/snippets.rs b/crates/metal2vulkan-validation/src/replay/snippets.rs index 13468a602..b76f2cbe5 100644 --- a/crates/metal2vulkan-validation/src/replay/snippets.rs +++ b/crates/metal2vulkan-validation/src/replay/snippets.rs @@ -123,11 +123,11 @@ fn read_back( } /// The exported-surface snippet (the module's docs), its surface of `handle`. Each frame `n` -/// (from 0) lands picture `n + 1` ([`video_picture`]), then commits command buffer `100 (n + 1) -/// + 1`, whose blit reads the luma plane through its view into a Shared buffer, cut by -/// `m2v.split` into a second part, whose blit reads the chroma plane into another; then a command -/// buffer whose blit fills a third Shared buffer, which uses none of the surface; then checks the -/// two buffers against the picture's bytes, and presents. +/// (from 0) lands picture `n + 1` ([`video_picture`]), then commits command buffer +/// `100 (n + 1) + 1`, whose blit reads the luma plane through its view into a Shared buffer, cut +/// by `m2v.split` into a second part, whose blit reads the chroma plane into another; then a +/// command buffer whose blit fills a third Shared buffer, which uses none of the surface; then +/// checks the two buffers against the picture's bytes, and presents. pub fn video_surfaces(w: &mut StreamWriter, handle: VideoHandle) -> io::Result<()> { w.call("MTLCreateSystemDefaultDevice", 0, DEVICE, &[], &[])?; w.call("newCommandQueue", DEVICE, QUEUE, &[], &[])?;