-
Notifications
You must be signed in to change notification settings - Fork 13.4k
treat &raw (const|mut) UNSAFE_STATIC
implied deref as safe
#125834
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
Merged
bors
merged 3 commits into
rust-lang:master
from
workingjubilee:weaken-thir-unsafeck-for-addr-of-static-mut
Jul 23, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//@ check-pass | ||
#![feature(const_mut_refs)] | ||
use std::ptr; | ||
|
||
// This code should remain unsafe because of the two unsafe operations here, | ||
// even if in a hypothetical future we deem all &raw (const|mut) *ptr exprs safe. | ||
|
||
static mut BYTE: u8 = 0; | ||
static mut BYTE_PTR: *mut u8 = ptr::addr_of_mut!(BYTE); | ||
// An unsafe static's ident is a place expression in its own right, so despite the above being safe | ||
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref | ||
static mut DEREF_BYTE_PTR: *mut u8 = unsafe { ptr::addr_of_mut!(*BYTE_PTR) }; | ||
|
||
fn main() { | ||
let _ = unsafe { DEREF_BYTE_PTR }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![feature(const_mut_refs)] | ||
|
||
use std::ptr; | ||
|
||
// This code should remain unsafe because of the two unsafe operations here, | ||
// even if in a hypothetical future we deem all &raw (const|mut) *ptr exprs safe. | ||
|
||
static mut BYTE: u8 = 0; | ||
static mut BYTE_PTR: *mut u8 = ptr::addr_of_mut!(BYTE); | ||
// An unsafe static's ident is a place expression in its own right, so despite the above being safe | ||
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref! | ||
static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR); | ||
//~^ ERROR: use of mutable static | ||
//~| ERROR: dereference of raw pointer | ||
|
||
fn main() { | ||
let _ = unsafe { DEREF_BYTE_PTR }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block | ||
--> $DIR/raw-ref-deref-without-unsafe.rs:12:56 | ||
| | ||
LL | static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR); | ||
| ^^^^^^^^^ dereference of raw pointer | ||
| | ||
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior | ||
|
||
error[E0133]: use of mutable static is unsafe and requires unsafe function or block | ||
--> $DIR/raw-ref-deref-without-unsafe.rs:12:57 | ||
| | ||
LL | static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR); | ||
| ^^^^^^^^ use of mutable static | ||
| | ||
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0133`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@ check-pass | ||
#![feature(raw_ref_op)] | ||
use std::ptr; | ||
|
||
// see https://github.com/rust-lang/rust/issues/125833 | ||
// notionally, taking the address of an extern static is a safe operation, | ||
// as we only point at it instead of generating a true reference to it | ||
|
||
// it may potentially induce linker errors, but the safety of that is not about taking addresses! | ||
// any safety obligation of the extern static's correctness in declaration is on the extern itself, | ||
// see RFC 3484 for more on that: https://rust-lang.github.io/rfcs/3484-unsafe-extern-blocks.html | ||
|
||
extern "C" { | ||
static THERE: u8; | ||
static mut SOMEWHERE: u8; | ||
} | ||
|
||
fn main() { | ||
let ptr2there = ptr::addr_of!(THERE); | ||
let ptr2somewhere = ptr::addr_of!(SOMEWHERE); | ||
let ptr2somewhere = ptr::addr_of_mut!(SOMEWHERE); | ||
|
||
// testing both addr_of and the expression it directly expands to | ||
let raw2there = &raw const THERE; | ||
let raw2somewhere = &raw const SOMEWHERE; | ||
let raw2somewhere = &raw mut SOMEWHERE; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//@ check-pass | ||
#![feature(raw_ref_op)] | ||
use std::ptr; | ||
|
||
// see https://github.com/rust-lang/rust/issues/125833 | ||
// notionally, taking the address of a static mut is a safe operation, | ||
// as we only point at it instead of generating a true reference to it | ||
static mut NOWHERE: usize = 0; | ||
|
||
fn main() { | ||
let p2nowhere = ptr::addr_of!(NOWHERE); | ||
let p2nowhere = ptr::addr_of_mut!(NOWHERE); | ||
|
||
// testing both addr_of and the expression it directly expands to | ||
let raw2nowhere = &raw const NOWHERE; | ||
let raw2nowhere = &raw mut NOWHERE; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.