Skip to content

Commit

Permalink
add unicorn dependency as submodule; remove modified tbs from cache d…
Browse files Browse the repository at this point in the history
…uring emulation
  • Loading branch information
ks0777 committed Apr 25, 2023
1 parent 1d14326 commit 9e429da
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "qemu"]
path = qemu
url = https://github.com/Fraunhofer-AISEC/archie-qemu.git
[submodule "emulation_worker/unicorn"]
path = emulation_worker/unicorn
url = https://github.com/unicorn-engine/unicorn.git
4 changes: 1 addition & 3 deletions emulation_worker/Cargo.lock

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

4 changes: 3 additions & 1 deletion emulation_worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
name = "emulation_worker"
version = "0.1.0"
edition = "2021"
authors = ["Kevin Schneider"]
license = "Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
Expand All @@ -10,7 +12,7 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.17.3", features = ["extension-module"] }
unicorn-engine = "2.0.0"
unicorn-engine = { path = "./unicorn" }
num = "0.4.0"
priority-queue = "1.3.1"
capstone = "0.11.0"
Expand Down
6 changes: 6 additions & 0 deletions emulation_worker/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ fn fault_hook_cb(uc: &mut Unicorn<'_, ()>, address: u64, _size: u32, state: &Arc
apply_model(&data, fault).to_bytes_le().as_slice(),
)
.expect("failed writing fault data to memory");
if matches!(fault.kind, FaultType::Instruction) {
// We need to remove the tb containing the modified instructions from the cache
// since they might not have any effect otherwise
uc.ctl_remove_cache(fault.address, fault.address + fault_size as u64)
.unwrap();
}
dump_memory(
uc,
fault.address,
Expand Down
8 changes: 8 additions & 0 deletions emulation_worker/src/hooks/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ pub fn undo_faults(
FaultType::Data | FaultType::Instruction => {
uc.mem_write(fault.address, prefault_data.to_bytes_le().as_slice())
.expect("failed restoring memory value");
if matches!(fault.kind, FaultType::Instruction) {
//uc.ctl_arg_2(UC_CTL_TB_REMOVE_CACHE | UC_CTL_IO_WRITE, unsafe { std::mem::transmute::<u64, *mut c_void>(address) }, unsafe { std::mem::transmute::<u64, *mut c_void>(fault.address + prefault_data.to_bytes_le().len() as u64) }).unwrap();
uc.ctl_remove_cache(
fault.address,
fault.address + prefault_data.to_bytes_le().len() as u64,
)
.unwrap();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions emulation_worker/unicorn
Submodule unicorn added at bde3cd

0 comments on commit 9e429da

Please sign in to comment.