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

Don't perform invalid checks in codegen_attrs #105605

Merged
merged 3 commits into from
Dec 26, 2022

Conversation

inquisitivecrystal
Copy link
Contributor

@inquisitivecrystal inquisitivecrystal commented Dec 12, 2022

The attributes #[track_caller] and #[cmse_nonsecure_entry] are only valid on functions. When validating one of these attributes, codegen_attrs previously called fn_sig, which can only be used on functions, on the item the attribute was attached to, assuming that the item was a function without checking. This led to ICEs in situations where the attribute was incorrectly used on non-functions.

With this change, we skip calling fn_sig if the item the attribute is attached to must be a function but isn't, because check_attr will reject such cases without codegen_attrs's intervention.

As a side note, some of the attributes in codegen_attrs are only valid on functions, but that property isn't actually checked. I'm planning to fix that in a follow up PR since it's a behavior change that will need to be validated rather than an obvious bugfix. Thankfully, all the attributes like that I've found so far are unstable.

Fixes #105594.

r? @cjgillot

@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 Dec 12, 2022
@bors
Copy link
Contributor

bors commented Dec 14, 2022

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

@inquisitivecrystal inquisitivecrystal changed the title Don't perform invalid checks in collect Don't perform invalid checks in codegen_attrs Dec 14, 2022
Some attributes are only valid on function items. When checking these
attributes, codegen_attrs previously sometimes called `fn_sig` on the
item they were attached to without first ensuring that the item was a
function. This led to an ICE (rust-lang#105594), since `fn_sig` can
only be called on functions.

After this change, we skip calling `fn_sig` if the item the attribute is
attached to must be a function but invalidly isn't, because `check_attr`
will reject such cases without codegen_attrs's intervention.
@inquisitivecrystal
Copy link
Contributor Author

inquisitivecrystal commented Dec 16, 2022

There's a bit of a backwards compatibility hazard here, in that #[track_caller] is currently allowed on macro definitions, match arms, and struct fields for legacy reasons (as in, it shouldn't be valid, but we issue a warning rather than an error to avoid breaking old code). This change doesn't seem to be breaking the legacy cases right now, presumably because this function is never called on them. Macros don't need codegen, and struct fields and match arms aren't items. Still, should I handle the legacy case somehow?

@cjgillot
Copy link
Contributor

There's a bit of a backwards compatibility hazard here, in that #[track_caller] is currently allowed on macro definitions, match arms, and struct fields for legacy reasons (as in, it shouldn't be valid, but we issue a warning rather than an error to avoid breaking old code). This change doesn't seem to be breaking the legacy cases right now, presumably because this function is never called on them. Macros don't need codegen, and struct fields and match arms aren't items. Still, should I handle the legacy case somehow?

The assertion def_kind.has_codegen_attrs at the beginning of the query should be enough to avoid triggering these cases.

@bors r+

@bors
Copy link
Contributor

bors commented Dec 25, 2022

📌 Commit 47b6426 has been approved by cjgillot

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 Dec 25, 2022
@bors
Copy link
Contributor

bors commented Dec 26, 2022

⌛ Testing commit 47b6426 with merge c29f2720036ecf11a7b77c6e666da43436e59cf1...

@bors
Copy link
Contributor

bors commented Dec 26, 2022

💔 Test failed - checks-actions

@bors bors 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. labels Dec 26, 2022
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@cjgillot
Copy link
Contributor

@bors retry

@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 Dec 26, 2022
@bors
Copy link
Contributor

bors commented Dec 26, 2022

⌛ Testing commit 47b6426 with merge f206533...

@bors
Copy link
Contributor

bors commented Dec 26, 2022

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing f206533 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 26, 2022
@bors bors merged commit f206533 into rust-lang:master Dec 26, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 26, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f206533): 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.3% [2.1%, 2.5%] 2
Improvements ✅
(primary)
-1.4% [-1.4%, -1.4%] 1
Improvements ✅
(secondary)
-3.1% [-3.7%, -2.7%] 3
All ❌✅ (primary) -1.4% [-1.4%, -1.4%] 1

@inquisitivecrystal inquisitivecrystal deleted the attr-validation branch December 26, 2022 23:49
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…r=cjgillot

Don't perform invalid checks in `codegen_attrs`

The attributes `#[track_caller]` and `#[cmse_nonsecure_entry]` are only valid on functions. When validating one of these attributes, codegen_attrs previously called `fn_sig`, [which can only be used on functions](rust-lang#105201), on the item the attribute was attached to, assuming that the item was a function without checking. This led to [ICEs in situations where the attribute was incorrectly used on non-functions](rust-lang#105594).

With this change, we skip calling `fn_sig` if the item the attribute is attached to must be a function but isn't, because `check_attr` will reject such cases without codegen_attrs's intervention.

As a side note, some of the attributes in codegen_attrs are only valid on functions, but that property isn't actually checked. I'm planning to fix that in a follow up PR since it's a behavior change that will need to be validated rather than an obvious bugfix. Thankfully, all the attributes like that I've found so far are unstable.

Fixes rust-lang#105594.

r? `@cjgillot`
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.

ICE when #[track_caller] is applied to a static item
6 participants