Skip to content

region: document GuestMemoryRegion::len() non-zero invariant#382

Open
Aakash456 wants to merge 1 commit into
rust-vmm:mainfrom
Aakash456:fix-issue-380-document-len-invariant
Open

region: document GuestMemoryRegion::len() non-zero invariant#382
Aakash456 wants to merge 1 commit into
rust-vmm:mainfrom
Aakash456:fix-issue-380-document-len-invariant

Conversation

@Aakash456

Copy link
Copy Markdown

Summary

Documents that GuestMemoryRegion::len() must always return a non-zero value, since last_addr()'s default implementation computes self.len() - 1 and would otherwise underflow.

This invariant was previously enforced implicitly for built-in mmap-backed regions but undocumented for the public GuestMemoryRegion trait, which custom backends also implement.

Changes

  • Added a # Invariant section to the doc comment on GuestMemoryRegion::len()
  • Added a debug_assert! in last_addr() to fail fast in debug builds if a custom implementation violates the invariant
  • Corrected the existing comment on the unchecked_add call, which previously implied the safety guarantee holds universally — it only held for the built-in mmap region, not for arbitrary custom implementations

Fixes #380

Happy to adjust scope if you'd prefer this as doc-only without the debug_assert, or if a hard assert! is preferred over debug_assert! given this is a safety-adjacent invariant.

Custom GuestMemoryRegion implementations must return a non-zero
length from len(), since last_addr() computes self.len() - 1 and
would otherwise underflow. This was previously enforced implicitly
for built-in mmap-backed regions but undocumented for the public
trait. Also adds a debug_assert in last_addr() to catch violations
early, and corrects the now-inaccurate safety comment on the
unchecked_add call.

Fixes rust-vmm#380

Signed-off-by: Aakash Chaudhary <aakashchoudhary66@gmail.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.

Clarify that custom GuestMemoryRegion implementations must not return zero length

1 participant