-
Notifications
You must be signed in to change notification settings - Fork 34
Description
So, I've had a quick chat with @RalfJung about our padding bytes problem, and I think I now get a decent grasp of what we need in order to resolve that particular UB in abomonation.
Padding bytes are uninitialized memory, and we now have a safe way to model that in Rust, in the form of MaybeUninit. So we can take a first step towards handling them correctly today by casting &[T]
into &[MaybeUninit<u8>]
instead of &[u8]
.
This is enough to memcpy the bytes into another &mut [MaybeUninit<u8>]
slice. But it's not yet enough to expose our unintialized bytes to the outside world, e.g. for the purpose of sending them to Write
in encode()
and entomb()
, because Write
wants initialized bytes, not possibly uninitialized ones.
To resolve this, we need another language functionality, which is not available yet but frequently requested from the UCG: the freeze()
operation, a tool which can turn MaybeUninit<u8>
into a nondeterministic valid u8
value. You can think of it as a way to opt out of the UB of reading bad data and defer to hardware "whatever was lying around at that memory address" behavior.
IIUC, something like that was proposed a long time ago, but it was initially rejected by security-conscious people on the ground that it could be used to observe the value of uninitialized memory coming from malloc()
, which may leak sensitive information like cryptographic secrets which a process forgot to volatile-erase before calling free()
.
That precaution is commendable, but on the other hand, giving the growing body of evidence that an UB-free way to access specific regions of memory is needed for many use cases (from IPC with untrusted processes to implementation of certain low-overhead thread synchronization algorithms like seqlock), I'm hopeful that we're likely to get something like that in Rust eventually (and I will in fact take steps to make this discussion move forward once I'm done with my current UCG effort).
TL;DR: For now, this is blocked on a missing Rust feature, but the issue seems understood and is likely to be eventually resolved.