use std::path::PathBuf;
use std::process::ExitCode;
use aa_sandbox::policy::{SandboxConfig, SandboxLimits};
use aa_sandbox::runtime::SandboxRuntime;
#[derive(Debug, clap::Args)]
pub struct SandboxArgs {
#[command(subcommand)]
pub subcommand: SandboxSubcommand,
}
#[derive(Debug, clap::Subcommand)]
pub enum SandboxSubcommand {
Run(RunArgs),
Info,
}
#[derive(Debug, clap::Args)]
pub struct RunArgs {
pub wasm: PathBuf,
#[arg(long)]
pub fuel: Option<u64>,
#[arg(long)]
pub memory_pages: Option<u32>,
#[arg(long)]
pub wall_clock_ms: Option<u64>,
}
pub fn dispatch(args: SandboxArgs) -> ExitCode {
match args.subcommand {
SandboxSubcommand::Run(run) => run_wasm(run),
SandboxSubcommand::Info => print_info(),
}
}
fn run_wasm(args: RunArgs) -> ExitCode {
let bytes = match std::fs::read(&args.wasm) {
Ok(b) => b,
Err(e) => {
eprintln!("error: failed to read {}: {e}", args.wasm.display());
return ExitCode::FAILURE;
}
};
let limits = SandboxLimits {
fuel: args.fuel.unwrap_or(SandboxLimits::default().fuel),
memory_pages: args.memory_pages.unwrap_or(SandboxLimits::default().memory_pages),
wall_clock_ms: args.wall_clock_ms.unwrap_or(SandboxLimits::default().wall_clock_ms),
};
let config = SandboxConfig {
preopened_dirs: Vec::new(),
limits,
};
let runtime = match SandboxRuntime::new(config) {
Ok(r) => r,
Err(e) => {
eprintln!("error: failed to build sandbox runtime: {e}");
return ExitCode::FAILURE;
}
};
match runtime.run_tool(&bytes, &[]) {
Ok(output) => {
println!("sandbox exited cleanly (exit_code={})", output.exit_code);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("sandbox refused or trapped the module: {e}");
ExitCode::FAILURE
}
}
}
fn print_info() -> ExitCode {
let limits = SandboxLimits::default();
println!("aasm sandbox — WASI preview 1 tool-execution sandbox");
println!(" fuel (instructions): {}", limits.fuel);
println!(
" memory ceiling: {} pages ({} KiB)",
limits.memory_pages,
(limits.memory_pages as usize) * 64
);
println!(" wall-clock deadline (ms): {}", limits.wall_clock_ms);
println!(" preopened dirs: (none — fully sealed FS)");
ExitCode::SUCCESS
}