use super::{run_capped, Backend, RunSpec, Sandbox, SandboxOutcome};
use crate::error::Result;
pub struct WindowsSandbox;
impl Sandbox for WindowsSandbox {
async fn run(&self, spec: RunSpec<'_>) -> Result<SandboxOutcome> {
let _job = JobLimits::from(spec.limits);
run_capped(Backend::WindowsJobObject, spec, |_cmd| {}).await
}
fn backend(&self) -> Backend {
Backend::WindowsJobObject
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct JobLimits {
pub process_memory: Option<u64>,
pub active_processes: Option<u64>,
pub cpu_ticks: Option<u64>,
pub kill_on_close: bool,
}
impl From<&super::SandboxLimits> for JobLimits {
fn from(l: &super::SandboxLimits) -> Self {
Self {
process_memory: l.max_memory_bytes,
active_processes: l.max_processes,
cpu_ticks: l.max_cpu_secs.map(|s| s.saturating_mul(10_000_000)),
kill_on_close: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sandbox::SandboxLimits;
#[test]
fn maps_limits_to_job_object_fields_and_ticks() {
let lim = SandboxLimits {
max_cpu_secs: Some(3),
max_memory_bytes: Some(64 * 1024 * 1024),
max_processes: Some(8),
..SandboxLimits::default()
};
let job = JobLimits::from(&lim);
assert_eq!(job.process_memory, Some(64 * 1024 * 1024));
assert_eq!(job.active_processes, Some(8));
assert_eq!(job.cpu_ticks, Some(30_000_000)); assert!(job.kill_on_close, "job must kill the tree on close");
}
}