From 8d3bf02041900ba6c780c02a679abb5029d4d279 Mon Sep 17 00:00:00 2001 From: "Takacs, Philipp" Date: Tue, 9 Jan 2024 17:06:49 +0100 Subject: [PATCH] fix cow when using uc_mem_write memory_cow expect the address and size to be aligned on pagesize. --- qemu/softmmu/memory.c | 3 +++ uc.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/qemu/softmmu/memory.c b/qemu/softmmu/memory.c index 2c52539c..ee72511e 100644 --- a/qemu/softmmu/memory.c +++ b/qemu/softmmu/memory.c @@ -98,6 +98,9 @@ MemoryRegion *memory_cow(struct uc_struct *uc, MemoryRegion *current, hwaddr beg hwaddr current_offset; MemoryRegion *ram = g_new(MemoryRegion, 1); + assert((begin & ~TARGET_PAGE_MASK) == 0); + assert((size & ~TARGET_PAGE_MASK) == 0); + if (current->container == uc->system_memory) { make_contained(uc, current); } diff --git a/uc.c b/uc.c index 370b6dc7..e2248fbd 100644 --- a/uc.c +++ b/uc.c @@ -788,7 +788,10 @@ uc_err uc_mem_write(uc_engine *uc, uint64_t address, const void *_bytes, len = memory_region_len(uc, mr, address, size - count); if (uc->snapshot_level && uc->snapshot_level > mr->priority) { - mr = uc->memory_cow(uc, mr, address, len); + mr = uc->memory_cow(uc, mr, address & ~uc->target_page_align, + (len + (address & uc->target_page_align) + + uc->target_page_align) & + ~uc->target_page_align); if (!mr) { return UC_ERR_NOMEM; }