Rollup merge of #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 #119571.
Close #125785.
This commit is contained in:
Matthias Krüger 2024-06-05 18:21:10 +02:00 committed by GitHub
commit e2ea7d82b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -85,6 +85,11 @@ fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsStrin
// Strip filenames // Strip filenames
let lib = lib.parent().unwrap(); let lib = lib.parent().unwrap();
let output = config.out_filename.parent().unwrap(); let output = config.out_filename.parent().unwrap();
// If output or lib is empty, just assume it locates in current path
let lib = if lib == Path::new("") { Path::new(".") } else { lib };
let output = if output == Path::new("") { Path::new(".") } else { output };
let lib = try_canonicalize(lib).unwrap(); let lib = try_canonicalize(lib).unwrap();
let output = try_canonicalize(output).unwrap(); let output = try_canonicalize(output).unwrap();
let relative = path_relative_from(&lib, &output) let relative = path_relative_from(&lib, &output)

View File

@ -57,6 +57,22 @@ fn test_rpath_relative() {
} }
} }
#[test]
fn test_rpath_relative_issue_119571() {
let config = &mut RPathConfig {
libs: &[],
out_filename: PathBuf::from("rustc"),
has_rpath: true,
is_like_osx: false,
linker_is_gnu: true,
};
// Should not panic when out_filename only contains filename.
// Issue 119571
let _ = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
// Should not panic when lib only contains filename.
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
}
#[test] #[test]
fn test_xlinker() { fn test_xlinker() {
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]); let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);