Skip to content

Commit

Permalink
Fix RISC-V port issues (#2642)
Browse files Browse the repository at this point in the history
fix riscv{32,64} stuff
  • Loading branch information
rmalmain authored Oct 30, 2024
1 parent af06d75 commit 9da113e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
10 changes: 8 additions & 2 deletions libafl_qemu/src/modules/usermode/asan_guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ pub struct AsanGuestModule<F> {
mappings: Vec<QemuAsanGuestMapping>,
}

#[cfg(any(cpu_target = "aarch64", cpu_target = "x86_64", feature = "clippy"))]
#[cfg(any(
cpu_target = "aarch64",
cpu_target = "x86_64",
cpu_target = "riscv64",
feature = "clippy"
))]
impl<F> AsanGuestModule<F> {
const HIGH_SHADOW_START: GuestAddr = 0x02008fff7000;
const HIGH_SHADOW_END: GuestAddr = 0x10007fff7fff;
Expand All @@ -135,7 +140,8 @@ impl<F> AsanGuestModule<F> {
cpu_target = "arm",
cpu_target = "i386",
cpu_target = "mips",
cpu_target = "ppc"
cpu_target = "ppc",
cpu_target = "riscv32",
))]
impl<F> AsanGuestModule<F> {
const HIGH_SHADOW_START: GuestAddr = 0x28000000;
Expand Down
24 changes: 15 additions & 9 deletions libafl_qemu/src/modules/usermode/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ use thread_local::ThreadLocal;

#[cfg(any(cpu_target = "arm", cpu_target = "i386", cpu_target = "mips"))]
use crate::SYS_fstatat64;
#[cfg(not(cpu_target = "arm"))]
#[cfg(not(any(cpu_target = "arm", cpu_target = "riscv32")))]
use crate::SYS_mmap;
#[cfg(any(cpu_target = "arm", cpu_target = "mips"))]
#[cfg(any(cpu_target = "arm", cpu_target = "mips", cpu_target = "riscv32"))]
use crate::SYS_mmap2;
#[cfg(not(any(
cpu_target = "arm",
cpu_target = "mips",
cpu_target = "i386",
cpu_target = "ppc"
cpu_target = "ppc",
cpu_target = "riscv32",
)))]
use crate::SYS_newfstatat;
use crate::{
Expand All @@ -26,9 +27,10 @@ use crate::{
NOP_ADDRESS_FILTER,
},
qemu::{Hook, SyscallHookResult},
Qemu, SYS_brk, SYS_fstat, SYS_fstatfs, SYS_futex, SYS_getrandom, SYS_mprotect, SYS_mremap,
SYS_munmap, SYS_pread64, SYS_read, SYS_readlinkat, SYS_statfs,
Qemu, SYS_brk, SYS_mprotect, SYS_mremap, SYS_munmap, SYS_pread64, SYS_read, SYS_readlinkat,
};
#[cfg(not(cpu_target = "riscv32"))]
use crate::{SYS_fstat, SYS_fstatfs, SYS_futex, SYS_getrandom, SYS_statfs};

// TODO use the functions provided by Qemu
pub const SNAPSHOT_PAGE_SIZE: usize = 4096;
Expand Down Expand Up @@ -804,6 +806,7 @@ where
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
h.access(a2, a3 as usize);
}
#[cfg(not(cpu_target = "riscv32"))]
SYS_futex => {
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
h.access(a0, a3 as usize);
Expand All @@ -812,7 +815,8 @@ where
cpu_target = "arm",
cpu_target = "i386",
cpu_target = "mips",
cpu_target = "ppc"
cpu_target = "ppc",
cpu_target = "riscv32"
)))]
SYS_newfstatat => {
if a2 != 0 {
Expand All @@ -827,10 +831,12 @@ where
h.access(a2, 4096); // stat is not greater than a page
}
}
SYS_statfs | SYS_fstatfs | SYS_fstat => {
#[cfg(not(cpu_target = "riscv32"))]
SYS_statfs | SYS_fstat | SYS_fstatfs => {
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
h.access(a1, 4096); // stat is not greater than a page
}
#[cfg(not(cpu_target = "riscv32"))]
SYS_getrandom => {
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
h.access(a0, a1 as usize);
Expand All @@ -855,15 +861,15 @@ where

// TODO handle huge pages

#[cfg(any(cpu_target = "arm", cpu_target = "mips"))]
#[cfg(any(cpu_target = "arm", cpu_target = "mips", cpu_target = "riscv32"))]
if sys_const == SYS_mmap2 {
if let Ok(prot) = MmapPerms::try_from(a2 as i32) {
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
h.add_mapped(result, a1 as usize, Some(prot));
}
}

#[cfg(not(cpu_target = "arm"))]
#[cfg(not(any(cpu_target = "arm", cpu_target = "riscv32")))]
if sys_const == SYS_mmap {
if let Ok(prot) = MmapPerms::try_from(a2 as i32) {
let h = emulator_modules.get_mut::<SnapshotModule>().unwrap();
Expand Down

0 comments on commit 9da113e

Please sign in to comment.