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

ICE: -Crpath without -o #119571

Closed
JakobDegen opened this issue Jan 4, 2024 · 2 comments · Fixed by #125861
Closed

ICE: -Crpath without -o #119571

JakobDegen opened this issue Jan 4, 2024 · 2 comments · Fixed by #125861
Labels
A-cli Area: Command line interface to the compiler. A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@JakobDegen
Copy link
Contributor

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:

fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> OsString {
// Mac doesn't appear to support $ORIGIN
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
// Strip filenames
let lib = lib.parent().unwrap();
let output = config.out_filename.parent().unwrap();
let lib = try_canonicalize(lib).unwrap();
let output = try_canonicalize(output).unwrap();
let relative = path_relative_from(&lib, &output)
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
let mut rpath = OsString::from(prefix);
rpath.push("/");
rpath.push(relative);
rpath
}

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 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::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::<alloc[9e8bbef5278c9f16]::boxed::Box<rustc_driver_impl[f5f02e8ab7f89dcf]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f944f613c06 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::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 - <rustc_codegen_llvm[8394d916703c6365]::LlvmCodegenBackend as rustc_codegen_ssa[dae9f9943a1cfee7]::traits::backend::CodegenBackend>::link
  24:     0x7f944df585a1 - <rustc_interface[8dea96efab884454]::queries::Linker>::link
  25:     0x7f944e62d70a - rustc_interface[8dea96efab884454]::interface::run_compiler::<core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>, rustc_driver_impl[f5f02e8ab7f89dcf]::run_compiler::{closure#0}>::{closure#0}
  26:     0x7f944e5b7306 - std[59bbd359e4a0812d]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[8dea96efab884454]::util::run_in_thread_with_globals<rustc_interface[8dea96efab884454]::util::run_in_thread_pool_with_globals<rustc_interface[8dea96efab884454]::interface::run_compiler<core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>, 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 - <<std[59bbd359e4a0812d]::thread::Builder>::spawn_unchecked_<rustc_interface[8dea96efab884454]::util::run_in_thread_with_globals<rustc_interface[8dea96efab884454]::util::run_in_thread_pool_with_globals<rustc_interface[8dea96efab884454]::interface::run_compiler<core[f50a4b4ddb6878bb]::result::Result<(), rustc_span[44814e3eac9e4a35]::ErrorGuaranteed>, 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 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hfced58be344e2c05
                               at /rustc/e51e98dde6a60637b6a71b8105245b629ac3fe77/library/alloc/src/boxed.rs:2015:9
  29:     0x7f944f61db35 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::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 - <unknown>
  32:     0x7f944952e7cc - <unknown>
  33:                0x0 - <unknown>

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 JakobDegen added A-linkage Area: linking into static, shared libraries and binaries I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ C-bug Category: This is a bug. A-cli Area: Command line interface to the compiler. labels Jan 4, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 4, 2024
@JakobDegen JakobDegen removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 4, 2024
@JakobDegen
Copy link
Contributor Author

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

@bjorn3
Copy link
Member

bjorn3 commented Jan 4, 2024

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.

@bors bors closed this as completed in e2ea7d8 Jun 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2024
Rollup merge of rust-lang#125861 - name1e5s:fix/rpath_null_panic, r=michaelwoerister

rustc_codegen_ssa: fix `get_rpath_relative_to_output` panic when lib only contains file name

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->

When compiles program with `-C rpath=yes` but with no output filename specified, or with filename ONLY, we will get an ICE for now. Fix it by treat empty `output` path in `get_rpath_relative_to_output`  as current dir.

Before this patch:

```bash
rustc -C prefer_dynamic=yes -C rpath=yes -O h.rs  # ICE, no output filename specified
rustc -o hello -C prefer_dynamic=yes -C rpath=yes -O h.rs # ICE, output filename has no path
rustc -o ./hello -C prefer_dynamic=yes -C rpath=yes -O h.rs # Works
```

All those examples work after the patch.

Close rust-lang#119571.
Close rust-lang#125785.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command line interface to the compiler. A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
3 participants