Skip to content
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

Merged
merged 5 commits into from
Jan 10, 2025

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Nov 16, 2024

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> 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.

@rustbot
Copy link
Collaborator

rustbot commented Nov 16, 2024

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 16, 2024
@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 force-pushed the randomize-me-harder branch from c91c222 to 119ccc4 Compare November 16, 2024 01:26
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Nov 16, 2024

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

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

r? compiler

@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 force-pushed the randomize-me-harder branch from 8cf5cd2 to c53f89c Compare November 17, 2024 00:49
@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 force-pushed the randomize-me-harder branch from c53f89c to e536d05 Compare November 17, 2024 01:04
@rust-log-analyzer

This comment has been minimized.

@the8472 the8472 force-pushed the randomize-me-harder branch from e536d05 to c276116 Compare November 17, 2024 02:01
@compiler-errors
Copy link
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 24, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 24, 2024
`-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.
@bors
Copy link
Contributor

bors commented Nov 24, 2024

⌛ Trying commit c276116 with merge 0e4cbcd...

compiler/rustc_abi/src/layout.rs Show resolved Hide resolved
@@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented out code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@bors
Copy link
Contributor

bors commented Nov 24, 2024

☀️ Try build successful - checks-actions
Build commit: 0e4cbcd (0e4cbcded5bee0aabf1c9b10281afc7b466a961d)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0e4cbcd): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking 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
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This 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.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.5% [3.5%, 3.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 797.356s -> 796.07s (-0.16%)
Artifact size: 336.34 MiB -> 336.30 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 24, 2024
@bors
Copy link
Contributor

bors commented Dec 27, 2024

☔ The latest upstream changes (presumably #134822) made this pull request unmergeable. Please resolve the merge conflicts.

@the8472 the8472 force-pushed the randomize-me-harder branch from 5d44eda to a9ab5dd Compare December 27, 2024 17:25
@workingjubilee workingjubilee self-requested a review January 4, 2025 17:47
tests/ui/layout/thumb-enum.stderr Outdated Show resolved Hide resolved
tests/ui/layout/issue-96185-overaligned-enum.stderr Outdated Show resolved Hide resolved
compiler/rustc_abi/src/lib.rs Outdated Show resolved Hide resolved
@workingjubilee workingjubilee added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 4, 2025
@the8472 the8472 force-pushed the randomize-me-harder branch 2 times, most recently from 3b702c1 to 113e6b3 Compare January 8, 2025 23:52
@the8472
Copy link
Member Author

the8472 commented Jan 8, 2025

I have updated the randomization UI test and added some cases that people on zulip were concerned about.

@the8472 the8472 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 9, 2025
tests/ui/layout/randomize.rs Outdated Show resolved Hide resolved
Copy link
Member

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...)

Copy link
Member Author

@the8472 the8472 Jan 9, 2025

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.

Copy link
Member Author

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.

Copy link
Member

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/layout.rs Show resolved Hide resolved
@@ -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),
Copy link
Member

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"

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member

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
@the8472 the8472 force-pushed the randomize-me-harder branch from 113e6b3 to d89b6d5 Compare January 10, 2025 01:23
@workingjubilee
Copy link
Member

thank you! the impl is a bit spread out but the overarching strategy being documented should hopefully prevent that from desyncing.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 10, 2025

📌 Commit d89b6d5 has been approved by workingjubilee

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jan 10, 2025

🌲 The tree is currently closed for pull requests below priority 1000. This pull request will be tested once the tree is reopened.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jan 10, 2025
…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.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 10, 2025
…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
@bors bors merged commit eaf4206 into rust-lang:master Jan 10, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 10, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 10, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants