test(core): allow logging from rust unit tests
What changed, and why it matters
This commit only changes how Rust unit tests inside the Trezor firmware project can print diagnostic messages. It enables a debug console feature for test builds, reads an optional RUST_LOG environment variable, and increases the maximum log message length from 128 to 512 bytes during tests. It also improves handling when a log message is too long by adding a clear '(message truncated)' note. There is nothing here that affects real device security or user funds.
No security action needed. Treat as a normal test-infrastructure improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch is confined to the test build of the embedded Rust code. Cargo.toml adds the ‘dbg_console’ feature to the ‘test’ feature set. lib.rs initializes Rust logging when RUST_LOG is set to a non-zero value before invoking the Rust test harness. util/logger.rs raises MAX_MESSAGE_LEN to 512 under #[cfg(test)], captures the result of write_fmt, and passes res.is_ok() as the final-chunk flag to syslog_write_chunk, appending ‘(message truncated)’ on overflow. These are test-only quality-of-life changes with no runtime device behavior change.
Changed components
core/embed/rust/Cargo.tomlcore/embed/rust/src/lib.rscore/embed/rust/src/util/logger.rsInspect captured patch +16 / −2
diff --git a/core/embed/rust/Cargo.toml b/core/embed/rust/Cargo.toml
index 2cd2bf49..c6e1d73f 100644
--- a/core/embed/rust/Cargo.toml
+++ b/core/embed/rust/Cargo.toml
@@ -68,6 +68,7 @@ test = [
"button",
"cc",
"crypto",
+ "dbg_console",
"dma2d",
"debug",
"glob",
diff --git a/core/embed/rust/src/lib.rs b/core/embed/rust/src/lib.rs
index 1bde575e..6586b436 100644
--- a/core/embed/rust/src/lib.rs
+++ b/core/embed/rust/src/lib.rs
@@ -108,6 +108,12 @@ pub fn main() -> i32 {
}
rust_tests_c_setup();
}
+
+ match std::env::var("RUST_LOG") {
+ Ok(s) if s != "0" => crate::util::logger::init_rust_logging(0),
+ _ => eprintln!("Set RUST_LOG=1 to enable logs."),
+ }
+
// Call the Rust test harness main function
// The function panics if any test fails.
// Asserting that it returns () to ensure that if a future Rust version
diff --git a/core/embed/rust/src/util/logger.rs b/core/embed/rust/src/util/logger.rs
index 6972fc72..0e168b04 100644
--- a/core/embed/rust/src/util/logger.rs
+++ b/core/embed/rust/src/util/logger.rs
@@ -11,6 +11,9 @@ use core::{
use crate::trezorhal::syslog::{syslog_start_record, syslog_write_chunk, LogLevel};
+#[cfg(test)]
+const MAX_MESSAGE_LEN: usize = 512;
+#[cfg(not(test))]
const MAX_MESSAGE_LEN: usize = 128;
static INITIALIZED: AtomicBool = AtomicBool::new(false);
@@ -46,11 +49,15 @@ impl Log for SysLogger {
let mut msg = Vec::<u8, MAX_MESSAGE_LEN>::new();
// Might still get partial message on error.
- let _ = msg.write_fmt(*record.args());
+ let res = msg.write_fmt(*record.args());
// SAFETY: passed to C which doesn't care about UTF-8
let text = unsafe { str::from_utf8_unchecked(&msg) };
- syslog_write_chunk(text, true);
+ syslog_write_chunk(text, res.is_ok());
+
+ if res.is_err() {
+ syslog_write_chunk("(message truncated)", true);
+ }
}
fn flush(&self) {}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.