[MG_Remote, MG_Pipe, MG_Test, CI, tools, docs] (P6.5): the first wave completed and reviewed - archive v2, publication boundary, half-close, AcceptPair/role fixes, device re-validation

This commit is contained in:
2026-09-22 10:56:19 -04:00
parent 018fab0f5b
commit fe28bdb5f3
75 changed files with 4887 additions and 254 deletions
+11
View File
@@ -213,6 +213,17 @@ endif()
include(GoogleTest)
gtest_discover_tests(PipeCatalogueTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
# This source scanner must inspect the configured checkout, including in out-of-tree builds.
# Apply the override after GoogleTest's deferred discovery, and only to this one test.
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/PipeCatalogueSourceRoot.cmake" CONTENT
"if(\"PipeCatalogue.FrontendNeverTakesAnApplierAddress\" IN_LIST PipeCatalogueTest_TESTS)
set_tests_properties(PipeCatalogue.FrontendNeverTakesAnApplierAddress PROPERTIES
WORKING_DIRECTORY [==[${MGL_ROOT}]==])
endif()
")
set_property(DIRECTORY APPEND PROPERTY TEST_INCLUDE_FILES
"${CMAKE_CURRENT_BINARY_DIR}/PipeCatalogueSourceRoot.cmake")
gtest_discover_tests(FieldOwnershipTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
foreach(pipeTest ResourceEmitTest VertexInputEmitTest
FramebufferEmitTest TextureEmitTest SamplerEmitTest ImageEmitTest
@@ -18,8 +18,8 @@
// and every one of TypeFacts' twenty members - a codec that dropped one would be invisible
// until a backend read a reflection answer that had quietly become zero;
// * a TRUNCATED stream is refused rather than guessed at;
// * a VERSION or struct-size mismatch is refused rather than deserialised into a layout this
// build does not have.
// * a VERSION or wire-schema mismatch is refused before decoding; native STL object
// sizes do not describe a cross-platform serialized archive.
//
// AND ONE PROPERTY THAT IS NOT ABOUT BYTES AT ALL: LinkArtifacts has 58 members and its
// VisitFields table visits 57. The 58th is the live SharedPtr<glslang::TProgram>, which is
@@ -32,6 +32,7 @@
// stays name-for-name identical between the pull and the push trees.
#include <gtest/gtest.h>
#include <cstring>
#include "Includes.h"
#if MOBILEGL_PIPE_PUSH
@@ -325,9 +326,8 @@ TEST(ProgramArtifactsCodec, ATruncatedStreamIsRefusedNotGuessed) {
#endif
}
// Negative control 2. The version word and the struct-size echo are the two things a compiler
// cannot check: a struct that gained a field and a codec that did not would otherwise
// deserialise garbage into the tail of a reflection table. Both have to REFUSE.
// Negative control 2: both the version and schema word must refuse mismatches.
// Monolith v1 retains the old native-size echo in that second word.
TEST(ProgramArtifactsCodec, AVersionMismatchIsRefused) {
#if MOBILEGL_PIPE_PUSH
Vector<Uint8> bytes;
@@ -344,9 +344,7 @@ TEST(ProgramArtifactsCodec, AVersionMismatchIsRefused) {
++wrongVersion[0];
EXPECT_FALSE(DecodeProgramArtifacts(wrongVersion.data(), wrongVersion.size(), link, spirv));
// Then the MGL_LINKARTIFACTS_SIZE echo, which is the half that catches a struct that grew
// under a codec that did not - the failure the four sizeof trip wires in
// ProgramArtifacts.h send an author here to fix.
// Then the wire schema (v2) or local native-size echo (v1). Neither may be ignored.
Vector<Uint8> wrongSize = bytes;
++wrongSize[4];
EXPECT_FALSE(DecodeProgramArtifacts(wrongSize.data(), wrongSize.size(), link, spirv));
@@ -373,3 +371,39 @@ TEST(ProgramArtifactsCodec, TheTablesVisitEveryMemberExceptTheLiveProgram) {
GTEST_SKIP() << "MOBILEGL_PIPE_PUSH is off: the archive codec is push-only";
#endif
}
TEST(ProgramArtifactsCodec, PortableHeaderUsesWireSchemaRatherThanNativeContainerSize) {
#if MOBILEGL_PIPE_PUSH && MOBILEGL_BUILD_DISAGGREGATED
Vector<Uint8> bytes;
EncodeProgramArtifacts(MakeLinkArtifacts(), MakeSpirvArtifacts(), bytes);
ASSERT_GT(bytes.size(), 12u);
Uint32 version = 0;
Uint64 schema = 0;
std::memcpy(&version, bytes.data(), sizeof(version));
std::memcpy(&schema, bytes.data() + sizeof(version), sizeof(schema));
EXPECT_EQ(version, 2u);
EXPECT_EQ(schema, ProgramArtifactsSchemaFingerprint());
EXPECT_NE(schema, 0u);
EXPECT_NE(schema, sizeof(LinkArtifacts));
RecordProperty("program_archive_wire_schema", std::to_string(schema));
// The old Linux/native echo (1056) and unpinned libc++ echo (0) both fail.
// This control distinguishes a portable schema from simply deleting the check.
for (const Uint64 legacyEcho : {Uint64{1056}, Uint64{0}}) {
auto legacy = bytes;
std::memcpy(legacy.data() + sizeof(version), &legacyEcho, sizeof(legacyEcho));
LinkArtifacts link;
SpirvArtifacts spirv;
EXPECT_FALSE(DecodeProgramArtifacts(legacy.data(), legacy.size(), link, spirv));
EXPECT_TRUE(link.uniformReflection.empty());
EXPECT_TRUE(spirv.generatedSpirv.empty());
}
auto v1 = bytes;
v1[0] = 1;
LinkArtifacts link;
SpirvArtifacts spirv;
EXPECT_FALSE(DecodeProgramArtifacts(v1.data(), v1.size(), link, spirv));
#else
GTEST_SKIP() << "portable archive headers apply to the disaggregated wire";
#endif
}
+13 -1
View File
@@ -22,7 +22,7 @@ if (NOT WIN32)
# P6 so: the SAME nine ITransport-contract cases InProcessTransportTest runs, over a real
# socketpair with real threads, plus the three a stream has and a message queue does not.
# POSIX for the same reason as FdPassingTest - and because CONTRACT-P6 §2.6 lands POSIX only.
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest StreamLinkTest)
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest StreamLinkTest StreamClientPublicationTest InitialCapsStartupTest)
# P6 sm: two MobileGL processes. Needs the real server image beside it, so
# the dependency is declared after the loop rather than assumed.
list(APPEND MOBILEGL_WIRE_TESTS ServerSpawnTest)
@@ -141,3 +141,15 @@ foreach (wiretest IN ITEMS RemoteClientTest ServerLoopTest SessionHandshakeTest
gtest_discover_tests(${wiretest} DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
endforeach ()
# P6.5 manual fault-injection probe: requires an explicit live peer and is never
# registered with ctest, so a device-less unit run cannot silently skip this gate.
if (NOT WIN32)
add_executable(MobileGLPeerLossProbe PeerLossProbe.cpp)
target_include_directories(MobileGLPeerLossProbe PRIVATE
${MGL_ROOT}/include
${MGL_ROOT}/MobileGL
${MGL_ROOT}/3rdparty/flatbuffers/include
)
target_link_libraries(MobileGLPeerLossProbe PRIVATE ${LINK_LIBRARIES})
endif ()
+18
View File
@@ -195,3 +195,21 @@ TEST(FramingTest, ManyMessagesCompactTheBufferInsteadOfGrowing) {
}
EXPECT_EQ(reader.BufferedBytes(), 0u);
}
TEST(Framing, ADataMagicReaderKeepsTheSameLengthAndReassemblyRules) {
using namespace MobileGL::MG_Remote::Transport;
constexpr std::uint32_t dataMagic = 0x444c474d;
std::vector<std::uint8_t> payload(20 + 9, 0x5a), framed;
ASSERT_EQ(AppendFrame(framed, payload.data(), payload.size()), MOBILEGL_OK);
std::memcpy(framed.data(), &dataMagic, sizeof dataMagic);
FrameReader reader(dataMagic);
ASSERT_EQ(reader.Feed(framed.data(), 7), MOBILEGL_OK);
EXPECT_FALSE(reader.HasMessage());
ASSERT_EQ(reader.Feed(framed.data() + 7, framed.size() - 7), MOBILEGL_OK);
EXPECT_EQ(reader.PendingMessageSize(), payload.size());
std::vector<std::uint8_t> received;
ASSERT_EQ(reader.TakeMessage(received), MOBILEGL_OK);
EXPECT_EQ(received, payload);
FrameReader controlReader;
EXPECT_EQ(controlReader.Feed(framed.data(), framed.size()), MOBILEGL_ERR_PROTOCOL_MISMATCH);
}
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Starts at the post-Welcome boundary with real TCP endpoints and ControlInbox.
// The peer publishes real CapsSnapshot bytes; no private run-ahead flag is set.
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Transport/ControlInbox.h>
#include <MG_Remote/Transport/SocketTransport.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <thread>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
template <class Tag, typename Tag::Type member>
struct StartupPeerAccess {
friend typename Tag::Type StartupPeerMember(Tag) { return member; }
};
struct StartupControlTag {
using Type = Transport::ITransport* Client::ClientSession::*;
friend Type StartupPeerMember(StartupControlTag);
};
template struct StartupPeerAccess<StartupControlTag, &Client::ClientSession::m_transport>;
struct StartupInboxTag {
using Type = std::unique_ptr<Transport::ControlInbox> Client::ClientSession::*;
friend Type StartupPeerMember(StartupInboxTag);
};
template struct StartupPeerAccess<StartupInboxTag, &Client::ClientSession::m_controlInbox>;
class InitialCapsStartup : public testing::Test {
protected:
Client::ClientSession client;
std::unique_ptr<Transport::SocketTransport> controlClient, controlPeer;
std::unique_ptr<Transport::ILink> dataPeer;
const decltype(MG_Config::Ipc) savedIpc = MG_Config::Ipc;
const MG_Config::TransportMode savedTransport = MG_Config::Transport;
void SetUp() override {
::alarm(10);
MG_Config::Transport = MG_Config::TransportMode::Spawn;
MG_Config::Ipc.RunAhead = 1;
MG_Config::Ipc.VerbBarrier = 1;
int reserve = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(reserve, 0);
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ASSERT_EQ(::bind(reserve, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
socklen_t length = sizeof address;
ASSERT_EQ(::getsockname(reserve, reinterpret_cast<sockaddr*>(&address), &length), 0);
const std::string endpoint = "tcp://127.0.0.1:" + std::to_string(ntohs(address.sin_port));
::close(reserve);
int listener = -1;
ASSERT_EQ(Transport::SocketTransport::Listen(endpoint, &listener), MOBILEGL_OK);
MobileGLResult accepted = MOBILEGL_ERR_TIMEOUT;
std::thread accepting(
[&] { accepted = Transport::SocketTransport::AcceptPair(listener, 2000, controlPeer); });
const auto connected = Transport::SocketTransport::ConnectTo(endpoint, 2000, controlClient);
accepting.join();
::close(listener);
ASSERT_EQ(connected, MOBILEGL_OK);
ASSERT_EQ(accepted, MOBILEGL_OK);
ASSERT_TRUE(controlClient->IsTcp());
ASSERT_TRUE(controlPeer->IsTcp());
Transport::SessionSegmentSizes sizes;
sizes.CmdRingBytes = 4096;
sizes.StageBytes = 4096;
sizes.EventRingBytes = 4096;
sizes.ReplyBytes = 4096;
ASSERT_EQ(client.AttachStreamLink(controlClient->TakeDataFd(), sizes), MOBILEGL_OK);
ASSERT_EQ(Transport::CreateStreamLink(controlPeer->TakeDataFd(), sizes,
Transport::TransportRoleTag::ServerConsumer, dataPeer),
MOBILEGL_OK);
dataPeer->InitializeEndpoints();
client.*StartupPeerMember(StartupControlTag{}) = controlClient.get();
client.*StartupPeerMember(StartupInboxTag{}) = std::make_unique<Transport::ControlInbox>(*controlClient);
}
void TearDown() override {
if (client.DataLink()) client.DataLink()->Detach();
if (dataPeer) dataPeer->Detach();
client.Stop();
MG_Config::Ipc = savedIpc;
MG_Config::Transport = savedTransport;
::alarm(0);
}
MobileGLResult Start() {
return client.FinishStartup(&client.DataLink()->ConsumerBell(), &client.DataLink()->ProducerBell(), false);
}
MobileGLResult SendCaps(Uint64 bits) {
MG_Backend::DynamicBackendParameters dynamic{};
MG_Backend::FormatCapabilityCache formats{};
RendererInfo renderer{};
Vector<Uint8> encodedFormats, encodedRenderer;
if (!EncodeFormatCapabilities(formats, encodedFormats) || !EncodeRendererInfo(renderer, encodedRenderer))
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
flatbuffers::FlatBufferBuilder builder;
const auto parameters = builder.CreateVector(reinterpret_cast<const Uint8*>(&dynamic), sizeof dynamic);
const auto rendererBytes = builder.CreateVector(encodedRenderer.data(), encodedRenderer.size());
const auto formatBytes = builder.CreateVector(encodedFormats.data(), encodedFormats.size());
const auto version = builder.CreateString("4.6");
const auto caps =
::MobileGL::Wire::CreateCapsSnapshot(builder, parameters, rendererBytes, formatBytes, 0, version, bits,
static_cast<Uint32>(BackendType::DirectGLES));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, caps.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()});
}
MobileGLResult SendLog() {
flatbuffers::FlatBufferBuilder builder;
const auto line = ::MobileGL::Wire::CreateLogLine(
builder, ::MobileGL::Wire::LogLevel::Info,
builder.CreateString("initial caps intentionally delayed by test peer"));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::LogLine, line.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()});
}
bool PumpSnapshot() {
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(1);
do {
if (client.PumpControlPlane()) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (std::chrono::steady_clock::now() < end);
return false;
}
};
TEST_F(InitialCapsStartup, DelayedTcpSnapshotArmsRunAheadAndLaterSnapshotsCanOnlyDemote) {
std::atomic<bool> startupReturned{false};
bool returnedBeforeCaps = false;
MobileGLResult logResult = MOBILEGL_ERR_TIMEOUT, capsResult = MOBILEGL_ERR_TIMEOUT;
std::thread sender([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
returnedBeforeCaps = startupReturned.load(std::memory_order_acquire);
logResult = SendLog();
capsResult = SendCaps(MG_Pipe::kCapRunAheadApply);
});
const auto result = Start();
startupReturned.store(true, std::memory_order_release);
sender.join();
ASSERT_EQ(logResult, MOBILEGL_OK);
ASSERT_EQ(capsResult, MOBILEGL_OK);
EXPECT_FALSE(returnedBeforeCaps) << "startup returned without a real initial snapshot";
ASSERT_EQ(result, MOBILEGL_OK);
EXPECT_TRUE(client.Started());
EXPECT_TRUE(client.RunAheadArmed());
ASSERT_EQ(SendCaps(0), MOBILEGL_OK);
ASSERT_TRUE(PumpSnapshot());
EXPECT_FALSE(client.RunAheadArmed());
ASSERT_EQ(SendCaps(MG_Pipe::kCapRunAheadApply), MOBILEGL_OK);
ASSERT_TRUE(PumpSnapshot());
EXPECT_FALSE(client.RunAheadArmed()) << "a later cap must not re-promote this session";
}
TEST_F(InitialCapsStartup, AHealthyPeerWithoutInitialCapsFailsStartupWithoutDeviceLoss) {
const auto before = std::chrono::steady_clock::now();
EXPECT_EQ(Start(), MOBILEGL_ERR_TIMEOUT);
const auto elapsed = std::chrono::steady_clock::now() - before;
EXPECT_GE(elapsed, std::chrono::seconds(4));
EXPECT_LT(elapsed, std::chrono::seconds(8));
EXPECT_FALSE(client.Started());
EXPECT_FALSE(client.RunAheadArmed());
EXPECT_FALSE(Client::ClientSession::DeviceLost());
}
TEST_F(InitialCapsStartup, ANullInitialSnapshotFailsInsteadOfStartingWithAPlaceholder) {
flatbuffers::FlatBufferBuilder builder;
const auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot,
flatbuffers::Offset<void>{});
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
ASSERT_EQ(controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()}), MOBILEGL_OK);
EXPECT_EQ(Start(), MOBILEGL_ERR_PROTOCOL_MISMATCH);
EXPECT_FALSE(client.Started());
EXPECT_FALSE(Client::ClientSession::DeviceLost());
}
} // namespace
+270
View File
@@ -0,0 +1,270 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Manual P6.5 S2/half-open probe. Never registered as a device-less unit test.
#include <Init.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <MG_Impl/Pipe/SlotAllocator.h>
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/Transport/Doorbell.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <csignal>
#include <sys/wait.h>
#include <unistd.h>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
struct Options {
bool kill = false;
bool external = false;
unsigned deadlineMs = 10000;
unsigned armTimeoutMs = 120000;
std::string readyFile;
std::string startFile;
};
void Usage() {
std::fprintf(stderr, "MobileGLPeerLossProbe (--kill | --external) [--deadline-ms 10000]\n"
" [--ready-file PATH] [--start-file PATH] [--arm-timeout-ms 120000]\n"
"MOBILEGL_IPC_CONTROL=tcp://host:port is required; token/build policy use normal env.\n"
"--kill executes MGITEST_PEER_KILL_CMD with {pid} replaced by Welcome.serverPid.\n"
"--external sends no fault command. After READY, create --start-file immediately\n"
"before the parent's Wi-Fi cut; without a start file, type GO on stdin to arm.\n"
"Only descriptor hangup passes. A deadline never sets the device-lost latch.\n");
}
bool Number(const char* text, unsigned& result) {
char* end = nullptr;
const auto value = std::strtoul(text, &end, 10);
if (!text[0] || !end || *end || !value || value > 600000) return false;
result = static_cast<unsigned>(value);
return true;
}
bool Parse(int argc, char** argv, Options& options) {
for (int i = 1; i < argc; ++i) {
const std::string argument = argv[i];
if (argument == "--kill")
options.kill = true;
else if (argument == "--external")
options.external = true;
else if (i + 1 < argc && argument == "--deadline-ms") {
if (!Number(argv[++i], options.deadlineMs)) return false;
} else if (i + 1 < argc && argument == "--arm-timeout-ms") {
if (!Number(argv[++i], options.armTimeoutMs)) return false;
} else if (i + 1 < argc && argument == "--ready-file")
options.readyFile = argv[++i];
else if (i + 1 < argc && argument == "--start-file")
options.startFile = argv[++i];
else
return false;
}
return options.kill != options.external && (!options.kill || options.startFile.empty());
}
bool Exists(const std::string& path) {
std::error_code error;
return std::filesystem::exists(path, error);
}
struct SessionCleanup {
Client::ClientSession& session;
~SessionCleanup() { session.Stop(); }
};
// The fault helper gets its own process group. It only execs the caller's
// explicit template; killing/reaping this helper never touches a supervisor.
class FaultCommand {
public:
bool Start(const std::string& command) {
m_pid = ::fork();
if (m_pid == 0) {
if (::setpgid(0, 0) != 0) ::_exit(126);
::execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char*>(nullptr));
::_exit(127);
}
if (m_pid < 0) return false;
(void)::setpgid(m_pid, m_pid);
return true;
}
bool Finished() {
if (m_pid <= 0) return true;
const auto result = ::waitpid(m_pid, &m_status, WNOHANG);
if (result == m_pid) {
m_pid = -1;
m_finished = true;
}
return m_finished;
}
bool Succeeded() const { return m_finished && WIFEXITED(m_status) && WEXITSTATUS(m_status) == 0; }
~FaultCommand() {
if (m_pid <= 0 || Finished()) return;
(void)::kill(-m_pid, SIGTERM);
for (int i = 0; i != 20 && !Finished(); ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (m_pid > 0) {
(void)::kill(-m_pid, SIGKILL);
(void)::waitpid(m_pid, &m_status, 0);
}
}
private:
pid_t m_pid = -1;
int m_status = -1;
bool m_finished = false;
};
bool ArmExternal(const Options& options) {
if (options.startFile.empty()) {
std::string line;
return std::getline(std::cin, line) && line == "GO";
}
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(options.armTimeoutMs);
while (!Exists(options.startFile)) {
if (std::chrono::steady_clock::now() >= deadline) return false;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return true;
}
} // namespace
int main(int argc, char** argv) {
if (argc == 2 && std::strcmp(argv[1], "--help") == 0) {
Usage();
return 0;
}
Options options;
if (!Parse(argc, argv, options)) {
Usage();
return 64;
}
const char* endpoint = std::getenv("MOBILEGL_IPC_CONTROL");
if (!endpoint || std::strncmp(endpoint, "tcp://", 6) != 0) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: MOBILEGL_IPC_CONTROL must name an existing TCP server\n");
return 64;
}
if ((!options.readyFile.empty() && Exists(options.readyFile)) ||
(!options.startFile.empty() && Exists(options.startFile))) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: readiness/start files must be fresh for this run\n");
return 64;
}
const char* killTemplate = std::getenv("MGITEST_PEER_KILL_CMD");
if (options.kill && (!killTemplate || !std::strstr(killTemplate, "{pid}"))) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: --kill requires MGITEST_PEER_KILL_CMD containing {pid}\n");
return 64;
}
// No server is forked by the client. StartSpawned resolves Connect from the
// ordinary configuration, exactly as the integration TCP lane does.
::setenv("MOBILEGL_TRANSPORT", "spawn", 1);
::setenv("MOBILEGL_IPC_DATA", "stream", 1);
::setenv("MOBILEGL_IPC_ROLE", "client", 1);
::setenv("MOBILEGL_BACKEND_TYPE", "DirectGLES", 0);
MG_ConfigLoader::Init();
auto& session = Client::ClientSessionInstance();
SessionCleanup cleanup{session};
const auto started = session.StartSpawned();
if (started != MOBILEGL_OK || !session.ConnectDial() || !session.DataLink() ||
session.DataLink()->Capabilities().SharedMapping) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: TCP+stream bringup rc=%d\n", started);
return 2;
}
const auto peerPid = session.PeerServerPid();
auto* bell = session.SelfDoorbellForTest();
if (peerPid <= 1 || !bell || bell->PeerHungUp() || Client::ClientSession::DeviceLost() ||
MG_Impl::GLImpl::GetGraphicsResetStatus() != GL_NO_ERROR) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: no healthy Welcome peer before arming\n");
return 2;
}
// A real control round-trip finishes before the idle measurement. No GL
// command or application heartbeat is sent during the detection interval.
session.SyncPeerLog();
if (bell->PeerHungUp() || Client::ClientSession::DeviceLost()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: peer disappeared before readiness\n");
return 2;
}
if (!options.readyFile.empty()) {
std::ofstream ready(options.readyFile, std::ios::out | std::ios::trunc);
ready << peerPid << '\n';
ready.flush();
if (!ready) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: cannot publish ready file\n");
return 2;
}
}
std::printf("P65_PEER_LOSS_READY pid=%u mode=%s control=%s data=stream deadline_ms=%u\n", peerPid,
options.kill ? "kill" : "external", endpoint, options.deadlineMs);
std::fflush(stdout);
if (options.external && !ArmExternal(options)) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: external trigger was not armed\n");
return 3;
}
if (bell->PeerHungUp()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: peer disappeared before timer arm; timing is invalid\n");
return 3;
}
std::string command;
if (options.kill) {
command = killTemplate;
const auto pidText = std::to_string(peerPid);
for (std::size_t at = 0; (at = command.find("{pid}", at)) != std::string::npos; at += pidText.size())
command.replace(at, 5, pidText);
}
const auto start = std::chrono::steady_clock::now();
const auto deadline = start + std::chrono::milliseconds(options.deadlineMs);
FaultCommand fault;
if (options.kill && !fault.Start(command)) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: could not start the caller's kill command\n");
return 4;
}
std::printf("P65_PEER_LOSS_ARMED pid=%u mode=%s\n", peerPid, options.kill ? "kill" : "external");
std::fflush(stdout);
while (!bell->PeerHungUp() && std::chrono::steady_clock::now() < deadline) {
if (options.kill && fault.Finished() && !fault.Succeeded()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: caller's kill command failed\n");
return 4;
}
(void)bell->Park(10);
}
const auto hangupAt = std::chrono::steady_clock::now();
const auto hangupMs = std::chrono::duration_cast<std::chrono::milliseconds>(hangupAt - start).count();
if (!bell->PeerHungUp() || !bell->Dead() || hangupAt >= deadline) {
std::fprintf(stderr,
"P65_PEER_LOSS_FAIL: deadline_ms=%u elapsed_ms=%lld peer_hung_up=%d dead=%d device_lost=%d\n",
options.deadlineMs, static_cast<long long>(hangupMs), bell->PeerHungUp(), bell->Dead(),
Client::ClientSession::DeviceLost());
return 3;
}
if (options.kill) {
while (!fault.Finished() && std::chrono::steady_clock::now() < deadline)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (!fault.Succeeded()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: kill command did not complete successfully within budget\n");
return 4;
}
}
// Exercise the production barrier's ShutDown branch, not a test-written
// latch. The peer is already proven gone, so this valid reply-owning record
// cannot reach a server or inspect an invented fence handle there.
const MG_Pipe::MGPHandleOnly fence{{1, 1}, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
Uint32 reply = 0;
Int32 status = Wire::ReplySink::kStatusError;
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceStatus, &fence, sizeof fence, nullptr, 0, &reply, sizeof reply,
&status);
const auto lostMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
const bool lost = Client::ClientSession::DeviceLost();
const auto reset = MG_Impl::GLImpl::GetGraphicsResetStatus();
if (!lost || reset != GL_UNKNOWN_CONTEXT_RESET || status != Wire::ReplySink::kStatusDeclined ||
lostMs >= options.deadlineMs) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: production latch=%d gl_reset=0x%x reply_status=%d elapsed_ms=%lld\n",
lost, reset, status, static_cast<long long>(lostMs));
return 5;
}
std::printf("P65_PEER_LOSS_PASS pid=%u mode=%s peer_hung_up_ms=%lld device_lost_ms=%lld "
"deadline_ms=%u device_lost=1 gl_reset=0x%x source=production-barrier\n",
peerPid, options.kill ? "kill" : "external", static_cast<long long>(hangupMs),
static_cast<long long>(lostMs), options.deadlineMs, reset);
std::fflush(stdout);
return 0;
}
+14 -7
View File
@@ -74,9 +74,12 @@ struct BackendTag {
};
template struct PeerAccess<BackendTag, &Srv::ServerLoop::m_backend>;
void StartControlSession() {
MG_Backend::BackendObject* ControlCapsPeer();
void StartControlSession(MG_Backend::BackendObject* capsSource = nullptr) {
::alarm(15);
MG_Config::Transport = MG_Config::TransportMode::InProcess;
Srv::ServerSessionInstance().SetBackend(capsSource ? capsSource : ControlCapsPeer());
Srv::ServerSessionInstance().SetCapabilityBits(0);
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK)
@@ -135,6 +138,13 @@ struct CapsPeer : MG_Backend::BackendObject {
BackendType GetBackendType() const override { return BackendType::DirectGLES; }
};
MG_Backend::BackendObject* ControlCapsPeer() {
// A real encoded initial snapshot is required even when the apply fixture
// substitutes every verb and has no GPU backend of its own.
static CapsPeer backend;
return &backend;
}
void ExpectNamedAbort(const ChildResult& child, const char* diagnostic) {
EXPECT_TRUE(DiedOfAbort(child)) << DescribeStatus(child) << '\n' << child.Log;
EXPECT_NE(child.Log.find(diagnostic), std::string::npos) << child.Log;
@@ -387,8 +397,7 @@ TEST(RemoteClientControls, BounceReadPixelsShortReplyRefusesBeforeScatter) {
TEST(RemoteClientControls, DeleteTexturesAdvancesTheWireOrdinal) {
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
ErrorPeer replies;
replies.status = 0; // The peer accepts the resource; the client must publish and delete it.
replies.Install();
@@ -442,8 +451,7 @@ TEST(RemoteClientControls, ServerRoleRespecifyDoesNotEmitClientInitialBytes) {
// SEG_EVENT producers are what replaces the mechanism this case used to drive.
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
MG_Config::Features.PipePush |= kMGPipeSubsystemResources;
Srv::ServerLoopInstance().RunProbeOnApplyThreadForTesting([](void*) {
MG_State::GLState::BufferObject buffer(701);
@@ -475,8 +483,7 @@ TEST(RemoteClientControls, ServerRoleFlushDoesNotRunTheClientSubDataFollowup) {
// (Fatal{RoleViolation, "MGPipeSlots"}; ev's events replace the mechanism).
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
MG_Config::Features.PipePush |= kMGPipeSubsystemResources;
Srv::ServerLoopInstance().RunProbeOnApplyThreadForTesting([](void*) {
MGPipeResourceOps ops{};
@@ -40,7 +40,7 @@ void CheckE1Boundary(Bool serverCap, MGPWireOp op, Bool expectWait, const char*
const Uint64 caps = serverCap ? static_cast<Uint64>(kCapRunAheadApply) : 0;
Srv::ServerSessionInstance().SetCapabilityBits(caps);
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
AdoptSnapshot(MakeSnapshot(kMGPipeSubsystemsMigratedAtP4a, caps));
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
auto& session = ClientSessionInstance();
if (session.Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) ::_exit(81);
auto& producer = session.Producer();
+3 -18
View File
@@ -2338,19 +2338,8 @@ void StartRunAheadSession() {
Srv::ServerSessionInstance().SetCapabilityBits(
static_cast<Uint64>(MG_Pipe::kCapRunAheadApply));
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
// THE MIRROR IS ADOPTED BEFORE Start, AND THAT IS THE LATCH'S RULE MADE VISIBLE. This
// process has no server backend, so PublishCapsSnapshot is deferred and the handshake
// carries no snapshot at all - and §1 says a later snapshot may only turn run-ahead OFF,
// never on. So the arm has to be true at the FIRST adoption, which for a fixture means
// before the session takes its latch. A test that adopted afterwards and found the client
// still lockstep would be reading the contract's own rule as a failure.
{
MG_Pipe::MGPCaps caps{};
caps.CallMask = static_cast<Uint64>(MG_Pipe::kCapRunAheadApply) |
MG_Remote::MGCapsConsumerBits(kMGPipeSubsystemsMigratedAtP4a);
CapsMirrorInstance().Adopt(caps, MG_Backend::FormatCapabilityCache{}, RendererInfo{},
String{"4.6"}, BackendType::DirectGLES);
}
// The test peer publishes the first snapshot through the real handshake.
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) {
::_exit(81);
}
@@ -2411,11 +2400,7 @@ TEST(RemoteRunAhead, ClearingTheVerbBarrierDisarmsRunAheadEvenWithTheCapBit) {
Srv::ServerSessionInstance().SetCapabilityBits(
static_cast<Uint64>(MG_Pipe::kCapRunAheadApply));
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
MG_Pipe::MGPCaps caps{};
caps.CallMask = static_cast<Uint64>(MG_Pipe::kCapRunAheadApply) |
MG_Remote::MGCapsConsumerBits(kMGPipeSubsystemsMigratedAtP4a);
CapsMirrorInstance().Adopt(caps, MG_Backend::FormatCapabilityCache{}, RendererInfo{},
String{"4.6"}, BackendType::DirectGLES);
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) {
::_exit(81);
}
@@ -41,6 +41,7 @@
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Handshake.h>
#include <MG_Pipe/PipeWireLayout.h>
#include <MG_State/GLState/ProgramState/ProgramArtifactsCodec.h>
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <MG_Remote/Server/ServerSession.h>
@@ -126,6 +127,8 @@ TEST(SessionHandshakeTest, TheAbiFingerprintChangesWhenAnyOfItsInputsDoes) {
EXPECT_EQ(inputs.MemberLayout, MG_Pipe::kMGPipeWireMemberLayoutDigest);
EXPECT_EQ(inputs.CatalogueLayout, MG_Pipe::kMGPipeWireCatalogueDigest);
EXPECT_EQ(inputs.RenderStateLayout, MG_Pipe::WireRenderStateDigest());
EXPECT_EQ(inputs.ProgramArtifactsCodecVersion, MG_State::GLState::kProgramArtifactsCodecVersion);
EXPECT_EQ(inputs.ProgramArtifactsSchema, MG_State::GLState::ProgramArtifactsSchemaFingerprint());
EXPECT_EQ(inputs.OpCount, static_cast<Uint64>(MG_Pipe::MGPWireOp::kOpCount));
EXPECT_EQ(inputs.PointerBits, sizeof(void*) * 8);
const auto perturbed = [&](auto mutate) {
@@ -142,6 +145,8 @@ TEST(SessionHandshakeTest, TheAbiFingerprintChangesWhenAnyOfItsInputsDoes) {
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilityFormats; })) << "FormatCapabilityFormats";
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilitiesCodecVersion; })) << "FormatCapabilitiesCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.RendererInfoCodecVersion; })) << "RendererInfoCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.ProgramArtifactsCodecVersion; })) << "ProgramArtifactsCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.ProgramArtifactsSchema; })) << "ProgramArtifactsSchema";
EXPECT_NE(production, perturbed([](auto& i) { ++i.OpCount; })) << "OpCount";
EXPECT_NE(production, perturbed([](auto& i) { ++i.AbiVersion; })) << "AbiVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.PointerBits; })) << "PointerBits";
@@ -174,6 +174,21 @@ TEST(SocketTransportTest, CarriesFramesInBothDirections) {
EXPECT_EQ(Receive(*pair.client), welcome);
}
TEST(SocketTransportTest, RequestHalfCloseStillDeliversTheFinalReply) {
std::unique_ptr<SocketTransport> client, server;
ASSERT_EQ(SocketTransport::CreatePair(client, server), MOBILEGL_OK);
ASSERT_EQ(client->SendFrame(Span("last request")), MOBILEGL_OK);
EXPECT_EQ(Receive(*server), "last request");
ASSERT_EQ(client->ShutdownSend(), MOBILEGL_OK);
std::uint64_t bytes = 0;
char buffer[32]{};
EXPECT_EQ(server->ReceiveFrame({buffer, sizeof buffer}, &bytes, 1000), MOBILEGL_ERR_TRANSPORT_CLOSED);
ASSERT_EQ(server->SendFrame(Span("cleanup complete")), MOBILEGL_OK);
EXPECT_EQ(Receive(*client), "cleanup complete");
server->Shutdown();
EXPECT_EQ(client->ReceiveFrame({buffer, sizeof buffer}, &bytes, 1000), MOBILEGL_ERR_TRANSPORT_CLOSED);
}
TEST(SocketTransportTest, PreservesOrder) {
Pair pair = MakePair();
@@ -0,0 +1,297 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Production session submission with a real stream reader and a deliberately
// non-applying peer. A passing test cannot be supplied by a later client wait.
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Transport/InProcessTransport.h>
#include <MG_Remote/Transport/StreamLink.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <gtest/gtest.h>
#include <sys/socket.h>
#include <unistd.h>
#include <atomic>
#include <MG_Pipe/PipeRoute.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <chrono>
#include <thread>
#include <csignal>
#include <sys/wait.h>
#include <vector>
#include <cstring>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
// Only the test's control endpoint is injected. A queued real CapsSnapshot
// arms the production latch without a backend, EGL context, or private toggle.
template <class Tag, typename Tag::Type member>
struct PublicationPeerAccess {
friend typename Tag::Type PublicationPeerMember(Tag) { return member; }
};
struct ControlEndpointTag {
using Type = Transport::ITransport* Client::ClientSession::*;
friend Type PublicationPeerMember(ControlEndpointTag);
};
template struct PublicationPeerAccess<ControlEndpointTag, &Client::ClientSession::m_transport>;
class StreamClientPublication : public testing::Test {
protected:
Client::ClientSession client;
std::unique_ptr<Transport::ILink> peer;
std::unique_ptr<Transport::InProcessTransport> controlClient, controlPeer;
const decltype(MG_Config::Ipc) savedIpc = MG_Config::Ipc;
const MG_Config::TransportMode savedTransport = MG_Config::Transport;
void SetUp() override {
// A regression that turns submission into an applied wait fails
// promptly instead of consuming the production 120-second budget.
::alarm(5);
MG_Config::Transport = MG_Config::TransportMode::Spawn;
MG_Config::Ipc.RunAhead = 1;
MG_Config::Ipc.VerbBarrier = 1;
MG_Config::Ipc.BatchWaits = 1;
MG_Config::Ipc.PresentCredit = 3;
Transport::SessionSegmentSizes sizes;
sizes.CmdRingBytes = 4096;
sizes.StageBytes = 4096;
sizes.EventRingBytes = 4096;
sizes.ReplyBytes = 4096;
int sockets[2];
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_STREAM, 0, sockets), 0);
ASSERT_EQ(AttachData(sockets, sizes), MOBILEGL_OK);
Transport::InProcessTransport::CreatePair(controlClient, controlPeer);
client.*PublicationPeerMember(ControlEndpointTag{}) = controlClient.get();
MG_Backend::DynamicBackendParameters dynamic{};
MG_Backend::FormatCapabilityCache formats{};
RendererInfo renderer{};
Vector<Uint8> encodedFormats, encodedRenderer;
ASSERT_TRUE(EncodeFormatCapabilities(formats, encodedFormats));
ASSERT_TRUE(EncodeRendererInfo(renderer, encodedRenderer));
flatbuffers::FlatBufferBuilder builder;
const auto dynamicBytes = builder.CreateVector(reinterpret_cast<const Uint8*>(&dynamic), sizeof dynamic);
const auto rendererBytes = builder.CreateVector(encodedRenderer.data(), encodedRenderer.size());
const auto formatBytes = builder.CreateVector(encodedFormats.data(), encodedFormats.size());
const auto version = builder.CreateString("4.6");
const auto caps = ::MobileGL::Wire::CreateCapsSnapshot(builder, dynamicBytes, rendererBytes, formatBytes, 0,
version, MG_Pipe::kCapRunAheadApply,
static_cast<Uint32>(BackendType::DirectGLES));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, caps.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
ASSERT_EQ(controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()}), MOBILEGL_OK);
ASSERT_EQ(
client.FinishStartup(&client.DataLink()->ConsumerBell(), &client.DataLink()->ProducerBell(), false),
MOBILEGL_OK);
ASSERT_TRUE(client.RunAheadArmed());
}
void TearDown() override {
// The peer intentionally never applies. Closing delivery before
// teardown prevents its drain from supplying the assertion.
if (client.DataLink()) client.DataLink()->Detach();
if (peer) peer->Detach();
client.Stop();
MG_Config::Ipc = savedIpc;
MG_Config::Transport = savedTransport;
::alarm(0);
}
virtual MobileGLResult AttachData(int sockets[2], const Transport::SessionSegmentSizes& sizes) {
const auto result = client.AttachStreamLink(sockets[0], sizes);
if (result != MOBILEGL_OK) {
::close(sockets[1]);
return result;
}
const auto attached =
Transport::CreateStreamLink(sockets[1], sizes, Transport::TransportRoleTag::ServerConsumer, peer);
if (attached == MOBILEGL_OK) peer->InitializeEndpoints();
return attached;
}
bool Upload(std::size_t count, unsigned char fill) {
std::vector<Uint8> bytes(count, fill);
MG_Pipe::MGPSubData record{};
record.Res = {1, 1};
record.Target = MG_Pipe::kMGPipeResourceTargetBuffer;
record.UnionBox.W = static_cast<Uint32>(count);
record.UnionBox.H = record.UnionBox.D = 1;
return MG_Pipe::MGPipeRouteResourceSubData(record, bytes.data(), bytes.size());
}
bool PeerGone() {
auto& bell = client.DataLink()->ProducerBell();
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(1);
while (!bell.PeerHungUp() && std::chrono::steady_clock::now() < end)
(void)bell.Park(1);
return bell.PeerHungUp();
}
bool StageIs(unsigned char fill) {
const auto& memory = client.DataLink()->Memory();
const auto* bytes = static_cast<const Uint8*>(memory.StageBase());
for (Uint64 i = 0; i < memory.StageBytes(); ++i)
if (bytes[i] != fill) return false;
return true;
}
bool ReceivedPublishedPrefix() {
const auto head = client.DataLink()->Signals().CmdHead->load(std::memory_order_acquire);
const auto signals = peer->Signals();
return peer->ConsumerBell().Wait(
*signals.ConsumerParked, [&] { return signals.CmdHead->load(std::memory_order_acquire) >= head; }, 0,
1000);
}
};
TEST_F(StreamClientPublication, AllThreePresentCreditsReachAnIdlePeerWithoutAnyClientWait) {
for (Uint64 serial = 1; serial <= 3; ++serial) {
MG_Pipe::MGPPresent present{};
present.FrameSerial = client.AcquirePresentCredit();
ASSERT_EQ(present.FrameSerial, serial);
client.EmitAndWait(MG_Pipe::MGPWireOp::Present, &present, sizeof present, nullptr, 0, nullptr, 0, nullptr);
ASSERT_TRUE(ReceivedPublishedPrefix()) << "present " << serial << " remained in the local batch";
}
EXPECT_EQ(client.Producer().Waits(), 0u);
EXPECT_EQ(client.PresentCreditWaits(), 0u);
EXPECT_EQ(client.DataLink()->Progress()->appliedSeq.load(), 0u);
for (int record = 0; record != 3; ++record) {
Transport::RingRecordView view{};
ASSERT_TRUE(peer->CommandsIn().Pop(view));
EXPECT_EQ(view.kind, static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::Present));
}
}
TEST_F(StreamClientPublication, ExportedGlFlushDeliversPublishedCommandsWithoutWaitingForApplied) {
MG_Pipe::MGPMemoryBarrier barrier{};
client.EmitAndWait(MG_Pipe::MGPWireOp::MemoryBarrier, &barrier, sizeof barrier, nullptr, 0, nullptr, 0,
nullptr);
ASSERT_EQ(client.LastPublishedSeq(), 1u);
EXPECT_EQ(peer->Signals().CmdHead->load(), 0u);
::glFlush();
ASSERT_TRUE(ReceivedPublishedPrefix());
EXPECT_EQ(client.Producer().Waits(), 0u);
EXPECT_EQ(client.DataLink()->Progress()->appliedSeq.load(), 0u);
Transport::RingRecordView view{};
ASSERT_TRUE(peer->CommandsIn().Pop(view));
EXPECT_EQ(view.kind, static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::MemoryBarrier));
}
TEST_F(StreamClientPublication, ADisconnectedPeerDeclinesALargeUploadWithoutCopyOrWireOrdinal) {
std::memset(client.DataLink()->Memory().StageBase(), 0x33, 4096);
peer->Detach();
ASSERT_TRUE(PeerGone());
// Larger than the whole window: cancellation must precede allocation,
// capacity checks and memcpy. The live path would refuse this request.
EXPECT_FALSE(Upload(32768, 0x99));
EXPECT_TRUE(Client::ClientSession::DeviceLost());
EXPECT_EQ(MG_Impl::GLImpl::GetGraphicsResetStatus(), GL_UNKNOWN_CONTEXT_RESET);
EXPECT_TRUE(client.Encoder().Cancelled());
EXPECT_EQ(client.Encoder().EmitSeq(), 0u);
EXPECT_EQ(client.LastPublishedSeq(), 0u);
EXPECT_TRUE(StageIs(0x33));
// A second local decline still consumes a nonzero MintReplySlot ticket.
EXPECT_FALSE(Upload(16, 0x88));
EXPECT_EQ(client.Encoder().EmitSeq(), 0u);
MG_Pipe::MGPMemoryBarrier record{};
EXPECT_EQ(client.Encoder().EncodeRecord(MG_Pipe::MGPWireOp::MemoryBarrier, &record, sizeof record,
static_cast<const void*>(nullptr), Uint64{0}),
MG_Remote::Wire::kInvalidSeq);
EXPECT_TRUE(StageIs(0x33));
}
class StreamClientStagingProcess : public StreamClientPublication {
protected:
pid_t peerPid = -1;
MobileGLResult AttachData(int sockets[2], const Transport::SessionSegmentSizes& sizes) override {
int ready[2];
if (::pipe(ready) != 0) {
::close(sockets[0]);
::close(sockets[1]);
return MOBILEGL_ERR_NOT_INITIALIZED;
}
// Fork before attaching either stream, so the child starts its own
// real reader and inherits no running thread or live link mutex.
peerPid = ::fork();
if (peerPid == 0) {
::close(ready[0]);
::close(sockets[0]);
std::unique_ptr<Transport::ILink> link;
const auto result =
Transport::CreateStreamLink(sockets[1], sizes, Transport::TransportRoleTag::ServerConsumer, link);
if (result == MOBILEGL_OK) link->InitializeEndpoints();
const unsigned char success = result == MOBILEGL_OK;
(void)::write(ready[1], &success, 1);
::close(ready[1]);
if (!success) ::_exit(81);
for (;;)
::pause(); // Reader receives; the peer deliberately never applies.
}
::close(ready[1]);
::close(sockets[1]);
unsigned char success = 0;
const auto received = peerPid > 0 ? ::read(ready[0], &success, 1) : -1;
::close(ready[0]);
if (received != 1 || !success) {
::close(sockets[0]);
return MOBILEGL_ERR_NOT_INITIALIZED;
}
return client.AttachStreamLink(sockets[0], sizes);
}
void TearDown() override {
if (peerPid > 0) {
(void)::kill(peerPid, SIGKILL);
(void)::waitpid(peerPid, nullptr, 0);
peerPid = -1;
}
StreamClientPublication::TearDown();
}
};
TEST_F(StreamClientStagingProcess, APeerKilledDuringAFullStageWaitDeclinesWithoutCopyingTheNextBlob) {
ASSERT_TRUE(Upload(4096, 0x5a));
ASSERT_EQ(client.Encoder().EmitSeq(), 1u);
client.Flush();
auto* parked = client.DataLink()->Signals().ProducerParked;
std::atomic<bool> killedWhileParked{false};
std::thread killer([&] {
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(2);
while (!parked->load(std::memory_order_acquire) && std::chrono::steady_clock::now() < end)
std::this_thread::yield();
killedWhileParked.store(parked->load(std::memory_order_acquire) != 0, std::memory_order_release);
(void)::kill(peerPid, SIGKILL);
});
const bool accepted = Upload(64, 0xa7);
killer.join();
EXPECT_TRUE(killedWhileParked.load());
EXPECT_FALSE(accepted);
EXPECT_TRUE(Client::ClientSession::DeviceLost());
EXPECT_EQ(MG_Impl::GLImpl::GetGraphicsResetStatus(), GL_UNKNOWN_CONTEXT_RESET);
EXPECT_GT(client.Encoder().StageReclaimWaits(), 0u);
EXPECT_EQ(client.Encoder().EmitSeq(), 1u);
EXPECT_EQ(client.LastPublishedSeq(), 1u);
EXPECT_TRUE(StageIs(0x5a));
}
class HealthyStageTimeoutHarness : public StreamClientPublication {
public:
void TestBody() override {}
void Run() {
SetUp();
client.Encoder().SetStageWaitTimeoutMs(5);
if (!Upload(4096, 0x5a)) ::_exit(82);
(void)Upload(64, 0xa7);
::_exit(83);
}
};
TEST(StreamClientCancellationDeath, AHealthyPeerStageTimeoutDoesNotInventDeviceLoss) {
EXPECT_EXIT(
{
::signal(
SIGABRT, +[](int) {
const bool lost = Client::ClientSession::DeviceLost();
const char* text = lost ? "stage-timeout-device-lost=1\n" : "stage-timeout-device-lost=0\n";
(void)::write(STDERR_FILENO, text, sizeof("stage-timeout-device-lost=0\n") - 1);
::_exit(lost ? 91 : 72);
});
HealthyStageTimeoutHarness harness;
harness.Run();
},
testing::ExitedWithCode(72), "RetirementWaitFailed(.|\n)*stage-timeout-device-lost=0");
}
} // namespace
+121 -5
View File
@@ -8,6 +8,9 @@
#include <thread>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <cstdio>
using namespace MobileGL::MG_Remote::Transport;
namespace {
@@ -64,18 +67,28 @@ namespace {
Queue(1, 24);
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView& v) {
RingRecordView view;
ASSERT_TRUE(consumer.Pop(view));
[&](const RingRecordView& v) {
std::uint64_t offset;
std::memcpy(&offset, v.payload, sizeof offset);
const void* p = nullptr;
ASSERT_EQ(server.ResolveSpan(LinkSegment::Stage, {offset, bytes.size()}, &p), MOBILEGL_OK);
EXPECT_EQ(std::memcmp(p, bytes.data(), bytes.size()), 0);
}));
apply.RetireThrough(1);
}(view);
// Advance locally without a periodic-progress callback: this isolates
// flush-on-idle so removing it cannot be masked by the one-ms timer.
Watermark::AdvanceApplied(*serverMemory.CmdControl(), 1);
Watermark::AdvanceRetired(*serverMemory.CmdControl(), 1);
consumer.PublishRetired();
// This is the production idle discipline, not a polling sender thread.
EXPECT_EQ(apply.WaitForWork(1), SessionWait::TimedOut);
EXPECT_EQ(publish.WaitForApplied(1, 2000), SessionWait::Reached);
EXPECT_EQ(client.Progress()->retiredSeq.load(), 1u);
// applied and retired are independent watermarks. The reader publishes
// them in dependency order, and an applied waiter can run between stores.
EXPECT_TRUE(client.ProducerBell().Wait(
clientMemory.CmdControl()->producerParked,
[&] { return client.Progress()->retiredSeq.load(std::memory_order_acquire) >= 1; }, 0, 2000));
}
TEST_F(StreamPair, RetiredByteCursorCrossesManyWrapsWithoutStageRingCursors) {
@@ -152,6 +165,109 @@ namespace {
EXPECT_EQ(publish.WaitForApplied(10, 2000), SessionWait::ShutDown);
}
TEST_F(StreamPair, PeerEofReleasesAnAlreadyWaitingReplyReader) {
std::atomic<bool> finished{false};
MobileGLResult result = MOBILEGL_OK;
std::thread waiting([&] {
std::int32_t status;
const void* payload;
std::uint64_t size;
result = client.ReadReply(1, &status, &payload, &size);
finished.store(true, std::memory_order_release);
});
server.Detach();
EXPECT_TRUE(Eventually([&] { return finished.load(std::memory_order_acquire); }));
waiting.join();
EXPECT_EQ(result, MOBILEGL_ERR_TRANSPORT_CLOSED);
}
TEST_F(StreamPair, AppliedWithoutItsReplyIsRefusedWithoutAnUnboundedWait) {
Queue(1, 1);
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
ASSERT_TRUE(apply.ApplyOne([](const RingRecordView&) {}));
apply.RetireThrough(1);
ASSERT_EQ(server.FlushProgress(), MOBILEGL_OK);
ASSERT_EQ(publish.WaitForApplied(1, 2000), SessionWait::Reached);
std::int32_t status;
const void* p;
std::uint64_t size;
EXPECT_EQ(client.ReadReply(1, &status, &p, &size), MOBILEGL_ERR_PROTOCOL_MISMATCH);
}
TEST_F(StreamPair, ControlFenceWaitsForEventBytesOnTheIndependentDataConnection) {
EventRingProducer events(serverMemory.EventControl(), serverMemory.CmdControl(), serverMemory.EventRingBase(),
1024);
auto* payload = events.Reserve(kEventGlError, 8);
ASSERT_NE(payload, nullptr);
std::memset(payload, 0, 8);
events.Ring().Publish();
const auto fence = server.EventPublishedHead();
EXPECT_EQ(client.WaitForEventDelivery(fence, 1), MOBILEGL_ERR_TIMEOUT);
ASSERT_EQ(server.Flush(), MOBILEGL_OK);
EXPECT_EQ(client.WaitForEventDelivery(fence, 2000), MOBILEGL_OK);
EXPECT_GE(client.EventPublishedHead(), fence);
}
TEST(StreamLinkTcp, RecordsSixtyFourMiBStageBurstThroughputOnRealLoopbackTcp) {
int listener = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(listener, 0);
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ASSERT_EQ(::bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
ASSERT_EQ(::listen(listener, 1), 0);
socklen_t addressBytes = sizeof address;
ASSERT_EQ(::getsockname(listener, reinterpret_cast<sockaddr*>(&address), &addressBytes), 0);
int clientFd = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(clientFd, 0);
ASSERT_EQ(::connect(clientFd, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
int serverFd = ::accept(listener, nullptr, nullptr);
ASSERT_GE(serverFd, 0);
::close(listener);
int enabled = 1;
ASSERT_EQ(::setsockopt(clientFd, IPPROTO_TCP, TCP_NODELAY, &enabled, sizeof enabled), 0);
ASSERT_EQ(::setsockopt(serverFd, IPPROTO_TCP, TCP_NODELAY, &enabled, sizeof enabled), 0);
SessionSegmentSizes sizes;
sizes.CmdRingBytes = 1024;
sizes.EventRingBytes = 1024;
sizes.StageBytes = 64ull * 1024 * 1024;
sizes.ReplyBytes = 4096;
std::unique_ptr<ILink> client, server;
ASSERT_EQ(CreateStreamLink(clientFd, sizes, TransportRoleTag::ClientProducer, client), MOBILEGL_OK);
ASSERT_EQ(CreateStreamLink(serverFd, sizes, TransportRoleTag::ServerConsumer, server), MOBILEGL_OK);
client->InitializeEndpoints();
server->InitializeEndpoints();
std::memset(client->Memory().StageBase(), 0x5a, static_cast<std::size_t>(sizes.StageBytes));
client->NoteStage({0, sizes.StageBytes});
SessionProducer producer;
producer.Attach(*client, 0);
SessionConsumer consumer;
consumer.Attach(*server, 0);
auto* record = client->CommandsOut().Reserve(1, 0, sizeof(std::uint64_t));
ASSERT_NE(record, nullptr);
std::memcpy(record, &sizes.StageBytes, sizeof(std::uint64_t));
producer.PublishAndNotify(1);
const auto start = std::chrono::steady_clock::now();
ASSERT_EQ(client->Flush(), MOBILEGL_OK);
ASSERT_EQ(consumer.WaitForWork(10000), SessionWait::Reached);
ASSERT_TRUE(consumer.ApplyOne([&](const RingRecordView&) {
const auto* bytes = static_cast<const unsigned char*>(server->Memory().StageBase());
for (std::uint64_t offset = 0; offset < sizes.StageBytes; offset += 4096)
EXPECT_EQ(bytes[offset], 0x5a);
EXPECT_EQ(bytes[sizes.StageBytes - 1], 0x5a);
}));
consumer.RetireThrough(1);
ASSERT_EQ(server->FlushProgress(), MOBILEGL_OK);
ASSERT_EQ(producer.WaitForApplied(1, 10000), SessionWait::Reached);
const double elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
const double mibPerSecond = 64.0 / elapsed;
RecordProperty("tcp_stage_bytes", std::to_string(sizes.StageBytes));
RecordProperty("tcp_stage_mib_per_second", std::to_string(mibPerSecond));
std::printf("P6.5 Stage burst control=none data=tcp-loopback bytes=%llu seconds=%.6f MiB/s=%.3f\n",
static_cast<unsigned long long>(sizes.StageBytes), elapsed, mibPerSecond);
}
void WriteFrame(int fd, unsigned kind, std::uint64_t a, const void* payload, std::uint32_t size) {
unsigned char h[28]{};
auto put = [&](unsigned at, std::uint64_t value, unsigned width) {
@@ -159,7 +275,7 @@ namespace {
h[at + i] = value >> (8 * i);
};
put(0, 0x444c474d, 4);
put(4, size, 4);
put(4, size + 20, 4);
h[8] = kind;
put(12, a, 8);
ASSERT_EQ(::send(fd, h, sizeof h, MSG_NOSIGNAL), sizeof h);