From 2faf2eb5b6ec0695b7ee47005bc15adb3ecfe7f4 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Thu, 1 Jan 2026 22:22:33 +0000 Subject: [PATCH] Windows: Invalidate code in freed memory after the free syscall Windows will populate Size/Address with those of the underlying free region. This is important for cross-process free operations, where the the 'before' callback is still called after the memory is freed so querying the size of the section (which is not freed) fails. --- Source/Windows/ARM64EC/Module.cpp | 8 +++----- Source/Windows/WOW64/Module.cpp | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp index 19ac4f33d..c2c9be484 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -793,12 +793,10 @@ void NotifyMemoryFree(void* Address, SIZE_T Size, ULONG FreeType, BOOL After, NT if (!After) { ThreadCreationMutex.lock(); - if (FreeType & MEM_DECOMMIT) { - InvalidationTracker->InvalidateAlignedInterval(reinterpret_cast(Address), static_cast(Size), true); - } else if (FreeType & MEM_RELEASE) { - InvalidationTracker->InvalidateContainingSection(reinterpret_cast(Address), true); - } } else { + if (!Status) { + InvalidationTracker->InvalidateAlignedInterval(reinterpret_cast(Address), static_cast(Size), true); + } ThreadCreationMutex.unlock(); } } diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp index dfd015582..23dbf679b 100644 --- a/Source/Windows/WOW64/Module.cpp +++ b/Source/Windows/WOW64/Module.cpp @@ -966,12 +966,10 @@ void BTCpuNotifyMemoryProtect(void* Address, SIZE_T Size, ULONG NewProt, BOOL Af void BTCpuNotifyMemoryFree(void* Address, SIZE_T Size, ULONG FreeType, BOOL After, ULONG Status) { if (!After) { ThreadCreationMutex.lock(); - if (!Size) { - InvalidationTracker->InvalidateContainingSection(reinterpret_cast(Address), true); - } else if (FreeType & MEM_DECOMMIT) { + } else { + if (!Status) { InvalidationTracker->InvalidateAlignedInterval(reinterpret_cast(Address), static_cast(Size), true); } - } else { ThreadCreationMutex.unlock(); } }