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

Tracking Issue for PanicInfo::can_unwind #92988

Open
3 tasks
Amanieu opened this issue Jan 17, 2022 · 7 comments
Open
3 tasks

Tracking Issue for PanicInfo::can_unwind #92988

Amanieu opened this issue Jan 17, 2022 · 7 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Amanieu
Copy link
Member

Amanieu commented Jan 17, 2022

Feature gate: #![feature(panic_can_unwind)]

This is a tracking issue for PanicInfo::can_unwind.

This function returns whether the panic handler is allowed to unwind the stack from the point where the panic occurred.

This is true for most kinds of panics with the exception of panics caused by trying to unwind out of a Drop implementation or a function whose ABI does not support unwinding.

Public API

// core::panic

impl PanicInfo {
    pub fn can_unwind(&self) -> bool;
}

Steps / History

Unresolved Questions

  • None yet.
@Amanieu Amanieu added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Jan 17, 2022
@Amanieu Amanieu changed the title Tracking Issue for XXX Tracking Issue for PanicInfo::can_unwind Jan 17, 2022
@nbdd0121
Copy link
Contributor

Just thought of this, what happens if the panic handler actually unwind? If it can cause undefined behaviour then the panic handler would have to be unsafe.

@Amanieu
Copy link
Member Author

Amanieu commented Jan 29, 2022

Unwinding is itself an unsafe operation. It is your responsibility to check that unwinding is actually possible before throwing an exception.

@nbdd0121
Copy link
Contributor

That'll break encapsulation. There is no way that code can check that unwinding isn't currently in progress.

@Amanieu
Copy link
Member Author

Amanieu commented Jan 29, 2022

That's what the can_unwind flag on PanicInfo is for. You must not start unwinding if that function returns false.

@nbdd0121
Copy link
Contributor

However there're no ways to prevent the panic handler from calling another function that unwinds. So either panic handler needs to be unsafe to take that responsibility, or it implies that C-unwind function cannot be encapsulated by safe APIs.

@Amanieu
Copy link
Member Author

Amanieu commented Jan 29, 2022

I just had a look through the code. By marking the panic_no_unwind lang item as nounwind (PR incoming), an unwind from a panic handler will become safe even if can_unwind is true. The compiler will add a TerminatorKind::Abort landing pad to panic_no_unwind which will simply call panic_no_unwind again. Eventually you will run out of stack and abort the process (which is safe).

This can be avoided by having a thread-local flag in the panic handler that catches recursive calls and aborts immediately. The standard library panic handler does this.

@nbdd0121
Copy link
Contributor

Or probably just tweak the codegen to generate trap instead of panic_no_unwind call if the function being codegen-ed is panic_no_unwind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants