From 489a26dd113929e11aad79e0fe3869685a7842d0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 5 Jan 2022 20:01:54 +0800 Subject: [PATCH] fix an issue where we don't decrement the connection counter on drop --- .vscode/settings.json | 1 + API.md | 1 + services/codec/src/lib.rs | 1 + services/com/src/lib.rs | 2 +- services/dns/src/lib.rs | 1 + services/engine-25519/src/lib.rs | 1 + services/gam/src/lib.rs | 1 + services/graphics-server/src/lib.rs | 1 + services/ime-plugin-api/src/lib.rs | 1 + services/jtag/src/lib.rs | 1 + services/keyboard/src/lib.rs | 1 + services/llio/src/lib.rs | 1 + services/modals/src/lib.rs | 1 + services/net/src/lib.rs | 1 + services/oqc-test/src/lib.rs | 1 + services/pddb/locales/i18n.json | 5 ++ services/pddb/src/api.rs | 22 ++++---- services/pddb/src/backend/hw.rs | 19 ++----- services/pddb/src/frontend/pddbkey.rs | 76 ++++----------------------- services/pddb/src/lib.rs | 75 ++++++++++++++------------ services/pddb/src/main.rs | 37 ++++++++----- services/root-keys/src/lib.rs | 1 + services/rtc/src/lib.rs | 1 + services/skeleton/src/lib.rs | 1 + services/spinor/src/lib.rs | 1 + services/susres/src/lib.rs | 1 + services/ticktimer-server/src/lib.rs | 1 + services/trng/src/lib.rs | 1 + services/xous-names/src/lib.rs | 1 + 29 files changed, 118 insertions(+), 140 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 58887c6f6..4312bfe05 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { "rust-analyzer.checkOnSave.allTargets": false, + "rust-analyzer.cargo.target":"riscv32imac-unknown-xous-elf" } diff --git a/API.md b/API.md index d14bf365b..119b30125 100644 --- a/API.md +++ b/API.md @@ -240,6 +240,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Codec { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/codec/src/lib.rs b/services/codec/src/lib.rs index 2a1ca0c23..027fb2cbb 100644 --- a/services/codec/src/lib.rs +++ b/services/codec/src/lib.rs @@ -129,6 +129,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Codec { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index dc669f05b..3e53a1400 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -557,8 +557,8 @@ impl Drop for Com { Message::new_scalar(api::Callback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); unsafe{xous::disconnect(cid).unwrap();} } - // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/dns/src/lib.rs b/services/dns/src/lib.rs index 0b1327453..f9187ab8f 100644 --- a/services/dns/src/lib.rs +++ b/services/dns/src/lib.rs @@ -55,6 +55,7 @@ impl Drop for Dns { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/engine-25519/src/lib.rs b/services/engine-25519/src/lib.rs index 16e55d824..fab252d5f 100644 --- a/services/engine-25519/src/lib.rs +++ b/services/engine-25519/src/lib.rs @@ -149,6 +149,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Engine25519 { fn drop(&mut self) { // disconnect from the main server, only if there are no more instances active + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 71c7aedc7..34c0d6532 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -344,6 +344,7 @@ impl Drop for Gam { unsafe{xous::disconnect(cid).unwrap();} xous::destroy_server(sid).unwrap(); } + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index 36ed661e3..15f5b5eb1 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -259,6 +259,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Gfx { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe { xous::disconnect(self.conn).unwrap(); diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index f975507a3..a6911733e 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -272,6 +272,7 @@ impl Drop for ImeFrontEnd { } xous::destroy_server(sid).unwrap(); } + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe { xous::disconnect(self.cid).unwrap(); diff --git a/services/jtag/src/lib.rs b/services/jtag/src/lib.rs index a4514a964..f9a5a9a9a 100644 --- a/services/jtag/src/lib.rs +++ b/services/jtag/src/lib.rs @@ -146,6 +146,7 @@ impl Drop for Jtag { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index b35c2e430..0e33148fd 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -65,6 +65,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Keyboard { fn drop(&mut self) { // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/llio/src/lib.rs b/services/llio/src/lib.rs index b005dc758..9c203e2ed 100644 --- a/services/llio/src/lib.rs +++ b/services/llio/src/lib.rs @@ -590,6 +590,7 @@ impl Drop for Llio { if let Some(sid) = self.gpio_sid.take() { drop_conn(sid); } + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 2a45b68db..c8abba3c7 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -216,6 +216,7 @@ impl Drop for Modals { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/net/src/lib.rs b/services/net/src/lib.rs index ee4727038..245a47e13 100644 --- a/services/net/src/lib.rs +++ b/services/net/src/lib.rs @@ -34,6 +34,7 @@ impl Drop for NetConn { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/oqc-test/src/lib.rs b/services/oqc-test/src/lib.rs index effbb2284..0cb6848c7 100644 --- a/services/oqc-test/src/lib.rs +++ b/services/oqc-test/src/lib.rs @@ -48,6 +48,7 @@ use core::sync::atomic::{AtomicU32, Ordering}; static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Oqc { fn drop(&mut self) { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/pddb/locales/i18n.json b/services/pddb/locales/i18n.json index afbb93a70..0c71b1737 100644 --- a/services/pddb/locales/i18n.json +++ b/services/pddb/locales/i18n.json @@ -24,6 +24,11 @@ "zh": "密码错误。 再试一次?", "en-audio": "Incorrect password. Try again?" }, + "pddb.badpass_infallible": { + "en": "Incorrect password.\n\nPlease try again.", + "zh": "密码错误。", + "en-audio": "Incorrect password. Please try again." + }, "pddb.requestformat": { "en": "The PDDB storage needs\nformatting. This takes\nabout 15 minutes and can't\nbe interrupted.\n\nProceed?\n", "zh": "存储需要格式化。这需要8分钟。继续?", diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 39ed8986d..31d3f9b97 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -64,28 +64,22 @@ pub(crate) const PDDB_MODAL_NAME: &'static str = "pddb modal"; pub(crate) enum Opcode { KeyRequest, - /// read implemented using only scalars -- fast, but limited size - ReadKeyScalar, - /// read implemented using a memory buffer -- slower, but just shy of a page in size - ReadKeyMem, - - WriteKeyScalar1, - WriteKeyScalar2, - WriteKeyScalar3, - WriteKeyScalar4, - WriteKeyMem, + ReadKey, + WriteKey, WriteKeyFlush, /// quit the server Quit, } +pub type ApiToken = [u32; 3]; + /// A structure for requesting a token to access a particular key/value pair #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub(crate) struct PddbKeyRequest { pub(crate) dict: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 pub(crate) key: xous_ipc::String::, // pending https://github.com/rust-lang/rust/issues/90195 - pub(crate) token: Option<[u32; 3]>, + pub(crate) token: Option, } /// Return codes for Read/Write API calls to the main server @@ -107,13 +101,15 @@ pub(crate) enum PddbRetcode { #[repr(C, packed)] pub(crate) struct PddbBuf { /// api token for the given buffer - pub(crate) token: [u32; 3], + pub(crate) token: ApiToken, + /// point in the key stream. 64-bit for future-compatibility; but, can't be larger than 32 bits on a 32-bit target. + pub(crate) position: u64, /// length of the data field pub(crate) len: u16, /// a field reserved for the return code pub(crate) retcode: PddbRetcode, pub(crate) reserved: u8, - pub(crate) data: [u8; 4080], + pub(crate) data: [u8; 4072], } impl PddbBuf { pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf { diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index c4bc427c4..a15996e07 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -505,20 +505,11 @@ impl PddbOs { } } fn syskey_ensure(&mut self) { - if self.system_basis_key.is_none() { - let scd = self.static_crypto_data_get(); - let mut system_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE]; - for (&src, dst) in scd.system_key.iter().zip(system_key.iter_mut()) { - *dst = src; - } - for block in system_key.chunks_mut(aes::BLOCK_SIZE) { - self.rootkeys.decrypt_block(block.try_into().unwrap()); - } - self.system_basis_key = Some(system_key); - } - if self.cipher_ecb.is_none() { - let cipher = Aes256::new(GenericArray::from_slice(&self.system_basis_key.unwrap())); - self.cipher_ecb = Some(cipher); + while !self.try_login() { + self.clear_password(); // clear the bad password entry + let xns = xous_names::XousNames::new().unwrap(); + let modals = modals::Modals::new(&xns).expect("can't connect to Modals server"); + modals.show_notification(t!("pddb.badpass_infallible", xous::LANG)).expect("notification failed"); } } diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index 3712259fd..b9d52323c 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -14,7 +14,9 @@ pub struct PddbKey<'a> { conn: CID, dict: String, key: String, - token: [u32; 3], + token: ApiToken, + /// position in the key's data "stream" + pos: u64, buf: Buffer<'a>, } impl<'a> PddbKey<'a> { @@ -80,6 +82,7 @@ impl<'a> PddbKey<'a> { dict, key, token, + pos: 0, buf: Buffer::new(core::mem::size_of::()), }) } else { @@ -100,35 +103,6 @@ impl<'a> Read for PddbKey<'a> { fn read(&mut self, buf: &mut [u8]) -> Result { if buf.len() == 0 { Ok(0) - } else if buf.len() <= 8 { - let response = send_message( - self.conn, - Message::new_blocking_scalar( - Opcode::ReadKeyScalar.to_usize().unwrap(), - self.token[0] as usize, - self.token[1] as usize, - self.token[2] as usize, - buf.len() as usize, - ) - ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; - // scalars can return up to 64 bits of data. Split into an array and copy into the buf as demanded. - if let xous::Result::Scalar2(a, b) = response { - let mut maxbuf: [u8; 8] = [0; 8]; - for (&src, dst) in (a as u32).to_le_bytes().iter().zip(maxbuf[..4].iter_mut()) { - *dst = src; - } - if buf.len() > 4 { - for (&src, dst) in (b as u32).to_le_bytes().iter().zip(maxbuf[4..].iter_mut()) { - *dst = src; - } - } - for (&src, dst) in maxbuf.iter().zip(buf.iter_mut()) { - *dst = src; - } - Ok(buf.len()) - } else { - Err(Error::new(ErrorKind::Other, "Xous internal error")) - } } else { // create pbuf from a pre-reserved chunk of memory, to save on allocator thrashing // note that it does mean that un-erased data from previous reads and writes are passed back @@ -148,10 +122,11 @@ impl<'a> Read for PddbKey<'a> { }; pbuf.len = readlen; pbuf.retcode = PddbRetcode::Uninit; + pbuf.position = self.pos; readlen }; // this takes the buffer and remaps it to the server, and on return the data is mapped back - self.buf.lend_mut(self.conn, Opcode::ReadKeyMem.to_u32().unwrap()) + self.buf.lend_mut(self.conn, Opcode::ReadKey.to_u32().unwrap()) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; { // at this point, pbuf has been mutated by the server with a return code and the return data. @@ -162,6 +137,7 @@ impl<'a> Read for PddbKey<'a> { *dst = src; } assert!(pbuf.len <= readlen, "More data returned than we requested"); + self.pos += buf.len() as u64; Ok(pbuf.len as usize) } PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), @@ -177,39 +153,6 @@ impl<'a> Write for PddbKey<'a> { fn write(&mut self, buf: &[u8]) -> Result { if buf.len() == 0 { Ok(0) - } else if buf.len() <= 4 { - let opcode = match buf.len() { - 1 => Opcode::WriteKeyScalar1.to_usize().unwrap(), - 2 => Opcode::WriteKeyScalar2.to_usize().unwrap(), - 3 => Opcode::WriteKeyScalar3.to_usize().unwrap(), - 4 => Opcode::WriteKeyScalar4.to_usize().unwrap(), - _ => panic!("This shouldn't happen") - }; - // guarantee we have enough bytes to do the u32 conversion, even if we have less data in the incoming buf - let mut u32buf: [u8; 4] = [0; 4]; - for (&src, dst) in buf.iter().zip(u32buf.iter_mut()) { - *dst = src; - } - let response = send_message( - self.conn, - Message::new_blocking_scalar( - opcode, - self.token[0] as usize, - self.token[1] as usize, - self.token[2] as usize, - u32::from_le_bytes(u32buf) as usize, - ) - ).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; - if let xous::Result::Scalar2(rcode, len) = response { - match FromPrimitive::from_u8(rcode as u8) { - Some(PddbRetcode::Ok) => Ok(len), - Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), - Some(PddbRetcode::AccessDenied) => Err(Error::new(ErrorKind::PermissionDenied, "Access denied")), - _ => Err(Error::new(ErrorKind::Other, "Unhandled error code in PddbKey Read")), - } - } else { - Err(Error::new(ErrorKind::Other, "Xous internal error")) - } } else { let writelen = { let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut()); @@ -227,10 +170,11 @@ impl<'a> Write for PddbKey<'a> { for (&src, dst) in buf.iter().zip(pbuf.data.iter_mut()) { *dst = src; } + pbuf.position = self.pos; writelen }; // this takes the buffer and remaps it to the server, and on return the data is mapped back - self.buf.lend_mut(self.conn, Opcode::WriteKeyMem.to_u32().unwrap()) + self.buf.lend_mut(self.conn, Opcode::WriteKey.to_u32().unwrap()) .or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?; { // at this point, pbuf has been mutated by the server with a return code and the return data. @@ -238,6 +182,7 @@ impl<'a> Write for PddbKey<'a> { match pbuf.retcode { PddbRetcode::Ok => { assert!(pbuf.len <= writelen, "More data written than we requested"); + self.pos += writelen as u64; Ok(pbuf.len as usize) } PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")), @@ -273,6 +218,7 @@ impl<'a> Drop for PddbKey<'a> { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 601f6742e..b36b9447d 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -7,54 +7,46 @@ pub use frontend::*; use num_traits::*; use std::io::{Result, Error, ErrorKind}; +use xous::CID; use std::collections::HashMap; - - -////// probably things in this file should eventually be split ount into their own -////// frontend module file. - -// this is an intenal structure for managing the overall PDDB -#[allow(dead_code)] -pub(crate) struct PddbManager { - -} -#[allow(dead_code)] -impl PddbManager { - // return a list of open bases - fn list_basis() {} -} - -// this is an internal struct for managing a basis -#[allow(dead_code)] -pub(crate) struct PddbBasis { - -} -#[allow(dead_code)] -impl PddbBasis { - // opening and closing basis will side-effect PddbDicts and PddbKeys - fn open() {} // will result in a password box being triggered to open a basis - fn close() {} -} +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); // this structure can be shared on the user side? -#[allow(dead_code)] -pub struct PddbDict<'a> { +pub struct Pddb<'a> { + conn: CID, contents: HashMap, &'a [u8]>, callback: Box, } -impl<'a> PddbDict<'a> { - // opens a dictionary only if it exists - pub fn open(_dict_name: &str) -> Option { None } - // creates a dictionary only if it does not already exist - pub fn create(_dict_name: &str) -> Option { None } +impl<'a> Pddb<'a> { + /// return a list of all open bases + pub fn list_basis() -> Vec:: { + Vec::new() + } + /// returns the latest basis that is opened -- this is where all new values are being sent by default + pub fn get_current_basis() -> String { + String::new() + } - // returns a key only if it exists + // opens a dictionary only if it exists + pub fn open(_dict_name: &str) -> Option { None } + // creates a dictionary only if it does not already exist + pub fn create(dict_name: &str) -> Option { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + None + } + + /// returns a key only if it exists pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result> { self.callback = Box::new(key_changed_cb); Ok(None) } + /// closes the key, de-allocating the OS memory to track it. + pub fn close(&mut self, _key: PddbKey) -> Result<()> { + Ok(()) + } // updates an existing key's value. mainly used by write(). pub fn update(&mut self, _key: PddbKey) -> Result> { Ok(None) } // creates a key or overwrites it @@ -65,3 +57,16 @@ impl<'a> PddbDict<'a> { pub fn delete(&mut self) {} } +impl<'a> Drop for Pddb<'a> { + fn drop(&mut self) { + // the connection to the server side must be reference counted, so that multiple instances of this object within + // a single process do not end up de-allocating the CID on other threads before they go out of scope. + // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } + // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), + // de-allocate those items here. They don't need a reference count because they are object-specific + } +} \ No newline at end of file diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index e8be7b595..3a67cf4be 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -381,8 +381,9 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message} use xous_ipc::Buffer; use core::cell::RefCell; use std::rc::Rc; - use std::thread; +use std::collections::HashSet; +use std::hash::{Hash, Hasher}; use locales::t; @@ -392,6 +393,25 @@ pub(crate) struct BasisRequestPassword { plaintext_pw: Option>, } +#[derive(Eq)] +struct TokenRecord { + token: ApiToken, + dict: String, + key: String, + basis: Option, +} +impl Hash for TokenRecord { + fn hash(&self, state: &mut H) { + self.token.hash(state); + } +} +impl PartialEq for TokenRecord { + fn eq(&self, other: &Self) -> bool { + self.token == other.token + } +} + + #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); @@ -415,6 +435,8 @@ fn xmain() -> ! { let mut pddb_os = PddbOs::new(Rc::clone(&entropy)); // storage for the basis cache let mut basis_cache = BasisCache::new(); + // storage for the token lookup: given an ApiToken, return a dict/key/basis set. Basis can be None or specified. + let mut token_dict = HashSet::::new(); // run the CI tests if the option has been selected #[cfg(all( @@ -540,19 +562,10 @@ fn xmain() -> ! { Some(Opcode::KeyRequest) => { // placeholder } - Some(Opcode::ReadKeyScalar) => msg_blocking_scalar_unpack!(msg, _tok0, _tok1, _tok2, _len, { - // placeholder - }), - Some(Opcode::ReadKeyMem) => { + Some(Opcode::ReadKey) => { // placeholder } - Some(Opcode::WriteKeyScalar1) - | Some(Opcode::WriteKeyScalar2) - | Some(Opcode::WriteKeyScalar3) - | Some(Opcode::WriteKeyScalar4) => msg_blocking_scalar_unpack!(msg, _tok0, _tok1, _tok2, _data, { - // placeholder - }), - Some(Opcode::WriteKeyMem) => { + Some(Opcode::WriteKey) => { // placeholder } Some(Opcode::WriteKeyFlush) => { diff --git a/services/root-keys/src/lib.rs b/services/root-keys/src/lib.rs index bd4120fd2..f720fd1cf 100644 --- a/services/root-keys/src/lib.rs +++ b/services/root-keys/src/lib.rs @@ -178,6 +178,7 @@ impl Drop for RootKeys { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/rtc/src/lib.rs b/services/rtc/src/lib.rs index 6c126be5f..764d5f2f7 100644 --- a/services/rtc/src/lib.rs +++ b/services/rtc/src/lib.rs @@ -115,6 +115,7 @@ impl Drop for Rtc { // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. // all implementations will need this + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/skeleton/src/lib.rs b/services/skeleton/src/lib.rs index 897a8313d..15b2ce66c 100644 --- a/services/skeleton/src/lib.rs +++ b/services/skeleton/src/lib.rs @@ -24,6 +24,7 @@ impl Drop for Codec { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/spinor/src/lib.rs b/services/spinor/src/lib.rs index 52e5ac2d0..dabe983e6 100644 --- a/services/spinor/src/lib.rs +++ b/services/spinor/src/lib.rs @@ -396,6 +396,7 @@ impl Drop for Spinor { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index 1b3eb0cce..e1aabd643 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -128,6 +128,7 @@ impl Drop for Susres { if let Some(sid) = self.suspend_cb_sid.take() { drop_conn(sid); } + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe{xous::disconnect(self.conn).unwrap();} } diff --git a/services/ticktimer-server/src/lib.rs b/services/ticktimer-server/src/lib.rs index 9038c5083..1a3873ef3 100644 --- a/services/ticktimer-server/src/lib.rs +++ b/services/ticktimer-server/src/lib.rs @@ -66,6 +66,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Ticktimer { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe { xous::disconnect(self.conn).unwrap(); diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index a4cd51df7..61d58f76c 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -124,6 +124,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Trng { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe { xous::disconnect(self.conn).unwrap(); diff --git a/services/xous-names/src/lib.rs b/services/xous-names/src/lib.rs index 111f20072..92af0dfbc 100644 --- a/services/xous-names/src/lib.rs +++ b/services/xous-names/src/lib.rs @@ -175,6 +175,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for XousNames { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); if REFCOUNT.load(Ordering::Relaxed) == 0 { unsafe { xous::disconnect(self.conn).unwrap();