Keep high guest mappings intact when a fixed map or unmap fails

Check room in the region table, splits included, before a fixed map or
unmap removes anything, return the host munmap error without touching
the table entry, and keep write-protecting low shared aliases of
executable ranges that sit above the host address limit.
This commit is contained in:
maxjivi05 committed 2026-12-03 17:07:16 -05:00
1 parent cc8bb7b0a8
commit f1641f2ffe
1 file changed
+69 -28
@@ -13,14 +13,14 @@ writes to code in them invalidate translated blocks.
Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (eac-2611, MIT).
---
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
.../LinuxSyscalls/GuestHighMemory.cpp | 452 ++++++++++++++++++
.../LinuxSyscalls/GuestHighMemory.h | 26 ++
.../LinuxSyscalls/GuestHighMemory.cpp | 485 ++++++++++++++++++
.../LinuxSyscalls/GuestHighMemory.h | 26 +
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 30 +-
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 58 ++-
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 60 ++-
.../LinuxSyscalls/ThreadManager.cpp | 2 +
.../LinuxSyscalls/ThreadManager.h | 12 +
8 files changed, 551 insertions(+), 22 deletions(-)
8 files changed, 599 insertions(+), 22 deletions(-)
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
@@ -38,10 +38,10 @@ index 3f02104ea..acf4c4c13 100644
LinuxSyscalls/SyscallsVMATracking.cpp
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
new file mode 100644
index 000000000..a097f97fa
index 000000000..23781b213
--- /dev/null
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
@@ -0,0 +1,452 @@
@@ -0,0 +1,485 @@
+#include "LinuxSyscalls/GuestHighMemory.h"
+#include "LinuxSyscalls/Syscalls.h"
+#include "LinuxSyscalls/SignalDelegator.h"
@@ -167,7 +167,29 @@ index 000000000..a097f97fa
+ return false;
+ }
+
+ bool UnmapLocked(uint64_t Address, uint64_t Length) {
+ bool HasRoomLocked(uint64_t Address, uint64_t Length, uint32_t Added) {
+ const auto End = Address + Length;
+ const auto Entries = Count.load(std::memory_order_relaxed);
+ uint64_t After = Entries + Added;
+ for (uint32_t i = 0; i < Entries; ++i) {
+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed);
+ const auto RegionEnd = Guest + Regions[i].Size.load(std::memory_order_relaxed);
+ if (RegionEnd <= Address || Guest >= End) {
+ continue;
+ }
+ if (Guest >= Address && RegionEnd <= End) {
+ --After;
+ } else if (Guest < Address && RegionEnd > End) {
+ ++After;
+ }
+ }
+ return After <= MaxRegions;
+ }
+
+ int UnmapLocked(uint64_t Address, uint64_t Length) {
+ if (!HasRoomLocked(Address, Length, 0)) {
+ return -ENOMEM;
+ }
+ const auto End = Address + Length;
+ uint32_t i = 0;
+ while (i < Count.load(std::memory_order_relaxed)) {
@@ -182,7 +204,9 @@ index 000000000..a097f97fa
+ }
+ const auto OverlapStart = std::max(Guest, Address);
+ const auto OverlapEnd = std::min(RegionEnd, End);
+ ::munmap(reinterpret_cast<void*>(Host + (OverlapStart - Guest)), OverlapEnd - OverlapStart);
+ if (::munmap(reinterpret_cast<void*>(Host + (OverlapStart - Guest)), OverlapEnd - OverlapStart) != 0) {
+ return -errno;
+ }
+
+ if (OverlapStart == Guest && OverlapEnd == RegionEnd) {
+ RemoveEntry(i);
@@ -194,13 +218,13 @@ index 000000000..a097f97fa
+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code);
+ } else {
+ if (!AddEntry(OverlapEnd, RegionEnd - OverlapEnd, Host + (OverlapEnd - Guest), Code)) {
+ return false;
+ return -ENOMEM;
+ }
+ SetEntry(i, Guest, OverlapStart - Guest, Host, Code);
+ }
+ ++i;
+ }
+ return true;
+ return 0;
+ }
+
+ constexpr uint32_t RtField(uint32_t Insn) {
@@ -283,12 +307,21 @@ index 000000000..a097f97fa
+ }
+
+ BeginWrite();
+ const bool Taken = (Flags & MAP_FIXED_NOREPLACE) && OverlapsLocked(Address, Size);
+ bool Ok = !Taken && UnmapLocked(Address, Size) && AddEntry(Address, Size, reinterpret_cast<uint64_t>(Host), false);
+ int Result = 0;
+ if ((Flags & MAP_FIXED_NOREPLACE) && OverlapsLocked(Address, Size)) {
+ Result = -EEXIST;
+ } else if (!HasRoomLocked(Address, Size, 1)) {
+ Result = -ENOMEM;
+ } else {
+ Result = UnmapLocked(Address, Size);
+ }
+ if (Result == 0 && !AddEntry(Address, Size, reinterpret_cast<uint64_t>(Host), false)) {
+ Result = -ENOMEM;
+ }
+ EndWrite();
+ if (!Ok) {
+ if (Result != 0) {
+ ::munmap(Host, Size);
+ return Taken ? -EEXIST : -ENOMEM;
+ return Result;
+ }
+ return Address;
+}
@@ -302,9 +335,9 @@ index 000000000..a097f97fa
+ return -EINVAL;
+ }
+ BeginWrite();
+ bool Ok = UnmapLocked(Address, Size);
+ const int Result = UnmapLocked(Address, Size);
+ EndWrite();
+ return Ok ? 0 : -ENOMEM;
+ return Result;
+}
+
+uint64_t Protect(uint64_t Address, uint64_t Length, int Prot) {
@@ -608,7 +641,7 @@ index 607ab0b97..078faedac 100644
case 255: Result = std::invoke(Def.Ptr1, Frame, GetArg(Is64Bit, Frame, 0)); break;
default:
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
index f9e85a26f..bebdfd300 100644
index f9e85a26f..ad4106d51 100644
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp
@@ -21,6 +21,7 @@ $end_info$
@@ -650,18 +683,18 @@ index f9e85a26f..bebdfd300 100644
auto rv = mprotect(reinterpret_cast<void*>(Start), Length, PROT_READ | PROT_WRITE);
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", Start, Length);
});
@@ -227,7 +238,9 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
@@ -227,6 +238,10 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
const auto ProtectBase = std::max(MapBase, Base);
const auto ProtectSize = std::min(MapTop, Top) - ProtectBase;
- if (Mapping->second.Flags.Shared) {
+ if (FEX::HLE::GuestHighMemory::Contains(ProtectBase)) {
+ const bool HighRange = FEX::HLE::GuestHighMemory::Contains(ProtectBase);
+ if (HighRange) {
+ FEX::HLE::GuestHighMemory::MarkCode(ProtectBase, ProtectSize);
+ } else if (Mapping->second.Flags.Shared) {
+ }
if (Mapping->second.Flags.Shared) {
LOGMAN_THROW_A_FMT(Mapping->second.Resource, "VMA tracking error");
const auto OffsetBase = ProtectBase - Mapping->first + Mapping->second.Offset;
@@ -245,6 +258,10 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
@@ -245,13 +260,17 @@ void SyscallHandler::MarkGuestExecutableRange(FEXCore::Core::InternalThreadState
const auto MirroredBase = std::max(VMAOffsetBase, OffsetBase);
const auto MirroredSize = std::min(OffsetTop, VMAOffsetTop) - MirroredBase;
@@ -672,7 +705,15 @@ index f9e85a26f..bebdfd300 100644
auto rv = mprotect((void*)(MirroredBase - VMAOffsetBase + VMABase), MirroredSize, PROT_READ);
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", MirroredBase, MirroredSize);
@@ -293,6 +310,10 @@ void SyscallHandler::ProtectWritableAliasOfCode(uint64_t Base, uint64_t Length)
}
} while ((VMA = VMA->ResourceNextVMA));
- } else if (Mapping->second.Prot.Writable) {
+ } else if (!HighRange && Mapping->second.Prot.Writable) {
int rv = mprotect((void*)ProtectBase, ProtectSize, PROT_READ);
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", ProtectBase, ProtectSize);
@@ -293,6 +312,10 @@ void SyscallHandler::ProtectWritableAliasOfCode(uint64_t Base, uint64_t Length)
const auto OverlapBase = std::max(VMA->Offset, OffsetBase);
const auto OverlapTop = std::min(VMA->Offset + VMA->Length, OffsetTop);
@@ -683,7 +724,7 @@ index f9e85a26f..bebdfd300 100644
auto rv = mprotect((void*)(MapBase + (OverlapBase - OffsetBase)), OverlapTop - OverlapBase, PROT_READ);
LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", OverlapBase, OverlapTop - OverlapBase);
}
@@ -429,6 +450,11 @@ void* SyscallHandler::GuestMmap(bool Is64Bit, FEXCore::Core::InternalThreadState
@@ -429,6 +452,11 @@ void* SyscallHandler::GuestMmap(bool Is64Bit, FEXCore::Core::InternalThreadState
return reinterpret_cast<void*>(Result);
}
LOGMAN_THROW_A_FMT(Is64Bit || (Result >> 32) == 0 || (Result >> 32) == 0xFFFFFFFF, "values must fit to 32 bits");
@@ -695,7 +736,7 @@ index f9e85a26f..bebdfd300 100644
} else {
Result = reinterpret_cast<uint64_t>(::mmap(reinterpret_cast<void*>(addr), length, prot, flags, fd, offset));
if (Result == ~0ULL) {
@@ -483,9 +509,16 @@ uint64_t SyscallHandler::GuestMunmap(bool Is64Bit, FEXCore::Core::InternalThread
@@ -483,9 +511,16 @@ uint64_t SyscallHandler::GuestMunmap(bool Is64Bit, FEXCore::Core::InternalThread
if (Addr > X86_64TaskSizeMax || Size > X86_64TaskSizeMax - Addr || (length != 0 && Size == 0)) {
return -EINVAL;
}
@@ -715,7 +756,7 @@ index f9e85a26f..bebdfd300 100644
}
if (reinterpret_cast<uintptr_t>(addr) < 0x1'0000'0000ULL) {
Get32BitAllocator()->TrackUnmapped(addr, length);
@@ -511,6 +544,10 @@ uint64_t SyscallHandler::GuestMremap(bool Is64Bit, FEXCore::Core::InternalThread
@@ -511,6 +546,10 @@ uint64_t SyscallHandler::GuestMremap(bool Is64Bit, FEXCore::Core::InternalThread
{
auto lk = FEXCore::GuardSignalDeferringSection(VMATracking.Mutex, Thread);
if (Is64Bit) {
@@ -726,7 +767,7 @@ index f9e85a26f..bebdfd300 100644
Result = reinterpret_cast<uint64_t>(::mremap(old_address, old_size, new_size, flags, new_address));
if (Result == -1) {
return -errno;
@@ -602,9 +639,16 @@ uint64_t SyscallHandler::GuestMprotect(FEXCore::Core::InternalThreadState* Threa
@@ -602,9 +641,16 @@ uint64_t SyscallHandler::GuestMprotect(FEXCore::Core::InternalThreadState* Threa
}
}