rust-lang/unsafe-code-guidelines

Box retags on drop causing Miri to report problem in custom allocator

zakarumych opened this issue · 3 comments

I'm implementing custom allocator that stores allocation metadata in first bytes few of the allocated block and offsets pointer before returning it from Allocator::allocate.
It goes in reverse in Allocator::deallocate and reads metadata from before the passed pointer.

It works until I try to use Box::new_in or similar.
When Box is dropped, pointer passed to A::deallocate has different tag than one that returned from A::allocate.
And Miri emits error saying that tag does not encompass first bytes of the block and I can't read them.

Here's playground link

Same error happens if pointer from Allocator::allocate is converted to reference and then back to pointer.
That's, I guess, can be expected. But Box should not retag pointer on drop as I understand it.
Or this is something strictly prohibited by Rust.

Yes, that's the same issue.
With -Zmiri-tree-borrows Miri is happy.

Thanks for checking! Closing as a duplicate then.