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

Creating a raw pointer to an external static requires an unsafe block #74843

Open
Nemo157 opened this issue Jul 27, 2020 · 5 comments
Open

Creating a raw pointer to an external static requires an unsafe block #74843

Nemo157 opened this issue Jul 27, 2020 · 5 comments
Labels
A-ffi Area: Foreign Function Interface (FFI) C-bug Category: This is a bug. F-raw_ref_op `#![feature(raw_ref_op)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Jul 27, 2020

I tried this code (playground):

#![feature(raw_ref_op)]

extern {
    static FOO: u32;
}

mod foo {
    #[no_mangle]
    static FOO: u32 = 5;
}

fn main() {
    dbg!(core::ptr::addr_of!(FOO));
    dbg!(&raw const FOO);
}

I expected to see this happen: it compile and print the address of FOO twice.

Instead, this happened: compilation failed because accessing FOO requires an unsafe block

error[E0133]: use of extern static is unsafe and requires unsafe function or block
  --> src/main.rs:13:10
   |
13 |     dbg!(core::ptr::addr_of!(FOO));
   |          ^^^^^^^^^^^^^^^^^^^^^^^^ use of extern static
   |
   = note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
   = note: this error originates in the macro `core::ptr::addr_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0133]: use of extern static is unsafe and requires unsafe function or block
  --> src/main.rs:14:10
   |
14 |     dbg!(&raw const FOO);
   |          ^^^^^^^^^^^^^^ use of extern static
   |
   = note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

As far as I can tell none of the potential causes of UB can be caused by just creating a raw pointer to the static. This should be a safe operation, and only when attempting to use the pointer in the future should you need to prove validity of it.

@Nemo157 Nemo157 added the C-bug Category: This is a bug. label Jul 27, 2020
@jonas-schievink jonas-schievink added A-ffi Area: Foreign Function Interface (FFI) F-raw_ref_op `#![feature(raw_ref_op)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Jul 27, 2020
@Nemo157
Copy link
Member Author

Nemo157 commented Jul 28, 2020

Based on the MIR generated this is creating a temporary reference to the static, asserting its validity, so technically does need unsafe with the current desugaring.

@RalfJung
Copy link
Member

Yeah I think this is a duplicate of #74840.

Basically the bug is that &raw const FOO desugars to &raw const *&FOO. That explains both problems.

@RalfJung
Copy link
Member

Closing in favor of #74840.

@Nemo157
Copy link
Member Author

Nemo157 commented Jul 20, 2023

I was reminded about this again today. #74840 got closed by banning extern static FOO: !;, not by changing how creating a raw pointer to it is treated, so this was not affected. #77096 I think covers the high-level "statics need to be fixed", but it doesn't directly mention this case. So I think it's worth having this re-opened as something that should be fixed. (I'll update the code in the OP for the new stable API).

@Nemo157 Nemo157 reopened this Jul 20, 2023
@RalfJung
Copy link
Member

FWIW, creating a raw pointer to a mutable static is also unsafe. So this looks deliberate to me. Not saying it can't be changed, but that should be considered a change/feature, not a bugfix (i.e. it will need T-lang FCP at least).

@Nemo157 Nemo157 removed the requires-nightly This issue requires a nightly compiler in some way. label Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ffi Area: Foreign Function Interface (FFI) C-bug Category: This is a bug. F-raw_ref_op `#![feature(raw_ref_op)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants