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

A shared reference to static mut stored in another static can be used mutably #2937

Closed
ChayimFriedman2 opened this issue Jun 20, 2023 · 4 comments

Comments

@ChayimFriedman2
Copy link

struct Foo(*mut i32);
unsafe impl Sync for Foo {}

static mut FOO: i32 = 0;
static PTR: Foo = Foo(unsafe { &FOO as *const i32 as *mut i32 });

fn main() {
    unsafe {
        let _r = &mut *PTR.0;
    }
}

Does not report any error, even though it definitely contains UB because a similar code does report:

static mut FOO: i32 = 0;

fn main() {
    unsafe {
        let p = &FOO as *const i32 as *mut i32;
        let _r = &mut *p;
    }
}

This may be because of #121.

@RalfJung
Copy link
Member

RalfJung commented Jun 21, 2023 via email

@isaacthefallenapple
Copy link

@RalfJung Does that imply this isn't UB? Or just unspecified? Chiming in because this originated from my question on SO.

@RalfJung
Copy link
Member

It is deep in undecided/unspecified territory. So please don't do it. :)

@RalfJung
Copy link
Member

I have opened rust-lang/unsafe-code-guidelines#424 to track this question from a specification side. Thanks for bringing this to my attention!

There's nothing to be done on the Miri side here currently, so I will close this issue.

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

3 participants