Skip to content

Commit

Permalink
ensure envvars are used for ldd invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Nov 5, 2024
1 parent 89aee36 commit 569d9bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl<T: CommandCreatorSync, I: CCompilerImpl> Compiler<T> for CCompiler<I> {
#[cfg(feature = "dist-client")]
fn get_toolchain_packager(&self) -> Box<dyn pkg::ToolchainPackager> {
Box::new(CToolchainPackager {
env_vars: vec![],
executable: self.executable.clone(),
kind: self.compiler.kind(),
})
Expand Down Expand Up @@ -1190,6 +1191,7 @@ impl<T: CommandCreatorSync, I: CCompilerImpl> Compilation<T> for CCompilation<I>
preprocessed_input,
executable,
compiler,
env_vars,
..
} = *self;
trace!("Dist inputs: {:?}", parsed_args.input);
Expand All @@ -1203,6 +1205,7 @@ impl<T: CommandCreatorSync, I: CCompilerImpl> Compilation<T> for CCompilation<I>
extra_hash_files: parsed_args.extra_hash_files,
});
let toolchain_packager = Box::new(CToolchainPackager {
env_vars,
executable,
kind: compiler.kind(),
});
Expand Down Expand Up @@ -1299,6 +1302,7 @@ impl pkg::InputsPackager for CInputsPackager {
#[cfg(feature = "dist-client")]
#[allow(unused)]
struct CToolchainPackager {
env_vars: Vec<(OsString, OsString)>,
executable: PathBuf,
kind: CCompilerKind,
}
Expand All @@ -1315,7 +1319,7 @@ impl pkg::ToolchainPackager for CToolchainPackager {
debug!("Generating toolchain {}", self.executable.display());
let mut package_builder = pkg::ToolchainPackageBuilder::new();
package_builder.add_common()?;
package_builder.add_executable_and_deps(self.executable.clone())?;
package_builder.add_executable_and_deps(&self.env_vars, self.executable.clone())?;

// Helper to use -print-file-name and -print-prog-name to look up
// files by path.
Expand Down Expand Up @@ -1356,7 +1360,7 @@ impl pkg::ToolchainPackager for CToolchainPackager {
let add_named_prog =
|builder: &mut pkg::ToolchainPackageBuilder, name: &str| -> Result<()> {
if let Some(path) = named_file("prog", name) {
builder.add_executable_and_deps(path)?;
builder.add_executable_and_deps(&self.env_vars, path)?;
}
Ok(())
};
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ impl pkg::ToolchainPackager for RustToolchainPackager {

let bins_path = sysroot.join(BINS_DIR);
let sysroot_executable = bins_path.join("rustc").with_extension(EXE_EXTENSION);
package_builder.add_executable_and_deps(sysroot_executable)?;
package_builder.add_executable_and_deps(&[], sysroot_executable)?;

package_builder.add_dir_contents(&bins_path)?;
if BINS_DIR != LIBS_DIR {
Expand Down
22 changes: 18 additions & 4 deletions src/dist/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod toolchain_imp {
use super::SimplifyPath;
use fs_err as fs;
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::io::{Read, Write};
use std::path::{Component, Path, PathBuf};
use std::process;
Expand Down Expand Up @@ -97,7 +98,11 @@ mod toolchain_imp {
self.add_dir(PathBuf::from("/tmp"))
}

pub fn add_executable_and_deps(&mut self, executable: PathBuf) -> Result<()> {
pub fn add_executable_and_deps(
&mut self,
env_vars: &[(OsString, OsString)],
executable: PathBuf,
) -> Result<()> {
let mut remaining = vec![executable];
while let Some(obj_path) = remaining.pop() {
assert!(obj_path.is_absolute());
Expand All @@ -110,10 +115,11 @@ mod toolchain_imp {
if self.file_set.contains_key(&tar_path) {
continue;
}
let ldd_libraries = find_ldd_libraries(&obj_path).with_context(|| {
let ldd_libraries = find_ldd_libraries(env_vars, &obj_path).with_context(|| {
format!("Failed to analyse {} with ldd", obj_path.display())
})?;
remaining.extend(ldd_libraries);
trace!("add_executable_and_deps {}", obj_path.display());
self.file_set.insert(tar_path, obj_path);
}
Ok(())
Expand All @@ -135,6 +141,7 @@ mod toolchain_imp {
{
return Ok(());
}
trace!("add_dir {}", dir_path.display());
let tar_path = self.tarify_path(&dir_path)?;
self.dir_set.insert(tar_path, dir_path);
Ok(())
Expand All @@ -148,6 +155,7 @@ mod toolchain_imp {
file_path.to_string_lossy()
))
}
trace!("add_file {}", file_path.display());
let tar_path = self.tarify_path(&file_path)?;
self.file_set.insert(tar_path, file_path);
Ok(())
Expand Down Expand Up @@ -252,12 +260,18 @@ mod toolchain_imp {
// - static + non-PIE = ET_EXEC, ldd stderrs something like "\tnot a dynamic executable" or
// "ldd: a.out: Not a valid dynamic program" and exits with code 1
//
fn find_ldd_libraries(executable: &Path) -> Result<Vec<PathBuf>> {
fn find_ldd_libraries(
env_vars: &[(OsString, OsString)],
executable: &Path,
) -> Result<Vec<PathBuf>> {
let process::Output {
status,
stdout,
stderr,
} = process::Command::new("ldd").arg(executable).output()?;
} = process::Command::new("ldd")
.envs(env_vars.to_vec())
.arg(executable)
.output()?;

// Not a file ldd can handle. This can be a non-executable, or a static non-PIE
if !status.success() {
Expand Down

0 comments on commit 569d9bd

Please sign in to comment.