rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: "Jonathan Corbet" <corbet@lwn.net>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"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@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	rust-for-linux@vger.kernel.org
Subject: [RFC PATCH 4/5] doc: rust: safety standard: add safety requirements
Date: Wed, 17 Jul 2024 22:12:53 +0000	[thread overview]
Message-ID: <20240717221133.459589-5-benno.lossin@proton.me> (raw)
In-Reply-To: <20240717221133.459589-1-benno.lossin@proton.me>

Add standardized safety requirements.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
---
 Documentation/rust/safety-standard/index.rst  |  5 ++
 .../rust/safety-standard/requirements.rst     | 80 +++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 Documentation/rust/safety-standard/requirements.rst

diff --git a/Documentation/rust/safety-standard/index.rst b/Documentation/rust/safety-standard/index.rst
index 40b17f59709c..2ef82d7dfbd8 100644
--- a/Documentation/rust/safety-standard/index.rst
+++ b/Documentation/rust/safety-standard/index.rst
@@ -157,6 +157,8 @@ The safety requirements have to be documented in the so called safety section::
         // ...
     }
 
+See requirements.rst for a full list of standardized safety requirements.
+
 .. _unsafe-Blocks:
 
 ``unsafe`` Blocks
@@ -208,6 +210,8 @@ are called safety requirements and need to be documented in the same way::
     /// <safety requirements>
     unsafe trait Foo {}
 
+See requirements.rst for a full list of standardized safety requirements.
+
 ``unsafe`` Impls
 ----------------
 
@@ -262,6 +266,7 @@ Further Pages
    examples
    guarantee
    type-invariants
+   requirements
 
 .. only::  subproject and html
 
diff --git a/Documentation/rust/safety-standard/requirements.rst b/Documentation/rust/safety-standard/requirements.rst
new file mode 100644
index 000000000000..b86bfb98179e
--- /dev/null
+++ b/Documentation/rust/safety-standard/requirements.rst
@@ -0,0 +1,80 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. highlight:: rust
+
+===================
+Safety Requirements
+===================
+
+There are many different kinds of safety requirements. The simplest example is the validity of raw
+pointers. But there is no limit to what they may require.
+
+Safety requirements are listed in an unordered markdown list in the ``# Safety`` section on
+``unsafe fn`` and ``unsafe trait`` items. Each list item should only contain a single requirement.
+The items should not specify the same requirement multiple times, especially not by expressing it in
+different terms.  The ``# Safety`` section should only consist of the list of safety requirements,
+if there is additional information, it should be documented in a different section.
+
+Common Safety Requirements
+==========================
+
++------------------------+---------------------+---------------------------------------------------+
+| Syntax                 | Meta Variables      | Meaning                                           |
+|                        |                     |                                                   |
++========================+=====================+===================================================+
+| ``ptr`` is valid for   |                     | Abbreviation for:                                 |
+| reads and writes.      |                     |                                                   |
+|                        | * ``ptr: *mut T``   | * ``ptr`` is valid for reads.                     |
+|                        |                     | * ``ptr`` is valid for writes.                    |
++------------------------+---------------------+---------------------------------------------------+
+| ``ptr`` is valid for   |                     | Abbreviation for:                                 |
+| reads.                 |                     |                                                   |
+|                        | * ``ptr: *const T`` | * ``ptr`` is valid for reads up to                |
+|                        |                     |   ``size_of::<T>()`` bytes for the duration of    |
+|                        |                     |   this function call.                             |
++------------------------+---------------------+---------------------------------------------------+
+| ``ptr`` is valid for   |                     | Abbreviation for:                                 |
+| writes.                |                     |                                                   |
+|                        | * ``ptr: *mut T``   | * ``ptr`` is valid for writes up to               |
+|                        |                     |   ``size_of::<T>()`` bytes for the duration of    |
+|                        |                     |   this function call.                             |
++------------------------+---------------------+---------------------------------------------------+
+| ``ptr`` is valid for   |                     | For the duration of ``'a``:                       |
+| reads up to ``size``   |                     |                                                   |
+| bytes for the duration | * ``ptr: *const T`` | * The pointer ``ptr`` is dereferenceable for      |
+| of ``'a``.             | * ``size: usize``   |   ``size`` bytes: all bytes with offset           |
+|                        |                     |   ``0..size`` have to be part of the same         |
+|                        |                     |   allocated object and it has to be alive.        |
+|                        |                     | * No concurrent write operation may occur to      |
+|                        |                     |   ``ptr`` at any offset between ``0..size``.      |
+|                        |                     | * The value at ``ptr`` is a valid instance of     |
+|                        |                     |   the type ``T``.                                 |
+|                        |                     |                                                   |
+|                        |                     | Additionally ``ptr`` must be:                     |
+|                        |                     |                                                   |
+|                        |                     | * non-null,                                       |
+|                        |                     | * aligned to ``align_of::<T>()`` i.e.             |
+|                        |                     |   ``ptr.addr() % align_of::<T>() == 0``.          |
++------------------------+---------------------+---------------------------------------------------+
+| ``ptr`` is valid for   |                     | For the duration of ``'a``:                       |
+| writes up to ``size``  |                     |                                                   |
+| bytes for the duration | * ``ptr: *mut T``   | * The pointer ``ptr`` is dereferenceable for      |
+| of ``'a``.             | * ``size: usize``   |   ``size`` bytes: all bytes with offset           |
+|                        |                     |   ``0..size`` have to be part of the same         |
+|                        |                     |   allocated object and it has to be alive.        |
+|                        |                     | * No concurrent read or write operation may occur |
+|                        |                     |   to ``ptr`` at any offset between ``0..size``.   |
+|                        |                     |                                                   |
+|                        |                     | Additionally ``ptr`` must be:                     |
+|                        |                     |                                                   |
+|                        |                     | * non-null,                                       |
+|                        |                     | * aligned to ``align_of::<T>()`` i.e.             |
+|                        |                     |   ``ptr.addr() % align_of::<T>() == 0``.          |
++------------------------+---------------------+---------------------------------------------------+
+
+
+Custom Safety Requirements
+==========================
+
+There are of course situations where the safety requirements listed above are insufficient. In that
+case the author can try to come up with their own safety requirement wording and ask the reviewers
+what they think. If the requirement is common enough, it should be added to the list above.
-- 
2.45.1



  parent reply	other threads:[~2024-07-17 22:12 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-17 22:12 [RFC PATCH 0/5] Introduce the Rust Safety Standard Benno Lossin
2024-07-17 22:12 ` [RFC PATCH 1/5] doc: rust: create safety standard Benno Lossin
2024-07-18  4:45   ` Greg KH
2024-07-24 19:13     ` Benno Lossin
2024-07-25  4:57       ` Greg KH
2024-07-18 12:20   ` Alice Ryhl
2024-07-24 19:36     ` Benno Lossin
2024-07-19 16:24   ` Daniel Almeida
2024-07-19 16:46     ` Alice Ryhl
2024-07-19 17:10     ` Danilo Krummrich
2024-07-19 18:09       ` Daniel Almeida
2024-07-19 19:20         ` Danilo Krummrich
2024-07-19 17:28     ` Miguel Ojeda
2024-07-19 18:18       ` Daniel Almeida
2024-07-19 17:56     ` Miguel Ojeda
2024-07-24 20:31     ` Benno Lossin
2024-07-24 21:20       ` Miguel Ojeda
2024-07-24 21:28         ` Benno Lossin
2024-08-08 13:01       ` Daniel Almeida
2024-08-08 13:08         ` Miguel Ojeda
2024-07-19 22:11   ` Boqun Feng
2024-07-24 22:01     ` Benno Lossin
2024-07-20  7:45   ` Dirk Behme
2024-07-17 22:12 ` [RFC PATCH 2/5] doc: rust: safety standard: add examples Benno Lossin
2024-07-19 16:28   ` Daniel Almeida
2024-07-19 16:36   ` Daniel Almeida
2024-07-25  7:47     ` Benno Lossin
2024-08-08 13:10       ` Daniel Almeida
2024-08-08 14:33         ` Benno Lossin
2024-07-17 22:12 ` [RFC PATCH 3/5] doc: rust: safety standard: add guarantees and type invariants Benno Lossin
2024-07-17 22:12 ` Benno Lossin [this message]
2024-07-17 22:13 ` [RFC PATCH 5/5] doc: rust: safety standard: add justifications Benno Lossin

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=20240717221133.459589-5-benno.lossin@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=corbet@lwn.net \
    --cc=gary@garyguo.net \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /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).