Skip to content

Double-word align _Unwind_Exception - #161368

Open
hsanzg wants to merge 1 commit into
rust-lang:mainfrom
hsanzg:uw-ex-align
Open

Double-word align _Unwind_Exception#161368
hsanzg wants to merge 1 commit into
rust-lang:mainfrom
hsanzg:uw-ex-align

Conversation

@hsanzg

@hsanzg hsanzg commented Aug 19, 2026

Copy link
Copy Markdown

The Itanium C++ ABI spec states that the _Unwind_Exception type must be double-word aligned (see Section 1.2). The missing alignment option does not seem to cause problems when a) using Rust's panic mechanism (because the exception object passed to _Unwind_RaiseException is heap-allocated and thus double-word aligned by accident---although this is not guaranteed) or b) when linking against libgcc's unwinder.

Most people don't need to unwind stacks themselves, so case (a) usually applies; and case (b) is the default for the amd64-unknown-linux-gnu target. Thus, misalignments seem unlikely to cause trouble in practice. However, I recently copied over some of the type definitions in library/unwind into a personal project, built rustc with rust.llvm-libunwind = "system", and installed LLVM's libunwind-24-dev. Calling _Unwind_RaiseException with a thread-local _Unwind_Exception object led to a segfault due to the too-small default alignment. So libunwind seems to depend on the double-word alignment.

N.B.: Both libunwind and libgcc have comments [1, 2] saying that the "double-word" alignment is a bit ambiguous when interpreted in a target-agnostic sense, and they both add __attribute__((__aligned__)) to _Unwind_Exception, with no specific alignment value (the default is the maximum alignment of any integer type). Rust doesn't have such a "default" alignment, so I interpreted "double-word" in a target-dependent manner via cfg_attr + the target_pointer_width feature.

[1]: https://github.com/llvm/llvm-project/blob/196786fa5fe4225539678fc7904a383eca05374e/libunwind/include/unwind_itanium.h#L41
[2]: https://github.com/gcc-mirror/gcc/blob/50a2eb56b9a350ecced4db4942e92d463dab8d8f/libgcc/unwind-generic.h#L106

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 19, 2026
@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @jieyouxu (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Fallback group: @Mark-Simulacrum, @jieyouxu
  • @Mark-Simulacrum, @jieyouxu expanded to Mark-Simulacrum, jieyouxu
  • Random selection from Mark-Simulacrum, jieyouxu

@jieyouxu

Copy link
Copy Markdown
Member

r? libs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Aug 20, 2026
@rustbot rustbot assigned tgross35 and unassigned jieyouxu Aug 20, 2026
Comment on lines 44 to 52
#[repr(C)]
#[cfg_attr(target_pointer_width = "16", repr(align(4)))]
#[cfg_attr(target_pointer_width = "32", repr(align(8)))]
#[cfg_attr(target_pointer_width = "64", repr(align(16)))]
pub struct _Unwind_Exception {
pub exception_class: _Unwind_Exception_Class,
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
pub private: [_Unwind_Word; unwinder_private_data_size],
}

@tgross35 tgross35 Aug 20, 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.

Could you add a comment similar to what's in LLVM? Otherwise LGTM.

View changes since the review

@tgross35

Copy link
Copy Markdown
Member

Cc @bjorn3 since this is your area of expertise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

4 participants