diff --git a/crates/conformance/src/cases/mod.rs b/crates/conformance/src/cases/mod.rs index e2e29af93..df648b95f 100644 --- a/crates/conformance/src/cases/mod.rs +++ b/crates/conformance/src/cases/mod.rs @@ -4219,13 +4219,13 @@ fn verify_security(root: &Path, id: &str) { assert_contains(root, "crates/prismpm/src/config.rs", &["ParentDir"]); } "SE-03" => { - let source = read(root, "crates/prismpm/src/verification.rs"); + let source = read(root, "crates/prismpm/src/sdk/process.rs"); assert!(source.contains("Command::new")); assert!(!source.contains("sh -c")); } "SE-04" => assert_contains( root, - "crates/prismpm/src/verification.rs", + "crates/prismpm/src/sdk/process.rs", &["env_clear", "LC_ALL", "LANG"], ), "SE-05" => assert_contains( diff --git a/crates/prismpm/src/replay/mod.rs b/crates/prismpm/src/replay/mod.rs index b8d9bed57..4106d417d 100644 --- a/crates/prismpm/src/replay/mod.rs +++ b/crates/prismpm/src/replay/mod.rs @@ -14,7 +14,7 @@ use crate::error::PrismError; use crate::holo::archive::parse_application; use crate::holo::canonical::encode_value; -use crate::verification::{executable, run_process_limited}; +use crate::sdk::process::{executable, run_process_limited, NoExtension}; use serde_json::{json, Map, Value}; use sha2::{Digest, Sha256}; use std::collections::{BTreeMap, BTreeSet}; @@ -254,7 +254,7 @@ fn run_guest(guest: &[u8], allocation: u64, requests: &[String]) -> Result( "replay", &node, &[&runner, &wasm, &plan, &outcomes].map(|path| path.to_string_lossy().into_owned()), diff --git a/crates/prismpm/src/sdk.rs b/crates/prismpm/src/sdk.rs index 892a9927e..72d055ce4 100644 --- a/crates/prismpm/src/sdk.rs +++ b/crates/prismpm/src/sdk.rs @@ -18,6 +18,7 @@ use std::path::{Component, Path}; mod advisory; mod executable_file; mod executable_inventory; +pub(crate) mod process; pub(crate) use executable_file::ExecutableFile; pub(crate) const STDLIB_SOURCES: &[u8] = include_bytes!("../sdk/stdlib-sources.tar"); @@ -410,7 +411,7 @@ pub fn fetch_project_dependencies(root: &Path) -> Result, let mut records = Vec::new(); if root.join("Cargo.toml").is_file() && root.join("Cargo.lock").is_file() { let cargo = executable("cargo")?; - let record = crate::verification::run_process( + let record = process::run_process::( "cargo-fetch", &cargo, &["fetch".to_owned(), "--locked".to_owned()], @@ -426,7 +427,7 @@ pub fn fetch_project_dependencies(root: &Path) -> Result, } if root.join("package.json").is_file() && root.join("package-lock.json").is_file() { let npm = executable("npm")?; - let record = crate::verification::run_process( + let record = process::run_process::( "npm-ci", &npm, &[ @@ -1402,7 +1403,7 @@ fn capture_platform_update( environment.insert(name.to_owned(), value.to_string_lossy().into_owned()); } } - let result = crate::verification::run_process_limited( + let result = process::run_process_limited::( "sdk-platform-update", &node, &[ diff --git a/crates/prismpm/src/sdk/process.rs b/crates/prismpm/src/sdk/process.rs new file mode 100644 index 000000000..22e292e15 --- /dev/null +++ b/crates/prismpm/src/sdk/process.rs @@ -0,0 +1,449 @@ +//! Bounded child processes: executable selection against pinned digests, a +//! cleared environment, a timeout wrapper and an output limit. +//! +//! Replay and the SDK inventory check run processes too, so the helpers live +//! in `verify` and the export chain in `verification` (images) calls them +//! (docs/proposals/440-nine-modules.md, "process helpers from verification"). + +use crate::error::PrismError; +use serde::Serialize; +use sha2::{Digest, Sha256}; +use std::collections::BTreeMap; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +pub(crate) const CHILD_OUTPUT_LIMIT: usize = 16_777_216; + +pub(crate) const CHILD_TIMEOUT_SECONDS: &str = "300"; + +const ELAN_PROXY_SHA256: &str = "840179e70803ef373c2ec53342d6a45ea7d022533e4145489fc1278b4f716385"; + +const RUSTUP_PROXY_SHA256: &str = + "20a06e644b0d9bd2fbdbfd52d42540bdde820ea7df86e92e533c073da0cdd43c"; + +const TIMEOUT_SHA256: &str = "5ef0eaaaa4220593add7716aad74da927ca3bb10605e964330de64fecc3ef15e"; + +// Node 22.23.2 x64 binary from the Dockerfile's checksum-verified tarball. +const NODE_SHA256: &str = "3517c2df0b2f8cd7f422b4b8450ef81c6889f08eb03e281d6de9079b15e6a327"; + +/// One bounded child process run and its normalized outcome. +/// +/// `X` is the caller's own extension record. The `images` exporter attaches +/// its custody record here; verification callers attach none, so this module +/// names no type from the modules above it. +#[derive(Debug, Serialize)] +pub(crate) struct ProcessRecord { + pub(crate) tool: String, + pub(crate) argv: Vec, + pub(crate) executable_sha256: String, + pub(crate) exit_code: i32, + pub(crate) stdout: String, + pub(crate) stderr: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub(crate) exporter: Option, +} + +/// The extension of a process record that carries none. +#[derive(Debug, Serialize)] +pub(crate) enum NoExtension {} + +pub(crate) fn executable(name: &str) -> Result { + // A released SDK has a generated, signed inventory of the exact final + // runtime filesystem for its architecture. It is the authority for + // executables in that environment; the constants below remain the + // independent pins for the source-bootstrap devcontainer and native CLI. + if super::inventory_path().is_some() { + return super::executable(name); + } + let path = + std::env::var_os("PATH").ok_or_else(|| PrismError::new("PP5008", "PATH is unavailable"))?; + for directory in std::env::split_paths(&path) { + let candidate = directory.join(name); + if candidate.is_file() { + let resolved = candidate.canonicalize().map_err(|error| { + PrismError::new("PP5008", format!("{}: {error}", candidate.display())) + })?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(&resolved) + .map_err(|error| { + PrismError::new("PP5008", format!("{}: {error}", resolved.display())) + })? + .permissions() + .mode(); + if mode & 0o111 == 0 { + return Err(PrismError::new( + "PP5008", + format!("required executable {name} is not executable"), + )); + } + } + let expected = match name { + "lake" | "lean" => Some(ELAN_PROXY_SHA256), + "rustc" | "rustfmt" => Some(RUSTUP_PROXY_SHA256), + "timeout" => Some(TIMEOUT_SHA256), + "node" => Some(NODE_SHA256), + _ => None, + }; + if let Some(expected) = expected { + let observed = format!( + "{:x}", + Sha256::digest(std::fs::read(&resolved).map_err(|error| { + PrismError::new("PP5008", format!("{}: {error}", resolved.display())) + })?) + ); + if observed != expected { + return Err(PrismError::new( + "PP5008", + format!("required executable {name} has an unpinned digest"), + )); + } + } + return Ok(candidate); + } + } + Err(PrismError::new( + "PP5008", + format!("required executable {name} is unavailable"), + )) +} + +// Coalesce only adjacent pre-child selections. Bootstrap keeps every original +// pinned launcher check; SDK commands retain fresh selected-executable hashes. +pub(crate) fn executables(names: [&str; N]) -> Result<[PathBuf; N], PrismError> { + if super::inventory_path().is_some() { + return super::executables(names); + } + let mut paths = std::array::from_fn(|_| PathBuf::new()); + for (name, path) in names.into_iter().zip(&mut paths) { + *path = executable(name)?; + } + Ok(paths) +} + +pub(crate) fn normalized(value: &str, replacements: &[(&Path, &str)]) -> String { + let mut out = value.to_owned(); + for (path, token) in replacements { + out = out.replace(&path.to_string_lossy().replace('\\', "/"), token); + out = out.replace(path.to_string_lossy().as_ref(), token); + } + out +} + +pub(crate) fn stable_success_output(tool: &str, value: String) -> String { + let had_final_newline = value.ends_with('\n'); + let mut value = value + .lines() + .map(|line| { + if let Some((prefix, duration)) = line.rsplit_once(" in ") { + if duration + .strip_suffix('s') + .is_some_and(|seconds| seconds.parse::().is_ok()) + { + return format!("{prefix} in "); + } + } + line.to_owned() + }) + .collect::>() + .join("\n"); + if had_final_newline { + value.push('\n'); + } + if tool == "hologram-oracle-build" { + return String::new(); + } + if tool == "hologram-oracle" { + return value + .lines() + .find(|line| line.starts_with('{')) + .map_or_else(String::new, |line| format!("{line}\n")); + } + if !matches!( + tool, + "lake-build-generated" + | "lean4-prod-build" + | "prod-export" + | "application-lean" + | "application-exporter" + | "application-export" + ) { + return value; + } + let mut lines = value + .lines() + .map(|line| { + let mut line = line.to_owned(); + for prefix in ["✔ [", "⚠ ["] { + if line.starts_with(prefix) { + if let Some((_, message)) = line.split_once("] ") { + line = format!("{} {message}", &prefix[..prefix.len() - 2]); + } + break; + } + } + if line.contains("Built ") { + if let Some((prefix, suffix)) = line.rsplit_once(" (") { + if suffix.ends_with(')') { + return format!("{prefix} ()"); + } + } + } + line + }) + .collect::>(); + lines.sort(); + if lines.is_empty() { + String::new() + } else { + format!("{}\n", lines.join("\n")) + } +} + +pub(crate) fn pipe(mut reader: R, limit: usize) -> std::io::Result> { + let mut saved = Vec::new(); + let mut buffer = [0_u8; 8192]; + loop { + let count = reader.read(&mut buffer)?; + if count == 0 { + break; + } + if saved.len() <= limit { + let remaining = limit.saturating_add(1).saturating_sub(saved.len()); + saved.extend_from_slice(&buffer[..count.min(remaining)]); + } + } + Ok(saved) +} + +// Keep both ends of a failed child's progress without enlarging the process +// output allowance, retaining decoded source graphs, or changing its deadline. +pub(crate) fn failure_excerpt(output: &str) -> String { + const END_BYTES: usize = 2048; + if output.len() <= 2 * END_BYTES { + return output.to_owned(); + } + let mut prefix = END_BYTES; + while !output.is_char_boundary(prefix) { + prefix -= 1; + } + let mut suffix = output.len() - END_BYTES; + while !output.is_char_boundary(suffix) { + suffix += 1; + } + format!( + "{}\n[{} bytes omitted]\n{}", + &output[..prefix], + suffix - prefix, + &output[suffix..] + ) +} + +pub(crate) fn run_process( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, +) -> Result, PrismError> { + run_process_limited( + tool, + program, + args, + cwd, + extra_env, + replacements, + failure_code, + CHILD_TIMEOUT_SECONDS, + CHILD_OUTPUT_LIMIT, + ) +} + +#[allow(clippy::too_many_arguments)] +pub(crate) fn run_process_limited( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, + timeout_seconds: &str, + output_limit: usize, +) -> Result, PrismError> { + run_process_limited_allowed( + tool, + program, + args, + cwd, + extra_env, + replacements, + failure_code, + timeout_seconds, + output_limit, + &[0], + ) +} + +/// Names the stage a build or verification is in, on stderr as +/// `prismpm: stage `, when `PRISMPM_PROGRESS` is set (issue #585). +/// A caller that streams stderr, such as the guided journey, can then say +/// which stage is running. Without the variable it prints nothing, so no +/// output another tool reads changes. +pub(crate) fn progress(stage: &str) { + if std::env::var_os("PRISMPM_PROGRESS").is_some() { + use std::io::Write as _; + let mut stderr = std::io::stderr().lock(); + let _ = writeln!(stderr, "prismpm: stage {stage}"); + let _ = stderr.flush(); + } +} + +#[allow(clippy::too_many_arguments)] +pub(crate) fn run_process_limited_allowed( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, + timeout_seconds: &str, + output_limit: usize, + allowed_exit_codes: &[i32], +) -> Result, PrismError> { + if allowed_exit_codes.is_empty() + || allowed_exit_codes + .iter() + .any(|code| !(0..=125).contains(code)) + { + return Err(PrismError::new( + "PP9001", + "allowed process exit-code set is empty or malformed", + )); + } + progress(tool); + let timeout = executable("timeout")?; + let program_bytes = + std::fs::read(program).or_else(|_| program.canonicalize().and_then(std::fs::read)); + let executable_sha256 = format!( + "{:x}", + Sha256::digest(program_bytes.map_err(|error| { + PrismError::new("PP5008", format!("{}: {error}", program.display())) + })?) + ); + let mut command = Command::new(timeout); + command + .arg("--signal=TERM") + .arg("--kill-after=5") + .arg(timeout_seconds) + .arg(program) + .args(args) + .current_dir(cwd) + .env_clear() + .env("LC_ALL", "C") + .env("LANG", "C") + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + for key in [ + "PATH", + "ELAN_HOME", + "ELAN_TOOLCHAIN", + "RUSTUP_HOME", + "RUSTUP_TOOLCHAIN", + "CARGO_HOME", + "HOME", + "XDG_CACHE_HOME", + ] { + if let Some(value) = std::env::var_os(key) { + command.env(key, value); + } + } + for (key, value) in extra_env { + command.env(key, value); + } + // This bounds Lake's runtime pool, not Lean CLI children: those require + // the traced -j2 argument in lean_project's generated configuration. + // No build, export, kernel check or deadline is removed or extended. + command.env("LEAN_NUM_THREADS", "2"); + let mut child = command + .spawn() + .map_err(|error| PrismError::new("PP5008", format!("start {tool}: {error}")))?; + let stdout = child + .stdout + .take() + .ok_or_else(|| PrismError::new("PP9001", "child stdout pipe is missing"))?; + let stderr = child + .stderr + .take() + .ok_or_else(|| PrismError::new("PP9001", "child stderr pipe is missing"))?; + let stdout_thread = std::thread::spawn(move || pipe(stdout, output_limit)); + let stderr_thread = std::thread::spawn(move || pipe(stderr, output_limit)); + let status = child + .wait() + .map_err(|error| PrismError::new(failure_code, format!("wait for {tool}: {error}")))?; + let stdout = stdout_thread + .join() + .map_err(|_| PrismError::new("PP9001", "stdout reader terminated"))? + .map_err(|error| PrismError::new(failure_code, format!("read stdout: {error}")))?; + let stderr = stderr_thread + .join() + .map_err(|_| PrismError::new("PP9001", "stderr reader terminated"))? + .map_err(|error| PrismError::new(failure_code, format!("read stderr: {error}")))?; + if stdout.len() > output_limit || stderr.len() > output_limit { + return Err(PrismError::new( + "PP5007", + format!("{tool} output limit exceeded"), + )); + } + let exit_code = status.code().unwrap_or(128); + let stdout = normalized(&String::from_utf8_lossy(&stdout), replacements); + let stderr = normalized(&String::from_utf8_lossy(&stderr), replacements); + if matches!(exit_code, 124 | 137) { + let mut error = PrismError::new("PP5007", format!("{tool} timed out")); + // The observed wrapper exit is evidence, not a claim that exit 137 + // identifies a timeout rather than an external resource kill. + for message in [ + format!("observed process exit code: {exit_code}"), + format!("stdout: {}", failure_excerpt(&stdout)), + format!("stderr: {}", failure_excerpt(&stderr)), + format!( + "normalized child streams: stdout_bytes={}, stdout_sha256={:x}; stderr_bytes={}, stderr_sha256={:x}", + stdout.len(), + Sha256::digest(stdout.as_bytes()), + stderr.len(), + Sha256::digest(stderr.as_bytes()) + ), + ] { + error.notes.push(crate::error::PrismNote { + message, + span: None, + }); + } + return Err(error); + } + if !allowed_exit_codes.contains(&exit_code) { + return Err(PrismError::new( + failure_code, + format!("{tool} exited {exit_code}: stdout={stdout:?}; stderr={stderr:?}"), + )); + } + let stdout = stable_success_output(tool, stdout); + let stderr = stable_success_output(tool, stderr); + Ok(ProcessRecord { + tool: tool.to_owned(), + argv: args + .iter() + .map(|arg| normalized(arg, replacements)) + .collect(), + executable_sha256, + exit_code, + stdout, + stderr, + exporter: None, + }) +} diff --git a/crates/prismpm/src/verification.rs b/crates/prismpm/src/verification.rs index dc7cfab07..ab60fbf2d 100644 --- a/crates/prismpm/src/verification.rs +++ b/crates/prismpm/src/verification.rs @@ -13,17 +13,102 @@ use crate::holo::canonical::{content_id, encode_value}; use crate::holo::model_document::decode_canonical; use fs4::fs_std::FileExt; use lexlean::{Engine, Selection, VerifyRequest as LexVerifyRequest}; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; use serde_json::{json, Value}; use sha2::{Digest, Sha256}; use std::collections::{BTreeMap, BTreeSet}; use std::fs::{File, OpenOptions}; -use std::io::{Read, Write}; +use std::io::Write; use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; -pub(crate) const CHILD_OUTPUT_LIMIT: usize = 16_777_216; -const CHILD_TIMEOUT_SECONDS: &str = "300"; +pub(crate) use crate::sdk::process::{executable, executables, progress, CHILD_OUTPUT_LIMIT}; +#[cfg(test)] +use crate::sdk::process::{failure_excerpt, stable_success_output, CHILD_TIMEOUT_SECONDS}; +#[cfg(test)] +use std::process::Command; + +/// A process record of the export chain, which may carry the exporter's +/// custody record. +pub(crate) type ProcessRecord = + crate::sdk::process::ProcessRecord; + +/// Run one bounded child process for the export chain. +pub(crate) fn run_process( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, +) -> Result { + crate::sdk::process::run_process( + tool, + program, + args, + cwd, + extra_env, + replacements, + failure_code, + ) +} + +/// Run one bounded child process for the export chain with its own deadline +/// and output limit. +#[allow(clippy::too_many_arguments)] +pub(crate) fn run_process_limited( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, + timeout_seconds: &str, + output_limit: usize, +) -> Result { + crate::sdk::process::run_process_limited( + tool, + program, + args, + cwd, + extra_env, + replacements, + failure_code, + timeout_seconds, + output_limit, + ) +} + +/// Run one bounded child process for the export chain that may exit with one +/// of `allowed_exit_codes`. +#[allow(clippy::too_many_arguments)] +pub(crate) fn run_process_limited_allowed( + tool: &str, + program: &Path, + args: &[String], + cwd: &Path, + extra_env: &BTreeMap, + replacements: &[(&Path, &str)], + failure_code: &'static str, + timeout_seconds: &str, + output_limit: usize, + allowed_exit_codes: &[i32], +) -> Result { + crate::sdk::process::run_process_limited_allowed( + tool, + program, + args, + cwd, + extra_env, + replacements, + failure_code, + timeout_seconds, + output_limit, + allowed_exit_codes, + ) +} + const GENERATED_NATIVE_BUILD_TIMEOUT_SECONDS: &str = "600"; const ROOTS_SOURCE: &str = include_str!("../model/runtime-roots.toml"); const STDLIB_EXPORTS_SOURCE: &str = include_str!("../model/stdlib-exports.toml"); @@ -36,12 +121,6 @@ const HOLOGRAM_ORACLE_CARGO: &[u8] = include_bytes!("embedded/hologram-oracle.Ca const HOLOGRAM_ORACLE_LOCK: &[u8] = include_bytes!("embedded/hologram-oracle.Cargo.lock"); const HOLOGRAM_ORACLE_MAIN: &[u8] = include_bytes!("embedded/hologram-oracle.main.rs"); const HOLOGRAM_ORACLE_BROWSER: &[u8] = include_bytes!("embedded/hologram-oracle.browser.mjs"); -const ELAN_PROXY_SHA256: &str = "840179e70803ef373c2ec53342d6a45ea7d022533e4145489fc1278b4f716385"; -const RUSTUP_PROXY_SHA256: &str = - "20a06e644b0d9bd2fbdbfd52d42540bdde820ea7df86e92e533c073da0cdd43c"; -const TIMEOUT_SHA256: &str = "5ef0eaaaa4220593add7716aad74da927ca3bb10605e964330de64fecc3ef15e"; -// Node 22.23.2 x64 binary from the Dockerfile's checksum-verified tarball. -const NODE_SHA256: &str = "3517c2df0b2f8cd7f422b4b8450ef81c6889f08eb03e281d6de9079b15e6a327"; #[derive(Deserialize)] #[serde(deny_unknown_fields)] @@ -276,93 +355,6 @@ fn execution_corpus(roots: &RuntimeRoots) -> Result<(ExecutionCorpus, String), P Ok((corpus, corpus_sha256)) } -#[derive(Debug, Serialize)] -pub(crate) struct ProcessRecord { - pub(crate) tool: String, - pub(crate) argv: Vec, - pub(crate) executable_sha256: String, - pub(crate) exit_code: i32, - pub(crate) stdout: String, - pub(crate) stderr: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub(crate) exporter: Option, -} - -pub(crate) fn executable(name: &str) -> Result { - // A released SDK has a generated, signed inventory of the exact final - // runtime filesystem for its architecture. It is the authority for - // executables in that environment; the constants below remain the - // independent pins for the source-bootstrap devcontainer and native CLI. - if crate::sdk::inventory_path().is_some() { - return crate::sdk::executable(name); - } - let path = - std::env::var_os("PATH").ok_or_else(|| PrismError::new("PP5008", "PATH is unavailable"))?; - for directory in std::env::split_paths(&path) { - let candidate = directory.join(name); - if candidate.is_file() { - let resolved = candidate.canonicalize().map_err(|error| { - PrismError::new("PP5008", format!("{}: {error}", candidate.display())) - })?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - let mode = std::fs::metadata(&resolved) - .map_err(|error| { - PrismError::new("PP5008", format!("{}: {error}", resolved.display())) - })? - .permissions() - .mode(); - if mode & 0o111 == 0 { - return Err(PrismError::new( - "PP5008", - format!("required executable {name} is not executable"), - )); - } - } - let expected = match name { - "lake" | "lean" => Some(ELAN_PROXY_SHA256), - "rustc" | "rustfmt" => Some(RUSTUP_PROXY_SHA256), - "timeout" => Some(TIMEOUT_SHA256), - "node" => Some(NODE_SHA256), - _ => None, - }; - if let Some(expected) = expected { - let observed = format!( - "{:x}", - Sha256::digest(std::fs::read(&resolved).map_err(|error| { - PrismError::new("PP5008", format!("{}: {error}", resolved.display())) - })?) - ); - if observed != expected { - return Err(PrismError::new( - "PP5008", - format!("required executable {name} has an unpinned digest"), - )); - } - } - return Ok(candidate); - } - } - Err(PrismError::new( - "PP5008", - format!("required executable {name} is unavailable"), - )) -} - -// Coalesce only adjacent pre-child selections. Bootstrap keeps every original -// pinned launcher check; SDK commands retain fresh selected-executable hashes. -pub(crate) fn executables(names: [&str; N]) -> Result<[PathBuf; N], PrismError> { - if crate::sdk::inventory_path().is_some() { - return crate::sdk::executables(names); - } - let mut paths = std::array::from_fn(|_| PathBuf::new()); - for (name, path) in names.into_iter().zip(&mut paths) { - *path = executable(name)?; - } - Ok(paths) -} - pub(crate) struct Toolchain { lake: PathBuf, rustfmt: PathBuf, @@ -480,85 +472,6 @@ pub(crate) fn preflight_toolchain( }) } -fn normalized(value: &str, replacements: &[(&Path, &str)]) -> String { - let mut out = value.to_owned(); - for (path, token) in replacements { - out = out.replace(&path.to_string_lossy().replace('\\', "/"), token); - out = out.replace(path.to_string_lossy().as_ref(), token); - } - out -} - -fn stable_success_output(tool: &str, value: String) -> String { - let had_final_newline = value.ends_with('\n'); - let mut value = value - .lines() - .map(|line| { - if let Some((prefix, duration)) = line.rsplit_once(" in ") { - if duration - .strip_suffix('s') - .is_some_and(|seconds| seconds.parse::().is_ok()) - { - return format!("{prefix} in "); - } - } - line.to_owned() - }) - .collect::>() - .join("\n"); - if had_final_newline { - value.push('\n'); - } - if tool == "hologram-oracle-build" { - return String::new(); - } - if tool == "hologram-oracle" { - return value - .lines() - .find(|line| line.starts_with('{')) - .map_or_else(String::new, |line| format!("{line}\n")); - } - if !matches!( - tool, - "lake-build-generated" - | "lean4-prod-build" - | "prod-export" - | "application-lean" - | "application-exporter" - | "application-export" - ) { - return value; - } - let mut lines = value - .lines() - .map(|line| { - let mut line = line.to_owned(); - for prefix in ["✔ [", "⚠ ["] { - if line.starts_with(prefix) { - if let Some((_, message)) = line.split_once("] ") { - line = format!("{} {message}", &prefix[..prefix.len() - 2]); - } - break; - } - } - if line.contains("Built ") { - if let Some((prefix, suffix)) = line.rsplit_once(" (") { - if suffix.ends_with(')') { - return format!("{prefix} ()"); - } - } - } - line - }) - .collect::>(); - lines.sort(); - if lines.is_empty() { - String::new() - } else { - format!("{}\n", lines.join("\n")) - } -} - fn hologram_oracle_environment(root: &Path) -> BTreeMap { // run_process clears the ambient Cargo policy. Bound this independent // cold compiler explicitly; debug symbols and incremental state do not @@ -1077,45 +990,6 @@ mod portable_oracle_tests { } } -fn pipe(mut reader: R, limit: usize) -> std::io::Result> { - let mut saved = Vec::new(); - let mut buffer = [0_u8; 8192]; - loop { - let count = reader.read(&mut buffer)?; - if count == 0 { - break; - } - if saved.len() <= limit { - let remaining = limit.saturating_add(1).saturating_sub(saved.len()); - saved.extend_from_slice(&buffer[..count.min(remaining)]); - } - } - Ok(saved) -} - -// Keep both ends of a failed child's progress without enlarging the process -// output allowance, retaining decoded source graphs, or changing its deadline. -fn failure_excerpt(output: &str) -> String { - const END_BYTES: usize = 2048; - if output.len() <= 2 * END_BYTES { - return output.to_owned(); - } - let mut prefix = END_BYTES; - while !output.is_char_boundary(prefix) { - prefix -= 1; - } - let mut suffix = output.len() - END_BYTES; - while !output.is_char_boundary(suffix) { - suffix += 1; - } - format!( - "{}\n[{} bytes omitted]\n{}", - &output[..prefix], - suffix - prefix, - &output[suffix..] - ) -} - #[derive(Clone, Copy)] pub(crate) enum NativeCompilation { Library, @@ -1147,213 +1021,6 @@ pub(crate) fn run_native_compilation( ) } -pub(crate) fn run_process( - tool: &str, - program: &Path, - args: &[String], - cwd: &Path, - extra_env: &BTreeMap, - replacements: &[(&Path, &str)], - failure_code: &'static str, -) -> Result { - run_process_limited( - tool, - program, - args, - cwd, - extra_env, - replacements, - failure_code, - CHILD_TIMEOUT_SECONDS, - CHILD_OUTPUT_LIMIT, - ) -} - -#[allow(clippy::too_many_arguments)] -pub(crate) fn run_process_limited( - tool: &str, - program: &Path, - args: &[String], - cwd: &Path, - extra_env: &BTreeMap, - replacements: &[(&Path, &str)], - failure_code: &'static str, - timeout_seconds: &str, - output_limit: usize, -) -> Result { - run_process_limited_allowed( - tool, - program, - args, - cwd, - extra_env, - replacements, - failure_code, - timeout_seconds, - output_limit, - &[0], - ) -} - -/// Names the stage a build or verification is in, on stderr as -/// `prismpm: stage `, when `PRISMPM_PROGRESS` is set (issue #585). -/// A caller that streams stderr, such as the guided journey, can then say -/// which stage is running. Without the variable it prints nothing, so no -/// output another tool reads changes. -pub(crate) fn progress(stage: &str) { - if std::env::var_os("PRISMPM_PROGRESS").is_some() { - use std::io::Write as _; - let mut stderr = std::io::stderr().lock(); - let _ = writeln!(stderr, "prismpm: stage {stage}"); - let _ = stderr.flush(); - } -} - -#[allow(clippy::too_many_arguments)] -pub(crate) fn run_process_limited_allowed( - tool: &str, - program: &Path, - args: &[String], - cwd: &Path, - extra_env: &BTreeMap, - replacements: &[(&Path, &str)], - failure_code: &'static str, - timeout_seconds: &str, - output_limit: usize, - allowed_exit_codes: &[i32], -) -> Result { - if allowed_exit_codes.is_empty() - || allowed_exit_codes - .iter() - .any(|code| !(0..=125).contains(code)) - { - return Err(PrismError::new( - "PP9001", - "allowed process exit-code set is empty or malformed", - )); - } - progress(tool); - let timeout = executable("timeout")?; - let program_bytes = - std::fs::read(program).or_else(|_| program.canonicalize().and_then(std::fs::read)); - let executable_sha256 = format!( - "{:x}", - Sha256::digest(program_bytes.map_err(|error| { - PrismError::new("PP5008", format!("{}: {error}", program.display())) - })?) - ); - let mut command = Command::new(timeout); - command - .arg("--signal=TERM") - .arg("--kill-after=5") - .arg(timeout_seconds) - .arg(program) - .args(args) - .current_dir(cwd) - .env_clear() - .env("LC_ALL", "C") - .env("LANG", "C") - .stdout(Stdio::piped()) - .stderr(Stdio::piped()); - for key in [ - "PATH", - "ELAN_HOME", - "ELAN_TOOLCHAIN", - "RUSTUP_HOME", - "RUSTUP_TOOLCHAIN", - "CARGO_HOME", - "HOME", - "XDG_CACHE_HOME", - ] { - if let Some(value) = std::env::var_os(key) { - command.env(key, value); - } - } - for (key, value) in extra_env { - command.env(key, value); - } - // This bounds Lake's runtime pool, not Lean CLI children: those require - // the traced -j2 argument in lean_project's generated configuration. - // No build, export, kernel check or deadline is removed or extended. - command.env("LEAN_NUM_THREADS", "2"); - let mut child = command - .spawn() - .map_err(|error| PrismError::new("PP5008", format!("start {tool}: {error}")))?; - let stdout = child - .stdout - .take() - .ok_or_else(|| PrismError::new("PP9001", "child stdout pipe is missing"))?; - let stderr = child - .stderr - .take() - .ok_or_else(|| PrismError::new("PP9001", "child stderr pipe is missing"))?; - let stdout_thread = std::thread::spawn(move || pipe(stdout, output_limit)); - let stderr_thread = std::thread::spawn(move || pipe(stderr, output_limit)); - let status = child - .wait() - .map_err(|error| PrismError::new(failure_code, format!("wait for {tool}: {error}")))?; - let stdout = stdout_thread - .join() - .map_err(|_| PrismError::new("PP9001", "stdout reader terminated"))? - .map_err(|error| PrismError::new(failure_code, format!("read stdout: {error}")))?; - let stderr = stderr_thread - .join() - .map_err(|_| PrismError::new("PP9001", "stderr reader terminated"))? - .map_err(|error| PrismError::new(failure_code, format!("read stderr: {error}")))?; - if stdout.len() > output_limit || stderr.len() > output_limit { - return Err(PrismError::new( - "PP5007", - format!("{tool} output limit exceeded"), - )); - } - let exit_code = status.code().unwrap_or(128); - let stdout = normalized(&String::from_utf8_lossy(&stdout), replacements); - let stderr = normalized(&String::from_utf8_lossy(&stderr), replacements); - if matches!(exit_code, 124 | 137) { - let mut error = PrismError::new("PP5007", format!("{tool} timed out")); - // The observed wrapper exit is evidence, not a claim that exit 137 - // identifies a timeout rather than an external resource kill. - for message in [ - format!("observed process exit code: {exit_code}"), - format!("stdout: {}", failure_excerpt(&stdout)), - format!("stderr: {}", failure_excerpt(&stderr)), - format!( - "normalized child streams: stdout_bytes={}, stdout_sha256={:x}; stderr_bytes={}, stderr_sha256={:x}", - stdout.len(), - Sha256::digest(stdout.as_bytes()), - stderr.len(), - Sha256::digest(stderr.as_bytes()) - ), - ] { - error.notes.push(crate::error::PrismNote { - message, - span: None, - }); - } - return Err(error); - } - if !allowed_exit_codes.contains(&exit_code) { - return Err(PrismError::new( - failure_code, - format!("{tool} exited {exit_code}: stdout={stdout:?}; stderr={stderr:?}"), - )); - } - let stdout = stable_success_output(tool, stdout); - let stderr = stable_success_output(tool, stderr); - Ok(ProcessRecord { - tool: tool.to_owned(), - argv: args - .iter() - .map(|arg| normalized(arg, replacements)) - .collect(), - executable_sha256, - exit_code, - stdout, - stderr, - exporter: None, - }) -} - fn write(path: &Path, bytes: &[u8]) -> Result<(), PrismError> { let parent = path .parent() diff --git a/model/modules.toml b/model/modules.toml index 6f50bdbaa..5a28723a7 100644 --- a/model/modules.toml +++ b/model/modules.toml @@ -629,23 +629,11 @@ module = "errors" path = "sdk/npm/verify/" module = "verify" -[[pending]] -from = "crates/prismpm/src/replay/mod.rs" -to = "crates/prismpm/src/verification.rs" -cut = "Process helpers move to verify" - -[[pending]] -from = "crates/prismpm/src/sdk.rs" -to = "crates/prismpm/src/verification.rs" -cut = "Process helpers move to verify" - # One member per line, so that removing one merges cleanly. [legacy_cycle] members = [ "acceptance", "application_build", - "authority", - "browser_oracle", "controller", "deployment", "exporter", diff --git a/xtask/src/audit.rs b/xtask/src/audit.rs index 240e710e5..2937a92f6 100644 --- a/xtask/src/audit.rs +++ b/xtask/src/audit.rs @@ -1459,7 +1459,7 @@ pub fn audit_tools_ci(root: &Path) -> Result<(), Fail> { .and_then(toml::Value::as_table) .ok_or("tools.lock has no verification-executables row")?; let verification_source = - std::fs::read_to_string(root.join("crates/prismpm/src/verification.rs"))?; + std::fs::read_to_string(root.join("crates/prismpm/src/sdk/process.rs"))?; for name in ["elan_proxy_sha256", "rustup_proxy_sha256", "timeout_sha256"] { let digest = verification .get(name)