mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
FEX: list only guest mappings in /proc/<pid>/maps on hosts below 47 address bits
Credit the anti-cheat emulation source in the patch header.
This commit is contained in:
1 parent
6ac2deb1d3
commit
c36804466d
2 files changed
+130
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 <FEXCore/Config/Config.h>
|
||||
#include <FEXCore/Core/Context.h>
|
||||
#include <FEXCore/Core/CPUID.h>
|
||||
+#include <FEXCore/Utils/Allocator.h>
|
||||
#include <FEXCore/Utils/LogManager.h>
|
||||
+#include <FEXCore/Utils/SignalScopeGuards.h>
|
||||
+#include <FEXCore/fextl/unordered_map.h>
|
||||
+#include <FEXCore/fextl/vector.h>
|
||||
#include <FEXCore/fextl/fmt.h>
|
||||
#include <FEXCore/fextl/string.h>
|
||||
#include <FEXCore/fextl/sstream.h>
|
||||
@@ -25,9 +29,13 @@ $end_info$
|
||||
|
||||
#include <git_version.h>
|
||||
|
||||
+#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
+#include <elf.h>
|
||||
+#include <mutex>
|
||||
+#include <shared_mutex>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <ostream>
|
||||
@@ -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<fextl::string, bool>& 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<ssize_t>(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<std::pair<uint64_t, uint64_t>> GuestRanges;
|
||||
+ if (HostSharesGuestRange && Self && FEX::HLE::_SyscallHandler) {
|
||||
+ auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(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<uint64_t, uint64_t>& 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<fextl::string, bool> 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;
|
||||
Reference in new issue
Block a user