-
Notifications
You must be signed in to change notification settings - Fork 349
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
Allocators that "shrink" allocations to fit the request cause Stacked Borrows errors when used with Box #2104
Comments
It's not just shrinking, allocators that overallocate (as allowed by the #![feature(allocator_api)]
use core::ptr::NonNull;
use std::alloc::{Allocator, Layout, AllocError, System};
struct OverAllocate;
unsafe impl Allocator for OverAllocate {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let (layout, _) = layout.extend(layout).unwrap();
let layout = layout.pad_to_align();
System.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
let (layout, _) = layout.extend(layout).unwrap();
let layout = layout.pad_to_align();
System.deallocate(ptr, layout);
}
}
fn main() {
let _ = Box::new_in(0, OverAllocate);
}
(If you assume this check in miri is correct, then fixing |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
That is what I mean by shrinking: allocating more, and then only returning a part of that from the allocation request (hence "shrinking" the underlying allocation to fit the request). We now have the very experimental Tree Borrows mode in Miri, which should be able to handle code like this. You can enable it via |
By "overallocating" I mean allocating something larger than the requested layout and returning the entire larger allocation, not returning a subset. |
I think that just means Box is doing the shrinking. It ignores the length of the returned slice.
|
When a Box allocator creates an allocation and returns only a part of that to Box, there will be Stacked Borrows errors when the Box is deallocated. This is because the pointer lost the provenance for the other parts of memory outside the Box, and hence does not have the right to deallocate them.
This affects, in particular, the
System
allocator on Windows:Also see the discussion on Zulip.
The text was updated successfully, but these errors were encountered: