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

Make compressed rmeta contain compressed data length after header #101550

Merged
merged 6 commits into from
Mar 5, 2023

Conversation

CraftSpider
Copy link
Contributor

@CraftSpider CraftSpider commented Sep 7, 2022

Fixes #90056, which is caused by link.exe introducing padding to the .rustc section, since it assumes this will have no effect besides allowing it to possibly use the extra space in future links.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 7, 2022
@rust-highfive
Copy link
Collaborator

r? @fee1-dead

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 7, 2022
@fee1-dead
Copy link
Member

r? rust-lang/compiler

@rust-highfive rust-highfive assigned oli-obk and unassigned fee1-dead Sep 8, 2022
@ChrisDenton
Copy link
Member

Can rust not be made more resilient to zero padding in .rustc so that it does indeed have no effect? Fighting linker expectations feels wrong even if there's a workaround.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 8, 2022

r? @ehuss (commented on the issue)

@rust-highfive rust-highfive assigned ehuss and unassigned oli-obk Sep 8, 2022
@CraftSpider
Copy link
Contributor Author

It may be possible to do that, but I'm not sure it's the best path forwards - I imagine it would effectively be removing some sanity checks on the length of the compressed data, and instead just decompressing the first N bytes. This is only a guess though, as I'm not actually incredibly familiar with how rustc does its compressed metadata writing.

@CraftSpider
Copy link
Contributor Author

I also feel I should note that, as far as I'm aware, either only the Windows linker does this, or we already tell other linkers not to. I wouldn't consider this change to be working against linker expectations in general, just disabling a slightly odd behavior on Windows (I wouldn't think adding extra null bytes to unrecognized sections is, in general, a safe thing to do)

@bjorn3
Copy link
Member

bjorn3 commented Sep 10, 2022

Maybe only disable it for the dylib and proc-macro crate types? bin and cdylib don't have crate metadata.

@bjorn3
Copy link
Member

bjorn3 commented Sep 10, 2022

On the other hand there are crates which use custom sections that will likely break with padding too. For example inventory.

@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2022

I am almost certainly not the right person to review this. I do not know what the implications are of disabling incremental linking. Is there anyone on the compiler team familiar with linking (link.exe especifically) and rust's metadata?

I also wonder if a better solution would be to track the length of the compressed data instead of assuming the section data length is correct? The METADATA_HEADER already lives outside of the compressed stream. It could be extended to include the metadata length (it already has a position offset which gets filled in after encoding).

It also seems a little surprising that the linker is padding a section. I wonder if it is related to this:

The size of this data in the file is indicated by the SizeOfRawData field. If SizeOfRawData is less than VirtualSize, the remainder is padded with zeros.

Perhaps they always feel at liberty to pad things?

@bjorn3
Copy link
Member

bjorn3 commented Sep 10, 2022

The METADATA_HEADER already lives outside of the compressed stream. It could be extended to include the metadata length (it already has a position offset which gets filled in after encoding).

Only the literal represented by METADATA_HEADER (rust\0x00\0x00\0x00\0x06) has a copy outside of the compressed stream. No other part of the header is outside of the compressed stream. That includes the crate root offset and the rustc version. Both are inside the compressed stream after another copy of METADATA_HEADER.

let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap();

Hex dump of section '.rustc':
  0x00000000 72757374 00000006 ff060000 734e6150 rust........sNaP
  0x00000010 7059000d 9f00e4dc e1798080 04307275 pY.......y...0ru

@ehuss
Copy link
Contributor

ehuss commented Sep 10, 2022

Ah, I misread that about the position. But I assume those lines could be changed to something like:

let mut compressed = rustc_metadata::METADATA_HEADER.to_vec(); 
compressed.write_all(length).unwrap();
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap(); 

where length is the length of metadata.raw_data() encoded in some format (like a 32-bit big endian or something)?

@bjorn3
Copy link
Member

bjorn3 commented Sep 10, 2022

That should work. Rust-analyzer will need to be updated to reflect this in the proc macro loading code though.

@CraftSpider
Copy link
Contributor Author

I'll work on making the necessary changes, since it seems like consensus is that supporting this is worth the change. Should I make a PR to rust-analyzer in sync with this change?

@bjorn3
Copy link
Member

bjorn3 commented Sep 11, 2022

Rust-analyzer is now a subtree, so you could make the changes in the same PR to this repo. Make sure to keep support for the current format though.

@apiraino
Copy link
Contributor

By reading the latest comments it seems this PR will be waiting on work happening on rust-analyzer. I'll signal this with s-waiting-on-author - feel free to request a review of his PR again with @rustbot ready, thanks!

@rustbot author

@rustbot rustbot 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 Oct 13, 2022
@pnkfelix
Copy link
Member

r? @wesleywiser

@rust-highfive rust-highfive assigned wesleywiser and unassigned ehuss Oct 13, 2022
@eddyb
Copy link
Member

eddyb commented Oct 13, 2022

Ah, I misread that about the position. But I assume those lines could be changed to something like:

let mut compressed = rustc_metadata::METADATA_HEADER.to_vec(); 
compressed.write_all(length).unwrap();
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap(); 

where length is the length of metadata.raw_data() encoded in some format (like a 32-bit big endian or something)?

@ehuss @CraftSpider If you do this change, don't forget to bump the (small) version number in METADATA_HEADER, otherwise the two encodings will get confused for one another (i.e. we try to guarantee that mixing versions of rustc, say stable and nightly, in the same build dir, will not cause unpredictable failure modes due to format mixing - that's why we have a "major" format version in METADATA_HEADER on top of the "rustc version" string later on, because the version string couldn't be correctly found if we change fundamental aspects of the encoding).

@CraftSpider
Copy link
Contributor Author

I'll make sure to do so, I did start on implementing this change. I'll push an update once I have something working hopefully.

@Dylan-DPC
Copy link
Member

@CraftSpider any updates on this?

@rustbot rustbot 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 Feb 20, 2023
Copy link
Member

@wesleywiser wesleywiser left a comment

Choose a reason for hiding this comment

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

Compiler changes look good to me!

@wesleywiser
Copy link
Member

@Veykril or @lnicola, would you mind double checking that the r-a changes are ok? Thanks! 🙂

@lnicola
Copy link
Member

lnicola commented Feb 23, 2023

The rust-analyzer changes were merged in rust-lang/rust-analyzer#14153, but we'll double-check once this hits nightly.

@wesleywiser wesleywiser 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 Feb 24, 2023
@CraftSpider
Copy link
Contributor Author

@rustbot label: -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot 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 Feb 28, 2023
@wesleywiser
Copy link
Member

Thanks @CraftSpider!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 2, 2023

📌 Commit 79f0021 has been approved by wesleywiser

It is now in the queue for this repository.

@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 Mar 2, 2023
@matthiaskrgr
Copy link
Member

@bors rollup=never let's get this out of the queue

@bors
Copy link
Contributor

bors commented Mar 5, 2023

⌛ Testing commit 79f0021 with merge a512c6c...

@bors
Copy link
Contributor

bors commented Mar 5, 2023

☀️ Test successful - checks-actions
Approved by: wesleywiser
Pushing a512c6c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 5, 2023
@bors bors merged commit a512c6c into rust-lang:master Mar 5, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 5, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (a512c6c): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

Results

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)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.3%, -3.3%] 1
All ❌✅ (primary) - - 0

Cycles

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

lnicola pushed a commit to lnicola/rust that referenced this pull request Mar 13, 2023
…leywiser

Make compressed rmeta contain compressed data length after header

Fixes rust-lang#90056, which is caused by link.exe introducing padding to the `.rustc` section, since it assumes this will have no effect besides allowing it to possibly use the extra space in future links.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

-Clink-dead-code causes extern crate failure_derive to not be found on Windows