rust-lang / rust-lang/rust

No backtrace from segfault handler after stack overflow in a proc macro on WSL

Open
#130,119 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-proc-macros C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

i have a presumably-extremely-broken proc macro which itself builds successfully but, when used, causes a segfault.

in lib.rs:

use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, Expr};

#[proc_macro]
pub fn eager(input: TokenStream) -> TokenStream {
    let expr = parse_macro_input!(input as Expr);
    let expanded = expand_macros(expr);
    TokenStream::from(quote! { #expanded })
}

fn expand_macros(expr: Expr) -> Expr {
    match expr {
        Expr::Macro(expr_macro) => {
            let macro_tokens = expr_macro.mac.to_token_stream();
            match syn::parse2::<Expr>(macro_tokens) {
                Ok(parsed_expr) => {
                    expand_macros(parsed_expr)
                }
                Err(_) => {
                    Expr::Verbatim(quote! {})
                }
            }
        }
        _ => expr,
    }
}

in main.rs:

use my_macros::eager;

macro_rules! test_macro {
    ($input:expr) => {
        concat!("test macro applied: ", $input)
    };
}

fn main() {
    let out = eager!(test_macro!("test"));
}

tree structure:

├── Cargo.lock
├── Cargo.toml
└── src
    ├── lib.rs
    └── tests
        ├── Cargo.lock
        ├── Cargo.toml
        └── src
            └── main.rs
Meta

rustc --version --verbose:

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

also happens when running cargo +nightly build

Error output
evan@DESKTOP-4LK1HQ6:~/my_macros/src/tests$ cargo build
   Compiling proc-macro2 v1.0.86
   Compiling unicode-ident v1.0.12
   Compiling quote v1.0.37
   Compiling syn v2.0.77
   Compiling my_macros v0.1.0 (/home/evan/my_macros)
   Compiling my_macros_tests v0.1.0 (/home/evan/my_macros/src/tests)
error: could not compile `my_macros_tests` (bin "my_macros_tests")

Caused by:
  process didn't exit successfully: `/home/evan/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name my_macros_tests --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=60 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=7c1fe1052e96595b -C extra-filename=-7c1fe1052e96595b --out-dir /home/evan/my_macros/src/tests/target/debug/deps -C incremental=/home/evan/my_macros/src/tests/target/debug/incremental -L dependency=/home/evan/my_macros/src/tests/target/debug/deps --extern my_macros=/home/evan/my_macros/src/tests/target/debug/deps/libmy_macros-c5775cc6c3a5c013.so` (signal: 11, SIGSEGV: invalid memory reference)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure from the proc-macro crate's lib.rs and the consumer in src/tests/src/main.rs with cargo build on WSL, including the reported stable and nightly toolchains. Compare the SIGSEGV behavior with RUST_BACKTRACE=1 and trace the compiler's proc-macro stack-overflow handling. Done means the supplied reproduction produces a useful backtrace or diagnostic instead of an unexplained segmentation fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.