From a8ed248a13a7e0341fbd236f409d62ffd5dcae78 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Thu, 25 Jan 2018 23:56:57 +0000 Subject: [PATCH] tests/A64: Test memory writes --- tests/A64/fuzz_with_unicorn.cpp | 5 +++-- tests/A64/testenv.h | 2 +- tests/A64/unicorn_emu/unicorn.cpp | 28 ++++++++++++++++++++++++++-- tests/A64/unicorn_emu/unicorn.h | 2 ++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/A64/fuzz_with_unicorn.cpp b/tests/A64/fuzz_with_unicorn.cpp index e4dc5fc7..9d58945f 100644 --- a/tests/A64/fuzz_with_unicorn.cpp +++ b/tests/A64/fuzz_with_unicorn.cpp @@ -52,7 +52,7 @@ restart: if (!should_continue && !is_last_inst) goto restart; for (const auto& ir_inst : block) - if (ir_inst.IsMemoryWrite() || ir_inst.GetOpcode() == IR::Opcode::A64ExceptionRaised || ir_inst.GetOpcode() == IR::Opcode::A64CallSupervisor) + if (ir_inst.GetOpcode() == IR::Opcode::A64ExceptionRaised || ir_inst.GetOpcode() == IR::Opcode::A64CallSupervisor) goto restart; return instruction; @@ -87,11 +87,12 @@ static void RunTestInstance(const std::array& regs, const std::array u32 { if (base_address < this_->testenv.code_mem.size() * 4) return UC_PROT_READ | UC_PROT_EXEC; - return UC_PROT_READ | UC_PROT_WRITE; + return UC_PROT_READ; }(); auto page = std::make_unique(); @@ -209,3 +210,26 @@ bool Unicorn::UnmappedMemoryHook(uc_engine* uc, uc_mem_type /*type*/, u64 start_ return true; } + +bool Unicorn::MemoryWriteHook(uc_engine* /*uc*/, uc_mem_type /*type*/, u64 start_address, int size, u64 value, void* user_data) { + Unicorn* this_ = reinterpret_cast(user_data); + + switch (size) { + case 1: + this_->testenv.MemoryWrite8(start_address, static_cast(value)); + break; + case 2: + this_->testenv.MemoryWrite16(start_address, static_cast(value)); + break; + case 4: + this_->testenv.MemoryWrite32(start_address, static_cast(value)); + break; + case 8: + this_->testenv.MemoryWrite64(start_address, value); + break; + default: + UNREACHABLE(); + } + + return true; +} diff --git a/tests/A64/unicorn_emu/unicorn.h b/tests/A64/unicorn_emu/unicorn.h index 1e48eac8..99e2fafe 100644 --- a/tests/A64/unicorn_emu/unicorn.h +++ b/tests/A64/unicorn_emu/unicorn.h @@ -49,6 +49,7 @@ public: private: static void InterruptHook(uc_engine* uc, u32 interrupt, void* user_data); static bool UnmappedMemoryHook(uc_engine* uc, uc_mem_type type, u64 addr, int size, u64 value, void* user_data); + static bool MemoryWriteHook(uc_engine* uc, uc_mem_type type, u64 addr, int size, u64 value, void* user_data); struct Page { u64 address; @@ -59,6 +60,7 @@ private: uc_engine* uc{}; uc_hook intr_hook{}; uc_hook mem_invalid_hook{}; + uc_hook mem_write_prot_hook{}; std::vector> pages; };