Skip to content
Open
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
1 change: 1 addition & 0 deletions src/compiler/cicc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub fn generate_compile_commands(
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

#[cfg(not(feature = "dist-client"))]
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ pub struct SingleCompileCommand {
pub arguments: Vec<OsString>,
pub env_vars: Vec<(OsString, OsString)>,
pub cwd: PathBuf,
/// Response file directory to clean up after compilation.
pub _response_file_dir: Option<TempDir>,
}

#[async_trait]
Expand Down Expand Up @@ -196,6 +198,7 @@ impl CompileCommandImpl for SingleCompileCommand {
arguments,
env_vars,
cwd,
..
} = self;
// Resolve compiler avoiding ccache wrappers to prevent double-caching.
let resolved_executable = resolve_compiler_avoiding_wrapper(executable, env_vars);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/cudafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub fn generate_compile_commands(
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

#[cfg(not(feature = "dist-client"))]
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ pub fn generate_compile_commands(
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

Ok((command, None, Cacheable::Yes))
Expand Down
1 change: 1 addition & 0 deletions src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ where
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

#[cfg(not(feature = "dist-client"))]
Expand Down
1 change: 1 addition & 0 deletions src/compiler/msvc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ fn generate_compile_commands(
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

#[cfg(not(feature = "dist-client"))]
Expand Down
60 changes: 53 additions & 7 deletions src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use std::future::Future;
use std::hash::Hash;
#[cfg(feature = "dist-client")]
use std::io;
use std::io::{BufReader, Read};
use std::io::{BufReader, Read, Write};
use std::iter;
use std::path::{Path, PathBuf};
use std::pin::Pin;
Expand Down Expand Up @@ -257,8 +257,9 @@ where
.tempdir()
.context("Failed to create temp dir")?;
let dep_file = temp_dir.path().join("deps.d");
let (rsp_args, _rsp_dir) = maybe_write_response_file(arguments.to_vec())?;
let mut cmd = creator.clone().new_command_sync(executable);
cmd.args(arguments)
cmd.args(&rsp_args)
.args(&["--emit", "dep-info"])
.arg("-o")
.arg(&dep_file)
Expand Down Expand Up @@ -381,8 +382,9 @@ async fn get_compiler_outputs<T>(
where
T: Clone + CommandCreatorSync,
{
let (rsp_args, _rsp_dir) = maybe_write_response_file(arguments)?;
let mut cmd = creator.clone().new_command_sync(executable);
cmd.args(&arguments)
cmd.args(&rsp_args)
.args(&["--print", "file-names"])
.env_clear()
.envs(env_vars.to_vec())
Expand Down Expand Up @@ -1120,6 +1122,45 @@ impl Iterator for ExpandResponseFile<'_> {
}
}

fn maybe_write_response_file(
flat_args: Vec<OsString>,
) -> Result<(Vec<OsString>, Option<tempfile::TempDir>)> {
// On Windows there is a hard limit of ~32KB, so we cut off at 30KB to
// give some buffer just incase.
#[cfg(windows)]
let threshold: usize = 30 * 1024;
// On unix the limit is defined by ARG_MAX. If its not explicitly set we set it to 1MB
// which is fairly large but lower than the ~2MB that it defaults to on most systems.
#[cfg(unix)]
let threshold: usize = std::env::var("ARG_MAX")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(1024 * 1024);
Comment on lines +1128 to +1138

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides ARG_MAX, this might fail if a single argument in the list exceeds MAX_ARG_STRLEN, which is 32 page sizes (128kb on most systems). This is a real issue (#2815) and I can confirm that lowering this threshold to 128kb solves it.

Even though setting a defensive limit solves the problem (32kb would cover both Windows and a single arg problem), it needs at least a comment explanation or a separate single-argument threshold to make it more transparent and less fragile to future changes.


let total_arg_len: usize = flat_args.iter().map(|a| a.len() + 1).sum();
if total_arg_len <= threshold {
return Ok((flat_args, None));
}

let dir = tempfile::Builder::new()
.prefix("sccache-rsp")
.tempdir()
.context("Failed to create response file directory")?;
let rsp_path = dir.path().join("args");
let mut rsp_file = fs::File::create(&rsp_path).context("Failed to create response file")?;
for arg in &flat_args {
let line = arg
.to_str()
.ok_or_else(|| anyhow!("rustc argument not valid UTF-8: {:?}", arg))?;
writeln!(rsp_file, "{}", line).context("Failed to write response file")?;
}
rsp_file.flush().context("Failed to flush response file")?;
Ok((
vec![OsString::from(format!("@{}", rsp_path.display()))],
Some(dir),
))
}

fn parse_arguments(arguments: &[OsString], cwd: &Path) -> CompilerArguments<ParsedArguments> {
let mut args = vec![];

Expand Down Expand Up @@ -1794,14 +1835,19 @@ impl<T: CommandCreatorSync> Compilation<T> for RustCompilation {

trace!("[{}]: compile", crate_name);

let flat_args: Vec<OsString> = arguments
.iter()
.flat_map(|arg| arg.iter_os_strings())
.collect();

let (command_args, rsp_dir) = maybe_write_response_file(flat_args)?;

let command = SingleCompileCommand {
executable: executable.to_owned(),
arguments: arguments
.iter()
.flat_map(|arg| arg.iter_os_strings())
.collect(),
arguments: command_args,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: rsp_dir,
};

#[cfg(not(feature = "dist-client"))]
Expand Down
1 change: 1 addition & 0 deletions src/compiler/tasking_vx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ fn generate_compile_commands(
arguments,
env_vars: env_vars.to_owned(),
cwd: cwd.to_owned(),
_response_file_dir: None,
};

Ok((command, None, Cacheable::Yes))
Expand Down
1 change: 1 addition & 0 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub fn try_compile_command_to_dist(
arguments,
env_vars,
cwd,
..
} = command;
Some(CompileCommand {
executable: executable.into_os_string().into_string().ok()?,
Expand Down
Loading