rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.17k stars 12.8k forks source link

ICE in rustc_codegen_llvm when cross compiling to linux on OSX #109711

Open ranmocy opened 1 year ago

ranmocy commented 1 year ago

Env setup

rustup +nightly target add aarch64-unknown-linux-gnu
rustup +nightly target add aarch64-apple-darwin
rustup +nightly target add x86_64-unknown-linux-gnu
rustup +nightly target add x86_64-apple-darwin

brew tap messense/macos-cross-toolchains
brew install x86_64-unknown-linux-gnu
brew install aarch64-unknown-linux-gnu

export CC_x86_64_unknown_linux_gnu=x86_64-unknown-linux-gnu-gcc
export CXX_x86_64_unknown_linux_gnu=x86_64-unknown-linux-gnu-g++
export AR_x86_64_unknown_linux_gnu=x86_64-unknown-linux-gnu-ar
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc
export CC_aarch64_unknown_linux_gnu=aarch64-unknown-linux-gnu-gcc
export CXX_aarch64_unknown_linux_gnu=aarch64-unknown-linux-gnu-g++
export AR_aarch64_unknown_linux_gnu=aarch64-unknown-linux-gnu-ar
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-unknown-linux-gnu-gcc

cargo build --bin my_bin --release --target aarch64-unknown-linux-gnu
cargo build --bin my_bin --release --target x86_64-unknown-linux-gnu
cargo build --bin my_bin --release --target x86_64-apple-darwin
cargo build --bin my_bin --release --target aarch64-apple-darwin

Code

TBD

Meta

rustc +nightly --version --verbose:

rustc 1.70.0-nightly (2036fdd24 2023-03-27)
binary: rustc
commit-hash: 2036fdd24f77d607dcfaa24c48fbe85d3f785823
commit-date: 2023-03-27
host: aarch64-apple-darwin
release: 1.70.0-nightly
LLVM version: 16.0.0

Error output

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_codegen_llvm/src/back/archive.rs:388:67
stack backtrace:
   0:        0x1055cd5bc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5fb738fb6b06a1d9
   1:        0x1056206d4 - core::fmt::write::h6a02464bebb2a88f
   2:        0x1055c306c - std::io::Write::write_fmt::h0365a706650ef7a0
   3:        0x1055cd3d0 - std::sys_common::backtrace::print::h2b2100bcc34ab976
   4:        0x1055cfde4 - std::panicking::default_hook::{{closure}}::hde9e5aa39a306a1c
   5:        0x1055cfba4 - std::panicking::default_hook::h3a7ed30f183fe740
   6:        0x10da3f554 - rustc_driver_impl[5332f8ff8b069570]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x1055d04a4 - std::panicking::rust_panic_with_hook::h3f818433f3792366
   8:        0x1055d025c - std::panicking::begin_panic_handler::{{closure}}::hba1d1542190b0a1d
   9:        0x1055cd9dc - std::sys_common::backtrace::__rust_end_short_backtrace::heb72c863cff1bc59
  10:        0x1055d0030 - _rust_begin_unwind
  11:        0x10564c0f0 - core::panicking::panic_fmt::h940abc2770676ab4
  12:        0x10564c160 - core::panicking::panic::hed9e6f165e9ca76a
  13:        0x10db6f750 - <rustc_codegen_llvm[fde183f7bc188e4d]::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa[c368d436ad072d1]::back::archive::ArchiveBuilder>::build
  14:        0x110ecfaa4 - rustc_codegen_ssa[c368d436ad072d1]::back::link::link_binary
  15:        0x10db636c8 - <rustc_codegen_llvm[fde183f7bc188e4d]::LlvmCodegenBackend as rustc_codegen_ssa[c368d436ad072d1]::traits::backend::CodegenBackend>::link
  16:        0x10dadd3cc - <rustc_interface[c04e01e9db0d2ec]::queries::Linker>::link
  17:        0x10d9d8aa4 - rustc_span[6c4d6989865837ed]::with_source_map::<core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>, rustc_interface[c04e01e9db0d2ec]::interface::run_compiler<core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>, rustc_driver_impl[5332f8ff8b069570]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  18:        0x10d9e6bc0 - std[6fbb0ec1f86da1b4]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[c04e01e9db0d2ec]::util::run_in_thread_pool_with_globals<rustc_interface[c04e01e9db0d2ec]::interface::run_compiler<core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>, rustc_driver_impl[5332f8ff8b069570]::run_compiler::{closure#1}>::{closure#0}, core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>>
  19:        0x10d9dec04 - <<std[6fbb0ec1f86da1b4]::thread::Builder>::spawn_unchecked_<rustc_interface[c04e01e9db0d2ec]::util::run_in_thread_pool_with_globals<rustc_interface[c04e01e9db0d2ec]::interface::run_compiler<core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>, rustc_driver_impl[5332f8ff8b069570]::run_compiler::{closure#1}>::{closure#0}, core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[62096c4622bbf560]::result::Result<(), rustc_span[6c4d6989865837ed]::ErrorGuaranteed>>::{closure#1} as core[62096c4622bbf560]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  20:        0x1055d8b4c - std::sys::unix::thread::Thread::new::thread_start::ha951329ff3fcdc44
  21:        0x1a2dca06c - __pthread_deallocate

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.70.0-nightly (2036fdd24 2023-03-27) running on aarch64-apple-darwin

note: compiler flags: --crate-type lib -C opt-level=s -C panic=abort -C linker-plugin-lto -C codegen-units=1 -C debuginfo=0 -C linker=aarch64-unknown-linux-gnu-gcc -C strip=symbols

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `openssl-sys` (lib)
Backtrace

``` thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_codegen_llvm/src/back/archive.rs:388:67 stack backtrace: Compiling libssh2-sys v0.2.23 Compiling openssl v0.10.45 0: _rust_begin_unwind 1: core::panicking::panic_fmt 2: core::panicking::panic 3: ::build 4: rustc_codegen_ssa::back::link::link_binary 5: ::link 6: ::link 7: rustc_span::with_source_map::, rustc_interface::interface::run_compiler, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}> note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: rustc 1.70.0-nightly (2036fdd24 2023-03-27) running on aarch64-apple-darwin note: compiler flags: --crate-type lib -C opt-level=s -C panic=abort -C linker-plugin-lto -C codegen-units=1 -C debuginfo=0 -C linker=aarch64-unknown-linux-gnu-gcc -C strip=symbols note: some of the compiler flags provided by cargo are hidden query stack during panic: end of query stack error: could not compile `openssl-sys` (lib) ```

jyn514 commented 1 year ago

Relevant code: https://github.com/rust-lang/rust/blob/2036fdd24f77d607dcfaa24c48fbe85d3f785823/compiler/rustc_codegen_llvm/src/back/archive.rs#L388

Looks like one of these tools has generated a .a archive in a format we don't expect?

ranmocy commented 1 year ago

Hi @jyn514 I'm totally unfamiliar with LLVM and Rust compiler. Is there any information I could grab from my side that would help debugging?

jyn514 commented 1 year ago

The most helpful information would be the archive it panics on and the contents of that archive (this is related to paths to even tar -tf foo.a would be enough). Unfortunately there's not a ton of debug logging in this part of the compiler, so I'm not sure how you could find out the name of the archive; maybe RUSTC_LOG=rustc_codegen_ssa,rustc_codegen_llvm will show something useful?

carloslbello commented 1 year ago

I'm experiencing this same issue - I've tried using RUSTC_LOG=rustc_codegen_ssa,rustc_codegen_llvm, however I get a large amount of output and I'm not sure how to decipher it into something useful. If anyone has any insight on how I could contribute more information I'd be happy to help further diagnose this.

davidtwco commented 1 year ago

I've tried to reproduce this but can't seem to - I've installed the cross-compilation toolchains, and the additional targets through rustup, with a simple "Hello, world" binary crate then running these commands doesn't cause an error. Are you still running into this?

kpreid commented 11 months ago

Relabeling issues which don't have a runnable reproduction (as opposed to not having a minimized one) to the new label S-needs-repro. @rustbot label +S-needs-repro -E-needs-mcve

juntyr commented 5 months ago

I've now come across this ICE as well, both in 1.78 and 1.79 stable.