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

When a proc_macro has a stack overflow, rustc crashes and errors confusingly #118701

Open
PizzasBear opened this issue Dec 7, 2023 · 2 comments
Labels
A-error-handling Area: Error handling A-proc-macros Area: Procedural macros C-bug Category: This is a bug. 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

@PizzasBear
Copy link
Contributor

Code

// --- macro_crate/src/lib.rs ---
use proc_macro::TokenStream;

#[proc_macro_derive(Trait)]
pub fn derive_trait(tokens: TokenStream) -> TokenStream {
    if true {
        derive_trait(tokens)
    } else {
        TokenStream::new()
    }
}


// --- my-crate/src/main.rs ---
#[derive(Trait)]
struct S;

fn main() {}

Current output

error: rustc interrupted by SIGSEGV, printing backtrace

~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-fd05879751a9d790.so(+0x3104966)[0x7f5e85f04966]
/lib64/libc.so.6(+0x3e9a0)[0x7f5e82ac49a0]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21420)[0x7f5e79482420]

### cycle encountered after 3 frames with period 4
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
### recursed 63 times

.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]

note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
error: could not compile `test-issue` (bin "test-issue")

Caused by:
  process didn't exit successfully: `~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name test_issue --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=212 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=10df6c618bb41553 -C extra-filename=-10df6c618bb41553 --out-dir .../test-issue/target/debug/deps -C incremental=.../test-issue/target/debug/incremental -L dependency=.../test-issue/target/debug/deps --extern askama=.../test-issue/target/debug/deps/libaskama-a71517d66164d0f1.rlib --extern macro_crate=.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so --extern serde=.../test-issue/target/debug/deps/libserde-916cd61b435466f2.rlib -C link-arg=-fuse-ld=lld` (signal: 11, SIGSEGV: invalid memory reference)

Desired output

Macro at `#[derive(Trait)]` has overflowed.

Or something that specifies the place where the macro overflowed, its crate, and that doesn't imply the issue is with rustc.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@PizzasBear PizzasBear added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 7, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 7, 2023
@PizzasBear PizzasBear changed the title When a proc_macro encounters a stack overflows, rustc crashes When a proc_macro has a stack overflow, rustc crashes and errors confusingly Dec 7, 2023
@fmease
Copy link
Member

fmease commented Dec 7, 2023

Strongly related: #106298

@fmease fmease added I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. C-bug Category: This is a bug. A-error-handling Area: Error handling A-proc-macros Area: Procedural macros and removed A-diagnostics Area: Messages for errors, warnings, and lints needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 7, 2023
@workingjubilee
Copy link
Member

@PizzasBear Can you explain the real use-case that led to this bug report?

It is challenging to extract useful error messages from rustc during SIGSEGV. Successfully identifying a stack overflow and formatting an error at all is more complicated than it seems. Allocation and locks are effectively a no-go. Getting the error message this nice was a little bit of a feat. So I'd like to at least know what real cases triggered this, because that would affect how I approached any improvement. Surely it can't be this simplistic unconditional recursion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error-handling Area: Error handling A-proc-macros Area: Procedural macros C-bug Category: This is a bug. 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

4 participants