mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 21:15:43 +00:00
Reject wrapping lengths and drop stale code on discarded high memory
A nonzero length that wraps when page-aligned now fails madvise with EINVAL and mprotect with ENOMEM, as on Linux. Discarding advice on high memory invalidates translated code for the range whenever advice reached any region.
This commit is contained in:
1 parent
7ce0b702bb
commit
15b5d46f70
2 files changed
+90
-24
No files matched your search
@@ -14,15 +14,15 @@ Builds on StonkGuy's anti-cheat emulation from https://github.com/StonkGuy/FEX (
|
||||
---
|
||||
.../Tools/FEXInterpreter/FEXInterpreter.cpp | 2 +
|
||||
Source/Tools/LinuxEmulation/CMakeLists.txt | 1 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1031 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 42 +
|
||||
.../LinuxSyscalls/GuestHighMemory.cpp | 1051 +++++++++++++++++
|
||||
.../LinuxSyscalls/GuestHighMemory.h | 43 +
|
||||
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
|
||||
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 52 +-
|
||||
.../LinuxSyscalls/Syscalls/Memory.cpp | 14 +
|
||||
.../LinuxSyscalls/Syscalls/Memory.cpp | 30 +
|
||||
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +-
|
||||
.../LinuxSyscalls/ThreadManager.cpp | 2 +
|
||||
.../LinuxSyscalls/ThreadManager.h | 12 +
|
||||
10 files changed, 1192 insertions(+), 30 deletions(-)
|
||||
10 files changed, 1229 insertions(+), 30 deletions(-)
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
|
||||
@@ -60,10 +60,10 @@ index 3f02104..acf4c4c 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 0000000..ded90fa
|
||||
index 0000000..72a79f6
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
|
||||
@@ -0,0 +1,1031 @@
|
||||
@@ -0,0 +1,1051 @@
|
||||
+#include "LinuxSyscalls/GuestHighMemory.h"
|
||||
+#include "LinuxSyscalls/Syscalls.h"
|
||||
+#include "LinuxSyscalls/SignalDelegator.h"
|
||||
@@ -190,14 +190,17 @@ index 0000000..ded90fa
|
||||
+ }
|
||||
+
|
||||
+ template<typename Fn>
|
||||
+ uint64_t ApplyToRegionsLocked(uint64_t Address, uint64_t Length, Fn&& Apply) {
|
||||
+ uint64_t ApplyToRegionsLocked(uint64_t Address, uint64_t Length, uint64_t WrapError, Fn&& Apply) {
|
||||
+ if (Address & (FEXCore::Utils::FEX_PAGE_SIZE - 1)) {
|
||||
+ return -EINVAL;
|
||||
+ }
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ if (Size == 0) {
|
||||
+ if (Length == 0) {
|
||||
+ return 0;
|
||||
+ }
|
||||
+ const auto Size = FEXCore::AlignUp(Length, FEXCore::Utils::FEX_PAGE_SIZE);
|
||||
+ if (Size == 0 || Size > UINT64_MAX - Address) {
|
||||
+ return WrapError;
|
||||
+ }
|
||||
+ const auto End = Address + std::min(Size, TaskTop - Address);
|
||||
+ uint64_t Covered = 0;
|
||||
+ const auto Entries = Count.load(std::memory_order_acquire);
|
||||
@@ -883,11 +886,28 @@ index 0000000..ded90fa
|
||||
+}
|
||||
+
|
||||
+uint64_t Protect(uint64_t Address, uint64_t Length, int Prot) {
|
||||
+ return ApplyToRegionsLocked(Address, Length, [Prot](void* Host, uint64_t Size) { return ::mprotect(Host, Size, Prot); });
|
||||
+ return ApplyToRegionsLocked(Address, Length, -ENOMEM, [Prot](void* Host, uint64_t Size) { return ::mprotect(Host, Size, Prot); });
|
||||
+}
|
||||
+
|
||||
+uint64_t Advise(uint64_t Address, uint64_t Length, int Advice) {
|
||||
+ return ApplyToRegionsLocked(Address, Length, [Advice](void* Host, uint64_t Size) { return ::madvise(Host, Size, Advice); });
|
||||
+uint64_t Advise(uint64_t Address, uint64_t Length, int Advice, bool* Applied) {
|
||||
+ *Applied = false;
|
||||
+ return ApplyToRegionsLocked(Address, Length, -EINVAL, [Advice, Applied](void* Host, uint64_t Size) {
|
||||
+ const int Result = ::madvise(Host, Size, Advice);
|
||||
+ if (Result == 0) {
|
||||
+ *Applied = true;
|
||||
+ }
|
||||
+ return Result;
|
||||
+ });
|
||||
+}
|
||||
+
|
||||
+bool DiscardsContents(int Advice) {
|
||||
+ switch (Advice) {
|
||||
+ case MADV_DONTNEED:
|
||||
+ case MADV_FREE:
|
||||
+ case MADV_REMOVE:
|
||||
+ case MADV_DONTNEED_LOCKED: return true;
|
||||
+ default: return false;
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+uint64_t ToHost(uint64_t Address) {
|
||||
@@ -1097,10 +1117,10 @@ index 0000000..ded90fa
|
||||
+} // 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 0000000..0e22ac8
|
||||
index 0000000..b744a6d
|
||||
--- /dev/null
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
|
||||
@@ -0,0 +1,42 @@
|
||||
@@ -0,0 +1,43 @@
|
||||
+#pragma once
|
||||
+
|
||||
+#include <FEXCore/fextl/vector.h>
|
||||
@@ -1132,7 +1152,8 @@ index 0000000..0e22ac8
|
||||
+uint64_t Map(uint64_t Address, uint64_t Length, int Prot, int Flags, int FD, off_t Offset);
|
||||
+uint64_t Unmap(uint64_t Address, uint64_t Length);
|
||||
+uint64_t Protect(uint64_t Address, uint64_t Length, int Prot);
|
||||
+uint64_t Advise(uint64_t Address, uint64_t Length, int Advice);
|
||||
+uint64_t Advise(uint64_t Address, uint64_t Length, int Advice, bool* Applied);
|
||||
+bool DiscardsContents(int Advice);
|
||||
+uint64_t ToHost(uint64_t Address);
|
||||
+uint64_t ToGuest(uint64_t Address);
|
||||
+bool TranslateRange(uint64_t Address, uint64_t Length, uint64_t* Host);
|
||||
@@ -1245,10 +1266,10 @@ index 607ab0b..5e8bba1 100644
|
||||
#ifdef DEBUG_STRACE
|
||||
Strace(Frame, Result);
|
||||
diff --git a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Memory.cpp b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Memory.cpp
|
||||
index 9397c9e..1d30e6e 100644
|
||||
index 9397c9e..6095bf3 100644
|
||||
--- a/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Memory.cpp
|
||||
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Memory.cpp
|
||||
@@ -5,12 +5,15 @@ tags: LinuxSyscalls|syscalls-shared
|
||||
@@ -5,12 +5,19 @@ tags: LinuxSyscalls|syscalls-shared
|
||||
$end_info$
|
||||
*/
|
||||
|
||||
@@ -1258,21 +1279,37 @@ index 9397c9e..1d30e6e 100644
|
||||
#include "LinuxSyscalls/x32/Syscalls.h"
|
||||
|
||||
#include <FEXCore/Debug/InternalThreadState.h>
|
||||
+#include <FEXCore/Utils/LogManager.h>
|
||||
+#include <FEXCore/Utils/MathUtils.h>
|
||||
+#include <FEXCore/Utils/SignalScopeGuards.h>
|
||||
+#include <FEXCore/Utils/TypeDefines.h>
|
||||
|
||||
+#include <algorithm>
|
||||
+#include <shared_mutex>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
@@ -25,6 +28,17 @@ void RegisterMemory(FEX::HLE::SyscallHandler* Handler) {
|
||||
@@ -25,6 +32,29 @@ void RegisterMemory(FEX::HLE::SyscallHandler* Handler) {
|
||||
});
|
||||
|
||||
REGISTER_SYSCALL_IMPL(madvise, [](FEXCore::Core::CpuStateFrame* Frame, void* addr, size_t length, int32_t advice) -> uint64_t {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(reinterpret_cast<uint64_t>(addr))) {
|
||||
+ uint64_t Result {};
|
||||
+ bool Applied {};
|
||||
+ {
|
||||
+ auto lk = FEXCore::GuardSignalDeferringSection<std::shared_lock>(FEX::HLE::_SyscallHandler->VMATracking.Mutex, Frame->Thread);
|
||||
+ Result = FEX::HLE::GuestHighMemory::Advise(reinterpret_cast<uint64_t>(addr), length, advice);
|
||||
+ Result = FEX::HLE::GuestHighMemory::Advise(reinterpret_cast<uint64_t>(addr), length, advice, &Applied);
|
||||
+ }
|
||||
+ if (Applied && FEX::HLE::GuestHighMemory::DiscardsContents(advice)) {
|
||||
+ const auto Base = reinterpret_cast<uint64_t>(addr);
|
||||
+ const auto Size = std::min(FEXCore::AlignUp(length, FEXCore::Utils::FEX_PAGE_SIZE), UINT64_MAX - Base);
|
||||
+ FEX::HLE::_SyscallHandler->TM.InvalidateGuestCodeRange(Frame->Thread, Base, Size, [](uintptr_t Start, uintptr_t Length) {
|
||||
+ if (FEX::HLE::GuestHighMemory::Contains(Start)) {
|
||||
+ return;
|
||||
+ }
|
||||
+ auto rv = mprotect(reinterpret_cast<void*>(Start), Length, PROT_READ | PROT_WRITE);
|
||||
+ LogMan::Throw::AFmt(rv == 0, "mprotect({}, {}) failed", Start, Length);
|
||||
+ });
|
||||
+ }
|
||||
+ if (!FEX::HLE::HasSyscallError(Result)) {
|
||||
+ FEX::HLE::_SyscallHandler->TrackMadvise(Frame->Thread, (uintptr_t)addr, length, advice);
|
||||
|
||||
@@ -174,18 +174,30 @@ int main() {
|
||||
Applied.emplace_back(reinterpret_cast<uint64_t>(Host), Size);
|
||||
return 0;
|
||||
};
|
||||
Show("split_apply_result", ApplyToRegionsLocked(Split, 2 * Page, Record));
|
||||
Show("split_apply_result", ApplyToRegionsLocked(Split, 2 * Page, -EINVAL, Record));
|
||||
Show("split_apply_count", Applied.size());
|
||||
Show("split_apply_first", Applied[0].first - Base);
|
||||
Show("split_apply_second", Applied[1].first - Base);
|
||||
Show("split_apply_sizes", Applied[0].second + Applied[1].second);
|
||||
Applied.clear();
|
||||
Show("gap_apply_result", ApplyToRegionsLocked(Single, 2 * Page, Record));
|
||||
Show("gap_apply_result", ApplyToRegionsLocked(Single, 2 * Page, -EINVAL, Record));
|
||||
Show("gap_apply_count", Applied.size());
|
||||
Show("advise_split", Advise(Split, 2 * Page, MADV_DONTNEED));
|
||||
Show("advise_gap", Advise(Single, 2 * Page, MADV_DONTNEED));
|
||||
Show("advise_unaligned", Advise(Single + 8, Page, MADV_DONTNEED));
|
||||
bool Advised = false;
|
||||
Show("advise_split", Advise(Split, 2 * Page, MADV_DONTNEED, &Advised));
|
||||
Show("advise_split_applied", Advised);
|
||||
Show("advise_gap", Advise(Single, 2 * Page, MADV_DONTNEED, &Advised));
|
||||
Show("advise_gap_applied", Advised);
|
||||
Show("advise_unaligned", Advise(Single + 8, Page, MADV_DONTNEED, &Advised));
|
||||
Show("advise_unaligned_applied", Advised);
|
||||
Show("advise_zero", Advise(Single, 0, MADV_DONTNEED, &Advised));
|
||||
Show("advise_wrap", Advise(Single, UINT64_MAX - 16, MADV_DONTNEED, &Advised));
|
||||
Show("advise_wrap_applied", Advised);
|
||||
Show("protect_split", Protect(Split, 2 * Page, PROT_READ | PROT_WRITE));
|
||||
Show("protect_zero", Protect(Single, 0, PROT_READ | PROT_WRITE));
|
||||
Show("protect_wrap", Protect(Single, UINT64_MAX - 16, PROT_READ | PROT_WRITE));
|
||||
Show("discards", DiscardsContents(MADV_DONTNEED) + DiscardsContents(MADV_FREE) + DiscardsContents(MADV_REMOVE) +
|
||||
DiscardsContents(MADV_DONTNEED_LOCKED));
|
||||
Show("keeps", DiscardsContents(MADV_WILLNEED) + DiscardsContents(MADV_NORMAL) + DiscardsContents(MADV_HUGEPAGE));
|
||||
|
||||
SetHostAtomics(false);
|
||||
Show("claims_after_disable", ClaimsMapping(High, Page, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED));
|
||||
@@ -251,6 +263,7 @@ def harness():
|
||||
"bool ClaimsMapping(",
|
||||
"uint64_t Protect(",
|
||||
"uint64_t Advise(",
|
||||
"bool DiscardsContents(",
|
||||
"uint64_t ToGuest(",
|
||||
"bool TranslateRange(",
|
||||
"uint64_t TranslateSyscallArguments(",
|
||||
@@ -363,10 +376,26 @@ class GuestHighMemoryTest(unittest.TestCase):
|
||||
self.assertEqual(self.value("gap_apply_result"), (1 << 64) - 12)
|
||||
self.assertEqual(self.value("gap_apply_count"), 1)
|
||||
self.assertEqual(self.value("advise_split"), 0)
|
||||
self.assertEqual(self.value("advise_split_applied"), 1)
|
||||
self.assertEqual(self.value("advise_gap"), (1 << 64) - 12)
|
||||
self.assertEqual(self.value("advise_gap_applied"), 1)
|
||||
self.assertEqual(self.value("advise_unaligned"), (1 << 64) - 22)
|
||||
self.assertEqual(self.value("advise_unaligned_applied"), 0)
|
||||
self.assertEqual(self.value("protect_split"), 0)
|
||||
|
||||
def test_wrapping_lengths_fail_like_linux(self):
|
||||
self.assertEqual(self.value("advise_zero"), 0)
|
||||
self.assertEqual(self.value("advise_wrap"), (1 << 64) - 22)
|
||||
self.assertEqual(self.value("advise_wrap_applied"), 0)
|
||||
self.assertEqual(self.value("protect_zero"), 0)
|
||||
self.assertEqual(self.value("protect_wrap"), (1 << 64) - 12)
|
||||
|
||||
def test_discarding_advice_invalidates_high_code(self):
|
||||
self.assertEqual(self.value("discards"), 4)
|
||||
self.assertEqual(self.value("keeps"), 0)
|
||||
text = PATCH.read_text()
|
||||
self.assertRegex(text, r"\n\+ if \(Applied && FEX::HLE::GuestHighMemory::DiscardsContents\(advice\)\) \{")
|
||||
|
||||
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))
|
||||
|
||||
Reference in new issue
Block a user