qemu: add cpu page_size call (#1433)

* qemu handy cpu page size call proposal.

* changes from feedback.
This commit is contained in:
David CARLIER 2023-08-23 19:27:58 +01:00 committed by GitHub
parent 8f27b14eb8
commit d338b30c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -120,6 +120,7 @@ pub fn generate(
.allowlist_function("syx_snapshot_root_restore") .allowlist_function("syx_snapshot_root_restore")
.allowlist_function("syx_snapshot_dirty_list_add") .allowlist_function("syx_snapshot_dirty_list_add")
.allowlist_function("device_list_all") .allowlist_function("device_list_all")
.allowlist_function("qemu_target_page_size")
.blocklist_function("main_loop_wait") // bindgen issue #1313 .blocklist_function("main_loop_wait") // bindgen issue #1313
.parse_callbacks(Box::new(bindgen::CargoCallbacks)); .parse_callbacks(Box::new(bindgen::CargoCallbacks));

View File

@ -11100,6 +11100,12 @@ impl ::std::ops::BitAndAssign for qemu_plugin_mem_rw {
self.0 &= rhs.0; self.0 &= rhs.0;
} }
} }
extern "C" {
#[doc = " qemu_target_page_size - return the target's page size"]
pub fn qemu_target_page_size() -> usize;
}
#[repr(transparent)] #[repr(transparent)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct qemu_plugin_mem_rw(pub ::std::os::raw::c_uint); pub struct qemu_plugin_mem_rw(pub ::std::os::raw::c_uint);

View File

@ -7,12 +7,16 @@ use core::{
mem::MaybeUninit, mem::MaybeUninit,
ptr::{addr_of, copy_nonoverlapping, null}, ptr::{addr_of, copy_nonoverlapping, null},
}; };
use std::{cell::OnceCell, slice::from_raw_parts, str::from_utf8_unchecked};
#[cfg(emulation_mode = "systemmode")] #[cfg(emulation_mode = "systemmode")]
use std::{ use std::{
ffi::{CStr, CString}, ffi::{CStr, CString},
ptr::null_mut, ptr::null_mut,
}; };
use std::{slice::from_raw_parts, str::from_utf8_unchecked};
thread_local! {
static SNAPSHOT_PAGE_SIZE: OnceCell<usize> = OnceCell::new();
}
#[cfg(emulation_mode = "usermode")] #[cfg(emulation_mode = "usermode")]
use libc::c_int; use libc::c_int;
@ -742,6 +746,25 @@ impl CPU {
pub fn raw_ptr(&self) -> CPUStatePtr { pub fn raw_ptr(&self) -> CPUStatePtr {
self.ptr self.ptr
} }
#[must_use]
pub fn page_size(&self) -> usize {
#[cfg(emulation_mode = "usermode")]
{
SNAPSHOT_PAGE_SIZE.with(|s| {
*s.get_or_init(|| {
unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) }
.try_into()
.expect("Invalid page size")
})
})
}
#[cfg(emulation_mode = "systemmode")]
{
SNAPSHOT_PAGE_SIZE
.with(|s| *s.get_or_init(|| unsafe { libafl_qemu_sys::qemu_target_page_size() }))
}
}
} }
static mut EMULATOR_IS_INITIALIZED: bool = false; static mut EMULATOR_IS_INITIALIZED: bool = false;