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

Optimize memory layout checks and fix allocation soundness #2122

Merged
merged 4 commits into from
Jan 3, 2023

Conversation

marc0246
Copy link
Contributor

@marc0246 marc0246 commented Jan 3, 2023

This introduces two new types and a new type alias: DeviceLayout, DeviceAlignment and NonZeroDeviceSize. Previously, allocation sizes and alignments were verified in the functions that allocate, and in functions that construct buffers. All that is now gone as those checks have been moved to DeviceLayout, which is a replica of std's Layout, but for Vulkan. Another issue was that previously, no checks were made whether the allocation size would overflow when rounded up, which would lead to undefined behavior. DeviceLayout's size can not overflow i64::MAX, which eliminates that concern.

I've also marked some private functions unsafe and added more thourough safety documentation for them, and tried to document every possible case of arithmetic overflow as well. Hopefully this will make it easier to review the code and change it in the future.

### Breaking changes
- `MemoryRequirements::{size, alignment}` fields have been replaced with a single `layout` field.
- `Suballocator::allocate_unchecked` has been removed.

### Additions
- Added `DeviceLayout` and `DeviceAlignment`.
- Added the type alias `NonZeroDeviceSize`.

### Bugs fixed
- Fixed possible arithmetic overflows when allocating memory with a size that's too large.
- Added missing check for the allocation type when binding memory.

@Rua
Copy link
Contributor

Rua commented Jan 3, 2023

Very nice work!

@Rua Rua merged commit 82389ae into vulkano-rs:master Jan 3, 2023
@marc0246 marc0246 deleted the memory-layout branch January 3, 2023 17:50
Rua added a commit that referenced this pull request Jan 3, 2023
hakolao pushed a commit to hakolao/vulkano that referenced this pull request Feb 20, 2024
…s#2122)

* Add `DeviceLayout`, `DeviceAlignment` and `NonZeroDeviceSize`

* Replace sizes and alignments with `DeviceLayout`

* Add missing allocation type checks

* Add forgotten docs
hakolao pushed a commit to hakolao/vulkano that referenced this pull request Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants