use super::{InstrumentRequest, Instrumented, Manifest, Skipped};
use std::path::{Path, PathBuf};
use std::process::Command;
pub const VERIFIABLE_SANITIZERS: &[(&str, &str)] =
&[("address", "asan"), ("thread", "tsan"), ("memory", "msan")];
const UBSAN_ALIASES: &[&str] = &["undefined", "ubsan"];
pub const UBSAN_NOTE: &str = "rustc-has-no-ubsan(no -Zsanitizer=undefined exists on any Rust \
target; the Rust equivalent for the integer class is \
-C overflow-checks=on, which panics -- see the overflow oracle)";
pub const OVERFLOW_CHECKS_LABEL: &str = "rust-overflow-checks";
pub fn instrument(project: &Path, request: &InstrumentRequest) -> Manifest {
let context = |skipped: Skipped| -> Manifest {
Manifest::Skipped(Skipped {
build_system: Some("cargo".to_string()),
project: Some(project.to_path_buf()),
..skipped
})
};
if which("cargo").is_none() {
return context(Skipped::new("cargo-not-on-path"));
}
if which("rustup").is_none() {
return context(Skipped::new(
"rustup-not-on-path(needed to select a nightly toolchain)",
));
}
if !nightly_is_installed() {
return context(Skipped::new(
"nightly-toolchain-unavailable(install: rustup toolchain install nightly)",
));
}
let Some(triple) = host_triple() else {
return context(Skipped::new("host-triple-unavailable"));
};
let Some(supported) = supported_sanitizers(&triple) else {
return context(Skipped {
target: Some(triple.clone()),
..Skipped::new(format!("sanitizer-capability-unknown(target={triple})"))
});
};
let plan = partition_requested(&request.sanitizers, &supported);
if plan.wanted.is_empty() {
let reason = plan.empty_reason();
return context(Skipped {
target: Some(triple),
requested: Some(request.sanitizers.join(",")),
supported: Some(supported.join(",")),
notes: plan.notes,
..Skipped::new(reason)
});
}
let manifest_path = project.join("Cargo.toml");
let bin = match request.bin.clone() {
Some(name) => name,
None => match sole_bin_target(&cargo_metadata(&manifest_path).unwrap_or_default()) {
Some(name) => name,
None => {
return context(Skipped::new("binary-target-ambiguous(pass --bin NAME)"));
}
},
};
if request.build_std && !rust_src_is_installed() {
return context(Skipped::new(
"rust-src-missing(needed by -Zbuild-std; rustup component add rust-src \
--toolchain nightly)",
));
}
let target_dir = request
.out_dir
.clone()
.unwrap_or_else(|| project.join("target-instrumented"));
let rustflags = rustflags(&plan.wanted);
let mut command = Command::new("cargo");
command
.arg("+nightly")
.arg("build")
.arg("--manifest-path")
.arg(&manifest_path)
.arg("--bin")
.arg(&bin)
.arg("--target")
.arg(&triple)
.arg("--target-dir")
.arg(&target_dir)
.env("RUSTFLAGS", &rustflags)
.env_remove("CARGO_ENCODED_RUSTFLAGS");
if request.build_std {
command.arg("-Zbuild-std");
}
let output = match command.output() {
Ok(output) => output,
Err(e) => {
return context(Skipped {
rustflags: Some(rustflags),
..Skipped::new(format!("instrumented-build-not-runnable({e})"))
})
}
};
if !output.status.success() {
let code = output
.status
.code()
.map(|c| c.to_string())
.unwrap_or_else(|| "signal".to_string());
return context(Skipped {
rustflags: Some(rustflags),
build_log_tail: Some(log_tail(&output.stdout, &output.stderr)),
..Skipped::new(format!("instrumented-build-failed(exit={code})"))
});
}
let binary = target_dir
.join(&triple)
.join("debug")
.join(format!("{bin}{}", std::env::consts::EXE_SUFFIX));
if !binary.is_file() {
return context(Skipped {
binary: Some(binary.clone()),
rustflags: Some(rustflags),
..Skipped::new(format!(
"instrumented-binary-not-found({})",
binary.display()
))
});
}
let detected = crate::engine::common::detect_instrumentation(&binary);
if let Some(reason) = verification_gap(&plan.wanted, &detected) {
return context(Skipped {
binary: Some(binary),
rustflags: Some(rustflags),
notes: plan.notes,
..Skipped::new(reason)
});
}
Manifest::Instrumented(Instrumented {
build_system: "cargo".to_string(),
toolchain: "nightly".to_string(),
target: triple,
bin,
binary,
rustflags,
build_std: request.build_std,
instrumentation: detected,
unsupported_requested: plan.unsupported,
notes: plan.notes,
})
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SanitizerPlan {
pub wanted: Vec<String>,
pub unsupported: Vec<String>,
pub unverifiable: Vec<String>,
pub notes: Vec<String>,
}
impl SanitizerPlan {
pub fn empty_reason(&self) -> String {
if self.unsupported.is_empty() && !self.unverifiable.is_empty() {
format!("sanitizer-not-verifiable({})", self.unverifiable.join(","))
} else {
"sanitizer-unsupported-on-target".to_string()
}
}
}
pub fn partition_requested(requested: &[String], supported: &[String]) -> SanitizerPlan {
let mut plan = SanitizerPlan::default();
let mut saw_ubsan = false;
for name in requested {
let name = name.trim().to_lowercase();
if name.is_empty() {
continue;
}
if UBSAN_ALIASES.contains(&name.as_str()) {
saw_ubsan = true;
push_unique(&mut plan.unsupported, "undefined");
continue;
}
if !supported.contains(&name) {
push_unique(&mut plan.unsupported, &name);
continue;
}
if !VERIFIABLE_SANITIZERS.iter().any(|(s, _)| *s == name) {
push_unique(&mut plan.unverifiable, &name);
continue;
}
push_unique(&mut plan.wanted, &name);
}
if saw_ubsan {
plan.notes.push(UBSAN_NOTE.to_string());
}
if !plan.unverifiable.is_empty() {
plan.notes.push(format!(
"cxg-cannot-verify({}): no symbol marker distinguishes this build, and cxg does not \
report instrumentation it cannot read back out of the artefact",
plan.unverifiable.join(",")
));
}
plan
}
fn push_unique(into: &mut Vec<String>, value: &str) {
if !into.iter().any(|v| v == value) {
into.push(value.to_string());
}
}
pub fn label_for(sanitizer: &str) -> Option<&'static str> {
VERIFIABLE_SANITIZERS
.iter()
.find(|(name, _)| *name == sanitizer)
.map(|(_, label)| *label)
}
pub fn rustflags(wanted: &[String]) -> String {
format!(
"-Zsanitizer={} -C debuginfo=2 -C force-frame-pointers=yes -C overflow-checks=on",
wanted.join(",")
)
}
pub fn verification_gap(wanted: &[String], detected: &[String]) -> Option<String> {
let labels: Vec<&str> = wanted.iter().filter_map(|s| label_for(s)).collect();
let missing: Vec<&str> = labels
.iter()
.copied()
.filter(|label| !detected.iter().any(|d| d == label))
.collect();
if missing.is_empty() {
return None;
}
Some(format!(
"build-produced-no-instrumentation(wanted={} detected={})",
labels.join(","),
if detected.is_empty() {
"none".to_string()
} else {
detected.join(",")
}
))
}
pub fn sole_bin_target(metadata: &str) -> Option<String> {
let parsed: serde_json::Value = serde_json::from_str(metadata).ok()?;
let mut bins: Vec<String> = Vec::new();
for package in parsed.get("packages")?.as_array()? {
for target in package.get("targets")?.as_array()? {
let is_bin = target
.get("kind")
.and_then(|k| k.as_array())
.is_some_and(|kinds| kinds.iter().any(|k| k.as_str() == Some("bin")));
if is_bin {
if let Some(name) = target.get("name").and_then(|n| n.as_str()) {
push_unique(&mut bins, name);
}
}
}
}
match bins.len() {
1 => bins.pop(),
_ => None,
}
}
pub fn parse_supported_sanitizers(target_spec_json: &str) -> Option<Vec<String>> {
let parsed: serde_json::Value = serde_json::from_str(target_spec_json).ok()?;
let listed = parsed.get("supported-sanitizers")?.as_array()?;
let sanitizers: Vec<String> = listed
.iter()
.filter_map(|s| s.as_str())
.map(|s| s.to_lowercase())
.collect();
if sanitizers.is_empty() {
None
} else {
Some(sanitizers)
}
}
fn log_tail(stdout: &[u8], stderr: &[u8]) -> String {
let combined = format!(
"{}{}",
String::from_utf8_lossy(stdout),
String::from_utf8_lossy(stderr)
);
let lines: Vec<&str> = combined.lines().collect();
let start = lines.len().saturating_sub(20);
let tail = lines[start..].join("\n");
const MAX_CHARS: usize = 4000;
let length = tail.chars().count();
if length <= MAX_CHARS {
return tail;
}
tail.chars().skip(length - MAX_CHARS).collect()
}
fn which(program: &str) -> Option<PathBuf> {
let path = std::env::var_os("PATH")?;
let program = format!("{program}{}", std::env::consts::EXE_SUFFIX);
std::env::split_paths(&path)
.map(|dir| dir.join(&program))
.find(|candidate| candidate.is_file())
}
fn nightly_is_installed() -> bool {
Command::new("rustup")
.args(["toolchain", "list"])
.output()
.ok()
.filter(|o| o.status.success())
.is_some_and(|o| {
String::from_utf8_lossy(&o.stdout)
.lines()
.any(|line| line.starts_with("nightly"))
})
}
fn rust_src_is_installed() -> bool {
Command::new("rustup")
.args(["component", "list", "--toolchain", "nightly", "--installed"])
.output()
.ok()
.filter(|o| o.status.success())
.is_some_and(|o| {
String::from_utf8_lossy(&o.stdout)
.lines()
.any(|line| line.starts_with("rust-src"))
})
}
fn host_triple() -> Option<String> {
let output = Command::new("rustup")
.args(["run", "nightly", "rustc", "-vV"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
String::from_utf8_lossy(&output.stdout)
.lines()
.find_map(|line| line.strip_prefix("host: "))
.map(|triple| triple.trim().to_string())
}
fn supported_sanitizers(triple: &str) -> Option<Vec<String>> {
let output = Command::new("rustup")
.args(["run", "nightly", "rustc", "-Zunstable-options", "--target"])
.arg(triple)
.args(["--print", "target-spec-json"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
parse_supported_sanitizers(&String::from_utf8_lossy(&output.stdout))
}
fn cargo_metadata(manifest_path: &Path) -> Option<String> {
let output = Command::new("cargo")
.args(["metadata", "--no-deps", "--format-version", "1"])
.arg("--manifest-path")
.arg(manifest_path)
.output()
.ok()?;
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).to_string())
}
#[cfg(test)]
mod tests {
use super::*;
fn names(values: &[&str]) -> Vec<String> {
values.iter().map(|v| v.to_string()).collect()
}
#[test]
fn reads_the_supported_sanitizers_rustc_reports() {
let spec = r#"{"arch":"aarch64","supported-sanitizers":["address","thread","cfi"]}"#;
assert_eq!(
parse_supported_sanitizers(spec),
Some(names(&["address", "thread", "cfi"]))
);
assert_eq!(
parse_supported_sanitizers(r#"{"arch":"wasm32"}"#),
None,
"a target that lists no sanitizers must not read as 'all of them'"
);
assert_eq!(parse_supported_sanitizers("not json"), None);
}
#[test]
fn keeps_only_sanitizers_the_target_supports() {
let plan = partition_requested(
&names(&["address", "memory"]),
&names(&["address", "thread", "cfi"]),
);
assert_eq!(plan.wanted, names(&["address"]));
assert_eq!(plan.unsupported, names(&["memory"]));
}
#[test]
fn msan_on_a_target_without_it_skips_by_name() {
let plan = partition_requested(&names(&["memory"]), &names(&["address", "thread", "cfi"]));
assert!(plan.wanted.is_empty());
assert_eq!(plan.empty_reason(), "sanitizer-unsupported-on-target");
}
#[test]
fn ubsan_on_rust_skips_with_the_reason_that_there_is_no_such_thing() {
for alias in UBSAN_ALIASES {
let plan =
partition_requested(&[alias.to_string()], &names(&["address", "thread", "cfi"]));
assert!(plan.wanted.is_empty(), "{alias} must not be buildable");
assert_eq!(plan.unsupported, names(&["undefined"]));
assert_eq!(plan.empty_reason(), "sanitizer-unsupported-on-target");
assert!(
plan.notes.iter().any(|n| n.contains("rustc-has-no-ubsan")),
"{alias} must carry the note naming the real answer: {:?}",
plan.notes
);
assert!(
plan.notes.iter().any(|n| n.contains("overflow-checks=on")),
"the note must point at the Rust equivalent"
);
}
}
#[test]
fn a_sanitizer_cxg_cannot_verify_is_declined_before_the_build() {
let plan = partition_requested(&names(&["cfi"]), &names(&["address", "cfi"]));
assert!(plan.wanted.is_empty());
assert_eq!(plan.unverifiable, names(&["cfi"]));
assert_eq!(plan.empty_reason(), "sanitizer-not-verifiable(cfi)");
}
#[test]
fn a_verifiable_sanitizer_survives_alongside_an_unverifiable_one() {
let plan = partition_requested(&names(&["address", "cfi"]), &names(&["address", "cfi"]));
assert_eq!(plan.wanted, names(&["address"]));
assert_eq!(plan.unverifiable, names(&["cfi"]));
}
#[test]
fn every_verifiable_sanitizer_maps_to_a_label_the_detector_reports() {
for (name, label) in VERIFIABLE_SANITIZERS {
assert_eq!(label_for(name), Some(*label));
}
assert_eq!(label_for("cfi"), None);
}
#[test]
fn the_build_flags_carry_debug_info_and_the_overflow_check() {
let flags = rustflags(&names(&["address"]));
assert!(flags.contains("-Zsanitizer=address"), "{flags}");
assert!(flags.contains("-C debuginfo=2"), "{flags}");
assert!(flags.contains("-C force-frame-pointers=yes"), "{flags}");
assert!(flags.contains("-C overflow-checks=on"), "{flags}");
assert_eq!(
rustflags(&names(&["address", "thread"])),
rustflags(&names(&["address", "thread"])),
"the flag string must be deterministic"
);
assert!(rustflags(&names(&["address", "thread"])).contains("-Zsanitizer=address,thread"));
}
#[test]
fn a_build_that_silently_dropped_the_flags_is_refused() {
assert_eq!(
verification_gap(&names(&["address"]), &[]),
Some("build-produced-no-instrumentation(wanted=asan detected=none)".to_string())
);
assert_eq!(
verification_gap(&names(&["address"]), &names(&["debug-info"])),
Some("build-produced-no-instrumentation(wanted=asan detected=debug-info)".to_string())
);
}
#[test]
fn a_partially_instrumented_build_is_refused_too() {
assert_eq!(
verification_gap(&names(&["address", "thread"]), &names(&["asan"])),
Some("build-produced-no-instrumentation(wanted=asan,tsan detected=asan)".to_string())
);
}
#[test]
fn a_verified_build_passes_verification() {
assert_eq!(
verification_gap(&names(&["address"]), &names(&["asan", "debug-info"])),
None
);
assert_eq!(
verification_gap(
&names(&["address", "thread"]),
&names(&["asan", "debug-info", "tsan"])
),
None
);
}
#[test]
fn infers_the_binary_target_when_a_project_has_exactly_one() {
let metadata = r#"{"packages":[{"targets":[
{"kind":["lib"],"name":"toy"},
{"kind":["bin"],"name":"toy"}
]}]}"#;
assert_eq!(sole_bin_target(metadata), Some("toy".to_string()));
}
#[test]
fn refuses_to_choose_between_several_binary_targets() {
let metadata = r#"{"packages":[
{"targets":[{"kind":["bin"],"name":"app"},{"kind":["bin"],"name":"helper"}]},
{"targets":[{"kind":["bin"],"name":"tool"}]}
]}"#;
assert_eq!(sole_bin_target(metadata), None);
}
#[test]
fn a_project_with_no_binary_target_is_also_ambiguous() {
let metadata = r#"{"packages":[{"targets":[{"kind":["lib"],"name":"toy"}]}]}"#;
assert_eq!(sole_bin_target(metadata), None);
assert_eq!(sole_bin_target("not json"), None);
}
}