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

Strengthen validation of FFI attributes #107257

Merged
merged 3 commits into from
Feb 1, 2023

Conversation

inquisitivecrystal
Copy link
Contributor

@inquisitivecrystal inquisitivecrystal commented Jan 24, 2023

Previously, codegen_attrs validated the attributes #[ffi_pure], #[ffi_const], and #[ffi_returns_twice] to make sure that they were only used on foreign functions. However, this validation was insufficient in two ways:

  1. codegen_attrs only sees items for which code must be generated, so it was unable to raise errors when the attribute was incorrectly applied to macros and the like.
  2. the validation code only checked that the item with the attr was foreign, but not that it was a foreign function, allowing these attributes to be applied to foreign statics as well.

This PR moves the validation to check_attr, which sees all items. It additionally changes the validation to ensure that the attribute's target is Target::ForeignFunction, only allowing the attributes on foreign functions and not foreign statics. Because these attributes are unstable, there is no risk for backwards compatibility. The changes also ending up making the code much easier to read.

This PR is best reviewed commit by commit. Additionally, I was considering moving the tests to the attribute subdirectory, to get them out of the general UI directory. I could do that as part of this PR or a follow-up, as the reviewer prefers.

CC: #58328, #58329

Previously, we verified that FFI attrs were used on foreign items,
but this allowed them on both foreign functions and foreign statics.
This change only allows them on foreign functions.
@rustbot
Copy link
Collaborator

rustbot commented Jan 24, 2023

r? @nagisa

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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 Jan 24, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 24, 2023

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

@inquisitivecrystal inquisitivecrystal added the A-FFI Area: Foreign function interface (FFI) label Jan 24, 2023
@bryangarza
Copy link
Contributor

Not an official reviewer, but I looked through the commits and it looks good to me.

Additionally, I was considering moving the tests to the attribute subdirectory, to get them out of the general UI directory. I could do that as part of this PR or a follow-up, as the reviewer prefers.

I think it'd be better if you move them into the attribute dir as part of this PR so if someone is looking at the commit for the tests, they can git diff that commit hash and get the rest of the code changes too

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

LGTM, but this is technically a breaking change, so I think we'll want to do a crater run to see how safe this is to land.

@davidtwco
Copy link
Member

r? @davidtwco

@rustbot rustbot assigned davidtwco and unassigned nagisa Jan 27, 2023
@davidtwco
Copy link
Member

@bors try

@bors
Copy link
Contributor

bors commented Jan 27, 2023

⌛ Trying commit 0983ac8b4283fec01e83fe3d41cf3add0597691a with merge cc7a22a8fa04e430abcb30a0b627b66011dd0ec3...

@inquisitivecrystal
Copy link
Contributor Author

LGTM, but this is technically a breaking change, so I think we'll want to do a crater run to see how safe this is to land.

All these attributes are still unstable, so it shouldn't be a problem? Unless we're worried about breaking nightly code.

@davidtwco
Copy link
Member

LGTM, but this is technically a breaking change, so I think we'll want to do a crater run to see how safe this is to land.

All these attributes are still unstable, so it shouldn't be a problem? Unless we're worried about breaking nightly code.

Oh, I should have checked that, great - that's much simpler.

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 27, 2023

📌 Commit 0983ac8b4283fec01e83fe3d41cf3add0597691a has been approved by davidtwco

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 Jan 27, 2023
@inquisitivecrystal
Copy link
Contributor Author

inquisitivecrystal commented Jan 27, 2023

Oh, I should have checked that, great - that's much simpler.

I did mention it in the OP, but I don't blame you for missing it - I'm not the most concise. Thanks for the review! ❤️

@bors
Copy link
Contributor

bors commented Jan 27, 2023

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing cc7a22a8fa04e430abcb30a0b627b66011dd0ec3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 27, 2023
@bors
Copy link
Contributor

bors commented Jan 27, 2023

👀 Test was successful, but fast-forwarding failed: 422 Update is not a fast forward

@compiler-errors
Copy link
Member

For future reference, do not approve a PR when a try-build is running, or else it will try to be merged into master 😬

@davidtwco
Copy link
Member

For future reference, do not approve a PR when a try-build is running, or else it will try to be merged into master 😬

Oh, oops, hadn't done that before. My bad.

@inquisitivecrystal
Copy link
Contributor Author

I'm going to force push and then close and reopen to reset things (at least, those are the steps I've seen before; not sure which of the two is required, but doing both can't hurt).

@inquisitivecrystal
Copy link
Contributor Author

I think if someone approves again we should be set? The change to the last commit only affected the commit message.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cc7a22a8fa04e430abcb30a0b627b66011dd0ec3): 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)

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

Cycles

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)
2.7% [2.3%, 3.1%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

@inquisitivecrystal
Copy link
Contributor Author

@davidtwco: could you r+ this again?

@inquisitivecrystal inquisitivecrystal added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. merged-by-bors This PR was explicitly merged by bors. labels Jan 30, 2023
@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jan 31, 2023

📌 Commit bc23e9a has been approved by davidtwco

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 Jan 31, 2023
@bors
Copy link
Contributor

bors commented Feb 1, 2023

⌛ Testing commit bc23e9a with merge 11d96b5...

@bors
Copy link
Contributor

bors commented Feb 1, 2023

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing 11d96b5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 1, 2023
@bors bors merged commit 11d96b5 into rust-lang:master Feb 1, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 1, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (11d96b5): 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)
2.4% [2.4%, 2.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.1%, -3.0%] 2
All ❌✅ (primary) 2.4% [2.4%, 2.4%] 1

Cycles

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

@inquisitivecrystal inquisitivecrystal deleted the ffi-attr branch February 1, 2023 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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.

8 participants