Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RalfJung opened this issue Apr 16, 2024 · 0 comments · Fixed by rust-lang/rust#124013
Closed

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

RalfJung opened this issue Apr 16, 2024 · 0 comments · Fixed by rust-lang/rust#124013

Comments

@RalfJung
Copy link
Member

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.

rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 17, 2024
Rollup merge of rust-lang#124013 - RalfJung:box-to-raw, r=oli-obk

Box::into_raw: make Miri understand that this is a box-to-raw cast

Turns out rust-lang#122647 went a bit too far in cleaning up `Box`... we still need a hack in `Box::into_raw`. The nicer fix would be to make Stacked Borrows not care about reference-to-raw-pointer casts, but it's unclear whether that will ever be possible without going to full Tree Borrows.

Fixes rust-lang/miri#3473.
github-actions bot pushed a commit that referenced this issue Apr 17, 2024
Box::into_raw: make Miri understand that this is a box-to-raw cast

Turns out rust-lang/rust#122647 went a bit too far in cleaning up `Box`... we still need a hack in `Box::into_raw`. The nicer fix would be to make Stacked Borrows not care about reference-to-raw-pointer casts, but it's unclear whether that will ever be possible without going to full Tree Borrows.

Fixes #3473.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant