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

Added -Z randomize-layout flag #87868

Merged
merged 1 commit into from
Oct 1, 2021
Merged

Conversation

Kixiron
Copy link
Member

@Kixiron Kixiron commented Aug 8, 2021

An implementation of #77316, it currently randomly shuffles the fields of repr(rust) types based on their DefPathHash
r? @eddyb

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @eddyb (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 8, 2021
@Kixiron
Copy link
Member Author

Kixiron commented Aug 8, 2021

@rustbot label +A-layout +T-compiler

@rustbot rustbot added A-layout Area: Memory layout of types T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 8, 2021
@Lokathor
Copy link
Contributor

So this makes structs bigger than the size of the sum of their fields?

Particularly, this makes structs with a single field bigger than that one field?

@chorman0773
Copy link
Contributor

chorman0773 commented Aug 26, 2021 via email

@Lokathor
Copy link
Contributor

On the one hand yes, on the other hand this flies against the current best estimation of the UCG.

I am aware it isn't RFC'd, but I'm not sure that part of field randomization is a good idea even if there's no RFC.

@the8472
Copy link
Member

the8472 commented Aug 26, 2021

It could be limited to structs with more than 1 non-zst fields of different types. Of course that depends on what exactly the UCG mean by "homogenous structs", which is still an open question.
We could be conservative and look for fields of different size or alignment, but that wouldn't catch cases where people try to transmute between Vecs for example where the only internal difference is Unique<T> vs. Unique<U>.

@Lokathor
Copy link
Contributor

Primarily I'm mostly concerned about single field structs. People should be able to, in general, trust their newtypes. Like even if you forget to specifically write down repr(transparent), rust has no legitimate reason to break the layout of a single field struct.

Secondarily I'm slightly concerned about situations with multi-field structs where this introduces padding bytes that were not present within the struct previously. Re-ordering fields so that the structure doesn't get bigger and have padding bytes added I'm all for, but if there is a Point(i32,i32) people should be able to look at that and assume no padding in the structure.

Though ultimately this is a debug sort of flag so it's not the end of the world if turning it on makes things go wild, it's better if turning on the flag doesn't trigger UB and make the entire debug attempt pointless because there's UB in the program.

@chorman0773
Copy link
Contributor

chorman0773 commented Aug 26, 2021 via email

@Lokathor
Copy link
Contributor

Sure, transmute itself will error, but other forms of transmutation will not. For example, the most popular crate for safe transmutation abstraction (bytemuck) doesn't even use the transmute function at all, because that function doesn't work with generics.

@chorman0773
Copy link
Contributor

chorman0773 commented Aug 26, 2021 via email

@Lokathor
Copy link
Contributor

Yes, that is what's documented in bytemuck, but people have misused it before despite the docs. Then they come to me and say "why did your library break my code?" and I have to look close at their program and point out what's wrong and so on.

Also, that's just one example to show that transmute itself isn't the only thing used to do transmutation.

My general point is that while some field randomization is probably fine we should not do too much of it and give trouble to people who are living in a gray zone.

@chorman0773
Copy link
Contributor

Well, the idea behind the flag is to find code that relies on something it shouldn't. It's designed to be opt-in, and I'm sure that someone was transmuting things (not via mem::transmute), and used this flag in conjunction with miri, they'd get a very loud error.

@Kixiron Kixiron changed the title Added -Z randomize-layout flag and basic offset increases Added -Z randomize-layout flag Aug 26, 2021
@Kixiron
Copy link
Member Author

Kixiron commented Sep 12, 2021

@eddyb This is ready for review, compiler-team/#457 was accepted

@bors

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Kixiron Kixiron force-pushed the packing-on-the-pounds branch 2 times, most recently from 34d896d to b60afed Compare September 30, 2021 19:35
@rust-log-analyzer

This comment has been minimized.

@eddyb
Copy link
Member

eddyb commented Sep 30, 2021

Under the assumption that the MCP (rust-lang/compiler-team#457) was enough:
@bors r+

@bors
Copy link
Contributor

bors commented Sep 30, 2021

📌 Commit 09f1542 has been approved by eddyb

@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 Sep 30, 2021
fee1-dead added a commit to fee1-dead-contrib/rust that referenced this pull request Oct 1, 2021
Added -Z randomize-layout flag

An implementation of rust-lang#77316, it currently randomly shuffles the fields of `repr(rust)` types based on their `DefPathHash`
r? `@eddyb`
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 1, 2021
…arth

Rollup of 6 pull requests

Successful merges:

 - rust-lang#87868 (Added -Z randomize-layout flag)
 - rust-lang#88820 (Add `pie` as another `relocation-model` value)
 - rust-lang#89029 (feat(rustc_parse): recover from pre-RFC-2000 const generics syntax)
 - rust-lang#89322 (Reapply "Remove optimization_fuel_crate from Session")
 - rust-lang#89340 (Improve error message for `printf`-style format strings)
 - rust-lang#89415 (Correct caller/callsite confusion in inliner message)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 37df275 into rust-lang:master Oct 1, 2021
@rustbot rustbot added this to the 1.57.0 milestone Oct 1, 2021
@Kixiron Kixiron deleted the packing-on-the-pounds branch November 8, 2021 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layout Area: Memory layout of types 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.