Skip to content

Commit

Permalink
Assert that Vulkan array-getters return the same length
Browse files Browse the repository at this point in the history
Originally introduced in [#489] this inserts the array-length equality
check everywhere else: in the supposedly invalid and inexistant event
where Vulkan suddenly returns less items (`count` has been modified)
than were originally queried through respective `_len()` functions (or
more likely: a slice of invalid length passed by the user) and some
elements at the end of the slice are left uninitialized, panic.

Wherever there is valid concern or possibility for this to happen - or
to circumvent assertions and panics altogether - mutable references to
mutable slices should be passed allowing the length to be promptly
updated.

[#489]: #489 (comment)
  • Loading branch information
MarijnS95 committed Dec 27, 2021
1 parent ab36e84 commit 307563d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions ash/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ impl Device {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkTrimCommandPool.html>"]
Expand Down
1 change: 1 addition & 0 deletions ash/src/extensions/khr/get_memory_requirements2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl GetMemoryRequirements2 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

pub fn name() -> &'static CStr {
Expand Down
2 changes: 2 additions & 0 deletions ash/src/extensions/khr/get_physical_device_properties2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl GetPhysicalDeviceProperties2 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

/// Retrieve the number of elements to pass to [`Self::get_physical_device_sparse_image_format_properties2()`]
Expand Down Expand Up @@ -144,6 +145,7 @@ impl GetPhysicalDeviceProperties2 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

pub fn name() -> &'static CStr {
Expand Down
18 changes: 11 additions & 7 deletions ash/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ impl Instance {
&self,
out: &mut [vk::PhysicalDeviceGroupProperties],
) -> VkResult<()> {
let mut group_count = out.len() as u32;
let mut count = out.len() as u32;
self.instance_fn_1_1
.enumerate_physical_device_groups(self.handle(), &mut group_count, out.as_mut_ptr())
.result()
.enumerate_physical_device_groups(self.handle(), &mut count, out.as_mut_ptr())
.result()?;
assert_eq!(count, out.len() as u32);
Ok(())
}

#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceFeatures2.html>"]
Expand Down Expand Up @@ -144,13 +146,14 @@ impl Instance {
physical_device: vk::PhysicalDevice,
out: &mut [vk::QueueFamilyProperties2],
) {
let mut queue_count = out.len() as u32;
let mut count = out.len() as u32;
self.instance_fn_1_1
.get_physical_device_queue_family_properties2(
physical_device,
&mut queue_count,
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceMemoryProperties2.html>"]
Expand Down Expand Up @@ -190,14 +193,15 @@ impl Instance {
format_info: &vk::PhysicalDeviceSparseImageFormatInfo2,
out: &mut [vk::SparseImageFormatProperties2],
) {
let mut format_count = out.len() as u32;
let mut count = out.len() as u32;
self.instance_fn_1_1
.get_physical_device_sparse_image_format_properties2(
physical_device,
format_info,
&mut format_count,
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
}

#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceExternalBufferProperties.html>"]
Expand Down

0 comments on commit 307563d

Please sign in to comment.