mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
log-server: remove UB in log server init
Previously, the log server would call `setup` on an uninitialized struct. This is very undefined behavior, and risked running into optimization errors. This reworks the log server such that the singleton is protected by a `None` value that gets initialized. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
c5ca371e08
commit
ac9570a767
1 changed files with 61 additions and 35 deletions
|
|
@ -1,10 +1,16 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
use xous_ipc::{String, Buffer};
|
use xous_ipc::{Buffer, String};
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum LogError {
|
||||||
|
LoggerExists,
|
||||||
|
NoConnection,
|
||||||
|
}
|
||||||
|
|
||||||
static XOUS_LOGGER: XousLogger = XousLogger {
|
static XOUS_LOGGER: XousLogger = XousLogger {
|
||||||
locked: AtomicBool::new(false),
|
locked: AtomicBool::new(false),
|
||||||
};
|
};
|
||||||
|
|
@ -13,33 +19,41 @@ struct XousLogger {
|
||||||
locked: AtomicBool,
|
locked: AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking {
|
// static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking {
|
||||||
conn: 0,
|
// conn: 0,
|
||||||
initialized: false,
|
// initialized: false,
|
||||||
buffer: None,
|
// buffer: None,
|
||||||
};
|
// };
|
||||||
|
|
||||||
|
static mut XOUS_LOGGER_BACKING: Option<XousLoggerBacking> = None;
|
||||||
|
|
||||||
struct XousLoggerBacking<'a> {
|
struct XousLoggerBacking<'a> {
|
||||||
conn: xous::CID,
|
conn: xous::CID,
|
||||||
buffer: Option<Buffer<'a>>,
|
buffer: Buffer<'a>,
|
||||||
initialized: bool,
|
}
|
||||||
|
|
||||||
|
impl<'a> XousLoggerBacking<'a> {
|
||||||
|
pub fn new() -> Result<Self, xous::Error> {
|
||||||
|
Ok(XousLoggerBacking {
|
||||||
|
conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?,
|
||||||
|
// why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
||||||
|
buffer: Buffer::new(4000),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for XousLoggerBacking<'_> {
|
||||||
|
fn default() -> Self {
|
||||||
|
XousLoggerBacking {
|
||||||
|
conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(),
|
||||||
|
// why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
||||||
|
buffer: Buffer::new(4000),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XousLoggerBacking<'_> {
|
impl XousLoggerBacking<'_> {
|
||||||
fn init(&mut self) -> Result<(), xous::Error> {
|
|
||||||
if self.initialized {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
self.conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?;
|
|
||||||
self.buffer = Some(Buffer::new(4000)); // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well...
|
|
||||||
self.initialized = true;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log_impl(&mut self, record: &log::Record) {
|
fn log_impl(&mut self, record: &log::Record) {
|
||||||
if !self.initialized && self.init().is_err() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let mut args = String::<2800>::new();
|
let mut args = String::<2800>::new();
|
||||||
write!(args, "{}", record.args()).unwrap();
|
write!(args, "{}", record.args()).unwrap();
|
||||||
let lr = api::LogRecord {
|
let lr = api::LogRecord {
|
||||||
|
|
@ -50,10 +64,8 @@ impl XousLoggerBacking<'_> {
|
||||||
args,
|
args,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(buf) = self.buffer.as_mut() {
|
self.buffer.rewrite(lr).unwrap();
|
||||||
buf.rewrite(lr).unwrap();
|
self.buffer.lend(self.conn, 0).unwrap(); // there is only one type of buffer we should be sending!
|
||||||
buf.lend(self.conn, 0).unwrap(); // there is only one type of buffer we should be sending!
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,7 +83,10 @@ impl log::Log for XousLogger {
|
||||||
xous::yield_slice();
|
xous::yield_slice();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { XOUS_LOGGER_BACKING.log_impl(record) };
|
if unsafe { XOUS_LOGGER_BACKING.is_none() } {
|
||||||
|
unsafe { XOUS_LOGGER_BACKING = Some(XousLoggerBacking::default()) };
|
||||||
|
}
|
||||||
|
unsafe { XOUS_LOGGER_BACKING.as_mut().unwrap().log_impl(record) };
|
||||||
self.locked
|
self.locked
|
||||||
.compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire)
|
.compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire)
|
||||||
.expect("LOG: logger became unlocked somehow");
|
.expect("LOG: logger became unlocked somehow");
|
||||||
|
|
@ -79,19 +94,30 @@ impl log::Log for XousLogger {
|
||||||
fn flush(&self) {}
|
fn flush(&self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() -> Result<(), log::SetLoggerError> {
|
pub fn init() -> Result<(), LogError> {
|
||||||
log::set_logger(&XOUS_LOGGER)?;
|
if let Ok(backing) = XousLoggerBacking::new() {
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
unsafe {
|
||||||
Ok(())
|
XOUS_LOGGER_BACKING = Some(backing);
|
||||||
|
}
|
||||||
|
log::set_logger(&XOUS_LOGGER).map_err(|_| LogError::LoggerExists)?;
|
||||||
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(LogError::NoConnection)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_wait() -> Result<(), log::SetLoggerError> {
|
pub fn init_wait() -> Result<(), log::SetLoggerError> {
|
||||||
|
loop {
|
||||||
|
if let Ok(backing) = XousLoggerBacking::new() {
|
||||||
|
unsafe {
|
||||||
|
XOUS_LOGGER_BACKING = Some(backing);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xous::yield_slice();
|
||||||
|
}
|
||||||
log::set_logger(&XOUS_LOGGER)?;
|
log::set_logger(&XOUS_LOGGER)?;
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
unsafe {
|
|
||||||
while XOUS_LOGGER_BACKING.init().is_err() {
|
|
||||||
xous::yield_slice();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue