use bitflags::bitflags;
use bytes::{Buf, BufMut, BytesMut};
use rand::{thread_rng, Rng};
use serde::Serialize;
use std::mem::size_of;
use tokio_util::codec::{Decoder, Encoder};
pub const MAX_OPCODE_SIZE: usize = 16;
pub const NUM_SYSCALL_ARGS: usize = 8;
pub trait ToBytes {
fn to_bytes(&self, bytes: &mut BytesMut);
}
pub trait FromBytes {
fn from_bytes(bytes: &mut BytesMut) -> Self;
}
bitflags! {
#[repr(C)]
#[derive(Default, Serialize)]
pub struct EventFlags: u32 {
const PC = 0b00000001;
const READS_WRITES = 0b00000010;
const INSTRS = 0b00001000;
const SYSCALLS = 0b00010000;
const BRANCHES = 0b00100000;
const EXECUTED = 0b01000000;
const FINISHED = 0b10000000;
const LOAD = 0b0000000100000000;
}
}
impl EventFlags {
pub fn from(
has_pc: bool,
has_instrs: bool,
has_reads_writes: bool,
has_syscalls: bool,
has_branches: bool,
) -> Self {
let mut flags = EventFlags::default();
if has_pc {
flags |= EventFlags::PC;
}
if has_instrs {
flags |= EventFlags::INSTRS;
}
if has_reads_writes {
flags |= EventFlags::READS_WRITES;
}
if has_syscalls {
flags |= EventFlags::SYSCALLS;
}
if has_branches {
flags |= EventFlags::BRANCHES;
}
flags
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, Serialize)]
pub struct QemuPc {
pub pc: u64,
pub branch: bool,
}
impl QemuPc {
pub fn new(pc: u64, branch: bool) -> Self {
Self { pc, branch }
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
Self {
pc: rng.gen(),
branch: rng.gen(),
}
}
}
impl ToBytes for QemuPc {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_u64(self.pc);
bytes.put_u8(self.branch as u8);
}
}
impl FromBytes for QemuPc {
fn from_bytes(bytes: &mut BytesMut) -> Self {
Self {
pc: bytes.get_u64(),
branch: bytes.get_u8() != 0,
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct QemuInstr {
pub pc: u64,
pub opcode: [u8; MAX_OPCODE_SIZE],
pub opcode_size: usize,
}
impl QemuInstr {
pub fn new(pc: u64, opcode: [u8; MAX_OPCODE_SIZE], opcode_size: usize) -> Self {
Self {
pc,
opcode,
opcode_size,
}
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
let pc = rng.gen();
let mut opcode = [0u8; MAX_OPCODE_SIZE];
for i in 0..MAX_OPCODE_SIZE {
opcode[i] = rng.gen();
}
Self {
pc,
opcode,
opcode_size: rng.gen_range(1..MAX_OPCODE_SIZE),
}
}
}
impl ToBytes for QemuInstr {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_u64(self.pc);
bytes.put_slice(&self.opcode[..]);
bytes.put_u64(self.opcode_size as u64);
}
}
impl FromBytes for QemuInstr {
fn from_bytes(bytes: &mut BytesMut) -> Self {
let pc = bytes.get_u64();
let mut opcode = [0u8; MAX_OPCODE_SIZE];
bytes.copy_to_slice(&mut opcode[..]);
let opcode_size = bytes.get_u64() as usize;
QemuInstr {
pc,
opcode,
opcode_size,
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct QemuMemAccess {
pub pc: u64,
pub addr: u64,
pub is_write: bool,
}
impl QemuMemAccess {
pub fn new(pc: u64, addr: u64, is_write: bool) -> Self {
Self { pc, addr, is_write }
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
Self {
pc: rng.gen(),
addr: rng.gen_range(0..u64::MAX),
is_write: rng.gen(),
}
}
}
impl ToBytes for QemuMemAccess {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_u64(self.pc);
bytes.put_u64(self.addr);
bytes.put_u8(self.is_write as u8);
}
}
impl FromBytes for QemuMemAccess {
fn from_bytes(bytes: &mut BytesMut) -> Self {
let pc = bytes.get_u64();
let addr = bytes.get_u64();
let is_write = bytes.get_u8() != 0;
QemuMemAccess { pc, addr, is_write }
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct QemuSyscall {
pub num: i64,
pub rv: i64,
pub args: [u64; NUM_SYSCALL_ARGS],
}
impl QemuSyscall {
pub fn new(num: i64, rv: i64, args: [u64; 8]) -> Self {
Self { num, rv, args }
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
let mut args = [0u64; 8];
for i in 0..NUM_SYSCALL_ARGS {
args[i] = rng.gen_range(0..u64::MAX);
}
Self {
num: rng.gen_range(0..i64::MAX),
rv: rng.gen_range(0..i64::MAX),
args,
}
}
}
impl ToBytes for QemuSyscall {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_i64(self.num);
bytes.put_i64(self.rv);
for arg in self.args.iter() {
bytes.put_u64(*arg);
}
}
}
impl FromBytes for QemuSyscall {
fn from_bytes(bytes: &mut BytesMut) -> Self {
let num = bytes.get_i64();
let rv = bytes.get_i64();
let mut args = [0u64; 8];
for arg in args.iter_mut() {
*arg = bytes.get_u64();
}
QemuSyscall { num, rv, args }
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct QemuLoad {
pub min: u64,
pub max: u64,
pub entry: u64,
pub prot: u8,
}
impl QemuLoad {
pub fn new(min: u64, max: u64, entry: u64, prot: u8) -> Self {
Self {
min,
max,
entry,
prot,
}
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
Self {
min: rng.gen_range(0..u64::MAX),
max: rng.gen_range(0..u64::MAX),
entry: rng.gen_range(0..u64::MAX),
prot: rng.gen_range(0..u8::MAX),
}
}
}
impl ToBytes for QemuLoad {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_u64(self.min);
bytes.put_u64(self.max);
bytes.put_u64(self.entry);
bytes.put_u8(self.prot);
}
}
impl FromBytes for QemuLoad {
fn from_bytes(bytes: &mut BytesMut) -> Self {
let min = bytes.get_u64();
let max = bytes.get_u64();
let entry = bytes.get_u64();
let prot = bytes.get_u8();
QemuLoad {
min,
max,
entry,
prot,
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub enum QemuEvent {
Pc(QemuPc),
Instr(QemuInstr),
MemAccess(QemuMemAccess),
Syscall(QemuSyscall),
Load(QemuLoad),
}
#[repr(C)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct QemuEventMsg {
pub flags: EventFlags,
pub event: QemuEvent,
}
impl QemuEventMsg {
pub fn new(flags: EventFlags, event: QemuEvent) -> Self {
Self { flags, event }
}
pub fn new_random() -> Self {
let mut rng = thread_rng();
let flags = EventFlags::from_bits_truncate(rng.gen_range(0..u32::MAX));
let event = match rng.gen_range(0..7) {
0 => QemuEvent::Pc(QemuPc::new_random()),
1 => QemuEvent::Instr(QemuInstr::new_random()),
2 => QemuEvent::MemAccess(QemuMemAccess::new_random()),
4 => QemuEvent::Syscall(QemuSyscall::new_random()),
6 => QemuEvent::Load(QemuLoad::new_random()),
_ => unreachable!(),
};
Self { flags, event }
}
}
impl ToBytes for QemuEventMsg {
fn to_bytes(&self, bytes: &mut BytesMut) {
bytes.put_u32(self.flags.bits());
match self.event {
QemuEvent::Pc(ref event) => event.to_bytes(bytes),
QemuEvent::Instr(ref event) => event.to_bytes(bytes),
QemuEvent::MemAccess(ref event) => event.to_bytes(bytes),
QemuEvent::Syscall(ref event) => event.to_bytes(bytes),
QemuEvent::Load(ref event) => event.to_bytes(bytes),
}
}
}
impl FromBytes for QemuEventMsg {
fn from_bytes(bytes: &mut BytesMut) -> Self {
let flags = EventFlags::from_bits_truncate(bytes.get_u32());
let event = if flags.contains(EventFlags::PC) {
QemuEvent::Pc(QemuPc::from_bytes(bytes))
} else if flags.contains(EventFlags::INSTRS) {
QemuEvent::Instr(QemuInstr::from_bytes(bytes))
} else if flags.contains(EventFlags::READS_WRITES) {
QemuEvent::MemAccess(QemuMemAccess::from_bytes(bytes))
} else if flags.contains(EventFlags::SYSCALLS) {
QemuEvent::Syscall(QemuSyscall::from_bytes(bytes))
} else if flags.contains(EventFlags::LOAD) {
QemuEvent::Load(QemuLoad::from_bytes(bytes))
} else {
unreachable!()
};
QemuEventMsg { flags, event }
}
}
pub struct QemuMsgCodec {}
impl Encoder<QemuEventMsg> for QemuMsgCodec {
type Error = std::io::Error;
fn encode(&mut self, item: QemuEventMsg, dst: &mut BytesMut) -> Result<(), Self::Error> {
item.to_bytes(dst);
Ok(())
}
}
impl Decoder for QemuMsgCodec {
type Item = QemuEventMsg;
type Error = std::io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
if src.len() < size_of::<QemuEventMsg>() {
return Ok(None);
}
let exec = QemuEventMsg::from_bytes(src);
return Ok(Some(exec));
}
}