Truly independent web browser
LibGfx+LibWebView: Refuse backing stores that can’t be imported
sideshowbarkerd29ab576d998
diff --git a/Libraries/LibGfx/SharedImageBuffer.cpp b/Libraries/LibGfx/SharedImageBuffer.cppindex 70a57e11068..fad7b6d1ff2 100644--- a/Libraries/LibGfx/SharedImageBuffer.cpp+++ b/Libraries/LibGfx/SharedImageBuffer.cpp@@ -91,10 +91,20 @@ ErrorOr<SharedImageBuffer> SharedImageBuffer::try_create(IntSize size)#endif}-SharedImageBuffer SharedImageBuffer::import_from_shared_image(SharedImage shared_image)+ErrorOr<void> SharedImageBuffer::check_can_import(SharedImage const& shared_image){#ifdef AK_OS_MACOS- auto iosurface_handle = Core::IOSurfaceHandle::from_mach_port(shared_image.m_port).release_value_but_fixme_should_propagate_errors();+ TRY(Core::IOSurfaceHandle::from_mach_port(shared_image.m_port));+#else+ (void)shared_image;+#endif+ return {};+}++ErrorOr<SharedImageBuffer> SharedImageBuffer::import_from_shared_image(SharedImage shared_image)+{+#ifdef AK_OS_MACOS+ auto iosurface_handle = TRY(Core::IOSurfaceHandle::from_mach_port(shared_image.m_port));auto bitmap = create_bitmap_from_iosurface(iosurface_handle);return SharedImageBuffer(move(iosurface_handle), move(bitmap));#elsediff --git a/Libraries/LibGfx/SharedImageBuffer.h b/Libraries/LibGfx/SharedImageBuffer.hindex 835de8b95e0..b6415e0407a 100644--- a/Libraries/LibGfx/SharedImageBuffer.h+++ b/Libraries/LibGfx/SharedImageBuffer.h@@ -26,7 +26,9 @@ public:static SharedImageBuffer create(IntSize);static ErrorOr<SharedImageBuffer> try_create(IntSize);- static SharedImageBuffer import_from_shared_image(SharedImage);+ static ErrorOr<SharedImageBuffer> import_from_shared_image(SharedImage);+ // Fails where import_from_shared_image() would, without importing anything.+ static ErrorOr<void> check_can_import(SharedImage const&);SharedImageBuffer(SharedImageBuffer&&);SharedImageBuffer& operator=(SharedImageBuffer&&);diff --git a/Libraries/LibWebView/PageBackingStores.cpp b/Libraries/LibWebView/PageBackingStores.cppindex 1195ab14fb8..353939c4872 100644--- a/Libraries/LibWebView/PageBackingStores.cpp+++ b/Libraries/LibWebView/PageBackingStores.cpp@@ -36,6 +36,7 @@ Vector<Gfx::SharedImageBuffer const*> PageBackingStores::shared_image_buffers()void PageBackingStores::track_installed_backing_stores(ReadonlySpan<i32> bitmap_ids){VERIFY(!bitmap_ids.is_empty());+ m_refuses_backing_stores = false;m_frame_shown_in_remote_layer = {};// NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.@@ -54,26 +55,72 @@ void PageBackingStores::track_added_backing_stores(ReadonlySpan<i32> bitmap_ids)m_other_bitmaps.append({ .id = bitmap_id, .last_painted_size = {}, .shared_image_buffer = nullptr });}-void PageBackingStores::install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+static ErrorOr<Vector<NonnullOwnPtr<Gfx::SharedImageBuffer>>> import_backing_stores(Vector<Gfx::SharedImage> backing_stores)+{+ Vector<NonnullOwnPtr<Gfx::SharedImageBuffer>> shared_image_buffers;+ TRY(shared_image_buffers.try_ensure_capacity(backing_stores.size()));+ for (auto& backing_store : backing_stores)+ shared_image_buffers.unchecked_append(make<Gfx::SharedImageBuffer>(TRY(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_store)))));+ return shared_image_buffers;+}++static Error refused_backing_stores_error()+{+ return Error::from_string_literal("The backing stores these belong to were refused");+}++ErrorOr<void> PageBackingStores::check_installed_backing_stores(ReadonlySpan<Gfx::SharedImage> backing_stores)+{+ for (auto const& backing_store : backing_stores) {+ if (auto result = Gfx::SharedImageBuffer::check_can_import(backing_store); result.is_error()) {+ m_refuses_backing_stores = true;+ return result.release_error();+ }+ }+ return {};+}++ErrorOr<void> PageBackingStores::check_added_backing_stores(ReadonlySpan<Gfx::SharedImage> backing_stores) const+{+ if (m_refuses_backing_stores)+ return refused_backing_stores_error();+ for (auto const& backing_store : backing_stores)+ TRY(Gfx::SharedImageBuffer::check_can_import(backing_store));+ return {};+}++ErrorOr<void> PageBackingStores::install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){VERIFY(bitmap_ids.size() == backing_stores.size());+ auto shared_image_buffers = import_backing_stores(move(backing_stores));+ if (shared_image_buffers.is_error()) {+ m_refuses_backing_stores = true;+ return shared_image_buffers.release_error();+ }track_installed_backing_stores(bitmap_ids);- m_front_bitmap.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[0])));- for (size_t i = 1; i < backing_stores.size(); ++i)- m_other_bitmaps[i - 1].shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i])));+ m_front_bitmap.shared_image_buffer = move(shared_image_buffers.value()[0]);+ for (size_t i = 1; i < shared_image_buffers.value().size(); ++i)+ m_other_bitmaps[i - 1].shared_image_buffer = move(shared_image_buffers.value()[i]);+ return {};}-void PageBackingStores::add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+ErrorOr<void> PageBackingStores::add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){VERIFY(bitmap_ids.size() == backing_stores.size());+ if (m_refuses_backing_stores)+ return refused_backing_stores_error();+ auto shared_image_buffers = TRY(import_backing_stores(move(backing_stores)));auto first_new_bitmap = m_other_bitmaps.size();track_added_backing_stores(bitmap_ids);- for (size_t i = 0; i < backing_stores.size(); ++i)- m_other_bitmaps[first_new_bitmap + i].shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i])));+ for (size_t i = 0; i < shared_image_buffers.size(); ++i)+ m_other_bitmaps[first_new_bitmap + i].shared_image_buffer = move(shared_image_buffers[i]);+ return {};}void PageBackingStores::retire(ReadonlySpan<i32> bitmap_ids){+ if (m_refuses_backing_stores)+ return;m_other_bitmaps.remove_all_matching([&](auto const& bitmap) {return bitmap_ids.contains_slow(bitmap.id);});@@ -81,6 +128,8 @@ void PageBackingStores::retire(ReadonlySpan<i32> bitmap_ids)PageBackingStores::PresentationResult PageBackingStores::present(i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio){+ if (m_refuses_backing_stores)+ return { .did_swap = false, .bitmap_id_to_release = bitmap_id };auto bitmap_index = m_other_bitmaps.find_first_index_if([bitmap_id](auto const& bitmap) { return bitmap_id == bitmap.id; });if (!bitmap_index.has_value())return { .did_swap = false, .bitmap_id_to_release = bitmap_id };diff --git a/Libraries/LibWebView/PageBackingStores.h b/Libraries/LibWebView/PageBackingStores.hindex e1521c07311..2f8047f630a 100644--- a/Libraries/LibWebView/PageBackingStores.h+++ b/Libraries/LibWebView/PageBackingStores.h@@ -43,8 +43,11 @@ public:Vector<Gfx::SharedImageBuffer const*> shared_image_buffers() const;// Replaces every backing store. The first of the new ones becomes the front backing store.- virtual void install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);- virtual void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);+ // When one of the images can't be imported, this fails without changing any backing store, and the backing stores+ // stay as they were until an installation succeeds: Additions and retirements are refused, and every presentation is+ // handed straight back, since they're all for the backing stores that were refused.+ virtual ErrorOr<void> install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);+ virtual ErrorOr<void> add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);virtual void retire(ReadonlySpan<i32> bitmap_ids);struct PresentationResult {@@ -70,6 +73,12 @@ protected:void track_installed_backing_stores(ReadonlySpan<i32> bitmap_ids);void track_added_backing_stores(ReadonlySpan<i32> bitmap_ids);+ // Fails when one of these images can't be imported, and refuses the installation they're for, as install() does.+ ErrorOr<void> check_installed_backing_stores(ReadonlySpan<Gfx::SharedImage>);+ // Fails when one of these images can't be imported, or while an installation stays refused, as add() does.+ ErrorOr<void> check_added_backing_stores(ReadonlySpan<Gfx::SharedImage>) const;+ bool refuses_backing_stores() const { return m_refuses_backing_stores; }+private:struct SharedBitmap {i32 id { -1 };@@ -83,6 +92,7 @@ private:bool m_has_usable_front_bitmap { false };SharedBitmap m_backup;bool m_has_backup { false };+ bool m_refuses_backing_stores { false };struct FrameShownInRemoteLayer {Gfx::IntSize size;diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 4972d05eea7..76747eb3417 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -3115,7 +3115,9 @@ void ViewImplementation::did_allocate_backing_stores(Badge<WebContentPage>, Vectvoid ViewImplementation::did_add_backing_stores(Badge<WebContentPage>, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){- m_backing_stores->add(move(bitmap_ids), move(backing_stores));+ // The compositor's presentations from backing stores that were refused are handed straight back.+ if (auto result = m_backing_stores->add(move(bitmap_ids), move(backing_stores)); result.is_error())+ warnln("Could not add backing stores for page {}: {}", page().id(), result.error());}void ViewImplementation::did_retire_backing_stores(Badge<WebContentPage>, ReadonlySpan<i32> bitmap_ids)@@ -3127,7 +3129,13 @@ void ViewImplementation::install_backing_stores(Vector<i32> bitmap_ids, Vector<G{dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI installing {} backing stores for page {} had_usable_bitmap={}",backing_stores.size(), page().id(), m_backing_stores->has_usable_front_bitmap());- m_backing_stores->install(move(bitmap_ids), move(backing_stores));+ // A publication that can't be imported is refused, and its front backing store goes straight back, since nothing+ // here will ever present from it.+ auto front_bitmap_id = bitmap_ids.first();+ if (auto result = m_backing_stores->install(move(bitmap_ids), move(backing_stores)); result.is_error()) {+ warnln("Could not install backing stores for page {}: {}", page().id(), result.error());+ page().release_presented_bitmap(front_bitmap_id);+ }}void ViewImplementation::update_zoom()diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cppindex 26a4a660469..8141e74a814 100644--- a/Services/Hub/View.cpp+++ b/Services/Hub/View.cpp@@ -23,23 +23,31 @@ public:}private:- virtual void install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override+ // The UI imports these images, so they're checked here first: A publication the UI would refuse is refused here as+ // well, and never sent, so both come to the same result.+ virtual ErrorOr<void> install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override{+ TRY(check_installed_backing_stores(backing_stores));track_installed_backing_stores(bitmap_ids);if (auto* ui = ConnectionFromUI::the())ui->async_view_install_backing_stores(m_view.view_id(), m_view.page().id().value(), move(bitmap_ids), move(backing_stores));m_view.did_install_backing_stores_in_ui();+ return {};}- virtual void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override+ virtual ErrorOr<void> add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override{+ TRY(check_added_backing_stores(backing_stores));track_added_backing_stores(bitmap_ids);if (auto* ui = ConnectionFromUI::the())ui->async_view_add_backing_stores(m_view.view_id(), move(bitmap_ids), move(backing_stores));+ return {};}virtual void retire(ReadonlySpan<i32> bitmap_ids) override{+ if (refuses_backing_stores())+ return;PageBackingStores::retire(bitmap_ids);if (auto* ui = ConnectionFromUI::the()) {Vector<i32> retired_bitmap_ids;@@ -48,8 +56,14 @@ private:}}+ // The UI hands back the backing stores it presents from, other than those of a refused installation, which it never+ // got: Those go straight back from here.virtual PresentationResult present(i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio) override{+ if (refuses_backing_stores()) {+ m_view.page().release_presented_bitmap(bitmap_id);+ return { .did_swap = false, .bitmap_id_to_release = bitmap_id };+ }auto result = PageBackingStores::present(bitmap_id, size, device_pixel_ratio);if (auto* ui = ConnectionFromUI::the())ui->async_view_present_backing_store(m_view.view_id(), m_view.page().id().value(), bitmap_id, size, device_pixel_ratio);diff --git a/Tests/LibGfx/CMakeLists.txt b/Tests/LibGfx/CMakeLists.txtindex 52507d084ed..92b4f22d56b 100644--- a/Tests/LibGfx/CMakeLists.txt+++ b/Tests/LibGfx/CMakeLists.txt@@ -14,6 +14,11 @@ set(TEST_SOURCESlist(APPEND TEST_SOURCES TestBitmapSequence.cpp TestShareableBitmap.cpp)+# Imports images through IOSurfaces, which only macOS has.+if (APPLE)+ list(APPEND TEST_SOURCES TestSharedImageBuffer.cpp)+endif()+# Opens the LibGfx and LibWeb shared libraries by name to reach their copies of the Rust graphics crate.if (BUILD_SHARED_LIBS AND (APPLE OR LINUX))list(APPEND TEST_SOURCES TestRustCrateCopies.cpp)diff --git a/Tests/LibGfx/TestSharedImageBuffer.cpp b/Tests/LibGfx/TestSharedImageBuffer.cppnew file mode 100644index 00000000000..cba4e5a68c0--- /dev/null+++ b/Tests/LibGfx/TestSharedImageBuffer.cpp@@ -0,0 +1,24 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/MachPort.h>+#include <LibGfx/SharedImage.h>+#include <LibGfx/SharedImageBuffer.h>+#include <LibTest/TestCase.h>++TEST_CASE(a_shared_image_buffer_imports_its_own_exported_image)+{+ auto buffer = Gfx::SharedImageBuffer::create({ 4, 4 });+ auto imported = TRY_OR_FAIL(Gfx::SharedImageBuffer::import_from_shared_image(buffer.export_shared_image()));+ EXPECT_EQ(imported.bitmap()->size(), Gfx::IntSize(4, 4));+}++TEST_CASE(a_port_that_is_not_an_iosurface_fails_to_import)+{+ auto receive_right = TRY_OR_FAIL(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ auto send_right = TRY_OR_FAIL(receive_right.insert_right(Core::MachPort::MessageRight::MakeSend));+ EXPECT(Gfx::SharedImageBuffer::import_from_shared_image(Gfx::SharedImage { move(send_right) }).is_error());+}diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex cdd09a85304..874f7952e87 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -101,7 +101,7 @@ if (APPLE OR LINUX)endif()if (APPLE)- list(APPEND TEST_SOURCES TestProcessReaper.cpp)+ list(APPEND TEST_SOURCES TestPageBackingStores.cpp TestProcessReaper.cpp)endif()if (LINUX)diff --git a/Tests/LibWebView/TestPageBackingStores.cpp b/Tests/LibWebView/TestPageBackingStores.cppnew file mode 100644index 00000000000..bc148dfcd1e--- /dev/null+++ b/Tests/LibWebView/TestPageBackingStores.cpp@@ -0,0 +1,78 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/MachPort.h>+#include <LibGfx/SharedImage.h>+#include <LibGfx/SharedImageBuffer.h>+#include <LibTest/TestCase.h>+#include <LibWebView/PageBackingStores.h>++static Vector<Gfx::SharedImageBuffer> s_buffers;++static Gfx::SharedImage importable_image()+{+ s_buffers.append(Gfx::SharedImageBuffer::create({ 4, 4 }));+ return s_buffers.last().export_shared_image();+}++// A send right to a port that isn't an IOSurface's.+static Gfx::SharedImage unimportable_image()+{+ auto receive_right = MUST(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ return Gfx::SharedImage { MUST(receive_right.insert_right(Core::MachPort::MessageRight::MakeSend)) };+}++static Vector<Gfx::SharedImage> images(Vector<bool> importable)+{+ Vector<Gfx::SharedImage> images;+ for (auto is_importable : importable)+ images.append(is_importable ? importable_image() : unimportable_image());+ return images;+}++TEST_CASE(a_refused_installation_keeps_the_backing_stores_on_display)+{+ WebView::PageBackingStores backing_stores;+ TRY_OR_FAIL(backing_stores.install({ 1, 2 }, images({ true, true })));+ EXPECT_EQ(backing_stores.present(2, { 4, 4 }, 1).bitmap_id_to_release, 1);++ EXPECT(backing_stores.install({ 10, 11 }, images({ true, false })).is_error());+ EXPECT_EQ(backing_stores.front_bitmap_id(), 2);+ EXPECT(backing_stores.has_usable_front_bitmap());+ EXPECT_EQ(backing_stores.displayed_frame()->bitmap_id, 2);+}++TEST_CASE(the_backing_stores_of_a_refused_installation_are_handed_back)+{+ WebView::PageBackingStores backing_stores;+ TRY_OR_FAIL(backing_stores.install({ 1, 2 }, images({ true, true })));+ EXPECT(backing_stores.install({ 1, 2 }, images({ false, true })).is_error());++ // Each of these is for the refused installation, even where its ID is one of the installed backing stores'.+ EXPECT(backing_stores.add({ 3 }, images({ true })).is_error());+ auto result = backing_stores.present(2, { 4, 4 }, 1);+ EXPECT(!result.did_swap);+ EXPECT_EQ(result.bitmap_id_to_release, 2);+ backing_stores.retire(Vector<i32> { 2 });++ // An installation that succeeds ends the refusal.+ TRY_OR_FAIL(backing_stores.install({ 20, 21 }, images({ true, true })));+ TRY_OR_FAIL(backing_stores.add({ 22 }, images({ true })));+ result = backing_stores.present(22, { 4, 4 }, 1);+ EXPECT(result.did_swap);+ EXPECT_EQ(result.bitmap_id_to_release, 20);+}++TEST_CASE(a_refused_addition_changes_nothing)+{+ WebView::PageBackingStores backing_stores;+ TRY_OR_FAIL(backing_stores.install({ 1, 2 }, images({ true, true })));+ EXPECT(backing_stores.add({ 3, 4 }, images({ true, false })).is_error());+ EXPECT_EQ(backing_stores.present(3, { 4, 4 }, 1).bitmap_id_to_release, 3);++ // A refused addition doesn't refuse the installation it adds to.+ EXPECT(backing_stores.present(2, { 4, 4 }, 1).did_swap);+}diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cppindex f5e1adac60f..755a557b847 100644--- a/UI/Qt/RemoteView.cpp+++ b/UI/Qt/RemoteView.cpp@@ -158,8 +158,15 @@ void RemoteView::did_finish_navigation_load(URL::URL const& url)void RemoteView::did_install_backing_stores(u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){+ // A publication that can't be imported is refused, so the backing stores, and the page they're from, stay as they+ // were. Its front backing store goes straight back, since nothing here will ever present from it.+ auto front_bitmap_id = bitmap_ids.first();+ if (auto result = m_backing_stores.install(move(bitmap_ids), move(backing_stores)); result.is_error()) {+ warnln("Could not install backing stores for page {}: {}", page_id, result.error());+ release_backing_store(page_id, front_bitmap_id);+ return;+ }m_backing_stores_page_id = page_id;- m_backing_stores.install(move(bitmap_ids), move(backing_stores));}void RemoteView::did_present_backing_store(u64 page_id, i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio)diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex 30e23fdea1e..0baef7c6a89 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -240,7 +240,11 @@ public:bool scales_frames_to_fit() const { return m_scales_frames_to_fit; }void did_install_backing_stores(u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);- void did_add_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) { m_backing_stores.add(move(bitmap_ids), move(backing_stores)); }+ void did_add_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+ {+ if (auto result = m_backing_stores.add(move(bitmap_ids), move(backing_stores)); result.is_error())+ warnln("Could not add backing stores: {}", result.error());+ }void did_retire_backing_stores(ReadonlySpan<i32> bitmap_ids) { m_backing_stores.retire(bitmap_ids); }void did_present_backing_store(u64 page_id, i32 bitmap_id, Gfx::IntSize, double device_pixel_ratio);void did_accept_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect);