Skip to content

SIGSEGV when compiling criterion benchmark with large const generic array #136739

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

Closed
cdellacqua opened this issue Feb 8, 2025 · 5 comments
Closed
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cdellacqua
Copy link

Code

Reproduction code can be found here hang_or_crash.rs

The culprit seems to be this:

pub struct CustomSlice<T, const N: usize>(pub [T; N]);

in combination with criterion iter_batched, when using a very large const generic:

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("Demo", |b| {
        b.iter_batched(
            || CustomSlice([0; 1_000_000]), // <-- here
            |test| {
                assert_eq!(test.0[10], 0);
            },
            criterion::BatchSize::NumIterations(1),
        );
    });
}

I tried this with criterion 0.4.0 and 0.5.1

Meta

rustc --version --verbose:

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-unknown-linux-gnu
release: 1.84.1
LLVM version: 19.1.5

rustc +nightly --version --verbose:

rustc 1.86.0-nightly (942db6782 2025-02-06)
binary: rustc
commit-hash: 942db6782f4a28c55b0b75b38fd4394d0483390f
commit-date: 2025-02-06
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Error output

When running cargo +stable bench --profile=dev I get the following, but note that the issue is present even when specifying -Znext-solver and -Znext-solver=coherence.

error: could not compile `hang_or_crash` (bench "bench")

Caused by:
  process didn't exit successfully: `/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name bench --edition=2021 benches/bench.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=176 --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg test --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' -C metadata=334ac19c27aebb52 -C extra-filename=-334ac19c27aebb52 --out-dir /home/carlo/Dev/Personal/hang_or_crash.rs/target/debug/deps -C incremental=/home/carlo/Dev/Personal/hang_or_crash.rs/target/debug/incremental -L dependency=/home/carlo/Dev/Personal/hang_or_crash.rs/target/debug/deps --extern criterion=/home/carlo/Dev/Personal/hang_or_crash.rs/target/debug/deps/libcriterion-71a6c66f6e81fca7.rlib --extern hang_or_crash=/home/carlo/Dev/Personal/hang_or_crash.rs/target/debug/deps/libhang_or_crash-3710c6af0466eef1.rlib` (signal: 11, SIGSEGV: invalid memory reference)
Backtrace

error: rustc interrupted by SIGSEGV, printing backtrace

/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x3633e63) [0x7a4d56833e63]
/usr/lib/libc.so.6(+0x3d1d0) [0x7a4d5304c1d0]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm12SelectionDAG18ReplaceAllUsesWithEPNS_6SDNodeEPKNS_7SDValueE+0x1fb) [0x7a4d513b9a3b]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(+0x6c2a999) [0x7a4d51c2a999]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm12SelectionDAG7CombineENS_12CombineLevelEPNS_9AAResultsENS_15CodeGenOptLevelE+0xd8b) [0x7a4d5131555b]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm16SelectionDAGISel17CodeGenAndEmitDAGEv+0x6d) [0x7a4d5131852d]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm16SelectionDAGISel20SelectAllBasicBlocksERKNS_8FunctionE+0x1ac4) [0x7a4d51405498]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm16SelectionDAGISel20runOnMachineFunctionERNS_15MachineFunctionE+0x11e) [0x7a4d513ebcde]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm22SelectionDAGISelLegacy20runOnMachineFunctionERNS_15MachineFunctionE+0x34e) [0x7a4d5127978e]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE+0xcc1) [0x7a4d5127ea4d]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE+0x2f) [0x7a4d5127dd0d]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.19.1-rust-1.84.1-stable(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0x2b9) [0x7a4d518fe54d]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(LLVMRustWriteOutputFile+0x171) [0x7a4d58bb8a31]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x59b867b) [0x7a4d58bb867b]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x59b91a0) [0x7a4d58bb91a0]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x59b8e28) [0x7a4d58bb8e28]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x5a81a5b) [0x7a4d58c81a5b]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x5a7ec9d) [0x7a4d58c7ec9d]
/home/carlo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-cbb5ad48aac6e327.so(+0x5a7efb9) [0x7a4d58c7efb9]
/usr/lib/libc.so.6(+0x942ce) [0x7a4d530a32ce]
/usr/lib/libc.so.6(+0x11929c) [0x7a4d5312829c]

note: we would appreciate a report at https://github.com/rust-lang/rust
help: you can increase rustc's stack size by setting RUST_MIN_STACK=16777216
note: backtrace dumped due to SIGSEGV! resuming signal

Possibly related issues

#136282

@cdellacqua cdellacqua 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 8, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 8, 2025
@saethlin
Copy link
Member

saethlin commented Feb 8, 2025

This is an LLVM assertion:

rustc: /checkout/src/llvm-project/llvm/include/llvm/CodeGen/SelectionDAGNodes.h:1135: llvm::SDNode::SDNode(unsigned int, unsigned int, DebugLoc, SDVTList): Assertion `NumValues == VTs.NumVTs && "NumValues wasn't wide enough for its operands!"' failed.

That's from running the linked reproducer with cargo +stage1 bench --profile=dev. The stage1 is my local stage1 build.

@saethlin saethlin added I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 8, 2025
@saethlin
Copy link
Member

saethlin commented Feb 8, 2025

A minimization would be awesome, but might be tricky if you don't have a compiler with LLVM assertions enabled.

@cyrgani
Copy link
Contributor

cyrgani commented Feb 8, 2025

reduction:

fn main() {
    let dummy = [0; 100_000];
    unsafe {
        std::ptr::read_volatile(&dummy);
    }
}

@rustbot label -E-needs-mcve S-has-mcve

@rustbot rustbot added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Feb 8, 2025
@matthiaskrgr
Copy link
Member

This is probably a duplicate of #83960 / #94793 / #110381

@saethlin
Copy link
Member

saethlin commented Feb 8, 2025

Oh duh. I always forget that criterion is still using the volatile hack instead of an actual black_box.

@saethlin saethlin closed this as completed Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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

5 participants