Stop message batches at unreadable vectors, send singly without a spare descriptor and reserve aio restore slots

A message whose iovec snapshot cannot be taken ends the prepared batch
instead of being sent with an unknown length; when it is the first
message the kernel handles the original array natively. When the
batch socket cannot be duplicated, messages still go one at a time on
the original descriptor with each length written before the next send.
Pending aio restore slots are reserved under the pending mutex before
the first guest edit and released or handed to the pending record, so
concurrent submissions cannot exceed the bound. Tests use a real
socket pair, injected descriptor exhaustion and concurrent
reservations.
This commit is contained in:
maxjivi05 committed 2026-12-04 14:41:00 -05:00
1 parent 7a396aac61
commit 0f54bc6f45
2 files changed
+309 -68

No files matched your search

@@ -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 | 2049 +++++++++++++++++
.../LinuxSyscalls/GuestHighMemory.h | 60 +
.../LinuxSyscalls/GuestHighMemory.cpp | 2085 +++++++++++++++++
.../LinuxSyscalls/GuestHighMemory.h | 62 +
.../LinuxSyscalls/SignalDelegator.cpp | 5 +
.../LinuxEmulation/LinuxSyscalls/Syscalls.cpp | 52 +-
.../LinuxSyscalls/Syscalls/Memory.cpp | 30 +
.../LinuxSyscalls/SyscallsSMCTracking.cpp | 61 +-
.../LinuxSyscalls/ThreadManager.cpp | 2 +
.../LinuxSyscalls/ThreadManager.h | 19 +
9 files changed, 2249 insertions(+), 30 deletions(-)
9 files changed, 2287 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..caf9046
index 0000000..1d4774b
--- /dev/null
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp
@@ -0,0 +1,2049 @@
@@ -0,0 +1,2085 @@
+#include "LinuxSyscalls/GuestHighMemory.h"
+#include "LinuxSyscalls/Syscalls.h"
+#include "LinuxSyscalls/SignalDelegator.h"
@@ -1317,6 +1317,14 @@ index 0000000..caf9046
+ Ready = i;
+ break;
+ }
+ if (!Received && Length != 0 && !Copied) {
+ if (i == 0) {
+ *Array = HostArray;
+ return 0;
+ }
+ Ready = i;
+ break;
+ }
+ if (Copied && !Received) {
+ Copy[i].msg_hdr.msg_iov = Local;
+ for (uint64_t j = 0; j < Length; ++j) {
@@ -1343,11 +1351,10 @@ index 0000000..caf9046
+ Storage->ReceivedMessages = Received;
+ Storage->SendFlags = static_cast<int>(Args[3]);
+ if (!Received && Ready > 1) {
+ Storage->Socket = fcntl(static_cast<int>(Args[0]), F_DUPFD_CLOEXEC, 0);
+ Storage->SendSocket = static_cast<int>(Args[0]);
+ Storage->Socket = fcntl(Storage->SendSocket, F_DUPFD_CLOEXEC, 0);
+ if (Storage->Socket >= 0) {
+ Args[0] = static_cast<uint64_t>(Storage->Socket);
+ } else {
+ *Entries = Ready;
+ }
+ }
+ return 0;
@@ -1463,6 +1470,7 @@ index 0000000..caf9046
+ constexpr size_t MaxPendingRestores = 64;
+ std::mutex PendingMutex;
+ fextl::vector<PendingRestore> PendingRestores;
+ size_t PendingReservations {};
+
+ bool ForceAccess(uint64_t Host, uint64_t* Value, bool Write) {
+ const int FD = open("/proc/self/mem", (Write ? O_RDWR : O_RDONLY) | O_CLOEXEC);
@@ -1514,7 +1522,7 @@ index 0000000..caf9046
+ *Words = std::move(Left);
+ }
+
+ size_t ReconcilePending() {
+ void ReconcilePending() {
+ auto lk = FEXCore::MaskSignalsAndLockMutex(PendingMutex);
+ fextl::vector<PendingRestore> Left;
+ for (auto& Pending : PendingRestores) {
@@ -1524,11 +1532,36 @@ index 0000000..caf9046
+ }
+ }
+ PendingRestores = std::move(Left);
+ return PendingRestores.size();
+ }
+
+ bool ReserveSlot(ArgumentStorage* Storage) {
+ if (Storage->PendingSlot) {
+ return true;
+ }
+ auto lk = FEXCore::MaskSignalsAndLockMutex(PendingMutex);
+ if (PendingRestores.size() + PendingReservations >= MaxPendingRestores) {
+ return false;
+ }
+ ++PendingReservations;
+ Storage->PendingSlot = true;
+ return true;
+ }
+
+ void ReleaseSlot(ArgumentStorage* Storage) {
+ if (!Storage->PendingSlot) {
+ return;
+ }
+ auto lk = FEXCore::MaskSignalsAndLockMutex(PendingMutex);
+ --PendingReservations;
+ Storage->PendingSlot = false;
+ }
+
+ void KeepPending(ArgumentStorage* Storage) {
+ auto lk = FEXCore::MaskSignalsAndLockMutex(PendingMutex);
+ if (Storage->PendingSlot) {
+ --PendingReservations;
+ Storage->PendingSlot = false;
+ }
+ PendingRestores.push_back({std::move(Storage->Restores), std::move(Storage->Heap)});
+ Storage->Restores = {};
+ Storage->Heap = {};
@@ -1557,7 +1590,7 @@ index 0000000..caf9046
+ }
+ Storage->Restores.reserve(Blocks);
+ auto lk = FEXCore::MaskSignalsAndLockMutex<std::shared_lock>(FEX::HLE::_SyscallHandler->VMATracking.Mutex);
+ const bool Full = ReconcilePending() >= MaxPendingRestores;
+ ReconcilePending();
+ bool Refused = false;
+ bool Changed = false;
+ uint64_t Vectors = 0;
@@ -1580,7 +1613,7 @@ index 0000000..caf9046
+ if (TranslateIoBuffer(Block, Storage->Heap.data() + Vectors, &Buffer) != 0) {
+ break;
+ }
+ if (Buffer != Block.Buffer && Full) {
+ if (Buffer != Block.Buffer && !ReserveSlot(Storage)) {
+ Refused = true;
+ break;
+ }
@@ -1594,6 +1627,7 @@ index 0000000..caf9046
+ List[Usable] = Host;
+ }
+ if (Usable == 0) {
+ ReleaseSlot(Storage);
+ return Refused ? -EAGAIN : -EFAULT;
+ }
+ if (Changed) {
@@ -1917,6 +1951,7 @@ index 0000000..caf9046
+ KeepPending(Storage);
+ }
+ }
+ ReleaseSlot(Storage);
+ if (Storage->GuestCapabilityHeader != 0 && Storage->CapabilityHeader[0] != Storage->CapabilityVersion &&
+ !CopyToGuest(Storage->GuestCapabilityHeader, &Storage->CapabilityHeader[0], sizeof(Storage->CapabilityHeader[0]))) {
+ return -EFAULT;
@@ -1937,12 +1972,13 @@ index 0000000..caf9046
+ }
+ }
+ if (Storage->GuestMessages != 0) {
+ const bool FollowUps = !Storage->ReceivedMessages && Socket.FD >= 0;
+ const int SendFD = Socket.FD >= 0 ? Socket.FD : Storage->SendSocket;
+ const bool FollowUps = !Storage->ReceivedMessages && SendFD >= 0;
+ const auto Total = FollowUps ? Storage->Messages.size() : std::min<uint64_t>(Result, Storage->Messages.size());
+ uint64_t Done = 0;
+ for (; Done < Total; ++Done) {
+ auto& Message = Storage->Messages[Done];
+ if (Done >= Result && ::sendmmsg(Socket.FD, &Message, 1, Storage->SendFlags) != 1) {
+ if (Done >= Result && ::sendmmsg(SendFD, &Message, 1, Storage->SendFlags) != 1) {
+ break;
+ }
+ const auto Guest = Storage->GuestMessages + Done * sizeof(struct mmsghdr);
@@ -2094,10 +2130,10 @@ index 0000000..caf9046
+} // 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..38e9793
index 0000000..ce1e06d
--- /dev/null
+++ b/Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h
@@ -0,0 +1,60 @@
@@ -0,0 +1,62 @@
+#pragma once
+
+#include <FEXCore/fextl/vector.h>
@@ -2133,6 +2169,8 @@ index 0000000..38e9793
+ uint64_t GuestMessages {};
+ bool ReceivedMessages {};
+ int Socket {-1};
+ int SendSocket {-1};
+ bool PendingSlot {};
+ int SendFlags {};
+ uint32_t CapabilityHeader[2] {};
+ uint32_t CapabilityVersion {};
+255 -52
View File
@@ -42,6 +42,7 @@ PRELUDE = r"""
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <thread>
#include <type_traits>
#include <unistd.h>
namespace fextl { template<typename T> using vector = std::vector<T>; }
@@ -70,7 +71,12 @@ static int SentSocket = -1;
static size_t SentLengths[8];
static struct iovec* GrowAfterSend;
static void* UnmapAfterSend;
static int TestSendmmsg(int Socket, struct mmsghdr* Messages, unsigned int Count, int) {
static bool PassSend;
static int DupFailures;
static int TestSendmmsg(int Socket, struct mmsghdr* Messages, unsigned int Count, int Flags) {
if (PassSend) {
return sendmmsg(Socket, Messages, Count, Flags);
}
SentSocket = Socket;
if (Count != 1 || SendFailures > 0) {
SendFailures -= SendFailures > 0;
@@ -94,6 +100,15 @@ static int TestSendmmsg(int Socket, struct mmsghdr* Messages, unsigned int Count
return 1;
}
#define sendmmsg TestSendmmsg
static int TestFcntl(int FD, int Command, int Argument = 0) {
if (Command == F_DUPFD_CLOEXEC && DupFailures > 0) {
--DupFailures;
errno = EMFILE;
return -1;
}
return fcntl(FD, Command, Argument);
}
#define fcntl TestFcntl
static bool ForcedEmulated;
static int ForcedPageProt = PROT_READ;
static int ForcedReads;
@@ -1202,6 +1217,80 @@ int main() {
Show("io_submit_full_untouched", FullBlock.Buffer - Single);
Show("io_submit_full_plain_result", Reconcile());
Show("io_submit_full_kept", PendingRestores.size() == MaxPendingRestores);
Show("io_submit_reservations_idle", PendingReservations);
PendingRestores.pop_back();
TestIocb FirstBlock {};
FirstBlock.Buffer = Single + 768;
FirstBlock.Bytes = 64;
uint64_t FirstList[1] = {reinterpret_cast<uint64_t>(&FirstBlock)};
ArgumentStorage FirstSubmit;
uint64_t FirstSubmitArgs[6] = {1, 1, reinterpret_cast<uint64_t>(FirstList), 0, 0, 0};
Show("io_submit_last_slot_first", Call(Sys::SYSCALL_x64_io_submit, FirstSubmitArgs, &FirstSubmit));
Show("io_submit_last_slot_reserved", PendingReservations * 0x10 + FirstSubmit.PendingSlot);
TestIocb SecondBlock {};
SecondBlock.Buffer = Single + 768;
SecondBlock.Bytes = 64;
uint64_t SecondList[1] = {reinterpret_cast<uint64_t>(&SecondBlock)};
ArgumentStorage SecondSubmit;
uint64_t SecondSubmitArgs[6] = {1, 1, reinterpret_cast<uint64_t>(SecondList), 0, 0, 0};
Show("io_submit_last_slot_second", Call(Sys::SYSCALL_x64_io_submit, SecondSubmitArgs, &SecondSubmit));
Show("io_submit_last_slot_second_untouched", SecondBlock.Buffer - Single);
Show("io_submit_last_slot_second_holds", SecondSubmit.PendingSlot);
Show("io_submit_last_slot_reserved_still", PendingReservations);
std::atomic<size_t> Granted {0};
std::vector<std::vector<ArgumentStorage>> Contenders(16);
std::vector<std::thread> Threads;
for (auto& Contender : Contenders) {
Contender.resize(8);
Threads.emplace_back([&Contender, &Granted] {
for (auto& Attempt : Contender) {
if (ReserveSlot(&Attempt)) {
++Granted;
}
}
});
}
for (auto& Thread : Threads) {
Thread.join();
}
Show("io_submit_contended_granted", Granted);
Show("io_submit_contended_reservations", PendingReservations);
for (auto& Contender : Contenders) {
for (auto& Attempt : Contender) {
ReleaseSlot(&Attempt);
}
}
Show("io_submit_contended_released", PendingReservations);
FinishResult(1, &SecondSubmit);
Show("io_submit_last_slot_finish", FinishResult(1, &FirstSubmit));
Show("io_submit_last_slot_restored", FirstBlock.Buffer - Single);
Show("io_submit_last_slot_free", PendingReservations * 0x10 + FirstSubmit.PendingSlot);
Show("io_submit_last_slot_pending", PendingRestores.size() == MaxPendingRestores - 1);
std::vector<std::vector<ArgumentStorage>> Room(16);
std::atomic<size_t> Fitted {0};
const size_t Vacancies = MaxPendingRestores - PendingRestores.size() - PendingReservations;
std::vector<std::thread> Crowd;
for (auto& Contender : Room) {
Contender.resize(8);
Crowd.emplace_back([&Contender, &Fitted] {
for (auto& Attempt : Contender) {
if (ReserveSlot(&Attempt)) {
++Fitted;
}
}
});
}
for (auto& Thread : Crowd) {
Thread.join();
}
Show("io_submit_crowd_fitted", Fitted == Vacancies && PendingReservations == Vacancies);
for (auto& Contender : Room) {
for (auto& Attempt : Contender) {
ReleaseSlot(&Attempt);
}
}
Show("io_submit_crowd_released", PendingReservations);
ForcedReadFailures = 0;
Protect(Sealing, Page, PROT_READ | PROT_WRITE);
Reconcile();
@@ -1260,43 +1349,116 @@ int main() {
const int FailedPin = ShortFailed.Socket;
Show("sendmmsg_error_finish", FinishResult(static_cast<uint64_t>(-EAGAIN), &ShortFailed));
Show("sendmmsg_error_closed", FailedPin >= 0 && fcntl(FailedPin, F_GETFD) == -1 && errno == EBADF);
const int Closed = dup(Peer);
close(Closed);
ArgumentStorage Unpinned;
uint64_t UnpinnedArgs[6] = {static_cast<uint64_t>(Closed), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
Show("sendmmsg_unpinned_result", Call(Sys::SYSCALL_x64_sendmmsg, UnpinnedArgs, &Unpinned));
Show("sendmmsg_unpinned_submitted", UnpinnedArgs[2]);
Show("sendmmsg_unpinned_queued", Unpinned.Messages.size());
Show("sendmmsg_unpinned_socket", UnpinnedArgs[0] == static_cast<uint64_t>(Closed) && Unpinned.Socket == -1);
for (uint64_t i = 0; i < 3; ++i) {
reinterpret_cast<struct mmsghdr*>(UnpinnedArgs[1])[i].msg_len = 32 - i;
int Pair[2];
socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, Pair);
struct iovec Payloads[3] = {};
for (int i = 0; i < 3; ++i) {
std::memset(Backing + 2048 + 16 * i, 'A' + i, 4);
Payloads[i] = {reinterpret_cast<void*>(Single + 2048 + 16 * i), 4};
}
SendCalls = 0;
Show("sendmmsg_unpinned_finish", FinishResult(3, &Unpinned));
Show("sendmmsg_unpinned_sends", SendCalls);
Show("sendmmsg_unpinned_lengths", Shorts[0].msg_len * 0x10000 + Shorts[1].msg_len * 0x100 + Shorts[2].msg_len);
for (auto& Short : Shorts) {
Short.msg_len = 0;
auto Drain = [&] {
uint64_t Packed = 0;
unsigned char Data[16];
while (recv(Pair[1], Data, sizeof(Data), MSG_DONTWAIT) > 0) {
Packed = Packed * 0x100 + Data[0];
}
return Packed;
};
auto Dispatch = [&](uint64_t* Args) {
return static_cast<uint64_t>(sendmmsg(static_cast<int>(Args[0]), reinterpret_cast<struct mmsghdr*>(Args[1]), static_cast<unsigned>(Args[2]), 0));
};
auto Lengths = [&](struct mmsghdr* Headers) {
return Headers[0].msg_len * 0x10000 + Headers[1].msg_len * 0x100 + Headers[2].msg_len;
};
PassSend = true;
struct mmsghdr Real[3] {};
for (int i = 0; i < 3; ++i) {
Real[i].msg_hdr.msg_iov = &Payloads[i];
Real[i].msg_hdr.msg_iovlen = 1;
}
ArgumentStorage UnpinnedPrefix;
uint64_t UnpinnedPrefixArgs[6] = {static_cast<uint64_t>(Closed), reinterpret_cast<uint64_t>(Shorts), 3, 0, 0, 0};
Call(Sys::SYSCALL_x64_sendmmsg, UnpinnedPrefixArgs, &UnpinnedPrefix);
reinterpret_cast<struct mmsghdr*>(UnpinnedPrefixArgs[1])[0].msg_len = 32;
Show("sendmmsg_unpinned_prefix_finish", FinishResult(1, &UnpinnedPrefix));
Show("sendmmsg_unpinned_prefix_lengths", Shorts[0].msg_len * 0x10000 + Shorts[1].msg_len * 0x100 + Shorts[2].msg_len);
SealedHeaders[0].msg_len = 0;
ArgumentStorage SealedWhole;
uint64_t SealedWholeArgs[6] = {static_cast<uint64_t>(Closed), Sealed + Page - sizeof(struct mmsghdr), 3, 0, 0, 0};
Show("sealed_whole_result", Call(Sys::SYSCALL_x64_sendmmsg, SealedWholeArgs, &SealedWhole));
Show("sealed_whole_submitted", SealedWholeArgs[2]);
for (uint64_t i = 0; i < 3; ++i) {
reinterpret_cast<struct mmsghdr*>(SealedWholeArgs[1])[i].msg_len = 32;
DupFailures = 1;
ArgumentStorage Emfile;
uint64_t EmfileArgs[6] = {static_cast<uint64_t>(Pair[0]), reinterpret_cast<uint64_t>(Real), 3, 0, 0, 0};
Show("sendmmsg_emfile_result", Call(Sys::SYSCALL_x64_sendmmsg, EmfileArgs, &Emfile));
Show("sendmmsg_emfile_submitted", EmfileArgs[2]);
Show("sendmmsg_emfile_original", EmfileArgs[0] == static_cast<uint64_t>(Pair[0]) && Emfile.Socket == -1 && DupFailures == 0);
Show("sendmmsg_emfile_first_only", Drain());
Show("sendmmsg_emfile_finish", FinishResult(Dispatch(EmfileArgs), &Emfile));
Show("sendmmsg_emfile_received", Drain());
Show("sendmmsg_emfile_lengths", Lengths(Real));
const uint64_t ReadOnly = 0x730000000000ULL;
Map(ReadOnly, 2 * Page, PROT_READ | PROT_WRITE, Anonymous, -1, 0);
uint64_t ReadOnlyHost {};
TranslateRange(ReadOnly, 2 * Page, &ReadOnlyHost);
auto* ReadOnlyHeaders = reinterpret_cast<struct mmsghdr*>(ReadOnlyHost + Page - sizeof(struct mmsghdr));
for (int i = 0; i < 3; ++i) {
ReadOnlyHeaders[i] = {};
ReadOnlyHeaders[i].msg_hdr.msg_iov = &Payloads[i];
ReadOnlyHeaders[i].msg_hdr.msg_iovlen = 1;
}
SendCalls = 0;
Show("sealed_whole_finish", FinishResult(3, &SealedWhole));
Show("sealed_whole_sends", SendCalls);
Show("sealed_whole_first_length", SealedHeaders[0].msg_len);
Show("sealed_whole_second_length", SealedHeaders[1].msg_len);
Protect(ReadOnly + Page, Page, PROT_READ);
DupFailures = 1;
ArgumentStorage ReadOnlyBatch;
uint64_t ReadOnlyArgs[6] = {static_cast<uint64_t>(Pair[0]), ReadOnly + Page - sizeof(struct mmsghdr), 3, 0, 0, 0};
Show("sendmmsg_emfile_readonly_result", Call(Sys::SYSCALL_x64_sendmmsg, ReadOnlyArgs, &ReadOnlyBatch));
Show("sendmmsg_emfile_readonly_submitted", ReadOnlyArgs[2]);
Show("sendmmsg_emfile_readonly_finish", FinishResult(Dispatch(ReadOnlyArgs), &ReadOnlyBatch));
Show("sendmmsg_emfile_readonly_received", Drain());
Show("sendmmsg_emfile_readonly_lengths", Lengths(ReadOnlyHeaders));
DupFailures = 1;
ArgumentStorage ReadOnlyFirst;
uint64_t ReadOnlyFirstArgs[6] = {static_cast<uint64_t>(Pair[0]), ReadOnly + Page, 1, 0, 0, 0};
Show("sendmmsg_emfile_only_result", Call(Sys::SYSCALL_x64_sendmmsg, ReadOnlyFirstArgs, &ReadOnlyFirst));
Show("sendmmsg_emfile_only_finish", FinishResult(Dispatch(ReadOnlyFirstArgs), &ReadOnlyFirst));
Show("sendmmsg_emfile_only_received", Drain());
DupFailures = 0;
auto* Unreadable = static_cast<struct iovec*>(mmap(nullptr, Page, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
struct mmsghdr Middle[3] {};
for (int i = 0; i < 3; ++i) {
Middle[i].msg_hdr.msg_iov = i == 1 ? Unreadable : &Payloads[i];
Middle[i].msg_hdr.msg_iovlen = 1;
}
ArgumentStorage MiddleBatch;
uint64_t MiddleArgs[6] = {static_cast<uint64_t>(Pair[0]), reinterpret_cast<uint64_t>(Middle), 3, 0, 0, 0};
Show("sendmmsg_unreadable_middle_result", Call(Sys::SYSCALL_x64_sendmmsg, MiddleArgs, &MiddleBatch));
Show("sendmmsg_unreadable_middle_submitted", MiddleArgs[2]);
Show("sendmmsg_unreadable_middle_queued", MiddleBatch.Messages.size());
Show("sendmmsg_unreadable_middle_finish", FinishResult(Dispatch(MiddleArgs), &MiddleBatch));
Show("sendmmsg_unreadable_middle_received", Drain());
Show("sendmmsg_unreadable_middle_lengths", Lengths(Middle));
struct mmsghdr Last[3] {};
for (int i = 0; i < 3; ++i) {
Last[i].msg_hdr.msg_iov = i == 2 ? Unreadable : &Payloads[i];
Last[i].msg_hdr.msg_iovlen = 1;
}
ArgumentStorage LastBatch;
uint64_t LastArgs[6] = {static_cast<uint64_t>(Pair[0]), reinterpret_cast<uint64_t>(Last), 3, 0, 0, 0};
Show("sendmmsg_unreadable_last_result", Call(Sys::SYSCALL_x64_sendmmsg, LastArgs, &LastBatch));
Show("sendmmsg_unreadable_last_submitted", LastArgs[2]);
Show("sendmmsg_unreadable_last_queued", LastBatch.Messages.size());
const int LastPin = LastBatch.Socket;
Show("sendmmsg_unreadable_last_pinned", LastPin >= 0 && LastArgs[0] == static_cast<uint64_t>(LastPin));
Show("sendmmsg_unreadable_last_finish", FinishResult(Dispatch(LastArgs), &LastBatch));
Show("sendmmsg_unreadable_last_received", Drain());
Show("sendmmsg_unreadable_last_lengths", Lengths(Last));
struct mmsghdr First[3] {};
for (int i = 0; i < 3; ++i) {
First[i].msg_hdr.msg_iov = i == 0 ? Unreadable : &Payloads[i];
First[i].msg_hdr.msg_iovlen = 1;
}
ArgumentStorage FirstBatch;
uint64_t FirstArgs[6] = {static_cast<uint64_t>(Pair[0]), reinterpret_cast<uint64_t>(First), 3, 0, 0, 0};
Show("sendmmsg_unreadable_first_result", Call(Sys::SYSCALL_x64_sendmmsg, FirstArgs, &FirstBatch));
Show("sendmmsg_unreadable_first_native", FirstArgs[1] == reinterpret_cast<uint64_t>(First) && FirstArgs[2] == 3 && FirstBatch.GuestMessages == 0);
Show("sendmmsg_unreadable_first_error", static_cast<uint64_t>(sendmmsg(Pair[0], First, 3, 0)) == static_cast<uint64_t>(-1) && errno == EFAULT);
Show("sendmmsg_unreadable_first_received", Drain());
PassSend = false;
auto* LowVectors = static_cast<struct iovec*>(mmap(nullptr, Page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
char KeptLow[16];
@@ -1455,7 +1617,7 @@ class GuestHighMemoryTest(unittest.TestCase):
source = Path(tmp) / "high.cpp"
binary = Path(tmp) / "high"
source.write_text(harness())
subprocess.run([compiler, "-std=c++20", "-o", str(binary), str(source), "-latomic"], check=True)
subprocess.run([compiler, "-std=c++20", "-o", str(binary), str(source), "-latomic", "-pthread"], check=True)
output = subprocess.run([str(binary)], capture_output=True, text=True, check=True).stdout
cls.results = dict(line.split() for line in output.splitlines())
@@ -1835,22 +1997,63 @@ class GuestHighMemoryTest(unittest.TestCase):
self.assertEqual(self.value("sendmmsg_error_finish"), (1 << 64) - 11)
self.assertEqual(self.value("sendmmsg_error_closed"), 1)
def test_unpinned_message_batches_go_to_the_kernel_whole(self):
self.assertEqual(self.value("sendmmsg_unpinned_result"), 0)
self.assertEqual(self.value("sendmmsg_unpinned_submitted"), 3)
self.assertEqual(self.value("sendmmsg_unpinned_queued"), 3)
self.assertEqual(self.value("sendmmsg_unpinned_socket"), 1)
self.assertEqual(self.value("sendmmsg_unpinned_finish"), 3)
self.assertEqual(self.value("sendmmsg_unpinned_sends"), 0)
self.assertEqual(self.value("sendmmsg_unpinned_lengths"), 0x201F1E)
self.assertEqual(self.value("sendmmsg_unpinned_prefix_finish"), 1)
self.assertEqual(self.value("sendmmsg_unpinned_prefix_lengths"), 0x200000)
self.assertEqual(self.value("sealed_whole_result"), 0)
self.assertEqual(self.value("sealed_whole_submitted"), 3)
self.assertEqual(self.value("sealed_whole_finish"), 1)
self.assertEqual(self.value("sealed_whole_sends"), 0)
self.assertEqual(self.value("sealed_whole_first_length"), 32)
self.assertEqual(self.value("sealed_whole_second_length"), 0)
def test_batches_without_a_spare_descriptor_still_send_one_message_at_a_time(self):
self.assertEqual(self.value("sendmmsg_emfile_result"), 0)
self.assertEqual(self.value("sendmmsg_emfile_submitted"), 1)
self.assertEqual(self.value("sendmmsg_emfile_original"), 1)
self.assertEqual(self.value("sendmmsg_emfile_first_only"), 0)
self.assertEqual(self.value("sendmmsg_emfile_finish"), 3)
self.assertEqual(self.value("sendmmsg_emfile_received"), 0x414243)
self.assertEqual(self.value("sendmmsg_emfile_lengths"), 0x040404)
def test_batches_without_a_spare_descriptor_stop_at_an_unwritable_length(self):
self.assertEqual(self.value("sendmmsg_emfile_readonly_result"), 0)
self.assertEqual(self.value("sendmmsg_emfile_readonly_submitted"), 1)
self.assertEqual(self.value("sendmmsg_emfile_readonly_finish"), 1)
self.assertEqual(self.value("sendmmsg_emfile_readonly_received"), 0x4142)
self.assertEqual(self.value("sendmmsg_emfile_readonly_lengths"), 0x040000)
self.assertEqual(self.value("sendmmsg_emfile_only_result"), 0)
self.assertEqual(self.value("sendmmsg_emfile_only_finish"), self.efault())
self.assertEqual(self.value("sendmmsg_emfile_only_received"), 0x42)
def test_unreadable_later_vectors_end_the_prepared_batch(self):
self.assertEqual(self.value("sendmmsg_unreadable_middle_result"), 0)
self.assertEqual(self.value("sendmmsg_unreadable_middle_submitted"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_middle_queued"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_middle_finish"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_middle_received"), 0x41)
self.assertEqual(self.value("sendmmsg_unreadable_middle_lengths"), 0x040000)
self.assertEqual(self.value("sendmmsg_unreadable_last_result"), 0)
self.assertEqual(self.value("sendmmsg_unreadable_last_submitted"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_last_queued"), 2)
self.assertEqual(self.value("sendmmsg_unreadable_last_pinned"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_last_finish"), 2)
self.assertEqual(self.value("sendmmsg_unreadable_last_received"), 0x4142)
self.assertEqual(self.value("sendmmsg_unreadable_last_lengths"), 0x040400)
def test_an_unreadable_first_vector_reaches_the_kernel_natively(self):
self.assertEqual(self.value("sendmmsg_unreadable_first_result"), 0)
self.assertEqual(self.value("sendmmsg_unreadable_first_native"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_first_error"), 1)
self.assertEqual(self.value("sendmmsg_unreadable_first_received"), 0)
def test_pending_aio_slots_are_reserved_before_the_first_edit(self):
self.assertEqual(self.value("io_submit_reservations_idle"), 0)
self.assertEqual(self.value("io_submit_last_slot_first"), 0)
self.assertEqual(self.value("io_submit_last_slot_reserved"), 0x11)
self.assertEqual(self.value("io_submit_last_slot_second"), (1 << 64) - 11)
self.assertEqual(self.value("io_submit_last_slot_second_untouched"), 768)
self.assertEqual(self.value("io_submit_last_slot_second_holds"), 0)
self.assertEqual(self.value("io_submit_last_slot_reserved_still"), 1)
self.assertEqual(self.value("io_submit_contended_granted"), 0)
self.assertEqual(self.value("io_submit_contended_reservations"), 1)
self.assertEqual(self.value("io_submit_contended_released"), 1)
self.assertEqual(self.value("io_submit_last_slot_finish"), 1)
self.assertEqual(self.value("io_submit_last_slot_restored"), 768)
self.assertEqual(self.value("io_submit_last_slot_free"), 0)
self.assertEqual(self.value("io_submit_last_slot_pending"), 1)
self.assertEqual(self.value("io_submit_crowd_fitted"), 1)
self.assertEqual(self.value("io_submit_crowd_released"), 0)
def test_received_fields_follow_the_kernel_write_order(self):
self.assertEqual(self.value("recvmsg_nameless_result"), 0)