-
Notifications
You must be signed in to change notification settings - Fork 220
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
VecStorage and DenseVecStorage are unsound #646
Comments
|
This issue concerns the UB-ness of slices which contain uninitialized values. I have previously argued that this was okay, based on things like
I concur that slices which contain uninitialized elements are indeed unsound in current Rust. #634 contains a patch to that effect. I can move it to a new PR if you'd like to consider it separately. |
There was a set of issues that popped up in some rust programs that showed that having an invalid pointer ever to exist, ever, means that LLVM can start compiling in certain assumptions, specifically making all following code that depends on the unsound code to do absolutely crazy things. This is the reason that uninit and zero are to be deprecated and they are replaced with MaybeUninit. |
Has anyone run specs under Miri? It can aid in discovering UB. |
yes: #644 |
I think this can be close thanks to #634 |
Hi all!
DenseVecStorage
The problem is located in this function.
Having uninitialized data inside a slice is insta-UB, in this function
data_id
has uninitializedu32
thanks to the contract of set_len not being respected. Then get_unchecked_mut will create a slice and trigger UB.This slice is created in every function accessing data inside the storage too.
This could be fixed by replacing uninitialized value by
0
orstd::u32::MAX
for example (it's never accessed anyway).VecStorage
The problem is in insert too and spread to any function accessing data in the storage. This is basically the same thing, a call to
set_len
not respecting the contract followed by a call toget_unchecked_mut
but this time it's aVec<T>
.I think the solution would be
VecStorage<T>(Vec<MaybeUninit<T>>)
. An other way would be to index inside theVec
using raw pointer only but that's dangerous since any "classic" access would still trigger UB.Also creating a reference to uninitialized data is not currently ok but might change in the future for some types (like
u32
) so it might be a good thing to replace them with raw pointer arithmetic.The text was updated successfully, but these errors were encountered: