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

alloc debug overflow on size 0 #159

Open
m4b opened this issue Jun 8, 2019 · 11 comments
Open

alloc debug overflow on size 0 #159

m4b opened this issue Jun 8, 2019 · 11 comments

Comments

@m4b
Copy link

m4b commented Jun 8, 2019

if the size is 0, this will abort on a sub overflow in debug mode.

    fn alloc(
        &mut self,
        device: &B::Device,
        size: u64,
        align: u64,
    ) -> Result<(DynamicBlock<B>, u64), gfx_hal::device::AllocationError> {
        debug_assert!(size <= self.max_allocation());
        debug_assert!(align.is_power_of_two());
        let aligned_size = ((size - 1) | (align - 1) | (self.block_size_granularity - 1)) + 1;
@zakarumych
Copy link
Member

Size and align 0 are not supported. Should add this to asserts as nd doc.

@m4b
Copy link
Author

m4b commented Jun 11, 2019

please don't assert if size is 0. just return an allocation error if possible, or at least do a checked sub and unwrap so it doesn't do weird stuff in release mode; but ideally just return an error and let the client decide if they want to crash the process?

@zakarumych
Copy link
Member

I don't want to add another error variant. Especially since we use error type from gfx-hal.
Creating buffer of size 0 violates Vulkan API usage already and it typically happens before memory allocation so we already in dangerous waters where anything can happen.

Doing checking sub and unwrapping is much worse than meaningful assert IMHO.
You can't allocate 0 bytes not because of this expression (it can be modified to permit 0) so unwrapping there will only confuse the user.

@m4b
Copy link
Author

m4b commented Jun 12, 2019

ok, agreed assert better than debug overflow unwrap; even better it's caught even farther upstream, but yea.

Is creating a buffer of size 0 in vulkan really a validation error? I would assume it wouldn't care; it should be able to hand out an infinite amount of 0 size buffers ; )

@zakarumych
Copy link
Member

Yes. Creating buffers of size 0 is invalid usage.

@zakarumych
Copy link
Member

zakarumych commented Jul 10, 2019

I think we can just add asserts in debug mode.

@paulocsanz
Copy link

@omni-viral

Shouldn't the assert also be there in release? Since in release it will wrap and a bunch of unexpected things will happen?

@m4b
Copy link
Author

m4b commented Sep 14, 2019

If it’s invalid usage for size 0 simple panic with error message is best imho, and then documenting this as a panics condition :)

Asserts are for pre and post condition enforcement and author programmer errors

@zakarumych
Copy link
Member

@m4b std uses asserts for invalid inputs like out of bounds for some Vec methods etc.
So I'd say assert is idiomatic way in Rust.

@m4b
Copy link
Author

m4b commented Sep 15, 2019

Out of bounds panics with bounds len violation message, last I checked. I’m sure there are asserts in vec but I can’t remember last time an assert violation bubbled up to the user but I could be mistaken.

@m4b
Copy link
Author

m4b commented Sep 15, 2019

In any event I mostly care about fixing the debug overflow which wasn’t super helpful wrt tracking down cause, and I don’t really care if it’s a panic or assert, just a better error message. This is effectively a one line fix we’re talking about so maybe we should just add it :)

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

No branches or pull requests

3 participants