Skip to content

Produce sane CARGO when invoked through ld.so #10115

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,32 @@ impl Config {
}

let exe = from_current_exe()
.ok()
.filter(|exe| {
if let Some(name) = exe.file_name().and_then(|name| name.to_str()) {
if name.starts_with("ld-")
&& (name.ends_with(".so") || name.contains(".so."))
{
// Cargo seems to be executed directly through the dynamic linker
// (e.g., /usr/lib64/ld-linux-x86-64.so.2), which is then what
// current_exe points at. That's not the path to Cargo itself
// though, so we need to fall back to argv[0], which the step that
// set up the invocation of Cargo this way has hopefully set
// correctly (likely with exec -a).
//
// See
//
// https://github.com/rust-lang/cargo/issues/10113
//
// and
//
// https://linux.die.net/man/8/ld-linux.so
return false;
}
}
true
})
.ok_or(())
.or_else(|_| from_argv())
.with_context(|| "couldn't get the path to cargo executable")?;
Ok(exe)
Expand Down
52 changes: 50 additions & 2 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use cargo_test_support::compare::assert_match_exact;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::tools;
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
use cargo_test_support::{basic_manifest, cargo_exe, cross_compile, is_coarse_mtime, project};
use cargo_test_support::{execs, process, tools};
use cargo_test_support::{rustc_host, sleep_ms, slow_cpu_multiplier, symlink_supported};
use cargo_util::paths::remove_dir_all;
use std::env;
Expand Down Expand Up @@ -137,6 +137,54 @@ fn custom_build_env_vars() {
p.cargo("build --features bar_feat").run();
}

#[cargo_test]
fn issue_10113() {
use std::path::Path;

let p = project()
.file(
"build.rs",
r#"
use std::env;
use std::path::Path;

fn main() {
let cargo = dbg!(env::var("CARGO").unwrap());
let cargo = Path::new(&cargo);
assert!(cargo.ends_with(&format!("cargo{}", env::consts::EXE_SUFFIX)));
assert!(cargo.exists());
}
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("build").run();

if cfg!(target_os = "linux") {
let ld = if cfg!(target_arch = "x86_64") {
Some(Path::new("/usr/lib64/ld-linux-x86-64.so.2"))
} else if cfg!(target_arch = "x86") {
Some(Path::new("/usr/lib/ld-linux.so.2"))
} else {
None
};

// This test needs to be updated with the path to ld.so for this arch.
// We error on an unknown ld path to catch typos in the `cfg!`s above and such.
let ld = ld.expect("path to ld-linux.so unknown for current architecture");
assert!(ld.exists());
let mut proc = process(ld);
proc.cwd(p.root());
proc.arg(&cargo_exe());
proc.arg("build");
// Touch the file so build is re-run.
p.change_file("src/lib.rs", "");
let mut execs = execs().with_process_builder(proc);
execs.run();
}
}

#[cargo_test]
fn custom_build_env_var_rustflags() {
let rustflags = "--cfg=special";
Expand Down