Mods: close three storage gaps Codex's review found
- Reject a second running mod with an id already in use: two mods sharing an id would also share capability storage's data_dir, letting one read, overwrite or delete the other's state. - Cap storage to 64 keys per mod, not just 4 MiB: zero- or near-zero- byte values could otherwise flood the directory with files the byte quota never sees. - Add a 1 MiB per-call write budget, separate from the 4 MiB on-disk quota: a loop replacing one key stays under the quota on every check (the total on disk never grows) while still doing unbounded synchronous disk I/O per call, which fuel never meters. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,6 +40,15 @@ const HUD_LINES: usize = 16;
|
||||
const LINE_CHARS: usize = 256;
|
||||
/// Total bytes a mod's `storage` may hold on disk, across all its keys.
|
||||
const STORAGE_QUOTA: u64 = 4 << 20;
|
||||
/// Distinct keys a mod's `storage` may hold: `STORAGE_QUOTA` alone doesn't
|
||||
/// bound a flood of zero- or near-zero-byte files (each still a file name,
|
||||
/// a directory entry and an inode).
|
||||
const STORAGE_MAX_KEYS: usize = 64;
|
||||
/// Bytes `storage::set` may write across one hook call: fuel bounds a
|
||||
/// mod's CPU time per call, not the time a synchronous disk write takes, so
|
||||
/// a cheap-in-fuel loop replacing one key with large payloads needs its own
|
||||
/// cap, separate from the on-disk quota it would otherwise stay under.
|
||||
const STORAGE_WRITE_BUDGET: u64 = 1 << 20;
|
||||
/// A mod's data, beside its `horizon-mods` folder (`find`'s `path.parent()`
|
||||
/// never has a manifest there, so `find` never mistakes it for a mod).
|
||||
const STORAGE_DIR: &str = ".horizon-mod-data";
|
||||
@@ -104,6 +113,11 @@ struct State {
|
||||
panel: Option<Panel>,
|
||||
/// Capability `storage`'s folder; made on first `set`.
|
||||
data_dir: PathBuf,
|
||||
/// Bytes `storage::set` may still write before this hook call ends
|
||||
/// (reset per call in [`Mods::call`]): fuel bounds a mod's CPU time but
|
||||
/// not disk I/O time, so a loop of small-fuel, large-payload `set`s
|
||||
/// needs its own budget.
|
||||
storage_budget: u64,
|
||||
limits: StoreLimits,
|
||||
}
|
||||
|
||||
@@ -212,6 +226,15 @@ impl Mods {
|
||||
if m.side == Side::Server {
|
||||
return Err("server mods do not run yet".into());
|
||||
}
|
||||
// Two running mods with the same id would share one `data_dir`
|
||||
// (capability `storage`): the first loaded keeps the id.
|
||||
if self
|
||||
.entries
|
||||
.iter()
|
||||
.any(|e| e.run.is_some() && e.manifest.as_ref().is_some_and(|other| other.id == m.id))
|
||||
{
|
||||
return Err(format!("another running mod already uses id {}", m.id));
|
||||
}
|
||||
let taken: Vec<String> = self
|
||||
.entries
|
||||
.iter()
|
||||
@@ -240,6 +263,7 @@ impl Mods {
|
||||
out: Vec::new(),
|
||||
panel: None,
|
||||
data_dir,
|
||||
storage_budget: STORAGE_WRITE_BUDGET,
|
||||
limits: StoreLimitsBuilder::new()
|
||||
.memory_size(MEMORY)
|
||||
.instances(16)
|
||||
@@ -309,6 +333,7 @@ impl Mods {
|
||||
let entry = &mut self.entries[i];
|
||||
let run = entry.run.as_mut()?;
|
||||
run.store.data_mut().snapshot = self.snapshot.clone();
|
||||
run.store.data_mut().storage_budget = STORAGE_WRITE_BUDGET;
|
||||
let started = Instant::now();
|
||||
let result = run
|
||||
.store
|
||||
@@ -532,23 +557,35 @@ impl storage::Host for State {
|
||||
if !storage_key_ok(&key) {
|
||||
return Err(format!("{key:?} is not [a-z0-9_.-]{{1,64}}"));
|
||||
}
|
||||
let len = value.len() as u64;
|
||||
if len > self.storage_budget {
|
||||
return Err("too much storage traffic in one call".into());
|
||||
}
|
||||
// Everything already on disk but the file this call replaces.
|
||||
let used: u64 = std::fs::read_dir(&self.data_dir)
|
||||
let existing: Vec<std::fs::DirEntry> = std::fs::read_dir(&self.data_dir)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.filter(|e| e.file_name().to_string_lossy() != key)
|
||||
.collect();
|
||||
if existing.len() >= STORAGE_MAX_KEYS {
|
||||
return Err(format!("would hold over {STORAGE_MAX_KEYS} keys"));
|
||||
}
|
||||
let used: u64 = existing
|
||||
.iter()
|
||||
.filter_map(|e| e.metadata().ok())
|
||||
.map(|m| m.len())
|
||||
.sum();
|
||||
if used + value.len() as u64 > STORAGE_QUOTA {
|
||||
if used + len > STORAGE_QUOTA {
|
||||
return Err(format!(
|
||||
"would use over {} MiB of storage",
|
||||
STORAGE_QUOTA >> 20
|
||||
));
|
||||
}
|
||||
std::fs::create_dir_all(&self.data_dir).map_err(|e| e.to_string())?;
|
||||
std::fs::write(self.data_dir.join(&key), value).map_err(|e| e.to_string())
|
||||
std::fs::write(self.data_dir.join(&key), value).map_err(|e| e.to_string())?;
|
||||
self.storage_budget -= len;
|
||||
Ok(())
|
||||
}
|
||||
fn delete(&mut self, key: String) {
|
||||
if storage_key_ok(&key) {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! A mod for the runtime's tests: `/spin` loops forever, `/panic` panics,
|
||||
//! `/grow` asks for a gigabyte, `/hello` chats, `/save` and `/load` exercise
|
||||
//! capability `storage`; `tick` and `frame` do nothing, to measure the cost
|
||||
//! of a call.
|
||||
//! capability `storage` on a fixed key, `/save_key <key> <text>` exercises
|
||||
//! it on an arbitrary key (for the quota and key-count limits); `tick` and
|
||||
//! `frame` do nothing, to measure the cost of a call.
|
||||
use horizon_mod_sdk::{Mod, Registration, chat, storage};
|
||||
|
||||
struct Guest {
|
||||
@@ -10,7 +11,7 @@ struct Guest {
|
||||
|
||||
impl Mod for Guest {
|
||||
fn init() -> Result<(Self, Registration), String> {
|
||||
let hooks = ["spin", "panic", "grow", "hello", "save", "load"]
|
||||
let hooks = ["spin", "panic", "grow", "hello", "save", "load", "save_key"]
|
||||
.into_iter()
|
||||
.fold(Registration::new(), Registration::command)
|
||||
.on_tick()
|
||||
@@ -44,6 +45,13 @@ impl Mod for Guest {
|
||||
chat::show(text.as_deref().unwrap_or("(empty)"));
|
||||
Ok(None)
|
||||
}
|
||||
"save_key" => {
|
||||
let (key, text) = args
|
||||
.split_once(' ')
|
||||
.ok_or("Usage: /save_key <key> <text>")?;
|
||||
storage::set(key, text.as_bytes())?;
|
||||
Ok(Some("saved".into()))
|
||||
}
|
||||
_ => {
|
||||
chat::show(&format!("hello {args} after {} ticks", self.ticks));
|
||||
Ok(None)
|
||||
|
||||
@@ -240,6 +240,25 @@ fn storage_persists_across_reload() {
|
||||
assert_eq!(mods.drain(), [chat]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_write_budget_limits_one_call() {
|
||||
let dir = mods_dir().join("storage_budget");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("guest.hmod");
|
||||
std::fs::copy(mods_dir().join("horizon_mods_test_guest.hmod"), &path).unwrap();
|
||||
let mut mods = Mods::load(std::slice::from_ref(&path), |_| true, |_| false);
|
||||
// Under the 4 MiB on-disk quota, but over the 1 MiB a single call may
|
||||
// write: a loop replacing one key can never bypass the quota by
|
||||
// writing a lot in one call, only across many.
|
||||
let over_budget = "a".repeat((1 << 20) + 1024);
|
||||
assert!(mods.command(&format!("save {over_budget}")));
|
||||
let refused = Output::Feedback {
|
||||
text: "too much storage traffic in one call".into(),
|
||||
error: true,
|
||||
};
|
||||
assert_eq!(mods.drain(), [refused]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_quota_is_enforced() {
|
||||
let dir = mods_dir().join("storage_quota");
|
||||
@@ -247,8 +266,20 @@ fn storage_quota_is_enforced() {
|
||||
let path = dir.join("guest.hmod");
|
||||
std::fs::copy(mods_dir().join("horizon_mods_test_guest.hmod"), &path).unwrap();
|
||||
let mut mods = Mods::load(std::slice::from_ref(&path), |_| true, |_| false);
|
||||
let over_quota = "a".repeat((4 << 20) + 1024);
|
||||
assert!(mods.command(&format!("save {over_quota}")));
|
||||
// Each call is under the per-call write budget; distinct keys so the
|
||||
// quota (not the fixed "note" key being overwritten) is what's tested.
|
||||
let chunk = "a".repeat(900 << 10);
|
||||
for i in 0..4 {
|
||||
assert!(mods.command(&format!("save_key k{i} {chunk}")));
|
||||
assert_eq!(
|
||||
mods.drain(),
|
||||
[Output::Feedback {
|
||||
text: "saved".into(),
|
||||
error: false,
|
||||
}]
|
||||
);
|
||||
}
|
||||
assert!(mods.command(&format!("save_key k4 {chunk}")));
|
||||
let refused = Output::Feedback {
|
||||
text: "would use over 4 MiB of storage".into(),
|
||||
error: true,
|
||||
@@ -256,6 +287,49 @@ fn storage_quota_is_enforced() {
|
||||
assert_eq!(mods.drain(), [refused]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_key_count_is_bounded() {
|
||||
let dir = mods_dir().join("storage_keys");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("guest.hmod");
|
||||
std::fs::copy(mods_dir().join("horizon_mods_test_guest.hmod"), &path).unwrap();
|
||||
let mut mods = Mods::load(std::slice::from_ref(&path), |_| true, |_| false);
|
||||
for i in 0..64 {
|
||||
assert!(mods.command(&format!("save_key k{i} x")));
|
||||
assert_eq!(
|
||||
mods.drain(),
|
||||
[Output::Feedback {
|
||||
text: "saved".into(),
|
||||
error: false,
|
||||
}],
|
||||
"key {i}"
|
||||
);
|
||||
}
|
||||
assert!(mods.command("save_key k64 x"));
|
||||
let refused = Output::Feedback {
|
||||
text: "would hold over 64 keys".into(),
|
||||
error: true,
|
||||
};
|
||||
assert_eq!(mods.drain(), [refused]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_mod_ids_are_refused() {
|
||||
let dir = mods_dir().join("duplicate_ids");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let first = dir.join("first.hmod");
|
||||
let second = dir.join("second.hmod");
|
||||
std::fs::copy(mods_dir().join("horizon_mods_test_guest.hmod"), &first).unwrap();
|
||||
std::fs::copy(mods_dir().join("horizon_mods_test_guest.hmod"), &second).unwrap();
|
||||
// Both declare id "test_guest" (the guest's own manifest): sharing a
|
||||
// `data_dir` would let the second read, overwrite or delete the
|
||||
// first's storage, so only the first-loaded one may start.
|
||||
let mods = Mods::load(&[first, second], |_| true, |_| false);
|
||||
assert_eq!(mods.entries()[0].status, Status::Running);
|
||||
let refused = Status::Failed("another running mod already uses id test_guest".into());
|
||||
assert_eq!(mods.entries()[1].status, refused);
|
||||
}
|
||||
|
||||
/// `cargo test -p horizon-mods --release -- --ignored --nocapture`: the
|
||||
/// cost of the per-tick and per-frame hooks (docs/mods.md, Performance).
|
||||
#[test]
|
||||
|
||||
+5
-1
@@ -82,7 +82,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3.
|
||||
| `game` | `game-read` | `get-player()`: x y z, yaw, pitch, health, food, dimension; `get-clock()`: game time, day time |
|
||||
| `chat` | `chat` | `show(text)`: a line only the local player sees, after the mod's name (8 per call) |
|
||||
| `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters |
|
||||
| `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of files, 4 MiB total |
|
||||
| `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call |
|
||||
|
||||
| Hook (export) | When |
|
||||
|---|---|
|
||||
@@ -192,6 +192,10 @@ allocation, next to a healthy mod).
|
||||
(`mod.<id>:true`).
|
||||
3. A mod that failed to load or stopped shows why, in red, on its row.
|
||||
|
||||
Two turned-on mods never share an `id` while running (it is also capability
|
||||
`storage`'s directory): the one `find` lists first starts, the other stays
|
||||
off with the reason on its row.
|
||||
|
||||
Benchmarks (`--benchmark`) never load mods.
|
||||
|
||||
## Making a mod
|
||||
|
||||
Reference in New Issue
Block a user