use std::io::Write;
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
use std::path::{Path, PathBuf};
use std::process::ExitCode;
pub(crate) fn run(args: &[String], out: &mut dyn Write, err: &mut dyn Write) -> ExitCode {
let result = command(args).and_then(|value| {
serde_json::to_writer(&mut *out, &value).map_err(|error| error.to_string())?;
writeln!(out).map_err(|error| error.to_string())
});
match result {
Ok(()) => ExitCode::SUCCESS,
Err(error) if error.starts_with("error[SAIL6308]") => {
let _ = writeln!(err, "{error}");
ExitCode::from(2)
}
Err(error) => {
let _ = writeln!(err, "error[SAIL6306]: Kani runtime setup: {error}");
ExitCode::from(2)
}
}
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
pub(crate) fn root() -> Result<PathBuf, String> {
use std::os::unix::fs::{DirBuilderExt as _, PermissionsExt as _};
let cache = sail_verus::cache_dir().map_err(|error| error.to_string())?;
let root = cache.join("kani");
std::fs::DirBuilder::new()
.recursive(true)
.mode(0o700)
.create(&root)
.map_err(|error| error.to_string())?;
let metadata = std::fs::symlink_metadata(&root).map_err(|error| error.to_string())?;
if !metadata.is_dir() || metadata.permissions().mode() & 0o077 != 0 {
return Err("Kani cache must be a private regular directory".into());
}
root.canonicalize().map_err(|error| error.to_string())
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
pub(crate) fn private_attempt(parent: &Path) -> Result<PathBuf, String> {
use std::os::unix::fs::DirBuilderExt as _;
use std::time::{SystemTime, UNIX_EPOCH};
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|error| error.to_string())?
.as_nanos();
let path = parent.join(format!("attempt-{}-{stamp}", std::process::id()));
std::fs::DirBuilder::new()
.mode(0o700)
.create(&path)
.map_err(|error| error.to_string())?;
Ok(path)
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
fn command(args: &[String]) -> Result<serde_json::Value, String> {
let mut args = args.to_vec();
if args.ends_with(&["--output".into(), "json".into()]) {
args.truncate(args.len() - 2);
}
let [action, flag, backend] = args.as_slice() else {
return Err("usage: sail engine install|status --backend kani [--output json]".into());
};
if flag != "--backend" || backend != "kani" || !matches!(action.as_str(), "install" | "status")
{
return Err("usage: sail engine install|status --backend kani [--output json]".into());
}
let root = root()?;
let destination = root.join(format!(
"{}-{}",
sail_kani::tools::RUNTIME_RELEASE,
sail_kani::host::SUFFIX
));
if action == "install" && std::fs::symlink_metadata(&destination).is_err() {
install(&root, &destination)?;
}
let tools = sail_kani::tools::Tools::authenticate(
&destination.join("bundle"),
&destination.join("rust"),
)
.map_err(|error| error.to_string())?;
Ok(
serde_json::json!({"kind":"sail-kani-runtime-status","version":1,"backend":"kani","experimental":true,"runtime_release":sail_kani::tools::RUNTIME_RELEASE,"bundle":tools.bundle(),"rust":tools.rust(),"runtime_authenticated":true,"backend_executed":false,"proof_authority":false}),
)
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
fn require_python() -> Result<(), String> {
let checked = std::process::Command::new("python3")
.args([
"-I",
"-c",
"import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)",
])
.stdin(std::process::Stdio::null())
.output();
match checked {
Ok(output) if output.status.success() => Ok(()),
_ => Err(format!(
"error[SAIL6308]: Kani runtime setup needs Python 3.11 or newer as `python3`{}",
if cfg!(target_os = "macos") {
"; the Command Line Tools ship 3.9, so install one from python.org or Homebrew"
} else {
""
}
)),
}
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
fn install(root: &Path, destination: &Path) -> Result<(), String> {
require_python()?;
let pins = sail_kani::host::pins().ok_or_else(|| {
format!(
"no pinned Kani runtime for this host ({})",
sail_kani::host::CURRENT.map_or("unsupported", |host| host.triple)
)
})?;
let name = sail_kani::host::PIN_NAME;
let bundle_pin = format!("engine/kani/fork-bundle-{name}.json");
let rust_pin = format!("engine/kani/toolchain-{name}.json");
let preparation = private_attempt(root)?;
let result = (|| {
let resources: [(&str, &[u8]); 6] = [
(
"ci/kani-fetch.py",
include_bytes!("../resources/ci/kani-fetch.py"),
),
(
"ci/kani-install.py",
include_bytes!("../resources/ci/kani-install.py"),
),
(
"ci/kani-bundle.py",
include_bytes!("../resources/ci/kani-bundle.py"),
),
(
"ci/kani-toolchain.py",
include_bytes!("../resources/ci/kani-toolchain.py"),
),
(&bundle_pin, pins.fork_bundle.as_bytes()),
(&rust_pin, pins.toolchain.as_bytes()),
];
for (name, bytes) in resources {
let path = preparation.join(name);
std::fs::create_dir_all(path.parent().ok_or("missing preparation parent")?)
.map_err(|error| error.to_string())?;
std::fs::write(path, bytes).map_err(|error| error.to_string())?;
}
let output = std::process::Command::new("python3")
.arg("-I")
.arg(preparation.join("ci/kani-fetch.py"))
.arg("--bundle-pin")
.arg(preparation.join(&bundle_pin))
.arg("--rust-pin")
.arg(preparation.join(&rust_pin))
.arg("--destination")
.arg(destination)
.output()
.map_err(|error| {
format!("original runtime installation requires Python 3.11+: {error}")
})?;
if !output.status.success() {
return Err(String::from_utf8_lossy(&output.stderr).trim().to_owned());
}
Ok(())
})();
let _ = std::fs::remove_dir_all(preparation);
result
}
#[cfg(not(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
)))]
fn command(_args: &[String]) -> Result<serde_json::Value, String> {
Err("the pinned experimental Kani runtime supports native Linux x86-64 and macOS arm64".into())
}