-
Notifications
You must be signed in to change notification settings - Fork 569
replace 'UB on raw ptr deref' with UB on place projection/access #1387
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
Conversation
|
I am not sure if this counts as a "small" decision that t-opsem can just make without t-lang involvement, so I'll add both teams. This is an irreversible decision after all and a change to the core "list of UB", that seems quite fundamental. The justification for why we think this first example should not be UB is mostly that many people are confused about the entire place expression business, so they don't realize The justification for keeping the later 2 examples UB is that our current codegen actually does exploit both of them, so allowing them comes at a cost. The first example should be rather uncontroversial; the fact that every access has to be in-bounds and aligned has never been up for debate. The second example is probably less clear, but we do want to keep using @rfcbot fcp merge |
|
Team member @RalfJung has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
Fwiw I don't expect this example to have needed T-lang signoff, but there's no harm. @rfcbot reviewed |
|
@rfcbot reviewed |
|
rust-lang/rust#114330 implements this change in Miri. I found some other interesting cases that are allowed now. A mere "place mention" of a dangling place is no longer UB -- that is in line with fn deref_invalid_underscore() {
unsafe {
let _ = *ptr::invalid::<i32>(0);
let _ = *ptr::invalid::<i32>(1);
}
}If fn deref_partially_dangling() {
let x = (1, 13);
let xptr = &x as *const _ as *const (i32, i32, i32);
let val = unsafe { (*xptr).1 };
assert_eq!(val, 13);
} |
|
Ah, and here is another fun one that is allowed now: fn deref_too_big_slice() {
unsafe {
let slice: *const [u8] = mem::transmute((1usize, usize::MAX));
let _val = addr_of!(*slice);
}
}
|
Have we decided that the order of the fields in the tuple |
|
No we have not decided that, the proper version of that test would use a |
|
This makes good sense to me. Having |
|
@rfcbot reviewed |
1 similar comment
|
@rfcbot reviewed |
I agree with this and don't have any blocking concerns over this change specifically. But I am concerned that this will lead users to a mental model where place projections inside At a high level I would like us to strongly prefer simple rules for what is and isn't UB, wherever possible, and justify complexity as either inherently necessary or temporary. " From the description it sounded like this condition might be temporary (and is due to a current limitation in LLVM), but if we could wave a magic wand and fix the LLVM limitation, are we sure we would? |
I agree, generally, but I think this is a problem of documentation. Currently we have essentially no documentation for what valid uses of |
With any luck |
Those are the rules we currently have. I also think they are simpler. But many, many people have been caught off-guard by the fact that
You just have to separate alignment and inbounds:
|
If this is talking about the requirement that field projections follow the rules of the We considered weakening the requirements of |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 psst @RalfJung, I wasn't able to add the |
As discussed by @rust-lang/opsem in rust-lang/opsem-team#9: instead of saying that
*ptris insta-UB for "bad" pointers, only have UB when the place is actually accessed, and when place projections go out-of-bounds.This makes the following code (which currently has UB) legal:
However, the following code is still UB: