Repository navigation
Closed
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: 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.Relevant to the library team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem teamrequires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Description
Box::into_unique creates the returned pointer through Unique::from(&mut), which causes its provenance to be restricted to only size_of::<T>() bytes:
rust/library/alloc/src/boxed.rs
Lines 1780 to 1784 in 0630f86
| 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
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: 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.Relevant to the library team, which will review and decide on the PR/issue.T-opsemRelevant to the opsem teamRelevant to the opsem teamrequires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.This issue requires a nightly compiler in some way. When possible, use a F-* label instead.
Activity
Rollup merge of rust-lang#162732 - maxdexh:water-leak, r=Darksonn
Rollup merge of rust-lang#162732 - maxdexh:water-leak, r=Darksonn
Unrolled build for #162732 in rollup 162742