Commit graph

647 commits

Author SHA1 Message Date
Andreas Kling
a25fc5ad8a LibCore+LibWeb: Add ScopedAutoreleasePool and use it on macOS
On macOS, Objective-C methods frequently return autoreleased objects
that accumulate until an autorelease pool is drained. Our event loop
(Core::EventLoop) and rendering thread both lacked autorelease pools,
causing unbounded accumulation of autoreleased objects.

The rendering thread was the worst offender: every Skia flush triggers
Metal resource allocation which sets labels on GPU textures via
-[IOGPUMetalResource setLabel:], creating autoreleased CFData objects.
With ~1M+ such objects at 112 bytes each, this leaked ~121MB. Metal
command buffer objects (_MTLCommandBufferEncoderInfo, etc.) also
accumulated, adding another ~128MB.

Add Core::ScopedAutoreleasePool, a RAII wrapper around the ObjC runtime
autorelease pool (no-op on non-macOS), and drain it:
- Every event loop pump (like NSRunLoop does)
- Every compositor loop iteration on the rendering thread
2026-03-15 11:42:43 +01:00
Andreas Kling
643f2884cc LibCore: Fix signal handler deadlock in EventLoopImplementationUnix
handle_signal() called ThreadData::the() which can acquire a write
lock on s_thread_data_lock if the thread hasn't initialized its
ThreadData yet. If the signal interrupts a thread that already holds
a read lock on s_thread_data_lock (e.g. in unregister_notifier()),
this deadlocks — the write lock waits for the read lock, but the
read lock holder is blocked in the signal handler.

Fix by accessing the thread-local s_this_thread_data directly. If
the thread has no ThreadData, there's no wake pipe to write to, so
we just return.
2026-03-07 13:09:50 +01:00
Andreas Kling
656a84e180 LibThreading: Fix data race in RWLock::unlock()
The generic unlock() wrote to m_write_locked from every thread
regardless of whether a read or write lock was held. When multiple
threads held concurrent read locks, their unlock() calls would race
on the non-atomic m_write_locked and m_read_locked_with_write_lock
fields.

Split unlock() into unlock_read() and unlock_write() so that read
unlocks never touch the write-lock tracking fields. The RWLockLocker
template dispatches at compile time based on LockMode.
2026-03-07 13:09:50 +01:00
Zaggy1024
8c96f7b3aa LibCore: Stop atomically ref-counting EventReceiver
Inheriting from AtomicRefCounted and Weakable is asking for misuse of
WeakPtr to result in TOCTOU-caused UAFs.

In order to ensure we're not misusing EventReceiver across threads, I
ran test-web and some sites with a temporary hack to verify that ref()s
and unref()s are always called from the same thread on the class.
2026-03-02 17:06:39 -06:00
Zaggy1024
d6ff027593 LibCore: Remove the pending jobs functionality from ThreadEventQueue
This is no longer used.
2026-03-02 17:06:39 -06:00
Zaggy1024
035b489266 LibCore: Prevent UAF on event loop wake handles on Windows
We were holding a reference to ThreadData, which could be destroyed
earlier than the EventLoop itself, causing other threads to UAF trying
to signal a wake.
2026-03-02 17:06:39 -06:00
Zaggy1024
04e95b7dd1 LibCore: Avoid UAF on the array of wake pipes when exit()ing
If exit() is called on a thread with an EventLoop in the stack, the
ThreadData storing the array of wake pipes will be destroyed first.
Threads can still take a strong reference to the EventLoop after that,
and will read the fds from freed memory.

Instead, take a copy of the write fd, and swallow EBADF when writing to
it, since that only indicates that the thread and event loop are
exiting, so there's nothing to do with the wake.
2026-03-02 17:06:39 -06:00
R-Goc
919f44f3a5 LibCore: Explicitly export symbols from LibCore
This patch adds explicit symbol export to LibCore. This leads to about
350 less symbols being exported.
2026-02-26 18:31:57 +01:00
R-Goc
57b12d3ca0 LibCore: Remove LibCoreMinimal 2026-02-26 18:31:57 +01:00
Ben Wiederhake
1fd672c4c2 LibCore: Remove unused header in File 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
0206150b70 LibCore: Remove unused header in TimeZoneWatcherUnix 2026-02-21 19:27:35 +01:00
Ben Wiederhake
08f4cf10ed LibCore: Remove unused header in LocalServer 2026-02-21 19:27:35 +01:00
Ben Wiederhake
084f5ababd LibCore: Remove unused header in EventLoopImplementationUnix 2026-02-21 19:27:35 +01:00
Ben Wiederhake
ee5a6e1b28 LibCore: Remove unused header in AnonymousBuffer 2026-02-21 19:27:35 +01:00
Ben Wiederhake
6a34849238 LibCore: Remove unused header in ThreadEventQueue 2026-02-21 19:27:35 +01:00
Ben Wiederhake
e27bdaafe7 LibCore: Remove unused header in ResourceImplementationFile 2026-02-21 19:27:35 +01:00
Ben Wiederhake
387f378f45 LibCore: Remove unused header in ResourceImplementation 2026-02-21 19:27:35 +01:00
Ben Wiederhake
fb7095ab88 LibCore: Remove unused header in Resource 2026-02-21 19:27:35 +01:00
Ben Wiederhake
6335f4745f LibCore: Remove unused header in MimeData 2026-02-21 19:27:35 +01:00
Ben Wiederhake
b33b08050a LibCore: Remove unused header in EventReceiver 2026-02-21 19:27:35 +01:00
Ben Wiederhake
ca28b98b83 LibCore: Remove unused header in EventLoopImplementation 2026-02-21 19:27:35 +01:00
Ben Wiederhake
a929850fdb LibCore: Remove unused header in EventLoop 2026-02-21 19:27:35 +01:00
Ben Wiederhake
543fb77e5d LibCore: Remove unused header in ConfigFile 2026-02-21 19:27:35 +01:00
Ben Wiederhake
94dee9bea2 LibCore: Remove unused header in System 2026-02-21 19:27:35 +01:00
Ben Wiederhake
e0f39d5114 LibCore: Remove unused header in StandardPaths 2026-02-21 19:27:35 +01:00
Ben Wiederhake
36063f6881 LibCore: Remove unused header in MappedFile 2026-02-21 19:27:35 +01:00
Ben Wiederhake
e6e0bf0136 LibCore: Remove unused header in Environment 2026-02-21 19:27:35 +01:00
Andreas Kling
e87f889e31 Everywhere: Abandon Swift adoption
After making no progress on this for a very long time, let's acknowledge
it's not going anywhere and remove it from the codebase.
2026-02-17 10:48:09 -05:00
Shannon Booth
32c51774ee LibCore: Remove unusued Core::System::mkdtemp
This suffers from TOCTOU issues, and alternatives should probably
be used. Since it is unused, let's just remove it.
2026-02-14 10:25:33 -05:00
Timothy Flynn
4ad9543a65 LibCore: Add helpers to share arbitrary versions between processes
This will allow sharing e.g. document cookie versions between the UI and
WebContent processes, and safely accessing those versions.

Core::AnonymousBuffer internally creates a minimum buffer of PAGE_SIZE
bytes. This is much more than the size of a single version, but this
affords us the opportunity to share multiple versions in a single buffer
between processes. With a PAGE_SIZE of 4096, we can share up to 512
versions in a single buffer.
2026-02-05 07:28:07 -05:00
Andrew Kaster
643605099e LibCore: Move Core::MappedFile to LibCoreMinimal 2026-02-03 10:29:51 +01: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
R-Goc
1ac35d19f9 LibCore: Handle long paths in ProcessWindows
This commit handles the case where the process executable has a long
path.
2026-02-02 10:35:11 +01:00
Andreas Kling
5968ff90af LibCore: Add AnonymousBuffer::bytes() getter 2026-02-01 22:46:09 +01:00
Andreas Kling
9ad9c65368 Revert "LibCore: Add thread-safe weak deferred_invoke()"
This reverts commit 96ce468b60.

Appears to have regressed WPT.
2026-01-25 12:20:39 +01:00
Andreas Kling
96ce468b60 LibCore: Add thread-safe weak deferred_invoke()
Add a thread-safe deferred_invoke() API on WeakEventLoopReference that
queues work onto the owning thread's event queue and wakes that thread
via EventLoopManager hooks. This avoids calling wake() from foreign
threads during teardown.

Implement current_thread_handle()/wake_thread() in each backend and
track per-thread data so handles are validated before waking:

- Unix: wake via per-thread wake pipe
- Windows: wake via thread wake event
- macOS: wake via stored CFRunLoopRef
- Qt: wake via event target or QEventLoop::wakeUp()
- Android: wake via stored ALooper
2026-01-25 09:32:51 +01:00
Andreas Kling
2734f72835 LibCore: Fix data race in WeakEventLoopReference::revoke()
This was taking a read lock while modifying m_event_loop, which is a
data race with concurrent calls to take() that also hold read locks.
2026-01-25 09:32:51 +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
Zaggy1024
4a5350a2ee LibCore: Use sendfile instead of mmap+write on macOS 2026-01-19 06:53:29 -05:00
Zaggy1024
84c0eb3dbf LibCore+LibHTTP+RequestServer: Send data via sockets instead of pipes
This brings the implementation on Unix in line with Windows, so we can
drop a few ifdefs.
2026-01-19 06:53:29 -05:00
Zaggy1024
a3f0b513b6 LibCore: Remove an unnecessary ifdef from ioctl
Passing argp to ioctl as void* instead of FlatPtr works on Linux, but
Haiku needs a void*, so let's just do that unconditionally.
2026-01-19 06:53:29 -05:00
Zaggy1024
8eda26c5cf LibCore+Everywhere: Make Windows's System::ioctl consistent with POSIX
Passing the option by value on Windows where it's a pointer on all
other platforms seems like it may cause some unnecessary ifdef soup.
2026-01-19 06:53:29 -05:00
Undefine
822d8463c2 LibCore: Use the Linux implementation of TimeZoneWatcher on BSDs
The same implementation works just fine on any BSD system provided it
has a working FileWatcher implementation.
2026-01-16 10:59:50 -07:00
Undefine
941797c807 LibCore: Implement FileWatcher for any systems with inotify
FreeBSD 15 added support for inotify so let's just allow this file to
be used there too as it's needed for watching the time zone file.
2026-01-16 10:59:50 -07:00
Jelle Raaijmakers
771a9c9266 LibCore: Simplify anon_create()
After 07630f3b0c, this simplification
became obvious. No functional changes.
2026-01-16 07:26:46 -05:00
Andreas Kling
07630f3b0c LibCore: Fix memory leak in anon_create() on BSD/macOS
The mmap() call here served no purpose - anon_create() is only meant to
create and return a file descriptor. The actual mapping is done later
by AnonymousBufferImpl::create().

This leaked an mmap of `size` bytes for every AnonymousBuffer created,
which includes backing stores, shareable bitmaps, and more.
2026-01-16 08:04:22 +01:00
Andreas Kling
0e6f2cb734 LibCore: Add pipe2() implementation for Windows
This is needed by LibWebView's process output capture feature.
2026-01-13 21:05:58 +01:00
Andreas Kling
568758e55a LibCore: Fix Windows build for STDOUT_FILENO/STDERR_FILENO macros
Wrap the _get_osfhandle() return value with to_fd() to convert from
intptr_t to int, fixing a narrowing conversion error on Windows.
2026-01-13 21:05:58 +01:00
Tim Ledbetter
2a58b5b608 Revert "LibCore: Use weak ownership in EventReceiver::deferred_invoke"
This reverts commit 1ed94388e9.
2026-01-11 13:54:53 -05:00