From 88f11c3fd04be8febab80f7eb0a5c39847546009 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Fri, 4 Dec 2026 21:34:58 -0500 Subject: [PATCH] fex: leave aio rejections, keys and unwritable event buffers to the kernel A copied control block whose key slot is read-only is now passed to the kernel as the guest's own block at the end of the list, so the kernel reports its native fd, eventfd or key error. After io_submit, guest keys follow the private copies through the first rejected position, and they are stored through a fresh translation of the guest address, skipped when it no longer maps. io_getevents and io_pgetevents probe the guest event buffer and reap at most its writable prefix, handing an unwritable buffer to the kernel so completions are not consumed. --- ...uest-memory-above-host-address-limit.patch | 55 +++++++-- tools/tests/test_fex_guest_high_memory.py | 115 +++++++++++++++++- 2 files changed, 156 insertions(+), 14 deletions(-) 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 544f10ab19..43a5a7bdec 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,15 +13,15 @@ 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 | 2503 +++++++++++++++++ - .../LinuxSyscalls/GuestHighMemory.h | 60 + + .../LinuxSyscalls/GuestHighMemory.cpp | 2531 +++++++++++++++++ + .../LinuxSyscalls/GuestHighMemory.h | 61 + .../LinuxSyscalls/SignalDelegator.cpp | 5 + .../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 53 +- .../LinuxSyscalls/Syscalls/Memory.cpp | 30 + .../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +- .../LinuxSyscalls/ThreadManager.cpp | 2 + .../LinuxSyscalls/ThreadManager.h | 34 + - 9 files changed, 2719 insertions(+), 30 deletions(-) + 9 files changed, 2748 insertions(+), 30 deletions(-) create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp create mode 100644 Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h @@ -39,10 +39,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..414f6e7 +index 0000000..c81a146 --- /dev/null +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp -@@ -0,0 +1,2503 @@ +@@ -0,0 +1,2531 @@ +#include "LinuxSyscalls/GuestHighMemory.h" +#include "LinuxSyscalls/Syscalls.h" +#include "LinuxSyscalls/SignalDelegator.h" @@ -1876,6 +1876,20 @@ index 0000000..414f6e7 + constexpr uint64_t AioRingMagicOffset = 16; + constexpr uint32_t AioRingMagic = 0xa10a10a1; + ++ uint64_t WritableEvents(uint64_t Host, uint64_t Events) { ++ const auto End = Host + Events * IoEventSize; ++ auto Probe = Host; ++ while (Probe < End) { ++ uint8_t Byte {}; ++ if (FaultSafeUserMemAccess::CopyFromUser(&Byte, reinterpret_cast(Probe), sizeof(Byte)) != 0 || ++ FaultSafeUserMemAccess::CopyToUser(reinterpret_cast(Probe), &Byte, sizeof(Byte)) != 0) { ++ break; ++ } ++ Probe = (Probe | (FEXCore::Utils::FEX_PAGE_SIZE - 1)) + 1; ++ } ++ return (std::min(Probe, End) - Host) / IoEventSize; ++ } ++ + void ClearRingMagic(uint64_t ContextPointer) { + uint64_t Ring {}; + uint32_t Magic {}; @@ -1897,7 +1911,10 @@ index 0000000..414f6e7 + if (Wanted <= 0 || Guest[3] == 0) { + break; + } -+ const auto Events = std::min(Wanted, MaxIoEvents); ++ const auto Events = WritableEvents(Args[3], std::min(Wanted, MaxIoEvents)); ++ if (Events == 0) { ++ break; ++ } + if (static_cast(Wanted) > Events) { + if (static_cast(Args[1]) > static_cast(Events) && static_cast(Args[1]) <= Wanted) { + Args[1] = Events; @@ -1931,6 +1948,7 @@ index 0000000..414f6e7 + } + Storage->IoCopies.reserve(Blocks); + Storage->IoKeys.reserve(Blocks); ++ Storage->IoKeyValues.reserve(Blocks); + bool Refused = false; + bool Changed = false; + uint64_t Vectors = 0; @@ -1956,6 +1974,11 @@ index 0000000..414f6e7 + uint64_t Submitted = Host; + if (Buffer != Block.Buffer) { + if (FaultSafeUserMemAccess::CopyToUser(reinterpret_cast(Host + offsetof(IoControlBlock, Key)), &Block.Key, sizeof(Block.Key)) != 0) { ++ Storage->IoCopies.push_back(0); ++ Storage->IoKeys.push_back(0); ++ Storage->IoKeyValues.push_back(Block.Key); ++ Changed = Changed || Host != List[Usable]; ++ List[Usable++] = Host; + break; + } + Block.Buffer = Buffer; @@ -1969,7 +1992,8 @@ index 0000000..414f6e7 + Vectors += Block.Bytes; + } + Storage->IoCopies.push_back(Submitted != Host ? Submitted : 0); -+ Storage->IoKeys.push_back(Submitted != Host ? Host + offsetof(IoControlBlock, Key) : 0); ++ Storage->IoKeys.push_back(Submitted != Host ? List[Usable] + offsetof(IoControlBlock, Key) : 0); ++ Storage->IoKeyValues.push_back(Block.Key); + Changed = Changed || Submitted != List[Usable]; + List[Usable] = Submitted; + } @@ -2298,10 +2322,14 @@ index 0000000..414f6e7 + } Socket {std::exchange(Storage->Socket, -1)}; + if (!Storage->IoCopies.empty()) { + const auto Submitted = FEX::HLE::HasSyscallError(Result) ? 0 : std::min(Result, Storage->IoCopies.size()); -+ const uint32_t Accepted {}; -+ for (uint64_t i = 0; i < Submitted; ++i) { -+ if (Storage->IoKeys[i] != 0) { -+ static_cast(FaultSafeUserMemAccess::CopyToUser(reinterpret_cast(Storage->IoKeys[i]), &Accepted, sizeof(Accepted))); ++ const auto Examined = std::min(Submitted + 1, Storage->IoCopies.size()); ++ for (uint64_t i = 0; i < Examined; ++i) { ++ if (Storage->IoKeys[i] == 0) { ++ continue; ++ } ++ const uint32_t Key = i < Submitted ? 0 : reinterpret_cast(Storage->IoCopies[i])->Key; ++ if (Key != Storage->IoKeyValues[i]) { ++ static_cast(CopyToGuest(Storage->IoKeys[i], &Key, sizeof(Key))); + } + } + ReleaseIoCopies(Storage->IoCopies.data() + Submitted, Storage->IoCopies.size() - Submitted); @@ -2548,10 +2576,10 @@ index 0000000..414f6e7 +} // 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..b5a8ee3 +index 0000000..22a429d --- /dev/null +++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h -@@ -0,0 +1,60 @@ +@@ -0,0 +1,61 @@ +#pragma once + +#include @@ -2587,6 +2615,7 @@ index 0000000..b5a8ee3 + fextl::vector Words; + fextl::vector IoCopies; + fextl::vector IoKeys; ++ fextl::vector IoKeyValues; + fextl::vector EventBuffer; + uint64_t Events {}; + uint64_t SetupContext {}; diff --git a/tools/tests/test_fex_guest_high_memory.py b/tools/tests/test_fex_guest_high_memory.py index 418d416d4a..47f08ad7ab 100644 --- a/tools/tests/test_fex_guest_high_memory.py +++ b/tools/tests/test_fex_guest_high_memory.py @@ -831,15 +831,62 @@ int main() { uint64_t LateCancelArgs[6] = {4, reinterpret_cast(&KeyFirst), reinterpret_cast(&KeyCancelEvent), 0, 0, 0}; Call(Sys::SYSCALL_x64_io_cancel, LateCancelArgs, &LateCancel); Show("io_cancel_accepted_private", LateCancelArgs[1] == KeyFirstSent); + TestIocb KeyA = ReadBlock; + KeyA.Key = 7; + TestIocb KeyB = ReadBlock; + KeyB.Key = 8; + TestIocb KeyC = ReadBlock; + KeyC.Key = 9; + uint64_t KeyTriple[3] = {reinterpret_cast(&KeyA), reinterpret_cast(&KeyB), reinterpret_cast(&KeyC)}; + ArgumentStorage RejectSubmit; + uint64_t RejectSubmitArgs[6] = {6, 3, reinterpret_cast(KeyTriple), 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_submit, RejectSubmitArgs, &RejectSubmit); + const auto* RejectSent = reinterpret_cast(RejectSubmitArgs[2]); + reinterpret_cast(RejectSent[0])->Key = 0; + reinterpret_cast(RejectSent[1])->Key = 0; + FinishSyscallArguments(1, &RejectSubmit); + Show("io_submit_rejected_key_cleared", KeyA.Key == 0 && KeyB.Key == 0); + Show("io_submit_unreached_key", KeyC.Key); + TestIocb KeyD = ReadBlock; + KeyD.Key = 3; + uint64_t KeySingle[1] = {reinterpret_cast(&KeyD)}; + ArgumentStorage FirstRejected; + uint64_t FirstRejectedArgs[6] = {6, 1, reinterpret_cast(KeySingle), 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_submit, FirstRejectedArgs, &FirstRejected); + reinterpret_cast(reinterpret_cast(FirstRejectedArgs[2])[0])->Key = 0; + FinishSyscallArguments(static_cast(-EBADF), &FirstRejected); + Show("io_submit_first_rejected_key", KeyD.Key); + ArgumentStorage RejectDestroy; + uint64_t RejectDestroyArgs[6] = {6, 0, 0, 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_destroy, RejectDestroyArgs, &RejectDestroy); + FinishSyscallArguments(0, &RejectDestroy); + Show("io_submit_rejected_released", IoCopyCount.load() - CopiesBefore); auto* SealedEvents = static_cast(mmap(nullptr, Page, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + ArgumentStorage UnwritableReap; + uint64_t UnwritableReapArgs[6] = {4, 1, 1, reinterpret_cast(SealedEvents), 0, 0}; + Call(Sys::SYSCALL_x64_io_getevents, UnwritableReapArgs, &UnwritableReap); + Show("io_getevents_unwritable_native", UnwritableReapArgs[3] == reinterpret_cast(SealedEvents) && UnwritableReap.EventBuffer.empty()); + Show("io_getevents_unwritable_result", FinishSyscallArguments(static_cast(-EFAULT), &UnwritableReap)); + Show("io_getevents_unwritable_kept", IoCopyCount.load() - CopiesBefore); + mprotect(SealedEvents, Page, PROT_READ | PROT_WRITE); ArgumentStorage SealedReap; uint64_t SealedReapArgs[6] = {4, 1, 1, reinterpret_cast(SealedEvents), 0, 0}; Call(Sys::SYSCALL_x64_io_getevents, SealedReapArgs, &SealedReap); + mprotect(SealedEvents, Page, PROT_READ); const TestEvent SealedEvent {0x77, KeyFirstSent, 64, 0}; Deliver(SealedReapArgs, &SealedEvent, sizeof(SealedEvent)); Show("io_getevents_sealed_result", FinishSyscallArguments(1, &SealedReap)); Show("io_getevents_sealed_released", IoCopyCount.load() - CopiesBefore); munmap(SealedEvents, Page); + auto* EdgeEvents = static_cast(mmap(nullptr, 2 * Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); + mprotect(EdgeEvents + Page, Page, PROT_READ); + ArgumentStorage EdgeReap; + uint64_t EdgeReapArgs[6] = {4, 3, 4, reinterpret_cast(EdgeEvents + Page - 2 * sizeof(TestEvent)), 0, 0}; + Call(Sys::SYSCALL_x64_io_getevents, EdgeReapArgs, &EdgeReap); + Show("io_getevents_writable_prefix", EdgeReapArgs[2]); + Show("io_getevents_writable_min", EdgeReapArgs[1]); + Show("io_getevents_writable_buffer", EdgeReap.EventBuffer.size() * sizeof(uint64_t) / sizeof(TestEvent)); + munmap(EdgeEvents, 2 * Page); uint32_t Ring[8] = {0, 128, 0, 0, 0xa10a10a1, 1, 0, 32}; uint64_t RingContext = reinterpret_cast(Ring); ArgumentStorage Setup; @@ -1207,7 +1254,23 @@ int main() { ArgumentStorage SealedSubmit; uint64_t SealedSubmitArgs[6] = {1, 1, reinterpret_cast(HighIocbList), 0, 0, 0}; Show("io_submit_read_only_result", Call(Sys::SYSCALL_x64_io_submit, SealedSubmitArgs, &SealedSubmit)); + Show("io_submit_read_only_count", SealedSubmitArgs[1]); + Show("io_submit_read_only_passed", reinterpret_cast(SealedSubmitArgs[2])[0] == BlocksHost); Show("io_submit_read_only_copies", IoCopyCount.load() - CopiesBefore); + FinishSyscallArguments(static_cast(-EBADF), &SealedSubmit); + TestIocb Leading = ReadBlock; + uint64_t LeadingList[2] = {reinterpret_cast(&Leading), Blocks}; + ArgumentStorage LeadingSubmit; + uint64_t LeadingSubmitArgs[6] = {5, 2, reinterpret_cast(LeadingList), 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_submit, LeadingSubmitArgs, &LeadingSubmit); + const auto* LeadingSent = reinterpret_cast(LeadingSubmitArgs[2]); + Show("io_submit_read_only_after_copy", LeadingSubmitArgs[1] == 2 && FindIoCopyLocked(LeadingSent[0]) != nullptr && LeadingSent[1] == BlocksHost); + FinishSyscallArguments(1, &LeadingSubmit); + ArgumentStorage LeadingDestroy; + uint64_t LeadingDestroyArgs[6] = {5, 0, 0, 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_destroy, LeadingDestroyArgs, &LeadingDestroy); + FinishSyscallArguments(0, &LeadingDestroy); + Show("io_submit_read_only_after_copy_released", IoCopyCount.load() - CopiesBefore); Show("io_submit_read_only_untouched", std::memcmp(HighIocb, &HighExpected, sizeof(TestIocb)) == 0); Protect(Blocks, Page, PROT_READ | PROT_WRITE); HighIocb->Opcode = 1; @@ -1220,7 +1283,33 @@ int main() { Show("io_submit_read_only_native_block", reinterpret_cast(ReadOnlyNativeArgs[2])[0] == BlocksHost); Show("io_submit_read_only_native_copies", IoCopyCount.load() - CopiesBefore); Protect(Blocks, Page, PROT_READ | PROT_WRITE); + HighIocb->Opcode = 7; + HighIocb->Buffer = reinterpret_cast(Scatter); + HighIocb->Bytes = 2; + HighIocb->Key = 4; + ArgumentStorage KeyedHigh; + uint64_t KeyedHighArgs[6] = {7, 1, reinterpret_cast(HighIocbList), 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_submit, KeyedHighArgs, &KeyedHigh); + FinishSyscallArguments(1, &KeyedHigh); + Show("io_submit_high_key_accepted", HighIocb->Key); + HighIocb->Key = 4; + ArgumentStorage MovedHigh; + uint64_t MovedHighArgs[6] = {7, 1, reinterpret_cast(HighIocbList), 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_submit, MovedHighArgs, &MovedHigh); Unmap(Blocks, Page); + void* ReusedPage = mmap(reinterpret_cast(BlocksHost), Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); + Show("io_submit_key_reused_mapped", ReusedPage == reinterpret_cast(BlocksHost)); + if (ReusedPage == reinterpret_cast(BlocksHost)) { + static_cast(ReusedPage)->Key = 4; + FinishSyscallArguments(1, &MovedHigh); + Show("io_submit_key_unmapped_skipped", static_cast(ReusedPage)->Key); + munmap(ReusedPage, Page); + } + ArgumentStorage KeyedDestroy; + uint64_t KeyedDestroyArgs[6] = {7, 0, 0, 0, 0, 0}; + Call(Sys::SYSCALL_x64_io_destroy, KeyedDestroyArgs, &KeyedDestroy); + FinishSyscallArguments(0, &KeyedDestroy); + Show("io_submit_keyed_high_released", IoCopyCount.load() - CopiesBefore); auto* LowBlock = static_cast(mmap(nullptr, Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); *LowBlock = {}; @@ -2058,6 +2147,26 @@ class GuestHighMemoryTest(unittest.TestCase): self.assertEqual(self.value("io_getevents_sealed_result"), self.efault()) self.assertEqual(self.value("io_getevents_sealed_released"), 0) + def test_aio_reaps_only_into_writable_guest_events(self): + self.assertEqual(self.value("io_getevents_unwritable_native"), 1) + self.assertEqual(self.value("io_getevents_unwritable_result"), self.efault()) + self.assertEqual(self.value("io_getevents_unwritable_kept"), 1) + self.assertEqual(self.value("io_getevents_writable_prefix"), 2) + self.assertEqual(self.value("io_getevents_writable_min"), 2) + self.assertEqual(self.value("io_getevents_writable_buffer"), 2) + + def test_aio_keys_follow_the_kernel_through_the_first_rejection(self): + self.assertEqual(self.value("io_submit_rejected_key_cleared"), 1) + self.assertEqual(self.value("io_submit_unreached_key"), 9) + self.assertEqual(self.value("io_submit_first_rejected_key"), 0) + self.assertEqual(self.value("io_submit_rejected_released"), 1) + + def test_aio_keys_are_stored_through_the_current_guest_mapping(self): + self.assertEqual(self.value("io_submit_high_key_accepted"), 0) + self.assertEqual(self.value("io_submit_key_reused_mapped"), 1) + self.assertEqual(self.value("io_submit_key_unmapped_skipped"), 4) + self.assertEqual(self.value("io_submit_keyed_high_released"), 0) + def test_aio_rings_send_userspace_reapers_to_the_kernel(self): self.assertEqual(self.value("io_setup_magic"), 0) self.assertEqual(self.value("io_setup_header_kept"), 1) @@ -2085,8 +2194,12 @@ class GuestHighMemoryTest(unittest.TestCase): self.assertEqual(self.value("io_destroy_other_released"), 0) def test_read_only_control_blocks_fault_like_the_kernel(self): - self.assertEqual(self.value("io_submit_read_only_result"), self.efault()) + self.assertEqual(self.value("io_submit_read_only_result"), 0) + self.assertEqual(self.value("io_submit_read_only_count"), 1) + self.assertEqual(self.value("io_submit_read_only_passed"), 1) self.assertEqual(self.value("io_submit_read_only_copies"), 0) + self.assertEqual(self.value("io_submit_read_only_after_copy"), 1) + self.assertEqual(self.value("io_submit_read_only_after_copy_released"), 0) self.assertEqual(self.value("io_submit_read_only_native_result"), 0) self.assertEqual(self.value("io_submit_read_only_native_block"), 1) self.assertEqual(self.value("io_submit_read_only_native_copies"), 0)