Skip to content

Read version of rustc that compiled proc macro #6822

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

Merged
merged 8 commits into from
Mar 9, 2021
Merged

Read version of rustc that compiled proc macro #6822

merged 8 commits into from
Mar 9, 2021

Conversation

jsomedon
Copy link
Contributor

Signed-off-by: Jay Somedon [email protected]

This PR is to fix #6174.

I basically

  • added two methods, read_version and read_section(used by read_version)
  • two new crates snap and object to be used by those two methods

I just noticed that some part of code were auto-reformatted by rust-analyzer on file save. Does it matter?

snappy_decoder
.read_exact(&mut bytes_before_version)
.unwrap();
let length = bytes_before_version[12]; // what? can't use -1 indexing?
Copy link
Member

Choose a reason for hiding this comment

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

This should probably also check that the metadata version is 5.

Copy link
Member

Choose a reason for hiding this comment

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

Or rather, it should be checked before decompression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean, the last byte 5 in 8 bytes sequence r u s t 0 0 0 5?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is resolved by the condition check if header != EXPECTED_HEADER {...} that I added in later commit? @bjorn3

Copy link
Member

Choose a reason for hiding this comment

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

I think this is resolved by the condition check if header != EXPECTED_HEADER {...} that I added in later commit?

Yes, I think so .

@lnicola
Copy link
Member

lnicola commented Dec 11, 2020

There were some changes in this crate (dummy is now gone), so you should try to rebase in some way or another. And maybe try to avoid unwraps just in case. You can probably return io::Error like in #6817.

@jsomedon
Copy link
Contributor Author

If I keep add commit to this branch on my laptop then push them to my fork on github, do those change get automatically updated here?

@lnicola
Copy link
Member

lnicola commented Dec 11, 2020

If I keep add commit to this branch on my laptop then push them to my fork on github, do those change get automatically updated here?

Yes

@jsomedon
Copy link
Contributor Author

If I keep add commit to this branch on my laptop then push them to my fork on github, do those change get automatically updated here?

Yes

noice!

@lnicola
Copy link
Member

lnicola commented Dec 11, 2020

See #6393 (comment) if some rebasing tips, but it's fine if you don't feel like trying that.

@jsomedon
Copy link
Contributor Author

See #6393 (comment) if some rebasing tips, but it's fine if you don't feel like trying that.

👀

@jsomedon
Copy link
Contributor Author

jsomedon commented Dec 11, 2020

And maybe try to avoid unwraps just in case. You can probably return io::Error like in #6817.

I see. Is it ok to use ?? EDIT: I think it's okay, I saw bunch of ? use case in that link. 😁

@lnicola
Copy link
Member

lnicola commented Dec 11, 2020

It's ok, but you'll need to use a bunch of .map-err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?.

@matklad
Copy link
Member

matklad commented Dec 11, 2020

r? @jonas-schievink :)

@lnicola
Copy link
Member

lnicola commented Dec 20, 2020

@jsomedon hey, do you need help with the rebase?

@jsomedon
Copy link
Contributor Author

@jsomedon hey, do you need help with the rebase?

My bad for disappearing without words.. I caught the flu so I was mostly taking rest these days instead of wrestling with rust code. Probably shouldn't skip sleeping 😓 I haven't look much onto what I need for rebasing but I guess I should cherry pick some commits just like you did in the comment you linked?

@lnicola
Copy link
Member

lnicola commented Dec 20, 2020

Yes, you can cherry-pick the first one.

No worries, just shout if you need help.

@lnicola
Copy link
Member

lnicola commented Dec 21, 2020

@bjorn3 do you think this is worth the complexity over reading the output of rustc -V?

@bjorn3
Copy link
Member

bjorn3 commented Dec 21, 2020

🤷

@jonas-schievink
Copy link
Contributor

I think it is – running rustc might not work or might invoke the wrong compiler when using project.json

@jsomedon
Copy link
Contributor Author

I just pushed commits to resolve issues mentioned in code review. Will work on the rebasing task.

// * [some more bytes that we don really care but still there] :-)
// Check this issue for more about the bytes layout:
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
fn read_version(&self, dylib_path: &Path) -> io::Result<String> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually getting called somewhere?

Copy link
Member

Choose a reason for hiding this comment

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

Not yet, I think. We need to call it before loading the library, around ProcMacroLibraryLibloading::open or above.

But we still need to figure out what version to expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, but there is this discussion.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it would still be nice to wire it up. I just didn't want to scare you off right from the start 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it would still be nice to wire it up. I just didn't want to scare you off right from the start grimacing

I see, sure thing. 😄

Copy link
Member

Choose a reason for hiding this comment

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

That won't quite work for beta and stable, as those don't show the date at which the branch forked from master (and thus abi compatibility) but the date at which they were built.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, right. We need three version strings, then?

I hate this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(confused and scratching my head)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So how do I get the version that I am comparing with for stable and beta? And is there universal way of doing this for all three including nightly?

Copy link
Member

Choose a reason for hiding this comment

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

We have some discussion in zulip that only stable is fine for now.

@lnicola
Copy link
Member

lnicola commented Dec 23, 2020

So I think the last ABI change was rust-lang/rust#74653, which means the earliest rustc we support is... the nightly from 2020-07-28?

@lnicola
Copy link
Member

lnicola commented Dec 23, 2020

rustc 1.47.0-nightly (6c8927b0c 2020-07-26) doesn't work
rustc 1.47.0-nightly (76e83339b 2020-07-27) works

@jsomedon
Copy link
Contributor Author

Just updated my local repo and github fork, then rebased my fix branch(jsomedon:check_version), then git push --forced. The git push includes copies of 5 previous commits and 1 new commit that removes one extra entry for object crate in Cargo.lock(probably added twice during rebase)

Copy link
Member

@edwin0cheng edwin0cheng left a comment

Choose a reason for hiding this comment

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

@jsomedon

Sorry for the delay. We want to push forward this PR for #7328 now.

I think current form is okay now (we could refactoring later on), and the miss piece of this PR is actually call read_version from ProcMacroLibraryLibloading::open and logging warning if the version is invalid.

If you are busy, I could co-author this PR if you want.

// * [some more bytes that we don really care but still there] :-)
// Check this issue for more about the bytes layout:
// https://github.com/rust-analyzer/rust-analyzer/issues/6174
fn read_version(&self, dylib_path: &Path) -> io::Result<String> {
Copy link
Member

Choose a reason for hiding this comment

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

We have some discussion in zulip that only stable is fine for now.

snappy_decoder
.read_exact(&mut bytes_before_version)
.unwrap();
let length = bytes_before_version[12]; // what? can't use -1 indexing?
Copy link
Member

Choose a reason for hiding this comment

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

I think this is resolved by the condition check if header != EXPECTED_HEADER {...} that I added in later commit?

Yes, I think so .

@jsomedon
Copy link
Contributor Author

jsomedon commented Mar 2, 2021

If you are busy, I could co-author this PR if you want.

Yeah, sure. 😅
I guess I should do something so that you have write access to this PR, or is it done by github automatically?
nvm I think I saw that "allow edit for maintainer" checkbox automatically checked on on this page

jsomedon and others added 4 commits March 4, 2021 09:11
* check metadata version
* use memmap
* use Result instead of unwrap

with Jay Somedon <[email protected]>
Co-authored-by: Jonas Schievink <[email protected]>
@edwin0cheng
Copy link
Member

@jsomedon basically I just rebased and add the missing part (print out warning if the version is too old) of your PR.

r? @lnicola

@bjorn3
Copy link
Member

bjorn3 commented Mar 4, 2021

There is a typo in the commit message: *built

@matklad
Copy link
Member

matklad commented Mar 9, 2021

LGTM

@edwin0cheng is this waiting for anything else?

@edwin0cheng
Copy link
Member

bors r+

@edwin0cheng
Copy link
Member

@jsomedon Thanks for the PR !

@bors
Copy link
Contributor

bors bot commented Mar 9, 2021

@bors bors bot merged commit 654313d into rust-lang:master Mar 9, 2021
@lnicola
Copy link
Member

lnicola commented Apr 8, 2021

Did the metadata format change recently?

proc-macro target/debug/deps/libderivative-0649283d66d689a0.so failed to find the given version. Reason: snappy: corrupt input (expected stream header but got unexpected chunk type byte 228)

image

@lnicola
Copy link
Member

lnicola commented Apr 8, 2021

Never mind, I ended up on an old toolchain. I guess that's another thing to check for :-(.

bors bot added a commit that referenced this pull request May 4, 2021
8716: Replace `memmap` with `memmap2` in `proc_macro_api` r=edwin0cheng a=memoryruins

#7522 did the same for `proc_macro_srv` before this usage of `memmap` was introduced to `proc_macro_api` in #6822 .

Something like [`cargo-deny`](https://github.com/EmbarkStudios/cargo-deny-action) could help prevent specific crates (and versions, licenses, etc) from being introduced into the crate tree, but that's unrelated to this pull request.



Co-authored-by: memoryruins <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check rustc version before loading a proc-macro
6 participants