From 622788fb46b62a59bf39163f690f1ac99a5aee06 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Fri, 19 Apr 2019 14:09:37 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/1574964 Commit-Ready: ChromeOS CL Exonerator Bot Tested-by: kokoro Reviewed-by: Zach Reizner --- sys_util/src/mmap.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'sys_util/src/mmap.rs') 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(()) } -- cgit 1.4.1