Skip to content

Box::into_unique(self) -> (Unique<T>, A) goes through &mut *ptr #162719

Closed

Description

@maxdexh

Box::into_unique creates the returned pointer through Unique::from(&mut), which causes its provenance to be restricted to only size_of::<T>() bytes:

pub fn into_unique(b: Self) -> (Unique<T>, A) {
let (ptr, alloc) = Box::into_raw_with_allocator(b);
// SAFETY: Pointer is valid and unique.
unsafe { (Unique::from(&mut *ptr), alloc) }
}

Code using this function is expected to pass the pointer back to the allocator, but this is unsound for the same reason it is unsound to Box::from_raw_in a Box::leaked reference that is not from Global: The allocator may store extra metadata outside the allocation and may assume that deallocate is called with a pointer with the same provenance as was returned by allocate.

Example:

#![feature(allocator_api)]

use std::{alloc::*, mem::MaybeUninit, ptr::NonNull, sync::Arc};

fn main() {
    // Fine, pointer with same provenance is passed to `deallocate`
    drop(Arc::<i32, _>::new_uninit_in(MyMetadataAlloc));

    // Deallocated pointer is from `&mut *ptr` (through `into_unique`), UB!
    drop(Arc::new_in(MaybeUninit::<i32>::uninit(), MyMetadataAlloc));
}

struct MyMetadataAlloc;

fn widen(layout: Layout) -> Layout {
    Layout::from_size_align(layout.size() + 10, layout.align())
        .unwrap_or_else(|_| std::process::abort())
}

unsafe impl Allocator for MyMetadataAlloc {
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        // imagine we are storing metadata in the extra bytes
        let ptr = Global.allocate(widen(layout))?;
        Ok(NonNull::slice_from_raw_parts(ptr.cast(), layout.size()))
    }
    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
        // SAFETY: we get back a pointer with the same provenance as was
        // returned by `allocate`, so it's okay to deallocate the full
        // `layout.size() + 10` bytes through it.
        unsafe { Global.deallocate(ptr, widen(layout)) }
    }
}

Running with miri:

error: Undefined Behavior: attempting deallocation using <743> at alloc332, but that tag does not exist in the borrow stack for this location
  --> src/main.rs:29:18
   |
29 |         unsafe { Global.deallocate(ptr, widen(layout)) }
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
   |

@rustbot label I-unsound A-allocators requires-nightly T-libs T-opsem -needs-triage
@rustbot claim

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-allocatorsArea: Custom and system allocators
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    T-opsemRelevant to the opsem team
    on Sep 13, 2026
  2. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 13, 2026
  3. added 2 commits that reference this issue on Sep 13, 2026
    f371233
    4edca2f
  4. added a commit that references this issue on Sep 14, 2026
    b991789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-libsRelevant to the library team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamrequires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions