rust-lang / rust

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

ICE: `-Crpath` without `-o` #119571

Closed JakobDegen closed 3 months ago

JakobDegen commented 8 months ago

I tried this:

touch empty.rs && rustc +nightly empty.rs --crate-type dylib -Crpath

I expected to see this happen: Anything but an ICE

Instead, this happened:

thread 'rustc' panicked at compiler/rustc_codegen_ssa/src/back/rpath.rs:88:43:
called `Result::unwrap()` on an `Err` value: Error { kind: InvalidInput, message: "cannot make an empty path absolute" }

The unwraps in this function all look a bit suspicious tbh:

https://github.com/rust-lang/rust/blob/139fb2214675fed8143a12f6287a3a1e6e2e866d/compiler/rustc_codegen_ssa/src/back/rpath.rs#L80-L96

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (e51e98dde 2023-12-31)
binary: rustc
commit-hash: e51e98dde6a60637b6a71b8105245b629ac3fe77
commit-date: 2023-12-31
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
Backtrace

``` thread 'rustc' panicked at compiler/rustc_codegen_ssa/src/back/rpath.rs:88:43: called `Result::unwrap()` on an `Err` value: Error { kind: InvalidInput, message: "cannot make an empty path absolute" } stack backtrace: 0: 0x7f944f610946 - std::backtrace_rs::backtrace::libunwind::trace::h44f4ca7c442c0986 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5 1: 0x7f944f610946 - std::backtrace_rs::backtrace::trace_unsynchronized::h8e72beef8f98799e at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f944f610946 - std::sys_common::backtrace::_print_fmt::had1f7322c7191a83 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f944f610946 - ::fmt::hc02075f68ac68e83 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f944f663060 - core::fmt::rt::Argument::fmt::h4fb797bf569c9381 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/core/src/fmt/rt.rs:142:9 5: 0x7f944f663060 - core::fmt::write::h6f1ea15fe9368d17 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/core/src/fmt/mod.rs:1120:17 6: 0x7f944f60436f - std::io::Write::write_fmt::haaa4143b58137630 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/io/mod.rs:1810:15 7: 0x7f944f610724 - std::sys_common::backtrace::_print::h16f1f390d8dff6aa at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f944f610724 - std::sys_common::backtrace::print::h7a4cfc718891723c at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f944f6134b7 - std::panicking::default_hook::{{closure}}::h66f09592313f4ce8 10: 0x7f944f613219 - std::panicking::default_hook::he45e0a376ce42f7d at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/panicking.rs:292:9 11: 0x7f944c36605c - std[59bbd359e4a0812d]::panicking::update_hook::>::{closure#0} 12: 0x7f944f613c06 - as core::ops::function::Fn>::call::h0ebdda5c503ff158 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/alloc/src/boxed.rs:2029:9 13: 0x7f944f613c06 - std::panicking::rust_panic_with_hook::h4f38919015b1c60f at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/panicking.rs:783:13 14: 0x7f944f613952 - std::panicking::begin_panic_handler::{{closure}}::h6e933e94ded3024f at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/panicking.rs:657:13 15: 0x7f944f610e46 - std::sys_common::backtrace::__rust_end_short_backtrace::h4efef00f0bd07d56 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f944f6136b0 - rust_begin_unwind at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/panicking.rs:645:5 17: 0x7f944f65f765 - core::panicking::panic_fmt::h3f2350d70561f7cb at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/core/src/panicking.rs:72:14 18: 0x7f944f65fe63 - core::result::unwrap_failed::hc28f4ee1a6255957 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/core/src/result.rs:1649:5 19: 0x7f944c2acd1b - rustc_codegen_ssa[dae9f9943a1cfee7]::back::rpath::get_rpath_flags 20: 0x7f944e5c5d9a - rustc_codegen_ssa[dae9f9943a1cfee7]::back::link::linker_with_args 21: 0x7f944e534f0a - rustc_codegen_ssa[dae9f9943a1cfee7]::back::link::link_natively 22: 0x7f944e1ec237 - rustc_codegen_ssa[dae9f9943a1cfee7]::back::link::link_binary 23: 0x7f944e1eb6ea - ::link 24: 0x7f944df585a1 - ::link 25: 0x7f944e62d70a - rustc_interface[8dea96efab884454]::interface::run_compiler::, rustc_driver_impl[f5f02e8ab7f89dcf]::run_compiler::{closure#0}>::{closure#0} 26: 0x7f944e5b7306 - std[59bbd359e4a0812d]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[f5f02e8ab7f89dcf]::run_compiler::{closure#0}>::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>>::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>> 27: 0x7f944e5b7133 - <::spawn_unchecked_, rustc_driver_impl[f5f02e8ab7f89dcf]::run_compiler::{closure#0}>::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>>::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>>::{closure#1} as core[f50a4b4ddb6878bb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 28: 0x7f944f61db35 - as core::ops::function::FnOnce>::call_once::hfced58be344e2c05 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/alloc/src/boxed.rs:2015:9 29: 0x7f944f61db35 - as core::ops::function::FnOnce>::call_once::h7842a7154462ae57 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/alloc/src/boxed.rs:2015:9 30: 0x7f944f61db35 - std::sys::unix::thread::Thread::new::thread_start::ha365c5d9b9d0ac99 at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/std/src/sys/unix/thread.rs:108:17 31: 0x7f94494aa9eb - 32: 0x7f944952e7cc - 33: 0x0 - 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: please attach the file at `/tmp/rusttest/tmp/rustc-ice-2024-01-04T07_26_52-1064517.txt` to your bug report note: compiler flags: --crate-type dylib -C rpath query stack during panic: end of query stack ```

JakobDegen commented 8 months ago

Also, I don't think there's an ICE-golf competition, but if there were then I might be winning

bjorn3 commented 8 months ago

In this case config.out_filename is Path::from("empty"), and as such the parent directory is the empty path and replacing it with Path::from(".") would fix this ICE.

Both parent calls can't return None as if config.out_filename were / or an empty path we would have errored out when trying to read the source code. And lib can't be / or an empty path either as we would have failed reading the crate metadata for the library in that case. The try_canonicalize calls can only fail in case of empty path or / or if env::current_dir() returns an error which I believe would have resulted in an error earlier anyway.