rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Konstantin Andrikopoulos <kernel@mandragore.io>
To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org,
	"Konstantin Andrikopoulos" <kernel@mandragore.io>
Subject: [PATCH v2] rust: add safety comment in rust_fmt_argument
Date: Mon, 18 Nov 2024 03:14:17 +0000	[thread overview]
Message-ID: <20241118031225.38080-1-kernel@mandragore.io> (raw)

The function rust_fmt_argument dereferences a pointer, and thus it
needs an unsafe block. The safety comment for that block was missing.

Link: https://github.com/Rust-for-Linux/linux/issues/351.

Signed-off-by: Konstantin Andrikopoulos <kernel@mandragore.io>
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/print.rs | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs
index a28077a7cb30..57a4fc48bbcd 100644
--- a/rust/kernel/print.rs
+++ b/rust/kernel/print.rs
@@ -13,8 +13,27 @@
 
 use crate::str::RawFormatter;
 
-// Called from `vsprintf` with format specifier `%pA`.
-#[expect(clippy::missing_safety_doc)]
+/// Handles the `%pA` format specifier.
+///
+/// This function is called by [`vsprintf`] when it encounters a `%pA` format specifier in a
+/// format string. `ptr` points to the corresponding argument, `buf` points to a buffer to hold
+/// the formatted argument, and `end` points at the buffer's end.
+///
+/// `rust_fmt_argument` treats `ptr` as a pointer to a [`core::fmt::Arguments`], which it formats,
+/// and writes the result to `buf`.
+///
+/// It returns the address after the last byte it has written in `buf`.
+///
+/// # Safety
+///
+/// `buf` and `end` must follow the requirements of [`RawFormatter`]. `ptr` must be valid for
+/// reading a [`core::fmt::Arguments`].
+///
+/// `vsprintf` guarantees that `buf` and `end` will uphold their requirements. However, it will
+/// just forward the argument that corresponds to `%pA`. Callers that provide format strings must
+/// guarantee that arguments which correspond to `%pA` are valid pointers.
+///
+/// [`vsprintf`]: srctree/lib/vsprintf.c
 #[no_mangle]
 unsafe extern "C" fn rust_fmt_argument(
     buf: *mut c_char,
@@ -24,7 +43,8 @@
     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: The safety preconditions require that `ptr` is a valid pointer to a `fmt::Arguments`
+    // struct.
     let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
     w.pos().cast()
 }
@@ -104,7 +124,8 @@ pub unsafe fn call_printk(
 ) {
     // `_printk` does not seem to fail in any path.
     #[cfg(CONFIG_PRINTK)]
-    // SAFETY: TODO.
+    // SAFETY: The format string and the module name are valid to pass to `_printk` as required
+    // by the preconditions. Additionally we pass a pointer to a valid `fmt::Arguments` struct.
     unsafe {
         bindings::_printk(
             format_string.as_ptr() as _,
@@ -124,7 +145,7 @@ pub unsafe fn call_printk(
 pub fn call_printk_cont(args: fmt::Arguments<'_>) {
     // `_printk` does not seem to fail in any path.
     //
-    // SAFETY: The format string is fixed.
+    // SAFETY: The format string is fixed and we pass a valid pointer to a `fmt::Arguments`.
     #[cfg(CONFIG_PRINTK)]
     unsafe {
         bindings::_printk(
-- 
2.47.0



             reply	other threads:[~2024-11-18  3:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-18  3:14 Konstantin Andrikopoulos [this message]
2024-11-19 19:41 ` [PATCH v2] rust: add safety comment in rust_fmt_argument Alice Ryhl
2024-11-21  9:42 ` Rasmus Villemoes
2024-11-23 21:56   ` Konstantin Andrikopoulos
2024-11-26  9:07     ` Rasmus Villemoes
2024-12-01 15:37       ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241118031225.38080-1-kernel@mandragore.io \
    --to=kernel@mandragore.io \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).