Skip to content

Commit

Permalink
add logging framework
Browse files Browse the repository at this point in the history
  • Loading branch information
ks0777 committed Mar 22, 2023
1 parent ba76fab commit 64ab813
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 29 deletions.
9 changes: 5 additions & 4 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def controller(
num_workers,
queuedepth,
compressionlevel,
qemu_output,
engine_output,
goldenrun=True,
logger=hdf5collector,
qemu_pre=None,
Expand Down Expand Up @@ -283,7 +283,7 @@ def controller(
goldenrun_data,
faultlist,
] = run_goldenrun(
config_qemu, qemu_output, queue_output, faultlist, qemu_pre, qemu_post
config_qemu, engine_output, queue_output, faultlist, qemu_pre, qemu_post
)
pickle.dump(
(
Expand Down Expand Up @@ -376,6 +376,7 @@ def controller(
config_qemu,
faults["index"],
queue_output,
engine_output,
pregoldenrun_data,
goldenrun_data,
True,
Expand All @@ -390,7 +391,7 @@ def controller(
config_qemu,
faults["index"],
queue_output,
qemu_output,
engine_output,
goldenrun_data,
True,
queue_ram_usage,
Expand Down Expand Up @@ -673,7 +674,7 @@ def process_arguments(args):
parguments["num_workers"], # num_workers
parguments["queuedepth"], # queuedepth
parguments["compressionlevel"], # compressionlevel
args.debug, # qemu_output
args.debug, # engine_output
parguments["goldenrun"], # goldenrun
hdf5collector, # logger
None, # qemu_pre
Expand Down
113 changes: 113 additions & 0 deletions emulation_worker/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions emulation_worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ unicorn-engine = "2.0.0"
num = "0.4.0"
priority-queue = "1.3.1"
capstone = "0.11.0"
log = "0.4.17"
simplelog = "0.12.1"
time = "0.3.20"
17 changes: 17 additions & 0 deletions emulation_worker/src/architecture.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use capstone::{prelude::BuildsCapstone, Capstone};
use pyo3::types::PyDict;
use std::collections::HashMap;
use std::sync::RwLockWriteGuard;
Expand Down Expand Up @@ -71,6 +72,7 @@ pub enum Architecture {

pub trait ArchitectureDependentOperations {
fn initialize_unicorn(&self) -> Unicorn<()>;
fn initialize_cs_engine(&self) -> Capstone;
fn initialize_registers(
&self,
uc: &mut Unicorn<()>,
Expand Down Expand Up @@ -127,6 +129,21 @@ impl ArchitectureDependentOperations for ArchitectureDependentOperator {
}
}

fn initialize_cs_engine(self: &ArchitectureDependentOperator) -> Capstone {
match self.architecture {
Architecture::Arm => Capstone::new()
.arm()
.mode(capstone::arch::arm::ArchMode::Thumb)
.build()
.unwrap(),
Architecture::Riscv => Capstone::new()
.riscv()
.mode(capstone::arch::riscv::ArchMode::RiscV64)
.build()
.unwrap(),
}
}

fn dump_registers(
self: &ArchitectureDependentOperator,
uc: &mut Unicorn<()>,
Expand Down
19 changes: 10 additions & 9 deletions emulation_worker/src/hooks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::{debug, info};
use num::{BigUint, ToPrimitive};
use pyo3::types::{PyDict, PyList};
use std::{collections::HashMap, io, sync::Arc};
Expand Down Expand Up @@ -71,15 +72,15 @@ fn end_hook_cb(

let counter = endpoints.get_mut(&address).unwrap();
if *counter > 1 {
println!(
"Decreasing endpoint counter for {:?} to {:?}",
debug!(
"Decreasing endpoint counter for 0x{:x} to {:?}",
address, *counter
);
*counter -= 1;
} else {
let mut endpoint = state.logs.endpoint.write().unwrap();
*endpoint = (address == first_endpoint, address, 1);
println!("Reached endpoint at {address:?}");
info!("Reached endpoint at 0x{address:x}");

// Since this hook has been registered before the single step hook we need to call it
// manullay to log the last instruction, since the callback would not be called otherwise
Expand Down Expand Up @@ -112,7 +113,7 @@ fn single_step_hook_cb(uc: &mut Unicorn<'_, ()>, address: u64, size: u32, state:
);

if let Some(fault) = undone_fault {
if matches!(fault.kind, FaultType::Register) {
if !matches!(fault.kind, FaultType::Register) {
dump_memory(
uc,
address,
Expand Down Expand Up @@ -153,10 +154,10 @@ fn mem_hook_cb(
meminfo.insert(
(address, pc),
MemInfo {
ins: address,
ins: pc,
counter: 1,
direction: if mem_type == MemType::READ { 0 } else { 1 },
address: pc,
address,
tbid: last_tbid,
size,
},
Expand All @@ -177,7 +178,7 @@ fn fault_hook_cb(uc: &mut Unicorn<'_, ()>, address: u64, _size: u32, state: &Arc
}
}

println!("Executing fault at {address:?}");
info!("Executing fault at 0x{address:x}");

let prefault_data;

Expand All @@ -190,8 +191,8 @@ fn fault_hook_cb(uc: &mut Unicorn<'_, ()>, address: u64, _size: u32, state: &Arc
.as_slice(),
);
prefault_data = data.clone();
println!(
"Overwriting {:?} with {:?}",
debug!(
"Overwriting 0x{:x} with 0x{:x}",
data,
apply_model(&data, fault)
);
Expand Down
3 changes: 2 additions & 1 deletion emulation_worker/src/hooks/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::structs::{TbExecEntry, TbInfoBlock};
use crate::{Fault, FaultModel, FaultType, MemDump};
use log::debug;
use num::{BigUint, ToPrimitive};
use priority_queue::PriorityQueue;
use std::collections::HashMap;
Expand Down Expand Up @@ -85,7 +86,7 @@ pub fn undo_faults(

let fault = faults.get(&address).unwrap();

println!("Undoing fault");
debug!("Undoing fault");
match fault.kind {
FaultType::Register => {
uc.reg_write(fault.address as i32, prefault_data.to_u64().unwrap())
Expand Down
Loading

0 comments on commit 64ab813

Please sign in to comment.