-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AsRef impl for DumbMapping #154
Conversation
In softbuffer's DRM implementation, we need to have a way to get a [u8] slice for our Deref implementation. Right now, we just create an entire buffer of zeroes and read from that. As that is unidiomatic, this PR adds an AsRef impl to DumbMapping to get a reference to the underlying memory. See: rust-windowing/softbuffer#135 (comment) Signed-off-by: John Nunley <dev@notgull.net>
UB question: is the mapped memory for this |
It may be uninitialized... but in that case the |
Assuming one can only make a |
Memory allocations by the OS should generally always be zeroed, for security, so an application can't read sensitive information from a different application. I think the same should apply for dumb buffers? Regardless, if the OS returned arbitrary memory, I don't think that would be "uninitialized" in the sense of compiler undefined behavior. It won't be annotated as uninitialized in LLVM IR, and the compiler can't distinguish it from other memory obtained through FFI that's been "initialized". |
Ah, I see. In that case, it would definitely be dependent on how the kernel implements But you are right, I don't think LLVM IR would annotate a That said, would it be a good idea to add a |
Yeah, whether or not the kernel has zeroed the memory isn't really relevant to whether or not it's "uninitialized" . What matters is whether or not LLVM's optimizer considers it such, and it's not going to try to analyze whether or not the memory allocated by a system call or dynamic library call is uninitialized. (Except calls made though the language's own allocator.) And good point. It makes sense to offer |
Hey @notgull - Would you be able to add a |
This also adds Borrow impls, which aren't as necessary, but are nice to have. Signed-off-by: John Nunley <dev@notgull.net>
Done! |
Looks good. Just merged |
Yes, my concern was with the existing API as well.
That's a poor assumption to make (in general): just because there are cases where the developer knows the memory is initialized doesn't mean it is always initialized and shouldn't drive the API in such a way. It's in the name of the type (Those still being unstable is probably good enough of a reason to just skip this for now...)
Is It's been a while, but perhaps these might still be relevant: https://docs.rs/presser/latest/presser/ |
Normally kernels are not even compiled with support for But I don't believe it actually matters either way, for safety. The memory isn't allocated by LLVM, so optimizations related to uninitialized memory don't apply. LLVM doesn't know if we've created an anonymous mmap or an mmap of a file, and it doesn't care as long as the memory isn't mutated while the reference to it exists. And mapping in new memory pages, regardless of there content, isn't mutation in the relevant sense, since it's not observable to userspace. |
Though I think there is a different soundness issue possible with |
This isn't about mutation, it's about possibly reading uninitialized bytes, which is considered undefined behaviour? |
"Uninitialized memory" isn't actually a concept that exists at the hardware-level, so what matters is whether or not the compiler understands the memory to be uninitialized. Which applies to stack and heap memory it has allocated but not initialized, but shouldn't apply to memory from a dynamic library or assembly (that isn't being invoked by the compiler's own allocator). |
Is there a reason we would consider reading mapped memory given to us for a dumb buffer UB but not reading mapped memory for, lets say, a regular file? In my mind, they can both be considered
Yes, in the general case that's absolutely true, especially when it comes to memory allocation. But I don't think it's a poor assumption to make in the case of IO though. Programs should be able to assume that file operations are going to behave sanely. Honestly the idea of using Now that it's been brought up, I am a bit more concerned about the aliasing/mutability issues of having the memory mapped with |
@Slabity reading fixed byte ranges from a regular file is something completely different from mapping a range of bytes of "unknown origin". In my eyes |
Different in what way? They both have the same soundness issues of never knowing the contents of the mapped region with certainty even after being written to (at least with
Intended? No idea. But a |
In softbuffer's DRM implementation, we need to have a way to get a [u8] slice for our Deref implementation. Right now, we just create an entire buffer of zeroes and read from that. As that is unidiomatic, this PR adds an AsRef impl to DumbMapping to get a reference to the underlying memory.
See:
rust-windowing/softbuffer#135 (comment)