Commit Graph

197 Commits

Author SHA1 Message Date
Timothy Flynn
cfa24d3367 LibIPC: Return a StringView for IPC message names 2026-04-23 09:42:07 -04:00
Shannon Booth
02911253dd LibWeb+LibIPC: Preserve MessagePort queue state across transfer
A MessagePort can be transferred while it already has local queued
state such as incoming messages drained from its transport,
outgoing messages posted before a transport exists, and a pending
shutdown to apply once the port is enabled.

Serialize and restore that state as part of transfer so it moves with
the port instead of being left behind on the old transport.

Also mark transports that are being transferred so shutdown of the old
endpoint during handoff is not reported as peer EOF. That shutdown is
part of moving the transport to the new owner, not peer disconnected.

Co-Authored-By: Alexander Kalenik <kalenik.aliaksandr@gmail.com>
2026-04-09 19:59:16 +02:00
Aliaksandr Kalenik
ffbbc7f49a LibIPC: Remove send timeout from Mach port transport
Remove the 5-second send timeout from mach_msg() to align Mach port
transport behavior with the Unix domain socket transport, which blocks
indefinitely on send.

The timeout also made it impossible to attach a debugger to a child
process on macOS: if you didn't attach within the 5-second window, the
send would time out and the connection would be marked as EOF.
2026-04-09 17:47:47 +02:00
kalenikaliaksandr
bdd9c98d44 LibIPC: Move Windows handle serialization into TransportSocketWindows
Move handle serialization and deserialization entirely into
TransportSocketWindows so that Windows can share the common Message and
File implementations with other platforms.
2026-04-08 20:19:05 +02:00
Aliaksandr Kalenik
1d025620e3 Everywhere: Move Mach bootstrap listener into LibIPC
Move MachPortServer from LibWebView into LibIPC as MachBootstrapListener
and move the Mach message structs from MachMessageTypes.h into LibIPC.

These types are IPC infrastructure, not UI or platform concerns.
Consolidating them in LibIPC keeps the Mach bootstrap handshake
self-contained in a single library and removes LibWebView's dependency
on LibThreading.
2026-03-24 19:51:52 +01:00
Aliaksandr Kalenik
e47f4cf90f Everywhere: Simplify Mach bootstrap transport handshake
Previously, the bootstrap handshake used a two-state machine
(WaitingForPorts / WaitingForReplyPort) to handle a race: the parent
registering transport ports and the child sending a bootstrap request
could arrive in either order, so whichever came first stored its half
and the second completed the handshake.

Eliminate the race by holding a mutex across spawn() and
register_child_transport(). Since the child cannot send a bootstrap
request before it exists, and the lock isn't released until its
transport is registered, handle_bootstrap_request() is guaranteed to
find the entry. This reduces the pending map to a simple pid-to-ports
lookup and collapses the two-variant state into two straightforward
branches: known child, or on-demand (non-child) caller like WebDriver.
2026-03-24 19:51:52 +01:00
Aliaksandr Kalenik
c6d740ea41 Everywhere: Remove dynamic Mach bootstrap registration on macOS
Registering multiple Mach port names with the bootstrap server at
runtime is not how macOS expects it to be used — the bootstrap server
is meant for static services, and the only reason we used it originally
was so child processes could reach back to the UI process.

Remove bootstrap_transport_over_socket(), which had both sides register
dynamic names with the bootstrap server and exchange them over a socket.
Instead, WebDriver and BrowserProcess connections now go through
MachPortServer instances directly. When a non-child process contacts a
MachPortServer, the server creates a port pair on demand (detected via
sysctl ppid check) and returns the local half immediately. This keeps
bootstrap server usage limited to the one original case: child processes
looking up their parent's MachPortServer.

WebDriver Session now runs its own MachPortServer per session.
--webdriver-content-path becomes --webdriver-mach-server-name on macOS.
Spare WebContent launches are skipped when a WebDriver session is active
to avoid bootstrap races.
2026-03-23 18:50:48 +01:00
Aliaksandr Kalenik
4ea4d63008 Everywhere: Replace Unix socket IPC transport with Mach ports on macOS
On macOS, use Mach port messaging instead of Unix domain sockets for
all IPC transport. This makes the transport capable of carrying Mach
port rights as message attachments, which is a prerequisite for sending
IOSurface handles over the main IPC channel (currently sent via a
separate out-of-band path). It also avoids the need for the FD
acknowledgement protocol that TransportSocket requires, since Mach port
right transfers are atomic in the kernel.

Three connection establishment patterns:

- Spawned helper processes (WebContent, RequestServer, etc.) use the
  existing MachPortServer: the child sends its task port with a reply
  port, and the parent responds with a pre-created port pair.

- Socket-bootstrapped connections (WebDriver, BrowserProcess) exchange
  Mach port names over the socket, then drop the socket.

- Pre-created pairs for IPC tests and in-message transport transfer.

Attachment on macOS now wraps a MachPort instead of a file descriptor,
converting between the two via fileport_makeport()/fileport_makefd().

The LibIPC socket transport tests are disabled on macOS since they are
socket-specific.
2026-03-23 18:50:48 +01:00
Davi Gomes
6d77c9edd1 Libraries: Move #pragma once above include headers
The #pragma once was placed after the #include directives instead of
immediately after the copyright comment, inconsistent with every other
header file
2026-03-22 14:05:44 +01:00
Aliaksandr Kalenik
03f142f54d LibIPC+LibWeb: Preserve raw TransferDataEncoder attachments
Encode transfer-data attachments as raw IPC attachments instead of first
rewrapping them as IPC::File values.

This is preparatory refactoring for the upcoming Mach-port transport
introduction on macOS, where attachments should remain transport-native
rather than being normalized through file descriptors.
2026-03-21 00:45:12 +01:00
Aliaksandr Kalenik
4ddba48e13 LibIPC+LibWebView: Remove MultiServer
MultiServer was inherited from SerenityOS where it was used in many
places. Now that BrowserProcess is its only consumer, inline the
connection acceptance logic directly into BrowserProcess and remove
the abstraction.
2026-03-20 23:23:28 +01:00
Timothy Flynn
02783a6f1d LibIPC: Disable unused LocalSocket notifier in TransportSocket
TransportSocket uses its own pipe-based notification mechanism on the IO
thread, making LocalSocket's built-in Core::Notifier redundant. When the
socket reaches EOF, this notifier is disabled from the IO thread. Since
the QSocketNotifier lives on the main thread, the its destruction is
deferred. If the socket is closed before the deferred destruction runs,
Qt detects the invalid socket on the next poll and prints:

    QSocketNotifier: Invalid socket 50 and type 'Read', disabling...

Fix this by disabling the redundant socket-level notifier upfront in the
TransportSocket constructor.
2026-03-19 09:44:22 +01:00
Aliaksandr Kalenik
19627bba54 LibIPC: Return TransportHandle directly from create_paired()
Previously, `create_paired()` returned two full Transport objects, and
callers would immediately call `from_transport()` on the remote side to
extract its underlying fd. This wasted resources: the remote
Transport's IO thread, wakeup pipes, and send queue were initialized
only to be torn down without ever sending or receiving a message.

Now `create_paired()` returns `{Transport, TransportHandle}` — the
remote side is born as a lightweight handle containing just the raw fd,
skipping all unnecessary initialization.

Also replace `release_underlying_transport_for_transfer()` (which
returned a raw int fd) with `release_for_transfer()` (which returns a
TransportHandle directly), hiding the socket implementation detail
from callers including MessagePort.
2026-03-14 18:25:18 +01:00
Jonathan Gamble
fd0709b6ce LibIPC: Notify readers when thread exits 2026-03-14 02:05:34 -05:00
Aliaksandr Kalenik
da6b928909 LibIPC+LibWeb: Introduce IPC::Attachment abstraction
Replace IPC::File / AutoCloseFileDescriptor / MessageFileType in
the IPC message pipeline with a new IPC::Attachment class. This
wraps a file descriptor transferred alongside IPC messages, and
provides a clean extension point for platform-specific transport
mechanisms (e.g., Mach ports on macOS) that will be introduced later.
2026-03-13 20:22:50 +01:00
Aliaksandr Kalenik
db9652643a LibIPC+LibWeb+LibWebView: Remove clone_from_transport() API
Replace clone_from_transport() (which dup()s the FD) with
from_transport() (which releases the FD) in the WebWorkerClient
call site. The UI process never uses the WebWorkerClient connection
after spawning — it only passes the transport to WebContent — so
releasing instead of cloning is safe and simpler.

This removes clone_from_transport() from TransportHandle, and
clone_for_transfer() from TransportSocket/TransportSocketWindows,
as they no longer have any callers.
2026-03-13 15:34:15 +01:00
Aliaksandr Kalenik
7c8bdccc26 LibIPC+LibWebView: Remove raw fd accessors from TransportHandle
Now that auxiliary service sockets are sent over IPC rather than passed
as command-line arguments, TransportHandle no longer needs to expose raw
file descriptors or manage close-on-exec flags. Remove fd() and
clear_close_on_exec(), and simplify the connect helpers accordingly.
2026-03-12 20:32:55 +01:00
Aliaksandr Kalenik
3bea3908b2 LibIPC+LibWeb+LibWebView+Services: Add IPC::TransportHandle
Add IPC::TransportHandle as an abstraction for passing IPC
transports through .ipc messages. This replaces IPC::File at
all sites where a transport (not a generic file) is being
transferred between processes.

TransportHandle provides from_transport(),
clone_from_transport(), and create_transport() methods that
encapsulate the fd-to-socket-to-transport conversion in one
place. This is preparatory work for Mach port support on
macOS -- when that lands, only TransportHandle's internals
need to change while all .ipc definitions and call sites
remain untouched.
2026-03-12 20:32:55 +01:00
Aliaksandr Kalenik
2e881978af LibIPC+LibWeb+LibWebView+Services: Add Transport::create_paired()
Consolidate the repeated socketpair + adopt + configure pattern from
4 call sites into a single Transport::create_paired() factory method.
This fixes inconsistent error handling and socket configuration across
call sites, and prepares for future mach port support on macOS.
2026-03-11 14:42:24 +01:00
Jelle Raaijmakers
d3e1396ece LibIPC: Remove message buffer allocations for enqueuing messages
We were allocating vectors on the heap and copying the message header's
and payload's bytes to it before passing them on to
`::enqueue_message()`.

Remove these allocations and just pass `ReadonlyBytes` views into the
message header and payload directly. On my machine, this reduces the
time spent on the send-side queuing path by 13% to 42%, depending on the
message size.
2026-03-04 17:17:49 -05:00
Jelle Raaijmakers
0b1206cf54 LibIPC: Do not allocate for unprocessed bytes
While we're processing received messages, we can end up with unprocessed
bytes after the last message. Instead of copying the data into a new
ByteBuffer, just move the bytes inside the existing buffer and resize
it.

This does mean that as long as there are unprocessed bytes after reading
incoming messages, the buffer does not shrink. But as soon as there's
nothing left, we clear this buffer again.
2026-03-04 17:15:21 -05:00
Andreas Kling
ec55c80929 LibIPC: Take MessageBuffer by lvalue reference in post_message()
Change post_message(MessageBuffer) to post_message(MessageBuffer&)
to avoid copying the MessageBuffer onto the stack. MessageBuffer
contains a Vector<u8, 1024> with a 1024-byte inline buffer, so
passing by value was adding over 1 KiB to the stack frame of
handle_messages().

This reduces the handle_messages() stack frame from 1328 bytes to
224 bytes, which matters because handle_messages() sits near the
base of the call stack when GC runs its conservative stack scan
in response to IPC requests.
2026-02-28 14:10:14 +01:00
Ben Wiederhake
31158ef448 LibIPC: Remove unused header in TransportSocket 2026-02-23 12:15:23 +01:00
Ben Wiederhake
35954223af LibIPC: Remove unused header in Message 2026-02-23 12:15:23 +01:00
Ben Wiederhake
481f6b9154 LibIPC: Remove unused header in Encoder 2026-02-23 12:15:23 +01:00
Ben Wiederhake
7093082d75 LibCore: Remove forward declaration of non-existent type 2026-02-23 12:15:23 +01:00
Ben Wiederhake
ba01b2c322 LibIPC: Remove unused header in Decoder 2026-02-23 12:15:23 +01:00
Ben Wiederhake
738294f4cd LibIPC: Remove unused header in Connection 2026-02-23 12:15:23 +01:00
Shannon Booth
64532bcfa0 LibURL: Add ability to store whether an origin is a file scheme origin 2026-02-21 23:00:57 +01:00
Timothy Flynn
f31c72b6a6 LibIPC: Allow passing arguments to system server connections 2026-02-15 15:25:30 -05:00
R-Goc
3a86e779bd LibCore/LibIPC/Meta: Stop using deprecated Winsock functions
This commit stops using deprecated WSA functions. While the ANSI
versions are most likely not going anywhere, Windows is natively UTF-16
so it has to convert to ANSI internally. All the ANSI functions in
Winsock are marked as deprecated. The macro suppressing the warnings is
no longer defined.
2026-02-02 10:35:11 +01:00
Zaggy1024
e2635af2ed Everywhere: Move the thread name parameter for Thread constructors
The name parameter formats very poorly when a lambda is passed to
Thread, so let's instead put it first now that all Threads are named.
2026-01-26 15:51:46 -06:00
Zaggy1024
d2a1d727ac Everywhere: Give unnamed threads names 2026-01-26 15:51:46 -06:00
Andreas Kling
a0c389846e Revert "LibIPC: Move message decoding from main thread to I/O thread"
This reverts commit 757795ada4.

Appears to have regressed WPT.
2026-01-25 12:19:53 +01:00
Andreas Kling
757795ada4 LibIPC: Move message decoding from main thread to I/O thread
Previously, IPC messages were decoded on the main thread:

1. I/O thread received raw bytes and file descriptors
2. I/O thread stored them in a queue and notified main thread
3. Main thread decoded bytes into Message objects
4. Main thread processed the messages

Now, decoding happens on the I/O thread:

1. I/O thread receives raw bytes and file descriptors
2. I/O thread decodes them using a configurable MessageDecoder
3. I/O thread calls MessageHandler which stores decoded messages
4. I/O thread signals condition variable (for sync waiters)
5. I/O thread wakes main event loop via deferred_invoke()
6. Main thread processes already-decoded messages

This is achieved by:

- Adding MessageDecoder and MessageHandler callbacks to TransportSocket
- Connection template sets up the decoder (tries both endpoints)
- ConnectionBase::initialize_messaging() sets up the handler
- Storing a WeakEventLoopReference to wake the main thread
- Using mutex + condition variable for thread-safe queue access
- Sync message waiting now uses the CV directly instead of polling

The raw message API (read_as_many_messages_as_possible_without_blocking)
is preserved for MessagePort which uses its own decoding logic.

This architecture prepares for future multi-thread dispatch where
different message types could be routed to different handler threads
(e.g., scrolling messages to a dedicated scroll thread).
2026-01-25 09:32:51 +01:00
Andreas Kling
534c4c3736 LibIPC: Log actual decode errors when message parsing fails
Previously, when an IPC message failed to parse, we only logged
"Failed to parse IPC message" followed by a hex dump, making it
difficult to diagnose the actual cause.

Now we log the specific error from each endpoint's decode attempt,
making it much easier to identify issues like size limit violations
or invalid field values.
2026-01-24 15:23:02 +01:00
Andreas Kling
51ddfaafbf LibIPC: Don't apply decode size limit to AnonymousBuffer
AnonymousBuffer is backed by shared memory, not heap allocation.
The MAX_DECODED_SIZE limit in decode_size() is meant to prevent OOM
from malicious peers claiming huge sizes that would cause heap
allocations, but this doesn't apply to AnonymousBuffer since the
memory is already allocated by the sender.

This fixes decoding of large animated images (e.g. 300 frames at
240x240) where the total bitmap data exceeds 64 MiB.
2026-01-24 15:23:02 +01:00
Andreas Kling
2a6045833c LibCore: Make ProxyData::port a u16
TCP ports are always u16 and this prevents invalid values from getting
into this field somehow.
2026-01-22 17:38:15 +01:00
Andreas Kling
b4cbd321d9 LibIPC: Add additional validation for decoded values
Also add upfront bounds checking for variant indices before entering
the recursive template, allowing us to convert the template's fallback
error into VERIFY_NOT_REACHED() since the index is now guaranteed to
be valid when we enter the recursive decode.
2026-01-22 17:38:15 +01:00
Andreas Kling
980f23e58b LibIPC: Add VERIFY assertions to catch encoding bugs
- VERIFY that IPC::File has a valid fd (>= 0) before encoding
- VERIFY that container sizes fit in u32 before encoding

These catch programming errors where we accidentally try to encode
invalid data. Encoding bugs should crash immediately so we catch them
during development.
2026-01-22 17:38:15 +01:00
Andreas Kling
4d344e6cf7 LibIPC: VERIFY message limits when encoding outgoing messages
Add VERIFY assertions to catch bugs where we accidentally try to send
messages that exceed MAX_MESSAGE_PAYLOAD_SIZE or MAX_MESSAGE_FD_COUNT.

These are programming errors and should crash immediately so we catch
them during development, rather than silently sending invalid data.
2026-01-22 17:38:15 +01:00
Andreas Kling
cb3bc3c493 LibIPC: Move IPC message limits to a shared Limits.h header
This consolidates the message size and FD count limits into a single
header file that can be used by both the encoding and decoding sides
of the IPC layer.
2026-01-22 17:38:15 +01:00
Andreas Kling
6188c5a40f LibIPC: Add maximum size limit for decoded containers
A malicious peer could claim that a string, buffer, or vector has an
extremely large size (up to 4 GiB), causing allocation failures or OOM.

Add MAX_DECODED_SIZE (64 MiB) check in decode_size() to reject
excessively large containers before attempting allocation.
2026-01-22 17:38:15 +01:00
Andreas Kling
bef09fc277 LibIPC: Add limits on accumulated unprocessed data in TransportSocket
A malicious or misbehaving peer could send data faster than we process
it, causing unbounded memory growth in the unprocessed bytes buffer
and file descriptor queue.

Add MAX_UNPROCESSED_BUFFER_SIZE (128 MiB) and MAX_UNPROCESSED_FDS (512)
limits. When exceeded, the peer is disconnected gracefully rather than
allowing memory exhaustion.
2026-01-22 17:38:15 +01:00
Andreas Kling
bdca85f985 LibIPC: Add maximum limits for IPC message size and FD count
Reject messages from peers that exceed reasonable limits:
- Maximum payload size: 64 MiB
- Maximum file descriptor count: 128

Also use checked arithmetic for message size calculations to prevent
integer overflow attacks.

These limits prevent malicious peers from causing excessive memory
allocation or resource exhaustion.
2026-01-22 17:38:15 +01:00
Andreas Kling
8235506d5f LibIPC: Use TRY instead of MUST in post_message()
post_message() returns ErrorOr but was using MUST internally for
transfer_message(). If the transfer fails (e.g., socket error), we
would crash instead of propagating the error to the caller.
2026-01-22 17:38:15 +01:00
Andreas Kling
d15acc46d6 LibIPC: Use checked arithmetic for message size calculations
The message parsing loop performs arithmetic on payload_size (u32) and
index (size_t). While overflow is unlikely on 64-bit systems, use
Checked<size_t> to explicitly validate:

1. message_size = payload_size + sizeof(MessageHeader)
2. new_index = index + payload_size + sizeof(MessageHeader)

This prevents potential integer overflow attacks from malicious peers.
2026-01-22 17:38:15 +01:00
Andreas Kling
9e34434f3b LibIPC: Handle allocation failures gracefully in TransportSocket
Replace crash-on-OOM patterns with graceful error handling:

- Use try_append() instead of append() for buffer operations
- Handle ByteBuffer::copy() failure instead of using MUST()

A malicious peer could send messages with large payload sizes to
trigger OOM conditions. Instead of crashing, we now disconnect
the misbehaving peer.
2026-01-22 17:38:15 +01:00
Andreas Kling
a85abca388 LibIPC: Use try_resize() when decoding Vector of arithmetic types
The previous code used Vector::resize() which internally uses MUST(),
causing a crash if memory allocation fails. A malicious peer could
send a message with a large vector size to trigger OOM and crash the
recipient.

Use try_resize() instead to propagate the error gracefully.
2026-01-22 17:38:15 +01:00
Andreas Kling
1a213a9020 LibIPC: Harden TransportSocket against malicious FD count values
A malicious peer could exploit the file descriptor acknowledgement
protocol in several ways:

1. Send messages with fd_count values that overflow when accumulated
2. Send acknowledgements claiming more FDs were received than we sent

Both attacks would crash the recipient via VERIFY failures in
Queue::dequeue() or integer overflow.

Fix by using Checked<u32> for fd counters and validating the queue
isn't empty before dequeuing acknowledged FDs.
2026-01-22 17:38:15 +01:00