//! `VTC1` write-once NVMe cache format — pure metadata contracts shared by //! `ld` (atomic writer) and `cc` (`O_RDONLY` reader). //! //! Eliminates audit-05 F-05 (pre-reserve OOM on corrupt count) by construction: //! the record count is validated against the blob size BEFORE any allocation. pub const CACHE_MAGIC: [u8; 8] = *b"VTCACHE1"; pub const CACHE_VERSION: u32 = 1; pub const CACHE_ALIGN: u64 = 4096; pub const HEADER_SIZE: usize = 48; pub const MIN_REC_BYTES: usize = 1 + 4 * 4 + 4 * 8 + 4; // 53 B minimum per serialized record #[derive(Debug, Clone, PartialEq, Eq)] pub struct CacheRecord { pub kind: u8, pub i0: i32, pub i1: i32, pub i2: i32, pub flags: u32, pub u0: u64, pub u1: u64, pub data_offset: u64, pub data_bytes: u64, pub name: String, } pub fn fnv1a_u64(bytes: &[u8], seed: u64) -> u64 { let mut h = seed; for &b in bytes { h ^= b as u64; h = h.wrapping_mul(0x0000_0100_0000_01B3); } h } pub fn align_up(v: u64, align: u64) -> Result { let rem = v % align; if rem == 0 { Ok(v) } else { v.checked_add(align - rem).ok_or_else(|| "align_up overflow".into()) } } pub fn serialize_meta(recs: &[CacheRecord]) -> Vec { let mut out = Vec::new(); for r in recs { out.push(r.kind); out.extend_from_slice(&r.i0.to_le_bytes()); out.extend_from_slice(&r.i1.to_le_bytes()); out.extend_from_slice(&r.i2.to_le_bytes()); out.extend_from_slice(&r.flags.to_le_bytes()); out.extend_from_slice(&r.u0.to_le_bytes()); out.extend_from_slice(&r.u1.to_le_bytes()); out.extend_from_slice(&r.data_offset.to_le_bytes()); out.extend_from_slice(&r.data_bytes.to_le_bytes()); let nlen = r.name.len() as u32; out.extend_from_slice(&nlen.to_le_bytes()); out.extend_from_slice(r.name.as_bytes()); } out } pub fn deserialize_meta(blob: &[u8], count: u32) -> Result, String> { // Anti-OOM check BEFORE `Vec::with_capacity(count)` (audit-05 F-05) if (count as usize) > blob.len() / MIN_REC_BYTES { return Err(format!( "corrupt cache metadata: count={count} exceeds blob capacity ({} B)", blob.len() )); } let mut out = Vec::with_capacity(count as usize); let mut pos = 0usize; let take = |pos: &mut usize, n: usize| -> Result<&[u8], String> { if n > blob.len().saturating_sub(*pos) { return Err("truncated cache metadata".into()); } let s = &blob[*pos..*pos + n]; *pos += n; Ok(s) }; for _ in 0..count { let kind = take(&mut pos, 1)?[0]; let i0 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()); let i1 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()); let i2 = i32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()); let flags = u32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()); let u0 = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap()); let u1 = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap()); let data_offset = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap()); let data_bytes = u64::from_le_bytes(take(&mut pos, 8)?.try_into().unwrap()); let nlen = u32::from_le_bytes(take(&mut pos, 4)?.try_into().unwrap()) as usize; if nlen > 4096 { return Err("cache record name too long".into()); } let nbytes = take(&mut pos, nlen)?; let name = std::str::from_utf8(nbytes) .map_err(|_| "non-UTF-8 cache record name")? .to_string(); out.push(CacheRecord { kind, i0, i1, i2, flags, u0, u1, data_offset, data_bytes, name, }); } if pos != blob.len() { return Err("trailing bytes in cache metadata".into()); } Ok(out) } #[cfg(test)] mod tests { use super::*; #[test] fn anti_oom_before_reserve() { // Absurd entry count rejected BEFORE Vec::with_capacity assert!(deserialize_meta(&[0u8; 64], u32::MAX).is_err()); } #[test] fn meta_roundtrip_and_alignment() { let rec = CacheRecord { kind: 1, i0: 128, i1: 256, i2: 0, flags: 0, u0: 4096, u1: 0, data_offset: 4096, data_bytes: 8192, name: "layer.0.q8".into(), }; let blob = serialize_meta(&[rec.clone()]); let back = deserialize_meta(&blob, 1).unwrap(); assert_eq!(back, vec![rec]); assert_eq!(align_up(4097, CACHE_ALIGN).unwrap(), 8192); assert_eq!(align_up(4096, CACHE_ALIGN).unwrap(), 4096); assert!(align_up(u64::MAX, CACHE_ALIGN).is_err()); } }