From 04c8d5208b92904b08321944c8b4a39bf9d5f529 Mon Sep 17 00:00:00 2001 From: Andrea Fioraldi Date: Fri, 25 Aug 2023 11:42:23 +0200 Subject: [PATCH] qemu: Fix cpu page size function for full-system (#1452) * Revert "qemu: add cpu page_size call (#1433)" This reverts commit d338b30c080ecfe1a6639185b6505b7a7b8edbeb. * Reintroduce page_size --- libafl_qemu/libafl_qemu_build/src/bindings.rs | 2 +- .../libafl_qemu_sys/src/x86_64_stub_bindings.rs | 11 +++++------ libafl_qemu/src/emu.rs | 13 ++++++------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/libafl_qemu/libafl_qemu_build/src/bindings.rs b/libafl_qemu/libafl_qemu_build/src/bindings.rs index a2c9ed2567..37b339197a 100644 --- a/libafl_qemu/libafl_qemu_build/src/bindings.rs +++ b/libafl_qemu/libafl_qemu_build/src/bindings.rs @@ -115,12 +115,12 @@ pub fn generate( .allowlist_function("tlb_plugin_lookup") .allowlist_function("qemu_plugin_hwaddr_phys_addr") .allowlist_function("qemu_plugin_get_hwaddr") + .allowlist_function("qemu_target_page_size") .allowlist_function("syx_snapshot_init") .allowlist_function("syx_snapshot_create") .allowlist_function("syx_snapshot_root_restore") .allowlist_function("syx_snapshot_dirty_list_add") .allowlist_function("device_list_all") - .allowlist_function("qemu_target_page_size") .blocklist_function("main_loop_wait") // bindgen issue #1313 .parse_callbacks(Box::new(bindgen::CargoCallbacks)); diff --git a/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs b/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs index 95e9e177c5..b780f74d35 100644 --- a/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs +++ b/libafl_qemu/libafl_qemu_sys/src/x86_64_stub_bindings.rs @@ -1,5 +1,10 @@ /* automatically generated by rust-bindgen 0.66.1 */ +extern "C" { + #[doc = " qemu_target_page_size - return the target's page size"] + pub fn qemu_target_page_size() -> usize; +} + #[repr(C)] #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct __BindgenBitfieldUnit { @@ -11100,12 +11105,6 @@ impl ::std::ops::BitAndAssign for qemu_plugin_mem_rw { 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)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub struct qemu_plugin_mem_rw(pub ::std::os::raw::c_uint); diff --git a/libafl_qemu/src/emu.rs b/libafl_qemu/src/emu.rs index 14074c3516..21ed930630 100644 --- a/libafl_qemu/src/emu.rs +++ b/libafl_qemu/src/emu.rs @@ -14,10 +14,6 @@ use std::{ ptr::null_mut, }; -thread_local! { - static SNAPSHOT_PAGE_SIZE: OnceCell = OnceCell::new(); -} - #[cfg(emulation_mode = "usermode")] use libc::c_int; use num_enum::{IntoPrimitive, TryFromPrimitive}; @@ -751,7 +747,11 @@ impl CPU { pub fn page_size(&self) -> usize { #[cfg(emulation_mode = "usermode")] { - SNAPSHOT_PAGE_SIZE.with(|s| { + thread_local! { + static PAGE_SIZE: OnceCell = OnceCell::new(); + } + + PAGE_SIZE.with(|s| { *s.get_or_init(|| { unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) } .try_into() @@ -761,8 +761,7 @@ impl CPU { } #[cfg(emulation_mode = "systemmode")] { - SNAPSHOT_PAGE_SIZE - .with(|s| *s.get_or_init(|| unsafe { libafl_qemu_sys::qemu_target_page_size() })) + unsafe { libafl_qemu_sys::qemu_target_page_size() } } } }