Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkgs/id/crates/id-core/src/world_limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,19 @@ impl WorldExecutionProfile {
///
/// A requested bound of zero or a value that cannot be represented on
/// this host.
pub fn resolve(self, hard: RuntimeLimits) -> Result<RuntimeLimits> {
/// Validate the requested bounds on their own: every bound that is set
/// must be positive and at or above its floor. inherits the
/// server ceiling and is always valid.
///
/// This is the single source of truth for the bound checks: []
/// runs it against the live profile at apply time, and the world
/// manifest validator runs it at parse time, so a manifest can never
/// accept bounds that apply would later refuse (#3015).
///
/// # Errors
///
/// Names the first offending bound.
pub fn validate(&self) -> Result<()> {
ensure!(self.wasm_fuel != Some(0), "world Wasm fuel must be nonzero");
ensure!(
self.wasm_memory_bytes.is_none_or(|bytes| bytes >= 64 * KIB),
Expand All @@ -164,6 +176,11 @@ impl WorldExecutionProfile {
self.native_call_deadline_ms != Some(0),
"world native call deadline must be nonzero"
);
Ok(())
}

pub fn resolve(self, hard: RuntimeLimits) -> Result<RuntimeLimits> {
self.validate()?;

#[cfg(feature = "sandbox")]
let sandbox = {
Expand Down
67 changes: 65 additions & 2 deletions pkgs/id/src/world_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use std::path::PathBuf;
use anyhow::{Result, ensure};
use serde::{Deserialize, Serialize};

use crate::world_caps::{MAX_NAME_BYTES, validate_ceiling_names, validate_grant_names};
use crate::world_caps::{
MAX_NAME_BYTES, MAX_PENDING_SCHEDULES, validate_ceiling_names, validate_grant_names,
};
use crate::world_limits::WorldExecutionProfile;

/// A declarative description of a world. Fields the manifest omits are left
Expand Down Expand Up @@ -204,7 +206,9 @@ fn reject_unknown_profile_keys(text: &str) -> Result<()> {

impl WorldManifest {
/// Validate the manifest: grants and ceiling name only known capabilities,
/// schedule ids are bounded printable ASCII, and fire times are positive.
/// the requested profile bounds satisfy the execution-profile floors, the
/// schedule count is within the pending-schedule cap, schedule ids are
/// bounded printable ASCII, and fire times are positive.
///
/// # Errors
///
Expand All @@ -216,7 +220,17 @@ impl WorldManifest {
if let Some(ceiling) = &self.ceiling {
validate_ceiling_names(ceiling)?;
}
// The requested profile bounds are refused here, at parse time,
// with the same checks resolve() runs at apply time — an invalid
// manifest must never parse and plan clean, then fail partway
// through a live apply (#3015).
self.profile.validate()?;
if let Some(schedules) = &self.schedules {
ensure!(
schedules.len() <= MAX_PENDING_SCHEDULES,
"too many schedules: {} exceeds the maximum of {MAX_PENDING_SCHEDULES} pending schedules",
schedules.len()
);
for (id, at_ms) in schedules {
crate::world_caps::validate_schedule_id(id)?;
ensure!(*at_ms > 0, "schedule {id:?} must fire in the future");
Expand Down Expand Up @@ -537,6 +551,55 @@ wasm_fuel = 5_000_000
assert!(format!("{err}").contains("wasm_fuell"), "{err}");
}

#[test]
fn test_invalid_profile_bounds_are_rejected() {
// #3015: each bound resolve() refuses at apply time must already
// fail parse(), with the same checks.
for text in [
"[profile]
wasm_fuel = 0",
"[profile]
wasm_memory_bytes = 1024",
"[profile]
wasm_message_bytes = 0",
"[profile]
native_memory_bytes = 1024",
"[profile]
native_cpu_seconds = 0",
"[profile]
native_call_deadline_ms = 0",
] {
assert!(parse(text).is_err(), "parsed clean: {text}");
}
}

#[test]
fn test_minimum_profile_bounds_are_accepted() {
// #3015: the boundary-valid values (one Wasm page, 16 MiB native)
// sit exactly on the floors and must parse.
let manifest = parse("[profile]
wasm_memory_bytes = 65536
native_memory_bytes = 16777216")
.unwrap();
assert_eq!(manifest.profile.wasm_memory_bytes, Some(65_536));
assert_eq!(manifest.profile.native_memory_bytes, Some(16_777_216));
}

#[test]
fn test_schedule_count_is_capped_at_parse() {
// #3015: a manifest may declare at most MAX_PENDING_SCHEDULES
// pending schedules; one more must fail parse, not a live apply.
let build = |count: usize| {
let entries: Vec<String> = (0..count)
.map(|i| format!("\"s{i}\" = 1800000000000"))
.collect();
format!("schedules = {{ {} }}", entries.join(", "))
};
assert!(parse(&build(MAX_PENDING_SCHEDULES)).is_ok());
let err = parse(&build(MAX_PENDING_SCHEDULES + 1)).unwrap_err();
assert!(format!("{err}").contains("too many schedules"), "{err}");
}

#[test]
fn full_width_u64_values_still_parse() {
// Regression (Codex review of the fix for this issue): the
Expand Down
Loading