Skip to content

Box::into_raw no longer does proper retag-to-raw #3473

Closed
rust-lang/rust
#124013
@RalfJung

Description

@RalfJung

The following program used to work, but now it fails:

use std::{
    cell::UnsafeCell,
    sync::atomic::{AtomicPtr, Ordering},
};

#[repr(C)]
#[derive(Default)]
struct Node {
    _meta: UnsafeCell<usize>,
    value: usize,
}

impl Node {
    fn value(&self) -> &usize {
        &self.value
    }
}

fn main() {
    unsafe {
        let a = Box::into_raw(Box::new(Node::default()));
        let ptr = &*a;
        *UnsafeCell::raw_get(a.cast::<UnsafeCell<usize>>()) = 2;
        println!("{}", ptr.value());
    }
}

That is caused by rust-lang/rust#122647. That PR even has a commenting about this situation -- code doing a box-to-raw cast while being generic over the allocator is just not something we can handle properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions