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_gpa_access_state: avoid returning dangling reference #163

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions mshv-ioctls/src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl VmFd {
base_pfn: u64,
nr_pfns: u32,
flags: u64,
) -> Result<mshv_get_gpa_pages_access_state> {
) -> Result<Vec<hv_gpa_page_access_state>> {
let mut states: Vec<hv_gpa_page_access_state> =
vec![hv_gpa_page_access_state { as_uint8: 0 }; nr_pfns as usize];
let mut gpa_pages_access_state: mshv_get_gpa_pages_access_state =
Expand All @@ -664,7 +664,7 @@ impl VmFd {
)
};
if ret == 0 {
Ok(gpa_pages_access_state)
Ok(states)
} else {
Err(errno::Error::last().into())
}
Expand Down Expand Up @@ -707,11 +707,7 @@ impl VmFd {
current_size = cmp::min(PAGE_ACCESS_STATES_BATCH_SIZE, remaining);
let page_states =
self.get_gpa_access_state(base_pfn + processed as u64, current_size, flags)?;
// SAFETY: we're sure states and count meet the requirements for from_raw_parts
let slices: &[hv_gpa_page_access_state] = unsafe {
std::slice::from_raw_parts(page_states.states, page_states.count as usize)
};
for item in slices.iter() {
for item in page_states.iter() {
let bits = &mut bitmap[bitmap_index];
mask = 1 << bit_index;
// SAFETY: access union field
Expand All @@ -723,7 +719,9 @@ impl VmFd {
bitmap_index = processed / 64;
bit_index = processed % 64;
}
remaining -= page_states.count;
// There is no risk of overflow on this cast, since
// page_states.len() is at most PAGE_ACCESS_STATES_BATCH_SIZE
remaining -= page_states.len() as u32;
}
Ok(bitmap)
}
Expand Down
Loading