From 1ff0ce142134eedf5d7cd19004de06fb9d271f13 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Thu, 3 Dec 2026 19:55:32 -0500 Subject: [PATCH] Leave MAP_FIXED_NOREPLACE above the host limit to the host Wine finds the top of the address space with MAP_FIXED_NOREPLACE probes. Claiming those probes above a 39-bit host limit made Wine place its stacks in relocated memory, and every wineserver request then failed with EFAULT because the iovec buffers it passes to writev are nested pointers that are not translated. Only MAP_FIXED requests are relocated again, so the probes see the real host limit while fixed mappings above it keep working. --- ...uest-memory-above-host-address-limit.patch | 42 ++++-------- tools/tests/test_fex_guest_high_memory.py | 66 +++++++++++++++++++ 2 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 tools/tests/test_fex_guest_high_memory.py diff --git a/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch b/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch index 9eab2c8cda..26e070b6e0 100644 --- a/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch +++ b/tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch @@ -13,19 +13,19 @@ 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 | 485 ++++++++++++++++++ + .../LinuxSyscalls/GuestHighMemory.cpp | 471 ++++++++++++++++++ .../LinuxSyscalls/GuestHighMemory.h | 26 + .../LinuxSyscalls/SignalDelegator.cpp | 5 + .../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 30 +- .../LinuxSyscalls/SyscallsSMCTracking.cpp | 60 ++- .../LinuxSyscalls/ThreadManager.cpp | 2 + .../LinuxSyscalls/ThreadManager.h | 12 + - 8 files changed, 599 insertions(+), 22 deletions(-) + 8 files changed, 585 insertions(+), 22 deletions(-) create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h diff --git a/Source/Tools/LinuxEmulation/CMakeLists.txt b/Source/Tools/LinuxEmulation/CMakeLists.txt -index 3f02104ea..acf4c4c13 100644 +index 3f02104..acf4c4c 100644 --- a/Source/Tools/LinuxEmulation/CMakeLists.txt +++ b/Source/Tools/LinuxEmulation/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(LinuxEmulation STATIC @@ -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..23781b213 +index 0000000..5be6708 --- /dev/null +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp -@@ -0,0 +1,485 @@ +@@ -0,0 +1,471 @@ +#include "LinuxSyscalls/GuestHighMemory.h" +#include "LinuxSyscalls/Syscalls.h" +#include "LinuxSyscalls/SignalDelegator.h" @@ -155,18 +155,6 @@ index 000000000..23781b213 + return true; + } + -+ bool OverlapsLocked(uint64_t Address, uint64_t Length) { -+ const auto Entries = Count.load(std::memory_order_relaxed); -+ for (uint32_t i = 0; i < Entries; ++i) { -+ const auto Guest = Regions[i].Guest.load(std::memory_order_relaxed); -+ const auto Size = Regions[i].Size.load(std::memory_order_relaxed); -+ if (Guest < Address + Length && Address < Guest + Size) { -+ return true; -+ } -+ } -+ return false; -+ } -+ + 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); @@ -288,7 +276,7 @@ index 000000000..23781b213 +} + +bool ClaimsMapping(uint64_t Address, uint64_t Length, int Flags) { -+ if (!(Flags & (MAP_FIXED | MAP_FIXED_NOREPLACE)) || Length == 0) { ++ if (!(Flags & MAP_FIXED) || (Flags & MAP_FIXED_NOREPLACE) || Length == 0) { + return false; + } + if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) { @@ -300,7 +288,7 @@ index 000000000..23781b213 + +uint64_t Map(uint64_t Address, uint64_t Length, int Prot, int Flags, int FD, off_t Offset) { + const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE); -+ const int HostFlags = Flags & ~(MAP_FIXED | MAP_FIXED_NOREPLACE); ++ const int HostFlags = Flags & ~MAP_FIXED; + void* Host = ::mmap(nullptr, Size, Prot, HostFlags, FD, Offset); + if (Host == MAP_FAILED) { + return -errno; @@ -308,9 +296,7 @@ index 000000000..23781b213 + + BeginWrite(); + int Result = 0; -+ if ((Flags & MAP_FIXED_NOREPLACE) && OverlapsLocked(Address, Size)) { -+ Result = -EEXIST; -+ } else if (!HasRoomLocked(Address, Size, 1)) { ++ if (!HasRoomLocked(Address, Size, 1)) { + Result = -ENOMEM; + } else { + Result = UnmapLocked(Address, Size); @@ -529,7 +515,7 @@ index 000000000..23781b213 +} // namespace FEX::HLE::GuestHighMemory diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h new file mode 100644 -index 000000000..326b1a37d +index 0000000..326b1a3 --- /dev/null +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h @@ -0,0 +1,26 @@ @@ -560,7 +546,7 @@ index 000000000..326b1a37d +void ReleaseThread(FEX::HLE::ThreadStateObject* Thread); +} // namespace FEX::HLE::GuestHighMemory diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp -index efe49dbf1..e8723e8f1 100644 +index efe49db..e8723e8 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SignalDelegator.cpp @@ -7,6 +7,7 @@ $end_info$ @@ -583,7 +569,7 @@ index efe49dbf1..e8723e8f1 100644 if (FaultSafeUserMemAccess::TryHandleSafeFault(Signal, SigInfo, UContext)) { diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp -index 607ab0b97..078faedac 100644 +index 607ab0b..078faed 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.cpp @@ -16,6 +16,7 @@ $end_info$ @@ -641,7 +627,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..ad4106d51 100644 +index f9e85a2..ad4106d 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/SyscallsSMCTracking.cpp @@ -21,6 +21,7 @@ $end_info$ @@ -788,7 +774,7 @@ index f9e85a26f..ad4106d51 100644 TrackMprotect(Thread, addr, len, prot); diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp -index e13cb1ca9..49df38c30 100644 +index e13cb1c..49df38c 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp @@ -1,6 +1,7 @@ @@ -808,7 +794,7 @@ index e13cb1ca9..49df38c30 100644 FEX::HLE::_SyscallHandler->SeccompEmulator.FreeSeccompFilters(Thread); diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h -index 657de6ceb..16ef19209 100644 +index 657de6c..16ef192 100644 --- a/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h @@ -103,6 +103,18 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators { diff --git a/tools/tests/test_fex_guest_high_memory.py b/tools/tests/test_fex_guest_high_memory.py new file mode 100644 index 0000000000..0a1222c4b7 --- /dev/null +++ b/tools/tests/test_fex_guest_high_memory.py @@ -0,0 +1,66 @@ +import re +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +REPO = Path(__file__).resolve().parents[2] +PATCH = REPO / "tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch" + +HARNESS = r""" +#include +#include +#include +namespace FEXCore { +namespace Utils { constexpr uint64_t FEX_PAGE_SIZE = 4096; } +constexpr uint64_t AlignUp(uint64_t V, uint64_t A) { return (V + A - 1) & ~(A - 1); } +} +static uint64_t HostLimit() { return 1ULL << 39; } +static constexpr uint64_t TaskTop = 1ULL << 47; +bool Contains(uint64_t Address) { + return Address >= HostLimit() && Address < TaskTop; +} +CLAIMS +int main() { + const uint64_t High = 0x7ffffffff000ULL; + const uint64_t Low = 0x140000000ULL; + std::printf("%d %d %d %d\n", + ClaimsMapping(High, 4096, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED), + ClaimsMapping(High, 4096, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE), + ClaimsMapping(Low, 4096, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED), + ClaimsMapping(High, 4096, MAP_PRIVATE | MAP_ANONYMOUS)); + return 0; +} +""" + + +def added_function(text, signature): + lines = [line[1:] for line in text.splitlines() if line.startswith("+")] + start = next(i for i, line in enumerate(lines) if line.startswith(signature)) + end = next(i for i in range(start, len(lines)) if lines[i] == "}") + return "\n".join(lines[start:end + 1]) + + +class GuestHighMemoryTest(unittest.TestCase): + def test_only_fixed_mappings_above_the_host_limit_are_relocated(self): + compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++") + if not compiler: + self.skipTest("no C++ compiler") + claims = added_function(PATCH.read_text(), "bool ClaimsMapping(") + with tempfile.TemporaryDirectory() as tmp: + source = Path(tmp) / "claims.cpp" + binary = Path(tmp) / "claims" + source.write_text(HARNESS.replace("CLAIMS", claims)) + subprocess.run([compiler, "-std=c++17", "-o", str(binary), str(source)], check=True) + result = subprocess.run([str(binary)], capture_output=True, text=True, check=True) + self.assertEqual(result.stdout.split(), ["1", "0", "0", "0"]) + + def test_no_replace_probes_reach_the_host(self): + text = PATCH.read_text() + self.assertIsNone(re.search(r"^\+.*Flags & \(MAP_FIXED \| MAP_FIXED_NOREPLACE\)", text, re.M)) + self.assertNotIn("OverlapsLocked", text) + + +if __name__ == "__main__": + unittest.main()