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

is it UB or not if using cell's underlying data raw ptr after cell is moved #2796

Closed
gftea opened this issue Feb 22, 2023 · 4 comments
Closed

Comments

@gftea
Copy link

gftea commented Feb 22, 2023

rust playground

I am a bit confused about the "Cell::as_ptr()", user can escape the compiler check, and may still use the raw pointer after the cell is moved.

#[derive(Debug)]
struct Inner(isize);

impl Drop for Inner {
    fn drop(&mut self) {
        println!("drop inner {:?}", self as *const _);
    }
}

let m = Cell::new(Inner(0));
let m_ptr = m.as_ptr();

println!("before move: {:?}", m.as_ptr());

let h = thread::spawn(move || {
    // the `m` cell underlying data has different address after move
    println!("after move: {:?}", m.as_ptr());
    // `m` drop
});

// we can still read/write the original address after moving `m`
// should it be UB? 
// if yes, miri does not report it
// If not, what does the address really means?
unsafe {println!("m_ptr: {:?}, value: {:?}",  m_ptr, *m_ptr)};
@gftea gftea changed the title is it UB or not if using cell as ptr as below is it UB or not if using cell's underlying data raw ptr after cell is moved Feb 22, 2023
@oli-obk
Copy link
Contributor

oli-obk commented Feb 22, 2023

Yea. We don't invalidate memory on move. Though maybe we should invalidate borrows to moved out memory?

@saethlin
Copy link
Member

This is rust-lang/unsafe-code-guidelines#188

@RalfJung
Copy link
Member

RalfJung commented Feb 23, 2023 via email

@RalfJung
Copy link
Member

This is a UCG / t-opsem question, not a Miri question, so closing in favor of rust-lang/unsafe-code-guidelines#188.

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

No branches or pull requests

4 participants