Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion library/std/src/sys/personality/dwarf/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ pub struct EHContext<'a> {
type LPad = *const u8;
pub enum EHAction {
None,
/// Destructors should be executed when stack unwinds.
Cleanup(LPad),
/// Stack unwind should be stopped as the exception is going to be caught by `catch_unwind`.
Catch(LPad),
/// Stack unwind should be stopped for termination (`UnwindAction::Terminate`).
///
/// Note that due to inlining the landing pad can execute destructors before terminating. So
/// this is different from `Terminate`.
///
/// Handling of this is mostly identical to `Catch`; except that Rust frames that have no
/// destructors but only `UnwindAction::Terminate` is considered as plain-old-frame (POF) and
/// forced unwind is allowed to unwind past it; so this is treated as `None` during forced unwind.
Filter(LPad),
/// Process should be terminated as the call site does not permit unwinding.
Terminate,
}

Expand Down Expand Up @@ -160,7 +171,19 @@ unsafe fn interpret_cs_action(
let action_record = unsafe { action_table.offset(cs_action_entry as isize - 1) };
let mut action_reader = DwarfReader::new(action_record);
let ttype_index = unsafe { action_reader.read_sleb128() };
if ttype_index == 0 {
let next_action = unsafe { action_reader.read_sleb128() };
if next_action != 0 {
// We observed multiple actions. Action records contain no duplicates (at least that is
// true for both LLVM/GCC), and as Rust does not have exception specification, this
// indicates that we have at least 2 of "cleanup", "catch" and "filter", so we should
// catch all exceptions.
//
// Note that even for the case of "cleanup" + "filter", decoding them as "catch" is
// fine: "filter" behaves identically to "catch" except for forced unwind; in case of
// forced unwind, hitting a "cleanup" landing pad is UB as it indicates that we're
// unwinding past a non-POF Rust frame.

@bjorn3 bjorn3 Aug 27, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if there are two cleanup actions for whatever reason? Should this explicitly check that at least one action is a filter or catch if there are multiple ones and abort otherwise?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That'll be a compiler bug? I don't think we should defend against something that won't happen at a cost of code size.

Also, nothing will be catastrophically wrong even if we catch a cleanup frame, we will just be skipping phase 1 of unwind and perform phase 2 unwind for further stack frames, similar to forced unwind.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't the unwinder misbehave if you _Unwind_Resume out of a frame that you said is a catch?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It might unwind more frames, but nothing will be terribly wrong. Anyhow, neither GCC nor LLVM will emit duplicate action record entry, so I don't think this is worth handling.

EHAction::Catch(lpad)
} else if ttype_index == 0 {
EHAction::Cleanup(lpad)
} else if ttype_index > 0 {
// Stop unwinding Rust panics at catch_unwind.
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/panics/lsda-multiple-action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-pass
//@ needs-unwind
//@ ignore-backends: gcc
//@ compile-flags: -Copt-level=3

struct Guard;

impl Drop for Guard {
fn drop(&mut self) {
core::hint::black_box(());
}
}

#[inline(never)]
fn unwind() {
if core::hint::black_box(true) {
std::panic::resume_unwind(Box::new(()));
}
}

fn main() {
// The `catch_unwind` will generate `landingpad catch` and the destructor will generate
// `landingpad cleanup`; after LLVM inlining it will become `landingpad cleanup catch`, and this
// is translated to action record chains in LSDA.
let _ = std::panic::catch_unwind(|| {
let _guard = Guard;
unwind();
});
}
Loading