The users of __LOG_PREFIX require that it is 0-terminated. Currently this requirement is only encoded in safety comments.
Switch to CStr which can guarantee this invariant through the type system. As __LOG_PREFIX and Module::NAME are now identical, reuse the former to implement the latter. Signed-off-by: Thomas Weißschuh <[email protected]> --- rust/kernel/lib.rs | 4 ++-- rust/kernel/print.rs | 11 ++++------- rust/macros/module.rs | 4 ++-- scripts/rustdoc_test_gen.rs | 4 ++-- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 7225abc64084..7d48f931877d 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -159,7 +159,7 @@ pub use uapi; /// Prefix to appear before log messages printed from within the `kernel` crate. -const __LOG_PREFIX: &[u8] = b"rust_kernel\0"; +const __LOG_PREFIX: &str::CStr = c"rust_kernel"; /// Dummy module type for `#[vtable]` `impl` blocks within the `kernel` crate (e.g. KUnit tests). // The `allow` is needed since it may be unused (e.g. KUnit tests may be disabled). @@ -167,7 +167,7 @@ struct LocalModule; impl ModuleMetadata for LocalModule { - const NAME: &'static str::CStr = c"rust_kernel"; + const NAME: &'static str::CStr = __LOG_PREFIX; const THIS_MODULE: ThisModule = { // SAFETY: `try_module_get`/`module_put` handle null module pointers gracefully. diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 0d62beeedca5..9ce52b9fa69f 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -96,15 +96,14 @@ pub mod format_strings { /// /// # Safety /// -/// The format string must be one of the ones in [`format_strings`], and -/// the module name must be null-terminated. +/// The format string must be one of the ones in [`format_strings`]. /// /// [`_printk`]: srctree/include/linux/printk.h #[doc(hidden)] #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] pub unsafe fn call_printk( format_string: &[u8; format_strings::LENGTH], - module_name: &[u8], + module_name: &CStr, args: fmt::Arguments<'_>, ) { // `_printk` does not seem to fail in any path. @@ -113,7 +112,7 @@ pub unsafe fn call_printk( unsafe { bindings::_printk( format_string.as_ptr(), - module_name.as_ptr(), + module_name.as_char_ptr(), core::ptr::from_ref(&args).cast::<c_void>(), ); } @@ -157,9 +156,7 @@ macro_rules! print_macro ( match $crate::prelude::fmt!($($arg)+) { // SAFETY: This hidden macro should only be called by the documented // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. + // ones. args => unsafe { $crate::print::call_printk( &$format_string, diff --git a/rust/macros/module.rs b/rust/macros/module.rs index 0a7b9e24c960..6228fe23c310 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -496,14 +496,14 @@ pub(crate) fn module(info: ModuleInfo) -> Result<TokenStream> { /// The module name. /// /// Used by the printing macros, e.g. [`info!`]. - const __LOG_PREFIX: &[u8] = #name_cstr.to_bytes_with_nul(); + const __LOG_PREFIX: &::kernel::str::CStr = #name_cstr; /// The `LocalModule` type is the type of the module created by `module!`, /// `module_pci_driver!`, `module_platform_driver!`, etc. type LocalModule = #type_; impl ::kernel::ModuleMetadata for #type_ { - const NAME: &'static ::kernel::str::CStr = #name_cstr; + const NAME: &'static ::kernel::str::CStr = __LOG_PREFIX; #[cfg(MODULE)] const THIS_MODULE: ::kernel::ThisModule = { diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs index 754c5aafcce4..488de817b425 100644 --- a/scripts/rustdoc_test_gen.rs +++ b/scripts/rustdoc_test_gen.rs @@ -224,7 +224,7 @@ macro_rules! line {{ BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()), r#"//! `kernel` crate documentation tests. -const __LOG_PREFIX: &[u8] = b"rust_doctests_kernel\0"; +const __LOG_PREFIX: &kernel::str::CStr = c"rust_doctests_kernel"; /// Dummy module type for doctest context. struct LocalModule; @@ -237,7 +237,7 @@ macro_rules! line {{ use core::ptr::null_mut; impl ModuleMetadata for LocalModule {{ - const NAME: &'static CStr = c"rust_doctests_kernel"; + const NAME: &'static CStr = __LOG_PREFIX; const THIS_MODULE: ThisModule = {{ // SAFETY: `try_module_get`/`module_put` handle null module pointers gracefully. unsafe {{ ThisModule::from_ptr(null_mut()) }} -- 2.56.0

