diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index e6023edc8..bc720a577 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -154,6 +154,35 @@ pub(crate) const SMALL_POOL_STRIDE: u64 = 0xFE_0000; pub(crate) const SMALL_CAPACITY: usize = VPAGE_SIZE; pub(crate) const LARGE_POOL_START: u64 = 0x0000_FE00_0000_0000; pub(crate) const KEY_MAXCOUNT: usize = 131_071; // 2^17 - 1 +/// This is a size limit on the biggest file you can create. It's currently 32GiB. No, this is not +/// web scale, but it's big enough to hold a typical blu-ray movie as a single file. You can adjust +/// this constant up or down, and the trade-off is, you get more or less total number of large files +/// allocated over the life of the filesystem. We simply "increment a pointer" when a new large file +/// is added to create the next virtual memory spot for the large file. So at 32GiB, you can create +/// a lifetime total of about 200 million files (this includes files you've previously deleted, until +/// we create a mechanism for sweeping through the memory space and tracking de-allocations). Note that +/// a "large" file includes anything over 4kiB, so if you create a 5kiB file, it can potentially grow to +/// 32 GiB without bumping into the next large file. This is a very "lazy" way to deal with large files. +/// Given that the PDDB is designed for a 32-bit device with only 128MiB of memory and a read/write lifetime +/// of 100k cycles for the FLASH, 200 million file allocations is probably greater than the lifetime of +/// the device itself. If the PDDB migrates to a larger handphone-style application, I think it'll probably +/// still hold up OK with 200 million total large file allocations over the device lifetime and a limit +/// of 32GiB. That's about 73k files created per day for 10 years, or about 50 files per minute -- roughly +/// one new file per second for 10 years straight before the PDDB runs out of virtual memory space. +/// A web server creating a >4k temporary log file for every client that hit and then deleting it +/// would probably crush this limit in months. So don't use the PDDB to back a high volume web server. +/// But it's probably OK for a consumer electronics device with a typical lifetime of less than 10 years. +/// If you really think you want larger files and also more write life, you'd need to implement an in-memory +/// "free" file allocator, but honestly, this is not something I think we need to burn resources on for +/// the initial target of the PDDB (that is, a 100MiB device with 100k read/write endurance lifetime). +/// Anyways, the code is written so you can just slide this constant up or down and change the behavior +/// of the system; it's recommended you reformat when you do that but I /think/ it should actually be OK +/// if you made a change "on the fly". +/// +/// Also note that in practice, a file size is limited to 4GiB on a 32-bit Precursor device anyways +/// because the usize type isn't big enough. Recompiling for a 64-bit target, however, should give +/// you access to the 32GiB file size limit. +pub(crate) const LARGE_FILE_MAX_SIZE: u64 = 0x0000_0008_0000_0000; /// The chosen "stride" of a dict/key entry. Drives a lot of key parameters in the database's characteristics. /// This is chosen such that 32 of these entries fit evenly into a VPAGE. @@ -256,9 +285,12 @@ impl BasisCache { // allocate a vpage offset for the dictionary let dict_index = basis.dict_get_free_offset(hw); let dict_offset = VirtAddr::new(dict_index as u64 * DICT_VSIZE).unwrap(); - let pp = basis.v2p_map.entry(dict_offset).or_insert_with(|| - hw.try_fast_space_alloc().expect("No free space to allocate dict")); - pp.set_valid(true); + let pp = basis.v2p_map.entry(dict_offset).or_insert_with(|| { + let mut ap = hw.try_fast_space_alloc().expect("No free space to allocate dict"); + ap.set_valid(true); + ap + }); + assert!(pp.valid(), "v2p returned an invalid page"); // create the cache entry let mut dict_name = [0u8; DICT_NAME_LEN]; @@ -418,6 +450,7 @@ impl BasisCache { log::trace!("reading offset {}/{}:{}", cur_offset, endstop, kcache.len); let mut read_offset = (cur_offset % VPAGE_SIZE as u64) as usize; if let Some(pp) = basis.v2p_map.get(&VirtAddr::new(start_vpage_addr).unwrap()) { + assert!(pp.valid(), "v2p returned an invalid page"); let pt_data = hw.data_decrypt_page(&basis.cipher, &basis.aad, pp).expect("Decryption auth error"); if blocks_read != 0 { assert!(read_offset == 0, "algorithm error in handling offset data"); @@ -762,6 +795,7 @@ impl BasisCacheEntry { while try_entry <= DICT_MAXCOUNT && dict_count < self.num_dicts { let dict_vaddr = VirtAddr::new(try_entry as u64 * DICT_VSIZE).unwrap(); if let Some(pp) = self.v2p_map.get(&dict_vaddr) { + assert!(pp.valid(), "v2p returned an invalid page"); if let Some(dict) = self.dict_decrypt(hw, &pp) { if dict.flags.valid() { let dict_name = String::from(cstr_to_string(&dict.name)); @@ -814,12 +848,14 @@ impl BasisCacheEntry { for index in 0..dcache.small_pool.len() { let pool_vaddr = VirtAddr::new(small_storage_base_vaddr_from_indices(dcache.index, index)).unwrap(); if let Some(pp) = self.v2p_map.get_mut(&pool_vaddr) { - if paranoid { + assert!(pp.valid(), "v2p returned an invalid page"); + { // always nuke old data let mut random = [0u8; PAGE_SIZE]; hw.trng_slice(&mut random); hw.patch_data(&random, pp.page_number() * PAGE_SIZE as u32); } hw.fast_space_free(pp); + assert!(pp.valid() == false, "pp is still marked as valid!"); } } // erase the entire dictionary + key allocation area by writing over with random data. It's important to @@ -830,11 +866,13 @@ impl BasisCacheEntry { // note: check this code against dict_indices_to_vaddr() -- it's recoded here because we go by page, not by index, but this needs to be consistent with that function let dk_vaddr = VirtAddr::new(dcache.index.get() as u64 * DICT_VSIZE as u64 + page as u64 * VPAGE_SIZE as u64).unwrap(); if let Some(pp) = self.v2p_map.get_mut(&dk_vaddr) { + assert!(pp.valid(), "v2p returned an invalid page"); log::info!("erasing dk page 0x{:x}/0x{:x}", dk_vaddr, pp.page_number() as usize * PAGE_SIZE); let mut random = [0u8; PAGE_SIZE]; hw.trng_slice(&mut random); hw.patch_data(&random, pp.page_number() * PAGE_SIZE as u32); hw.fast_space_free(pp); + assert!(pp.valid() == false, "pp is still marked as valid!"); } else { log::warn!("Inconsistent internal state: requested dictionary didn't have a mapping in the page table."); } @@ -862,6 +900,7 @@ impl BasisCacheEntry { while try_entry <= DICT_MAXCOUNT { let dict_vaddr = VirtAddr::new(try_entry as u64 * DICT_VSIZE).unwrap(); if let Some(pp) = self.v2p_map.get(&dict_vaddr) { + assert!(pp.valid(), "v2p returned an invalid page"); if self.dict_decrypt(hw, &pp).is_none() { // mapping exists, but the data is invalid. It's a free entry. return try_entry as u32 @@ -886,6 +925,7 @@ impl BasisCacheEntry { while try_entry <= DICT_MAXCOUNT && dict_count < self.num_dicts { let dict_vaddr = VirtAddr::new(try_entry as u64 * DICT_VSIZE).unwrap(); if let Some(pp) = self.v2p_map.get(&dict_vaddr) { + assert!(pp.valid(), "v2p returned an invalid page"); if let Some(dict) = self.dict_decrypt(hw, &pp) { if cstr_to_string(&dict.name) == name { return Some((try_entry as u32, dict)) @@ -985,8 +1025,12 @@ impl BasisCacheEntry { } // 1. resolve the virtual address to a target page let cur_vpage = VirtAddr::new(dict_offset.get() + (vpage_num as u64 * VPAGE_SIZE as u64)).unwrap(); - let pp = self.v2p_map.entry(cur_vpage).or_insert_with(|| - hw.try_fast_space_alloc().expect("FastSpace empty")).clone(); + let pp = self.v2p_map.entry(cur_vpage).or_insert_with(|| { + let mut ap = hw.try_fast_space_alloc().expect("FastSpace empty"); + ap.set_valid(true); + ap + }); + assert!(pp.valid(), "v2p returned an invalid page"); // 2(a). fill in the target vpage with data: header special case let mut dk_vpage = DictKeyVpage::default(); @@ -1100,6 +1144,7 @@ impl BasisCacheEntry { }; let pp = self.v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap()) .expect("Internal consistency error: Basis exists, but its root map was not allocated!"); + assert!(pp.valid(), "basis page was invalid"); let journal_bytes = self.journal.to_le_bytes(); // journal gets bumped by the patching function now let slice_iter = journal_bytes.iter() // journal rev diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 116feb472..07b8c1c1f 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -105,6 +105,7 @@ impl DictCacheEntry { } else { let cache_pp = index_cache.tag.as_ref().expect("PP should be in existence, it was already checked..."); let pp = v2p_map.get(&VirtAddr::new(req_vaddr).unwrap()).expect("dictionary PP should be in existence"); + assert!(pp.valid(), "v2p returned an invalid page"); assert!(cache_pp.page_number() == pp.page_number(), "cache inconsistency error"); let cache = index_cache.data.as_ref().expect("Cache should be full, it was already checked..."); let mut keydesc = KeyDescriptor::default(); @@ -193,6 +194,7 @@ impl DictCacheEntry { let cache = index_cache.data.as_ref().expect("Cache should be full, it was already checked..."); let cache_pp = index_cache.tag.as_ref().expect("PP should be in existence, it was already checked..."); let pp = v2p_map.get(&VirtAddr::new(req_vaddr).unwrap()).expect("dictionary PP should be in existence"); + assert!(pp.valid(), "v2p returned an invalid page"); assert!(cache_pp.page_number() == pp.page_number(), "cache inconsistency error"); let mut keydesc = KeyDescriptor::default(); let start = size_of::() + (try_entry % DK_PER_VPAGE) * DK_STRIDE; @@ -302,8 +304,55 @@ impl DictCacheEntry { let kcache = self.keys.get_mut(name).expect("Entry was assured, but then not there!"); // the update isn't going to fit in the reserved space, remove it, and re-insert it with an entirely new entry. if kcache.reserved < (data.len() + offset) as u64 { - self.key_remove(hw, v2p_map, cipher, name, false); - return self.key_update(hw, v2p_map, cipher, name, data, offset, alloc_hint, truncate, large_alloc_ptr); + if kcache.start < SMALL_POOL_END { + // this started life as a small key. the algorithm is to remove and retry upon extend. + // allocate a new vector that contains the *entire* data contents (not just the updated portion), and re-insert it + let mut update_data = Vec::::with_capacity(data.len() + offset); + if let Some(KeyCacheData::Small(old_data)) = &kcache.data { + for &b in &old_data.data { + update_data.push(b); + } + } else { + log::error!("expected a small key, but the data type was incorrect or not in cache"); + panic!("expected a small key, but the data type was incorrect or not in cache"); + } + // extend the vector out to the expected length, so the indexed slice in the next step doesn't panic + while update_data.len() < offset + data.len() { + update_data.push(0); + } + for (&src, dst) in data.iter().zip(update_data[offset..].iter_mut()) { *dst = src }; + log::info!("update/extend: removing {}", name); + // now remove the old key entirely + self.key_remove(hw, v2p_map, cipher, name, false); + if update_data.len() > 4 { // just make sure that this log call doesn't fail on an index violation... + log::info!("update/extend: re-adding {} with data len {}: {:x?}...", name, update_data.len(), &update_data[..4]); + } + // and re-add it with the extended data; if it's no longer a small key after this, it'll be handled inside this call. + return self.key_update(hw, v2p_map, cipher, name, &update_data, 0, alloc_hint, truncate, large_alloc_ptr); + } else { + // large data sets will need more physical pages to be allocated for the new file length. It's a hard error + // if the requested size goes beyond the pre-allocated virtual memory space limit. + if (offset + data.len()) as u64 > LARGE_FILE_MAX_SIZE { + log::error!("Requested file update would exceed our file size limit. Asked: {}, limit: {}", offset + data.len(), LARGE_FILE_MAX_SIZE); + panic!("Updated file size is greater than the large file size limit."); + } + assert!((kcache.start + kcache.reserved) % VPAGE_SIZE as u64 == 0, "large space allocation rules were violated"); + let new_reservation = PageAlignedVa::from(kcache.start + offset as u64 + data.len() as u64).as_u64(); + for vpage_addr in ( + (kcache.start + kcache.reserved)..new_reservation + ).step_by(VPAGE_SIZE) { + // ensures that a physical page entry exists for every new virtual address required by the extended key + v2p_map.entry(VirtAddr::new(vpage_addr).unwrap()).or_insert_with(|| { + let mut ap = hw.try_fast_space_alloc().expect("No free space to allocate additional large key storage"); + ap.set_valid(true); + ap + }); + } + kcache.reserved = new_reservation; + kcache.clean = false; + // now retry the call, with the new physical page reservations in place + return self.key_update(hw, v2p_map, cipher, name, data, offset, alloc_hint, truncate, large_alloc_ptr); + } } // the key exists, *and* there's sufficient space for the data if kcache.start < SMALL_POOL_END { @@ -313,6 +362,7 @@ impl DictCacheEntry { if let KeyCacheData::Small(cache_data) = kcache.data.as_mut().expect("small pool should all have their data 'hot' if the index entry is also in cache") { cache_data.clean = false; // grow the data cache to accommodate the necessary length; this should be efficient because we reserved space when the vector was allocated + assert!(cache_data.data.len() != 0, "(temporary assert - expected data in the cache for a particular test. Remove this assert if you're hitting this on an actual zero-lenth data set."); while cache_data.data.len() < data.len() + offset { cache_data.data.push(0); } @@ -349,6 +399,7 @@ impl DictCacheEntry { if ((kcache.start + offset as u64 + written as u64) % VPAGE_SIZE as u64) != 0 { let start_vpage_addr = ((kcache.start + offset as u64) / VPAGE_SIZE as u64) * VPAGE_SIZE as u64; let pp = v2p_map.get(&VirtAddr::new(start_vpage_addr).unwrap()).expect("large key data allocation missing"); + assert!(pp.valid(), "v2p returned an invalid page"); let mut pt_data = match hw.data_decrypt_page(&cipher, &self.aad, pp) { Some(data) => data, None => { @@ -378,6 +429,7 @@ impl DictCacheEntry { while written < data.len() { let vpage_addr = ((kcache.start + written as u64 + offset as u64) / VPAGE_SIZE as u64) * VPAGE_SIZE as u64; let pp = v2p_map.get(&VirtAddr::new(vpage_addr).unwrap()).expect("large key data allocation missing"); + assert!(pp.valid(), "v2p returned an invalid page"); if data.len() - written >= VPAGE_SIZE { // overwrite whole pages without decryption let mut block = [0u8; VPAGE_SIZE + size_of::()]; @@ -417,7 +469,9 @@ impl DictCacheEntry { if (vpage_end_offset.as_u64() - kcache.start) > kcache.reserved { for vpage in (vpage_end_offset.as_u64()..kcache.start + kcache.reserved).step_by(VPAGE_SIZE) { if let Some(pp) = v2p_map.get_mut(&VirtAddr::new(vpage).unwrap()) { + assert!(pp.valid(), "v2p returned an invalid page"); hw.fast_space_free(pp); + assert!(pp.valid() == false, "pp is still marked as valid!"); } } kcache.reserved = vpage_end_offset.as_u64() - kcache.start; @@ -533,7 +587,9 @@ impl DictCacheEntry { v2p_map.insert(VirtAddr::new(vpage_addr).unwrap(), pp); } // 2. Recurse. Now, the key should exist, and it should go through the "write the data out" section of the algorithm. - return self.key_update(hw, v2p_map, cipher, name, data, offset, alloc_hint, truncate, large_alloc_ptr + reservation); + // note that the alloc pointer at this point sets the ultimate limit for the large file size (32GiB as of writing). + return self.key_update(hw, v2p_map, cipher, name, data, offset, alloc_hint, truncate, + large_alloc_ptr + PageAlignedVa::from(LARGE_FILE_MAX_SIZE)); } } Ok(large_alloc_ptr) @@ -554,6 +610,10 @@ impl DictCacheEntry { /// overwritten in paranoid mode, but large keys are. pub fn key_remove(&mut self, hw: &mut PddbOs, v2p_map: &mut HashMap::, cipher: &Aes256GcmSiv, name_str: &str, paranoid: bool) { + if paranoid { + // large records are paranoid-erased, by default, because of the pool-reuse problem. + unimplemented!("Paranoid erase for small records not yet implemented. Calling sync after an update, however, effectively does a paranoid erase."); + } // this call makes sure we have a cache entry to operate on. self.ensure_key_entry(hw, v2p_map, cipher, name_str); let name = String::from(name_str); @@ -564,8 +624,15 @@ impl DictCacheEntry { if let Some(small_index) = small_storage_index_from_key(kcache, self.index) { // handle the small pool case let ksp = &mut self.small_pool[small_index]; + log::info!("victim: {}", name); + for s in &ksp.contents { + log::info!("before: {}", s); + } ksp.contents.swap_remove(ksp.contents.iter().position(|s| *s == name) .expect("Small pool did not contain the element we expected")); + for s in &ksp.contents { + log::info!("after: {}", s); + } assert!(kcache.reserved <= SMALL_CAPACITY as u64, "error in small key entry size"); ksp.avail += kcache.reserved as u16; assert!(ksp.avail <= SMALL_CAPACITY as u16, "bookkeeping error in small pool capacity"); @@ -583,12 +650,15 @@ impl DictCacheEntry { // ...but we remove the virtual pages from the page pool, effectively reclaiming the physical space. for vpage in kcache.large_pool_vpages() { if let Some(pp) = v2p_map.get_mut(&vpage) { - if paranoid { + assert!(pp.valid(), "v2p returned an invalid page"); + { // this slows things down but it prevents data from leaking back into other basis data structures when the sector is re-allocated + // in other words, i think it's probably very unsafe to not always secure-erase large data as it's de-allocated. let mut noise = [0u8; PAGE_SIZE]; hw.trng_slice(&mut noise); hw.patch_data(&noise, pp.page_number() * PAGE_SIZE as u32); } hw.fast_space_free(pp); + assert!(pp.valid() == false, "pp is still marked as valid!"); } } } @@ -650,9 +720,12 @@ impl DictCacheEntry { for (index, entry) in self.small_pool.iter_mut().enumerate() { if !entry.clean { let pool_vaddr = VirtAddr::new(small_storage_base_vaddr_from_indices(self.index, index)).unwrap(); - let pp= v2p_map.entry(pool_vaddr).or_insert_with(|| - hw.try_fast_space_alloc().expect("No free space to allocate small key storage")); - pp.set_valid(true); + let pp = v2p_map.entry(pool_vaddr).or_insert_with(|| { + let mut ap = hw.try_fast_space_alloc().expect("No free space to allocate small key storage"); + ap.set_valid(true); + ap + }); + assert!(pp.valid(), "v2p returned an invalid page"); // WARNING - we don't read back the journal number before loading data into the page! // we /could/ do that, but it incurs an expensive full-page decryption when we plan to nuke all the data. @@ -957,6 +1030,7 @@ impl PlaintextCache { req_vaddr: VirtAddr ) { if let Some(pp) = v2p_map.get(&req_vaddr) { + assert!(pp.valid(), "v2p returned an invalid page"); let mut fill_needed = false; if let Some(tag) = self.tag { if tag.page_number() != pp.page_number() { diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 95766e3cd..495583510 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -1220,6 +1220,7 @@ impl PddbOs { let aad = basis_root.aad(self.dna); let pp = basis_v2p_map.get(&VirtAddr::new(1 * VPAGE_SIZE as u64).unwrap()) .expect("Internal consistency error: Basis exists, but its root map was not allocated!"); + assert!(pp.valid(), "v2p returned an invalid page"); let journal_bytes = (0 as u32).to_le_bytes(); let slice_iter = journal_bytes.iter() // journal rev diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index d0e7e4c07..7d6ac46b8 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -428,7 +428,8 @@ fn xmain() -> ! { pddb_os.dbg_dump(Some("basecase1e".to_string())); log::info!("Doing delete/add consistency"); - delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, None, None, None); + delete_add_dict_consistency(&mut pddb_os, &mut basis_cache, None, + None, None, None); log::info!("Saving `dachecke` to local host"); pddb_os.dbg_dump(Some("dachecke".to_string())); diff --git a/services/pddb/src/tests.rs b/services/pddb/src/tests.rs index 969df84b8..1ab22f095 100644 --- a/services/pddb/src/tests.rs +++ b/services/pddb/src/tests.rs @@ -2,6 +2,7 @@ use rand::Rng; use crate::*; const UPPER_BOUND: usize = 9000; +const LOWER_BOUND: usize = 12; // needs to be big enough to compute murmur3 hash + hold checksum fn gen_key(dictname: &str, keynum: usize, lower_size_bound: usize, upper_size_bound: usize) -> (String, Vec::) { let mut rng = rand::thread_rng(); @@ -44,7 +45,7 @@ pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCach let num_dicts = maybe_num_dicts.unwrap_or(4); let num_keys = maybe_num_keys.unwrap_or(34); - let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((1, UPPER_BOUND - 4)); // 4 bytes for the CI checksum + let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); // 4 bytes for the CI checksum let extra_reserved = maybe_extra_reserved.unwrap_or(0); // make all the directories first @@ -76,10 +77,13 @@ pub(crate) fn create_basis_testcase(hw: &mut PddbOs, basis_cache: &mut BasisCach /// Delete & add dictionary consistency check pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut BasisCache, - maybe_num_dicts: Option, maybe_num_keys: Option, maybe_key_sizes: Option<(usize, usize)>) { + maybe_num_dicts: Option, maybe_num_keys: Option, maybe_key_sizes: Option<(usize, usize)>, + maybe_extra_reserved: Option, +) { let evict_count = maybe_num_dicts.unwrap_or(1); let num_keys = maybe_num_keys.unwrap_or(36); - let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((1, 9000)); + let (key_lower_bound, key_upper_bound) = maybe_key_sizes.unwrap_or((LOWER_BOUND, UPPER_BOUND - 4)); + let extra_reserved = maybe_extra_reserved.unwrap_or(0); let dict_list = basis_cache.dict_list(hw); let dict_start_index = dict_list.len(); @@ -99,7 +103,16 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas let dictname = format!("dict{}", dictnum + dict_start_index); let (keyname, keydata) = gen_key(&dictname, keynum, key_lower_bound, key_upper_bound); // now we're ready to write it out - basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, None, None, false).unwrap(); + if maybe_extra_reserved.is_none() { + // we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly + basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, + None, + None, false).unwrap(); + } else { + basis_cache.key_update(hw, &dictname, &keyname, &keydata, None, + Some(keydata.len() + extra_reserved), + None, false).unwrap(); + } } } }