From 2f61592ff9147363ea7f47e35226c218ebaf73bf Mon Sep 17 00:00:00 2001 From: lazymio Date: Sun, 7 Nov 2021 20:37:58 +0100 Subject: [PATCH] Fix uc_mem_protect --- qemu/softmmu/memory.c | 1 + tests/unit/test_mem.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/qemu/softmmu/memory.c b/qemu/softmmu/memory.c index 7399f873..50cd2448 100644 --- a/qemu/softmmu/memory.c +++ b/qemu/softmmu/memory.c @@ -1084,6 +1084,7 @@ void memory_region_set_readonly(MemoryRegion *mr, bool readonly) if (mr->readonly != readonly) { memory_region_transaction_begin(); mr->readonly = readonly; + mr->uc->memory_region_update_pending |= mr->enabled; memory_region_transaction_commit(mr); } } diff --git a/tests/unit/test_mem.c b/tests/unit/test_mem.c index 987a3ae3..57c76a48 100644 --- a/tests/unit/test_mem.c +++ b/tests/unit/test_mem.c @@ -35,6 +35,32 @@ static void test_map_wrapping() OK(uc_close(uc)); } +static void test_mem_protect() +{ + uc_engine *qc; + int r_eax = 0x2000; + int r_esi = 0xdeadbeef; + uint32_t mem; + // add [eax + 4], esi + char code[] = {0x01, 0x70, 0x04}; + + OK(uc_open(UC_ARCH_X86, UC_MODE_32, &qc)); + OK(uc_reg_write(qc, UC_X86_REG_EAX, &r_eax)); + OK(uc_reg_write(qc, UC_X86_REG_ESI, &r_esi)); + OK(uc_mem_map(qc, 0x1000, 0x1000, UC_PROT_READ | UC_PROT_EXEC)); + OK(uc_mem_map(qc, 0x2000, 0x1000, UC_PROT_READ)); + OK(uc_mem_protect(qc, 0x2000, 0x1000, UC_PROT_READ | UC_PROT_WRITE)); + OK(uc_mem_write(qc, 0x1000, code, sizeof(code))); + + OK(uc_emu_start(qc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 1)); + OK(uc_mem_read(qc, 0x2000 + 4, &mem, 4)); + + TEST_CHECK(mem == 0xdeadbeef); + + OK(uc_close(qc)); +} + TEST_LIST = {{"test_map_correct", test_map_correct}, {"test_map_wrapping", test_map_wrapping}, + {"test_mem_protect", test_mem_protect}, {NULL, NULL}};