Skip to content

"SIGSEGV: invalid memory reference" when compiling with optimizations #127260

Closed
@notverymoe

Description

@notverymoe

Code

I worked out this minimal reproducible example from some code I've been working on. Changing almost anything about this causes the compiler crash to go away, which probably makes it a little hard to figure out what's going on here. I've checked and performing a release build, or a debug build with an opt level >= 1 will cause the compiler to SIGSEGV. Clearing the target dir, switching between stable/nightly, reinstalling toolchains, updating my system etc. didn't have an effect.

// lib.rs - minimal reproducible example

// // -------- -------- // //

// Adding an interger #[repr(?)] fixes the crash, except for u8, i8 & i16 (?)
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum EnumValue {
    // Removing any of these fixes it (?)
    A = 0, 
    B = 1,
    C = 2,
    D = 3,
}

impl EnumValue {

    pub fn map_value(self) -> usize {
        // Changing anything in this mapping fixes it (?)
        match self {
            EnumValue::A => 0,
            EnumValue::B => 1,
            EnumValue::C => 2,
            EnumValue::D => 0,
        }
    }

}

// // -------- -------- // //

pub trait ProviderTrait {
    fn get(&self, value: EnumValue) -> u64;
}

// // -------- -------- // //

// Removing the lifetime fixes it (?)
pub struct ProviderTraitImpl<'a> {
    // Moving the lifetime out of the inner array or to the inner `u64` fixes it (?)
    pub data: [&'a [u64; 3]; 4],
}

// Not implementing the trait fixes it (?)
impl<'a> ProviderTrait for ProviderTraitImpl<'a> {
    fn get(&self, value: EnumValue) -> u64 {
        // Changing almost anything in here fixes it (?)
        if value < EnumValue::D { 
            self.data[value as usize][value.map_value()]
        } else {
            self.data[3][value.map_value()]
        }
    }
}

Meta

Occurs in both Stable and Nightly.

rustc +stable --version --verbose:

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

rustc +nightly --version --verbose:

rustc 1.81.0-nightly (6292b2af6 2024-07-02)
binary: rustc
commit-hash: 6292b2af620dbd771ebb687c3a93c69ba8f97268
commit-date: 2024-07-02
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Error output

> RUST_BACKTRACE=1 cargo build --release --verbose
Compiling sigsegv-mre v0.1.0 (/home/nyatalie/Dev/Misc/RustCrash)
     Running `/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name sigsegv_mre --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=212 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=e25b70c6bc8e498d -C extra-filename=-e25b70c6bc8e498d --out-dir /home/nyatalie/Dev/Misc/RustCrash/target/release/deps -C strip=debuginfo -L dependency=/home/nyatalie/Dev/Misc/RustCrash/target/release/deps`
error: rustc interrupted by SIGSEGV, printing backtrace

/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x2ea5963)[0x7712886a5963]
/usr/lib/libc.so.6(+0x3cae0)[0x771285650ae0]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(+0x60d54d7)[0x771283cd54d7]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(+0x60d7e96)[0x771283cd7e96]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(+0x60d3bdc)[0x771283cd3bdc]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE+0xad4)[0x771283939714]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE+0x2f)[0x771283938b85]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM.so.18.1-rust-1.79.0-stable(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0x260)[0x771283e7a474]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(LLVMRustWriteOutputFile+0x190)[0x77128a568790]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x4d683a8)[0x77128a5683a8]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x4d6603f)[0x77128a56603f]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x4d65d21)[0x77128a565d21]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x4d6409b)[0x77128a56409b]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-1ccb730c51a3970e.so(+0x4d63667)[0x77128a563667]
/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-d5189b81a4fa4d36.so(rust_metadata_std_3c8ba8ebcf555201+0xc3cab)[0x77128baa4cab]
/usr/lib/libc.so.6(+0x92ded)[0x7712856a6ded]
/usr/lib/libc.so.6(+0x1160dc)[0x77128572a0dc]

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
error: could not compile `sigsegv-mre` (lib)

Caused by:
  process didn't exit successfully: `/home/nyatalie/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name sigsegv_mre --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=212 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=e25b70c6bc8e498d -C extra-filename=-e25b70c6bc8e498d --out-dir /home/nyatalie/Dev/Misc/RustCrash/target/release/deps -C strip=debuginfo -L dependency=/home/nyatalie/Dev/Misc/RustCrash/target/release/deps` (signal: 11, SIGSEGV: invalid memory reference)

No backtrace was output.
Also attempted to enable LLVM assertions and saw no change in output.
Increasing stack size, as per advice, had no effect.

Activity

added
C-bugCategory: This is a bug.
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jul 3, 2024
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jul 3, 2024
zirconium-n

zirconium-n commented on Jul 3, 2024

@zirconium-n
Contributor

rustc 1.77.0 compiles OK. rustc 1.78.0+ crashes.

added
I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
and removed
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jul 3, 2024
nikic

nikic commented on Jul 3, 2024

@nikic
Contributor

Crash in TwoAddressInstructionPass, appears to still occur on LLVM main.

self-assigned this
on Jul 3, 2024
nikic

nikic commented on Jul 3, 2024

@nikic
Contributor

Upstream issue: llvm/llvm-project#97533

added
llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes
on Jul 4, 2024

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @nikic@notverymoe@zirconium-n@rustbot

    Issue actions

      "SIGSEGV: invalid memory reference" when compiling with optimizations · Issue #127260 · rust-lang/rust