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

rustc crash when proc macro panics if compiled with -Cpanic=abort #82320

Open
ojeda opened this issue Feb 20, 2021 · 6 comments
Open

rustc crash when proc macro panics if compiled with -Cpanic=abort #82320

ojeda opened this issue Feb 20, 2021 · 6 comments
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ojeda
Copy link
Contributor

ojeda commented Feb 20, 2021

Code

Panicking proc macro p.rs:

extern crate proc_macro;

#[proc_macro]
pub fn p(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
    panic!()
}

User m.rs:

extern crate p;
p::p! {}

Then:

$ rustc -Cpanic=abort --crate-type proc-macro p.rs
$ rustc -L. m.rs

Meta

Reproduces in 1.49.0, 1.50.0 as well as nightlies.

Error output

Depends on how the proc macro is compiled. It can range from some output:

$ rustc -Cpanic=abort --crate-type proc-macro p.rs
$ rustc -L. m.rs
fatal runtime error: failed to initiate panic, error 5
(aborted)

To nothing at all:

$ rustc -Cpanic=abort --crate-type proc-macro p.rs -Cforce-unwind-tables=y
$ rustc -L. m.rs
(return code 101)
@ojeda ojeda added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 20, 2021
@ojeda
Copy link
Contributor Author

ojeda commented Feb 20, 2021

By the way, passing -Z proc-macro-backtrace does display an ICE, which is useful to at least know in which line the proc macro failed, e.g.:

thread 'rustc' panicked at 'explicit panic', p.rs:5:5

ojeda added a commit to Rust-for-Linux/linux that referenced this issue Feb 20, 2021
Avoids the rust-lang/rust#82320 rustc crash.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
fanninpm added a commit to fanninpm/glacier that referenced this issue Feb 20, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Feb 21, 2021
@ojeda
Copy link
Contributor Author

ojeda commented Feb 22, 2021

@rustbot label: +I-crash, -I-ICE

@rustbot rustbot added I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Feb 22, 2021
@ojeda ojeda changed the title No output ICE when proc macro panics if compiled with -Cpanic=abort rustc crash when proc macro panics if compiled with -Cpanic=abort Feb 22, 2021
@Aaron1011
Copy link
Member

It might be possible to improve the error message here. However, proc macros are loaded into the compiler process via a shared library, so there's no way for us to 'catch' the abort panic without running proc macros in another process.

@ojeda
Copy link
Contributor Author

ojeda commented Feb 22, 2021

We could make the proc macros' panic-abort not actually abort the process, communicating they are "dead" instead to the host process.

A user passing -Cpanic=abort in a proc macro is likely trying to save space, or was trying to simplify a custom build system, or did it by accident (e.g. see next comment). It is unlikely they were trying to ask to actually abort rustc. Perhaps -Cpanic= shouldn't be supported for --crate-type proc-macro at all.

@ojeda
Copy link
Contributor Author

ojeda commented Feb 22, 2021

For context: I ended up seeing this because I used a set of flags which I passed to both rlibs and proc-macros in a custom build system. It was definitely not my intention crashing rustc when a proc macro fails! A warning for --crate-type proc-macro + -Cpanic=abort could be another alternative.

@bjorn3
Copy link
Member

bjorn3 commented Jun 5, 2021

We could make the proc macros' panic-abort not actually abort the process, communicating they are "dead" instead to the host process.

Proc-macros are run as part of the host process on the main compilation thread. Without -Cpanic=unwind the only way to jump out of the panicking proc macro would be longjmp. Longjmp is UB when there are destructors in any of the skipped frames. Unix systems skip the destructors in this case, but Windows will run any destructors that exist. Because libstd is compiled with -Cpanic=unwind and the proc macro with -Cpanic=abort some things still have a destructor while others don't. LLVM optimizes the proc macro with the expectation that there are no destructors.

I think there are only two possible solutions: Ignore/warn on -Cpanic=abort for proc macros or run the proc macro in a different process. Running the proc macro in a different thread was already enough to cause a non-trivial perf regression I believe.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…eholk

Warn when using panic-strategy abort for proc-macro crates

See rust-lang#82320, this simply warns for now as that seems like the best step that can be immediately taken (opposed to straight up rejecting or ignoring)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…eholk

Warn when using panic-strategy abort for proc-macro crates

See rust-lang#82320, this simply warns for now as that seems like the best step that can be immediately taken (opposed to straight up rejecting or ignoring)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 13, 2023
…eholk

Warn when using panic-strategy abort for proc-macro crates

See rust-lang#82320, this simply warns for now as that seems like the best step that can be immediately taken (opposed to straight up rejecting or ignoring)
@Enselic Enselic added the A-proc-macros Area: Procedural macros label Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants