Skip to content

Is unwinding from lang_start really UB? #107381

Closed
@ChrisDenton

Description

@ChrisDenton

A comment in the std claims that unwinding past lang_start is UB:

rust/library/std/src/rt.rs

Lines 136 to 145 in 6874f4e

// Guard against the code called by this function from unwinding outside of the Rust-controlled
// code, which is UB. This is a requirement imposed by a combination of how the
// `#[lang="start"]` attribute is implemented as well as by the implementation of the panicking
// mechanism itself.
//
// There are a couple of instances where unwinding can begin. First is inside of the
// `rt::init`, `rt::cleanup` and similar functions controlled by bstd. In those instances a
// panic is a std implementation bug. A quite likely one too, as there isn't any way to
// prevent std from accidentally introducing a panic to these functions. Another is from
// user code from `main` or, more nefariously, as described in e.g. issue #86030.

See: #86034, #86030, #86027.

This is surprising because C++ panics escaping main is well defined to terminate (and will trigger an attached debugger, which is useful).

cc @Amanieu, who questioned this assertion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions