Skip to content

Commit 1addbd0

Browse files
rmalmaintokatoka
andauthored
Expose qemu's image_info for qemu usermode (#2889)
* image info for qemu usermode * must use --------- Co-authored-by: Toka <[email protected]>
1 parent 4083f0b commit 1addbd0

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

libafl_qemu/src/qemu/usermode.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
mem::MaybeUninit, ptr::copy_nonoverlapping, slice::from_raw_parts_mut,
2+
mem::MaybeUninit, ops::Range, ptr::copy_nonoverlapping, slice::from_raw_parts_mut,
33
str::from_utf8_unchecked_mut,
44
};
55

@@ -21,6 +21,18 @@ pub struct GuestMaps {
2121
pageflags_node: *mut IntervalTreeNode,
2222
}
2323

24+
/// Information about the image loaded by QEMU.
25+
pub struct ImageInfo {
26+
pub code: Range<GuestAddr>,
27+
pub data: Range<GuestAddr>,
28+
pub stack: Range<GuestAddr>,
29+
pub vdso: GuestAddr,
30+
pub entry: GuestAddr,
31+
pub brk: GuestAddr,
32+
pub alignment: GuestAddr,
33+
pub exec_stack: bool,
34+
}
35+
2436
// Consider a private new only for Emulator
2537
impl GuestMaps {
2638
#[must_use]
@@ -129,6 +141,33 @@ impl Qemu {
129141
GuestMaps::new()
130142
}
131143

144+
#[must_use]
145+
pub fn image_info(&self) -> ImageInfo {
146+
// # Safety
147+
// Safe because QEMU has been correctly initialized since it takes self as parameter.
148+
let image_info = unsafe { *libafl_qemu_sys::libafl_get_image_info() };
149+
150+
let code_start = image_info.start_code;
151+
let code_end = image_info.end_code;
152+
153+
let data_start = image_info.start_data;
154+
let data_end = image_info.end_data;
155+
156+
let stack_start = image_info.stack_limit;
157+
let stack_end = image_info.start_stack;
158+
159+
ImageInfo {
160+
code: code_start..code_end,
161+
data: data_start..data_end,
162+
stack: stack_start..stack_end,
163+
vdso: image_info.vdso,
164+
entry: image_info.entry,
165+
brk: image_info.brk,
166+
alignment: image_info.alignment,
167+
exec_stack: image_info.exec_stack,
168+
}
169+
}
170+
132171
#[must_use]
133172
pub fn g2h<T>(&self, addr: GuestAddr) -> *mut T {
134173
unsafe { (addr as usize + guest_base) as *mut T }

0 commit comments

Comments
 (0)