Re: C aggregate passing (Rust kernel policy)

Alice Ryhl
2025-02-25T16:12:53Z

On Sun, Feb 23, 2025 at 4:30 PM Ventura Jack <venturajack85@gmail.com> wrote:
>
> Just to be clear and avoid confusion, I would
> like to clarify some aspects of aliasing.
> In case that you do not already know about this,
> I suspect that you may find it very valuable.
>
> I am not an expert at Rust, so for any Rust experts
> out there, please feel free to point out any errors
> or mistakes that I make in the following.
>
> The Rustonomicon is (as I gather) the semi-official
> documentation site for unsafe Rust.
>
> Aliasing in C and Rust:
>
> C "strict aliasing":
> - Is not a keyword.
> - Based on "type compatibility".
> - Is turned off by default in the kernel by using
> a compiler flag.
>
> C "restrict":
> - Is a keyword, applied to pointers.
> - Is opt-in to a kind of aliasing.
> - Is seldom used in practice, since many find
> it difficult to use correctly and avoid
> undefined behavior.
>
> Rust aliasing:
> - Is not a keyword.
> - Applies to certain pointer kinds in Rust, namely
> Rust "references".
> Rust pointer kinds:
> https://doc.rust-lang.org/reference/types/pointer.html
> - Aliasing in Rust is not opt-in or opt-out,
> it is always on.
> https://doc.rust-lang.org/nomicon/aliasing.html
> - Rust has not defined its aliasing model.
> https://doc.rust-lang.org/nomicon/references.html
> "Unfortunately, Rust hasn't actually
> defined its aliasing model.
> While we wait for the Rust devs to specify
> the semantics of their language, let's use
> the next section to discuss what aliasing is
> in general, and why it matters."
> There is active experimental research on
> defining the aliasing model, including tree borrows
> and stacked borrows.
> - The aliasing model not being defined makes
> it harder to reason about and work with
> unsafe Rust, and therefore harder to avoid
> undefined behavior/memory safety bugs.

I think all of this worrying about Rust not having defined its
aliasing model is way overblown. Ultimately, the status quo is that
each unsafe operation that has to do with aliasing falls into one of
three categories:

* This is definitely allowed.
* This is definitely UB.
* We don't know whether we want to allow this yet.

The full aliasing model that they want would eliminate the third
category. But for practical purposes you just stay within the first
subset and you will be happy.

Alice