Commit Graph

136 Commits

Author SHA1 Message Date
Aliaksandr Kalenik
509c86dca0 LibIPC: Simplify IPC read hook
- Return `PeerEOF` enum instead of `Error` containing string from
  `drain_messages_from_peer()`. There are no other error types to return
  from this function, so boolean-like enum is sufficient.
- Don't override read hook in `ConnectionFromClient` constructor. It was
  previously redefined only to suppress EOF error returned by
  `drain_messages_from_peer()`.
2025-10-21 09:31:22 +02:00
Rocco Corsi
b6b56910e8 LibIPC: Shutdown IPC handler when transport is lost during sync event
If the Ladybird process crashes or just ends normally, the IPC transport
connection with WebContent may be shutdown after a send sync event (for
example: WebContentClient DidRequestCookie) was sent from WebContent,
but before the Ladybird process provided the matching sync event
response (for example: WebContentClient DidRequestCookieResponse). This
can lead to a runaway WebContent process if other IPC events (for
example: WebContentServer DidPaint, or SetSystemVisibilityState, or
MouseEvent, or CloseServer, etc...) are also queued when the IPC
connection is shutdown.

At the core of the issue is that the loop waiting for the matching
send sync response will prioritize waiting for the response and remain
spinning even if the IPC connection is reporting that it was shutdown,
but only if there happens to be other unrelated events received before
the IPC shutdown is detected. These unrelated events will not be
processed because the loop is stuck waiting for the response that due
to the Ladybird process having stopped, will never be sent.

Because the shutdown of the IPC connection is not handled when other
events happen to be also present, new events may be posted for transfer
by the WebContent process if the page is very active. If many new events
are posted this could lead to a slow or very quick memory leak in the
WebContent process due to the queue growing large, sometimes all the way
to total system memory exhaustion. If no events or only a few new events
are sent, then the leak may be hard to detect.

This PR fixes the faulty IPC shutdown handling by not getting stuck if
any messages are present in the receive queue. Before returning to the
caller any remaining messages will be immediately processed.
2025-10-07 17:04:32 -05:00
Aliaksandr Kalenik
93d7efa4c3 LibIPC: Delete unused code in Connection 2025-10-01 14:59:23 -04:00
Tomasz Strejczek
6fc4c544de LibIPC: Remove DateTime encoder/decoder
Remove LibCore::DateTime encoder/decoder as it is not longer needed.
2025-09-30 12:39:01 +02:00
Zaggy1024
2aaf53bd2c Everywhere: Use a forward declaration for pointers to Threading::Thread 2025-09-22 17:28:21 -05:00
Tete17
658477620a LibWeb/LibURL/LibIPC: Extend createObjectURL to also accept MediaSources
This required some changes in LibURL & LibIPC since it has its own
definition of an BlobURLEntry. For now, we don't have a concrete usage
of MediaSource in LibURL so it is defined as an empty struct.

This removes one FIXME in an idl file.
2025-08-19 23:50:38 +02:00
Jelle Raaijmakers
63119355e3 LibIPC: Do not try to send a response back if transport was closed
The local handling of some messages might cause the transport to get
closed. If that's the case, we shouldn't try to send back a response.

This fixes many of the "Trying to post_message during IPC shutdown"
errors I was seeing when terminating Ladybird or when abnormally exiting
from LibWeb tests.
2025-08-17 20:51:56 -04:00
Timothy Flynn
13ed6aba71 AK+LibIPC: Implement an encoder/decoder for UTF-16 strings 2025-08-02 10:10:14 -07:00
Timothy Flynn
fd6d868ae2 LibIPC: Add some type aliases and MessageBuffer helpers
To re-use some of these wordy types outside of LibIPC, let's add some
aliases.
2025-07-18 10:09:02 -04:00
ayeteadoe
25f5936dee CMake: Rename serenity_* helper functions/macros to ladybird_* 2025-07-03 23:19:41 +02:00
ayeteadoe
dbba6c0df9 LibWeb: Enable in Windows CI 2025-06-30 10:50:36 -06:00
Shannon Booth
38765fd617 LibURL: Use a nonce to distinguish opaque origins
Opaque origins are meant to be unique in terms of equality from
one another. Since this uniqueness needs to be across processes,
use a nonce to implement the uniqueness check.
2025-06-25 16:47:09 +01:00
stasoid
8af2a49b5c LibIPC: Make TransportSocketWindows responsible for reading entire
messages. Port of a371f84 to Windows.
2025-06-17 15:36:47 -06:00
stasoid
ccf303eefc LibIPC: Move AutoCloseFileDescriptor to its own header 2025-06-17 15:36:47 -06:00
Shannon Booth
e0d7278820 LibURL+LibWeb: Make URL::Origin default constructor private
Instead, porting over all users to use the newly created
Origin::create_opaque factory function. This also requires porting
over some users of Origin to avoid default construction.
2025-06-17 20:54:03 +02:00
rmg-x
a4d931d14a LibCore+LibIPC: Move various encode/decode specializations to LibIPC
This removes a dependency on LibIPC from LibCore.
2025-06-14 16:03:26 -04:00
rmg-x
18f28f398b LibCore+LibIPC: Remove badge on File::leak_fd
This removes a dependency on LibIPC from LibCore.
2025-06-14 16:03:26 -04:00
Andrew Kaster
8095663f86 LibIPC: Chunk sent file descriptors by MAX_TRANSFER_FDS
This limitation of the underlying Unix socket implementation can
cause IPC failures on pages with tons of images and network requests.

Modify the code called from TransportSocket's send thread to limit the
number of fds to MAX_TRANSFER_FDS, and ensure that we will keep sending
as long as we have either bytes or file descriptors to send.
2025-05-24 19:15:06 +03:00
Andrew Kaster
87fbfcadd1 LibIPC: Refactor message header encoding to use a helper method
Manually memcpying into a Vector in the body of post_message is
a bit much.
2025-05-24 19:15:06 +03:00
Timothy Flynn
36da270dbe LibIPC+LibWeb: Flush MessagePort messages before closing
The spec isn't super clear on what disentagling a MessagePort means. But
we are required to send all pending messages before closing the port.

This is a bit tricky because the transport socket performs writes on a
background thread. From the main thread, where the disentanglement will
occur, we don't really know the state of the write thread. So what we do
here is stop the background thread then flush all remaining data from
the main thread.
2025-05-21 06:54:44 -04:00
Timothy Flynn
8b3355ed0d LibIPC: Address a couple of clangd warnings in IPC::TransportSocket
* We need the full definition of IPC::File in the header.
* We need(ed) Core::System in the header. Move AutoCloseFileDescriptor's
  ctor and dtor out-of-line to avoid this.
2025-05-21 06:54:44 -04:00
Timothy Flynn
7280ed6312 Meta: Enforce newlines around namespaces
This has come up several times during code review, so let's just enforce
it using a new clang-format 20 option.
2025-05-14 02:01:59 -06:00
Timothy Flynn
08e82ddaf0 LibCore: Prefer Error::is_errno over Error::is_syscall
These are the only users of the latter, so let's just use is_errno
(which will effectively give us the same result) to avoid confusion
over these.
2025-05-10 21:19:46 -04:00
Andrew Kaster
16f72ca36c LibIPC: Don't verify that socket is closed when getting EPIPE on send
We can get an EPIPE when the other end of the connection closes, such
as when switching to a new WebContent process due to site isolation.
2025-04-30 11:12:23 -04:00
Andrew Kaster
27db7ed11f LibIPC: Guard better against closure in the TransportSocket send thread
And crash less when the socket is closed while there are still messages
in the queue.
2025-04-29 09:51:22 -06:00
Aliaksandr Kalenik
466c793fdb LibIPC: Use AllocatingMemoryStream in TransportSocket send queue
Memory stream is a more suitable container for the socket send queue,
as using it results in fewer allocations than trying to emulate a stream
using a Vector.
2025-04-15 18:48:53 +02:00
Aliaksandr Kalenik
b53694b4c0 LibIPC+LibWeb: Delete LargeMessageWrapper workaround in IPC connection
Bring back 2d625f5c23
2025-04-10 23:40:02 +02:00
Aliaksandr Kalenik
681333d329 LibIPC: Protect underlying socket of TransportSocket with RWLock
This is necessary to prevent the socket from being closed while it is
being used for reading or writing.
2025-04-10 23:40:02 +02:00
Aliaksandr Kalenik
14dc7686c3 LibIPC: Change TransportSocket to write large messages in small chunks
Bring back d6080d1fdc with a missing check
whether underlying socket is closed, before accessing `fd()` that is
optional and empty in case of closed socket.
2025-04-10 23:40:02 +02:00
Tim Ledbetter
1ee56d34e7 Revert "LibIPC+LibWeb: Delete LargeMessageWrapper workaround in IPC…
…connection"

This reverts commit 2d625f5c23.
2025-04-10 16:24:38 +01:00
Tim Ledbetter
3fcdbef327 Revert "LibIPC: Change TransportSocket to write large messages in…"
…small chunks.

This reverts commit d6080d1fdc.
2025-04-10 16:24:38 +01:00
Aliaksandr Kalenik
2d625f5c23 LibIPC+LibWeb: Delete LargeMessageWrapper workaround in IPC connection
It's no longer needed because TransportSocket is now capable of properly
sending large messages.
2025-04-10 01:30:08 +02:00
Aliaksandr Kalenik
d6080d1fdc LibIPC: Change TransportSocket to write large messages in small chunks
With this change TransportSocket becomes capable of sending large
messages without relying on workarounds, such as sending the message as
a shared memory file descriptor when it can't fully fit into the socket
buffer.

It's implemented by combining all enqueued messages into two buffers:
one for bytes and another for fds, and repeatedly attempts to write them
in smaller chunks, waiting for the socket to become writable again if
the receiver needs time to consume the data.

Another significant improvement brought by this change is that we no
longer drop messages queued for sending if the socket doesn't become
writable after a 100ms timeout. Instead, we return the message to the
send buffer and continue waiting for the socket to become writable.
2025-04-10 01:30:08 +02:00
Aliaksandr Kalenik
db8c443392 Everywhere: Make TransportSocket non-movable
Instead of wrapping all non-movable members of TransportSocket in OwnPtr
to keep it movable, make TransportSocket itself non-movable and wrap it
in OwnPtr.
2025-04-09 15:27:52 +02:00
Aliaksandr Kalenik
14bac7b287 LibIPC: Move send thread from IPC connection to the transport layer
By doing this we also make MessagePort, that relies on IPC transport,
to send messages from separate thread, which solves the problem when
WebWorker and WebContent could deadlock if both were trying to post
messages at the same time.

Fixes https://github.com/LadybirdBrowser/ladybird/issues/4254
2025-04-08 21:09:24 +02:00
Aliaksandr Kalenik
ab35325003 LibIPC: Move early fd deallocation workaround to the transport layer
Reimplements c3121c9d at the transport layer, allowing us to solve the
same problem once, in a single place, for both the LibIPC connection and
MessagePort. This avoids exposing a workaround for a macOS specific Unix
domain socket issue to higher abstraction layers.
2025-04-08 21:09:24 +02:00
Aliaksandr Kalenik
3525467e56 LibIPC: Join send thread instead of detaching in ~ConnectionBase()
By doing this we ensure that sending thread won't try to write into a
closed socket.
2025-04-08 21:09:24 +02:00
Aliaksandr Kalenik
ac643aa392 LibIPC: Break from message parsing if whole message payload is not ready
Fixes the bug when we try to read message payload without checking if we
received enough bytes or file descriptors.
2025-04-07 20:26:01 +02:00
Aliaksandr Kalenik
a371f849e3 LibIPC: Make TransportSocket responsible for reading entire messages
With this change, the responsibility for prepending messages with their
size and ensuring the entire message is received before returning it to
the caller is moved to TransportSocket. This removes the need to
duplicate this logic in both LibIPC and MessagePort.

Another advantage of reducing message granularity at IPC::Transport
layer is that it will make it easier to support alternative transport
implementations (like Mach ports, which unlike Unix domain sockets are
not stream oriented).
2025-04-07 16:59:49 +02:00
Aliaksandr Kalenik
c3121c9d8a LibIPC+Meta: Keep message buffer alive until acknowledged by peer
This change ensures that instead of immediately deallocating the message
buffer after sending, we retain it in an acknowledgement wait queue
until an acknowledgement is received from the peer. This is necessary
to handle a behavior of the macOS kernel, which may prematurely
garbage-collect file descriptors contained within the message buffer
before the peer receives them.

The acknowledgement mechanism assumes messages are received in the same
order they were sent so, each acknowledgement message simply indicates
the count of successfully received messages, specifying how many entries
can safely be removed from the acknowledgement wait queue.
2025-04-05 23:14:32 +02:00
R-Goc
28d5d982ce Everywhere: Remove unused private fields
This commit removes the -Wno-unusued-private-field flag, thus
reenabling the warning. Unused field were either removed or marked
[[maybe_unused]] when unsure.
2025-04-04 12:40:07 +02:00
Aliaksandr Kalenik
4b04e97feb LibWeb: Send IPC messages exceeding socket buffer through shared memory
It turned out that some web applications want to send fairly large
messages to WebWorker through IPC (for example, MapLibre GL sends
~1200KiB), which led to failures (at least on macOS) because buffer size
of TransportSocket is limited to 128KiB. This change solves the problem
by wrapping messages that exceed socket buffer size into another message
that holds wrapped message content in shared memory.

Co-Authored-By: Luke Wilde <luke@ladybird.org>
2025-04-03 13:55:41 +02:00
stasoid
2abc792938 LibCore: Implement System::set_close_on_exec 2025-03-19 20:25:24 -06:00
Timothy Flynn
cf69f52d53 LibIPC+Everywhere: Always pass ownership of transferred data to clients
This has been a longstanding ergonomic issue with our IPC compiler. Non-
trivial types were previously passed by const&. So if we wanted to avoid
expensive copies, we would have to const_cast and move the data.

We now pass ownership of all transferred data to the client subclasses.
This allows us to remove const_cast from these methods, and allows us to
avoid some trivial expensive copies that we didn't bother to const_cast.
2025-03-09 11:14:20 -04:00
Timothy Flynn
68947d55d9 LibIPC: Do not require constructing containers when sending IPC messages
For example, consider the following IPC message:

    do_something(u64 page_id, String string, Vector<Data> data) =|

We would previously generate the following C++ method to encode/transfer
this message:

    void do_something(u64 page_id, String string, Vector<Data> data);

This required the caller to either have to copy the non-trivial types or
`move` them in. In some places, this meant we had to construct temporary
vectors just to send an IPC.

This isn't necessary because we weren't holding onto these parameters
anyways. We would construct an IPC::Message subclass with them (which
does require owning types), but then immediate encode the message to
an IPC::MessageBuffer and send it.

We now generate code such that we don't need to construct a Message. We
can simply encode the parameters directly without needing ownership.
This allows us to take view-types to IPC parameters.

So the above example now becomes:

    void do_something(u64, StringView, ReadonlySpan<Data>);
2025-03-09 11:14:20 -04:00
Timothy Flynn
a5b996c079 LibIPC: Add a generic encoder for spans 2025-03-09 11:14:20 -04:00
Timothy Flynn
b090952274 LibIPC: Remove outdated warning about changing IPC encodings
We are no longer constrained by this LibC encoding.
2025-03-09 11:14:20 -04:00
Timothy Flynn
fe2dff4944 AK+Everywhere: Convert JSON value serialization to String
This removes the use of StringBuilder::OutputType (which was ByteString,
and only used by the JSON classes). And it removes the StringBuilder
template parameter from the serialization methods; this was only ever
used with StringBuilder, so a template is pretty overkill here.
2025-02-20 19:27:51 -05:00
Shannon Booth
25df98875d LibIPC: Ensure only valid URLs are passed over IPC
Invalid URLs should be signified by a wrapper class, such as an
Optional<URL::URL> in the IPC file. I do not believe that we have
anything which currently relies on passing through an invalid URL.
2025-02-19 08:01:35 -05:00
Sam Atkins
b3b7e76c10 IPCCompiler: Remove invalid state from IPC message classes
At some point, we stopped ever constructing invalid messages. This makes
that clearer, and will allow us to stop requiring that IPC arguments be
default-constructible.
2025-02-17 11:37:38 -05:00