Skip to content
Merged
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 changelog.d/+cargo-prelude-quoting.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The cargo-on-EESSI prelude quotes `CC` and `LIBRARY_PATH`. `CC='gcc; echo pwned'` is stored as the linker, and it is not run.
3 changes: 2 additions & 1 deletion docs/orgmode/howto/pypi-extend.org
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ a residual, not a silent SAT edge. A PEP 518 ~build_system~
block with ~build-backend = mesonpy~ adds the listed build requirements
and emits ~wrap_mode=default~ plus the same cargo-on-EESSI isolation
prelude used by Cargo leftovers: drop host rustc wrappers, set
~LINKER=${CC:-gcc}~, put the EESSI compat ~ld~ on ~PATH~, and point
~LINKER="$CC"~ (an empty ~CC~ becomes ~gcc~, and the value is not
evaluated), put the EESSI compat ~ld~ on ~PATH~, and point
~RUSTFLAGS -B~ at that ~ld~. The cargo target triple and compat arch
come from the build host (~rustc -vV~, ~uname -m~), not from a
plan-time x86_64 literal. Hatchling and ~meson-python~ already on
Expand Down
4 changes: 3 additions & 1 deletion docs/orgmode/reference/cli.org
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ it. Do not hand-edit the emitted recipe for those names. A PEP 518
~build_system~ object with ~build-backend = mesonpy~ is the mesonpy hint:
listed ~requires~ become build deps in the Boolean satisfiability (SAT) solve, and the emitter writes
~wrap_mode=default~ plus the shared cargo-on-EESSI isolation prelude
(host wrappers off, ~LINKER=${CC:-gcc}~, compat ~ld~ from ~uname -m~).
(host wrappers off, ~LINKER="$CC"~ after an empty ~CC~ becomes ~gcc~,
compat ~ld~ from ~uname -m~). ~CC~ and ~LIBRARY_PATH~ stay quoted, so a
value in either is not run as shell.
Hatchling stays a ~Python-bundle-PyPI~ module when the robot provides
it. See [[file:../howto/pypi-extend.org][site Python extras]].

Expand Down
7 changes: 5 additions & 2 deletions llms-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,8 @@ a residual, not a silent SAT edge. A PEP 518 `build_system`
block with `build-backend = mesonpy` adds the listed build requirements
and emits `wrap_mode=default` plus the same cargo-on-EESSI isolation
prelude used by Cargo leftovers: drop host rustc wrappers, set
`LINKER=${CC:-gcc}`, put the EESSI compat `ld` on `PATH`, and point
`LINKER="$CC"` (an empty `CC` becomes `gcc`, and the value is not
evaluated), put the EESSI compat `ld` on `PATH`, and point
`RUSTFLAGS -B` at that `ld`. The cargo target triple and compat arch
come from the build host (`rustc -vV`, `uname -m`), not from a
plan-time x86_64 literal. Hatchling and `meson-python` already on
Expand Down Expand Up @@ -6885,7 +6886,9 @@ it. Do not hand-edit the emitted recipe for those names. A PEP 518
`build_system` object with `build-backend = mesonpy` is the mesonpy hint:
listed `requires` become build deps in the Boolean satisfiability (SAT) solve, and the emitter writes
`wrap_mode=default` plus the shared cargo-on-EESSI isolation prelude
(host wrappers off, `LINKER=${CC:-gcc}`, compat `ld` from `uname -m`).
(host wrappers off, `LINKER="$CC"` after an empty `CC` becomes `gcc`,
compat `ld` from `uname -m`). `CC` and `LIBRARY_PATH` stay quoted, so a
value in either is not run as shell.
Hatchling stays a `Python-bundle-PyPI` module when the robot provides
it. See [site Python extras](https://eb-stack.rgoswami.me/../howto/pypi-extend.html.md).

Expand Down
43 changes: 38 additions & 5 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ pub fn eessi_cargo_host_isolation() -> &'static str {
"unset RUSTC_WRAPPER CARGO_BUILD_RUSTC_WRAPPER RUSTC_WORKSPACE_WRAPPER RUSTFLAGS CARGO_ENCODED_RUSTFLAGS && ",
r#"eval $(env | awk -F= '/^CARGO_TARGET_[A-Z0-9_]*_(LINKER|RUSTFLAGS)=/{print "unset "$1}') && "#,
"export CARGO_HOME=%(builddir)s/.cargohome && ",
"export LINKER=${CC:-gcc} && ",
// CC and LIBRARY_PATH are data. An unquoted expansion is reparsed by
// `eval`, so `CC='gcc; echo pwned'` would run. Default an empty CC,
// keep the value quoted, and only allow a triple that is a variable name.
r#"if [ -z "${CC:-}" ]; then CC=gcc; fi && "#,
r#"export LINKER="$CC" && "#,
// An ordinary recipe has no rustc. The host triple is best-effort,
// the same way scripts/eessi-extend-eb.sh discovers it, so a missing
// compiler does not drop the compat-layer linker from PATH.
r#"_triple=""; if _host=$(rustc -vV 2>/dev/null); then _triple=$(printf '%s\n' "$_host" | awk '/^host:/{print $2}'); fi && "#,
r#"_triple_env=$(printf %s "$_triple" | tr "[:lower:]-" "[:upper:]_") && "#,
r#"if [ -n "$_triple_env" ]; then eval export CARGO_TARGET_${_triple_env}_LINKER=${CC:-gcc}; fi && "#,
r#"_triple_env=$(printf %s "$_triple" | tr "[:lower:]-" "[:upper:]_" | tr -cd 'A-Z0-9_') && "#,
r#"if [ -n "$_triple_env" ]; then eval "export CARGO_TARGET_${_triple_env}_LINKER=\"\$CC\""; fi && "#,
r#"_arch=$(uname -m) && "#,
r#"_ebld=${EESSI_EPREFIX:-${EESSI_CVMFS_REPO:-/cvmfs/software.eessi.io}/versions/${EESSI_VERSION:-2026.06}/compat/linux/${_arch}}/usr/bin && "#,
r#"export PATH="$_ebld:$PATH" && "#,
r#"export RUSTFLAGS="-C link-arg=-B$_ebld $(printf -- '-L %s ' $(echo ${LIBRARY_PATH:-} | tr ':' ' '))" && "#,
r#"_rlibs= && _ifs="$IFS" && IFS=: && set -f && for _dir in ${LIBRARY_PATH:-}; do [ -n "$_dir" ] || continue; _rlibs="${_rlibs} -L ${_dir}"; done && IFS="$_ifs" && set +f && "#,
r#"export RUSTFLAGS="-C link-arg=-B$_ebld${_rlibs}" && "#,
)
}

Expand Down Expand Up @@ -518,10 +523,38 @@ pyo3 = "0.22"
"a missing rustc must not abort the prelude"
);
assert!(prelude.contains("link-arg=-B"));
assert!(prelude.contains("LINKER=${CC:-gcc}"));
assert!(prelude.contains("export LINKER=\"$CC\""));
assert!(prelude.contains("tr -cd 'A-Z0-9_'"));
assert!(!prelude.contains("LINKER=${CC:-gcc}"));
assert!(
!prelude.contains("X86_64"),
"triple and compat arch are runtime, not a plan-time x86_64 literal:\n{prelude}"
);
}

#[test]
fn eessi_cargo_isolation_does_not_run_a_command_hidden_in_cc() {
let prelude = eessi_cargo_host_isolation();
// EasyBuild substitutes %(builddir)s before the shell sees the line.
// A raw parenthesis is a syntax error, so the substitution has to
// happen here too or the injection never runs.
let script = format!("{} true\n", prelude.replace("%(builddir)s", "/tmp"));
let out = std::process::Command::new("bash")
.arg("-c")
.arg(&script)
.env("CC", "gcc; echo CC_PWNED")
.env("LIBRARY_PATH", "/usr/lib")
.output()
.expect("bash");
let text = format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(
out.status.success() && !text.contains("CC_PWNED"),
"status {:?} prelude injected or failed:\nPRELUDE {prelude}\nOUT {text}",
out.status
);
}
}
5 changes: 4 additions & 1 deletion tests/pypi_cran_extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ fn assert_eessi_cargo_isolation(text: &str) {
&& text.contains("${EESSI_EPREFIX:-")
&& text.contains("compat/linux/${_arch}}/usr/bin")
&& text.contains("link-arg=-B")
&& text.contains("LINKER=${CC:-gcc}")
&& text.contains("export LINKER=\"$CC\"")
&& text.contains("tr -cd")
&& !text.contains("LINKER=${CC:-gcc}")
&& !text.contains("echo ${LIBRARY_PATH")
&& !text.contains("X86_64"),
"cargo on EESSI must share one host-derived isolation prelude:\n{text}"
);
Expand Down
Loading