Skip to content

Truly independent web browser

LibWebView: Ignore a worker's release of a decoder its process left

Andreas Klingd242297354e6
diff --git a/Libraries/LibWebView/ImageDecoderManager.cpp b/Libraries/LibWebView/ImageDecoderManager.cpp
index 89557386b67..5d0da6764cf 100644
--- a/Libraries/LibWebView/ImageDecoderManager.cpp
+++ b/Libraries/LibWebView/ImageDecoderManager.cpp
@@ -26,7 +26,12 @@ ErrorOr<IPC::TransportHandle> ImageDecoderManager::connect_new_client(u64 client
site.clear();
// NB: A decoder that died is of no use to anyone, whether or not its clients have noticed yet.
- m_instances.remove_all_matching([](auto const& instance) { return !instance.control_client->is_open(); });
+ m_instances.remove_all_matching([&](auto const& instance) {
+ if (instance.control_client->is_open())
+ return false;
+ did_lose_decoder(instance);
+ return true;
+ });
auto instance = m_instances.first_matching([&](auto const& instance) {
if (site.has_value())
@@ -45,7 +50,7 @@ ErrorOr<IPC::TransportHandle> ImageDecoderManager::connect_new_client(u64 client
return move(channel.second);
}
-Optional<IPC::TransportHandle> ImageDecoderManager::connect_requesting_process(u64 client_child_id, RefPtr<BrowsingSession> const& session, Optional<Utf16String> site)
+Optional<IPC::TransportHandle> ImageDecoderManager::connect_requesting_process(u64 client_child_id, RefPtr<BrowsingSession> const& session, Optional<Utf16String> site, Optional<u64>& client_generation)
{
// NB: A request can still be queued when the process's exit is handled. Connecting it then would leave a client that
// never exits, which would keep its decoder alive for as long as the Browser runs.
@@ -57,9 +62,23 @@ Optional<IPC::TransportHandle> ImageDecoderManager::connect_requesting_process(u
warnln("Unable to connect an ImageDecoder client: {}", handle.error());
return {};
}
+ note_connected_client(client_child_id, client_generation);
return handle.release_value();
}
+void ImageDecoderManager::note_connected_client(u64 client_child_id, Optional<u64>& client_generation)
+{
+ auto generation_of_lost_client = exchange(client_generation, this->client_generation(client_child_id));
+ if (generation_of_lost_client.has_value())
+ process_did_release_decoder(client_child_id, *generation_of_lost_client);
+}
+
+void ImageDecoderManager::did_lose_decoder(Instance const& instance)
+{
+ for (auto const& [client_child_id, count] : instance.client_counts)
+ ++m_client_generations.ensure(client_child_id, [] { return 0uz; });
+}
+
bool ImageDecoderManager::remove_client_from_decoder_of_site(u64 client_child_id)
{
bool was_removed = false;
@@ -69,9 +88,17 @@ bool ImageDecoderManager::remove_client_from_decoder_of_site(u64 client_child_id
was_removed = true;
return instance.client_counts.is_empty();
});
+ if (was_removed)
+ ++m_client_generations.ensure(client_child_id, [] { return 0uz; });
return was_removed;
}
+void ImageDecoderManager::process_did_release_decoder(u64 client_child_id, u64 client_generation)
+{
+ if (client_generation == this->client_generation(client_child_id))
+ process_did_release_decoder(client_child_id);
+}
+
void ImageDecoderManager::process_did_release_decoder(u64 client_child_id)
{
m_instances.remove_all_matching([&](auto& instance) {
@@ -87,11 +114,15 @@ void ImageDecoderManager::process_did_release_decoder(u64 client_child_id)
void ImageDecoderManager::process_did_exit(u64 child_id)
{
+ m_client_generations.remove(child_id);
+
// NB: Letting go of a decoder that exited also closes the channels of any clients that were sent to it, which it can no
// longer take up, so that those clients learn they have no decoder.
m_instances.remove_all_matching([&](auto& instance) {
- if (instance.control_client->child_id() == child_id)
+ if (instance.control_client->child_id() == child_id) {
+ did_lose_decoder(instance);
return true;
+ }
return instance.client_counts.remove(child_id) && instance.client_counts.is_empty();
});
}
diff --git a/Libraries/LibWebView/ImageDecoderManager.h b/Libraries/LibWebView/ImageDecoderManager.h
index 0cb095bd396..3ecb9ead25e 100644
--- a/Libraries/LibWebView/ImageDecoderManager.h
+++ b/Libraries/LibWebView/ImageDecoderManager.h
@@ -44,7 +44,8 @@ public:
ErrorOr<IPC::TransportHandle> connect_new_client(u64 client_child_id, RefPtr<BrowsingSession> const&, Optional<Utf16String> site);
// Answers a renderer or worker that asked for a decoder, which may have exited by the time its request is handled.
- Optional<IPC::TransportHandle> connect_requesting_process(u64 client_child_id, RefPtr<BrowsingSession> const&, Optional<Utf16String> site);
+ // Each of them has one client at a time, and keeps its generation in `client_generation`; see below.
+ Optional<IPC::TransportHandle> connect_requesting_process(u64 client_child_id, RefPtr<BrowsingSession> const&, Optional<Utf16String> site, Optional<u64>& client_generation);
// Returns whether the process was served by the decoder of a site, and so has to disconnect from it.
bool remove_client_from_decoder_of_site(u64 client_child_id);
@@ -52,6 +53,14 @@ public:
// Lets go of one client that a process connected to its decoder, which ends once no process has a client left.
void process_did_release_decoder(u64 client_child_id);
+ // A client can be let go of long after its process left the decoder of that client, or lost it. So a renderer or
+ // worker notes the generation of its process as it connects, and a release from an older generation is about a
+ // decoder the process has left. One that connects again has lost its last client without saying so, which counts
+ // as a release.
+ u64 client_generation(u64 client_child_id) const { return m_client_generations.get(client_child_id).value_or(0); }
+ void note_connected_client(u64 client_child_id, Optional<u64>& client_generation);
+ void process_did_release_decoder(u64 client_child_id, u64 client_generation);
+
void process_did_exit(u64 child_id);
private:
@@ -63,7 +72,10 @@ private:
HashMap<u64, size_t> client_counts;
};
+ void did_lose_decoder(Instance const&);
+
Vector<Instance> m_instances;
+ HashMap<u64, u64> m_client_generations;
};
}
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 2005e7e23f8..f2ea6066717 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -1205,19 +1205,20 @@ void WebContentClient::did_host_document_of_site(Utf16String site)
m_site_of_hosted_documents = move(site);
}
-Optional<IPC::TransportHandle> WebContentClient::connect_new_image_decoder_client()
+Optional<IPC::TransportHandle> WebContentClient::connect_new_image_decoder_client(Optional<u64>& client_generation)
{
- return ImageDecoderManager::the().connect_requesting_process(child_id(), m_session, m_site_of_hosted_documents);
+ return ImageDecoderManager::the().connect_requesting_process(child_id(), m_session, m_site_of_hosted_documents, client_generation);
}
Messages::WebContentClient::RequestImageDecoderConnectionResponse WebContentClient::request_image_decoder_connection()
{
- return connect_new_image_decoder_client();
+ return connect_new_image_decoder_client(m_image_decoder_client_generation);
}
void WebContentClient::did_release_image_decoder()
{
- ImageDecoderManager::the().process_did_release_decoder(child_id());
+ if (auto generation = exchange(m_image_decoder_client_generation, {}); generation.has_value())
+ ImageDecoderManager::the().process_did_release_decoder(child_id(), *generation);
}
Optional<u64> WebContentClient::exclusive_performance_owner() const
diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.h
index a0c9056a18b..544fb21e35c 100644
--- a/Libraries/LibWebView/WebContentClient.h
+++ b/Libraries/LibWebView/WebContentClient.h
@@ -128,7 +128,7 @@ public:
void did_host_document_of_site(Utf16String site);
// Connects a client to the ImageDecoder of this process, for its main thread or for a worker on a thread of it.
- Optional<IPC::TransportHandle> connect_new_image_decoder_client();
+ Optional<IPC::TransportHandle> connect_new_image_decoder_client(Optional<u64>& client_generation);
// A process that has hosted local file content may read local files. Site isolation gives such content processes
// of its own, which host nothing else.
@@ -255,6 +255,7 @@ private:
bool m_rejected_ipc { false };
// The site of the documents this process hosts, if it has only hosted those of one.
Optional<Utf16String> m_site_of_hosted_documents;
+ Optional<u64> m_image_decoder_client_generation;
bool m_has_hosted_documents_of_several_sites { false };
bool m_has_hosted_local_file_content { false };
Optional<String> m_hosted_web_ui;
diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cpp
index bcf096bf9eb..57eac8b6c17 100644
--- a/Libraries/LibWebView/WebWorkerClient.cpp
+++ b/Libraries/LibWebView/WebWorkerClient.cpp
@@ -61,8 +61,8 @@ void WebWorkerClient::die()
WorkerProcessManager::the().worker_did_die(m_agent_id);
// NB: The process of a worker on a thread goes on, and so may its use of the decoder.
- if (m_holds_image_decoder_client)
- ImageDecoderManager::the().process_did_release_decoder(child_id());
+ if (m_image_decoder_client_generation.has_value())
+ ImageDecoderManager::the().process_did_release_decoder(child_id(), *m_image_decoder_client_generation);
WorkerProcessManager::the().worker_thread_did_exit(*this);
@@ -206,15 +206,13 @@ Messages::WebWorkerClient::RequestImageDecoderConnectionResponse WebWorkerClient
auto host = m_host.strong_ref();
if (!host)
return Optional<IPC::TransportHandle> {};
- auto handle = host->connect_new_image_decoder_client();
- m_holds_image_decoder_client |= handle.has_value();
- return handle;
+ return host->connect_new_image_decoder_client(m_image_decoder_client_generation);
}
void WebWorkerClient::did_release_image_decoder()
{
- m_holds_image_decoder_client = false;
- ImageDecoderManager::the().process_did_release_decoder(child_id());
+ if (auto generation = exchange(m_image_decoder_client_generation, {}); generation.has_value())
+ ImageDecoderManager::the().process_did_release_decoder(child_id(), *generation);
}
Messages::WebWorkerClient::RequestCompositorConnectionResponse WebWorkerClient::request_compositor_connection()
diff --git a/Libraries/LibWebView/WebWorkerClient.h b/Libraries/LibWebView/WebWorkerClient.h
index 51c24c49f0d..0c71c627282 100644
--- a/Libraries/LibWebView/WebWorkerClient.h
+++ b/Libraries/LibWebView/WebWorkerClient.h
@@ -84,7 +84,7 @@ private:
u64 m_host_child_id { 0 };
WeakPtr<WebContentClient> m_host;
- bool m_holds_image_decoder_client { false };
+ Optional<u64> m_image_decoder_client_generation;
Web::HTML::WorkerAgentId m_agent_id { 0 };
Vector<URL::Origin> m_permission_origins;
diff --git a/Tests/LibWebView/TestImageDecoderIsolation.cpp b/Tests/LibWebView/TestImageDecoderIsolation.cpp
index 568067d5b72..c63cdb98767 100644
--- a/Tests/LibWebView/TestImageDecoderIsolation.cpp
+++ b/Tests/LibWebView/TestImageDecoderIsolation.cpp
@@ -344,6 +344,48 @@ TEST_CASE(image_decoders_end_once_released)
manager.process_did_release_decoder(2'000'005);
Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+ // A worker that still holds a client when its process leaves the decoder has no say over the next one.
+ Optional<IPC::TransportHandle> client_of_old_decoder = MUST(manager.connect_new_client(2'000'006, session, site));
+ auto generation_of_old_decoder = manager.client_generation(2'000'006);
+ VERIFY(manager.remove_client_from_decoder_of_site(2'000'006));
+ client_of_old_decoder.clear();
+ Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+ Optional<IPC::TransportHandle> client_of_new_decoder = MUST(manager.connect_new_client(2'000'006, session, site));
+ manager.process_did_release_decoder(2'000'006, generation_of_old_decoder);
+ VERIFY(decoder_count() == 1);
+ client_of_new_decoder.clear();
+ manager.process_did_release_decoder(2'000'006, manager.client_generation(2'000'006));
+ Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+
+ // A client that was lost without a release counts as one, once the same renderer or worker connects again.
+ Optional<u64> client_generation;
+ Optional<IPC::TransportHandle> lost_client = MUST(manager.connect_new_client(2'000'007, session, site));
+ manager.note_connected_client(2'000'007, client_generation);
+ Optional<IPC::TransportHandle> next_client = MUST(manager.connect_new_client(2'000'007, session, site));
+ manager.note_connected_client(2'000'007, client_generation);
+ VERIFY(decoder_count() == 1);
+ lost_client.clear();
+ next_client.clear();
+ manager.process_did_release_decoder(2'000'007, *client_generation);
+ Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+
+ // A client that went with its decoder does not count against the decoder that comes next.
+ client_generation.clear();
+ Optional<IPC::TransportHandle> client_of_dead_decoder = MUST(manager.connect_new_client(2'000'008, session, site));
+ manager.note_connected_client(2'000'008, client_generation);
+ WebView::Application::process_manager().for_each_process([&](WebView::Process& process) {
+ if (process.type() == WebView::ProcessType::ImageDecoder)
+ manager.process_did_exit(process.child_id());
+ });
+ client_of_dead_decoder.clear();
+ Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+ Optional<IPC::TransportHandle> client_of_next_decoder = MUST(manager.connect_new_client(2'000'008, session, site));
+ manager.note_connected_client(2'000'008, client_generation);
+ VERIFY(decoder_count() == 1);
+ client_of_next_decoder.clear();
+ manager.process_did_release_decoder(2'000'008, *client_generation);
+ Core::EventLoop::current().spin_until([&] { return decoder_count() == 0; });
+
// A process that has let go of its decoder gets a new one when it next asks.
auto handle = MUST(manager.connect_new_client(2'000'003, session, {}));
VERIFY(decoder_count() == 1);