Re: C aggregate passing (Rust kernel policy)

H. Peter Anvin
2025-02-25T18:16:43Z

On February 25, 2025 9:36:07 AM PST, Alice Ryhl <aliceryhl@google.com> wrote:
>On Tue, Feb 25, 2025 at 6:21 PM Ventura Jack <venturajack85@gmail.com> wrote:
>>
>> On Tue, Feb 25, 2025 at 9:12 AM Alice Ryhl <aliceryhl@google.com> wrote:
>> >
>> > 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
>>
>> Is there a specification for aliasing that defines your first bullet
>> point, that people can read and use, as a kind of partial
>> specification? Or maybe a subset of your first bullet point, as a
>> conservative partial specification? I am guessing that stacked
>> borrows or tree borrows might be useful for such a purpose.
>> But I do not know whether either of stacked borrows or tree
>> borrows have only false positives, only false negatives, or both.
>
>In general I would say read the standard library docs. But I don't
>know of a single resource with everything in one place.
>
>Stacked borrows and tree borrows are attempts at creating a full model
>that puts everything in the two first categories. They are not
>conservative partial specifications.
>
>> For Rust documentation, I have heard of the official
>> documentation websites at
>>
>> https://doc.rust-lang.org/book/
>> https://doc.rust-lang.org/nomicon/
>>
>> And various blogs, forums and research papers.
>>
>> If there is no such conservative partial specification for
>> aliasing yet, I wonder if such a conservative partial
>> specification could be made with relative ease, especially if
>> it is very conservative, at least in its first draft. Though there
>> is currently no specification of the Rust language and just
>> one major compiler.
>>
>> I know that Java defines an additional conservative reasoning
>> model for its memory model that is easier to reason about
>> than the full memory model, namely happens-before
>> relationship. That conservative reasoning model is taught in
>> official Java documentation and in books.
>
>On the topic of conservative partial specifications, I like the blog
>post "Tower of weakenings" from back when the strict provenance APIs
>were started, which I will share together with a quote from it:
>
>> Instead, we should have a tower of Memory Models, with the ones at the top being “what users should think about and try to write their code against”. As you descend the tower, the memory models become increasingly complex or vague but critically always more permissive than the ones above it. At the bottom of the tower is “whatever the compiler actually does” (and arguably “whatever the hardware actually does” in the basement, if you care about that).
>> https://faultlore.com/blah/tower-of-weakenings/
>
>You can also read the docs for the ptr module:
>https://doc.rust-lang.org/stable/std/ptr/index.html
>
>> On the topic of difficulty, even if there was a full specification,
>> it might still be difficult to work with aliasing in unsafe Rust.
>> For C "restrict", I assume that "restrict" is fully specified, and
>> C developers still typically avoid "restrict". And for unsafe
>> Rust, the Rust community helpfully encourages people to
>> avoid unsafe Rust when possible due to its difficulty.
>
>This I will not object to :)
>
>Alice
>
>

I do have to say one thing about the standards process: it forces a real specification to be written, as in a proper interface contract, including the corner cases (which of course may be "undefined", but the idea is that even what is out of scope is clear.)