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

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

Closed
notverymoe opened this issue Jul 3, 2024 · 3 comments · Fixed by #127364
Closed

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

notverymoe opened this issue Jul 3, 2024 · 3 comments · Fixed by #127364
Assignees
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. llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@notverymoe
Copy link

notverymoe commented Jul 3, 2024

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.

@notverymoe notverymoe 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 Jul 3, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 3, 2024
@zirconium-n
Copy link
Contributor

zirconium-n commented Jul 3, 2024

rustc 1.77.0 compiles OK. rustc 1.78.0+ crashes.

@nikic nikic 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. 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 Jul 3, 2024
@nikic
Copy link
Contributor

nikic commented Jul 3, 2024

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

@nikic nikic self-assigned this Jul 3, 2024
@nikic
Copy link
Contributor

nikic commented Jul 3, 2024

Upstream issue: llvm/llvm-project#97533

@nikic nikic added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade label Jul 4, 2024
@bors bors closed this as completed in 5c08cc7 Jul 6, 2024
bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this issue Aug 13, 2024
838: Add tests for backports r=skade a=Veykril

- `tests/ui/ferrocene/llvm/no-segfault.rs` tests [issue#127260](rust-lang/rust#127260) which was fixed by the backported [PR#127364](rust-lang/rust#127364) 
- `tests/ui/ferrocene/consts/storage-live-on-live.rs` tests [issue#119366](rust-lang/rust#119366) which was fixed by the backported [PR#126154](rust-lang/rust#126154)
- `tests/codegen/ferrocene/miscompile_127286.rs` tests [issue#127286](rust-lang/rust#127286) which was fixed by the backported [PR#127364](rust-lang/rust#127364) 

Co-authored-by: Lukas Wirth <[email protected]>
bors-ferrocene bot added a commit to ferrocene/ferrocene that referenced this issue Aug 13, 2024
838: Add tests for backports r=skade a=Veykril

- `tests/ui/ferrocene/llvm/no-segfault.rs` tests [issue#127260](rust-lang/rust#127260) which was fixed by the backported [PR#127364](rust-lang/rust#127364) 
- `tests/ui/ferrocene/consts/storage-live-on-live.rs` tests [issue#119366](rust-lang/rust#119366) which was fixed by the backported [PR#126154](rust-lang/rust#126154)
- `tests/codegen/ferrocene/miscompile_127286.rs` tests [issue#127286](rust-lang/rust#127286) which was fixed by the backported [PR#127364](rust-lang/rust#127364) 

Co-authored-by: Lukas Wirth <[email protected]>
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. llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants