-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Pointer dereference after free with allocator containing shared state #95269
Comments
This seems to be a problem with the - for b in input {
- output.push(b as i32);
+ for b in &input {
+ output.push(*b as i32);
} There is no crash and no Miri error, this to me indicate that there is a problem in the implementation of Another interesting things is the message I have locally @rustbot label +T-libs +I-unsound |
The drop guard used in the implementation of IntoIter makes a raw copy of an allocator when reconstructing RawVec. The allocator is then dropped twice, once by RawVec drop and second time by IntoIter drop. rust/library/alloc/src/vec/into_iter.rs Lines 315 to 317 in d2df372
cc @TimDiekmann #78461. |
Minimal reproduction of problem: #![feature(allocator_api)]
use core::ptr::NonNull;
use core::alloc::{AllocError, Allocator, Layout};
struct PrintOnDrop;
impl Drop for PrintOnDrop {
fn drop(&mut self) {
println!("dropped")
}
}
unsafe impl Allocator for PrintOnDrop {
fn allocate(&self, _layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
Err(AllocError)
}
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) { }
}
fn main() {
Vec::<u32, _>::new_in(PrintOnDrop { }).into_iter();
} Prints:
|
…ator-in-vec-into-iter, r=oli-obk Fix double drop of allocator in IntoIter impl of Vec Fixes rust-lang#95269 The `drop` impl of `IntoIter` reconstructs a `RawVec` from `buf`, `cap` and `alloc`, when that `RawVec` is dropped it also drops the allocator. To avoid dropping the allocator twice we wrap it in `ManuallyDrop` in the `InttoIter` struct. Note this is my first contribution to the standard library, so I might be missing some details or a better way to solve this.
…ator-in-vec-into-iter, r=oli-obk Fix double drop of allocator in IntoIter impl of Vec Fixes rust-lang#95269 The `drop` impl of `IntoIter` reconstructs a `RawVec` from `buf`, `cap` and `alloc`, when that `RawVec` is dropped it also drops the allocator. To avoid dropping the allocator twice we wrap it in `ManuallyDrop` in the `InttoIter` struct. Note this is my first contribution to the standard library, so I might be missing some details or a better way to solve this.
…ator-in-vec-into-iter, r=oli-obk Fix double drop of allocator in IntoIter impl of Vec Fixes rust-lang#95269 The `drop` impl of `IntoIter` reconstructs a `RawVec` from `buf`, `cap` and `alloc`, when that `RawVec` is dropped it also drops the allocator. To avoid dropping the allocator twice we wrap it in `ManuallyDrop` in the `InttoIter` struct. Note this is my first contribution to the standard library, so I might be missing some details or a better way to solve this.
I'm trying out the allocator api with the goal of being able to track memory usage for parts of a program:
I expected this to work (and print 0 at the end), but instead it fails by freeing an invalid pointer:
Running with miri gives a bit more context:
Meta
The text was updated successfully, but these errors were encountered: