implement simple memory snapshot mechanismus

Uses Copy on Write to make it posible to restore the memory state after a snapshot
was made. To restore all MemoryRegions created after the snapshot are removed.
This commit is contained in:
Takacs, Philipp
2022-12-22 15:14:07 +01:00
parent 065af19dc5
commit 80bd825420
25 changed files with 302 additions and 52 deletions

View File

@@ -275,6 +275,73 @@ static void test_mem_protect_mmio(void)
OK(uc_close(uc));
}
static void test_snapshot(void)
{
uc_engine *uc;
uint32_t mem;
// mov eax, [0x2020]; inc eax; mov [0x2020], eax
char code[] = "\xa1\x20\x20\x00\x00\x00\x00\x00\x00\xff\xc0\xa3\x20\x20\x00"
"\x00\x00\x00\x00\x00";
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_ALL));
OK(uc_mem_write(uc, 0x1000, code, sizeof(code) - 1));
OK(uc_mem_map(uc, 0x2000, 0x1000, UC_PROT_ALL));
OK(uc_snapshot(uc));
OK(uc_emu_start(uc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 1);
OK(uc_snapshot(uc));
OK(uc_emu_start(uc, 0x1000, 0x1000 + sizeof(code) - 1, 0, 0));
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 2);
OK(uc_restore_latest_snapshot(uc));
//TODO check mem
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 1);
OK(uc_restore_latest_snapshot(uc));
OK(uc_mem_read(uc, 0x2020, &mem, sizeof(mem)));
TEST_CHECK(mem == 0);
//TODO check mem
OK(uc_context_free(c0));
OK(uc_context_free(c1));
OK(uc_close(uc));
}
static void test_context_snapshot(void)
{
uc_engine *uc;
uc_context *ctx;
uint64_t tmp = 1;
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
OK(uc_ctl_context_use_snapshots(uc, 1));
OK(uc_mem_map(uc, 0x1000, 0x1000, UC_PROT_ALL));
OK(uc_context_alloc(uc, &ctx));
OK(uc_context_save(uc, ctx));
OK(uc_mem_write(uc, 0x1000, &tmp, sizeof(tmp)));
OK(uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
TEST_CHECK(tmp == 1);
OK(uc_context_restore(uc, ctx));
OK(uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
TEST_CHECK(tmp == 0);
tmp = 2;
OK(uc_mem_write(uc, 0x1000, &tmp, sizeof(tmp)));
OK(uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
TEST_CHECK(tmp == 2);
OK(uc_context_restore(uc, ctx));
OK(uc_mem_read(uc, 0x1000, &tmp, sizeof(tmp)));
TEST_CHECK(tmp == 0);
OK(uc_context_free(ctx));
OK(uc_close(uc));
}
TEST_LIST = {{"test_map_correct", test_map_correct},
{"test_map_wrapping", test_map_wrapping},
{"test_mem_protect", test_mem_protect},
@@ -286,4 +353,6 @@ TEST_LIST = {{"test_map_correct", test_map_correct},
{"test_map_big_memory", test_map_big_memory},
{"test_mem_protect_remove_exec", test_mem_protect_remove_exec},
{"test_mem_protect_mmio", test_mem_protect_mmio},
{"test_snapshot", test_snapshot},
{"test_context_snapshot", test_context_snapshot},
{NULL, NULL}};