-
Notifications
You must be signed in to change notification settings - Fork 13k
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
-Zrandomize-layout
harder. Foo<T> != Foo<U>
#133088
Conversation
rustbot has assigned @petrochenkov. Use |
This comment has been minimized.
This comment has been minimized.
c91c222
to
119ccc4
Compare
This comment has been minimized.
This comment has been minimized.
rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead. cc @rust-lang/rust-analyzer |
This comment has been minimized.
This comment has been minimized.
r? compiler |
bb177bb
to
8cf5cd2
Compare
This comment has been minimized.
This comment has been minimized.
8cf5cd2
to
c53f89c
Compare
This comment has been minimized.
This comment has been minimized.
c53f89c
to
e536d05
Compare
This comment has been minimized.
This comment has been minimized.
e536d05
to
c276116
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
`-Zrandomize-layout` harder. `Foo<T> != Foo<U>` Tracking issue: rust-lang#106764 Previously randomize-layout only used a deterministic shuffle based on the seed stored in an Adt's ReprOptions, meaning that `Foo<T>` and `Foo<U>` were shuffled by the same seed. This change adds a similar seed to each calculated LayoutData so that a struct can be randomized both based on the layout of its fields and its per-type seed. Primitives start with simple seed derived from some of their properties. Though some types can no longer be distinguished at that point, e.g. usize and u64 will still be treated the same.
compiler/rustc_abi/src/layout.rs
Outdated
@@ -1043,10 +1067,12 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> { | |||
{ | |||
use rand::SeedableRng; | |||
use rand::seq::SliceRandom; | |||
//let field_entropy = fields_excluding_tail.iter().map(|f| f.).sum(); |
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.
remove commented out code
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.
done
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (0e4cbcd): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (secondary 3.5%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 797.356s -> 796.07s (-0.16%) |
☔ The latest upstream changes (presumably #134822) made this pull request unmergeable. Please resolve the merge conflicts. |
5d44eda
to
a9ab5dd
Compare
3b702c1
to
113e6b3
Compare
I have updated the randomization UI test and added some cases that people on zulip were concerned about. |
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. Do we already have a test for MaybeUninit<T>
and T
having matching layouts, even under the influence of -Zrandomize-layout
? I'm surprised it wasn't diffed here if so (or maybe I'm missing something in the GH view...)
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.
MaybeUninit
is repr(transparent)
not repr(Rust)
. Randomization only applies to the latter.
If randomization injected front padding then Option
would be an interesting case because it makes guarantees without having a special repr. But currently randomization only affects field order (and thus only structs with more than 1 field), it does not yet add padding beyond what's required by the alignment of the reordered fields.
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've added the Option case for future randomization improvements, though I'd expect things to blow up earlier if someone broke this.
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.
thanks! yeah I was more wondering about "is there something we need to be making sure we aren't screwing up because it's already semantically guaranteed...?" but you're right, the transparent repr should take care of that.
compiler/rustc_abi/src/lib.rs
Outdated
@@ -1748,6 +1759,7 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> { | |||
align, | |||
max_repr_align: None, | |||
unadjusted_abi_align: align.abi, | |||
randomization_seed: size.bytes().wrapping_add(seed_extra << 32), |
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.
Can you lift this up into a single let randomization_seed
and comment that the combination of choices is to always distinguish the seeds of the minimal pairs
f32
,f64
(byte size)*mut ()
,usize
(base type)i32
,u32
(signedness)
or just "all the primitive types, layout-wise, should get their own seed, so later things like accumulating seeds based on scalars works"
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.
all the primitive types, layout-wise
This is an implementation artifact. All primitive types should get a distinct seed, but some of the information is already erased at this point so this impl only is a conservative approximation of the layout freedoms we have.
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.
Thanks for poking here. This lead me to include the valid-range information for primitives, so more primitives can be distinguished now.
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 was wondering if there was something we could do to distinguish bool and u8! excellent.
previously field ordering was using the same seed for all instances of Foo, now we pass seed values through the layout tree so that not only the struct itself affects layout but also its fields
113e6b3
to
d89b6d5
Compare
thank you! the impl is a bit spread out but the overarching strategy being documented should hopefully prevent that from desyncing. @bors r+ rollup |
🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened. |
…kingjubilee `-Zrandomize-layout` harder. `Foo<T> != Foo<U>` Tracking issue: rust-lang#106764 Previously randomize-layout only used a deterministic shuffle based on the seed stored in an Adt's ReprOptions, meaning that `Foo<T>` and `Foo<U>` were shuffled by the same seed. This change adds a similar seed to each calculated LayoutData so that a struct can be randomized both based on the layout of its fields and its per-type seed. Primitives start with simple seed derived from some of their properties. Though some types can no longer be distinguished at that point, e.g. usize and u64 will still be treated the same.
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#133088 (`-Zrandomize-layout` harder. `Foo<T> != Foo<U>`) - rust-lang#134619 (Improve prose around `as_slice` example of IterMut) - rust-lang#134855 (Add `default_field_values` entry to unstable book) - rust-lang#134908 (Fix `ptr::from_ref` documentation example comment) - rust-lang#135275 (Add Pin::as_deref_mut to 1.84 relnotes) - rust-lang#135294 (Make `bare-fn-no-impl-fn-ptr-99875` test less dependent on path width) - rust-lang#135304 (Add tests cases from review of rust-lang#132289) - rust-lang#135308 (Make sure to walk into nested const blocks in `RegionResolutionVisitor`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#133088 - the8472:randomize-me-harder, r=workingjubilee `-Zrandomize-layout` harder. `Foo<T> != Foo<U>` Tracking issue: rust-lang#106764 Previously randomize-layout only used a deterministic shuffle based on the seed stored in an Adt's ReprOptions, meaning that `Foo<T>` and `Foo<U>` were shuffled by the same seed. This change adds a similar seed to each calculated LayoutData so that a struct can be randomized both based on the layout of its fields and its per-type seed. Primitives start with simple seed derived from some of their properties. Though some types can no longer be distinguished at that point, e.g. usize and u64 will still be treated the same.
Tracking issue: #106764
Previously randomize-layout only used a deterministic shuffle based on the seed stored in an Adt's ReprOptions, meaning that
Foo<T>
andFoo<U>
were shuffled by the same seed. This change adds a similar seed to each calculated LayoutData so that a struct can be randomized both based on the layout of its fields and its per-type seed.Primitives start with simple seed derived from some of their properties. Though some types can no longer be distinguished at that point, e.g. usize and u64 will still be treated the same.