diff --git a/crates/openhuman-core/src/sandbox/plan.rs b/crates/openhuman-core/src/sandbox/plan.rs index 31b9bd3364c..4129f1f04ec 100644 --- a/crates/openhuman-core/src/sandbox/plan.rs +++ b/crates/openhuman-core/src/sandbox/plan.rs @@ -250,33 +250,40 @@ fn validate_resources( executor: PlannedExecutor, ) -> Result<(), PlanMappingError> { let limits = &artifact.limits; + if limits.cpu_millicores == Some(0) + || limits.memory_bytes == Some(0) + || limits.pids_max == Some(0) + { + return Err(PlanMappingError::ZeroLimit); + } if limits.cpu_time_ms.is_some() { return Err(PlanMappingError::CpuTimeUnenforceable); } - if limits.cpu_millicores.is_some() { - return Err(if executor == PlannedExecutor::Host { - PlanMappingError::HostResourceLimitUnenforceable - } else { - PlanMappingError::CpuLimitUnenforceable - }); - } if limits.disk_bytes.is_some() { return Err(PlanMappingError::DiskLimitUnenforceable); } - if limits.cpu_millicores == Some(0) - || limits.memory_bytes == Some(0) - || limits.pids_max == Some(0) + if executor == PlannedExecutor::Docker { + // A container enforces CPU and memory bounds: `PlannedSpawn::apply` + // writes them into the Docker overrides, and TinyBox's RunDocker passes + // them as `--cpus` and `-m`. That request has no PID limit. + return if limits.pids_max.is_some() { + Err(PlanMappingError::PidsLimitUnenforceable) + } else { + Ok(()) + }; + } + if executor == PlannedExecutor::Host + && (limits.cpu_millicores.is_some() + || limits.memory_bytes.is_some() + || limits.pids_max.is_some()) { - return Err(PlanMappingError::ZeroLimit); + return Err(PlanMappingError::HostResourceLimitUnenforceable); + } + if limits.cpu_millicores.is_some() { + return Err(PlanMappingError::CpuLimitUnenforceable); } if limits.memory_bytes.is_some() || limits.pids_max.is_some() { - return Err(match executor { - PlannedExecutor::Host => PlanMappingError::HostResourceLimitUnenforceable, - PlannedExecutor::Docker if limits.pids_max.is_some() => { - PlanMappingError::PidsLimitUnenforceable - } - _ => PlanMappingError::NativeResourceLimitsUnenforceable, - }); + return Err(PlanMappingError::NativeResourceLimitsUnenforceable); } Ok(()) } diff --git a/crates/openhuman-core/src/sandbox/plan_tests.rs b/crates/openhuman-core/src/sandbox/plan_tests.rs index 54e71133af7..f01fa373281 100644 --- a/crates/openhuman-core/src/sandbox/plan_tests.rs +++ b/crates/openhuman-core/src/sandbox/plan_tests.rs @@ -65,22 +65,55 @@ fn an_unbounded_plan_does_not_synthesize_native_resource_limits() { } #[test] -fn unsupported_planned_bounds_are_refused_instead_of_being_ignored() { - let mut planned = artifact(SandboxBackend::Docker); - planned.limits = ResourceLimits2 { - cpu_time_ms: None, - cpu_millicores: Some(500), - memory_bytes: Some(64 << 20), - pids_max: None, - disk_bytes: None, - }; - planned.constraints.push(SandboxConstraint2::Subprocesses); +fn docker_cpu_and_memory_bounds_are_accepted() { + // TS-E2E-002: PlannedSpawn::apply carries both into the Docker overrides and + // RunDocker enforces them, so refusing them refused every bounded container. + for limits in [ + ResourceLimits2 { + cpu_millicores: Some(1000), + memory_bytes: Some(512 << 20), + ..ResourceLimits2::default() + }, + ResourceLimits2 { + memory_bytes: Some(512 << 20), + ..ResourceLimits2::default() + }, + ResourceLimits2 { + cpu_millicores: Some(500), + ..ResourceLimits2::default() + }, + ] { + let mut planned = artifact(SandboxBackend::Docker); + planned.limits = limits; + planned.constraints.push(SandboxConstraint2::Subprocesses); + assert!( + BoxPlan::from_artifact(&planned, None).is_ok(), + "a Docker plan with {:?} must map", + planned.limits + ); + } +} + +#[test] +fn native_cpu_bounds_are_refused_instead_of_being_ignored() { + let mut planned = artifact(SandboxBackend::Jail); + planned.limits.cpu_millicores = Some(500); assert_eq!( BoxPlan::from_artifact(&planned, None), Err(PlanMappingError::CpuLimitUnenforceable) ); } +#[test] +fn a_zero_docker_bound_is_refused() { + let mut planned = artifact(SandboxBackend::Docker); + planned.limits.cpu_millicores = Some(0); + assert_eq!( + BoxPlan::from_artifact(&planned, None), + Err(PlanMappingError::ZeroLimit) + ); +} + #[test] fn native_memory_limits_are_refused_instead_of_being_dropped() { let mut planned = artifact(SandboxBackend::Jail); diff --git a/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs b/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs index 04b86b2fd99..fa49456e3cb 100644 --- a/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs +++ b/crates/openhuman-core/src/sandbox/spawn_plan/mod.rs @@ -164,12 +164,14 @@ fn limits(policy: &SandboxPolicy) -> ResourceLimits2 { }; ResourceLimits2 { cpu_time_ms: None, - // Fractional CPUs become thousandths of a core, rounded up so a bound is - // never silently loosened; absent bounds stay absent. + // Fractional CPUs become thousandths of a core, rounded down: a CPU bound + // is a ceiling, so rounding up would let the container use more than the + // operator allowed. Below one millicore this is 0, which the plan refuses + // (`ZeroLimit`) rather than widening. Absent bounds stay absent. cpu_millicores: overrides .cpu_limit .filter(|cpus| cpus.is_finite() && *cpus > 0.0) - .map(|cpus| (cpus * 1000.0).ceil().min(f64::from(u32::MAX)) as u32), + .map(|cpus| (cpus * 1000.0).floor().min(f64::from(u32::MAX)) as u32), memory_bytes: overrides .memory_limit_mb .filter(|mb| *mb > 0) diff --git a/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs b/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs index 22061175c55..6c03cb2eef5 100644 --- a/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs +++ b/crates/openhuman-core/src/sandbox/spawn_plan/mod_tests.rs @@ -37,10 +37,16 @@ fn bounds_come_from_the_docker_overrides_and_never_loosen() { assert_eq!(bounded.cpu_millicores, Some(1500)); assert_eq!(bounded.memory_bytes, Some(512 * 1024 * 1024)); assert_eq!(bounded.cpu_time_ms, None); - // A fraction rounds up to the next millicore rather than down to a looser bound. + // A fraction rounds down: a CPU bound is a ceiling, so rounding up would + // widen it (0.1004 cores must not run as 0.101). + assert_eq!( + limits(&with_overrides(Some(0.1004), None)).cpu_millicores, + Some(100) + ); + // Below one millicore the bound is 0, which the plan refuses, not widens. assert_eq!( limits(&with_overrides(Some(0.0004), None)).cpu_millicores, - Some(1) + Some(0) ); // Absent, zero and non-finite bounds are not bounds. for cpu in [