summary refs log tree commit diff
path: root/sys_util/src/guest_memory.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-05-30 18:31:02 -0700
committerCommit Bot <commit-bot@chromium.org>2019-06-04 20:29:25 +0000
commit127453d7eccdb6a903d0855fabb8f0935be90882 (patch)
tree65bd9f0b4c6b2a98c60bb2580949a93209c0f639 /sys_util/src/guest_memory.rs
parent6a0bfb037a109030b69feb9dec4a546548636940 (diff)
downloadcrosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar.gz
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar.bz2
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar.lz
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar.xz
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.tar.zst
crosvm-127453d7eccdb6a903d0855fabb8f0935be90882.zip
eliminate mut from non-mut references
This manifested itself in a couple places that were turning shared
memory buffers into slices for the purposes of passing these slices to
`Read` and `Write` trait methods.

However, this required the removal of the methods that took `Read` and
`Write` instances. This was a convenient interface but impossible to
implement safely because making slices from raw pointers without
enforcing safety guarantees causes undefined behaviour in Rust. It turns
out lots of code in crosvm was using these interfaces indirectly, which
explains why this CL touches so much.

TEST=crosvm run
BUG=chromium:938767

Change-Id: I4ff40c98da6ed08a4a42f4c31f0717f81b1c5863
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1636685
Reviewed-by: Zach Reizner <zachr@chromium.org>
Tested-by: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/guest_memory.rs')
-rw-r--r--sys_util/src/guest_memory.rs23
1 files changed, 8 insertions, 15 deletions
diff --git a/sys_util/src/guest_memory.rs b/sys_util/src/guest_memory.rs
index aad5b37..40ae925 100644
--- a/sys_util/src/guest_memory.rs
+++ b/sys_util/src/guest_memory.rs
@@ -6,7 +6,6 @@
 
 use std::ffi::CStr;
 use std::fmt::{self, Display};
-use std::io::{Read, Write};
 use std::os::unix::io::{AsRawFd, RawFd};
 use std::result;
 use std::sync::Arc;
@@ -432,7 +431,7 @@ impl GuestMemory {
         })
     }
 
-    /// Reads data from a readable object like a File and writes it to guest memory.
+    /// Reads data from a file descriptor and writes it to guest memory.
     ///
     /// # Arguments
     /// * `guest_addr` - Begin writing memory at this offset.
@@ -458,15 +457,12 @@ impl GuestMemory {
     /// #     Ok(rand_val)
     /// # }
     /// ```
-    pub fn read_to_memory<F>(
+    pub fn read_to_memory(
         &self,
         guest_addr: GuestAddress,
-        src: &mut F,
+        src: &AsRawFd,
         count: usize,
-    ) -> Result<()>
-    where
-        F: Read,
-    {
+    ) -> Result<()> {
         self.do_in_region(guest_addr, move |mapping, offset| {
             mapping
                 .read_to_memory(offset, src, count)
@@ -474,7 +470,7 @@ impl GuestMemory {
         })
     }
 
-    /// Writes data from memory to a writable object.
+    /// Writes data from memory to a file descriptor.
     ///
     /// # Arguments
     /// * `guest_addr` - Begin reading memory from this offset.
@@ -498,15 +494,12 @@ impl GuestMemory {
     /// #     Ok(())
     /// # }
     /// ```
-    pub fn write_from_memory<F>(
+    pub fn write_from_memory(
         &self,
         guest_addr: GuestAddress,
-        dst: &mut F,
+        dst: &AsRawFd,
         count: usize,
-    ) -> Result<()>
-    where
-        F: Write,
-    {
+    ) -> Result<()> {
         self.do_in_region(guest_addr, move |mapping, offset| {
             mapping
                 .write_from_memory(offset, dst, count)