-
Notifications
You must be signed in to change notification settings - Fork 13.7k
RawVecInner: add missing unsafe
to unsafe fns
#145067
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
- RawVecInner::grow_exact causes UB if called with len and additional arguments such that len + additional is less than the current capacity. Indeed, in that case it calls Allocator::grow with a new_layout that is smaller than old_layout, which violates a safety precondition. - All RawVecInner methods for resizing the buffer cause UB if called with an elem_layout different from the one used to initially allocate the buffer, because in that case Allocator::grow/shrink is called with an old_layout that does not fit the allocated block, which violates a safety precondition. - RawVecInner::current_memory might cause UB if called with an elem_layout different from the one used to initially allocate the buffer, because the unchecked_mul might overflow. - Furthermore, these methods cause UB if called with an elem_layout where the size is not a multiple of the alignment. This is because Layout::repeat is used (in layout_array) to compute the allocation's layout when allocating, which includes padding to ensure alignment of array elements, but simple multiplication is used (in current_memory) to compute the old allocation's layout when resizing or deallocating, which would cause the layout used to resize or deallocate to not fit the allocated block, which violates a safety precondition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not an std reviewer, but I wanted to suggest that you add safety comments to the functions that you marked as unsafe.
You could even go one step further and try adding contracts to them, but be aware that it is still highly experimental, and it may not work.
@celinval I considered that, but I noticed that most of the safety preconditions of the functions that were already marked |
Some (module-private) functions in
library/alloc/src/raw_vec/mod.rs
are unsafe (i.e. may cause UB when called from safe code) but are not markedunsafe
. Specifically:RawVecInner::grow_exact
causes UB if called withlen
andadditional
arguments such thatlen + additional
is less than the current capacity. Indeed, in that case it calls Allocator::grow with anew_layout
that is smaller thanold_layout
, which violates a safety precondition.elem_layout
different from the one used to initially allocate the buffer, because in that caseAllocator::grow
orAllocator::shrink
are called with anold_layout
that does not fit the allocated block, which violates a safety precondition.RawVecInner::current_memory
might cause UB if called with anelem_layout
different from the one used to initially allocate the buffer, because theunchecked_mul
might overflow.elem_layout
where the size is not a multiple of the alignment. This is becauseLayout::repeat
is used (inlayout_array
) to compute the allocation's layout when allocating, which includes padding to ensure alignment of array elements, but simple multiplication is used (incurrent_memory
) to compute the old allocation's layout when resizing or deallocating, which would cause the layout used to resize or deallocate to not fit the allocated block, which violates a safety precondition.I discovered these issues while performing formal verification of
library/alloc/src/raw_vec/mod.rs
per Challenge 19 of the AWS Rust Standard Library Verification Contest.