Skip to content

Truly independent web browser

LibWeb: Keep agent registries and task IDs per agent

Andreas Klingb8ac4ec0fb5e
diff --git a/Libraries/LibWeb/HTML/EventLoop/Task.cpp b/Libraries/LibWeb/HTML/EventLoop/Task.cpp
index ce55c064c4c..f1480cf03e5 100644
--- a/Libraries/LibWeb/HTML/EventLoop/Task.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/Task.cpp
@@ -4,8 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Diagnostics.h>
#include <AK/IDAllocator.h>
-#include <AK/NeverDestroyed.h>
#include <LibGC/Heap.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/EventLoop/Task.h>
@@ -17,13 +17,18 @@ GC_DEFINE_ALLOCATOR(Task);
static IDAllocator& unique_task_source_allocator()
{
- static NeverDestroyed<IDAllocator> allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) };
- return *allocator;
+ // NB: Free what this thread kept when it exits.
+#ifdef AK_COMPILER_CLANG
+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local IDAllocator allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) })
+#else
+ static thread_local IDAllocator allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) };
+#endif
+ return allocator;
}
[[nodiscard]] static TaskID allocate_task_id()
{
- static u64 next_task_id = 1;
+ static thread_local u64 next_task_id = 1;
return next_task_id++;
}
diff --git a/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp
index dc7c201bef3..7648990463c 100644
--- a/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/SharedWorkerGlobalScope.cpp
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/NeverDestroyed.h>
+#include <AK/Diagnostics.h>
#include <LibWeb/HTML/SharedWorkerGlobalScope.h>
#include <LibWeb/Page/Page.h>
@@ -14,8 +14,13 @@ GC_DEFINE_ALLOCATOR(SharedWorkerGlobalScope);
HashTable<GC::RawRef<SharedWorkerGlobalScope>>& all_shared_worker_global_scopes()
{
- static NeverDestroyed<HashTable<GC::RawRef<SharedWorkerGlobalScope>>> set;
- return *set;
+ // NB: Free what this thread kept when it exits.
+#ifdef AK_COMPILER_CLANG
+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local HashTable<GC::RawRef<SharedWorkerGlobalScope>> set)
+#else
+ static thread_local HashTable<GC::RawRef<SharedWorkerGlobalScope>> set;
+#endif
+ return set;
}
SharedWorkerGlobalScope::SharedWorkerGlobalScope(GC::Ref<Web::Page> page)
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
index a590ce664f5..213bee9f59d 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
@@ -4,8 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Atomic.h>
+#include <AK/Diagnostics.h>
#include <AK/HashMap.h>
-#include <AK/NeverDestroyed.h>
#include <LibGC/Heap.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/WrapperWorld.h>
@@ -34,8 +35,14 @@ GC_DEFINE_ALLOCATOR(WorkerAgentParent);
static HashMap<WorkerAgentOwnerToken, GC::Ref<WorkerAgentParent>>& worker_agent_parents()
{
- static NeverDestroyed<HashMap<WorkerAgentOwnerToken, GC::Ref<WorkerAgentParent>>> map;
- return *map;
+ using Parents = HashMap<WorkerAgentOwnerToken, GC::Ref<WorkerAgentParent>>;
+ // NB: Free what this thread kept when it exits.
+#ifdef AK_COMPILER_CLANG
+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local Parents map)
+#else
+ static thread_local Parents map;
+#endif
+ return map;
}
static void report_worker_exception(WindowOrWorkerGlobalScopeMixin& window_or_worker, ErrorEvent& error)
@@ -244,8 +251,9 @@ void WorkerAgentParent::report_content_security_policy_violation(u64 policy_inde
WorkerAgentOwnerToken WorkerAgentParent::next_owner_token()
{
- static WorkerAgentOwnerToken s_next_owner_token = 0;
- return ++s_next_owner_token;
+ // NB: The client of the owner tells workers apart by their tokens, so these stay unique across the agents of a process.
+ static Atomic<WorkerAgentOwnerToken, AK::MemoryOrder::memory_order_relaxed> s_next_owner_token = 0;
+ return s_next_owner_token.fetch_add(1) + 1;
}
void WorkerAgentParent::finalize()
diff --git a/Libraries/LibWeb/ServiceWorker/Job.cpp b/Libraries/LibWeb/ServiceWorker/Job.cpp
index f1cd231f8d6..bb443e29943 100644
--- a/Libraries/LibWeb/ServiceWorker/Job.cpp
+++ b/Libraries/LibWeb/ServiceWorker/Job.cpp
@@ -76,10 +76,16 @@ void Job::visit_edges(JS::Cell::Visitor& visitor)
// https://w3c.github.io/ServiceWorker/#dfn-scope-to-job-queue-map
static HashMap<ByteString, NonnullOwnPtr<JobQueue>>& scope_to_job_queue_map()
{
- static NeverDestroyed<HashMap<ByteString, NonnullOwnPtr<JobQueue>>> map;
+ // NB: The jobs in here are cells of the agent that scheduled them.
+ static thread_local NeverDestroyed<HashMap<ByteString, NonnullOwnPtr<JobQueue>>> map;
return *map;
}
+void forget_job_queues_of_this_agent()
+{
+ scope_to_job_queue_map().clear();
+}
+
// https://w3c.github.io/ServiceWorker/#register-algorithm
static void register_(JS::VM& vm, GC::Ref<Job> job)
{
diff --git a/Libraries/LibWeb/ServiceWorker/Job.h b/Libraries/LibWeb/ServiceWorker/Job.h
index 3c2430a64f1..2a5de987161 100644
--- a/Libraries/LibWeb/ServiceWorker/Job.h
+++ b/Libraries/LibWeb/ServiceWorker/Job.h
@@ -77,4 +77,7 @@ private:
// https://w3c.github.io/ServiceWorker/#schedule-job-algorithm
void schedule_job(JS::VM&, GC::Ref<Job>);
+// The queues of an agent hold roots, which have to go before its heap does.
+void forget_job_queues_of_this_agent();
+
}
diff --git a/Libraries/LibWeb/ServiceWorker/Registration.cpp b/Libraries/LibWeb/ServiceWorker/Registration.cpp
index 58bd01e8d5a..a04e4bcb713 100644
--- a/Libraries/LibWeb/ServiceWorker/Registration.cpp
+++ b/Libraries/LibWeb/ServiceWorker/Registration.cpp
@@ -17,10 +17,15 @@ namespace Web::ServiceWorker {
// Does this need to be serialized to disk as well?
static OrderedHashMap<RegistrationKey, NonnullOwnPtr<Registration>>& registration_map()
{
- static NeverDestroyed<OrderedHashMap<RegistrationKey, NonnullOwnPtr<Registration>>> registrations;
+ static thread_local NeverDestroyed<OrderedHashMap<RegistrationKey, NonnullOwnPtr<Registration>>> registrations;
return *registrations;
}
+void Registration::forget_registrations_of_this_agent()
+{
+ registration_map().clear();
+}
+
Registration::Registration(StorageAPI::StorageKey storage_key, URL::URL scope, ServiceWorkerUpdateViaCache update_via_cache)
: m_storage_key(move(storage_key))
, m_scope_url(move(scope))
diff --git a/Libraries/LibWeb/ServiceWorker/Registration.h b/Libraries/LibWeb/ServiceWorker/Registration.h
index 2eb427a0ea6..73b18f41e53 100644
--- a/Libraries/LibWeb/ServiceWorker/Registration.h
+++ b/Libraries/LibWeb/ServiceWorker/Registration.h
@@ -29,6 +29,8 @@ class Registration {
AK_ALLOC_WITH_KMALLOC;
public:
+ static void forget_registrations_of_this_agent();
+
// https://w3c.github.io/ServiceWorker/#get-registration-algorithm
static Optional<Registration&> get(StorageAPI::StorageKey const&, Optional<URL::URL> scope);
diff --git a/Libraries/LibWeb/WebLocks/AbstractOperations.cpp b/Libraries/LibWeb/WebLocks/AbstractOperations.cpp
index f086aa92600..ac64dded6b8 100644
--- a/Libraries/LibWeb/WebLocks/AbstractOperations.cpp
+++ b/Libraries/LibWeb/WebLocks/AbstractOperations.cpp
@@ -4,11 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/NeverDestroyed.h>
#include <LibGC/Function.h>
#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
+#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/WebIDL/CallbackType.h>
@@ -24,8 +24,12 @@ void web_locks_queue_callback_task(WebIDL::CallbackType*, GC::Cell*, void (*step
static HTML::ParallelQueue& lock_task_queue()
{
// A user agent has a lock task queue which is the result of starting a new parallel queue.
- static NeverDestroyed<NonnullRefPtr<HTML::ParallelQueue>> queue { HTML::ParallelQueue::create() };
- return **queue;
+ // NB: Lock managers are per environment, and a parallel queue queues tasks on the agent that uses it. So this is one
+ // queue per agent.
+ auto& agent = HTML::surrounding_agent();
+ if (!agent.lock_task_queue)
+ agent.lock_task_queue = HTML::ParallelQueue::create();
+ return *agent.lock_task_queue;
}
void web_locks_queue_task(JS::Realm* realm, GC::Cell* cell, void (*steps)(JSVM*, void*))