Skip to content

Fix variable deallocation order in panic unwinding paths#149435

Open
sladyn98 wants to merge 9 commits intorust-lang:mainfrom
sladyn98:fix-panic-drop-ordering-rebased
Open

Fix variable deallocation order in panic unwinding paths#149435
sladyn98 wants to merge 9 commits intorust-lang:mainfrom
sladyn98:fix-panic-drop-ordering-rebased

Conversation

@sladyn98
Copy link
Contributor

@sladyn98 sladyn98 commented Nov 29, 2025

View all comments

This PR fixes a soundness bug where local variables are deallocated out of order during panic unwinding, allowing destructors to access freed memory. This violates Rust's safety guarantees and has caused real-world unsoundness in crates like generatively.

This PR removes the is_generator check and unconditionally emits StorageDead statements during unwinding for ALL functions, bringing non-generator behavior in line with generators. It ensures that during unwinding, when a local variable goes out of scope, its storage is properly marked as dead via StorageDead, allowing the borrow checker to enforce the
invariant that values must outlive their references even in panic paths.

Fixes #147875

@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 Nov 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@sladyn98
Copy link
Contributor Author

r? @dianne

@rustbot rustbot assigned dianne and unassigned wesleywiser Nov 29, 2025
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@dianne dianne left a comment

Choose a reason for hiding this comment

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

It looks like there's a bug causing an assertion failure when building the standard library. I've given it a look and offered a guess at what's causing it below. There's still work to do here beyond fixing that, though.

First, could you add a ui test to demonstrate that this fixes #147875? It looks like it might not yet, since the code for scheduling unwind drops on calls panicking looks unchanged.

Second, after verifying that this results in the correct borrow-checking behavior, we need to make sure that this change doesn't negatively affect codegen. Per the old comment on needs_cleanup, at least at the time it was written, LLVM didn't handle the unnecessary cleanup blocks and StorageDeads particularly well. If you can demonstrate with codegen tests that that's not an issue anymore, and perf isn't too bad, that might be all that's needed. But my expectation is that we'll have to get rid of or ignore the StorageDeads later in compilation (sometime after they serve their purpose in borrowck). Unless there's a reason to keep the StorageDeads around longer, my gut feeling is that this cleanup would be best as a post-borrowck MIR pass (maybe as part of CleanupPostBorrowck?), since then optimization passes can be done on cleaner MIR and we can test it works with MIR tests rather than codegen tests. Could you also add a test for this not affecting later stages of compilation? If you accomplish that by removing the unwind-path StorageDeads as part of a MIR pass, that'd be a mir-opt test.

Before you push again, you'll probably want to run the codegen and mir-opt tests to make sure the former is clean and to bless the latter. Regardless of what approach we take here, if we're changing how the MIR is built, there should be differences in the MIR building test output (part of the mir-opt suite).

View changes since this review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 4, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 4, 2025
@dianne
Copy link
Contributor

dianne commented Dec 4, 2025

Also, could you change the PR description? #147875 on its own doesn't allow destructors to access freed memory, it doesn't allow for the creation of dangling references, and I'm at least not aware of a safety guarantee that it violates. You should only get unsoundness out of it if you write unsafe code on the assumption that the borrow checker will enforce the relative drop order of locals that may have destructors and those that definitely don't. Of course, per language team decision, consistent drop order is a promise Rust would like to make. But it's not quite the same as the borrow-checker failing to ensure places outlive their references.

@sladyn98
Copy link
Contributor Author

sladyn98 commented Dec 5, 2025

So what i did was write this simple rust program panic drop.rs

 //@ compile-flags: -C no-prepopulate-passes

#![crate_type = "lib"]

#[no_mangle]
pub fn function_with_drops() {

    let _a = String::from("first");
    let _b = String::from("second");
    let _c = String::from("third");
    might_panic();
}

#[inline(never)]
pub fn might_panic() {
    // This might panic
    panic!()
}

I ran the llvm to get the intermediate representaion and on looking at the IR I cannot find any llvm.lifetime.end statements suggesting to us that on master the StorageDead statements are missing, which according to my understanding means that the borrowchecker does not know when the storage becomes invalid. Let me now write the UI test to see what is up

@dianne
Copy link
Contributor

dianne commented Dec 5, 2025

StorageDead is indeed not present there on the main branch, but that doesn't create a soundness hole in the borrow-checker; those locals are simply treated as being maybe live until the end of the stack frame, so it's technically sound for destructors on the unwind path to reference them. When unwinding, the stack frame will be popped anyway, so we don't need to hint to llvm that the memory's no longer in use. The issue is that we'd like to be more strict in the borrow-checker, both for consistency and so that unsafe code can rely on drop order being enforced.

edit: adjusted wording

@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from 5afe7c2 to 59a7e56 Compare January 30, 2026 09:32
@rustbot rustbot added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Jan 30, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@sladyn98 sladyn98 requested a review from dianne January 30, 2026 09:32
@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 Jan 30, 2026
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@dianne dianne left a comment

Choose a reason for hiding this comment

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

This still needs CI to pass before I can review it properly. I've left a few comments on obvious things, but I don't think reviewing the code changes would be helpful at this point. Please test your changes locally. You don't have to run the whole test suite yourself, but for this change, you'll at least want make sure that the mir-opt and codegen tests all pass, that any relevant ui tests pass, and that tidy passes as well.

Could you rebase onto a more recent commit, also? I don't expect there will be conflicts in the MIR building part of this, but I'm not sure about the rest.

I don't mean to be harsh, but this is a relatively complex and nuanced change. If you're not familiar with what's being changed, why it's being changed, the consequences/needs of that, and general contribution procedure, I'd recommend gaining familiarity with easier issues instead.

View changes since this review

@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 Jan 30, 2026
@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from 59a7e56 to 44fbdb3 Compare February 1, 2026 00:14
@rustbot
Copy link
Collaborator

rustbot commented Feb 1, 2026

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from d8a1764 to 2ed7b45 Compare February 9, 2026 05:55
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from e6b9a18 to 1556a29 Compare February 9, 2026 08:05
@rust-log-analyzer

This comment has been minimized.

@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from 1556a29 to 44b8dda Compare February 10, 2026 07:34
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@WaffleLapkin WaffleLapkin removed the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Feb 25, 2026
@rustbot rustbot added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Mar 1, 2026
@sladyn98 sladyn98 requested review from ShoyuVanilla and dianne March 1, 2026 03:28
@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 Mar 1, 2026
@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from 424738c to f05ca97 Compare March 1, 2026 03:29
sladyn98 and others added 9 commits February 28, 2026 19:31
This commit fixes several issues related to StorageDead and ForLint drops:

1. Add StorageDead and ForLint drops to unwind_drops for all functions
   - Updated diverge_cleanup_target to include StorageDead and ForLint drops
     in the unwind_drops tree for all functions (not just coroutines), but only
     when there's a cleanup path (i.e., when there are Value or ForLint drops)
   - This ensures proper drop ordering for borrow-checking on panic paths

2. Fix break_for_tail_call to handle StorageDead and ForLint drops
   - Don't skip StorageDead drops for non-drop types
   - Adjust unwind_to pointer for StorageDead and ForLint drops, matching
     the behavior in build_scope_drops
   - Only adjust unwind_to when it's valid (not DropIdx::MAX)
   - This prevents debug assert failures when processing drops in tail calls

3. Fix index out of bounds panic when unwind_to is DropIdx::MAX
   - Added checks to ensure unwind_to != DropIdx::MAX before accessing
     unwind_drops.drop_nodes[unwind_to]
   - Only emit StorageDead on unwind paths when there's actually an unwind path
   - Only add entry points to unwind_drops when unwind_to is valid
   - This prevents panics when there's no cleanup needed

4. Add test for explicit tail calls with StorageDead drops
   - Tests that tail calls work correctly when StorageDead and ForLint drops
     are present in the unwind path
   - Verifies that unwind_to is correctly adjusted for all drop kinds

These changes make the borrow-checker stricter and more consistent by ensuring
that StorageDead statements are emitted on unwind paths for all functions when
there's a cleanup path, allowing unsafe code to rely on drop order being enforced
consistently.
- Add StorageDead to unwind paths for all functions (not just coroutines)
- Modify CleanupPostBorrowck to remove StorageDead from cleanup blocks
- Add tests for the fix and StorageDead removal
When processing drops in reverse order, unwind_to might not point to
the current drop. Only adjust unwind_to when the drop matches what
unwind_to is pointing to, rather than asserting they must match.
Fix lifetime issues in rust-analyzer where automaton doesn't live long
enough for op.union(). Move op declaration inside each match arm to
ensure proper lifetime scope.

This fixes compilation errors that are blocking CI, though these are
pre-existing issues unrelated to the StorageDead changes.
When processing drops in reverse order, unwind_to might not point to the
current drop. Make the unwind_to adjustment conditional on the drop matching,
matching the behavior in build_scope_drops. This prevents assertion failures
when unwind_to points to a different drop than the one being processed.
- Remove conditional logic and optimization from diverge_cleanup_target
- Remove conditional logic from build_exit_tree
- Always add StorageDead when there are Value/ForLint drops
- Cleanup passes (CleanupPostBorrowck, RemoveNoopLandingPads) handle removal
- Fixes reviewer comments about code duplication and fragility
Simplify boolean expressions in scope.rs to fix clippy::needless_bool
lint failures, and update test expectations for dropck_trait_cycle_checked
and ctfe-arg-bad-borrow to match new StorageDead behavior.
@sladyn98 sladyn98 force-pushed the fix-panic-drop-ordering-rebased branch from f05ca97 to 3fd5fc6 Compare March 1, 2026 03:34
@rustbot
Copy link
Collaborator

rustbot commented Mar 1, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-gcc failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
/dev/sda15      105M  6.2M   99M   6% /boot/efi
tmpfs           1.6G   12K  1.6G   1% /run/user/1001
================================================================================

Sufficient disk space available (94917288KB >= 52428800KB). Skipping cleanup.
##[group]Run echo "[CI_PR_NUMBER=$num]"
echo "[CI_PR_NUMBER=$num]"
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
---
[RUSTC-TIMING] git2 test:false 16.723
error[E0597]: `counter` does not live long enough
   --> src/tools/cargo/src/cargo/sources/git/utils.rs:910:25
    |
864 |             let mut counter = MetricsCounter::<10>::new(0, last_update);
    |                 ----------- binding `counter` declared here
...
886 |             rcb.transfer_progress(|stats| {
    |                                   ------- value captured here
...
910 |                         counter.add(stats.received_bytes(), now);
    |                         ^^^^^^^ borrowed value does not live long enough
...
926 |         });
    |         -
    |         |
    |         `counter` dropped here while still borrowed
    |         borrow might be used here, when `rcb` is dropped and runs the destructor for type `RemoteCallbacks<'_>`
    |
    = note: values in a scope are dropped in the opposite order they are defined

For more information about this error, try `rustc --explain E0597`.
[RUSTC-TIMING] cargo test:false 15.604

For more information how to resolve CI failures of this job, visit this link.

Copy link
Contributor

@dianne dianne left a comment

Choose a reason for hiding this comment

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

Well, that's awkward. I took another look at the r-a error too, and I think it and the cargo error are the same thing: a Drop local is modified to reference a later-declared non-Drop local, then moved so it's dropped before the non-Drop local is deallocated. Previously this worked fine. After this change, if there's a function call before the move happens, the non-Drop local is StorageDead on the call's unwind path before the Drop local's destructor is called, even though that's not the order on the happy path. Given that this has come up multiple times in PR CI, I imagine it'd be fairly common in the wild.

I've only looked at the smaller test diffs so far, but I have some comments I'd like addressed before I dig deeper.

@rustbot author

View changes since this review

bb8: {
+ _12 = const false;
StorageDead(_6);
StorageDead(_5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why's there an extra StorageDead on the normal execution path here?

Copy link
Contributor

Choose a reason for hiding this comment

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

What is this testing? A test for the change in static semantics should be something that compiled before this change and doesn't compile after, like the example in #147875 or a minimization of the r-a or cargo failures in CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

I imagine this is from an old revision?

Comment on lines -113 to +116
o2.set0(&o2); //~ ERROR `o2` does not live long enough
o2.set1(&o3); //~ ERROR `o3` does not live long enough
o2.set0(&o2);
o2.set1(&o3);
o3.set0(&o1); //~ ERROR `o1` does not live long enough
o3.set1(&o2); //~ ERROR `o2` does not live long enough
o3.set1(&o2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know what's going on here?

@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 Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-explicit_tail_calls `#![feature(explicit_tail_calls)]` 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-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local variable deallocated out of order in the panic path?

7 participants