Skip to content

Commit d0da90c

Browse files
authored
Make Qemu.mmap public and accept fd as the argument (#3083)
* chg mmap * Proper error handling * lol * fix ci
1 parent 0a923b2 commit d0da90c

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

libafl_qemu/src/emu/usermode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use libafl_bolts::Error;
12
use libafl_qemu_sys::{GuestAddr, MmapPerms, VerifyAccess};
23

34
use crate::{Emulator, GuestMaps, NopSnapshotManager, TargetSignalHandling};
@@ -68,7 +69,7 @@ impl<C, CM, ED, ET, I, S, SM> Emulator<C, CM, ED, ET, I, S, SM> {
6869
addr: GuestAddr,
6970
size: usize,
7071
perms: MmapPerms,
71-
) -> Result<GuestAddr, String> {
72+
) -> Result<GuestAddr, Error> {
7273
self.qemu.map_private(addr, size, perms)
7374
}
7475

@@ -77,7 +78,7 @@ impl<C, CM, ED, ET, I, S, SM> Emulator<C, CM, ED, ET, I, S, SM> {
7778
addr: GuestAddr,
7879
size: usize,
7980
perms: MmapPerms,
80-
) -> Result<GuestAddr, String> {
81+
) -> Result<GuestAddr, Error> {
8182
self.qemu.map_fixed(addr, size, perms)
8283
}
8384

libafl_qemu/src/qemu/usermode.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
str::from_utf8_unchecked_mut,
44
};
55

6-
use libafl_bolts::os::unix_signals::Signal;
6+
use libafl_bolts::{Error, os::unix_signals::Signal};
77
use libafl_qemu_sys::{
88
GuestAddr, GuestUsize, IntervalTreeNode, IntervalTreeRoot, MapInfo, MmapPerms, VerifyAccess,
99
exec_path, free_self_maps, guest_base, libafl_force_dfl, libafl_get_brk,
@@ -324,18 +324,22 @@ impl Qemu {
324324
}
325325

326326
#[expect(clippy::cast_sign_loss)]
327-
fn mmap(
327+
pub fn mmap(
328328
self,
329329
addr: GuestAddr,
330330
size: usize,
331331
perms: MmapPerms,
332332
flags: c_int,
333-
) -> Result<GuestAddr, ()> {
333+
fd: i32,
334+
) -> Result<GuestAddr, Error> {
334335
let res = unsafe {
335-
libafl_qemu_sys::target_mmap(addr, size as GuestUsize, perms.into(), flags, -1, 0)
336+
libafl_qemu_sys::target_mmap(addr, size as GuestUsize, perms.into(), flags, fd, 0)
336337
};
337338
if res <= 0 {
338-
Err(())
339+
let errno = std::io::Error::last_os_error().raw_os_error();
340+
Err(Error::illegal_argument(format!(
341+
"failed to mmap addr: {addr:x} (size: {size:?} prot: {perms:?} flags: {flags:?} fd: {fd:?}). The errno is {errno:?}",
342+
)))
339343
} else {
340344
Ok(res as GuestAddr)
341345
}
@@ -346,26 +350,29 @@ impl Qemu {
346350
addr: GuestAddr,
347351
size: usize,
348352
perms: MmapPerms,
349-
) -> Result<GuestAddr, String> {
350-
self.mmap(addr, size, perms, libc::MAP_PRIVATE | libc::MAP_ANONYMOUS)
351-
.map_err(|()| format!("Failed to map {addr}"))
352-
.map(|addr| addr as GuestAddr)
353+
) -> Result<GuestAddr, Error> {
354+
self.mmap(
355+
addr,
356+
size,
357+
perms,
358+
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
359+
-1,
360+
)
353361
}
354362

355363
pub fn map_fixed(
356364
&self,
357365
addr: GuestAddr,
358366
size: usize,
359367
perms: MmapPerms,
360-
) -> Result<GuestAddr, String> {
368+
) -> Result<GuestAddr, Error> {
361369
self.mmap(
362370
addr,
363371
size,
364372
perms,
365373
libc::MAP_FIXED | libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
374+
-1,
366375
)
367-
.map_err(|()| format!("Failed to map {addr}"))
368-
.map(|addr| addr as GuestAddr)
369376
}
370377

371378
pub fn mprotect(&self, addr: GuestAddr, size: usize, perms: MmapPerms) -> Result<(), String> {
@@ -532,7 +539,7 @@ pub mod pybind {
532539
if let Ok(p) = MmapPerms::try_from(perms) {
533540
self.qemu
534541
.map_private(addr, size, p)
535-
.map_err(PyValueError::new_err)
542+
.map_err(|_| PyValueError::new_err("Failed to mmap"))
536543
} else {
537544
Err(PyValueError::new_err("Invalid perms"))
538545
}
@@ -541,8 +548,8 @@ pub mod pybind {
541548
fn map_fixed(&self, addr: GuestAddr, size: usize, perms: i32) -> PyResult<GuestAddr> {
542549
if let Ok(p) = MmapPerms::try_from(perms) {
543550
self.qemu
544-
.map_fixed(addr, size, p)
545-
.map_err(PyValueError::new_err)
551+
.map_private(addr, size, p)
552+
.map_err(|_| PyValueError::new_err("Failed to mmap"))
546553
} else {
547554
Err(PyValueError::new_err("Invalid perms"))
548555
}

0 commit comments

Comments
 (0)