Skip to content

Commit 7473f46

Browse files
Serban Iorgaalxiord
authored andcommitted
[mmap.rs] documentation improvements and fixes
Signed-off-by: Serban Iorga <seriorga@amazon.com>
1 parent 448291d commit 7473f46

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/mmap.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,9 @@
88
// Use of this source code is governed by a BSD-style license that can be
99
// found in the THIRD-PARTY file.
1010

11-
//! A default implementation of the GuestMemory trait by mmap()-ing guest's memory into the current
12-
//! process.
11+
//! The default implementation for the [`GuestMemory`](trait.GuestMemory.html) trait.
1312
//!
14-
//! The main structs to access guest's memory are:
15-
//! - [MmapRegion](struct.MmapRegion.html): mmap a continuous region of guest's memory into the
16-
//! current process
17-
//! - [GuestRegionMmap](struct.GuestRegionMmap.html): tracks a mapping of memory in the current
18-
//! process and the corresponding base address. It relays guest memory access requests to the
19-
//! underline [MmapRegion](struct.MmapRegion.html) object.
20-
//! - [GuestMemoryMmap](struct.GuestMemoryMmap.html): provides methods to access a collection of
21-
//! GuestRegionMmap objects.
13+
//! This implementation is mmap-ing the memory of the guest into the current process.
2214
2315
use std::borrow::Borrow;
2416
use std::error;
@@ -43,15 +35,25 @@ pub use crate::mmap_windows::MmapRegion;
4335
#[cfg(windows)]
4436
pub use std::io::Error as MmapRegionError;
4537

46-
// For MmapRegion
38+
/// Trait implemented by the underlying `MmapRegion`.
4739
pub(crate) trait AsSlice {
40+
/// Returns a slice corresponding to the data in the underlying `MmapRegion`.
41+
///
42+
/// # Safety
43+
///
44+
/// This is unsafe because of possible aliasing.
4845
unsafe fn as_slice(&self) -> &[u8];
4946

47+
/// Returns a mutable slice corresponding to the data in the underlying `MmapRegion`.
48+
///
49+
/// # Safety
50+
///
51+
/// This is unsafe because of possible aliasing.
5052
#[allow(clippy::mut_from_ref)]
5153
unsafe fn as_mut_slice(&self) -> &mut [u8];
5254
}
5355

54-
/// Errors that can happen when creating a memory map
56+
/// Errors that can occur when creating a memory map.
5557
#[derive(Debug)]
5658
pub enum Error {
5759
/// Adding the guest base address to the length of the underlying mapping resulted
@@ -91,6 +93,8 @@ impl error::Error for Error {}
9193

9294
// TODO: use this for Windows as well after we redefine the Error type there.
9395
#[cfg(unix)]
96+
/// Checks if a mapping of `size` bytes fits at the provided `file_offset`.
97+
///
9498
/// For a borrowed `FileOffset` and size, this function checks whether the mapping does not
9599
/// extend past EOF, and that adding the size to the file offset does not lead to overflow.
96100
pub fn check_file_offset(
@@ -113,16 +117,19 @@ pub fn check_file_offset(
113117
Ok(())
114118
}
115119

116-
/// Tracks a mapping of memory in the current process and the corresponding base address
117-
/// in the guest's memory space.
120+
/// [`GuestMemoryRegion`](trait.GuestMemoryRegion.html) implementation that mmaps the guest's
121+
/// memory region in the current process.
122+
///
123+
/// Represents a continuous region of the guest's physical memory that is backed by a mapping
124+
/// in the virtual address space of the calling process.
118125
#[derive(Debug)]
119126
pub struct GuestRegionMmap {
120127
mapping: MmapRegion,
121128
guest_base: GuestAddress,
122129
}
123130

124131
impl GuestRegionMmap {
125-
/// Create a new memory-mapped memory region for guest's physical memory.
132+
/// Create a new memory-mapped memory region for the guest's physical memory.
126133
pub fn new(mapping: MmapRegion, guest_base: GuestAddress) -> result::Result<Self, Error> {
127134
if guest_base.0.checked_add(mapping.len() as u64).is_none() {
128135
return Err(Error::InvalidGuestRegion);
@@ -350,7 +357,6 @@ impl GuestMemoryRegion for GuestRegionMmap {
350357
Some(self.mapping.as_mut_slice())
351358
}
352359

353-
/// Get the host virtual address corresponding to the region address.
354360
fn get_host_address(&self, addr: MemoryRegionAddress) -> guest_memory::Result<*mut u8> {
355361
// Not sure why wrapping_offset is not unsafe. Anyway this
356362
// is safe because we've just range-checked addr using check_address.
@@ -360,20 +366,27 @@ impl GuestMemoryRegion for GuestRegionMmap {
360366
}
361367
}
362368

363-
/// Tracks memory regions allocated/mapped for the guest in the current process.
369+
/// [`GuestMemory`](trait.GuestMemory.html) implementation that mmaps the guest's memory
370+
/// in the current process.
371+
///
372+
/// Represents the entire physical memory of the guest by tracking all its memory regions.
373+
/// Each region is an instance of `GuestRegionMmap`, being backed by a mapping in the
374+
/// virtual address space of the calling process.
364375
#[derive(Clone, Debug)]
365376
pub struct GuestMemoryMmap {
366377
regions: Vec<Arc<GuestRegionMmap>>,
367378
}
368379

369380
impl GuestMemoryMmap {
370381
/// Creates a container and allocates anonymous memory for guest memory regions.
382+
///
371383
/// Valid memory regions are specified as a slice of (Address, Size) tuples sorted by Address.
372384
pub fn new(ranges: &[(GuestAddress, usize)]) -> result::Result<Self, Error> {
373385
Self::with_files(ranges.iter().map(|r| (r.0, r.1, None)))
374386
}
375387

376388
/// Creates a container and allocates anonymous memory for guest memory regions.
389+
///
377390
/// Valid memory regions are specified as a sequence of (Address, Size, Option<FileOffset>)
378391
/// tuples sorted by Address.
379392
pub fn with_files<A, T>(ranges: T) -> result::Result<Self, Error>

0 commit comments

Comments
 (0)