Skip to content

Document MaybeUninit<T> inline assembly operands - #2428

Open
traviscross wants to merge 5 commits into
masterfrom
TC/asm-maybe-uninit
Open

traviscross wants to merge 5 commits into
masterfrom
TC/asm-maybe-uninit

Conversation

@traviscross

Copy link
Copy Markdown
Contributor

For inline assembly, we list the set of types that are allowed as operands. And, for each register class, we list the set of value types that can be used with it. And we say that input and output expressions of an inout operand must have the same type.

In Rust 1.74, we started allowing MaybeUninit<T> types in these operands (see rust-lang/rust#114790), treating them as T for the purpose of these rules.

But we didn't document that. Let's do so.

In the list of types allowed as inline assembly operands, some items
end with a period while others don't.  Let's add a trailing period to
the items missing one.
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 28, 2026
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. labels Nov 28, 2026
@traviscross

traviscross commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@Amanieu: What do you think? Look OK?

cc @rust-lang/lang

Comment thread src/inline-assembly.md Outdated
Comment on lines +912 to +927
// Passing a possibly uninitialized value through a register gives a
// value that is initialized. If the input was uninitialized, the
// result is an arbitrary value.
fn freeze(x: MaybeUninit<u64>) -> u64 {
let y: u64;
unsafe { asm!("/* {} */", inout(reg) x => y); }
y
}

assert_eq!(freeze(MaybeUninit::new(5)), 5);
let _: u64 = freeze(MaybeUninit::uninit());
# }
```

> [!NOTE]
> The rules stated in this chapter imply that implementing `freeze` via inline assembly is sound. See [asm.register-operands.maybe-uninit] and [asm.rules.reg-not-input]. But whether this is sound is an open question. See [RFC 3605].

@traviscross traviscross Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RalfJung: I particularly want to highlight this for your attention. It's not clear to me, given our other stated rules and our support for MaybeUnit<T> in inline assembly operands, how to avoid the implication that freeze must be sound. Thoughts?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that points to a gap in our asm docs. freeze violates the storytelling principle. assert_eq!(freeze(MaybeUninit::new(5)), 5) is wrong for the same reason that it is unsound to use an inline asm block to mutate memory behind a shared reference: there is a key assumption we place on all asm blocks, that when considering their effect on Rust-AM-visible state, they only do things that the Rust AM could already do without inline asm.

@traviscross traviscross Nov 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I agree with that principle and that model.

One thing I'd highlight is, while the model in your post discusses what assembly code makes observable to Rust, we must also, I think, consider what we make reliably observable from Rust to assembly code, since the author of a block of assembly code can rely on what we guarantee to be observable when arguing that the observable behavior of the assembly matches or refines that of the stand-in Rust code (i.e., that the assembly satisfies the postcondition it must uphold for soundness).

Consider, e.g.:

use core::arch::asm;
use core::hint::unreachable_unchecked;
use core::mem::MaybeUninit;
/// Returns zero, regardless of its input, and callers may rely on
/// this for soundness.
fn weird_zero(x: MaybeUninit<u64>) -> u64 {
    if cfg!(miri) { 0 } else {
        let r: u64;
        // (UN)SAFETY: If the language were to guarantee that both
        // registers contain the same value, then we would argue that
        // this code must return zero and thereby have equivalent
        // observable behavior to the stand-in Rust code.
        unsafe {
            asm!(
                "xor {a}, {b}",
                a = inout(reg) x => r,
                b = in(reg) x,
            );
        }
        r
    }
}

fn main() {
    if weird_zero(MaybeUninit::uninit()) != 0 {
        unsafe { unreachable_unchecked() };
    }
}

Presumably we wouldn't want to call that sound. But its story is OK. The only question is whether it can uphold it. And to prevent it from upholding its story, we have to limit what the assembly code can reliably observe.

I've revised and added a rule with such a limit. Please have a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I'd argue what happens here is: The uninit byte is refined by any concrete machine byte. The asm blocks receives concrete values that refine the AM value for each argument, and crucially, that choice is made independently for separate arguments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revised and added a rule with such a limit. Please have a look.

Which rule do you mean?
asm.register-operands.uninit-input looks reasonable, but there's still no rule really explaining why this does not let one write freeze. I can't tell if you're implicitly saying that this should be deferred to a future PR or if you consider that to be somehow explained by this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I'd argue what happens here is: The uninit byte is refined by any concrete machine byte. The asm blocks receives concrete values that refine the AM value for each argument, and crucially, that choice is made independently for separate arguments.

Agreed. This is similarly what I had done in the added rule:

r[asm.register-operands.uninit-input]
If any byte of the representation of an input is uninitialized, the allocated register contains an undefined value (see [asm.rules.reg-not-input]) at the start of the assembly code; an arbitrary value is chosen separately for each such input and for each execution of the assembly code.

It conservatively treats any uninitialized byte in the representation as making the entire value arbitrary.

I've revised and added a rule with such a limit. Please have a look.

Which rule do you mean? asm.register-operands.uninit-input looks reasonable, but there's still no rule really explaining why this does not let one write freeze.

Yes. My goal in this PR is to document MaybeUninit<T> types in inline assembly operands without making the situation any worse, e.g., by making clear why the weird_zero program above is unsound and by giving examples (arbitrary and zero) of the upper bound of what can be done soundly.

For making clear why we can't write freeze (and why the examples in this PR represent an upper bound), I think that requires adopting the storytelling principle, which I'd like to do in a separate PR, and FCP that.

@traviscross
traviscross marked this pull request as draft November 29, 2026 18:22
@rustbot rustbot removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 29, 2026
For inline assembly, we list the set of types that are allowed as
operands.  And, for each register class, we list the set of value
types that can be used with it.  And we say that input and output
expressions of an `inout` operand must have the same type.

In Rust 1.74, we started allowing `MaybeUninit<T>` types in these
operands (see rust-lang/rust#114790), treating them as `T` for the
purpose of these rules.

But we didn't document that.  Let's do so.
We support values of type `MaybeUninit<T>` as inputs to inline
assembly; these values may be uninitialized.

But we hadn't said what assembly code can observe when given an
uninitialized value.  The answer isn't entirely obvious, especially
given the `asm.rules.reg-not-input` rule, which discusses how
*undefined value* in the context of inline assembly means an arbitrary
but fixed value.

(Does it matter what the assembly code can observe if we constrain
what it makes observable to Rust?  Yes: the assembly code can use what
the language guarantees is observable to the assembly when proving
that its own observable behavior matches or refines that of proposed
stand-in Rust code.)

The answer, for now, must be that an arbitrary value is chosen
separately for each input and for each execution.  So let's add a rule
to that effect.
In the special types and traits chapter, we describe the language-
relevant behaviors of each listed type or trait.

For `MaybeUninit<T>`, let's add a note summarizing and pointing at the
rules related to using the type as an inline assembly operand.
Some inline assembly operands have special coercion behavior.  This
allows `&T` to be accepted (even though reference types are not
directly accepted), by coercion to `*const T`.  But `MaybeUninit<&T>`
is not accepted (even though `MaybeUninit<U>` is otherwise treated as
`U`).

To explain this, we need to document the special coercion behavior of
expressions used as inline assembly operands.  So let's do that.

These contexts are not general coercion sites (as the rules are
limited and special).  Still, these are coercion sites of a kind, so
let's add a note to our list of coercion sites and point to this rule.

And let's clarify, in our list of types acceptable for inline assembly
operands, that we mean only (thin) raw pointers (and not references).
@traviscross
traviscross marked this pull request as ready for review November 29, 2026 22:47
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants