summary refs log tree commit diff
path: root/sys_util/src/mmap.rs
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2019-04-19 14:09:37 -0700
committerchrome-bot <chrome-bot@chromium.org>2019-04-22 12:28:08 -0700
commit622788fb46b62a59bf39163f690f1ac99a5aee06 (patch)
tree888e3bcf7f77eeb21defcd46320954ee7054c2d6 /sys_util/src/mmap.rs
parente9c4383764c5657f3389c68c8039d39e5a519d54 (diff)
downloadcrosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar.gz
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar.bz2
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar.lz
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar.xz
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.tar.zst
crosvm-622788fb46b62a59bf39163f690f1ac99a5aee06.zip
sys_util: add size to mmap InvalidRange error
The current error doesn't provide sufficient information to debug
InvalidRange errors; add the size of the region so that the bounds of
the comparison can be determined from the error message.

BUG=None
TEST=cargo test -p sys_util

Change-Id: I8e7fbd750ab84c43bbf0435230b7d3cf466783da
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1574964
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'sys_util/src/mmap.rs')
-rw-r--r--sys_util/src/mmap.rs16
1 files changed, 8 insertions, 8 deletions
diff --git a/sys_util/src/mmap.rs b/sys_util/src/mmap.rs
index d094ba3..f5df598 100644
--- a/sys_util/src/mmap.rs
+++ b/sys_util/src/mmap.rs
@@ -30,7 +30,7 @@ pub enum Error {
     /// Overlapping regions
     Overlapping(usize, usize),
     /// Requested memory range spans past the end of the region.
-    InvalidRange(usize, usize),
+    InvalidRange(usize, usize, usize),
     /// Couldn't read from the given source.
     ReadFromSource(std::io::Error),
     /// `mmap` returned the given error.
@@ -55,10 +55,10 @@ impl Display for Error {
                 "requested memory range overlaps with existing region: offset={} size={}",
                 offset, count
             ),
-            InvalidRange(offset, count) => write!(
+            InvalidRange(offset, count, region_size) => write!(
                 f,
-                "requested memory range spans past the end of the region: offset={} count={}",
-                offset, count,
+                "requested memory range spans past the end of the region: offset={} count={} region_size={}",
+                offset, count, region_size,
             ),
             ReadFromSource(e) => write!(f, "failed to read from the given source: {}", e),
             SystemCallFailed(e) => write!(f, "mmap system call failed: {}", e),
@@ -450,7 +450,7 @@ impl MemoryMapping {
     {
         let mem_end = self
             .range_end(mem_offset, count)
-            .map_err(|_| Error::InvalidRange(mem_offset, count))?;
+            .map_err(|_| Error::InvalidRange(mem_offset, count, self.size()))?;
         unsafe {
             // It is safe to overwrite the volatile memory.  Acessing the guest
             // memory as a mutable slice is OK because nothing assumes another
@@ -489,7 +489,7 @@ impl MemoryMapping {
     {
         let mem_end = self
             .range_end(mem_offset, count)
-            .map_err(|_| Error::InvalidRange(mem_offset, count))?;
+            .map_err(|_| Error::InvalidRange(mem_offset, count, self.size()))?;
         unsafe {
             // It is safe to read from volatile memory.  Acessing the guest
             // memory as a slice is OK because nothing assumes another thread
@@ -504,7 +504,7 @@ impl MemoryMapping {
     /// to the pages in the range will return zero bytes.
     pub fn remove_range(&self, mem_offset: usize, count: usize) -> Result<()> {
         self.range_end(mem_offset, count)
-            .map_err(|_| Error::InvalidRange(mem_offset, count))?;
+            .map_err(|_| Error::InvalidRange(mem_offset, count, self.size()))?;
         let ret = unsafe {
             // madvising away the region is the same as the guest changing it.
             // Next time it is read, it may return zero pages.
@@ -515,7 +515,7 @@ impl MemoryMapping {
             )
         };
         if ret < 0 {
-            Err(Error::InvalidRange(mem_offset, count))
+            Err(Error::InvalidRange(mem_offset, count, self.size()))
         } else {
             Ok(())
         }