fix up xous-names and resolve issue #26 thanks to stdlib

also frees up about 24k of unused RAM, i'll take it!
This commit is contained in:
bunnie 2021-08-04 01:59:17 +08:00
parent 8b83fb7c65
commit ef671dd6a3
4 changed files with 119 additions and 209 deletions

143
Cargo.lock generated
View file

@ -150,8 +150,8 @@ dependencies = [
"lazycell", "lazycell",
"log", "log",
"peeking_take_while", "peeking_take_while",
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"regex", "regex",
"rustc-hash", "rustc-hash",
"shlex", "shlex",
@ -623,9 +623,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
@ -767,26 +767,6 @@ dependencies = [
"xous-names", "xous-names",
] ]
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]]
name = "hash32-derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc0efbd154a17cddc3616d83faef479c0076d871a2143c157b310cc7ca799a2"
dependencies = [
"proc-macro2 0.3.8",
"quote 0.5.2",
"syn 0.13.11",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.18" version = "0.1.18"
@ -1098,9 +1078,9 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
@ -1188,22 +1168,13 @@ version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]]
name = "proc-macro2"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b06e2f335f48d24442b35a19df506a835fb3547bc3c06ef27340da9acf5cae7"
dependencies = [
"unicode-xid 0.1.0",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.24" version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [ dependencies = [
"unicode-xid 0.2.1", "unicode-xid",
] ]
[[package]] [[package]]
@ -1221,9 +1192,9 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bcd6232fa7d7fcd15703da5c07c9f10d203ee4fdf6ecf6d7ac5056db78089a3" checksum = "7bcd6232fa7d7fcd15703da5c07c9f10d203ee4fdf6ecf6d7ac5056db78089a3"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
@ -1241,22 +1212,13 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "quote"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8"
dependencies = [
"proc-macro2 0.3.8",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.8" version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
] ]
[[package]] [[package]]
@ -1471,9 +1433,9 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddf04e93b51e6cfcc0eeee3bbf121d978303b83fbf2dc49085cff7855478fd4b" checksum = "ddf04e93b51e6cfcc0eeee3bbf121d978303b83fbf2dc49085cff7855478fd4b"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
@ -1482,9 +1444,9 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95a169f6bc5a81033e86ed39d0f4150e2608160b73d2b93c6e8e6a3efa873f14" checksum = "95a169f6bc5a81033e86ed39d0f4150e2608160b73d2b93c6e8e6a3efa873f14"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
@ -1787,26 +1749,15 @@ dependencies = [
"quick-xml", "quick-xml",
] ]
[[package]]
name = "syn"
version = "0.13.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14f9bf6292f3a61d2c716723fdb789a41bbe104168e6f496dc6497e531ea1b9b"
dependencies = [
"proc-macro2 0.3.8",
"quote 0.5.2",
"unicode-xid 0.1.0",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.67" version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702" checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"unicode-xid 0.2.1", "unicode-xid",
] ]
[[package]] [[package]]
@ -1815,10 +1766,10 @@ version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
"unicode-xid 0.2.1", "unicode-xid",
] ]
[[package]] [[package]]
@ -1958,12 +1909,6 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"
@ -2047,9 +1992,9 @@ dependencies = [
"bumpalo", "bumpalo",
"lazy_static", "lazy_static",
"log", "log",
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2059,7 +2004,7 @@ version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4"
dependencies = [ dependencies = [
"quote 1.0.8", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
] ]
@ -2069,9 +2014,9 @@ version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -2138,8 +2083,8 @@ version = "0.28.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389d680d7bd67512dc9c37f39560224327038deb0f0e8d33f870900441b68720" checksum = "389d680d7bd67512dc9c37f39560224327038deb0f0e8d33f870900441b68720"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"xml-rs", "xml-rs",
] ]
@ -2317,18 +2262,16 @@ dependencies = [
name = "xous-macros" name = "xous-macros"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"rand 0.7.3", "rand 0.7.3",
"syn 1.0.67", "syn",
] ]
[[package]] [[package]]
name = "xous-names" name = "xous-names"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"hash32",
"hash32-derive",
"log", "log",
"log-server", "log-server",
"num-derive", "num-derive",
@ -2345,8 +2288,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"glob", "glob",
"lazy_static", "lazy_static",
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"regex", "regex",
"serde_json", "serde_json",
] ]
@ -2372,8 +2315,8 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c1e130bebaeab2f23886bf9acbaca14b092408c452543c857f66399cd6dab1" checksum = "a2c1e130bebaeab2f23886bf9acbaca14b092408c452543c857f66399cd6dab1"
dependencies = [ dependencies = [
"proc-macro2 1.0.24", "proc-macro2",
"quote 1.0.8", "quote",
"syn 1.0.67", "syn",
"synstructure", "synstructure",
] ]

View file

@ -6,8 +6,6 @@ name = "xous-names"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
hash32 = "0.2"
hash32-derive = "0.1.0"
num-derive = {version = "0.3", default-features = false} num-derive = {version = "0.3", default-features = false}
num-traits = {version = "0.2", default-features = false} num-traits = {version = "0.2", default-features = false}
log = "0.4" log = "0.4"

View file

@ -1,5 +1,3 @@
use hash32::{Hash, Hasher};
#[allow(dead_code)] #[allow(dead_code)]
pub const AUTHENTICATE_TIMEOUT: u32 = 10_000; // time in ms that a process has to respond to an authentication request pub const AUTHENTICATE_TIMEOUT: u32 = 10_000; // time in ms that a process has to respond to an authentication request
@ -165,14 +163,14 @@ impl core::fmt::Write for XousServerName {
} }
} }
impl hash32::Hash for XousServerName { impl std::hash::Hash for XousServerName {
fn hash<H>(&self, state: &mut H) fn hash<H>(&self, state: &mut H)
where where
H: Hasher, H: std::hash::Hasher,
{ {
assert!(self.length < self.value.len(), "incorret length on hash!"); assert!(self.length < self.value.len(), "incorret length on hash!");
Hash::hash(&self.value[..self.length as usize], state); std::hash::Hash::hash(&self.value[..self.length as usize], state);
Hash::hash(&self.length, state) std::hash::Hash::hash(&self.length, state)
} }
} }

View file

@ -1,8 +1,6 @@
#![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)] #![cfg_attr(target_os = "none", no_main)]
extern crate hash32_derive;
mod api; mod api;
use api::*; use api::*;
@ -12,6 +10,8 @@ use xous_ipc::{String, Buffer};
use log::{error, info}; use log::{error, info};
use std::collections::HashMap;
#[cfg(any(target_os = "none", target_os = "xous"))] #[cfg(any(target_os = "none", target_os = "xous"))]
mod implementation { mod implementation {
use utralib::generated::*; use utralib::generated::*;
@ -75,7 +75,6 @@ and we can use heap-allocated Rust primitives...
*/ */
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
struct Connection { struct Connection {
pub name: XousServerName,
pub sid: xous::SID, pub sid: xous::SID,
pub current_conns: u32, // number of unauthenticated (inherentely trusted) connections pub current_conns: u32, // number of unauthenticated (inherentely trusted) connections
pub max_conns: Option<u32>, // if None, unlimited connections allowed pub max_conns: Option<u32>, // if None, unlimited connections allowed
@ -84,105 +83,85 @@ struct Connection {
pub token: Option<[u32; 4]>, // a random number that must be presented to allow for disconnection for single-connection servers pub token: Option<[u32; 4]>, // a random number that must be presented to allow for disconnection for single-connection servers
} }
#[derive(Debug)] #[derive(Debug)]
struct SlowMap { struct CheckedHashMap {
pub map: [Option<Connection>; 128], pub map: HashMap::<XousServerName, Connection>,
} }
impl SlowMap { impl CheckedHashMap {
pub fn new() -> Self { pub fn new() -> Self {
SlowMap { CheckedHashMap {
map: [None; 128], map: HashMap::new(),
} }
} }
pub fn insert(&mut self, name: XousServerName, sid: xous::SID, max_conns: Option<u32>) -> Result<(), xous::Error> { pub fn insert(&mut self, name: XousServerName, sid: xous::SID, max_conns: Option<u32>) -> Result<(), xous::Error> {
let mut ok = false;
let token = if max_conns == Some(1) { let token = if max_conns == Some(1) {
// for the special case of 1-connection servers, provision a one-time use token for disconnects // for the special case of 1-connection servers, provision a one-time use token for disconnects
Some(xous::create_server_id().expect("couldn't create token").to_array()) Some(xous::create_server_id().expect("couldn't create token").to_array())
} else { } else {
None None
}; };
for entry in self.map.iter_mut() { self.map.insert(name, Connection {
if entry.is_none() { sid,
*entry = Some(Connection { current_conns: 0,
name, max_conns,
sid, allow_authenticate: false, // for now, we don't support authenticated connections
current_conns: 0, auth_conns: 0,
max_conns, token,
allow_authenticate: false, // for now, we don't support authenticated connections });
auth_conns: 0, Ok(())
token, }
}); pub fn remove(&mut self, sid: xous::SID) -> Option<XousServerName> {
ok = true; // remove is expensive, because we have to do a full search for the sid, which is not our usual key
// however, for security reasons, you have to let us know your sid (which is a secret) in order to delete
// your entry; whereas the human-readable name is not at all a secret
let mut removed_name: Option<XousServerName> = None;
for (name, mapping) in self.map.iter_mut() {
if mapping.sid == sid {
removed_name = Some(*name);
break; break;
} }
} }
if !ok { if let Some(name) = removed_name {
Err(xous::Error::OutOfMemory) self.map.remove(&name);
} else {
Ok(())
} }
}
pub fn remove(&mut self, sid: xous::SID) -> Option<XousServerName> { removed_name
let mut name: Option<XousServerName> = None;
for entry in self.map.iter_mut() {
if let Some(mapping) = entry {
if mapping.sid == sid {
name = Some(mapping.name);
*entry = None;
break;
}
}
}
name
} }
pub fn contains_key(&self, name: &XousServerName) -> bool { pub fn contains_key(&self, name: &XousServerName) -> bool {
for maybe_entry in self.map.iter() { self.map.contains_key(name)
if let Some(entry) = maybe_entry {
if entry.name == *name {
return true
}
}
}
return false
} }
pub fn connect(&mut self, name: &XousServerName) -> (Option<&xous::SID>, Option<[u32; 4]>) { pub fn connect(&mut self, name: &XousServerName) -> (Option<&xous::SID>, Option<[u32; 4]>) {
for maybe_entry in self.map.iter_mut() { let maybe_entry = self.map.get_mut(name);
if let Some(entry) = maybe_entry { if let Some(entry) = maybe_entry {
if *name == entry.name { if Some(1) == entry.max_conns {
if Some(1) == entry.max_conns { // single-connection case
// single-connection case if entry.current_conns < 1 {
if entry.current_conns < 1 { (*entry).current_conns = 1;
(*entry).current_conns = 1; return (Some(&entry.sid), entry.token)
return (Some(&entry.sid), entry.token) } else {
} else { return (None, None)
return (None, None)
}
}
if let Some(max) = entry.max_conns {
if entry.current_conns < max {
(*entry).current_conns += 1;
return (Some(&entry.sid), None);
} else {
return (None, None);
}
} else {
// unlimited connections allowed
(*entry).current_conns += 1;
return (Some(&entry.sid), None);
}
} }
} }
if let Some(max) = entry.max_conns {
if entry.current_conns < max {
(*entry).current_conns += 1;
return (Some(&entry.sid), None);
} else {
return (None, None);
}
} else {
// unlimited connections allowed
(*entry).current_conns += 1;
return (Some(&entry.sid), None);
}
} }
(None, None) (None, None)
} }
pub fn trusted_init_done(&self) -> bool { pub fn trusted_init_done(&self) -> bool {
let mut trusted_done = true; let mut trusted_done = true;
for maybe_entry in self.map.iter() { for (_name, entry) in self.map.iter() {
if let Some(entry) = maybe_entry { if let Some(max) = entry.max_conns {
if let Some(max) = entry.max_conns { if max != entry.current_conns {
if max != entry.current_conns { trusted_done = false;
trusted_done = false;
}
} }
} }
} }
@ -195,14 +174,12 @@ impl SlowMap {
// the caller can never talk to the server again. // the caller can never talk to the server again.
#[allow(dead_code)] #[allow(dead_code)]
pub fn disconnect(&mut self, sid: xous::SID) -> Option<XousServerName> { pub fn disconnect(&mut self, sid: xous::SID) -> Option<XousServerName> {
for entry in self.map.iter_mut() { for (name, mapping) in self.map.iter_mut() {
if let Some(mapping) = entry { if mapping.sid == sid {
if mapping.sid == sid { if mapping.current_conns > 0 {
if mapping.current_conns > 0 { mapping.current_conns -= 1;
mapping.current_conns -= 1;
}
return Some(mapping.name);
} }
return Some(*name);
} }
} }
None None
@ -210,15 +187,13 @@ impl SlowMap {
// this is a safer version of disconnect. we track servers that allow exactly one connection at a time // this is a safer version of disconnect. we track servers that allow exactly one connection at a time
// and give them a one-time-use token that a connector can use to disconnect. // and give them a one-time-use token that a connector can use to disconnect.
pub fn disconnect_with_token(&mut self, name: &XousServerName, token: [u32; 4]) -> bool { pub fn disconnect_with_token(&mut self, name: &XousServerName, token: [u32; 4]) -> bool {
for maybe_entry in self.map.iter_mut() { if let Some(entry) = self.map.get_mut(name) {
if let Some(entry) = maybe_entry { if let Some(old_token) = entry.token {
if let Some(old_token) = entry.token { if (token == old_token) && (entry.current_conns == 1) {
if (*name == entry.name) && (token == old_token) && (entry.current_conns == 1) { (*entry).current_conns = 0;
(*entry).current_conns = 0; // generate the token -- we should never re-use these!
// generate the token -- we should never re-use these! (*entry).token = Some(xous::create_server_id().expect("couldn't create token").to_array());
(*entry).token = Some(xous::create_server_id().expect("couldn't create token").to_array()); return true
return true
}
} }
} }
} }
@ -240,7 +215,7 @@ fn xmain() -> ! {
// this limits the number of available servers to be requested to 128...! // this limits the number of available servers to be requested to 128...!
//let mut name_table = FnvIndexMap::<XousServerName, xous::SID, 128>::new(); //let mut name_table = FnvIndexMap::<XousServerName, xous::SID, 128>::new();
let mut name_table = SlowMap::new(); let mut name_table = CheckedHashMap::new();
info!("started"); info!("started");
loop { loop {
@ -308,10 +283,8 @@ fn xmain() -> ! {
"Can't find request '{}' in table, dumping table:", "Can't find request '{}' in table, dumping table:",
name name
); );
for maybe_conn in name_table.map.iter() { for (_name, conn) in name_table.map.iter() {
if let Some(connection) = maybe_conn { log::debug!("{:?}", conn);
log::debug!("{:?}", connection);
}
} }
d11ctimeout.hosted_delay(); d11ctimeout.hosted_delay();
response = api::Return::Failure response = api::Return::Failure
@ -322,10 +295,8 @@ fn xmain() -> ! {
"Can't find request '{}' in table, dumping table:", "Can't find request '{}' in table, dumping table:",
name name
); );
for maybe_conn in name_table.map.iter() { for (_name, conn) in name_table.map.iter() {
if let Some(connection) = maybe_conn { log::debug!("{:?}", conn);
log::debug!("{:?}", connection);
}
} }
// no authenticate remedy currently supported, but we'd put that code somewhere around here eventually. // no authenticate remedy currently supported, but we'd put that code somewhere around here eventually.
let (c1, c2, c3, c4) = xous::create_server_id().unwrap().to_u32(); let (c1, c2, c3, c4) = xous::create_server_id().unwrap().to_u32();