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

Fix leaks from panics in destructors #125923

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

matthewjasper
Copy link
Contributor

Resurrects #78373.

This avoids the problem with #80949 by not unscheduling drops of function arguments until after the call (so they still get a drop terminator on the function unwind path).

Closes #47949

r? @lcnr

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 3, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2024

Some changes occurred in match lowering

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Jun 4, 2024

@bors try @rust-timer queue

r? @pnkfelix (as you've reviewed #78373 3 years ago :3) am looking through these changes myself but would like you to also take a look

@rust-timer

This comment has been minimized.

@rustbot rustbot assigned pnkfelix and unassigned lcnr Jun 4, 2024
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 4, 2024
@bors
Copy link
Contributor

bors commented Jun 4, 2024

⌛ Trying commit 28ac9be with merge 1c34390...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 4, 2024
Fix leaks from panics in destructors

Resurrects rust-lang#78373.

This avoids the problem with rust-lang#80949 by not unscheduling drops of function arguments until after the call (so they still get a drop terminator on the function unwind path).

Closes rust-lang#47949

r? `@lcnr`
@lcnr
Copy link
Contributor

lcnr commented Jun 4, 2024

Went through it and mostly understand this PR, am very much not comfortable enough to approve it myself 😅

Thanks @matthewjasper for picking this up again ❤️ really happy about this

@bors
Copy link
Contributor

bors commented Jun 4, 2024

☀️ Try build successful - checks-actions
Build commit: 1c34390 (1c343902c7b4004d4dcdeb3cc93fcdd886e21595)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1c34390): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.5% [0.3%, 12.1%] 245
Regressions ❌
(secondary)
38.2% [0.3%, 723.1%] 156
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.5% [0.3%, 12.1%] 245

Max RSS (memory usage)

Results (primary -1.8%, secondary -4.0%)

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.8% [0.8%, 5.9%] 7
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.7% [-8.7%, -0.9%] 36
Improvements ✅
(secondary)
-4.0% [-6.0%, -2.2%] 28
All ❌✅ (primary) -1.8% [-8.7%, 5.9%] 43

Cycles

Results (primary 2.4%, secondary 73.0%)

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% [0.9%, 6.3%] 48
Regressions ❌
(secondary)
73.0% [1.2%, 281.7%] 33
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.4% [0.9%, 6.3%] 48

Binary size

Results (primary 1.1%, secondary 1.3%)

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)
1.1% [0.0%, 4.9%] 136
Regressions ❌
(secondary)
1.3% [0.1%, 7.7%] 91
Improvements ✅
(primary)
-0.1% [-0.1%, -0.1%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [-0.1%, 4.9%] 138

Bootstrap: 673.596s -> 675.825s (0.33%)
Artifact size: 318.88 MiB -> 311.92 MiB (-2.19%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 4, 2024
@matthewjasper
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 14, 2024
Large arrays/tuples can have enough operands that removing items one at
a time is significantly slower than creating a hash set first.
When building the MIR we sometimes try to unschedule drops. In this we
assert that the drop has already been scheduled. Opaque types however
may be initialized with an expression kind that we know doesn't have a
type that needs to be dropped. To fix this we don't panic if we can't
find the drop of a variable with an opaque type.
@lcnr
Copy link
Contributor

lcnr commented Jul 12, 2024

(will take a day to check this out locally and fully understand these changes in 1-2 weeks if nobody else tries to review it by then)

It looks like the crater run still detected some ICE?

@matthewjasper
Copy link
Contributor Author

Yes, the push 3 days ago was a fix and test for that ICE

@saethlin
Copy link
Member

I really thought that given some time over the weekend I'd be able to review this, but no, this part of the compiler is too unfamiliar to me.

r? lcnr

@rustbot rustbot assigned lcnr and unassigned saethlin Jul 13, 2024
@lcnr
Copy link
Contributor

lcnr commented Jul 15, 2024

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 15, 2024
Fix leaks from panics in destructors

Resurrects rust-lang#78373.

This avoids the problem with rust-lang#80949 by not unscheduling drops of function arguments until after the call (so they still get a drop terminator on the function unwind path).

Closes rust-lang#47949

r? `@lcnr`
@bors
Copy link
Contributor

bors commented Jul 15, 2024

⌛ Trying commit 3e0db6a with merge b905bf4...

@bors
Copy link
Contributor

bors commented Jul 15, 2024

☀️ Try build successful - checks-actions
Build commit: b905bf4 (b905bf41e43f8442243991ba81032e62a0c5a3f1)

@lcnr
Copy link
Contributor

lcnr commented Jul 15, 2024

@craterbot
Copy link
Collaborator

👌 Experiment pr-125923-2 created and queued.
🤖 Automatically detected try build b905bf4
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2024
@craterbot
Copy link
Collaborator

🚧 Experiment pr-125923-2 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-125923-2 is completed!
📊 0 regressed and 0 fixed (19 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jul 17, 2024
@@ -1141,6 +1142,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
{
return;
}
// Opaque type may not have been scheduled if its underlying
// type does not need drop.
None if self.local_decls[local].ty.has_opaque_types() => return,

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

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

so the affected code is

fn if_no_else(c: bool) -> impl Sized {
    if c {}
}

where the opaque type gets unified with () and () does not need drop while impl Sized does? 🤔 we probably never emit drop for the return value of an if without an else block or sth?

} else if let Some(destination_local) = destination.as_local()
&& let Some(scope) = scope
{
this.schedule_drop(span, scope, destination_local, DropKind::Value);
Copy link
Contributor

@lcnr lcnr Aug 14, 2024

Choose a reason for hiding this comment

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

this is necessary to avoid ICEing when trying to unschedule the drop of a previous match arm here https:/matthewjasper/rust/blob/3e0db6a36f01a5a8978ced19e0b83ebca5cd103c/compiler/rustc_mir_build/src/build/matches/mod.rs#L480-L484

however, this is not enough as we don't schedule a drop if the destination_ty is an opaque

fn test_me() -> impl Sized {
    match 1 {
        1 => 'ret: {
            break 'ret test_me();
        }
        _ => {},
    };
}

fn main() {}

It works if the destination_ty is () as in this case unschedule_drop is a noop as needs_drop returns false

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

I am very confused by expr_into_dest.

Ignoring that opaque types require drop even if their hidden type don't means that the destination of stuff like ExprKind::Borrow would now add a drop, and more importantly, tries to remove a drop in unschedule_drop, expr_into_dest has to schedule the drop for dest to be the last drop in the given scope. If it does not, we just ICE when trying to unschedule the drop for the destination in matches/if else.

I've added an assert that stuff could get unscheduled and it always passes, see a57f57b

@lcnr
Copy link
Contributor

lcnr commented Aug 14, 2024

stopped my review for now in case you have thoughts here as my next steps would probably be to attempt to significantly restructure the code to see if it works 😅

I feel like the current setup is very prone to break in the future (and also very sus wrt to opaque types, which is more an opaque type issue than an issue with your impl)

While reviewing I've made some changes to document/rename things which were confusing to me, the important commit is here 4ba5010

@lcnr
Copy link
Contributor

lcnr commented Aug 14, 2024

@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 Aug 14, 2024
@alex-semenyuk alex-semenyuk 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panics in destructors can cause the return value to be leaked