Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing validation when binding memory to buffers with the shader_device_address usage #2031

Merged
merged 8 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vulkano/src/buffer/cpu_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ where
/// # Panics
///
/// - Panics if `T` has zero size.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn from_data(
device: Arc<Device>,
usage: BufferUsage,
Expand Down Expand Up @@ -130,6 +132,8 @@ where
///
/// - Panics if `T` has zero size.
/// - Panics if `data` is empty.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn from_iter<I>(
device: Arc<Device>,
usage: BufferUsage,
Expand Down Expand Up @@ -172,6 +176,8 @@ where
///
/// - Panics if `T` has zero size.
/// - Panics if `len` is zero.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub unsafe fn uninitialized_array(
device: Arc<Device>,
len: DeviceSize,
Expand Down Expand Up @@ -201,6 +207,8 @@ where
/// # Panics
///
/// - Panics if `size` is zero.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub unsafe fn raw(
device: Arc<Device>,
size: DeviceSize,
Expand Down
3 changes: 3 additions & 0 deletions vulkano/src/buffer/cpu_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ where
/// # Panics
///
/// - Panics if `T` has zero size.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
#[inline]
pub fn new(device: Arc<Device>, usage: BufferUsage) -> CpuBufferPool<T> {
assert!(size_of::<T>() > 0);
assert!(!usage.shader_device_address);
let pool = device.standard_memory_pool();

CpuBufferPool {
Expand Down
15 changes: 15 additions & 0 deletions vulkano/src/buffer/device_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ where
/// the initial upload operation. In order to be allowed to use the `DeviceLocalBuffer`, you
/// must either submit your operation after this future, or execute this future and wait for it
/// to be finished before submitting your own operation.
///
/// # Panics
///
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn from_buffer<B, L, A>(
source: Arc<B>,
usage: BufferUsage,
Expand Down Expand Up @@ -235,6 +240,8 @@ where
/// # Panics
///
/// - Panics if `T` has zero size.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn from_data<L, A>(
data: T,
usage: BufferUsage,
Expand Down Expand Up @@ -264,6 +271,8 @@ where
///
/// - Panics if `T` has zero size.
/// - Panics if `data` is empty.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn from_iter<D, L, A>(
data: D,
usage: BufferUsage,
Expand Down Expand Up @@ -297,6 +306,8 @@ where
///
/// - Panics if `T` has zero size.
/// - Panics if `len` is zero.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub fn array(
device: Arc<Device>,
len: DeviceSize,
Expand Down Expand Up @@ -327,6 +338,8 @@ where
/// # Panics
///
/// - Panics if `size` is zero.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub unsafe fn raw(
device: Arc<Device>,
size: DeviceSize,
Expand Down Expand Up @@ -367,6 +380,8 @@ where
/// # Panics
///
/// - Panics if `size` is zero.
/// - Panics if `usage.shader_device_address` is `true`.
// TODO: ^
pub unsafe fn raw_with_exportable_fd(
device: Arc<Device>,
size: DeviceSize,
Expand Down
16 changes: 16 additions & 0 deletions vulkano/src/buffer/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ impl UnsafeBuffer {
}

/// Binds device memory to this buffer.
///
/// # Panics
///
/// - Panics if `self.usage.shader_device_address` is `true` and the `memory` was not allocated
/// with the [`device_address`] flag set and the [`ext_buffer_device_address`] extension is
/// not enabled on the device.
///
/// [`device_address`]: crate::memory::MemoryAllocateFlags::device_address
/// [`ext_buffer_device_address`]: crate::device::DeviceExtensions::ext_buffer_device_address
pub unsafe fn bind_memory(
&self,
memory: &DeviceMemory,
Expand Down Expand Up @@ -435,6 +444,13 @@ impl UnsafeBuffer {
}
}

// VUID-vkBindBufferMemory-bufferDeviceAddress-03339
if self.usage.shader_device_address
&& !self.device.enabled_extensions().ext_buffer_device_address
{
assert!(memory.flags().device_address);
}

(fns.v1_0.bind_buffer_memory)(
self.device.internal_object(),
self.handle,
Expand Down
16 changes: 10 additions & 6 deletions vulkano/src/buffer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// according to those terms.

use super::{sys::UnsafeBuffer, BufferContents, BufferSlice, BufferUsage};
use crate::{device::DeviceOwned, DeviceSize, RequiresOneOf, SafeDeref, VulkanObject};
use crate::{device::DeviceOwned, DeviceSize, RequiresOneOf, SafeDeref, Version, VulkanObject};
use std::{
error::Error,
fmt::{Debug, Display, Error as FmtError, Formatter},
Expand Down Expand Up @@ -100,13 +100,17 @@ pub unsafe trait BufferAccess: DeviceOwned + Send + Sync {
..Default::default()
};
let fns = device.fns();
let ptr = (fns.ext_buffer_device_address.get_buffer_device_address_ext)(
device.internal_object(),
&info,
);
let f = if device.api_version() >= Version::V1_2 {
fns.v1_2.get_buffer_device_address
} else if device.enabled_extensions().khr_buffer_device_address {
fns.khr_buffer_device_address.get_buffer_device_address_khr
} else {
fns.ext_buffer_device_address.get_buffer_device_address_ext
};
let ptr = f(device.internal_object(), &info);

if ptr == 0 {
panic!("got null ptr from a valid GetBufferDeviceAddressEXT call");
panic!("got null ptr from a valid GetBufferDeviceAddress call");
}

Ok(NonZeroU64::new_unchecked(ptr + inner.offset))
Expand Down
7 changes: 7 additions & 0 deletions vulkano/src/buffer/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ vulkan_bitflags! {
indirect_buffer = INDIRECT_BUFFER,

/// The buffer's device address can be retrieved.
///
/// A buffer created with this usage can only be bound to device memory allocated with the
/// [`device_address`] flag set unless the [`ext_buffer_device_address`] extension is enabled
/// on the device.
///
/// [`device_address`]: crate::memory::MemoryAllocateFlags::device_address
/// [`ext_buffer_device_address`]: crate::device::DeviceExtensions::ext_buffer_device_address
shader_device_address = SHADER_DEVICE_ADDRESS {
api_version: V1_2,
device_extensions: [khr_buffer_device_address, ext_buffer_device_address],
Expand Down
Loading