From b099005951f3d7d304064d9be68480dfbbff229d Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Thu, 25 Aug 2022 15:36:09 -0700 Subject: [PATCH] kata-os-logger: support no logging interface connection out of a component When a CAmkES component lacks an outbound connection to send log msgs there will be no logger_log symbol. Use a weak ref here to handle that without resorting to a feature or similar. Mark logger connections as "maybe" so they are optional. Change-Id: I6ecd939014d26a612d115741fd2ac673afa40857 GitOrigin-RevId: 0b1bf2611cbb628500cae37889c6547a996d50e9 --- .../MailboxDriver/MailboxDriver.camkes | 2 +- .../MemoryManager/MemoryManager.camkes | 2 +- .../MlCoordinator/MlCoordinator.camkes | 3 +- .../ProcessManager/ProcessManager.camkes | 2 +- .../components/SDKRuntime/SDKRuntime.camkes | 2 +- .../SecurityCoordinator.camkes | 2 +- .../StorageManager/StorageManager.camkes | 2 +- .../TimerService/TimerService.camkes | 2 +- .../kata-os-common/src/logger/Cargo.toml | 2 +- .../kata-os-common/src/logger/src/lib.rs | 56 +++++++++++-------- 10 files changed, 42 insertions(+), 33 deletions(-) diff --git a/apps/system/components/MailboxDriver/MailboxDriver.camkes b/apps/system/components/MailboxDriver/MailboxDriver.camkes index f8cdf5c..b2027d7 100644 --- a/apps/system/components/MailboxDriver/MailboxDriver.camkes +++ b/apps/system/components/MailboxDriver/MailboxDriver.camkes @@ -25,5 +25,5 @@ component MailboxDriver { consumes Interrupt rtirq; consumes Interrupt eirq; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; } diff --git a/apps/system/components/MemoryManager/MemoryManager.camkes b/apps/system/components/MemoryManager/MemoryManager.camkes index 51b0391..d5f1237 100644 --- a/apps/system/components/MemoryManager/MemoryManager.camkes +++ b/apps/system/components/MemoryManager/MemoryManager.camkes @@ -20,7 +20,7 @@ import ; component MemoryManager { provides MemoryInterface memory; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; // Enable KataOS CAmkES support. attribute int kataos = true; diff --git a/apps/system/components/MlCoordinator/MlCoordinator.camkes b/apps/system/components/MlCoordinator/MlCoordinator.camkes index 24bfe0f..decbc72 100644 --- a/apps/system/components/MlCoordinator/MlCoordinator.camkes +++ b/apps/system/components/MlCoordinator/MlCoordinator.camkes @@ -16,6 +16,7 @@ import ; import ; import ; import ; +import ; component MlCoordinator { control; @@ -32,7 +33,7 @@ component MlCoordinator { dataport Buf CSR; dataport Buf(0x1000000) TCM; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; uses MemoryInterface memory; uses SecurityCoordinatorInterface security; diff --git a/apps/system/components/ProcessManager/ProcessManager.camkes b/apps/system/components/ProcessManager/ProcessManager.camkes index 288efa4..9663f91 100644 --- a/apps/system/components/ProcessManager/ProcessManager.camkes +++ b/apps/system/components/ProcessManager/ProcessManager.camkes @@ -24,7 +24,7 @@ component ProcessManager { provides PackageManagementInterface pkg_mgmt; provides ProcessControlInterface proc_ctrl; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; uses MemoryInterface memory; uses SecurityCoordinatorInterface security; diff --git a/apps/system/components/SDKRuntime/SDKRuntime.camkes b/apps/system/components/SDKRuntime/SDKRuntime.camkes index b84eee7..93df512 100644 --- a/apps/system/components/SDKRuntime/SDKRuntime.camkes +++ b/apps/system/components/SDKRuntime/SDKRuntime.camkes @@ -20,7 +20,7 @@ import ; component SDKRuntime { provides SDKRuntimeInterface sdk_runtime; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; // Enable KataOS CAmkES support. attribute int kataos = true; diff --git a/apps/system/components/SecurityCoordinator/SecurityCoordinator.camkes b/apps/system/components/SecurityCoordinator/SecurityCoordinator.camkes index 9937705..5cafda3 100644 --- a/apps/system/components/SecurityCoordinator/SecurityCoordinator.camkes +++ b/apps/system/components/SecurityCoordinator/SecurityCoordinator.camkes @@ -22,7 +22,7 @@ import ; component SecurityCoordinator { provides SecurityCoordinatorInterface security; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; uses MemoryInterface memory; uses MailboxAPI mailbox_api; diff --git a/apps/system/components/StorageManager/StorageManager.camkes b/apps/system/components/StorageManager/StorageManager.camkes index c519aa9..ab203e4 100644 --- a/apps/system/components/StorageManager/StorageManager.camkes +++ b/apps/system/components/StorageManager/StorageManager.camkes @@ -21,7 +21,7 @@ import ; component StorageManager { provides StorageInterface storage; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; uses SecurityCoordinatorInterface security; // Enable KataOS CAmkES support. diff --git a/apps/system/components/TimerService/TimerService.camkes b/apps/system/components/TimerService/TimerService.camkes index edb97ba..5d87c16 100644 --- a/apps/system/components/TimerService/TimerService.camkes +++ b/apps/system/components/TimerService/TimerService.camkes @@ -20,7 +20,7 @@ component TimerService { dataport Buf csr; consumes Interrupt timer_interrupt; - uses LoggerInterface logger; + maybe uses LoggerInterface logger; // Enable KataOS CAmkES support. attribute int kataos = true; diff --git a/apps/system/components/kata-os-common/src/logger/Cargo.toml b/apps/system/components/kata-os-common/src/logger/Cargo.toml index d42a418..c31e7d4 100644 --- a/apps/system/components/kata-os-common/src/logger/Cargo.toml +++ b/apps/system/components/kata-os-common/src/logger/Cargo.toml @@ -8,4 +8,4 @@ arrayvec = { version = "0.7", default-features = false } core2 = { version = "0.3", default-features = false } # Disable default so we don't pull in CString which requires an allocator cstr_core = { version = "0.2.3", default-features = false } -log = "0.4" +log = { version = "0.4" } diff --git a/apps/system/components/kata-os-common/src/logger/src/lib.rs b/apps/system/components/kata-os-common/src/logger/src/lib.rs index 2f6defc..1cc3d2d 100644 --- a/apps/system/components/kata-os-common/src/logger/src/lib.rs +++ b/apps/system/components/kata-os-common/src/logger/src/lib.rs @@ -13,6 +13,14 @@ // limitations under the License. #![cfg_attr(not(test), no_std)] +#![feature(linkage)] + +extern "C" { + // NB: components may not have a logging connection in which case + // logger_log will be undefined/null. + #[linkage = "extern_weak"] + static logger_log: *const (); +} use core2::io::{Cursor, Write}; use cstr_core::CStr; @@ -28,10 +36,12 @@ impl log::Log for KataLogger { fn enabled(&self, _metadata: &Metadata) -> bool { true } fn log(&self, record: &Record) { + let typed_logger_log: Option = + unsafe { core::mem::transmute(logger_log) }; + if typed_logger_log.is_none() { + return; + } if self.enabled(record.metadata()) { - extern "C" { - fn logger_log(level: u8, msg: *const cstr_core::c_char); - } let mut buf = [0 as u8; MAX_MSG_LEN]; let mut cur = Cursor::new(&mut buf[..]); // Log msgs are of the form: ':: @@ -41,29 +51,27 @@ impl log::Log for KataLogger { cur.write(b"...\0").expect("write!"); () }); - unsafe { - // If an embedded nul is identified, replace the message; there - // are likely better solutions but this should not happen. - fn embedded_nul_cstr<'a>( - buf: &'a mut [u8; MAX_MSG_LEN], - record: &Record, - ) -> &'a cstr_core::CStr { - let mut cur = Cursor::new(&mut buf[..]); - write!(&mut cur, "{}::\0", record.target()).expect("nul!"); - let pos = cur.position() as usize; - CStr::from_bytes_with_nul(&buf[..pos]).unwrap() - } - // NB: this releases the ref on buf held by the Cursor + // If an embedded nul is identified, replace the message; there + // are likely better solutions but this should not happen. + fn embedded_nul_cstr<'a>( + buf: &'a mut [u8; MAX_MSG_LEN], + record: &Record, + ) -> &'a cstr_core::CStr { + let mut cur = Cursor::new(&mut buf[..]); + write!(&mut cur, "{}::\0", record.target()).expect("nul!"); let pos = cur.position() as usize; - logger_log( - record.level() as u8, - match CStr::from_bytes_with_nul(&buf[..pos]) { - Ok(cstr) => cstr, - Err(_) => embedded_nul_cstr(&mut buf, record), - } - .as_ptr(), - ); + CStr::from_bytes_with_nul(&buf[..pos]).unwrap() } + // NB: this releases the ref on buf held by the Cursor + let pos = cur.position() as usize; + (typed_logger_log.unwrap())( + record.level() as u8, + match CStr::from_bytes_with_nul(&buf[..pos]) { + Ok(cstr) => cstr, + Err(_) => embedded_nul_cstr(&mut buf, record), + } + .as_ptr(), + ); } }