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 to change a non-mut extern static? #514

Closed
ChayimFriedman2 opened this issue May 25, 2024 · 3 comments · Fixed by rust-lang/reference#1502
Closed

Is it UB to change a non-mut extern static? #514

ChayimFriedman2 opened this issue May 25, 2024 · 3 comments · Fixed by rust-lang/reference#1502

Comments

@ChayimFriedman2
Copy link

Is it UB to change a non-mut non-UnsafeCell extern static, outside of Rust land?

Obviously it is UB to change it from Rust, but is it UB to change it from other. But is it UB to also change it from external code?

For instance, is the following UB?

Rust:

extern "C" {
    static foo: i32;

    fn black_box();
}

pub unsafe fn do_work() -> bool {
    let a = foo;
    black_box();
    let b = foo;
    a == b
}

C:

int32_t foo = 0;

void black_box() {
    foo++;
}

LLVM does not optimize the return value of do_work() to be true (https://godbolt.org/z/xdqGM839n), and we also don't tell it it is constant (we say @foo = external global i32). So this probably isn't UB from LLVM. But still, is it UB in the Rust AM?

The Reference does not hint in any way that this might be UB. It says the following about external statics:

Statics within external blocks are declared in the same way as statics outside of external blocks,
except that they do not have an expression initializing their value.
It is unsafe to access a static item declared in an extern block, whether or
not it's mutable, because there is nothing guaranteeing that the bit pattern at the static's
memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is in charge
of initializing the static.

Extern statics can be either immutable or mutable just like statics outside of external blocks.
An immutable static must be initialized before any Rust code is executed. It is not enough for
the static to be initialized before Rust code reads from it.

@Lokathor
Copy link
Contributor

Part of my intention with the Unsafe Extern Blocks RFC (rust-lang/rfcs#3484) is that, other than the problem of making type signatures match, an extern and a rust native function/static should behave the same.

So, I don't know the current state of it, but I'd say that this should be a UB case. If the extern static can be mutated, then it needs a type signature that reflects that.

@RalfJung
Copy link
Member

Is it UB to change a non-mut non-UnsafeCell extern static, outside of Rust land?

Yes.

@ChayimFriedman2
Copy link
Author

Closing since rust-lang/reference#1502 is merged.

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.

3 participants