Skip to content

cc_wrapper: Spill arguments back to a response file #430

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

Conversation

ParkMyCar
Copy link
Contributor

Fixes #421

This PR tweaks the *cc_wraper.sh scripts to spill command line arguments from response files, back into a file after sanitizing. Previously they were expanded into the cmd variable which would then get specified on the command line, possibly leading to an overflow.

Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

Thanks for picking this up!

if [[ ${!i} == @* && -r "${i:1}" ]]; then
# Create a temporary file that we'll spill sanitized options to since they
# were originally read from a response file.
temp_file=$(mktemp)
Copy link
Member

Choose a reason for hiding this comment

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

Can we do this only if the arguments have a length that gets somewhat close to the limit? Otherwise we are just doing needless file I/O.

done <"${!i:1}"
mv "${temp_file}" "${!i:1}"
Copy link
Member

Choose a reason for hiding this comment

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

This replaces an input file, which is something actions should avoid. Could we rewrite the argument to point to the temporary file instead?

done <"${!i:1}"
mv "${temp_file}" "${!i:1}"
else
Copy link
Member

Choose a reason for hiding this comment

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

The temporary file should be deleted after the compiler returns.

@faximan
Copy link

faximan commented Apr 9, 2025

Hello! What is the status of this PR, it would be great to see it merged!

We actually ran into a similar but different issue: golang (which is using our hermetic llvm toolchain for linking) will in certain situations produce response files where all arguments are quoted: https://github.com/golang/go/blob/ecc06f0db79193a4fe16138148c7eb26d9af96f1/src/cmd/link/internal/ld/lib.go#L2123-L2124

This does not work with the current sanitization logic: clang supports quotes in the response file but not when "unrolled" into regular args on the command line, leading to errors like clang: error: no such file or directory: '"-m64"'.

dfreese added a commit to dfreese/toolchains_llvm that referenced this pull request Apr 9, 2025
The cc_wrapper.sh scripts inspects response files and sanitizes each
line within them.  How the logic is structured currently causes all of
these arguments to the be directly used later in the script.  This can
run into argument overflows.  It can also run into problems where
quoting in response files and from args is treated differently.

This addresses some of the comments in bazel-contrib#430, namely cleaning up files
and not overwriting the input files.  I have not tried to apply a
threshold here as that is best left up to the tooling that previously
decided to use response files, which can cause difficult to debug
changes in behavior.

Fixes bazel-contrib#421
dfreese added a commit to dfreese/toolchains_llvm that referenced this pull request Apr 9, 2025
The cc_wrapper.sh scripts inspects response files and sanitizes each
line within them.  How the logic is structured currently causes all of
these arguments to the be directly used later in the script.  This can
run into argument overflows.  It can also run into problems where
quoting in response files and from args is treated differently.

This addresses some of the comments in bazel-contrib#430, namely cleaning up files
and not overwriting the input files.  I have not tried to apply a
threshold here as that is best left up to the tooling that previously
decided to use response files, which can cause difficult to debug
changes in behavior.

Fixes bazel-contrib#421
dfreese added a commit to dfreese/toolchains_llvm that referenced this pull request Apr 9, 2025
The cc_wrapper.sh scripts inspects response files and sanitizes each
line within them.  How the logic is structured currently causes all of
these arguments to the be directly used later in the script.  This can
run into argument overflows.  It can also run into problems where
quoting in response files and from args is treated differently.

This addresses some of the comments in bazel-contrib#430, namely cleaning up files
and not overwriting the input files.  I have not tried to apply a
threshold here as that is best left up to the tooling that previously
decided to use response files, which can cause difficult to debug
changes in behavior.

This conflicts with bazel-contrib#479, but is trying to address the same underlying
problem of quoted arguments in response files as the result of golang's
link actions.

Fixes bazel-contrib#421
fmeum pushed a commit that referenced this pull request Apr 10, 2025
The cc_wrapper.sh scripts inspects response files and sanitizes each
line within them. How the logic is structured currently causes all of
these arguments to the be directly used later in the script. This can
run into argument overflows. It can also run into problems where quoting
in response files and from args is treated differently.

This addresses some of the comments in #430, namely cleaning up files
and not overwriting the input files. I have not tried to apply a
threshold here as that is best left up to the tooling that previously
decided to use response files, which can cause difficult to debug
changes in behavior.

This conflicts with #479, but is trying to address the same underlying
problem of quoted arguments in response files as the result of golang's
link actions.

Fixes #421
@mering
Copy link
Contributor

mering commented Apr 10, 2025

@fmeum I think this can be closed after #480 has been merged.

@fmeum fmeum closed this Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why are response files expanded in osx_cc_wrapper?
4 participants