Skip to content

Commit

Permalink
test_all_vp_state_components_copy_from_buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Muminul Islam <[email protected]>
  • Loading branch information
russell-islam committed Apr 6, 2024
1 parent d1c5e0e commit afac11e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions mshv-bindings/src/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,36 @@ impl AllVpStateComponents {
mod tests {
use super::*;
use random_number::random;
use std::slice::{from_raw_parts, from_raw_parts_mut};

impl Buffer {
/// Converts a mutable reference to `self` into a mutable slice of bytes.
///
/// Because the slice is made from a reference to `self`, mutations to the returned slice are
/// immediately reflected in `self`. The value of bytes in the returned slice will depend on
/// the representation of the type in memory, and may change in an unstable fashion.
fn as_mut_slice(&mut self) -> &mut [u8] {
// SAFETY: Safe because the entire size of self is accessible as bytes because the trait
// guarantees it. The trait also guarantees that any combination of bytes is valid for this
// type, so modifying them in the form of a byte slice is valid. The lifetime of the
// returned slice is the same as the passed reference, so that no dangling pointers will
// result from this pointer alias. Although this does alias a mutable pointer, we do so by
// exclusively borrowing the given mutable reference.
unsafe { from_raw_parts_mut(self.buf as *mut Self as *mut u8, self.layout.size()) }
}

/// Converts a reference to `self` into a slice of bytes.
///
/// The value of `self` is not copied. Instead, the slice is made from a reference to `self`.
/// The value of bytes in the returned slice will depend on the representation of the type in
/// memory, and may change in an unstable fashion.
fn as_slice(&self) -> &[u8] {
// SAFETY: Safe because the entire size of self is accessible as bytes because the trait
// guarantees it. The lifetime of the returned slice is the same as the passed reference,
// so that no dangling pointers will result from this pointer alias.
unsafe { from_raw_parts(self.buf as *const Self as *const u8, self.layout.size()) }
}
}

#[test]
fn test_all_vp_state_components_copy_to_buffer() {
Expand All @@ -774,4 +804,32 @@ mod tests {
.all(|(a, b)| a == b));
}
}

#[test]
fn test_all_vp_state_components_copy_from_buffer() {
let mut states: AllVpStateComponents = AllVpStateComponents::new();
let mut buffer = Buffer::new(HV_PAGE_SIZE, HV_PAGE_SIZE).unwrap();
let copy_buffer = Buffer::new(HV_PAGE_SIZE, HV_PAGE_SIZE).unwrap();

let mut_buf = buffer.as_mut_slice();
for i in 0..HV_PAGE_SIZE {
mut_buf[i] = random!();
}

// SAFETY: buffer is large enough to hold state data
unsafe { ptr::copy(mut_buf.as_mut_ptr(), copy_buffer.buf, HV_PAGE_SIZE) };

//test copy to buffer
for i in 0..MSHV_VP_STATE_COUNT {
let start = get_comp_start_offet(i as usize);
let end = get_comp_start_offet((i + 1) as usize);
let len = VP_STATE_COMP_SIZES[i as usize];
states.copy_from_buffer(i as usize, &copy_buffer);
let buf_arr = &mut_buf[0..len];
assert!(states.buffer[start..end]
.iter()
.zip(buf_arr)
.all(|(a, b)| a == b));
}
}
}

0 comments on commit afac11e

Please sign in to comment.