From dc013df446bb77cdca4e85c31c5e0eb4a3ad5c5f Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 19:47:53 -0800 Subject: [PATCH] :bug: Unmap only the pages the detach tests left mapped pages_after's case 3 unmaps the second of its two pages, and every caller then unmapped both pages again. Whatever the process had mapped into that freed page in between, such as a new thread's malloc cache or the GPU driver's command buffer shared memory, was unmapped under it, and the process's next touch of it faulted. On the M3 the child of pages_without_access_on_vulkan died of SIGSEGV at exit under GPU load (merge leg 10332), the faulting write lying in the second 16 KiB page of the freed range. The tests now let go through let_go_pages_after, which unmaps only the first page after case 3. metal2vulkan's measurements of the pattern: case 3's steps beside thread starts crashed 33 of 309 children, and unmapping once, 0 of 308; a probe with case 3's cleanup, 34 of 6253, and unmapping only the first page, 0 of 5792. --- .../reims-vgpu-vulkan/tests/detach_backing.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/reims-vgpu-vulkan/tests/detach_backing.rs b/crates/reims-vgpu-vulkan/tests/detach_backing.rs index a82b80163..5224c3f10 100644 --- a/crates/reims-vgpu-vulkan/tests/detach_backing.rs +++ b/crates/reims-vgpu-vulkan/tests/detach_backing.rs @@ -1342,6 +1342,16 @@ fn pages_after(case: u8) -> HostPtr { at } +/// Lets go of what [`pages_after`] left mapped of its two pages at `at` for `case`: both +/// pages, but only the first after case 3, whose second page it already unmapped. Unmapping +/// that second page again would unmap whatever the process has mapped there since, as a new +/// thread's malloc cache or the GPU driver's shared memory, and the process's next touch of it +/// faults. +fn let_go_pages_after(case: u8, at: HostPtr) { + let length = if case == 3 { PAGE } else { 2 * PAGE }; + let _ = VulkanVm::new().deallocate(at, length); +} + /// The heaps cases' options: Shared, and read-only (resource option bit 24) or not. fn heap_options(read_only: bool) -> Vec { let mut options = vec![OptionSet::StorageMode(0)]; @@ -1374,7 +1384,7 @@ fn without_access(p: &P, dev: &P::Device) -> Steps { }) .unwrap(); let made = u64::from(made.heap.is_some()); - let _ = vm.deallocate(at, 2 * PAGE); + let_go_pages_after(case, at); made }); steps.push(( @@ -1395,7 +1405,7 @@ fn without_access(p: &P, dev: &P::Device) -> Steps { .unwrap(); let replaced = u64::from(buffer.replace_backing(&array, read_only).unwrap()); drop(buffer); - let _ = vm.deallocate(at, 2 * PAGE); + let_go_pages_after(case, at); replaced }); steps.push(( @@ -1431,7 +1441,7 @@ fn without_access(p: &P, dev: &P::Device) -> Steps { .unwrap(); let replaced = u64::from(heap.replace_backing(&array, read_only).unwrap()); drop(heap); - let _ = vm.deallocate(at, 2 * PAGE); + let_go_pages_after(case, at); let _ = vm.deallocate(dummy, 2 * PAGE); replaced }); @@ -1478,7 +1488,7 @@ fn past_the_size(p: &P, dev: &P::Device) -> Steps { true => vec![range(at, 0, PAGE), range(at, PAGE, PAGE)], }; made.push(heap_over::

(p, dev, &ranges, read_only)); - let _ = vm.deallocate(at, 2 * PAGE); + let_go_pages_after(case, at); } } let at = pages(1, 0x5a);