mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-17 06:53:22 +09:00
- New CMake option MOBILEGL_BUILD_DISAGGREGATED (default OFF, plan B appendix B). ON appends the MG_Remote sources to SOURCE_FILES, puts 3rdparty/flatbuffers/include on the include path and defines MOBILEGL_BUILD_DISAGGREGATED=1. OFF compiles nothing from MG_Remote and adds no include path and no library, which is one of the two byte-level equalities plan B section 10.3 keeps: measured `nm --defined-only build-linux/libMobileGL.so | grep -ic MG_Remote` = 0 with the option OFF and 93 with it ON, with an identical ldd set in both configurations.
- The option forces itself OFF with message(WARNING) when 3rdparty/flatbuffers/include/flatbuffers/flatbuffers.h is missing, so a checkout without the submodule still configures and builds rather than failing a hundred lines later on a missing header.
- ITransport.h: SendFrame / ReceiveFrame / PeekFrameSize / ShareFd / ReceiveFd / Shutdown. ReceiveFrame's contract is the fix for a defect of the earlier branch: a destination buffer smaller than the pending message returns MOBILEGL_ERR_BUFFER_TOO_SMALL with the required size and KEEPS the message queued. The earlier transport failed the call and popped the message anyway, which wedges the stream permanently the first time a reader guesses a size wrong.
- Framing.h: [u32 'MGLF'][u32 len][payload], 64 MiB cap, validated on read. A bad magic or an oversized length latches the reader dead, is logged at ERROR and makes every later call return MOBILEGL_ERR_PROTOCOL_MISMATCH. This is the second inherited defect: Feat/CS-Delta-IPC's Framing.h:41-45 Feed() always returned OK and its header peek merely returned false, so a desynchronized stream became a silent permanent hang; its LocalSocketTransport.cpp:232-236 then allocated on the peer-supplied length with no cap. The magic is also byte-ordered so it reads "MGLF" on the wire, where the earlier constant spelled "FLGM".
- Ring.h/.cpp: RingControl exactly as the inherited design (plan B section 8.1 -> earlier section 6.2): two independent cursor triples (cmd and stage, each {head, appliedTail, retiredTail}), appliedSeq / submittedSeq / retiredSeq / completedFrameSerial / presentAckSerial, serverEpoch, ringGeneration, consumerParked, producerParked, eventRingFull, eventDropped. One 4096-byte page with each contended group on its own cache line, pinned by static_assert on size and alignment and by an offset test.
- The SPSC pair uses monotonic byte cursors and a power-of-two mask, so a torn read can never look like a valid earlier position. A record never straddles the wrap: the producer emits a kPad filler to the boundary, which is always a multiple of 8 and therefore always has room for a header. The producer reclaims against retiredTail rather than appliedTail, so the day the server borrows a ring slot into the GPU timeline (kRecBorrowSlot) it does not silently degrade to early recycling. HardDrainRing bumps ringGeneration only when the ring is quiesced, and leaves the cursors monotonic so cached offsets are recognisably stale.
- The consumer bounds-checks every record header before dispatch (8-aligned, at least a header, no larger than what the producer published, contiguous inside the mapping) and reports corruption to the caller instead of dispatching into undefined behaviour. That is the runtime half of the earlier section 6.3 discipline: SEG_CMD is written by another process, so a compile-time static_assert on record sizes proves nothing about what is in the mapping.
- Doorbell.h/.cpp: spin then park, both directions (earlier section 6.2a). CondVarDoorbell for inproc, SocketDoorbell for spawn (one byte, codes 0x01 ring-advanced and 0x02 watermark-advanced) - no futex, eventfd or named event anywhere. The lost-wakeup window is closed by ordering: the waiter stores its park flag seq_cst and then re-tests the condition, NotifyIfParked loads the same flag seq_cst after the watermark is published, so one of the two always sees the other. kDefaultSpinUs is 50, the MOBILEGL_IPC_SPIN_US default.
- ShmSegment.{h,cpp} + ShmSegmentPosix.cpp: memfd_create by raw syscall on desktop Linux (the glibc wrapper is too recent to rely on), ASharedMemory_create on Android (API 26; libc's memfd_create wrapper is API 30, above MobileGL's floor), shm_open + immediate shm_unlink as the fallback. Adopt() refuses a descriptor whose fstat size is smaller than the size the peer announced, so a short segment cannot turn every later offset into an out-of-bounds map. ShmSegmentWin32.cpp (CreateFileMappingW in Local\) is compile-guarded and untested - this project's Windows machine is not a correctness gate.
- The Android path is compile-verified, not just written: an arm64-v8a NDK build with the option ON links, ShmSegmentPosix.cpp.o carries an undefined ASharedMemory_create, and ShmSegmentWin32.cpp.o is empty there. That build is also what caught the missing <cstddef> in ShmSegment.h and Framing.h, where std::size_t / std::ptrdiff_t only resolved through a transitive include on the host sysroot.
- FdPassing.{h,cpp}: SCM_RIGHTS in the FIRST transport commit, as plan B section 8.1 demands, over a dedicated AF_UNIX SOCK_DGRAM socketpair rather than the control byte stream (message boundaries survive on every POSIX - SOCK_SEQPACKET does not exist on macOS - and ancillary data can never be split from its payload). The third inherited defect this replaces: Feat/CS-Delta-IPC deferred fd passing and hardcoded `out->fd = -1` in LocalSocketTransport.cpp:296, so on the only platform that matters its data plane could not move one byte between processes. MSG_CTRUNC, an unexpected descriptor count and a malformed sideband header all close every descriptor received before failing, and a too-small sideband buffer is refused before the recvmsg so a datagram is never half-consumed.
- InProcessTransport.{h,cpp}: two in-memory queues plus the condvar doorbell pair. It keeps the frame size cap so nothing that passes in inproc becomes illegal after the switch to spawn, hands descriptors over with dup() under the same ownership rule as SCM_RIGHTS, and lets a peer's queued messages be drained after Shutdown - usually the last one says why it is going away.
- Not done here on purpose: the MOBILEGL_IPC_* environment variables are parsed in ConfigLoader.cpp, which belongs to another P0 work package running in parallel; this commit only exposes the constants (kDefaultSpinUs, kMaxFramePayloadSize, FdPassing::kMaxSidebandBytes) so that plumbing has something to set.
88 lines
3.5 KiB
C++
88 lines
3.5 KiB
C++
// MobileGL - MobileGL/MG_Remote/Transport/ShmSegment.h
|
|
// Copyright (c) 2025-2026 MobileGL-Dev
|
|
// Licensed under the GNU Lesser General Public License v3.0:
|
|
// https://www.gnu.org/licenses/gpl-3.0.txt
|
|
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// End of Source File Header
|
|
|
|
// One shared-memory segment: SEG_CMD, SEG_STAGE, SEG_REPLY, SEG_EVENT, a
|
|
// per-object SEG_SHADOW or a SEG_ADOPT store (inherited segment layout, plan
|
|
// section 8.1).
|
|
//
|
|
// Creation matrix (earlier plan section 6.1):
|
|
// - Android: ASharedMemory_create (API 26; libc's memfd_create wrapper
|
|
// only appears at API 30, which is above our floor)
|
|
// - desktop Linux: syscall(SYS_memfd_create, ...) directly, for the same
|
|
// reason - the glibc wrapper is recent and this file has to
|
|
// build against old sysroots
|
|
// - other POSIX: shm_open + immediate shm_unlink, the fd keeps it alive
|
|
// - Windows: CreateFileMappingW in the Local\ namespace
|
|
//
|
|
// Transfer is NOT done here. On POSIX the fd travels by SCM_RIGHTS
|
|
// (FdPassing.h / ITransport::ShareFd) and the name is only a debugging label;
|
|
// on Windows the section name travels inside the SegmentRef table.
|
|
//
|
|
// The Windows implementation is compile-guarded and untested at the time it
|
|
// was written: no Windows machine is a correctness gate for this project.
|
|
|
|
#pragma once
|
|
|
|
#include "../Protocol/mg_protocol_base.h"
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
namespace MobileGL::MG_Remote::Transport {
|
|
|
|
inline constexpr std::size_t kShmNameMax = 128;
|
|
|
|
class ShmSegment {
|
|
public:
|
|
ShmSegment() = default;
|
|
~ShmSegment();
|
|
|
|
ShmSegment(const ShmSegment&) = delete;
|
|
ShmSegment& operator=(const ShmSegment&) = delete;
|
|
ShmSegment(ShmSegment&& other) noexcept;
|
|
ShmSegment& operator=(ShmSegment&& other) noexcept;
|
|
|
|
// Creates a segment of `size` bytes owned by this process. `nameHint`
|
|
// is a short debug label (Windows: part of the section name peers
|
|
// resolve). The segment is NOT mapped yet.
|
|
static MobileGLResult Create(const char* nameHint, std::uint64_t size, ShmSegment& out);
|
|
|
|
// POSIX only: adopts a descriptor received over SCM_RIGHTS. Takes
|
|
// ownership of `fd` on success; on failure the caller still owns it.
|
|
static MobileGLResult Adopt(int fd, std::uint64_t size, ShmSegment& out);
|
|
|
|
// Windows only: opens a section the peer published by name.
|
|
static MobileGLResult OpenNamed(const char* name, std::uint64_t size, ShmSegment& out);
|
|
|
|
// Maps the whole segment. Read-only mappings are what the peer gets for
|
|
// a segment it does not own (SEG_CMD/SEG_STAGE on the server side).
|
|
MobileGLResult Map(bool readOnly);
|
|
void Unmap();
|
|
void Close(); // unmaps and releases the descriptor/handle
|
|
|
|
bool Valid() const;
|
|
void* Data() const { return m_mapping; }
|
|
std::uint64_t Size() const { return m_size; }
|
|
bool MappedReadOnly() const { return m_readOnly; }
|
|
const char* Name() const { return m_name; }
|
|
// POSIX: the descriptor to hand to ShareFd. -1 on Windows.
|
|
int Fd() const { return m_fd; }
|
|
|
|
private:
|
|
void Steal(ShmSegment&& other) noexcept;
|
|
|
|
char m_name[kShmNameMax] = {};
|
|
void* m_mapping = nullptr;
|
|
void* m_nativeHandle = nullptr; // Windows HANDLE; unused on POSIX
|
|
std::uint64_t m_size = 0;
|
|
int m_fd = -1;
|
|
bool m_readOnly = false;
|
|
};
|
|
|
|
} // namespace MobileGL::MG_Remote::Transport
|