Skip to content

virtio-queue: add verify_add_used and stub memory region #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

priyasiddharth
Copy link

@priyasiddharth priyasiddharth commented Jun 18, 2025

Summary of the PR

virtio-queue: add verification for add_used operation

Kani proofs like verify_add_used do not finish in practical time if we use the production memory region. So we implement a stub region with a simple vector backing it. This will help subsequent proofs work and also enable stateful proofs.

Note that unsafe code is added only for StubRegion that will run in Kani.
Kani verifies all unsafe accesses do not cause undefined behaviour (in the context of unit proof execution).

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.

@priyasiddharth

This comment was marked as outdated.

@priyasiddharth priyasiddharth force-pushed the verify_add_used branch 4 times, most recently from 96d3228 to 89e9614 Compare July 29, 2025 15:15
@priyasiddharth priyasiddharth changed the title verify add_used operation using guestmemory stubs unit proof for add_used operation Aug 5, 2025
@priyasiddharth priyasiddharth marked this pull request as ready for review August 5, 2025 15:24
@MatiasVara
Copy link
Contributor

LGTM!

Copy link
Member

@stefano-garzarella stefano-garzarella left a comment

Choose a reason for hiding this comment

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

Please for the commit description, follow the style of other commits, using the crate as prefix, something like this:

virtio-queue: add unit proof for add_used method (sec 2.8.22)

@@ -14,11 +290,11 @@ use super::*;
/// meaning we can use `kani::unwind(0)` instead of `kani::unwind(2)`. Functionally,
/// it works identically to `GuestMemoryMmap` with only a single contained region.
pub struct SingleRegionGuestMemory {
the_region: vm_memory::GuestRegionMmap,
the_region: StubRegion,
Copy link
Member

Choose a reason for hiding this comment

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

I'd split this patch in 2 patches, one where you introduce StubRegion (explaining why we need to do that) using it in the existing proofs and another patch where you add the new proof.

Copy link
Contributor

@MatiasVara MatiasVara Aug 6, 2025

Choose a reason for hiding this comment

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

I can add that we needed to rely on a StubRegion otherwise Kani won't finish if GuestRegionMmap is used. I agree that this needs to be explained.

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

@priyasiddharth I meant 2 patches in this PR, why removing the other changes from this PR?

Copy link
Author

Choose a reason for hiding this comment

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

fixed


/// Kani proof harness for verifying the behavior of the `add_used` method of the `Queue`.
///
/// # Specification (VirtIO 1.3, Section 2.8.22: "Receiving Used Buffers From The Device")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think section 2.8 is for packed virtqueues whereas here we are only dealing with split virtqueues. I think the section should be 2.7.14 (see https://docs.oasis-open.org/virtio/virtio/v1.3/csd01/virtio-v1.3-csd01.html#x1-7100014)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I could not find what the spec says about using a descriptor id that is out of bonds. I think that the spec may let the implementation decide that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think what we try to enforce here is 2.7.8.2 Device Requirements: The Virtqueue Used Ring. I think we may need a comment like in firecraker in which we state all the things we do not verify, e.g., the order in which the used-ring indexes are updated.

Copy link
Author

Choose a reason for hiding this comment

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

We are not enforcing any of the following in the current proof therefore I don't think 2.7.8.2 applies:

  1. The device MUST set len prior to updating the used idx.
  2. The device MUST write at least len bytes to descriptor, beginning at the first device-writable buffer, prior to updating the used idx.
  3. The device MAY write more than len bytes to descriptor. Note: There are potential error cases where a device might not know what parts of the buffers have been written. This is why len is permitted to be an underestimate: that’s preferable to the driver believing that uninitialized memory has been overwritten when it has not.

/// When the device has finished processing a buffer, it must add an element to the used ring,
/// indicating which descriptor chain was used and how many bytes were written. The device must
/// increment the used index after writing the element. If the descriptor index is out of bounds,
/// the operation must fail and the used index must not be incremented.
Copy link
Contributor

@MatiasVara MatiasVara Aug 7, 2025

Choose a reason for hiding this comment

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

I think this last sentence is not part of the specification. There is no requirement about what to do when the index in the descriptor is out of bonds. I think it is a decision of the implementation what to do in this case.

Copy link
Author

Choose a reason for hiding this comment

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

closing as not relevant to this PR

Copy link
Member

Choose a reason for hiding this comment

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

@priyasiddharth this means that we will lose all the comments of that code, so we need to review again the same changes. Why you removed that changes completely from this PR?

Maybe I was unclear, but I meant just to split the changes in multiple patches but still in the same PR.

Copy link
Author

Choose a reason for hiding this comment

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

Ok I will add it back. I interpreted the word patch to mean different PRs but perhaps you meant commit: #346 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

@priyasiddharth sorry, just to be clear, in my mind commit = patch + description.
PR (or series) is a set of commits/patches.

Copy link
Author

Choose a reason for hiding this comment

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

I clarified in the function doc

assert_eq!(queue.next_used, used_idx);
assert!(used_desc_table_index >= queue.size());
// The old value should still be the same as before the add_used call.
assert_eq!(old_val, get_used_idx(&queue, &memory).unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think different that the proof in firecraker (see https://sourcegraph.com/github.com/firecracker-microvm/firecracker/-/blob/src/vmm/src/devices/virtio/queue.rs?L982) we are checking that the memory values has not changed, right?

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand the link. But to answer your question, firecracker proof does not check guestmemory (because of path explosion), we add this extra check cause kani seems to be able to handle it.

Kani proofs do not finish in practical time if we use the production
memory region. So we implement a stub region with a simple vector backing it.
This will help subsequent proofs work and also enable stateful
proofs.

Signed-off-by: Siddharth Priya <[email protected]>
@priyasiddharth priyasiddharth reopened this Aug 7, 2025
@priyasiddharth priyasiddharth changed the title unit proof for add_used operation virtio-queue: add stub memory region for kani proofs Aug 7, 2025
@priyasiddharth priyasiddharth changed the title virtio-queue: add stub memory region for kani proofs virtio-queue: add verify_add_used and stub memory region Aug 8, 2025
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.

4 participants