diff --git a/tools/fex/patches/0020-anticheat-ptrace-seccomp-and-proc-emulation.patch b/tools/fex/patches/0020-anticheat-ptrace-seccomp-and-proc-emulation.patch index 8fefa3d56f..801f82b457 100644 --- a/tools/fex/patches/0020-anticheat-ptrace-seccomp-and-proc-emulation.patch +++ b/tools/fex/patches/0020-anticheat-ptrace-seccomp-and-proc-emulation.patch @@ -1,3 +1,6 @@ +Source: https://github.com/StonkGuy/FEX branch eac-2611 (tag eac-2611.1) +Copyright (c) StonkGuy and FEX contributors, MIT licence + diff --git a/FEXCore/Source/CMakeLists.txt b/FEXCore/Source/CMakeLists.txt index 61385d2c3..b515a4a02 100644 --- a/FEXCore/Source/CMakeLists.txt diff --git a/tools/fex/patches/0021-proc-maps-guest-only-on-small-address-space-hosts.patch b/tools/fex/patches/0021-proc-maps-guest-only-on-small-address-space-hosts.patch new file mode 100644 index 0000000000..0fd7689615 --- /dev/null +++ b/tools/fex/patches/0021-proc-maps-guest-only-on-small-address-space-hosts.patch @@ -0,0 +1,127 @@ +diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp +index 1b2396b01..156e3964c 100644 +--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp ++++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/EmulatedFiles/EmulatedFiles.cpp +@@ -17,7 +17,11 @@ $end_info$ + #include + #include + #include ++#include + #include ++#include ++#include ++#include + #include + #include + #include +@@ -25,9 +29,13 @@ $end_info$ + + #include + ++#include + #include + #include + #include ++#include ++#include ++#include + #include + #include + #include +@@ -608,6 +616,37 @@ EmulatedFDManager::EmulatedFDManager(FEXCore::Context::Context* ctx) + + EmulatedFDManager::~EmulatedFDManager() {} + ++static bool IsSelfProcPath(const char* Path) { ++ const char* P = Path + 6; ++ if (!isdigit(*P)) { ++ return true; ++ } ++ return strtol(P, nullptr, 10) == ::getpid(); ++} ++ ++static bool IsHostArchModule(const char* Path, fextl::unordered_map& Seen) { ++ auto Known = Seen.find(Path); ++ if (Known != Seen.end()) { ++ return Known->second; ++ } ++ bool Host = false; ++ int FD = ::open(Path, O_RDONLY | O_CLOEXEC); ++ if (FD >= 0) { ++ Elf64_Ehdr Header {}; ++ if (::pread(FD, &Header, sizeof(Header), 0) >= static_cast(offsetof(Elf64_Ehdr, e_version)) && ++ memcmp(Header.e_ident, ELFMAG, SELFMAG) == 0) { ++#if defined(__aarch64__) ++ Host = Header.e_machine == EM_AARCH64; ++#else ++ Host = Header.e_machine != EM_X86_64 && Header.e_machine != EM_386; ++#endif ++ } ++ ::close(FD); ++ } ++ Seen.emplace(Path, Host); ++ return Host; ++} ++ + static bool IsProcMapsPath(const char* Path) { + if (strncmp(Path, "/proc/", 6) != 0) { + return false; +@@ -660,14 +699,56 @@ int32_t EmulatedFDManager::ProcMaps(FEXCore::Context::Context* ctx, int32_t fd, + ::close(HostFD); + + constexpr uint64_t X86_64TaskSizeMax = (1ULL << 47) - 4096; ++ const bool HostSharesGuestRange = FEXCore::Allocator::GetHostVABits() < 47; ++ const bool Self = IsSelfProcPath(pathname); ++ fextl::vector> GuestRanges; ++ if (HostSharesGuestRange && Self && FEX::HLE::_SyscallHandler) { ++ auto lk = FEXCore::MaskSignalsAndLockMutex(FEX::HLE::_SyscallHandler->VMATracking.Mutex); ++ GuestRanges.reserve(FEX::HLE::_SyscallHandler->VMATracking.VMAs.size()); ++ for (const auto& [Base, VMA] : FEX::HLE::_SyscallHandler->VMATracking.VMAs) { ++ if (!GuestRanges.empty() && GuestRanges.back().second >= Base) { ++ GuestRanges.back().second = std::max(GuestRanges.back().second, Base + VMA.Length); ++ } else { ++ GuestRanges.emplace_back(Base, Base + VMA.Length); ++ } ++ } ++ } ++ auto IsGuestRange = [&GuestRanges](uint64_t Start, uint64_t End) { ++ auto It = std::upper_bound(GuestRanges.begin(), GuestRanges.end(), Start, ++ [](uint64_t Value, const std::pair& Range) { return Value < Range.first; }); ++ if (It != GuestRanges.begin() && std::prev(It)->second > Start) { ++ return true; ++ } ++ return It != GuestRanges.end() && It->first < End; ++ }; ++ fextl::unordered_map ModuleArch; + fextl::string Out; + Out.reserve(In.size()); + size_t Pos = 0; + while (Pos < In.size()) { + size_t Eol = In.find('\n', Pos); + const size_t End = Eol == fextl::string::npos ? In.size() : Eol + 1; +- const uint64_t Start = strtoull(In.c_str() + Pos, nullptr, 16); +- if (Start < X86_64TaskSizeMax) { ++ char* Cursor {}; ++ const uint64_t Start = strtoull(In.c_str() + Pos, &Cursor, 16); ++ const uint64_t Stop = *Cursor == '-' ? strtoull(Cursor + 1, nullptr, 16) : Start; ++ bool Keep = Start < X86_64TaskSizeMax; ++ if (Keep && HostSharesGuestRange) { ++ if (Self) { ++ Keep = IsGuestRange(Start, Stop); ++ } else { ++ const size_t LineEnd = Eol == fextl::string::npos ? In.size() : Eol; ++ size_t Name = In.find_first_of("/[", Pos); ++ if (Name != fextl::string::npos && Name < LineEnd) { ++ fextl::string Path(In, Name, LineEnd - Name); ++ if (Path.starts_with("[anon:FEX")) { ++ Keep = false; ++ } else if (Path[0] == '/') { ++ Keep = !IsHostArchModule(Path.c_str(), ModuleArch); ++ } ++ } ++ } ++ } ++ if (Keep) { + Out.append(In, Pos, End - Pos); + } + Pos = End;