-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Listing 19-03 is potentially UB under Stacked Borrows #3014
Comments
You don’t need any flags anymore. I believe since |
Hi, I just found this issue. I think a better fix would be to use (and explain) The problem here is that the temporary I also think that this is a great opportunity to warn and/or illustrate what can go wrong with unsafe, and maybe introduce miri. IMO, the best fix would be to leave the listing as is, explain and illustrate with miri why it's wrong, then provide a correct version. |
Oooof. We definitely don’t want unsound code in our example of |
I just spent some time poking at this and thinking about it, and a couple things stood out to me:
To @kellda’s suggestion—I like that thought, as it is safer and Miri is happy with it (playground). However, since https://github.com/rust-lang/rust#127679 stabilized and should be in 1.82.0, out in about two weeks, iff we change it then we should use fn main() {
let mut num = 5;
let r1 = &raw const num;
let r2 = &raw mut num;
unsafe {
println!("r1 is: {}", *r1);
println!("r2 is: {}", *r2);
}
} That works exactly as expected, including being safe on both Stacked and Tree Borrows—which, you know, it should; that’s one of the main points of the feature! The trick is thinking about how to rework this section to be correct. I do like the idea of getting a reference to Miri here, too. I’m going to see if I can figure out a good way to do both. All of that said, I welcome input from someone who is actually an expert in this space, seeing as this was the first time I had ever actually used Miri rather than simply reading about it. 😅 |
main
branch to see if this has already been fixedURL to the section(s) of the book with this problem: https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer
Description of the problem: Running the listing
Under MIRI with
MIRIFLAGS="-Zmiri-symbolic-alignment-check -Zmiri-check-number-validity -Zmiri-tag-raw-pointers"
results in the following:Suggested fix:
I'm not sure. The following fixes that specific problem:
But I'm not sure if it preserves the original meaning
Possibly related: rust-lang/unsafe-code-guidelines#133
The text was updated successfully, but these errors were encountered: