-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4062 from rust-lang/unsafe-fixes
Ch. 20: Address soundness issues and introduce Miri
- Loading branch information
Showing
6 changed files
with
97 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -311,6 +311,8 @@ microcontroller | |
microcontrollers | ||
millis | ||
minigrep | ||
Miri | ||
miri | ||
mixup | ||
mkdir | ||
MockMessenger | ||
|
This file contains 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 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 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 @@ | ||
$ cargo +nightly miri run | ||
Preparing a sysroot for Miri (target: aarch64-apple-darwin)... done | ||
Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example) | ||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s | ||
Running `/Users/chris/.rustup/toolchains/nightly-aarch64-apple-darwin/bin/cargo-miri runner target/miri/aarch64-apple-darwin/debug/unsafe-example` | ||
warning: creating a shared reference to mutable static is discouraged | ||
--> src/main.rs:14:33 | ||
| | ||
14 | println!("COUNTER: {}", COUNTER); | ||
| ^^^^^^^ shared reference to mutable static | ||
| | ||
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html> | ||
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives | ||
= note: `#[warn(static_mut_refs)]` on by default | ||
|
||
COUNTER: 3 |
This file contains 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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
static mut COUNTER: u32 = 0; | ||
|
||
fn add_to_count(inc: u32) { | ||
unsafe { | ||
COUNTER += inc; | ||
} | ||
/// SAFETY: Calling this from more than a single thread at a time is undefined | ||
/// behavior, so you *must* guarantee you only call it from a single thread at | ||
/// a time. | ||
unsafe fn add_to_count(inc: u32) { | ||
COUNTER += inc; | ||
} | ||
|
||
fn main() { | ||
add_to_count(3); | ||
|
||
unsafe { | ||
println!("COUNTER: {COUNTER}"); | ||
// SAFETY: This is only called from a single thread in `main`. | ||
add_to_count(3); | ||
println!("COUNTER: {}", COUNTER); | ||
} | ||
} |
This file contains 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