-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Description
https://doc.rust-lang.org/alloc/alloc/fn.dealloc.html points to https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#tymethod.dealloc, which says nothing about provenance, just
ptris a block of memory currently allocated via this allocator and,layoutis the same layout that was used to allocate that block of memory.
(https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html#tymethod.deallocate also doesn't mention provenance, nor does https://doc.rust-lang.org/alloc/alloc/trait.Allocator.html#currently-allocated-memory.)
Does the ptr need to have any particular provenance? Does an allocator need to launder the provenance back to a known-valid one in order to read/write through the pointer it got in dealloc?
Whatever the answer, it would be good to say either way in docs.
FWIW, MIRI rejects this today ("pointer not dereferenceable"):
let layout = Layout::new::<i32>();
let p: *mut u8 = alloc(layout);
let other: *mut u8 = without_provenance_mut(p.addr());
dealloc(other, layout);And also rejects this (with an odd "does not point to the beginning of an object" error that's arguably not true):
let layout = Layout::new::<i32>();
let p: *mut u8 = alloc(layout);
let mut mylocal = 10_u8;
let other = (&raw mut mylocal).with_addr(p.addr());
dealloc(other, layout);