Skip to content
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

Get a slice of a GuestRegionMmap #286

Open
tylerfanelli opened this issue Mar 25, 2024 · 5 comments
Open

Get a slice of a GuestRegionMmap #286

tylerfanelli opened this issue Mar 25, 2024 · 5 comments

Comments

@tylerfanelli
Copy link

Between versions 0.8 and 0.13, the AsSlice trait was deprecated for GuestRegionMmap. For users of the as_slice method, is there a way to recreate its functionality? That is, is there any current alternatives for this method that can gives us a slice of a guest memory region?

@tylerfanelli
Copy link
Author

@roypat It seems that the AsSlice trait was removed here over concerns about taking slices of running VMs. In the project I use it for, we take the slice for VM creation, not from running VMs. Do you know of an alternative for the as_slice method?

@roypat
Copy link
Collaborator

roypat commented Apr 3, 2024

Hi @tylerfanelli,
sorry for the late response, I was on holiday over the Easter period. Generally, I would recommend VolatileSlice's methods/the Read/WriteVolatile traits for operating on guest memory. Alternatively, you could use as_ptr() and manually call std::slice::from_raw_parts to get a &[u8] instead, if you can prove in your application code that this is save to do.

@bonzini
Copy link
Member

bonzini commented Apr 3, 2024

I think AsMutSlice can be added back, but not AsSlice. The as_slice() method can be added as unsafe.

@tylerfanelli
Copy link
Author

@roypat No problem, thanks for the reply.

You would fetch this VolatileSlice using get_slice(), correct? With that I could copy the underlying memory to a normal slice with read_slice(), specifying the VolatileSlice's len() for the amount of bytes to read.

Do I have this right? If so, I don't think AsMutSlice nor AsSlice are required to be added back, especially if I'm the only one who asked for them.

@roypat
Copy link
Collaborator

roypat commented Apr 15, 2024

@tylerfanelli

You would fetch this VolatileSlice using get_slice(), correct? With that I could copy the underlying memory to a normal slice with read_slice(), specifying the VolatileSlice's len() for the amount of bytes to read.

Do I have this right?

Yes, that would allow you to copy data from the VolatileSlice into a byte buffer. For the other way around, you'd use write_slice.

@bonzini

I think AsMutSlice can be added back, but not AsSlice. The as_slice() method can be added as unsafe.

I suppose having unsafe as_slice and as_slice_mut functions that are very clear about their requirements (no usage after VM has started, no aliasing mutable slices to the same memory region) would be fine (although I'm not 100% sure how they'd work with the xen feature). The reason I deprecated a bunch of them in #217 was because the vm-memory crate itself can never use them, since it does not have any "global" information about aliasing slices or the state of the virtual machine (AsSlice itself was actually pub(crate), so it wasn't usable outside of vm-memory, which is why it got removed altogether).

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

No branches or pull requests

3 participants