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 in rustc upon compiling with --release #119179

Closed
cfstras opened this issue Dec 21, 2023 · 17 comments · Fixed by #119802
Closed

SIGSEGV in rustc upon compiling with --release #119179

cfstras opened this issue Dec 21, 2023 · 17 comments · Fixed by #119802
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, or backported fixes

Comments

@cfstras
Copy link

cfstras commented Dec 21, 2023

I tried compiling this code with --release:

pub fn fun(number: usize, decision: bool) {
    for _ in 0..2 {
        for _ in 0..number {
            let iter: Box<dyn Iterator<Item = ()>> = if decision {
                Box::new(std::iter::empty())
            } else {
                Box::new(std::iter::once(()))
            };
            for _ in iter {}
        }
    }
}
rustc --crate-type lib -C opt-level=3 code.rs

(Thanks to @Xiretza for helping me find this shorter reproducer)

I expected to see this happen: (nothing, since there's no main method) a clean compile

Instead, this happened:

[1]    7213 segmentation fault  rustc --crate-type lib -C opt-level=3 code.rs

(Or, when compiling through cargo build --release:)

error: could not compile [...]` (lib)

Caused by:
  process didn't exit successfully: `[...]/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc [...] (signal: 11, SIGSEGV: invalid memory reference)
warning: build failed, waiting for other jobs to finish...

Meta

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: aarch64-apple-darwin
release: 1.74.1
LLVM version: 17.0.4

Note: this crashes on the playground on nightly (2023-12-20 5ac4c8a), beta (1.75.0-beta.7), and stable (1.74.1). Also crashes on aarch64-apple-darwin nightly (2023-12-19 f704f3b), beta (1.75.0-beta.7) and stable (1.74.1)

Backtrace

(From Playground since I don't have a debug build on macOS yet)

error: rustc interrupted by SIGSEGV, printing backtrace

/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x311dd46)[0x7f8e67528d46]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x14420)[0x7f8e64250420]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm16MemorySSAUpdater19updateForClonedLoopERKNS_13LoopBlocksRPOENS_8ArrayRefIPNS_10BasicBlockEEERKNS_8ValueMapIPKNS_5ValueENS_14WeakTrackingVHENS_14ValueMapConfigISB_NS_3sys10SmartMutexILb0EEEEEEEb+0x883)[0x7f8e62ea1b4d]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x696078a)[0x7f8e630ac78a]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x66b2315)[0x7f8e62dfe315]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm22SimpleLoopUnswitchPass3runERNS_4LoopERNS_15AnalysisManagerIS1_JRNS_27LoopStandardAnalysisResultsEEEES5_RNS_10LPMUpdaterE+0x44e)[0x7f8e62dfbb0e]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x66af6b9)[0x7f8e62dfb6b9]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_4LoopENS_15AnalysisManagerIS1_JRNS_27LoopStandardAnalysisResultsEEEEJS4_RNS_10LPMUpdaterEEE24runWithoutLoopNestPassesERS1_RS5_S4_S7_+0x3aa)[0x7f8e62ae8caa]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_4LoopENS_15AnalysisManagerIS1_JRNS_27LoopStandardAnalysisResultsEEEEJS4_RNS_10LPMUpdaterEEE3runERS1_RS5_S4_S7_+0x23)[0x7f8e62ae8823]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm25FunctionToLoopPassAdaptor3runERNS_8FunctionERNS_15AnalysisManagerIS1_JEEE+0x608)[0x7f8e6278e088]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x6041a71)[0x7f8e6278da71]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_8FunctionENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_+0xe51)[0x7f8e62ab2fd7]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm26CGSCCToFunctionPassAdaptor3runERNS_13LazyCallGraph3SCCERNS_15AnalysisManagerIS2_JRS1_EEES5_RNS_17CGSCCUpdateResultE+0x356)[0x7f8e627bff96]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x6073c39)[0x7f8e627bfc39]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_13LazyCallGraph3SCCENS_15AnalysisManagerIS2_JRS1_EEEJS4_RNS_17CGSCCUpdateResultEEE3runERS2_RS5_S4_S7_+0x667)[0x7f8e62b89527]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm21DevirtSCCRepeatedPass3runERNS_13LazyCallGraph3SCCERNS_15AnalysisManagerIS2_JRS1_EEES5_RNS_17CGSCCUpdateResultE+0x174)[0x7f8e62b88342]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm33ModuleToPostOrderCGSCCPassAdaptor3runERNS_6ModuleERNS_15AnalysisManagerIS1_JEEE+0x673)[0x7f8e6289d211]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x6150b99)[0x7f8e6289cb99]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_+0xb9)[0x7f8e627c10f9]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm24ModuleInlinerWrapperPass3runERNS_6ModuleERNS_15AnalysisManagerIS1_JEEE+0x1ab)[0x7f8e62ab07c7]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(+0x6364617)[0x7f8e62ab0617]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-17-rust-1.74.1-stable.so(_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_+0xb9)[0x7f8e627c10f9]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x25e38ac)[0x7f8e669ee8ac]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x25dc8ca)[0x7f8e669e78ca]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x25da3d9)[0x7f8e669e53d9]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x25d9a83)[0x7f8e669e4a83]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x25d7999)[0x7f8e669e2999]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-9ad8050f1171bcfc.so(+0x257b936)[0x7f8e66986936]
/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-b149a04e58514815.so(rust_metadata_std_afc0969a1e8d0069+0x10a395)[0x7f8e6437d395]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x8609)[0x7f8e64244609]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x43)[0x7f8e64167353]

note: we would appreciate a report at https://github.com/rust-lang/rust
note: backtrace dumped due to SIGSEGV! resuming signal
error: could not compile `playground` (lib)

@cfstras cfstras added the C-bug Category: This is a bug. label Dec 21, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 21, 2023
@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 needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 21, 2023
@nikic nikic self-assigned this Dec 21, 2023
@lqd

This comment was marked as outdated.

@nikic
Copy link
Contributor

nikic commented Dec 21, 2023

Reduced:

; RUN: opt -passes="loop-mssa(loop-instsimplify,simple-loop-unswitch<nontrivial>)" < %s
@vtable = constant ptr @foo

declare void @foo() memory(none)

define void @test(i1 %c) {
  br label %loop

loop:
  %fn = load ptr, ptr @vtable, align 8
  call void %fn()
  br i1 %c, label %exit, label %loop

exit:
  ret void
}

@nikic
Copy link
Contributor

nikic commented Dec 21, 2023

Upstream fix: llvm/llvm-project#76142

@jamesmunns
Copy link
Member

jamesmunns commented Dec 21, 2023

@cfstras, could you please try adding -Cllvm-args=--enable-machine-outliner=never and see if it still reproduces? I had thought this was cortex-m/thumb specific, but might be relevant to all ARM targets. CC:

#118867

edit: Ah nikic might have already found a more specific issue, probably nevermind :)

@cfstras
Copy link
Author

cfstras commented Dec 21, 2023

I verified successfully that this also fixes the build for my codebase where I originally found this 👍🏻 (by cherry-picking only your commit on top of the currently used nightly llvm branch in rust-lang/rust, 606bc11367b4)

edit @jamesmunns: Sorry, just saw your comment as I was leaving. I can try that later, but since it also reproduces on the (linux/ guessing amd64?) rust playground, I doubt this is ARM-related.

@cfstras
Copy link
Author

cfstras commented Dec 21, 2023

@jamesmunns: The argument doesn't seem to make a difference, neither on stable/beta, nor on nightly.
Current versions (as in my initial post) all crash (with and without the flag), and the stage1 I built with @nikic's fix works fine with and without the flag.

@nikic nikic added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes label Jan 9, 2024
@bors bors closed this as completed in 062e7c6 Jan 11, 2024
@cfstras
Copy link
Author

cfstras commented Jan 16, 2024

I'm confused, this code still crashes with yesterday's nightly? Could it be that the alternative fix you selected doesn't fully fix the issue I'm seeing?

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=af69bae1324b65ceba5deae931e26d70

@cfstras
Copy link
Author

cfstras commented Jan 16, 2024

I had also tested my code using your cherry-picked fix (llvm/llvm-project#76819) based on llvm 606bc11367b4 again, and saw the crash still happen. Apologies, I seemingly had written the comment but forgot to send.

@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

Hm yeah, I tested my original fix against the original test case, but not the final version. Let me take another look.

@nikic nikic reopened this Jan 16, 2024
@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

New reduction:

; RUN: opt -passes="loop-mssa(loop-instsimplify,simple-loop-unswitch<nontrivial>)" < %s
@vtable = constant ptr @foo

declare void @foo() memory(none)

define void @test(i1 %c) {
  br label %loop

loop:
  %fn = load ptr, ptr @vtable, align 8
  call void %fn()
  br i1 %c, label %exit, label %loop

exit:
  ret void
}

Took me a while to spot how this differs from the previous one: The branch destinations are swapped...

@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

New upstream fix: llvm/llvm-project#78272

@cfstras
Copy link
Author

cfstras commented Jan 16, 2024

When cherry-picking this onto latest rust master (fa0dc20, llvm 919a630125a0), I get:

rustc +stage1 --crate-type lib -C opt-level=3 rustcrash.rs
Assertion failed: (IncMUD && "MemoryUseOrDef cannot be null, all preds processed."), function operator(), file MemorySSAUpdater.cpp, line 703.
[1]    28009 abort      rustc +stage1 --crate-type lib -C opt-level=3 rustcrash.rs

Is this expected?

@nikic
Copy link
Contributor

nikic commented Jan 16, 2024

@cfstras Are you sure the commit was picked up? The patch removes that assertion.

@lqd
Copy link
Member

lqd commented Jan 16, 2024

Reopening, the auto closing on a merge in @saethlin's fork seems unintentional.

@lqd lqd reopened this Jan 16, 2024
@saethlin
Copy link
Member

🤦 yes, unintentional. If someone has a way to prevent that I'd appreciate it.

@cfstras
Copy link
Author

cfstras commented Jan 17, 2024

@nikic duh, forgot to commit the llvm submodule, thanks. Confirmed ✅ no crash, also builds my original project without issue.

@nikic
Copy link
Contributor

nikic commented Feb 16, 2024

Fixed in beta & nightly.

@nikic nikic closed this as completed Feb 16, 2024
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, or backported fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants