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 May 9, 2023
1 parent 1dcf389 commit efb1106
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ jobs:
args: --all --manifest-path ./emulation_worker/Cargo.toml -- --check
- run: |
cd emulation_worker
git submodule update --init unicorn
cargo clippy -- -D warnings
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 @@ -211,6 +211,12 @@ fn fault_hook_cb(uc: &mut Unicorn<'_, ()>, address: u64, _size: u32, state: &Arc
fault_data.extend(std::iter::repeat(0).take(fault_size as usize - fault_data.len()));
uc.mem_write(fault.address, fault_data.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 efb1106

Please sign in to comment.