rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

Incorrect index validation

Open atynagano opened this issue 2 years ago • 4 comments

https://github.com/EmbarkStudios/rust-gpu/blob/e87c324bfda6bea144fe2635586281c4ef144f0d/crates/spirv-std/src/byte_addressable_buffer.rs#L73-L78

if byte_index + mem::size_of::<T>() as u32 > (mem::size_of::<u32> * self.data.len()) as u32

Isn't the right-hand side multiplied by 4 because data is &[u32]?

atynagano avatar Sep 21 '23 11:09 atynagano

ByteAddressableBuffer requires for the data to be aligned to 4 bytes, so the current condition is fine IMO; the naming could be better (word_index: u32), but the comment above explains it's inherited from HLSL.

Note that buffer_load_intrinsic() ends up doing 4 * byte_index:

https://github.com/EmbarkStudios/rust-gpu/blob/e87c324bfda6bea144fe2635586281c4ef144f0d/crates/rustc_codegen_spirv/src/builder/byte_addressable_buffer.rs#L202

Patryk27 avatar Sep 21 '23 17:09 Patryk27

So does the left side size_of<T> needs to be divided by 4? Or does size_of returns dword size?

atynagano avatar Sep 22 '23 00:09 atynagano

Oh, yeah - I think it should say mem::size_of::<T>() / 4 then 🤔

Patryk27 avatar Sep 22 '23 05:09 Patryk27