Skip to content

Conversation

roypat
Copy link
Member

@roypat roypat commented Sep 26, 2025

Summary of the PR

Following up on some discussions in #339, attempt to figure out how feasible it is to replace try_access() with get_slices() + functions in Rust's iterator trait.

cc @XanClic
cc @bonzini

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@roypat roypat force-pushed the deprecate-try-access branch 3 times, most recently from 92a0e25 to 59e3811 Compare September 27, 2025 07:04
Copy link
Collaborator

@XanClic XanClic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! You make it look easy.

Typo note on the first commit: Should be “range” instead of “rnage” in the title.

@roypat roypat force-pushed the deprecate-try-access branch 2 times, most recently from a9ecfa4 to e70475c Compare October 1, 2025 10:30
@roypat
Copy link
Member Author

roypat commented Oct 1, 2025

Changes v1 -> v2:

  • Add an sanity assert to get_slices() so that we uphold the doc statement about cumulative slice length even in the face of incorrect GuestMemoryRegion implementations (this simplifies the iterator chain in check_range())
  • Rename .partial() to .stop_on_error()
  • Change partial()/stop_on_error() to terminate iteration on all errors after at least one slice has been produced successfully (and if not even one slice can be returned, return an error from stop_on_error() instead).

@roypat roypat requested a review from XanClic October 1, 2025 10:34
@bonzini
Copy link
Member

bonzini commented Oct 1, 2025

@roypat sorry I messed up using the suggestion feature :/ if you squash my misguided commit, I'll approve.

@roypat roypat force-pushed the deprecate-try-access branch from dd33c70 to db00f74 Compare October 1, 2025 11:00
@roypat
Copy link
Member Author

roypat commented Oct 1, 2025

@roypat sorry I messed up using the suggestion feature :/ if you squash my misguided commit, I'll approve.

haha, no worries! squashed (and fixed a typo I noticed, "when if" -> "when it") :)

@bonzini
Copy link
Member

bonzini commented Oct 1, 2025

thread 'mmap::tests::read_to_and_write_from_mem' (1574) panicked at src/mmap/mod.rs:482:18:
called `Result::unwrap()` on an `Err` value: PartialBuffer { expected: 4, completed: 2 }

No idea why that would fail only under Miri... I cannot even reproduce it locally.

@roypat
Copy link
Member Author

roypat commented Oct 1, 2025

thread 'mmap::tests::read_to_and_write_from_mem' (1574) panicked at src/mmap/mod.rs:482:18:
called `Result::unwrap()` on an `Err` value: PartialBuffer { expected: 4, completed: 2 }

No idea why that would fail only under Miri... I cannot even reproduce it locally.

Argh, that must be the same thing as 029c652. Let me add a commit marking this one as cfg(not(miri)) as well

roypat added 4 commits October 1, 2025 13:04
Switching the check_range() implementation from try_access() to
get_slices() will mean that the test requires a working implementation
of get_slice(), which MockRegion does not have. So instead have the test
use the actual memory regions defined in the mmap module.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
Declare `len` as type `usize`, which reduce the number of casts between
usize and u64 (the variable is most often uses as a usize, and only once
needs to be u64).

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
Panic in get_slices() if the call to .get_slice() returns a slice that
does not match the `len` parameter passed to it. If this happens there
is an application bug somewhere in the GuestMemoryRegion implementation,
and the library code in vm-memory, while not doing anything unsound,
will probably react unpredictably to this situation.

This way, the final paragraph in the get_slices() documentatoin about
cumulative lengths is actually true.

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
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>
@roypat roypat force-pushed the deprecate-try-access branch 2 times, most recently from d4469c1 to 8b0d3c1 Compare October 1, 2025 12:05
Miri sometimes non-deterministically has its mocks for read(2)/write(2)
do only partial reads/writes, which our unittests cannot deal with. See
also commit 029c652 ("fix(test): do not try to read files under
miri"). Mark these test cases as cfg(not(miri)).

Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
@roypat roypat force-pushed the deprecate-try-access branch from 8b0d3c1 to 9c8fb09 Compare October 1, 2025 13:39
@XanClic
Copy link
Collaborator

XanClic commented Oct 1, 2025

Looks good to me, thanks!

(Is it common to use ntoskrnl.exe as a known-content file under Windows 😅?)

Copy link
Collaborator

@XanClic XanClic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is how I need to do it.

@roypat roypat linked an issue Oct 1, 2025 that may be closed by this pull request
@roypat
Copy link
Member Author

roypat commented Oct 1, 2025

(Is it common to use ntoskrnl.exe as a known-content file under Windows 😅?)

I have no idea, I haven't touched a windows system since high school haha

@bonzini
Copy link
Member

bonzini commented Oct 1, 2025

@roypat any objections to inviting Hanna to the repo so that her reviews count as approving?

@roypat roypat changed the title [RFC] Deprecate try_access() Deprecate try_access() Oct 1, 2025
@roypat
Copy link
Member Author

roypat commented Oct 1, 2025

@roypat any objections to inviting Hanna to the repo so that her reviews count as approving?

No concern whatsoever :)

@XanClic wanna open a PR to add yourself to CODEOWNERS and then I can get github and crates.io permissions set up?

@roypat roypat merged commit f059f7e into rust-vmm:main Oct 1, 2025
2 checks passed
@XanClic
Copy link
Collaborator

XanClic commented Oct 1, 2025

@roypat any objections to inviting Hanna to the repo so that her reviews count as approving?

No concern whatsoever :)

@XanClic wanna open a PR to add yourself to CODEOWNERS and then I can get github and crates.io permissions set up?

Sure! Thanks to @stefano-garzarella it’s not as pressing anymore, but I can take on some reviewing anyway.

XanClic added a commit to XanClic/vm-memory that referenced this pull request Oct 1, 2025
As discussed on PR rust-vmm#349, adding myself to the CODEOWNERS file to help
with reviews.

Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
@XanClic XanClic mentioned this pull request Oct 1, 2025
4 tasks
bonzini pushed a commit that referenced this pull request Oct 1, 2025
As discussed on PR #349, adding myself to the CODEOWNERS file to help
with reviews.

Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test GuestMemory::try_access error cases
4 participants