From e36ed03720c2003bd148cd44786fd4a56d38bd12 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 31 Aug 2024 16:40:55 +0200 Subject: [PATCH] Fix classic alignment mistake --- src/windows_emulator/syscalls.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/windows_emulator/syscalls.cpp b/src/windows_emulator/syscalls.cpp index 9b8aa4f4..bd5539a0 100644 --- a/src/windows_emulator/syscalls.cpp +++ b/src/windows_emulator/syscalls.cpp @@ -770,16 +770,22 @@ namespace return STATUS_NOT_SUPPORTED; } - const auto address = page_align_down(base_address.read()); - base_address.write(address); + const auto orig_start = base_address.read(); + const auto orig_length = bytes_to_protect.read(); - const auto size = page_align_up(bytes_to_protect.read()); - bytes_to_protect.write(static_cast(size)); + const auto aligned_start = page_align_down(orig_start); + const auto aligned_length = page_align_up(orig_start + orig_length) - aligned_start; + + base_address.write(aligned_start); + bytes_to_protect.write(static_cast(aligned_length)); const auto requested_protection = map_nt_to_emulator_protection(protection); + printf("Changing protection at %llX-%llX to %s\n", aligned_start, aligned_start + aligned_length, + get_permission_string(requested_protection).c_str()); + memory_permission old_protection_value{}; - c.emu.protect_memory(address, size, requested_protection, &old_protection_value); + c.emu.protect_memory(aligned_start, aligned_length, requested_protection, &old_protection_value); const auto current_protection = map_emulator_to_nt_protection(old_protection_value); old_protection.write(current_protection);