Skip to content

Commit db00f74

Browse files
committed
deprecate GuestMemory::try_access()
Deprecate 'GuestMemory::try_access()` in favor of the external iterator provided by `GuestMemory::get_slices()`. Rewrite all internal usages of `try_access()` in terms of `get_slices()`. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent 3494d42 commit db00f74

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
- \[[#339](https://github.com/rust-vmm/vm-memory/pull/339)\] Fix `Bytes::read()` and `Bytes::write()` not to ignore `try_access()`'s `count` parameter
4141

42+
### Deprecated
43+
44+
- \[[#349](https://github.com/rust-vmm/vm-memory/pull/349)\] Deprecate `GuestMemory::try_access()`. Use `GuestMemory::get_slices()` instead.
45+
4246
## \[v0.16.1\]
4347

4448
### Added

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 91.78,
2+
"coverage_score": 90.82,
33
"exclude_path": "mmap_windows.rs",
44
"crate_features": "backend-mmap,backend-atomic,backend-bitmap"
55
}

src/bitmap/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,10 @@ pub(crate) mod tests {
288288

289289
// Finally, let's invoke the generic tests for `Bytes`.
290290
let check_range_closure = |m: &M, start: usize, len: usize, clean: bool| -> bool {
291-
let mut check_result = true;
292-
m.try_access(len, GuestAddress(start as u64), |_, size, reg_addr, reg| {
293-
if !check_range(&reg.bitmap(), reg_addr.0 as usize, size, clean) {
294-
check_result = false;
295-
}
296-
Ok(size)
291+
m.get_slices(GuestAddress(start as u64), len).all(|r| {
292+
let slice = r.unwrap();
293+
check_range(slice.bitmap(), 0, slice.len(), clean)
297294
})
298-
.unwrap();
299-
300-
check_result
301295
};
302296

303297
test_bytes(

src/guest_memory.rs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,9 @@ pub trait GuestMemory {
355355

356356
/// Check whether the range [base, base + len) is valid.
357357
fn check_range(&self, base: GuestAddress, len: usize) -> bool {
358-
match self.try_access(len, base, |_, count, _, _| -> Result<usize> { Ok(count) }) {
359-
Ok(count) => count == len,
360-
_ => false,
361-
}
358+
// get_slices() ensures that if no error happens, the cumulative length of all slices
359+
// equal `len`.
360+
self.get_slices(base, len).all(|r| r.is_ok())
362361
}
363362

364363
/// Returns the address plus the offset if it is present within the memory of the guest.
@@ -376,6 +375,10 @@ pub trait GuestMemory {
376375
/// - the error code returned by the callback 'f'
377376
/// - the size of the already handled data when encountering the first hole
378377
/// - the size of the already handled data when the whole range has been handled
378+
#[deprecated(
379+
since = "0.17.0",
380+
note = "supplemented by external iterator `get_slices()`"
381+
)]
379382
fn try_access<F>(&self, count: usize, addr: GuestAddress, mut f: F) -> Result<usize>
380383
where
381384
F: FnMut(usize, usize, MemoryRegionAddress, &Self::R) -> Result<usize>,
@@ -533,6 +536,17 @@ impl<'a, M: GuestMemory + ?Sized> GuestMemorySliceIterator<'a, M> {
533536
)
534537
}))
535538
}
539+
540+
/// Adapts this [`GuestMemorySliceIterator`] to return `None` (e.g. gracefully terminate)
541+
/// when it encounters an error after successfully producing at least one slice.
542+
/// Return an error if requesting the first slice returns an error.
543+
pub fn stop_on_error(self) -> Result<impl Iterator<Item = VolatileSlice<'a, MS<'a, M>>>> {
544+
let mut peek = self.peekable();
545+
if let Some(err) = peek.next_if(Result::is_err) {
546+
return Err(err.unwrap_err());
547+
}
548+
Ok(peek.filter_map(Result::ok))
549+
}
536550
}
537551

538552
impl<'a, M: GuestMemory + ?Sized> Iterator for GuestMemorySliceIterator<'a, M> {
@@ -562,23 +576,15 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
562576
type E = Error;
563577

564578
fn write(&self, buf: &[u8], addr: GuestAddress) -> Result<usize> {
565-
self.try_access(
566-
buf.len(),
567-
addr,
568-
|offset, count, caddr, region| -> Result<usize> {
569-
region.write(&buf[offset..(offset + count)], caddr)
570-
},
571-
)
579+
self.get_slices(addr, buf.len())
580+
.stop_on_error()?
581+
.try_fold(0, |acc, slice| Ok(acc + slice.write(&buf[acc..], 0)?))
572582
}
573583

574584
fn read(&self, buf: &mut [u8], addr: GuestAddress) -> Result<usize> {
575-
self.try_access(
576-
buf.len(),
577-
addr,
578-
|offset, count, caddr, region| -> Result<usize> {
579-
region.read(&mut buf[offset..(offset + count)], caddr)
580-
},
581-
)
585+
self.get_slices(addr, buf.len())
586+
.stop_on_error()?
587+
.try_fold(0, |acc, slice| Ok(acc + slice.read(&mut buf[acc..], 0)?))
582588
}
583589

584590
/// # Examples
@@ -642,9 +648,11 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
642648
where
643649
F: ReadVolatile,
644650
{
645-
self.try_access(count, addr, |_, len, caddr, region| -> Result<usize> {
646-
region.read_volatile_from(caddr, src, len)
647-
})
651+
self.get_slices(addr, count)
652+
.stop_on_error()?
653+
.try_fold(0, |acc, slice| {
654+
Ok(acc + slice.read_volatile_from(0, src, slice.len())?)
655+
})
648656
}
649657

650658
fn read_exact_volatile_from<F>(
@@ -670,11 +678,14 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
670678
where
671679
F: WriteVolatile,
672680
{
673-
self.try_access(count, addr, |_, len, caddr, region| -> Result<usize> {
674-
// For a non-RAM region, reading could have side effects, so we
675-
// must use write_all().
676-
region.write_all_volatile_to(caddr, dst, len).map(|()| len)
677-
})
681+
self.get_slices(addr, count)
682+
.stop_on_error()?
683+
.try_fold(0, |acc, slice| {
684+
// For a non-RAM region, reading could have side effects, so we
685+
// must use write_all().
686+
slice.write_all_volatile_to(0, dst, slice.len())?;
687+
Ok(acc + slice.len())
688+
})
678689
}
679690

680691
fn write_all_volatile_to<F>(&self, addr: GuestAddress, dst: &mut F, count: usize) -> Result<()>

0 commit comments

Comments
 (0)