Skip to content

Stack overflow in rustc when compiling generated function (7k+ lines) with debuginfo #93908

@w1th0utnam3

Description

@w1th0utnam3

I'm working on a project that heavily relies on generated code (e.g. derivatives obtained using symbolic differentiation) which I manually paste into my project. When I try to compile my crate with debuginfo (i.e. cargo build or cargo build --release with debug=true) I get a stack overflow error from rustc.

I tried to reduce my code to a "minimal" example and uploaded a crate to reproduce this here: https://github.com/w1th0utnam3/stackoverflow_symbolic_code. The repository contains almost only the relevant function which can be found here: https://github.com/w1th0utnam3/stackoverflow_symbolic_code/blob/15d0da7cc578f85b5feac3f0a44c4be469fe282f/src/sympy_expressions.rs#L36.

The function has about 7000 lines of mostly basic arithmetic expressions. I removed any return values and added #![allow(unused_variables)] as this didn't affect the outcome. I replaced some Vector/Matrix types with a dummy type to remove dependencies.

By removing lines from the function e.g. using bisection you can find a point where the stack overflow disappears. The exact point seems to be depending on the environment so I don't provide specific values here.

Compiling with optimizations but without debuginfo (i.e. the standard cargo build --release) works fine.

Exemplary output on Windows:

PS C:\Programming\sympy_expr> $env:RUST_BACKTRACE=1; cargo build
   Compiling sympy-expr v0.0.1 (C:\Programming\sympy_expr)

thread 'optimize module 4wjpmobh093em8cl' has overflowed its stack
error: could not compile `sympy-expr`

Caused by:
  process didn't exit successfully: `rustc --crate-name sympy_expr --edition=2021 src\lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=5bf94dff4ccd60be -C extra-filename=-5bf94dff4ccd60be --out-dir C:\Programming\sympy_expr\target\debug\deps -C incremental=C:\Programming\sympy_expr\target\debug\incremental -L dependency=C:\Programming\sympy_expr\target\debug\deps` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)

A similar error appears on Linux. On Windows rustfmt crashes as well:

cargo fmt

thread 'main' has overflowed its stack

I don't know how rustfmt works internally, so this might require separate investigation from the rustc crash.

Meta

Stack overflow happens on stable:

rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-pc-windows-msvc
release: 1.58.1
LLVM version: 13.0.0

but also on nightly:

rustc 1.60.0-nightly (e646f3d2a 2022-02-10)
binary: rustc
commit-hash: e646f3d2a9541952310778288854943678738ea9
commit-date: 2022-02-10
host: x86_64-pc-windows-msvc
release: 1.60.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions