rust: print: add SAFETY comments to unsafe blocks
PATCH 1/1 rust: print: add SAFETY comments to unsafe blocks
Replace the placeholder // SAFETY: TODO. comments with proper safety descriptions for the two unsafe blocks in rust_fmt_argument and call_printk.
For rust_fmt_argument the ptr parameter is provided by the %pA format specifier handler in vsprintf that guarantees it points to a valid properly aligned fmt::Arguments<'_> value. since fmt::Arguments implements Copy the dereference is a bitwise copy with no side effects.
For call_printk the safety depends on the functions documented preconditions the format string is one of the fixed compile time constants from format_strings and the module name is null terminated. the arguments match the format specifiers (%s for module name %pA for the fmt::Arguments pointer).
--- rust/kernel/print.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
rust/kernel/print.rs +7 -2
@@ -29,7 +29,9 @@use fmt::Write ;// SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`. let mut w =unsafe {RawFormatter ::from_ptrs (buf.cast (), end.cast ()) }; -// SAFETY: TODO. +// SAFETY: `ptr` is provided by the `%pA` format specifier in `vsprintf` which guarantees +// that it points to a valid, properly aligned `fmt::Arguments<'_>` value for the lifetime +// of this function call. let _ = w.write_fmt (unsafe { *ptr.cast ::<fmt::Arguments <'_>>() }); w.pos ().cast () }
@@ -109,7 +111,10 @@ pub unsafe fn call_printk( ) {// `_printk` does not seem to fail in any path. #[cfg( CONFIG_PRINTK )] -// SAFETY: TODO. +// SAFETY: `format_string` is one of the fixed `format_strings::*` constants, which are +// valid null-terminated C format strings. `module_name` is guaranteed by the caller to be +// null-terminated. `&args` points to a valid `fmt::Arguments` on the stack, passed as +// `%pA` which `_printk` will forward to `rust_fmt_argument` for rendering. unsafe { bindings::_printk ( format_string.as_ptr(),
Signature · 1 line
2.43.0
Quoted 11 lines · Albab Hasan
Replace the placeholder // SAFETY: TODO. comments with proper safety descriptions for the two unsafe blocks in
rust_fmt_argumentandcall_printk.For
rust_fmt_argumentthe ptr parameter is provided by the %pA format specifier handler in vsprintf that guarantees it points to a valid properly aligned fmt::Arguments<'_> value. sincefmt::Argumentsimplements Copy the dereference is a bitwise copy with no side effects.
Quoted 7 lines
For
call_printkthe safety depends on the functions documented preconditions the format string is one of the fixed compile time constants fromformat_stringsand the module name is null terminated. the arguments match the format specifiers (%s for module name %pA for thefmt::Argumentspointer).Signed-off-by Albab Hasan <[email protected]>
Did you see the following ones?
https://lore.kernel.org/rust-for-linux/[email protected]/
https://lore.kernel.org/rust-for-linux/[email protected]/
What is the relation with them? (Cc'ing their authors)
Thanks!
Cheers,
Miguel
Thanks for pointing these out Miguel. I wasn't aware of the prior patches. Sorry for the
duplicate. Happy to drop mine in favor of whichever version you prefer.
Thanks,
Albab
Quoted 34 lines · Miguel Ojeda
Quoted 11 lines · Albab Hasan
Replace the placeholder // SAFETY: TODO. comments with proper safety descriptions for the two unsafe blocks in
rust_fmt_argumentandcall_printk.For
rust_fmt_argumentthe ptr parameter is provided by the %pA format specifier handler in vsprintf that guarantees it points to a valid properly aligned fmt::Arguments<'_> value. sincefmt::Argumentsimplements Copy the dereference is a bitwise copy with no side effects.Quoted 7 lines
For
call_printkthe safety depends on the functions documented preconditions the format string is one of the fixed compile time constants fromformat_stringsand the module name is null terminated. the arguments match the format specifiers (%s for module name %pA for thefmt::Argumentspointer).Signed-off-by Albab Hasan <[email protected]>Did you see the following ones?
https://lore.kernel.org/rust-for-linux/[email protected]/
https://lore.kernel.org/rust-for-linux/[email protected]/What is the relation with them? (Cc'ing their authors)
Thanks!
Cheers,
Miguel
Quoted 5 lines · Albab Hasan
Thanks for pointing these out Miguel. I wasn't aware of the prior patches. Sorry for the
duplicate. Happy to drop mine in favor of whichever version you prefer.
Quoted 39 lines
Thanks,
AlbabQuoted 34 lines · Miguel Ojeda
Quoted 11 lines · Albab Hasan
Replace the placeholder // SAFETY: TODO. comments with proper safety descriptions for the two unsafe blocks in
rust_fmt_argumentandcall_printk.For
rust_fmt_argumentthe ptr parameter is provided by the %pA format specifier handler in vsprintf that guarantees it points to a valid properly aligned fmt::Arguments<'_> value. sincefmt::Argumentsimplements Copy the dereference is a bitwise copy with no side effects.Quoted 7 lines
For
call_printkthe safety depends on the functions documented preconditions the format string is one of the fixed compile time constants fromformat_stringsand the module name is null terminated. the arguments match the format specifiers (%s for module name %pA for thefmt::Argumentspointer).Signed-off-by Albab Hasan <[email protected]>Did you see the following ones?
https://lore.kernel.org/rust-for-linux/[email protected]/
https://lore.kernel.org/rust-for-linux/[email protected]/What is the relation with them? (Cc'ing their authors)
Thanks!
Cheers,
Miguel
Hi Albab,
Thank you for the kind response! It is incredibly easy to miss older threads on the mailing list. I really appreciate you taking the time to look into this and write up these safety requirements.
Hi Miguel,
Regarding the relation: the v2 patch I linked earlier targets the exact same two TODOs in print.rs.
The v2 implements the generic wording Alice Ryhl suggested to ensure the %pA guarantee covers all users. I also believe that v2 cleanly resolves all the lints related to Issue #351 in print.rs.
That said, Albab did provide some great extra detail regarding the call_printk invariants. If you prefer their more detailed wording for that specific block over the v2 version, just let me know and I am fully open to incorporating it!
Best regards,
Shivendra