Repository navigation
Document MaybeUninit<T> inline assembly operands - #2428
traviscross wants to merge 5 commits into
Conversation
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.
|
@Amanieu: What do you think? Look OK? cc @rust-lang/lang |
| // 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]. |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The way I'd argue what happens here is: The
uninitbyte 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.
1dbd67e to
509c22c
Compare
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.
509c22c to
6a9037f
Compare
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).
6a9037f to
ad5ab43
Compare
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
inoutoperand 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 asTfor the purpose of these rules.But we didn't document that. Let's do so.