use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::fs::{File, OpenOptions};
use std::io::{self, BufRead, BufReader, IsTerminal as _, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use object::{Architecture, BinaryFormat, Object as _};
use rsjs_ir::abi::{FinalUnitId, InvocationId, LinkUnitId, PlanId, SemanticHash};
use serde_json::Value;
use sha2::{Digest, Sha256};
use thiserror::Error;
use crate::build::{
acquire_descriptor_target_lease, atomic_write_exact, final_unit_tracking_inputs,
scrub_wrapper_control_environment, wrapper_control_environment,
write_content_addressed_final_unit_map, CargoArtifactCollector, CargoArtifactTarget,
CargoCompilerArtifact, CargoMessageError, DescriptorCargoRunError, DescriptorTargetLease,
FinalUnitMapError, ValidatedDescriptorUnit, COMPONENT_DESCRIPTOR_DIRS_ENV,
FINAL_CARGO_UNIT_STATUS_ENV, FINAL_CARGO_UNIT_STATUS_UNMAPPED, FINAL_TARGET_DIR_ENV,
FINAL_UNIT_MAP_HASH_ENV, FINAL_UNIT_MAP_PATH_ENV, FINAL_UNIT_PLAN_DIRECTORY_ENV,
FINAL_UNIT_PLAN_ENV_PREFIX, FINAL_WRAPPER_POLICY_VERSION, SOURCE_EXCLUSION_DIR_ENV,
WRAPPER_MODE_ENV, WRAPPER_MODE_FINAL_V2,
};
pub use crate::build::{
FINAL_PHASE_ENV, FINAL_PLAN_FILE_HASH_ENV, FINAL_PLAN_ID_ENV, FINAL_PLAN_PATH_ENV,
};
use crate::link::{transport_plan_json, TransportLinkPlan};
use crate::semantic_link::{
semantic_plan_json, SemanticLinkPlan, SEMANTIC_PLAN_FILE_SUFFIX, SEMANTIC_PLAN_FORMAT,
};
const PLAN_FILE_FORMAT: &str = "proa.rsjs.resolved_transport_plan";
const TRANSPORT_PLAN_FILE_SUFFIX: &str = "transport-plan.json";
static FINAL_SNAPSHOT_NONCE: AtomicU64 = AtomicU64::new(0);
pub trait FinalCargoPlan {
fn final_plan_id(&self) -> PlanId;
fn final_link_unit(&self) -> LinkUnitId;
fn final_plan_format(&self) -> &'static str;
fn final_plan_file_suffix(&self) -> &'static str;
fn final_plan_json(&self) -> Result<Value, String>;
}
impl FinalCargoPlan for TransportLinkPlan {
fn final_plan_id(&self) -> PlanId {
self.plan_id
}
fn final_link_unit(&self) -> LinkUnitId {
self.link_unit
}
fn final_plan_format(&self) -> &'static str {
PLAN_FILE_FORMAT
}
fn final_plan_file_suffix(&self) -> &'static str {
TRANSPORT_PLAN_FILE_SUFFIX
}
fn final_plan_json(&self) -> Result<Value, String> {
Ok(transport_plan_json(self))
}
}
impl FinalCargoPlan for SemanticLinkPlan {
fn final_plan_id(&self) -> PlanId {
self.plan_id
}
fn final_link_unit(&self) -> LinkUnitId {
self.link_unit
}
fn final_plan_format(&self) -> &'static str {
SEMANTIC_PLAN_FORMAT
}
fn final_plan_file_suffix(&self) -> &'static str {
SEMANTIC_PLAN_FILE_SUFFIX
}
fn final_plan_json(&self) -> Result<Value, String> {
semantic_plan_json(self).map_err(|error| error.to_string())
}
}
#[derive(Clone, Debug)]
pub struct FinalCargoRequest {
pub invocation: InvocationId,
pub cargo: OsString,
pub wrapper: PathBuf,
pub working_directory: PathBuf,
pub source_exclusion_directory: PathBuf,
pub target_root: PathBuf,
pub plan_path: PathBuf,
pub cargo_args: Vec<OsString>,
pub environment: BTreeMap<OsString, OsString>,
pub root_unit_fingerprint: SemanticHash,
pub descriptor_units: Vec<ValidatedDescriptorUnit>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FinalCargoArtifactReport {
pub package_id: String,
pub target: CargoArtifactTarget,
pub profile: Value,
pub features: BTreeSet<String>,
pub fresh: bool,
}
impl From<&CargoCompilerArtifact> for FinalCargoArtifactReport {
fn from(artifact: &CargoCompilerArtifact) -> Self {
Self {
package_id: artifact.package_id.clone(),
target: artifact.target.clone(),
profile: artifact.profile.clone(),
features: artifact.features.clone(),
fresh: artifact.fresh,
}
}
}
#[derive(Clone, Debug)]
pub struct FinalCargoOutput {
pub plan_id: PlanId,
pub final_unit_id: FinalUnitId,
pub target_directory: PathBuf,
pub unit_map_path: PathBuf,
pub unit_map_hash: String,
pub plan_file_hash: String,
pub cargo_artifacts: Vec<FinalCargoArtifactReport>,
}
#[must_use = "final Cargo runs retain their target lease until consumed"]
pub(crate) struct FinalCargoRun {
pub(crate) local_scripts_declare_inputs: bool,
output: FinalCargoOutput,
cargo_artifacts: Vec<CargoCompilerArtifact>,
_target_lease: DescriptorTargetLease,
}
impl FinalCargoRun {
pub(crate) fn output(&self) -> &FinalCargoOutput {
&self.output
}
pub(crate) fn cargo_artifacts(&self) -> &[CargoCompilerArtifact] {
&self.cargo_artifacts
}
pub(crate) fn into_output(self) -> FinalCargoOutput {
self.output
}
}
pub fn run_final_cargo<P: FinalCargoPlan>(
request: &FinalCargoRequest,
plan: &P,
) -> Result<FinalCargoOutput, FinalCargoError> {
run_final_cargo_leased(request, plan).map(FinalCargoRun::into_output)
}
pub(crate) enum OptimisticFinalCargoOutcome {
Succeeded(FinalCargoRun),
NeedsFullLinkedPass,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum FinalDiagnosticMode {
Forward,
BufferAndDetectUnmapped,
}
pub(crate) fn run_final_cargo_leased<P: FinalCargoPlan>(
request: &FinalCargoRequest,
plan: &P,
) -> Result<FinalCargoRun, FinalCargoError> {
match run_final_cargo_leased_with_mode(request, plan, FinalDiagnosticMode::Forward)? {
OptimisticFinalCargoOutcome::Succeeded(run) => Ok(run),
OptimisticFinalCargoOutcome::NeedsFullLinkedPass => {
unreachable!("forward diagnostic mode never returns NeedsFullLinkedPass")
}
}
}
pub(crate) fn run_final_cargo_leased_optimistic<P: FinalCargoPlan>(
request: &FinalCargoRequest,
plan: &P,
) -> Result<OptimisticFinalCargoOutcome, FinalCargoError> {
run_final_cargo_leased_with_mode(request, plan, FinalDiagnosticMode::BufferAndDetectUnmapped)
}
fn run_final_cargo_leased_with_mode<P: FinalCargoPlan>(
request: &FinalCargoRequest,
plan: &P,
diagnostic_mode: FinalDiagnosticMode,
) -> Result<OptimisticFinalCargoOutcome, FinalCargoError> {
let mut timing = super::timing::StageTimer::new();
validate_request(request)?;
let (plan_file_hash, plan_bytes) = validate_plan_file(&request.plan_path, plan)?;
let plan_id = plan.final_plan_id();
let unit_map_directory = request.target_root.join("rsjs-final-unit-maps");
ensure_plain_managed_directory(&request.source_exclusion_directory, &unit_map_directory)?;
let unit_map = write_content_addressed_final_unit_map(
&unit_map_directory,
plan_id,
&plan_file_hash,
&request.descriptor_units,
)
.map_err(FinalCargoError::UnitMap)?;
ensure_plain_managed_file(&request.source_exclusion_directory, &unit_map.path)?;
timing.mark("final request validation and unit map");
let target_directory = final_target_directory(&request.target_root, request.invocation);
ensure_plain_managed_directory(&request.source_exclusion_directory, &target_directory)?;
let target_lease =
acquire_descriptor_target_lease(&target_directory).map_err(FinalCargoError::TargetLease)?;
let mut environment = request.environment.clone();
environment.extend(
final_unit_tracking_inputs(&request.descriptor_units).map_err(FinalCargoError::UnitMap)?,
);
if plan.final_plan_format() == SEMANTIC_PLAN_FORMAT {
environment.extend(publish_unit_projections(request, plan_id, &plan_bytes)?);
}
inject_plan_environment(
&mut environment,
request,
plan,
&plan_file_hash,
&unit_map.path,
&unit_map.content_hash,
)?;
let cargo_args = compile_only_cargo_arguments(&request.cargo_args)?;
let buffered_color = diagnostic_mode == FinalDiagnosticMode::BufferAndDetectUnmapped
&& match env::var("CARGO_TERM_COLOR").as_deref() {
Ok("always") => true,
Ok("never") => false,
_ => io::stderr().is_terminal(),
};
let message_format = match diagnostic_mode {
FinalDiagnosticMode::Forward => "--message-format=json-render-diagnostics",
FinalDiagnosticMode::BufferAndDetectUnmapped if buffered_color => {
"--message-format=json-diagnostic-rendered-ansi"
}
FinalDiagnosticMode::BufferAndDetectUnmapped => "--message-format=json",
};
let mut command = Command::new(&request.cargo);
command
.current_dir(&request.working_directory)
.args(&cargo_args)
.arg("--target-dir")
.arg(&target_directory)
.arg(message_format);
scrub_wrapper_control_environment(&mut command);
command
.envs(&environment)
.env_remove("OUT_DIR")
.env("RUSTC_WRAPPER", &request.wrapper)
.env_remove("RUSTC_WORKSPACE_WRAPPER")
.env(WRAPPER_MODE_ENV, WRAPPER_MODE_FINAL_V2)
.env(
SOURCE_EXCLUSION_DIR_ENV,
&request.source_exclusion_directory,
)
.env(FINAL_TARGET_DIR_ENV, &target_directory)
.env_remove("RSJS_DESCRIPTOR_PHASE")
.env_remove("RSJS_CARGO_UNIT_ID")
.stdout(Stdio::piped());
match diagnostic_mode {
FinalDiagnosticMode::Forward => {
command.stderr(Stdio::inherit());
}
FinalDiagnosticMode::BufferAndDetectUnmapped => {
if buffered_color {
command.env("CARGO_TERM_COLOR", "always");
}
command.stderr(Stdio::piped());
}
}
timing.mark("final Cargo setup");
let mut child = command
.spawn()
.map_err(|source| FinalCargoError::SpawnCargo {
program: PathBuf::from(&request.cargo),
source,
})?;
let stdout = child
.stdout
.take()
.ok_or(FinalCargoError::MissingCargoStdout)?;
let stderr_relay = child
.stderr
.take()
.map(|stderr| std::thread::spawn(move || relay_cargo_status_lines(stderr)));
let mut collector = CargoArtifactCollector::default();
let mut stdout = BufReader::new(stdout);
let mut cargo_line_number = 0_usize;
let mut buffered_diagnostics = Vec::new();
let mut saw_unmapped_rsjs_unit = false;
loop {
let mut bytes = Vec::new();
let read = match stdout.read_until(b'\n', &mut bytes) {
Ok(read) => read,
Err(source) => {
let _ = child.kill();
let _ = child.wait();
return Err(FinalCargoError::ReadCargoStdout(source));
}
};
if read == 0 {
break;
}
cargo_line_number += 1;
if bytes.last() == Some(&b'\n') {
bytes.pop();
if bytes.last() == Some(&b'\r') {
bytes.pop();
}
}
let line = match std::str::from_utf8(&bytes) {
Ok(line) => line,
Err(source) => {
let _ = child.kill();
let _ = child.wait();
return Err(FinalCargoError::NonUtf8CargoProtocol {
line: cargo_line_number,
source,
});
}
};
if let Some(diagnostic) = extract_cargo_compiler_diagnostic(line) {
match diagnostic_mode {
FinalDiagnosticMode::Forward => eprint!("{}", diagnostic.rendered),
FinalDiagnosticMode::BufferAndDetectUnmapped => {
saw_unmapped_rsjs_unit |= diagnostic.unmapped_rsjs_unit;
buffered_diagnostics.push(diagnostic.rendered);
}
}
}
if let Err(source) = collector.ingest_line(line) {
let _ = child.kill();
let _ = child.wait();
return Err(FinalCargoError::CargoProtocol(source));
}
}
let status = child.wait().map_err(FinalCargoError::WaitForCargo)?;
let held_cargo_lines = stderr_relay
.map(|relay| relay.join().unwrap_or_default())
.unwrap_or_default();
if !status.success() && saw_unmapped_rsjs_unit {
return Ok(OptimisticFinalCargoOutcome::NeedsFullLinkedPass);
}
for rendered in buffered_diagnostics {
eprint!("{rendered}");
}
if !held_cargo_lines.is_empty() {
let _ = io::stderr().write_all(&held_cargo_lines);
}
if !status.success() {
return Err(FinalCargoError::CargoFailed(status));
}
let local_scripts_declare_inputs = collector.local_build_scripts_declare_inputs();
let cargo_artifacts = collector.finish().map_err(FinalCargoError::CargoProtocol)?;
if cargo_artifacts.is_empty() {
return Err(FinalCargoError::NoCargoArtifacts);
}
let cargo_artifact_reports = cargo_artifacts
.iter()
.map(FinalCargoArtifactReport::from)
.collect();
timing.mark("final Cargo execution");
Ok(OptimisticFinalCargoOutcome::Succeeded(FinalCargoRun {
local_scripts_declare_inputs,
output: FinalCargoOutput {
plan_id,
final_unit_id: FinalUnitId::for_plan(request.root_unit_fingerprint, plan_id),
target_directory,
unit_map_path: unit_map.path,
unit_map_hash: unit_map.content_hash,
plan_file_hash,
cargo_artifacts: cargo_artifact_reports,
},
cargo_artifacts,
_target_lease: target_lease,
}))
}
fn validate_request(request: &FinalCargoRequest) -> Result<(), FinalCargoError> {
if !request.wrapper.is_absolute() {
return Err(FinalCargoError::RelativeWrapper(request.wrapper.clone()));
}
if !request.working_directory.is_absolute() {
return Err(FinalCargoError::RelativeWorkingDirectory(
request.working_directory.clone(),
));
}
if !request.target_root.is_absolute() {
return Err(FinalCargoError::RelativeTargetRoot(
request.target_root.clone(),
));
}
if !request.source_exclusion_directory.is_absolute() {
return Err(FinalCargoError::RelativeSourceExclusionDirectory(
request.source_exclusion_directory.clone(),
));
}
if !request.plan_path.is_absolute() {
return Err(FinalCargoError::RelativePlanPath(request.plan_path.clone()));
}
if request.cargo_args.is_empty() {
return Err(FinalCargoError::MissingCargoArguments);
}
if env::var_os(COMPONENT_DESCRIPTOR_DIRS_ENV).is_some()
|| request
.environment
.contains_key(OsStr::new(COMPONENT_DESCRIPTOR_DIRS_ENV))
{
return Err(FinalCargoError::UnsupportedEnvironment(
COMPONENT_DESCRIPTOR_DIRS_ENV,
));
}
let cargo_args = compile_only_cargo_arguments(&request.cargo_args)?;
for argument in cargo_args {
let argument = argument.to_string_lossy();
if argument == "--target-dir"
|| argument.starts_with("--target-dir=")
|| argument == "--message-format"
|| argument.starts_with("--message-format=")
{
return Err(FinalCargoError::ReservedCargoArgument(
argument.into_owned(),
));
}
}
if let Some(name) = request
.environment
.keys()
.find(|name| name.to_str().is_some_and(wrapper_control_environment))
{
return Err(FinalCargoError::ReservedEnvironment(
name.to_string_lossy().into_owned(),
));
}
for reserved in [
OsStr::new(FINAL_PHASE_ENV),
OsStr::new(FINAL_PLAN_PATH_ENV),
OsStr::new(FINAL_PLAN_ID_ENV),
OsStr::new(FINAL_PLAN_FILE_HASH_ENV),
OsStr::new(FINAL_UNIT_MAP_PATH_ENV),
OsStr::new(FINAL_UNIT_MAP_HASH_ENV),
OsStr::new(FINAL_CARGO_UNIT_STATUS_ENV),
OsStr::new(WRAPPER_MODE_ENV),
OsStr::new(SOURCE_EXCLUSION_DIR_ENV),
OsStr::new(FINAL_TARGET_DIR_ENV),
OsStr::new("RSJS_DESCRIPTOR_PHASE"),
OsStr::new("RSJS_CARGO_UNIT_ID"),
] {
if request.environment.contains_key(reserved) {
return Err(FinalCargoError::ReservedEnvironment(
reserved.to_string_lossy().into_owned(),
));
}
}
if env::var_os("RUSTC_WORKSPACE_WRAPPER").is_some()
|| request
.environment
.contains_key(OsStr::new("RUSTC_WORKSPACE_WRAPPER"))
{
return Err(FinalCargoError::ExistingWorkspaceWrapper);
}
if env::var_os("RUSTC_WRAPPER").is_some()
|| request
.environment
.contains_key(OsStr::new("RUSTC_WRAPPER"))
{
return Err(FinalCargoError::ExistingRustcWrapper);
}
ensure_plain_managed_directory(&request.source_exclusion_directory, &request.target_root)?;
ensure_plain_managed_file(&request.source_exclusion_directory, &request.plan_path)?;
Ok(())
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ManagedPathError {
#[error("managed path contains `.` or `..`: `{0}`")]
NonNormalPath(PathBuf),
#[error("managed path `{path}` is outside source exclusion `{exclusion}`")]
OutsideSourceExclusion {
path: PathBuf,
exclusion: PathBuf,
},
#[error("failed to create managed directory `{path}`: {source}")]
CreateDirectory {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("failed to inspect managed path `{path}`: {source}")]
Inspect {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("managed directory component is not a plain directory: `{0}`")]
NonDirectory(PathBuf),
#[error("managed file is not a plain regular file: `{0}`")]
NonFile(PathBuf),
#[error("managed file has no parent directory: `{0}`")]
FileWithoutParent(PathBuf),
}
pub(crate) fn ensure_plain_managed_directory(
source_exclusion: &Path,
directory: &Path,
) -> Result<(), ManagedPathError> {
validate_normal_absolute_path(source_exclusion)?;
validate_normal_absolute_path(directory)?;
let relative = directory.strip_prefix(source_exclusion).map_err(|_| {
ManagedPathError::OutsideSourceExclusion {
path: directory.to_path_buf(),
exclusion: source_exclusion.to_path_buf(),
}
})?;
fs::create_dir_all(source_exclusion).map_err(|source| ManagedPathError::CreateDirectory {
path: source_exclusion.to_path_buf(),
source,
})?;
require_plain_directory(source_exclusion)?;
let mut current = source_exclusion.to_path_buf();
for component in relative.components() {
let std::path::Component::Normal(name) = component else {
return Err(ManagedPathError::NonNormalPath(directory.to_path_buf()));
};
current.push(name);
match fs::create_dir(¤t) {
Ok(()) => {}
Err(source) if source.kind() == io::ErrorKind::AlreadyExists => {}
Err(source) => {
return Err(ManagedPathError::CreateDirectory {
path: current,
source,
});
}
}
require_plain_directory(¤t)?;
}
Ok(())
}
pub(crate) fn ensure_plain_managed_file(
source_exclusion: &Path,
file: &Path,
) -> Result<(), ManagedPathError> {
validate_normal_absolute_path(file)?;
let parent = file
.parent()
.ok_or_else(|| ManagedPathError::FileWithoutParent(file.to_path_buf()))?;
ensure_plain_managed_directory(source_exclusion, parent)?;
let metadata = fs::symlink_metadata(file).map_err(|source| ManagedPathError::Inspect {
path: file.to_path_buf(),
source,
})?;
if !metadata.file_type().is_file() {
return Err(ManagedPathError::NonFile(file.to_path_buf()));
}
Ok(())
}
fn validate_normal_absolute_path(path: &Path) -> Result<(), ManagedPathError> {
if !path.is_absolute()
|| path.components().any(|component| {
matches!(
component,
std::path::Component::CurDir | std::path::Component::ParentDir
)
})
{
return Err(ManagedPathError::NonNormalPath(path.to_path_buf()));
}
Ok(())
}
fn require_plain_directory(path: &Path) -> Result<(), ManagedPathError> {
let metadata = fs::symlink_metadata(path).map_err(|source| ManagedPathError::Inspect {
path: path.to_path_buf(),
source,
})?;
if !metadata.file_type().is_dir() {
return Err(ManagedPathError::NonDirectory(path.to_path_buf()));
}
Ok(())
}
fn split_runtime_arguments(arguments: &[OsString]) -> (&[OsString], Option<&[OsString]>) {
match arguments
.iter()
.position(|argument| argument == OsStr::new("--"))
{
Some(index) => (&arguments[..index], Some(&arguments[index + 1..])),
None => (arguments, None),
}
}
fn compile_only_cargo_arguments(arguments: &[OsString]) -> Result<Vec<OsString>, FinalCargoError> {
let (compile_arguments, runtime_arguments) = split_runtime_arguments(arguments);
let mut compile_arguments = compile_arguments.to_vec();
let subcommand = compile_arguments
.first()
.cloned()
.ok_or(FinalCargoError::MissingCargoArguments)?;
match subcommand.as_os_str() {
value if value == OsStr::new("run") => {
compile_arguments[0] = OsString::from("build");
}
value if value == OsStr::new("test") => {
if !compile_arguments
.iter()
.any(|argument| argument == OsStr::new("--no-run"))
{
compile_arguments.push(OsString::from("--no-run"));
}
}
value if value == OsStr::new("build") || value == OsStr::new("check") => {
if runtime_arguments.is_some() {
return Err(FinalCargoError::RuntimeArgumentsForCompileOnlySubcommand(
subcommand.clone(),
));
}
}
_ => {
return Err(FinalCargoError::UnsupportedCargoSubcommand(
subcommand.clone(),
));
}
}
Ok(compile_arguments)
}
fn validate_plan_file<P: FinalCargoPlan>(
path: &Path,
plan: &P,
) -> Result<(String, Vec<u8>), FinalCargoError> {
let bytes = fs::read(path).map_err(|source| FinalCargoError::ReadPlan {
path: path.to_path_buf(),
source,
})?;
let value: Value =
serde_json::from_slice(&bytes).map_err(|source| FinalCargoError::ParsePlan {
path: path.to_path_buf(),
source,
})?;
let format = value.get("format").and_then(Value::as_str);
if format != Some(plan.final_plan_format()) {
return Err(FinalCargoError::PlanFormat {
actual: format.unwrap_or_default().to_string(),
});
}
let file_plan_id = value.get("plan_id").and_then(Value::as_str);
let expected_plan_id = plan.final_plan_id().to_string();
if file_plan_id != Some(expected_plan_id.as_str()) {
return Err(FinalCargoError::PlanIdMismatch {
expected: plan.final_plan_id(),
actual: file_plan_id.unwrap_or_default().to_string(),
});
}
let file_link_unit = value.get("link_unit_id").and_then(Value::as_str);
let expected_link_unit = plan.final_link_unit().to_string();
if file_link_unit != Some(expected_link_unit.as_str()) {
return Err(FinalCargoError::LinkUnitMismatch {
expected: expected_link_unit,
actual: file_link_unit.unwrap_or_default().to_string(),
});
}
let expected_name = format!("{}.{}", plan.final_plan_id(), plan.final_plan_file_suffix());
if path.file_name() != Some(OsStr::new(&expected_name)) {
return Err(FinalCargoError::PlanFilename {
path: path.to_path_buf(),
expected: expected_name,
});
}
let expected_value = plan
.final_plan_json()
.map_err(FinalCargoError::SerializeExpectedPlan)?;
if value != expected_value {
return Err(FinalCargoError::PlanContentMismatch);
}
Ok((exact_sha256_hex(&bytes), bytes))
}
fn publish_unit_projections(
request: &FinalCargoRequest,
plan_id: PlanId,
plan_bytes: &[u8],
) -> Result<BTreeMap<OsString, OsString>, FinalCargoError> {
let directory = request.target_root.join("rsjs-final-unit-plans");
ensure_plain_managed_directory(&request.source_exclusion_directory, &directory)?;
let mut environment = BTreeMap::new();
environment.insert(
OsString::from(FINAL_UNIT_PLAN_DIRECTORY_ENV),
directory.as_os_str().to_owned(),
);
let projections = rsjs_ir::final_plan::unit_projection_bytes_for_units(
request
.descriptor_units
.iter()
.map(|unit| *unit.unit.as_bytes()),
*plan_id.as_bytes(),
plan_bytes,
)
.map_err(FinalCargoError::UnitProjection)?;
for unit in &request.descriptor_units {
let unit_id = unit.unit;
let bytes = &projections[unit_id.as_bytes()];
let hash = hex_sha256(&Sha256::digest(bytes).into());
let path = directory.join(format!(
"{hash}.{}",
rsjs_ir::final_plan::UNIT_PROJECTION_FILE_SUFFIX
));
match fs::symlink_metadata(&path) {
Ok(_) => {
ensure_plain_managed_file(&request.source_exclusion_directory, &path)?;
let existing = fs::read(&path).map_err(|source| FinalCargoError::ReadPlan {
path: path.clone(),
source,
})?;
if &existing != bytes {
return Err(FinalCargoError::UnitProjection(format!(
"conflicting content-addressed unit projection `{}`",
path.display()
)));
}
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {
atomic_write_exact(&path, bytes).map_err(FinalCargoError::UnitMap)?;
ensure_plain_managed_file(&request.source_exclusion_directory, &path)?;
}
Err(source) => return Err(FinalCargoError::ReadPlan { path, source }),
}
environment.insert(
OsString::from(format!("{FINAL_UNIT_PLAN_ENV_PREFIX}{unit_id}")),
OsString::from(hash),
);
}
Ok(environment)
}
fn inject_plan_environment<P: FinalCargoPlan>(
environment: &mut BTreeMap<OsString, OsString>,
request: &FinalCargoRequest,
plan: &P,
plan_file_hash: &str,
unit_map_path: &Path,
unit_map_hash: &str,
) -> Result<(), FinalCargoError> {
let plan_path = request
.plan_path
.to_str()
.ok_or_else(|| FinalCargoError::NonUtf8PlanPath(request.plan_path.clone()))?;
environment.insert(OsString::from(FINAL_PHASE_ENV), OsString::from("1"));
environment.insert(
OsString::from(FINAL_PLAN_PATH_ENV),
OsString::from(plan_path),
);
environment.insert(
OsString::from(FINAL_PLAN_ID_ENV),
OsString::from(plan.final_plan_id().to_string()),
);
environment.insert(
OsString::from(FINAL_PLAN_FILE_HASH_ENV),
OsString::from(plan_file_hash),
);
environment.insert(
OsString::from(FINAL_UNIT_MAP_PATH_ENV),
unit_map_path.as_os_str().to_owned(),
);
environment.insert(
OsString::from(FINAL_UNIT_MAP_HASH_ENV),
OsString::from(unit_map_hash),
);
Ok(())
}
fn exact_sha256_hex(bytes: &[u8]) -> String {
use std::fmt::Write as _;
let digest: [u8; 32] = Sha256::digest(bytes).into();
let mut output = String::with_capacity(64);
for byte in digest {
let _ = write!(output, "{byte:02x}");
}
format!("sha256:{output}")
}
fn final_target_directory(target_root: &Path, _invocation: InvocationId) -> PathBuf {
if cfg!(windows) {
return target_root.join(format!("w{FINAL_WRAPPER_POLICY_VERSION}"));
}
target_root
.join("rsjs-final")
.join(format!("wrapper-v{FINAL_WRAPPER_POLICY_VERSION}"))
}
#[derive(Debug)]
pub struct FinalSnapshotLease {
_handle: same_file::Handle,
}
const FINAL_SNAPSHOT_LEASE_ROOT_NAME: &str = "rsjs-final-artifact-snapshot-leases";
fn final_snapshot_lease_path(snapshot_root: &Path, identity: &OsStr) -> Option<PathBuf> {
let mut file_name = identity.to_os_string();
file_name.push(".lease");
Some(
snapshot_root
.parent()?
.join(FINAL_SNAPSHOT_LEASE_ROOT_NAME)
.join(file_name),
)
}
fn acquire_final_snapshot_lease(
source_exclusion: &Path,
directory: &Path,
) -> Result<FinalSnapshotLease, FinalCargoError> {
let Some(snapshot_root) = directory.parent() else {
return Err(FinalCargoError::SnapshotArtifactIo {
path: directory.to_path_buf(),
source: io::Error::other("snapshot directory has no parent"),
});
};
let Some(identity) = directory.file_name() else {
return Err(FinalCargoError::SnapshotArtifactIo {
path: directory.to_path_buf(),
source: io::Error::other("snapshot directory has no identity name"),
});
};
let Some(path) = final_snapshot_lease_path(snapshot_root, identity) else {
return Err(FinalCargoError::SnapshotArtifactIo {
path: directory.to_path_buf(),
source: io::Error::other("snapshot root has no parent for lease storage"),
});
};
let lease_root = path
.parent()
.expect("final snapshot lease path always has a parent directory");
let lease_error = |source| FinalCargoError::SnapshotArtifactIo {
path: path.clone(),
source,
};
for _ in 0..8 {
ensure_plain_managed_directory(source_exclusion, lease_root)?;
match fs::symlink_metadata(&path) {
Ok(_) => ensure_plain_managed_file(source_exclusion, &path)?,
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => return Err(lease_error(error)),
}
let file = match OpenOptions::new()
.create(true)
.read(true)
.write(true)
.truncate(false)
.open(&path)
{
Ok(file) => file,
Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
Err(error) => return Err(lease_error(error)),
};
ensure_plain_managed_file(source_exclusion, &path)?;
let handle = same_file::Handle::from_file(file).map_err(lease_error)?;
handle.as_file().lock_shared().map_err(lease_error)?;
match same_file::Handle::from_path(&path) {
Ok(current) if current == handle => {
ensure_plain_managed_directory(source_exclusion, directory)?;
return Ok(FinalSnapshotLease { _handle: handle });
}
Ok(_) => {}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => return Err(lease_error(error)),
}
}
Err(lease_error(io::Error::other(
"the snapshot lease was replaced repeatedly while this command tried to acquire it",
)))
}
pub fn prune_final_artifact_snapshot(executable: &Path) -> bool {
let Some(directory) = executable.parent() else {
return false;
};
let (Some(snapshot_root), Some(identity)) = (directory.parent(), directory.file_name()) else {
return false;
};
if snapshot_root.file_name() != Some(OsStr::new(FINAL_SNAPSHOT_ROOT_NAME)) {
return false;
}
if !fs::symlink_metadata(snapshot_root).is_ok_and(|meta| meta.file_type().is_dir())
|| !fs::symlink_metadata(directory).is_ok_and(|meta| meta.file_type().is_dir())
{
return false;
}
let Some(lease_path) = final_snapshot_lease_path(snapshot_root, identity) else {
return false;
};
if !fs::symlink_metadata(&lease_path).is_ok_and(|meta| meta.file_type().is_file()) {
return false;
}
let Ok(lease) = OpenOptions::new().read(true).write(true).open(&lease_path) else {
return false;
};
if lease.try_lock().is_err() {
return false;
}
if !fs::symlink_metadata(directory).is_ok_and(|meta| meta.file_type().is_dir()) {
return false;
}
let nonce = FINAL_SNAPSHOT_NONCE.fetch_add(1, Ordering::Relaxed);
let mut doomed = OsString::from(format!(".pruned.{}.{nonce}.", std::process::id()));
doomed.push(identity);
let doomed = snapshot_root.join(doomed);
if fs::rename(directory, &doomed).is_err() {
return false;
}
drop(lease);
fs::remove_dir_all(&doomed).is_ok()
}
pub(crate) const FINAL_SNAPSHOT_ROOT_NAME: &str = "rsjs-final-artifact-snapshots";
pub(crate) fn snapshot_final_artifact(
source_exclusion: &Path,
snapshot_root: &Path,
target_directory: &Path,
artifact: &CargoCompilerArtifact,
) -> Result<(CargoCompilerArtifact, Option<FinalSnapshotLease>), FinalCargoError> {
ensure_plain_managed_directory(source_exclusion, snapshot_root)?;
let mut snapshots = BTreeMap::new();
let mut lease = None;
if let Some(executable) = &artifact.executable {
ensure_plain_managed_file(target_directory, executable)?;
let (snapshot, executable_lease) = snapshot_final_file(
source_exclusion,
snapshot_root,
executable,
Some(target_directory),
true,
)?;
lease = executable_lease;
snapshots.insert(executable.clone(), snapshot);
}
for source in &artifact.filenames {
if artifact.executable.as_ref() == Some(source) {
continue;
}
ensure_plain_managed_file(target_directory, source)?;
snapshots.insert(
source.clone(),
snapshot_final_file(source_exclusion, snapshot_root, source, None, false)?.0,
);
}
let mut snapshot = artifact.clone();
snapshot.filenames = artifact
.filenames
.iter()
.map(|source| {
snapshots
.get(source)
.cloned()
.expect("every final filename was snapshotted")
})
.collect();
snapshot.executable = artifact.executable.as_ref().map(|source| {
snapshots
.get(source)
.cloned()
.expect("the final executable was snapshotted")
});
Ok((snapshot, lease))
}
fn snapshot_final_file(
source_exclusion: &Path,
snapshot_root: &Path,
source: &Path,
launch_target_directory: Option<&Path>,
leased: bool,
) -> Result<(PathBuf, Option<FinalSnapshotLease>), FinalCargoError> {
let metadata = fs::symlink_metadata(source).map_err(|source_error| {
FinalCargoError::SnapshotArtifactIo {
path: source.to_path_buf(),
source: source_error,
}
})?;
if !metadata.file_type().is_file() {
return Err(FinalCargoError::SnapshotArtifactNotFile(
source.to_path_buf(),
));
}
let file_name = source
.file_name()
.ok_or_else(|| FinalCargoError::SnapshotArtifactWithoutFilename(source.to_path_buf()))?;
let permissions = metadata.permissions();
let permission_identity = snapshot_permission_identity(&permissions);
let nonce = FINAL_SNAPSHOT_NONCE.fetch_add(1, Ordering::Relaxed);
let temporary = snapshot_root.join(format!(".snapshot.{}.{}.tmp", std::process::id(), nonce));
let mut input =
File::open(source).map_err(|source_error| FinalCargoError::SnapshotArtifactIo {
path: source.to_path_buf(),
source: source_error,
})?;
let mut output = OpenOptions::new()
.create_new(true)
.write(true)
.open(&temporary)
.map_err(|source_error| FinalCargoError::SnapshotArtifactIo {
path: temporary.clone(),
source: source_error,
})?;
let mut hash = Sha256::new();
let mut buffer = [0_u8; 64 * 1024];
let copy_result = (|| -> io::Result<()> {
loop {
let read = input.read(&mut buffer)?;
if read == 0 {
break;
}
hash.update(&buffer[..read]);
output.write_all(&buffer[..read])?;
}
output.sync_all()?;
drop(output);
fs::set_permissions(&temporary, permissions)?;
Ok(())
})();
if let Err(source_error) = copy_result {
let _ = fs::remove_file(&temporary);
return Err(FinalCargoError::SnapshotArtifactIo {
path: temporary,
source: source_error,
});
}
if let Some(target_directory) = launch_target_directory {
if let Err(error) =
validate_executable_snapshot_contract(&temporary, source, target_directory)
{
let _ = fs::remove_file(&temporary);
return Err(error);
}
}
let digest: [u8; 32] = hash.finalize().into();
let identity = format!("{}-{permission_identity}", hex_sha256(&digest));
let destination_directory = snapshot_root.join(identity);
let lease = if leased {
match acquire_final_snapshot_lease(source_exclusion, &destination_directory) {
Ok(lease) => Some(lease),
Err(error) => {
let _ = fs::remove_file(&temporary);
return Err(error);
}
}
} else {
ensure_plain_managed_directory(source_exclusion, &destination_directory)?;
None
};
let destination = destination_directory.join(file_name);
match fs::hard_link(&temporary, &destination) {
Ok(()) => {
fs::remove_file(&temporary).map_err(|source_error| {
FinalCargoError::SnapshotArtifactIo {
path: temporary,
source: source_error,
}
})?;
}
Err(source_error) if source_error.kind() == io::ErrorKind::AlreadyExists => {
let _ = fs::remove_file(&temporary);
let (existing_digest, existing_permissions) = snapshot_file_identity(&destination)?;
if existing_digest != digest || existing_permissions != permission_identity {
return Err(FinalCargoError::ConflictingSnapshotArtifact(destination));
}
}
Err(source_error) => {
let _ = fs::remove_file(&temporary);
return Err(FinalCargoError::SnapshotArtifactIo {
path: destination,
source: source_error,
});
}
}
ensure_plain_managed_file(source_exclusion, &destination)?;
Ok((destination, lease))
}
pub fn snapshot_wrapper_executable(
source_exclusion: &Path,
snapshot_root: &Path,
source: &Path,
) -> Result<PathBuf, FinalCargoError> {
ensure_plain_managed_directory(source_exclusion, snapshot_root)?;
let (digest, permissions) = snapshot_file_identity(source)?;
let file_name = source
.file_name()
.ok_or_else(|| FinalCargoError::SnapshotArtifactWithoutFilename(source.to_path_buf()))?;
let directory = snapshot_root.join(format!("{}-{permissions}", hex_sha256(&digest)));
ensure_plain_managed_directory(source_exclusion, &directory)?;
let destination = directory.join(file_name);
match fs::symlink_metadata(&destination) {
Ok(_) => {
ensure_plain_managed_file(source_exclusion, &destination)?;
let (existing_digest, existing_permissions) = snapshot_file_identity(&destination)?;
if existing_digest != digest || existing_permissions != permissions {
return Err(FinalCargoError::ConflictingSnapshotArtifact(destination));
}
validate_executable_snapshot_contract(&destination, source, source_exclusion)?;
return Ok(destination);
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(source) => {
return Err(FinalCargoError::SnapshotArtifactIo {
path: destination,
source,
});
}
}
snapshot_final_file(
source_exclusion,
snapshot_root,
source,
Some(source_exclusion),
false,
)
.map(|(destination, _)| destination)
}
#[derive(Debug, Default)]
struct ExecutableSnapshotDependencies {
libraries: BTreeSet<Vec<u8>>,
loader_paths: BTreeSet<Vec<u8>>,
interpreters: BTreeSet<Vec<u8>>,
}
fn validate_executable_snapshot_contract(
snapshot: &Path,
executable: &Path,
target_directory: &Path,
) -> Result<(), FinalCargoError> {
let bytes = fs::read(snapshot).map_err(|source| FinalCargoError::SnapshotArtifactIo {
path: snapshot.to_path_buf(),
source,
})?;
let file = object::File::parse(bytes.as_slice()).map_err(|source| {
FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: format!("the executable format could not be parsed: {source}"),
}
})?;
let format = file.format();
validate_snapshot_host_identity(executable, format, file.architecture())?;
let dependencies = executable_snapshot_dependencies(&file).map_err(|source| {
FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: format!("the runtime dependency table could not be validated: {source}"),
}
})?;
if let Some(loader_path) = dependencies.loader_paths.into_iter().next() {
let loader_path = dependency_name(&loader_path).ok_or_else(|| {
FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: "a loader search path is not valid UTF-8".to_string(),
}
})?;
if loader_path.is_empty() || loader_path_is_relative(loader_path) {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: loader_path.to_string(),
reason:
"it uses a relative loader search path whose meaning changes after relocation",
});
}
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: loader_path.to_string(),
reason: "custom loader search paths are outside the system-only snapshot contract",
});
}
for interpreter in dependencies.interpreters {
let interpreter = dependency_name(&interpreter).ok_or_else(|| {
FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: "the program interpreter or dynamic loader is not valid UTF-8".to_string(),
}
})?;
validate_snapshot_interpreter(executable, target_directory, interpreter)?;
}
for dependency in dependencies.libraries {
let dependency = dependency_name(&dependency).ok_or_else(|| {
FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: "a dynamic-library dependency name is not valid UTF-8".to_string(),
}
})?;
validate_snapshot_dependency(format, executable, target_directory, dependency)?;
}
Ok(())
}
fn validate_snapshot_host_identity(
executable: &Path,
format: BinaryFormat,
architecture: Architecture,
) -> Result<(), FinalCargoError> {
if format != host_executable_format() {
return Err(FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: format!(
"the {format:?} executable format is not the current host's supported launch format ({:?})",
host_executable_format()
),
});
}
let Some(host_architecture) = host_architecture() else {
return Err(FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: format!(
"the current host architecture `{}` has no immutable launch-snapshot contract",
std::env::consts::ARCH
),
});
};
if architecture != host_architecture {
return Err(FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: format!(
"the {architecture:?} executable architecture does not match the current host architecture {host_architecture:?}"
),
});
}
Ok(())
}
fn executable_snapshot_dependencies(
file: &object::File<'_>,
) -> Result<ExecutableSnapshotDependencies, String> {
match file {
object::File::Elf32(file) => elf_snapshot_dependencies(file),
object::File::Elf64(file) => elf_snapshot_dependencies(file),
object::File::MachO32(file) => macho_snapshot_dependencies(file),
object::File::MachO64(file) => macho_snapshot_dependencies(file),
object::File::Pe32(file) => pe_snapshot_dependencies(file),
object::File::Pe64(file) => pe_snapshot_dependencies(file),
_ => {
let mut dependencies = ExecutableSnapshotDependencies::default();
for import in file.imports().map_err(|error| error.to_string())? {
let import = import.map_err(|error| error.to_string())?;
if import.library().is_empty() {
return Err(
"an imported symbol has no authoritative library identity".to_string()
);
}
dependencies.libraries.insert(import.library().to_vec());
}
Ok(dependencies)
}
}
}
fn elf_snapshot_dependencies<'data, Elf>(
file: &object::read::elf::ElfFile<'data, Elf>,
) -> Result<ExecutableSnapshotDependencies, String>
where
Elf: object::read::elf::FileHeader,
{
use object::read::elf::{Dyn as _, ProgramHeader as _};
let mut dependencies = ExecutableSnapshotDependencies::default();
let endian = file.endian();
let data = file.data();
let mut has_dynamic_segment = false;
for segment in file.elf_program_headers() {
has_dynamic_segment |= segment.p_type(endian) == object::elf::PT_DYNAMIC;
if let Some(interpreter) = segment
.interpreter(endian, data)
.map_err(|error| error.to_string())?
{
dependencies.interpreters.insert(interpreter.to_vec());
}
}
let sections = file.elf_section_table();
let Some((dynamic, strings_index)) = sections
.dynamic(endian, data)
.map_err(|error| error.to_string())?
else {
if has_dynamic_segment {
return Err(
"ELF has a PT_DYNAMIC segment but no authoritative SHT_DYNAMIC table".to_string(),
);
}
return Ok(dependencies);
};
let strings = sections
.strings(endian, data, strings_index)
.map_err(|error| error.to_string())?;
for entry in dynamic {
match entry.tag(endian) {
object::elf::DT_NEEDED => {
dependencies.libraries.insert(
entry
.string(endian, strings)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
object::elf::DT_RPATH | object::elf::DT_RUNPATH => {
dependencies.loader_paths.insert(
entry
.string(endian, strings)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
_ => {}
}
}
Ok(dependencies)
}
fn pe_snapshot_dependencies<'data, Pe>(
file: &object::read::pe::PeFile<'data, Pe>,
) -> Result<ExecutableSnapshotDependencies, String>
where
Pe: object::read::pe::ImageNtHeaders,
{
use object::LittleEndian as LE;
let mut dependencies = ExecutableSnapshotDependencies::default();
if let Some(table) = file.import_table().map_err(|error| error.to_string())? {
let mut descriptors = table.descriptors().map_err(|error| error.to_string())?;
while let Some(descriptor) = descriptors.next().map_err(|error| error.to_string())? {
let name = table
.name(descriptor.name.get(LE))
.map_err(|error| error.to_string())?;
dependencies.libraries.insert(name.to_vec());
}
}
let sections = file.section_table();
if let Some(table) = file
.data_directories()
.delay_load_import_table(file.data(), §ions)
.map_err(|error| error.to_string())?
{
let mut descriptors = table.descriptors().map_err(|error| error.to_string())?;
while let Some(descriptor) = descriptors.next().map_err(|error| error.to_string())? {
if descriptor.attributes.get(LE) != 1 {
return Err("PE delay-load imports require RVA-based descriptors with no reserved attributes".to_string());
}
let name = table
.name(descriptor.dll_name_rva.get(LE))
.map_err(|error| error.to_string())?;
dependencies.libraries.insert(name.to_vec());
}
}
Ok(dependencies)
}
fn macho_snapshot_dependencies<'data, Mach>(
file: &object::read::macho::MachOFile<'data, Mach>,
) -> Result<ExecutableSnapshotDependencies, String>
where
Mach: object::read::macho::MachHeader,
{
let mut dependencies = ExecutableSnapshotDependencies::default();
let endian = file.endian();
let mut commands = file
.macho_load_commands()
.map_err(|error| error.to_string())?;
while let Some(command) = commands.next().map_err(|error| error.to_string())? {
match command.variant().map_err(|error| error.to_string())? {
object::read::macho::LoadCommandVariant::Dylib(dylib) => {
dependencies.libraries.insert(
command
.string(endian, dylib.dylib.name)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
object::read::macho::LoadCommandVariant::Rpath(rpath) => {
dependencies.loader_paths.insert(
command
.string(endian, rpath.path)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
object::read::macho::LoadCommandVariant::LoadDylinker(dylinker) => {
dependencies.interpreters.insert(
command
.string(endian, dylinker.name)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
object::read::macho::LoadCommandVariant::DyldEnvironment(environment) => {
dependencies.loader_paths.insert(
command
.string(endian, environment.name)
.map_err(|error| error.to_string())?
.to_vec(),
);
}
_ => {}
}
}
Ok(dependencies)
}
fn host_executable_format() -> BinaryFormat {
#[cfg(target_os = "windows")]
{
BinaryFormat::Pe
}
#[cfg(target_os = "macos")]
{
BinaryFormat::MachO
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
{
BinaryFormat::Elf
}
}
fn host_architecture() -> Option<Architecture> {
match std::env::consts::ARCH {
"aarch64" => Some(Architecture::Aarch64),
"arm" => Some(Architecture::Arm),
"loongarch64" => Some(Architecture::LoongArch64),
"mips" => Some(Architecture::Mips),
"mips64" => Some(Architecture::Mips64),
"powerpc" => Some(Architecture::PowerPc),
"powerpc64" => Some(Architecture::PowerPc64),
"riscv32" => Some(Architecture::Riscv32),
"riscv64" => Some(Architecture::Riscv64),
"s390x" => Some(Architecture::S390x),
"sparc64" => Some(Architecture::Sparc64),
"x86" => Some(Architecture::I386),
"x86_64" => Some(Architecture::X86_64),
_ => None,
}
}
fn dependency_name(bytes: &[u8]) -> Option<&str> {
std::str::from_utf8(bytes).ok()
}
fn validate_snapshot_dependency(
format: BinaryFormat,
executable: &Path,
target_directory: &Path,
dependency: &str,
) -> Result<(), FinalCargoError> {
if dependency.is_empty() {
return Err(FinalCargoError::UnsupportedSnapshotExecutable {
path: executable.to_path_buf(),
reason: "an imported dynamic library has an empty identity".to_string(),
});
}
if rust_dynamic_dependency(dependency) {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "Rust dynamic libraries are not copied into immutable launch snapshots",
});
}
if loader_path_is_relative(dependency) {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "it is relative to the original executable or loader search path",
});
}
let dependency_path = Path::new(dependency);
if dependency_path.is_absolute() {
if dependency_path.starts_with(target_directory) {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "it points into the mutable final Cargo target",
});
}
if platform_system_dependency(format, dependency) {
return Ok(());
}
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "non-system absolute dynamic libraries are outside the snapshot contract",
});
}
if platform_system_dependency(format, dependency) {
if format == BinaryFormat::Pe {
if let Some(directory) = executable.parent() {
let adjacent = directory.join(dependency);
match fs::symlink_metadata(&adjacent) {
Ok(_) => {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "an app-local DLL beside the executable is not copied into the relocated snapshot",
});
}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(source) => {
return Err(FinalCargoError::SnapshotArtifactIo {
path: adjacent,
source,
});
}
}
}
}
return Ok(());
}
Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: dependency.to_string(),
reason: "non-system dynamic libraries are not copied into immutable launch snapshots",
})
}
fn validate_snapshot_interpreter(
executable: &Path,
target_directory: &Path,
interpreter: &str,
) -> Result<(), FinalCargoError> {
let interpreter_path = Path::new(interpreter);
let reason = if !interpreter_path.is_absolute() {
Some("the program interpreter or dynamic-loader path is relative")
} else if interpreter_path.starts_with(target_directory) {
Some("the program interpreter or dynamic loader points into the mutable final Cargo target")
} else if !platform_system_interpreter(interpreter) {
Some("the program interpreter or dynamic loader is outside the host system-loader contract")
} else {
None
};
if let Some(reason) = reason {
return Err(FinalCargoError::UnsupportedSnapshotDependency {
path: executable.to_path_buf(),
dependency: interpreter.to_string(),
reason,
});
}
Ok(())
}
fn platform_system_interpreter(interpreter: &str) -> bool {
interpreter.starts_with("/lib/")
|| interpreter.starts_with("/lib64/")
|| interpreter.starts_with("/usr/lib/")
|| interpreter.starts_with("/usr/lib64/")
|| matches!(interpreter, "/system/bin/linker" | "/system/bin/linker64")
}
fn loader_path_is_relative(path: &str) -> bool {
path.starts_with('@')
|| path.contains("$ORIGIN")
|| path.contains("${ORIGIN}")
|| path.split(':').any(|entry| {
entry.is_empty()
|| entry.starts_with('@')
|| entry.contains("$ORIGIN")
|| entry.contains("${ORIGIN}")
|| (entry.contains('/') && !Path::new(entry).is_absolute())
})
}
fn rust_dynamic_dependency(dependency: &str) -> bool {
let name = dependency
.rsplit(['/', '\\'])
.next()
.unwrap_or(dependency)
.to_ascii_lowercase();
[
"libstd-",
"std-",
"libtest-",
"test-",
"libpanic_",
"panic_",
"librustc_",
"rustc_",
]
.iter()
.any(|prefix| name.starts_with(prefix))
&& [".so", ".dylib", ".dll"]
.iter()
.any(|suffix| name.contains(suffix))
}
fn platform_system_dependency(format: BinaryFormat, dependency: &str) -> bool {
match format {
BinaryFormat::MachO => {
dependency.starts_with("/usr/lib/")
|| dependency.starts_with("/System/Library/")
|| dependency.starts_with("/System/DriverKit/")
}
BinaryFormat::Elf => matches!(
dependency,
"ld-linux-x86-64.so.2"
| "libc.so"
| "libc.so.6"
| "libdl.so"
| "libdl.so.2"
| "libgcc_s.so.1"
| "libm.so"
| "libm.so.6"
| "libpthread.so"
| "libpthread.so.0"
| "libresolv.so"
| "libresolv.so.2"
| "librt.so"
| "librt.so.1"
| "libthr.so.3"
| "libutil.so"
| "libutil.so.1"
| "libutil.so.9"
),
BinaryFormat::Pe => {
let dependency = dependency.to_ascii_lowercase();
if dependency.contains(['/', '\\', ':']) || !dependency.ends_with(".dll") {
return false;
}
dependency.starts_with("api-ms-win-")
|| dependency.starts_with("ext-ms-win-")
|| matches!(
dependency.as_str(),
"advapi32.dll"
| "bcrypt.dll"
| "bcryptprimitives.dll"
| "crypt32.dll"
| "gdi32.dll"
| "iphlpapi.dll"
| "kernel32.dll"
| "msvcrt.dll"
| "ncrypt.dll"
| "ntdll.dll"
| "ole32.dll"
| "secur32.dll"
| "shell32.dll"
| "ucrtbase.dll"
| "user32.dll"
| "userenv.dll"
| "vcruntime140.dll"
| "vcruntime140_1.dll"
| "ws2_32.dll"
)
}
_ => false,
}
}
fn snapshot_file_identity(path: &Path) -> Result<([u8; 32], String), FinalCargoError> {
let metadata =
fs::symlink_metadata(path).map_err(|source| FinalCargoError::SnapshotArtifactIo {
path: path.to_path_buf(),
source,
})?;
if !metadata.file_type().is_file() {
return Err(FinalCargoError::SnapshotArtifactNotFile(path.to_path_buf()));
}
let mut file = File::open(path).map_err(|source| FinalCargoError::SnapshotArtifactIo {
path: path.to_path_buf(),
source,
})?;
let mut hash = Sha256::new();
let mut buffer = [0_u8; 64 * 1024];
loop {
let read =
file.read(&mut buffer)
.map_err(|source| FinalCargoError::SnapshotArtifactIo {
path: path.to_path_buf(),
source,
})?;
if read == 0 {
break;
}
hash.update(&buffer[..read]);
}
Ok((
hash.finalize().into(),
snapshot_permission_identity(&metadata.permissions()),
))
}
fn hex_sha256(digest: &[u8; 32]) -> String {
use std::fmt::Write as _;
let mut output = String::with_capacity(64);
for byte in digest {
let _ = write!(output, "{byte:02x}");
}
output
}
#[cfg(unix)]
fn snapshot_permission_identity(permissions: &fs::Permissions) -> String {
use std::os::unix::fs::PermissionsExt as _;
format!("mode-{:o}", permissions.mode())
}
#[cfg(not(unix))]
fn snapshot_permission_identity(permissions: &fs::Permissions) -> String {
format!("readonly-{}", u8::from(permissions.readonly()))
}
struct CargoCompilerDiagnostic {
rendered: String,
unmapped_rsjs_unit: bool,
}
fn extract_cargo_compiler_diagnostic(line: &str) -> Option<CargoCompilerDiagnostic> {
let value = serde_json::from_str::<Value>(line).ok()?;
if value.get("reason").and_then(Value::as_str) != Some("compiler-message") {
return None;
}
let message = value.get("message")?;
let rendered = message.get("rendered").and_then(Value::as_str)?.to_owned();
let unmapped_rsjs_unit = message
.get("message")
.and_then(Value::as_str)
.is_some_and(is_unmapped_rsjs_unit_diagnostic);
Some(CargoCompilerDiagnostic {
rendered,
unmapped_rsjs_unit,
})
}
fn relay_cargo_status_lines(stderr: impl Read) -> Vec<u8> {
let mut reader = BufReader::new(stderr);
let mut held = Vec::new();
let mut line = Vec::new();
loop {
line.clear();
match reader.read_until(b'\n', &mut line) {
Ok(0) | Err(_) => break,
Ok(_) => {}
}
if is_cargo_status_line(&line) {
let mut stderr = io::stderr().lock();
let _ = stderr.write_all(&line);
let _ = stderr.flush();
} else {
held.extend_from_slice(&line);
}
}
held
}
fn is_cargo_status_line(line: &[u8]) -> bool {
let mut plain = Vec::with_capacity(line.len());
let mut bytes = line.iter().copied();
while let Some(byte) = bytes.next() {
if byte == 0x1b {
for byte in bytes.by_ref() {
if byte.is_ascii_alphabetic() {
break;
}
}
} else {
plain.push(byte);
}
}
let Some((head, rest)) = plain.split_at_checked(12) else {
return false;
};
let verb = head.trim_ascii_start();
rest.first() == Some(&b' ')
&& verb.first().is_some_and(u8::is_ascii_uppercase)
&& verb[1..].iter().all(u8::is_ascii_lowercase)
}
fn is_unmapped_rsjs_unit_diagnostic(message: &str) -> bool {
message.contains(&format!(
"{FINAL_CARGO_UNIT_STATUS_ENV}={FINAL_CARGO_UNIT_STATUS_UNMAPPED}"
)) || message.contains("could not map this Cargo unit to its descriptor pass")
}
#[derive(Debug, Error)]
pub enum FinalCargoError {
#[error(transparent)]
ManagedPath(#[from] ManagedPathError),
#[error("failed to acquire the final Cargo target lease: {0}")]
TargetLease(DescriptorCargoRunError),
#[error("selected final Cargo artifact has no filename: `{0}`")]
SnapshotArtifactWithoutFilename(PathBuf),
#[error("selected final Cargo artifact is not a plain regular file: `{0}`")]
SnapshotArtifactNotFile(PathBuf),
#[error("failed to publish final Cargo artifact snapshot `{path}`: {source}")]
SnapshotArtifactIo {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("content-addressed final Cargo artifact conflicts with `{0}`")]
ConflictingSnapshotArtifact(PathBuf),
#[error("final executable `{path}` cannot become an immutable launch snapshot: {reason}")]
UnsupportedSnapshotExecutable {
path: PathBuf,
reason: String,
},
#[error(
"final executable `{path}` cannot become an immutable launch snapshot because dependency `{dependency}` is unsupported: {reason}"
)]
UnsupportedSnapshotDependency {
path: PathBuf,
dependency: String,
reason: &'static str,
},
#[error("final Cargo wrapper path must be absolute: `{0}`")]
RelativeWrapper(PathBuf),
#[error("final Cargo working directory must be absolute: `{0}`")]
RelativeWorkingDirectory(PathBuf),
#[error("final Cargo source exclusion directory must be absolute: `{0}`")]
RelativeSourceExclusionDirectory(PathBuf),
#[error("final Cargo target root must be absolute: `{0}`")]
RelativeTargetRoot(PathBuf),
#[error("final RSJS plan path must be absolute: `{0}`")]
RelativePlanPath(PathBuf),
#[error("final Cargo invocation has no arguments")]
MissingCargoArguments,
#[error("final Cargo subcommand {0:?} is unsupported; expected build, check, run, or test")]
UnsupportedCargoSubcommand(OsString),
#[error("final Cargo subcommand {0:?} may not contain runtime arguments after `--`")]
RuntimeArgumentsForCompileOnlySubcommand(OsString),
#[error("final Cargo argument `{0}` is reserved")]
ReservedCargoArgument(String),
#[error("final Cargo environment `{0}` is reserved")]
ReservedEnvironment(String),
#[error("final Cargo environment `{0}` is unsupported by the exact linked pipeline")]
UnsupportedEnvironment(&'static str),
#[error("RUSTC_WORKSPACE_WRAPPER is set; unset both Cargo rustc wrapper variables before invoking cargo-proa")]
ExistingWorkspaceWrapper,
#[error(
"RUSTC_WRAPPER is set; unset it before invoking cargo-proa so final compilation cannot diverge from descriptor cache identity"
)]
ExistingRustcWrapper,
#[error("failed to read final RSJS plan `{path}`: {source}")]
ReadPlan {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("failed to parse final RSJS plan `{path}`: {source}")]
ParsePlan {
path: PathBuf,
#[source]
source: serde_json::Error,
},
#[error("unsupported final RSJS plan format `{actual}`")]
PlanFormat {
actual: String,
},
#[error("final plan ID mismatch: expected {expected}, found `{actual}`")]
PlanIdMismatch {
expected: PlanId,
actual: String,
},
#[error("final link-unit ID mismatch: expected {expected}, found `{actual}`")]
LinkUnitMismatch {
expected: String,
actual: String,
},
#[error("final plan `{path}` must be named `{expected}`")]
PlanFilename {
path: PathBuf,
expected: String,
},
#[error("final RSJS plan file does not match the complete in-memory linked plan")]
PlanContentMismatch,
#[error("failed to serialize the expected final RSJS plan: {0}")]
SerializeExpectedPlan(String),
#[error("failed to prepare final RSJS unit projection: {0}")]
UnitProjection(String),
#[error(transparent)]
UnitMap(FinalUnitMapError),
#[error("final plan path is not UTF-8: `{0}`")]
NonUtf8PlanPath(PathBuf),
#[error("failed to run final Cargo `{program}`: {source}")]
SpawnCargo {
program: PathBuf,
#[source]
source: io::Error,
},
#[error("final Cargo process has no piped stdout")]
MissingCargoStdout,
#[error("failed to read final Cargo stdout: {0}")]
ReadCargoStdout(io::Error),
#[error("final Cargo protocol line {line} is not UTF-8: {source}")]
NonUtf8CargoProtocol {
line: usize,
#[source]
source: std::str::Utf8Error,
},
#[error("failed to wait for final Cargo: {0}")]
WaitForCargo(io::Error),
#[error("final Cargo stopped after the compiler diagnostic above ({0})")]
CargoFailed(ExitStatus),
#[error(transparent)]
CargoProtocol(CargoMessageError),
#[error("final Cargo compilation emitted no artifacts")]
NoCargoArtifacts,
}
#[cfg(test)]
mod tests {
use super::*;
use rsjs_ir::abi::{InvocationId, LinkUnitId};
use rsjs_ir::build::LinkPlanMaterial;
fn plan() -> TransportLinkPlan {
let material = LinkPlanMaterial {
link_unit: LinkUnitId::derive("final-link-unit"),
component_descriptors: Vec::new(),
helper_descriptors: Vec::new(),
payload: SemanticHash::derive("empty-final-payload"),
};
TransportLinkPlan {
link_unit: material.link_unit,
plan_id: material.plan_id(),
material,
components: Vec::new(),
helpers: Vec::new(),
shared_state_definitions: Vec::new(),
shared_state_acquisitions: Vec::new(),
}
}
#[test]
fn plan_environment_preserves_inherited_rustflags_byte_for_byte() {
let plan = plan();
let request = FinalCargoRequest {
invocation: InvocationId::derive("fixture-final-cargo-request"),
cargo: OsString::from("cargo"),
wrapper: PathBuf::from("/fixture/cargo-proa"),
working_directory: PathBuf::from("/fixture"),
source_exclusion_directory: PathBuf::from("/fixture/target"),
target_root: PathBuf::from("/fixture/target"),
plan_path: PathBuf::from("/fixture/plan.json"),
cargo_args: vec![OsString::from("build")],
environment: BTreeMap::new(),
root_unit_fingerprint: SemanticHash::derive("fixture"),
descriptor_units: Vec::new(),
};
let plain = OsString::from("--cfg custom_cfg -C opt-level=1");
let encoded = OsString::from("-Copt-level=2\u{1f}-Ctarget-cpu=native");
let mut environment = BTreeMap::from([
(OsString::from("RUSTFLAGS"), plain.clone()),
(OsString::from("CARGO_ENCODED_RUSTFLAGS"), encoded.clone()),
]);
inject_plan_environment(
&mut environment,
&request,
&plan,
&format!("sha256:{}", "1".repeat(64)),
Path::new("/fixture/unit-map.json"),
&"2".repeat(64),
)
.unwrap();
assert_eq!(environment.get(OsStr::new("RUSTFLAGS")), Some(&plain));
assert_eq!(
environment.get(OsStr::new("CARGO_ENCODED_RUSTFLAGS")),
Some(&encoded)
);
}
#[test]
fn final_target_is_stable_within_one_wrapper_policy() {
let root = Path::new("/fixture/final-target-root");
let first = InvocationId::derive("stable-final-target-a");
let second = InvocationId::derive("stable-final-target-b");
let expected = if cfg!(windows) {
root.join(format!("w{FINAL_WRAPPER_POLICY_VERSION}"))
} else {
root.join("rsjs-final")
.join(format!("wrapper-v{FINAL_WRAPPER_POLICY_VERSION}"))
};
assert_eq!(final_target_directory(root, first), expected);
assert_eq!(final_target_directory(root, second), expected);
}
#[test]
fn snapshot_final_artifact_preserves_immutable_generations() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("reusable-target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let source = target_directory.join("fixture-artifact");
fs::write(&source, b"artifact-a\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
fs::set_permissions(&source, fs::Permissions::from_mode(0o751)).unwrap();
}
let artifact = CargoCompilerArtifact {
package_id: "path+file:///fixture#snapshot_fixture@0.0.0".to_string(),
manifest_path: temp.path().join("Cargo.toml"),
target: CargoArtifactTarget {
name: "snapshot_fixture".to_string(),
kind: BTreeSet::from(["rlib".to_string()]),
crate_types: BTreeSet::from(["rlib".to_string()]),
src_path: temp.path().join("src/main.rs"),
edition: "2021".to_string(),
},
profile: serde_json::json!({"opt_level":"0","test":false}),
features: BTreeSet::new(),
filenames: BTreeSet::from([source.clone()]),
executable: None,
fresh: false,
};
let snapshot_a = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap()
.0;
let path_a = snapshot_a.filenames.iter().next().unwrap();
assert_eq!(fs::read(path_a).unwrap(), b"artifact-a\n");
fs::write(&source, b"artifact-b\n").unwrap();
let snapshot_b = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap()
.0;
let path_b = snapshot_b.filenames.iter().next().unwrap();
assert_ne!(path_a, path_b);
assert_eq!(fs::read(path_a).unwrap(), b"artifact-a\n");
assert_eq!(fs::read(path_b).unwrap(), b"artifact-b\n");
let snapshot_b_again = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap()
.0;
assert_eq!(snapshot_b_again.filenames, snapshot_b.filenames);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
assert_eq!(
fs::metadata(path_a).unwrap().permissions().mode() & 0o777,
0o751
);
assert_eq!(
fs::metadata(path_b).unwrap().permissions().mode() & 0o777,
0o751
);
}
}
#[cfg(windows)]
#[test]
fn windows_pe_snapshot_rejects_dll_beside_original_executable() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let executable = target_directory.join("fixture.exe");
fs::copy(std::env::current_exe().unwrap(), &executable).unwrap();
fs::write(
target_directory.join("bcryptprimitives.dll"),
b"app-local runtime",
)
.unwrap();
let artifact = snapshot_fixture_artifact(temp.path(), executable.clone());
assert!(matches!(
snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
),
Err(FinalCargoError::UnsupportedSnapshotDependency { path, dependency, reason })
if path == executable
&& dependency.eq_ignore_ascii_case("bcryptprimitives.dll")
&& reason.contains("app-local DLL")
));
assert_eq!(fs::read_dir(&snapshot_root).unwrap().count(), 0);
}
#[cfg(windows)]
#[test]
fn windows_pe_cached_wrapper_rechecks_original_dll_directory() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let executable = target_directory.join("wrapper.exe");
fs::copy(std::env::current_exe().unwrap(), &executable).unwrap();
let published =
snapshot_wrapper_executable(&source_exclusion, &snapshot_root, &executable).unwrap();
fs::write(
target_directory.join("bcryptprimitives.dll"),
b"app-local runtime",
)
.unwrap();
assert!(matches!(
snapshot_wrapper_executable(&source_exclusion, &snapshot_root, &executable),
Err(FinalCargoError::UnsupportedSnapshotDependency { path, dependency, reason })
if path == executable
&& dependency.eq_ignore_ascii_case("bcryptprimitives.dll")
&& reason.contains("app-local DLL")
));
assert_eq!(fs::read(published).unwrap(), fs::read(executable).unwrap());
}
#[cfg(windows)]
#[test]
fn windows_pe_snapshot_of_current_executable_can_launch() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let executable = target_directory.join("fixture.exe");
fs::copy(std::env::current_exe().unwrap(), &executable).unwrap();
let artifact = snapshot_fixture_artifact(temp.path(), executable.clone());
let snapshot = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap()
.0;
let published = snapshot.executable.unwrap();
assert_ne!(published, executable);
let output = Command::new(published).arg("--list").output().unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(String::from_utf8_lossy(&output.stdout)
.contains("windows_pe_snapshot_of_current_executable_can_launch"));
}
#[cfg(not(target_os = "windows"))]
#[test]
fn ordinary_host_executable_can_be_snapshotted() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("reusable-target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let executable = compile_snapshot_fixture(&target_directory, false);
let artifact = snapshot_fixture_artifact(temp.path(), executable.clone());
let snapshot = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap()
.0;
let published = snapshot.executable.unwrap();
assert_ne!(published, executable);
assert!(published.is_file());
}
#[cfg(not(target_os = "windows"))]
#[test]
fn dynamic_rust_executable_is_rejected_before_snapshot_publication() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("reusable-target");
let snapshot_root = source_exclusion.join("snapshots");
fs::create_dir_all(&target_directory).unwrap();
let executable = compile_snapshot_fixture(&target_directory, true);
let artifact = snapshot_fixture_artifact(temp.path(), executable);
let error = snapshot_final_artifact(
&source_exclusion,
&snapshot_root,
&target_directory,
&artifact,
)
.unwrap_err();
assert!(matches!(
error,
FinalCargoError::UnsupportedSnapshotDependency {
dependency,
reason,
..
} if dependency.to_ascii_lowercase().contains("libstd")
|| reason.contains("relative loader search path")
));
assert_eq!(fs::read_dir(&snapshot_root).unwrap().count(), 0);
}
#[test]
fn relative_loader_dependency_is_rejected_explicitly() {
let executable = Path::new("/fixture/target/app");
let target = Path::new("/fixture/target");
assert!(matches!(
validate_snapshot_dependency(
BinaryFormat::MachO,
executable,
target,
"@rpath/libfixture.dylib",
),
Err(FinalCargoError::UnsupportedSnapshotDependency {
dependency,
reason,
..
}) if dependency == "@rpath/libfixture.dylib" && reason.contains("relative")
));
}
#[test]
fn windows_pe_snapshots_read_regular_and_delayed_dlls_for_both_bitnesses() {
for is_64 in [false, true] {
let (bytes, _) = windows_pe_fixture(is_64, "KERNEL32.dll", "bcryptprimitives.dll");
let file = object::File::parse(bytes.as_slice()).unwrap();
let dependencies = executable_snapshot_dependencies(&file).unwrap();
assert_eq!(
dependencies.libraries,
BTreeSet::from([b"KERNEL32.dll".to_vec(), b"bcryptprimitives.dll".to_vec()])
);
for dependency in &dependencies.libraries {
validate_snapshot_dependency(
BinaryFormat::Pe,
Path::new("fixture.exe"),
Path::new("target"),
dependency_name(dependency).unwrap(),
)
.unwrap();
}
}
for runtime in ["msvcrt.dll", "vcruntime140.dll", "VCRUNTIME140_1.DLL"] {
assert!(platform_system_dependency(BinaryFormat::Pe, runtime));
}
}
#[test]
fn windows_pe_snapshot_does_not_silently_drop_an_app_local_runtime() {
let temp = tempfile::tempdir().unwrap();
let executable = temp.path().join("app.exe");
fs::write(temp.path().join("vcruntime140.dll"), b"app-local runtime").unwrap();
assert!(matches!(
validate_snapshot_dependency(BinaryFormat::Pe, &executable, temp.path(), "vcruntime140.dll"),
Err(FinalCargoError::UnsupportedSnapshotDependency { reason, .. })
if reason.contains("app-local DLL")
));
}
#[test]
fn windows_pe_snapshots_reject_non_system_dlls_in_either_import_table() {
for is_64 in [false, true] {
for unsupported in [
"plugin.dll",
"libstd-fixture.dll",
".\\kernel32.dll",
"api-ms-win-core-fixture\\plugin.dll",
"api-ms-win-core-fixture.dll:stream",
"api-ms-win-core-fixture",
] {
for (normal, delayed) in
[(unsupported, "kernel32.dll"), ("kernel32.dll", unsupported)]
{
let (bytes, _) = windows_pe_fixture(is_64, normal, delayed);
let file = object::File::parse(bytes.as_slice()).unwrap();
let dependencies = executable_snapshot_dependencies(&file).unwrap();
assert!(dependencies.libraries.iter().any(|dependency| {
validate_snapshot_dependency(
BinaryFormat::Pe,
Path::new("fixture.exe"),
Path::new("target"),
dependency_name(dependency).unwrap(),
)
.is_err()
}));
}
}
}
}
#[test]
fn windows_pe_snapshots_reject_malformed_or_legacy_delay_descriptors() {
for is_64 in [false, true] {
for attributes in [0_u32, 2, 0x8000_0000] {
let (mut bytes, section) = windows_pe_fixture(is_64, "kernel32.dll", "user32.dll");
bytes[section + 64..section + 68].copy_from_slice(&attributes.to_le_bytes());
let file = object::File::parse(bytes.as_slice()).unwrap();
assert!(executable_snapshot_dependencies(&file)
.unwrap_err()
.contains("RVA-based"));
}
for name_offset in [12, 68] {
let (mut bytes, section) = windows_pe_fixture(is_64, "kernel32.dll", "user32.dll");
bytes[section + name_offset..section + name_offset + 4]
.copy_from_slice(&u32::MAX.to_le_bytes());
let file = object::File::parse(bytes.as_slice()).unwrap();
assert!(executable_snapshot_dependencies(&file).is_err());
}
}
}
fn windows_pe_fixture(is_64: bool, normal: &str, delayed: &str) -> (Vec<u8>, usize) {
use object::write::pe::{NtHeaders, Writer};
let mut bytes = Vec::new();
let mut writer = Writer::new(is_64, 0x1000, 0x200, &mut bytes);
writer.reserve_dos_header();
writer.reserve_nt_headers(16);
writer.reserve_section_headers(1);
let section = writer.reserve_rdata_section(512);
writer.set_data_directory(
object::pe::IMAGE_DIRECTORY_ENTRY_IMPORT,
section.virtual_address,
40,
);
writer.set_data_directory(
object::pe::IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT,
section.virtual_address + 64,
64,
);
let mut data = vec![0; 512];
data[12..16].copy_from_slice(&(section.virtual_address + 160).to_le_bytes());
data[64..68].copy_from_slice(&1_u32.to_le_bytes());
data[68..72].copy_from_slice(&(section.virtual_address + 256).to_le_bytes());
data[160..160 + normal.len()].copy_from_slice(normal.as_bytes());
data[256..256 + delayed.len()].copy_from_slice(delayed.as_bytes());
writer.write_empty_dos_header().unwrap();
writer.write_nt_headers(NtHeaders {
machine: if is_64 {
object::pe::IMAGE_FILE_MACHINE_AMD64
} else {
object::pe::IMAGE_FILE_MACHINE_I386
},
time_date_stamp: 0,
characteristics: object::pe::IMAGE_FILE_EXECUTABLE_IMAGE,
major_linker_version: 0,
minor_linker_version: 0,
address_of_entry_point: 0,
image_base: 0x400000,
major_operating_system_version: 6,
minor_operating_system_version: 0,
major_image_version: 0,
minor_image_version: 0,
major_subsystem_version: 6,
minor_subsystem_version: 0,
subsystem: object::pe::IMAGE_SUBSYSTEM_WINDOWS_CUI,
dll_characteristics: object::pe::DllFlags(0),
size_of_stack_reserve: 0x100000,
size_of_stack_commit: 0x1000,
size_of_heap_reserve: 0x100000,
size_of_heap_commit: 0x1000,
});
writer.write_section_headers();
writer.write_section(section.file_offset, &data);
(bytes, section.file_offset as usize)
}
#[test]
fn elf_system_loader_soname_is_an_allowed_runtime_dependency() {
assert!(validate_snapshot_dependency(
BinaryFormat::Elf,
Path::new("/fixture/target/app"),
Path::new("/fixture/target"),
"ld-linux-x86-64.so.2",
)
.is_ok());
}
#[test]
fn executable_architecture_must_match_the_host() {
let host = host_architecture().expect("the test host must have a supported architecture");
let mismatch = if host == Architecture::X86_64 {
Architecture::Aarch64
} else {
Architecture::X86_64
};
assert!(matches!(
validate_snapshot_host_identity(
Path::new("/fixture/target/app"),
host_executable_format(),
mismatch,
),
Err(FinalCargoError::UnsupportedSnapshotExecutable { reason, .. })
if reason.contains("does not match the current host architecture")
));
}
#[test]
fn interpreter_inside_mutable_target_is_rejected() {
let temp = tempfile::TempDir::new().unwrap();
let target = temp.path().join("target");
let executable = target.join("app");
let interpreter = target.join("ld.so");
let interpreter = interpreter.to_str().unwrap();
assert!(matches!(
validate_snapshot_interpreter(&executable, &target, interpreter),
Err(FinalCargoError::UnsupportedSnapshotDependency {
dependency,
reason,
..
}) if dependency == interpreter && reason.contains("mutable final Cargo target")
));
}
#[cfg(not(target_os = "windows"))]
fn compile_snapshot_fixture(target_directory: &Path, prefer_dynamic: bool) -> PathBuf {
let source = target_directory.join(if prefer_dynamic {
"dynamic-fixture.rs"
} else {
"static-fixture.rs"
});
fs::write(&source, "fn main() {}\n").unwrap();
let executable = target_directory.join(if prefer_dynamic {
"dynamic-fixture"
} else {
"static-fixture"
});
let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let mut command = Command::new(rustc);
command.arg(&source).arg("-o").arg(&executable);
if prefer_dynamic {
command.arg("-Cprefer-dynamic");
}
let status = command.status().unwrap();
assert!(status.success(), "fixture rustc failed with {status}");
executable
}
fn snapshot_fixture_artifact(root: &Path, executable: PathBuf) -> CargoCompilerArtifact {
CargoCompilerArtifact {
package_id: "path+file:///fixture#snapshot_fixture@0.0.0".to_string(),
manifest_path: root.join("Cargo.toml"),
target: CargoArtifactTarget {
name: "snapshot_fixture".to_string(),
kind: BTreeSet::from(["bin".to_string()]),
crate_types: BTreeSet::from(["bin".to_string()]),
src_path: root.join("src/main.rs"),
edition: "2021".to_string(),
},
profile: serde_json::json!({"opt_level":"0","test":false}),
features: BTreeSet::new(),
filenames: BTreeSet::from([executable.clone()]),
executable: Some(executable),
fresh: false,
}
}
#[test]
fn final_cargo_rewrites_execution_requests_to_compile_only_arguments() {
let arguments = vec![
OsString::from("test"),
OsString::from("-p"),
OsString::from("demo"),
OsString::from("--test"),
OsString::from("http"),
OsString::from("--"),
OsString::from("--nocapture"),
OsString::from("--target-dir"),
];
assert_eq!(
compile_only_cargo_arguments(&arguments).unwrap(),
[
OsString::from("test"),
OsString::from("-p"),
OsString::from("demo"),
OsString::from("--test"),
OsString::from("http"),
OsString::from("--no-run"),
]
);
assert_eq!(
compile_only_cargo_arguments(
&["run", "-p", "demo", "--bin", "server", "--", "--probe",].map(OsString::from)
)
.unwrap(),
[
OsString::from("build"),
OsString::from("-p"),
OsString::from("demo"),
OsString::from("--bin"),
OsString::from("server"),
]
);
assert!(matches!(
compile_only_cargo_arguments(&["build", "--", "ignored"].map(OsString::from)),
Err(FinalCargoError::RuntimeArgumentsForCompileOnlySubcommand(subcommand))
if subcommand == OsStr::new("build")
));
}
#[test]
fn final_unit_identity_changes_with_the_plan() {
let fingerprint = SemanticHash::derive("root-unit");
let first = FinalUnitId::for_plan(fingerprint, plan().plan_id);
let second = FinalUnitId::for_plan(
fingerprint,
PlanId::derive(&InvocationId::derive("other-plan")),
);
assert_ne!(first, second);
}
#[test]
fn managed_final_paths_must_remain_beneath_source_exclusion() {
let temp = tempfile::TempDir::new().unwrap();
let exclusion = temp.path().join("generated");
let outside = temp.path().join("outside/final");
assert!(matches!(
ensure_plain_managed_directory(&exclusion, &outside),
Err(ManagedPathError::OutsideSourceExclusion { .. })
));
}
#[cfg(unix)]
#[test]
fn managed_final_paths_reject_symlinked_directories_and_plan_files() {
use std::os::unix::fs::symlink;
let temp = tempfile::TempDir::new().unwrap();
let exclusion = temp.path().join("generated");
let outside = temp.path().join("outside");
fs::create_dir_all(&outside).unwrap();
ensure_plain_managed_directory(&exclusion, &exclusion).unwrap();
let redirected_target = exclusion.join("final-target");
symlink(&outside, &redirected_target).unwrap();
assert!(matches!(
ensure_plain_managed_directory(&exclusion, &redirected_target),
Err(ManagedPathError::NonDirectory(path)) if path == redirected_target
));
let plans = exclusion.join("plans");
ensure_plain_managed_directory(&exclusion, &plans).unwrap();
let real_plan = outside.join("plan.json");
fs::write(&real_plan, b"{}").unwrap();
let redirected_plan = plans.join("plan.json");
symlink(&real_plan, &redirected_plan).unwrap();
assert!(matches!(
ensure_plain_managed_file(&exclusion, &redirected_plan),
Err(ManagedPathError::NonFile(path)) if path == redirected_plan
));
}
#[test]
fn same_ids_cannot_authorize_different_plan_content() {
let plan = plan();
let temp = tempfile::TempDir::new().unwrap();
let path = temp
.path()
.join(format!("{}.transport-plan.json", plan.plan_id));
let mut value = transport_plan_json(&plan);
value["payload_hash"] = Value::String(format!("sha256:{}", "ff".repeat(32)));
fs::write(&path, serde_json::to_vec_pretty(&value).unwrap()).unwrap();
assert!(matches!(
validate_plan_file(&path, &plan),
Err(FinalCargoError::PlanContentMismatch)
));
}
#[test]
fn leased_snapshot_survives_prune_until_every_holder_releases_it() {
let temp = tempfile::TempDir::new().unwrap();
let source_exclusion = temp.path().join("generated");
let target_directory = source_exclusion.join("reusable-target");
let snapshot_root = source_exclusion.join(FINAL_SNAPSHOT_ROOT_NAME);
fs::create_dir_all(&target_directory).unwrap();
fs::create_dir_all(&snapshot_root).unwrap();
let publish = || {
snapshot_final_file(
&source_exclusion,
&snapshot_root,
&compile_or_copy_launchable(&target_directory),
Some(&target_directory),
true,
)
.unwrap()
};
let (first_path, first_lease) = publish();
let (second_path, second_lease) = publish();
assert_eq!(first_path, second_path);
assert_eq!(
fs::read_dir(first_path.parent().unwrap()).unwrap().count(),
1
);
drop(first_lease);
assert!(!prune_final_artifact_snapshot(&first_path));
assert!(second_path.is_file(), "a held snapshot was pruned");
drop(second_lease);
assert!(prune_final_artifact_snapshot(&first_path));
assert!(!first_path.exists());
assert_eq!(fs::read_dir(&snapshot_root).unwrap().count(), 0);
let (third_path, _third_lease) = publish();
assert_eq!(third_path, first_path);
assert!(third_path.is_file());
}
#[test]
fn prune_ignores_paths_outside_leased_snapshot_directories() {
let temp = tempfile::TempDir::new().unwrap();
let unrelated = temp.path().join("other/abc123/app");
fs::create_dir_all(unrelated.parent().unwrap()).unwrap();
fs::write(&unrelated, b"keep").unwrap();
assert!(!prune_final_artifact_snapshot(&unrelated));
assert!(unrelated.is_file());
let legacy = temp
.path()
.join(FINAL_SNAPSHOT_ROOT_NAME)
.join("abc123-mode-100755/app");
fs::create_dir_all(legacy.parent().unwrap()).unwrap();
fs::write(&legacy, b"keep").unwrap();
assert!(!prune_final_artifact_snapshot(&legacy));
assert!(legacy.is_file());
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
let outside = temp.path().join("outside");
fs::create_dir_all(&outside).unwrap();
fs::write(outside.join("app"), b"keep").unwrap();
let symlinked_dir = temp
.path()
.join(FINAL_SNAPSHOT_ROOT_NAME)
.join("symlinked-mode-100755");
symlink(&outside, &symlinked_dir).unwrap();
let lease_dir = temp.path().join(FINAL_SNAPSHOT_LEASE_ROOT_NAME);
fs::create_dir_all(&lease_dir).unwrap();
fs::write(lease_dir.join("symlinked-mode-100755.lease"), b"").unwrap();
assert!(!prune_final_artifact_snapshot(&symlinked_dir.join("app")));
assert!(outside.join("app").is_file());
}
}
fn compile_or_copy_launchable(target_directory: &Path) -> PathBuf {
#[cfg(windows)]
{
let executable = target_directory.join("fixture.exe");
if !executable.is_file() {
fs::copy(std::env::current_exe().unwrap(), &executable).unwrap();
}
executable
}
#[cfg(not(windows))]
{
let executable = target_directory.join("static-fixture");
if executable.is_file() {
executable
} else {
compile_snapshot_fixture(target_directory, false)
}
}
}
#[test]
fn only_cargo_progress_lines_stream_while_optimistic_output_is_held() {
for line in [
" Compiling proa_ui v0.4.0 (/work/proa_ui)\n",
" Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.2s\n",
" Downloaded serde v1.0.0\n",
"\x1b[1m\x1b[92m Compiling\x1b[0m site v0.1.0\n",
] {
assert!(is_cargo_status_line(line.as_bytes()), "{line:?}");
}
for line in [
"error: could not compile `proa_ui` (lib) due to 2 previous errors\n",
"warning: build failed, waiting for other jobs to finish...\n",
"\x1b[1m\x1b[91merror\x1b[0m: could not compile `site`\n",
"Caused by:\n",
" process didn't exit successfully: `build-script-build`\n",
"\n",
] {
assert!(!is_cargo_status_line(line.as_bytes()), "{line:?}");
}
}
#[test]
fn is_unmapped_rsjs_unit_diagnostic_matches_only_unmapped_marker() {
assert!(is_unmapped_rsjs_unit_diagnostic(
"error: final RSJS compilation could not map this Cargo unit to its descriptor pass (`RSJS_FINAL_CARGO_UNIT_STATUS=unmapped-v1`)"
));
assert!(!is_unmapped_rsjs_unit_diagnostic(
"error[E0425]: cannot find value `missing` in this scope"
));
}
}