Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-kernel/tests/store_v1_negative.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-kernel / tests / store_v1_negative.rs` 473 lines ยท 14458 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use aienos_kernel::store::v1::{ 17 crc32c, Catalog, CatalogEntry, CommitRecord, FormatError, ObjectId, Superblock, 18 COMMIT_RECORD_BYTES, MAX_CATALOG_ENTRIES, STORE_UNIT_BYTES, 19 }; 20 21 fn oid(kind: u16, data: &[u8]) -> ObjectId { 22 ObjectId::calculate(kind, 1, data).unwrap() 23 } 24 25 fn entry(first_unit: u64, data: &[u8]) -> CatalogEntry { 26 CatalogEntry { 27 object_id: oid(3, data), 28 kind: 3, 29 version: 1, 30 first_unit, 31 byte_length: data.len() as u64, 32 unit_count: 1, 33 flags: 0, 34 } 35 } 36 37 fn empty_catalog() -> (Catalog, Vec, ObjectId) { 38 let catalog = Catalog { entries: vec![] }; 39 let bytes = catalog.encode().unwrap(); 40 let id = oid(1, &bytes); 41 (catalog, bytes, id) 42 } 43 44 fn genesis() -> CommitRecord { 45 let (_, _, catalog_id) = empty_catalog(); 46 CommitRecord { 47 store_uuid: [0x11; 16], 48 region_units: 8, 49 generation: 1, 50 previous_generation: 0, 51 previous_commit_id: ObjectId([0; 32]), 52 previous_catalog_id: ObjectId([0; 32]), 53 catalog_id, 54 catalog_first_unit: 2, 55 catalog_byte_length: 16, 56 catalog_unit_count: 1, 57 catalog_entry_count: 0, 58 committed_high_water: 4, 59 } 60 } 61 62 fn superblock(slot_id: u32, commit: &CommitRecord) -> Superblock { 63 Superblock { 64 store_uuid: commit.store_uuid, 65 slot_id, 66 region_units: commit.region_units, 67 generation: commit.generation, 68 commit_record_id: commit.object_id().unwrap(), 69 commit_record_unit: 3, 70 catalog_id: commit.catalog_id, 71 catalog_first_unit: commit.catalog_first_unit, 72 catalog_byte_length: commit.catalog_byte_length, 73 catalog_unit_count: commit.catalog_unit_count, 74 catalog_entry_count: commit.catalog_entry_count, 75 committed_high_water: commit.committed_high_water, 76 } 77 } 78 79 fn rechecksum_superblock(bytes: &mut [u8]) { 80 bytes[168..172].fill(0); 81 let crc = crc32c(bytes); 82 bytes[168..172].copy_from_slice(&crc.to_le_bytes()); 83 } 84 85 #[test] 86 fn catalog_rejects_bad_header_counts_and_exact_lengths() { 87 let (_, valid, _) = empty_catalog(); 88 89 for (offset, replacement) in [(0, b'X'), (8, 2), (10, 63)] { 90 let mut malformed = valid.clone(); 91 malformed[offset] = replacement; 92 assert!(Catalog::decode(&malformed).is_err(), "offset {offset}"); 93 } 94 95 let mut wrong_count = valid.clone(); 96 wrong_count[12..16].copy_from_slice(&1u32.to_le_bytes()); 97 assert_eq!( 98 Catalog::decode(&wrong_count), 99 Err(FormatError::InvalidLength) 100 ); 101 assert_eq!( 102 Catalog::decode(&valid[..15]), 103 Err(FormatError::InvalidLength) 104 ); 105 let mut trailing = valid.clone(); 106 trailing.push(0); 107 assert_eq!(Catalog::decode(&trailing), Err(FormatError::InvalidLength)); 108 109 let mut over_limit = valid; 110 over_limit[12..16].copy_from_slice(&((MAX_CATALOG_ENTRIES as u32) + 1).to_le_bytes()); 111 assert_eq!( 112 Catalog::decode(&over_limit), 113 Err(FormatError::CatalogTooLarge) 114 ); 115 } 116 117 #[test] 118 fn catalog_rejects_entry_reserved_flags_and_invalid_lengths() { 119 let mut encoded = entry(2, b"x").encode().unwrap(); 120 for offset in 58..64 { 121 let mut malformed = encoded; 122 malformed[offset] = 1; 123 assert_eq!( 124 CatalogEntry::decode(&malformed), 125 Err(FormatError::NonzeroReserved) 126 ); 127 } 128 129 encoded[56] = 1; 130 assert_eq!( 131 CatalogEntry::decode(&encoded), 132 Err(FormatError::MalformedDescriptor) 133 ); 134 135 for (offset, value) in [ 136 (32, 0u64), // kind zero 137 (34, 0), // version zero 138 (44, 0), // length zero 139 (44, 67_108_865), 140 (52, 0), // unit count zero 141 (52, 2), // incorrect ceil(length / unit) 142 ] { 143 let mut malformed = entry(2, b"x").encode().unwrap(); 144 let bytes = value.to_le_bytes(); 145 let width = if offset == 44 { 146 8 147 } else if offset == 52 { 148 4 149 } else { 150 2 151 }; 152 malformed[offset..offset + width].copy_from_slice(&bytes[..width]); 153 assert!( 154 CatalogEntry::decode(&malformed).is_err(), 155 "offset {offset}, value {value}" 156 ); 157 } 158 assert_eq!( 159 ObjectId::calculate(3, 1, b""), 160 Err(FormatError::InvalidObject) 161 ); 162 } 163 164 #[test] 165 fn catalog_rejects_unsorted_and_duplicate_object_ids() { 166 let a = entry(2, b"a"); 167 let b = entry(3, b"b"); 168 let (low, high) = if a.object_id < b.object_id { 169 (a, b) 170 } else { 171 (b, a) 172 }; 173 174 let sorted = Catalog { 175 entries: vec![low, high], 176 } 177 .encode() 178 .unwrap(); 179 assert!(Catalog::decode(&sorted).is_ok()); 180 181 let descending = Catalog { 182 entries: vec![high, low], 183 } 184 .encode(); 185 assert_eq!(descending, Err(FormatError::CatalogOrder)); 186 187 let duplicate = Catalog { 188 entries: vec![low, low], 189 } 190 .encode(); 191 assert_eq!(duplicate, Err(FormatError::DuplicateObject)); 192 } 193 194 #[test] 195 fn catalog_entry_bounds_reject_metadata_overflow_and_high_water_violations() { 196 let valid = entry(2, b"x"); 197 assert!(valid.validate_bounds(8, 4, 5).is_ok()); 198 assert_eq!( 199 entry(0, b"x").validate_bounds(8, 4, 5), 200 Err(FormatError::OutOfBounds) 201 ); 202 assert_eq!( 203 entry(1, b"x").validate_bounds(8, 4, 5), 204 Err(FormatError::OutOfBounds) 205 ); 206 assert_eq!( 207 entry(4, b"x").validate_bounds(8, 4, 5), 208 Err(FormatError::OutOfBounds) 209 ); 210 assert_eq!( 211 entry(2, b"x").validate_bounds(8, 4, 2), 212 Err(FormatError::OutOfBounds) 213 ); 214 assert_eq!( 215 entry(3, b"x").validate_bounds(2, 8, 8), 216 Err(FormatError::OutOfBounds) 217 ); 218 219 let mut overflow = valid; 220 overflow.first_unit = u64::MAX; 221 assert_eq!( 222 overflow.validate_bounds(8, 8, 8), 223 Err(FormatError::LengthOverflow) 224 ); 225 } 226 227 #[test] 228 fn catalog_extents_reject_overlap_and_metadata_overlap() { 229 let a = entry(2, b"first"); 230 let b = entry(2, b"second"); 231 let mut entries = vec![a, b]; 232 entries.sort_by_key(|item| item.object_id); 233 let catalog = Catalog { entries }; 234 assert_eq!( 235 catalog.validate_extents(16, 4, 8), 236 Err(FormatError::Overlap) 237 ); 238 239 let metadata_overlap = Catalog { 240 entries: vec![entry(3, b"third")], 241 }; 242 assert_eq!( 243 metadata_overlap.validate_extents(16, 3, 8), 244 Err(FormatError::OutOfBounds) 245 ); 246 } 247 248 #[test] 249 fn object_id_rejects_semantic_mutation_wrong_identity_and_nonzero_padding() { 250 let semantic = b"payload"; 251 let id = oid(3, semantic); 252 let mut extent = vec![0; STORE_UNIT_BYTES]; 253 extent[..semantic.len()].copy_from_slice(semantic); 254 assert!(id 255 .validate_extent(3, 1, semantic.len() as u64, &extent) 256 .is_ok()); 257 258 let mut changed_semantic = extent.clone(); 259 changed_semantic[0] ^= 1; 260 assert_eq!( 261 id.validate_extent(3, 1, semantic.len() as u64, &changed_semantic), 262 Err(FormatError::IntegrityFailure) 263 ); 264 265 let mut changed_padding = extent.clone(); 266 changed_padding[STORE_UNIT_BYTES - 1] = 1; 267 assert_eq!( 268 id.validate_extent(3, 1, semantic.len() as u64, &changed_padding), 269 Err(FormatError::NonzeroReserved) 270 ); 271 272 assert_eq!( 273 id.validate_extent(4, 1, semantic.len() as u64, &extent), 274 Err(FormatError::IntegrityFailure) 275 ); 276 assert_eq!( 277 id.validate_extent(3, 2, semantic.len() as u64, &extent), 278 Err(FormatError::IntegrityFailure) 279 ); 280 assert_eq!( 281 id.validate_extent(3, 1, (semantic.len() + 1) as u64, &extent), 282 Err(FormatError::IntegrityFailure) 283 ); 284 assert_eq!( 285 id.validate_extent(3, 1, semantic.len() as u64, &extent[..STORE_UNIT_BYTES - 1]), 286 Err(FormatError::InvalidLength) 287 ); 288 assert_eq!( 289 ObjectId::calculate(3, 1, b""), 290 Err(FormatError::InvalidObject) 291 ); 292 } 293 294 #[test] 295 fn crc32c_known_answer_and_superblock_rejects_corruption_reserved_and_slot() { 296 assert_eq!(crc32c(b"`123456789`"), 0xe306_9283); 297 let commit = genesis(); 298 let valid = superblock(0, &commit).encode().unwrap(); 299 assert!(Superblock::decode(&valid, 0).is_ok()); 300 301 let mut field_corrupt = valid; 302 field_corrupt[56] ^= 1; 303 assert_eq!( 304 Superblock::decode(&field_corrupt, 0), 305 Err(FormatError::BadSuperblockCrc) 306 ); 307 308 let mut crc_corrupt = valid; 309 crc_corrupt[168] ^= 1; 310 assert_eq!( 311 Superblock::decode(&crc_corrupt, 0), 312 Err(FormatError::BadSuperblockCrc) 313 ); 314 315 for offset in 172..STORE_UNIT_BYTES { 316 let mut reserved = valid; 317 reserved[offset] = 1; 318 rechecksum_superblock(&mut reserved); 319 assert_eq!( 320 Superblock::decode(&reserved, 0), 321 Err(FormatError::NonzeroReserved), 322 "offset {offset}" 323 ); 324 } 325 326 assert_eq!( 327 Superblock::decode(&valid, 1), 328 Err(FormatError::WrongSuperblockSlot) 329 ); 330 331 for (offset, value) in [(48, 3u64), (56, 0), (160, 5)] { 332 let mut malformed = valid; 333 malformed[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); 334 rechecksum_superblock(&mut malformed); 335 assert_eq!( 336 Superblock::decode(&malformed, 0), 337 Err(FormatError::MalformedSuperblock), 338 "offset {offset}" 339 ); 340 } 341 342 let mut unsupported_features = valid; 343 unsupported_features[12] = 1; 344 rechecksum_superblock(&mut unsupported_features); 345 assert_eq!( 346 Superblock::decode(&unsupported_features, 0), 347 Err(FormatError::UnsupportedFeatures) 348 ); 349 } 350 351 #[test] 352 fn commit_record_rejects_lengths_features_generation_and_high_water() { 353 let good = genesis(); 354 let encoded = good.encode().unwrap(); 355 assert_eq!(encoded.len(), COMMIT_RECORD_BYTES); 356 assert_eq!(CommitRecord::decode(&encoded).unwrap(), good); 357 358 for bad_length in [COMMIT_RECORD_BYTES - 1, COMMIT_RECORD_BYTES + 1] { 359 let malformed = vec![0u8; bad_length]; 360 assert!(CommitRecord::decode(&malformed).is_err()); 361 } 362 363 for (offset, value) in [ 364 (56, 0u64), 365 (64, 1), 366 (176, 15), 367 (184, 2), 368 (188, 1), 369 (192, 3), 370 (192, 5), 371 ] { 372 let mut malformed = encoded; 373 malformed[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); 374 assert!( 375 CommitRecord::decode(&malformed).is_err(), 376 "offset {offset}, value {value}" 377 ); 378 } 379 380 let mut oversized_region = encoded; 381 oversized_region[48..56].copy_from_slice(&4_294_967_297u64.to_le_bytes()); 382 assert!(CommitRecord::decode(&oversized_region).is_err()); 383 384 let mut features = encoded; 385 features[16] = 1; 386 assert_eq!( 387 CommitRecord::decode(&features), 388 Err(FormatError::UnsupportedFeatures) 389 ); 390 391 let commit_id = good.object_id().unwrap(); 392 let mut padded_commit = vec![0u8; STORE_UNIT_BYTES]; 393 padded_commit[..encoded.len()].copy_from_slice(&encoded); 394 assert!(commit_id 395 .validate_extent(2, 1, encoded.len() as u64, &padded_commit) 396 .is_ok()); 397 padded_commit[STORE_UNIT_BYTES - 1] = 1; 398 assert_eq!( 399 commit_id.validate_extent(2, 1, encoded.len() as u64, &padded_commit), 400 Err(FormatError::NonzeroReserved) 401 ); 402 403 let mut malformed_genesis = good.clone(); 404 malformed_genesis.previous_generation = 1; 405 assert_eq!( 406 malformed_genesis.encode(), 407 Err(FormatError::InvalidGeneration) 408 ); 409 } 410 411 #[test] 412 fn generation_link_requires_adjacent_generation_and_exact_predecessor_ids() { 413 let previous = genesis(); 414 let previous_id = previous.object_id().unwrap(); 415 let mut next = previous.clone(); 416 next.generation = 2; 417 next.previous_generation = 1; 418 next.previous_commit_id = previous_id; 419 next.previous_catalog_id = previous.catalog_id; 420 next.catalog_first_unit = 4; 421 next.committed_high_water = 6; 422 assert!(next.encode().is_ok()); 423 assert!(next.identifies_predecessor(&previous, previous_id)); 424 425 next.previous_catalog_id.0[0] ^= 1; 426 assert!(!next.identifies_predecessor(&previous, previous_id)); 427 next.previous_catalog_id = previous.catalog_id; 428 next.previous_commit_id.0[0] ^= 1; 429 assert!(!next.identifies_predecessor(&previous, previous_id)); 430 next.previous_commit_id = previous_id; 431 next.previous_generation = 0; 432 assert_eq!(next.encode(), Err(FormatError::InvalidGeneration)); 433 434 let mut overflow = next; 435 overflow.generation = u64::MAX; 436 overflow.previous_generation = u64::MAX - 1; 437 overflow.previous_commit_id = ObjectId([1; 32]); 438 overflow.previous_catalog_id = ObjectId([1; 32]); 439 assert!(overflow.encode().is_ok()); 440 overflow.generation = u64::MAX; 441 overflow.previous_generation = u64::MAX; 442 assert_eq!(overflow.encode(), Err(FormatError::InvalidGeneration)); 443 } 444 445 #[test] 446 fn superblock_commit_validation_binds_commit_unit_catalog_and_high_water() { 447 let commit = genesis(); 448 let sb = superblock(0, &commit); 449 assert!(sb 450 .validate_commit(commit.object_id().unwrap(), &commit) 451 .is_ok()); 452 453 let mut wrong_commit_unit = sb.clone(); 454 wrong_commit_unit.commit_record_unit = 2; 455 assert_eq!( 456 wrong_commit_unit.validate_commit(commit.object_id().unwrap(), &commit), 457 Err(FormatError::MalformedSuperblock) 458 ); 459 460 let mut wrong_catalog = sb.clone(); 461 wrong_catalog.catalog_first_unit = 1; 462 assert_eq!( 463 wrong_catalog.validate_commit(commit.object_id().unwrap(), &commit), 464 Err(FormatError::MalformedSuperblock) 465 ); 466 467 let mut wrong_high_water = sb; 468 wrong_high_water.committed_high_water = 3; 469 assert_eq!( 470 wrong_high_water.validate_commit(commit.object_id().unwrap(), &commit), 471 Err(FormatError::MalformedSuperblock) 472 ); 473 }