book icon indicating copy to clipboard operation
book copied to clipboard

Wrong concept in subsection "Using Miri to check unsafe code" of section "20.1. Unsafe Rust"

Open rusty-bytes opened this issue 6 months ago • 1 comments

Current text:

For an example of how helpful this can be, consider what happens when we run it against Listing 20-11:

$ cargo +nightly miri run
...
warning: creating a shared reference to mutable static is discouraged
...
14 |         println!("COUNTER: {}", COUNTER);
   |                                 ^^^^^^^ shared reference to mutable static
...

It helpfully and correctly notices that we have shared references to mutable data, and warns about it. In this case, it does not tell us how to fix the problem, but it means that we know there is a possible issue and can think about how to make sure it is safe. In other cases, it can actually tell us that some code is sure to be wrong and make recommendations about how to fix it.

  • Problem 1: Miri gives no errors on snippet 20-11. (https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a00e163b3eff2c6d245530e75350db76)

  • Problem 2: Line 14 in the quote above is different from the corresponding line in snippet 20-11, which is: println!("COUNTER: {}", *(&raw const COUNTER));

Note: Line 14 in the quote above produces the mentioned warning in edition 2021 while it produces a compilation error in edition 2024. For edition 2024, a different snippet must be devised to show Miri warnings on a code which otherwise successfully compiles.

rusty-bytes avatar Apr 17 '25 06:04 rusty-bytes