-
Notifications
You must be signed in to change notification settings - Fork 606
nail down the final validity rules: references and unions #2337
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
Open
RalfJung
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
RalfJung:finish-validity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like making inhabitedness depend on metadata values is not right / clean.
I would rather make this a separate restriction on the value of the metadata. That is, I would say something along the lines of "A reference or
Box<T>must ... have valid metadata" and "slice metadata for a slice type with uninhabited elements, it must be 0".This allows adding other restrictions in the future, such as the one mentioned by @scottmcm that slice metadata can at most be
usize::MAX / size_of::<T>()(for non-ZSTs).View changes since the review
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally pretty consistently treat the metadata as telling us the "actual type" that we use to inspect the pointee. I think it would be odd do do something else here. It would look like an arbitrary set of rules, rather than just something that falls out of a more general principle.
As I mentioned in reply to Scott, that restriction is already present. It follows from the fact that the reference has to be dereferenceable for its actual dynamic size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. It still feels weird to me to do it this way for slices, but if we are consistently doing it this way I suppose that is better.
I suppose the reason it feels weird to me is that in the compiler "inhabitedness" is a property of types, and does not concern the value in any way. If we would like exploit this UB in the compiler, we would have to add an arbitrary rule in the form of "if the tail is a slice of an uninhabited type,
assume(len == 0)".In a way, considering the "actual type" feels more arbitrary to me.
That being said, I imagine we have to consider the "actual type" for trait objects, at which point it's better do the same thing for slices too.
(I'd resolve this conversation, but it appears I don't have permissions for that)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler can only statically approximate all these properties using the types, yes. But that's expected, everything on this page is written assuming full knowledge of the current dynamic state (think: Miri, MiniRust). The compiler might get better at approximating things and that should not change the spec!
IOW, I think you're thinking too much like a compiler writer and not enough like a language specifier. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guilty as charged I'm afraid ^^'
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To also reply to this specifically -- the compiler does not have to exploit this UB if we think it's too weird or doesn't fit the current compiler architecture. But I would like to keep the door open to the compiler potentially exploiting this UB in the future, as the compiler architecture may change and make this less weird. Maybe one day it becomes trivial to make it so that the length metadata of
&[!]is annotated with range information saying it has to be 0. This seems quite plausible, don't we already encode that the length is at mostisize::MAXfor non-zero-sized types?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in many contexts we do encode that the length is
0..=floor(isize::MAX/size_of::<pointee>), in particular function arguments. in a few context we do not (due to perf regressions and dubious benefit), and we never do for unsized tails, it's only&[T].for example,
fn meow(x: &[u16])isi64 noundef range(i64 0, 4611686018427387904)for the length arg, butstruct Meow { val: [u16] }fn meow(x: &Meow)is a plaini64 noundef.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RalfJung I was not arguing against adding arbitrary rules to the compiler (it's full of them!), I was more so arguing that it should be fine to have them here too (which I don't believe anymore); thanks for explaining your view though :)
@asquared31415 do you know why we are only adding range metadata forTurns out this metadata is calculated with a special case in cg_llvm: https://github.com/rust-lang/rust/blob/c4c4a576936e9e67717d0deb8e74e02dd5dd10de/compiler/rustc_codegen_llvm/src/abi.rs#L561-L579 As the comment suggests, I don't think this is a good way to handle this, hm...&[T]and not&Meow? Seems like an oversight...Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it as a part of another PR, and it ended up regressing codegen in most cases, with minimal benefits: rust-lang/rust#159921 (comment) unfortunately I don't have numbers on hand, but llvm was making some bad assembly involving 64 bit immediates and sometimes even using a whole extra register (in a contrived test, but I'm unsure if it would go away even after inlining in normal usage)
The attributes are set here in rustc_codegen_llvm, where it matches only
Ref(Slice(T))types, and there's even a note that it's suboptimal.During the aforementioned PR, I even hit the layout cycles that the comment alludes to, so it's definitely still an issue as of july-ish.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scottmcm might have some additional context as the original author of that hack. They mentioned rust-lang/rust#152843 could help get some of the layout info we would like on DSTs, but IIRC I also touched that code in similar ways (with nuw and/or nsw on that math) and ended up getting similar "llvm suddenly emits 64 bit literals and extra registers".