Skip to content

Commit

Permalink
Assert that Vulkan array-getters return the same length (#534)
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 Jan 5, 2022
1 parent fdaafe7 commit 570b554
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions ash/src/device.rs
Expand Up @@ -328,6 +328,7 @@ impl Device {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count as usize, out.len());
}

#[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
Expand Up @@ -70,6 +70,7 @@ impl GetMemoryRequirements2 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count as usize, out.len());
}

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

/// 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 as usize, out.len());
}

pub fn name() -> &'static CStr {
Expand Down
2 changes: 1 addition & 1 deletion ash/src/extensions/khr/get_surface_capabilities2.rs
Expand Up @@ -66,7 +66,7 @@ impl GetSurfaceCapabilities2 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
assert_eq!(count as usize, out.len());
err_code.result()
}

Expand Down
2 changes: 1 addition & 1 deletion ash/src/extensions/khr/maintenance4.rs
Expand Up @@ -69,7 +69,7 @@ impl Maintenance4 {
&mut count,
out.as_mut_ptr(),
);
assert_eq!(count, out.len() as u32);
assert_eq!(count as usize, out.len());
}

pub fn name() -> &'static CStr {
Expand Down
18 changes: 11 additions & 7 deletions ash/src/instance.rs
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 as usize, out.len());
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 as usize, out.len());
}

#[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 as usize, out.len());
}

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

0 comments on commit 570b554

Please sign in to comment.