Truly independent web browser
LibWeb: Check for transferred canvases after converting context options
Tim Ledbetter06844033704b
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex b72f34ee1ed..4a0fc1560e1 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -1221,6 +1221,7 @@ namespace Web::WebGL {class RemoteWebGLTransport;class WebGL2RenderingContext;+struct WebGLContextAttributes;class WebGLRenderingContext;class WebGLRenderingContextBase;diff --git a/Libraries/LibWeb/HTML/BindingsGlue.h b/Libraries/LibWeb/HTML/BindingsGlue.hindex cab0d164d0a..71267a50006 100644--- a/Libraries/LibWeb/HTML/BindingsGlue.h+++ b/Libraries/LibWeb/HTML/BindingsGlue.h@@ -23,6 +23,6 @@ namespace Web::Bindings {WEB_API WebIDL::ExceptionOr<GC::Ref<HTML::OffscreenCanvas>> construct_offscreen_canvas(JS::Realm&, WebIDL::UnsignedLongLong width, WebIDL::UnsignedLongLong height);WEB_API GC::Ref<WebIDL::Promise> convert_to_blob(JS::Realm&, HTML::OffscreenCanvas&, Optional<ImageEncodeOptions> const&);-WEB_API JS::ThrowCompletionOr<HTML::OffscreenRenderingContext> get_context(JS::Realm&, HTML::OffscreenCanvas&, OffscreenRenderingContextId, JS::Value options);+WEB_API JS::ThrowCompletionOr<HTML::OffscreenRenderingContext> get_context(HTML::OffscreenCanvas&, OffscreenRenderingContextId, JS::Value options);}diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp b/Libraries/LibWeb/HTML/HTMLCanvasElement.cppindex d4d1cc74b67..95780900db1 100644--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp@@ -361,6 +361,7 @@ HTMLCanvasElement::HasOrCreatedContext HTMLCanvasElement::create_2d_context(Canvif (!m_context.has<Empty>())return m_context.has<GC::Ref<CanvasRenderingContext2D>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;+ VERIFY(!m_is_placeholder);m_context = CanvasRenderingContext2D::create(*this, context_attributes);return HasOrCreatedContext::Yes;}@@ -370,21 +371,38 @@ HTMLCanvasElement::HasOrCreatedContext HTMLCanvasElement::create_bitmap_rendererif (!m_context.has<Empty>())return m_context.has<GC::Ref<ImageBitmapRenderingContext>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;+ VERIFY(!m_is_placeholder);m_context = ImageBitmapRenderingContext::create(*this, alpha);Painting::push_canvas_paint_facts(*this);return HasOrCreatedContext::Yes;}+JS::ThrowCompletionOr<void> HTMLCanvasElement::throw_if_control_transferred_to_offscreen() const+{+ if (!m_is_placeholder)+ return {};++ auto& realm = HTML::relevant_realm(*this);+ return throw_completion(realm, WebIDL::InvalidStateError::create(realm, "Canvas has transferred control to an OffscreenCanvas"_utf16));+}+template<typename ContextType>JS::ThrowCompletionOr<HTMLCanvasElement::HasOrCreatedContext> HTMLCanvasElement::create_webgl_context(JS::Value options){if (!m_context.has<Empty>())return m_context.has<GC::Ref<ContextType>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;- auto maybe_context = TRY(ContextType::create(HTML::relevant_realm(*this), WebGL::CanvasOwner { GC::Ref { *this } }, options));+ auto context_attributes = TRY(WebGL::convert_value_to_context_attributes_dictionary(vm(), options));+ TRY(throw_if_control_transferred_to_offscreen());+ // AD-HOC: Converting the options can run script that creates a context.+ if (!m_context.has<Empty>())+ return m_context.has<GC::Ref<ContextType>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;++ auto maybe_context = ContextType::create(HTML::relevant_realm(*this), WebGL::CanvasOwner { GC::Ref { *this } }, context_attributes);if (!maybe_context)return HasOrCreatedContext::No;+ VERIFY(!m_is_placeholder);m_context = GC::Ref<ContextType>(*maybe_context);return HasOrCreatedContext::Yes;}@@ -395,14 +413,13 @@ JS::ThrowCompletionOr<HTMLCanvasElement::RenderingContext> HTMLCanvasElement::geif (!options.is_object())options = JS::js_null();- if (m_is_placeholder) {- return WebIDL::throw_dom_exception_if_needed(vm(), HTML::relevant_realm(*this), [] -> WebIDL::ExceptionOr<RenderingContext> {- return WebIDL::InvalidStateError::create("Canvas has transferred control to an OffscreenCanvas"_utf16);- });- }+ TRY(throw_if_control_transferred_to_offscreen());+ // AD-HOC: Converting the options can run script that transfers control of this canvas to an OffscreenCanvas, so+ // we check for a placeholder canvas again after each conversion.if (type == u"2d"sv) {auto context_attributes = TRY(Bindings::convert_to_idl_value_for_canvas_rendering_context2d_settings(vm(), options));+ TRY(throw_if_control_transferred_to_offscreen());if (create_2d_context(context_attributes) == HasOrCreatedContext::Yes)return m_context.get<GC::Ref<HTML::CanvasRenderingContext2D>>();@@ -426,6 +443,7 @@ JS::ThrowCompletionOr<HTMLCanvasElement::RenderingContext> HTMLCanvasElement::geif (type == u"bitmaprenderer"sv) {auto settings = TRY(Bindings::convert_to_idl_value_for_image_bitmap_rendering_context_settings(vm(), options));+ TRY(throw_if_control_transferred_to_offscreen());if (create_bitmap_renderer_context(settings.alpha) == HasOrCreatedContext::Yes)return m_context.get<GC::Ref<ImageBitmapRenderingContext>>();diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.h b/Libraries/LibWeb/HTML/HTMLCanvasElement.hindex 71a3205bfa4..1385dfad55e 100644--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.h+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.h@@ -107,6 +107,7 @@ private:template<typename ContextType>JS::ThrowCompletionOr<HasOrCreatedContext> create_webgl_context(JS::Value options);HasOrCreatedContext create_bitmap_renderer_context(bool alpha);+ JS::ThrowCompletionOr<void> throw_if_control_transferred_to_offscreen() const;void reset_context_to_default_state();void notify_context_about_canvas_size_change();void did_commit_placeholder_frame(Gfx::IntSize, bool origin_clean);diff --git a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp b/Libraries/LibWeb/HTML/OffscreenCanvas.cppindex f46de74b55e..72717f30e12 100644--- a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp+++ b/Libraries/LibWeb/HTML/OffscreenCanvas.cpp@@ -35,6 +35,7 @@#include <LibWeb/Platform/EventLoopPlugin.h>#include <LibWeb/Platform/FontPlugin.h>#include <LibWeb/WebGL/WebGL2RenderingContext.h>+#include <LibWeb/WebGL/WebGLContextAttributes.h>#include <LibWeb/WebGL/WebGLRenderingContext.h>#include <LibWeb/WebIDL/DOMException.h>#include <LibWeb/WebIDL/ExceptionOrUtils.h>@@ -361,21 +362,38 @@ OffscreenCanvas::HasOrCreatedContext OffscreenCanvas::create_2d_context(CanvasReif (!m_context.has<Empty>())return m_context.has<GC::Ref<OffscreenCanvasRenderingContext2D>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;+ VERIFY(!is_detached());m_context = OffscreenCanvasRenderingContext2D::create(*this, context_attributes);return HasOrCreatedContext::Yes;}+JS::ThrowCompletionOr<void> OffscreenCanvas::throw_if_detached() const+{+ if (!is_detached())+ return {};++ auto& realm = relevant_global_object().shape().realm();+ return throw_completion(realm, WebIDL::InvalidStateError::create(realm, "OffscreenCanvas is detached"_utf16));+}+template<typename ContextType>JS::ThrowCompletionOr<OffscreenCanvas::HasOrCreatedContext> OffscreenCanvas::create_webgl_context(JS::Value options){if (!m_context.has<Empty>())return m_context.has<GC::Ref<ContextType>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;+ auto context_attributes = TRY(WebGL::convert_value_to_context_attributes_dictionary(vm(), options));+ TRY(throw_if_detached());+ // AD-HOC: Converting the options can run script that creates a context.+ if (!m_context.has<Empty>())+ return m_context.has<GC::Ref<ContextType>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;+auto& realm = relevant_global_object().shape().realm();- auto maybe_context = TRY(ContextType::create(realm, WebGL::CanvasOwner { GC::Ref { *this } }, options));+ auto maybe_context = ContextType::create(realm, WebGL::CanvasOwner { GC::Ref { *this } }, context_attributes);if (!maybe_context)return HasOrCreatedContext::No;+ VERIFY(!is_detached());m_context = GC::Ref<ContextType>(*maybe_context);return HasOrCreatedContext::Yes;}@@ -450,7 +468,7 @@ GC::Ref<WebIDL::Promise> convert_to_blob(JS::Realm& realm, HTML::OffscreenCanvasreturn result_promise;}-JS::ThrowCompletionOr<HTML::OffscreenRenderingContext> get_context(JS::Realm& realm, HTML::OffscreenCanvas& offscreen_canvas, OffscreenRenderingContextId context_id, JS::Value options)+JS::ThrowCompletionOr<HTML::OffscreenRenderingContext> get_context(HTML::OffscreenCanvas& offscreen_canvas, OffscreenRenderingContextId context_id, JS::Value options){// 1. If options is not an object, then set options to null.if (!options.is_object())@@ -463,14 +481,13 @@ JS::ThrowCompletionOr<HTML::OffscreenRenderingContext> get_context(JS::Realm& re// matches this OffscreenCanvas object's context mode and whose row header// matches contextId:// NOTE: See the spec for the full table.- if (offscreen_canvas.is_detached()) {- return WebIDL::throw_dom_exception_if_needed(realm.vm(), realm, [] -> WebIDL::ExceptionOr<HTML::OffscreenRenderingContext> {- return WebIDL::InvalidStateError::create("OffscreenCanvas is detached"_utf16);- });- }+ TRY(offscreen_canvas.throw_if_detached());+ // AD-HOC: Converting the options can run script that detaches this OffscreenCanvas, so we check whether it is+ // detached again after each conversion.if (context_id == OffscreenRenderingContextId::_2d) {auto context_attributes = TRY(convert_to_idl_value_for_canvas_rendering_context2d_settings(offscreen_canvas.vm(), options));+ TRY(offscreen_canvas.throw_if_detached());if (offscreen_canvas.create_2d_context(context_attributes) == HTML::OffscreenCanvas::HasOrCreatedContext::Yes)return offscreen_canvas.context().get<GC::Ref<HTML::OffscreenCanvasRenderingContext2D>>();diff --git a/Libraries/LibWeb/HTML/OffscreenCanvas.h b/Libraries/LibWeb/HTML/OffscreenCanvas.hindex 5d151d9634e..f053fb43a99 100644--- a/Libraries/LibWeb/HTML/OffscreenCanvas.h+++ b/Libraries/LibWeb/HTML/OffscreenCanvas.h@@ -51,6 +51,7 @@ public:void transfer_receiving_steps(TransferredFields const&);bool is_detached() const { return m_detached; }+ JS::ThrowCompletionOr<void> throw_if_detached() const;WebIDL::UnsignedLongLong width() const { return m_width; }WebIDL::UnsignedLongLong height() const { return m_height; }diff --git a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.hindex edf94ebb112..80ed4226dfe 100644--- a/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h+++ b/Libraries/LibWeb/WebGL/WebGL2RenderingContext.h@@ -18,7 +18,7 @@ class WEB_API WebGL2RenderingContext final : public WebGLRenderingContextBase {WEB_RUST_INTERFACE(WebGL2RenderingContext, web_gl2_rendering_context)public:- static JS::ThrowCompletionOr<GC::Ptr<WebGL2RenderingContext>> create(JS::Realm&, CanvasOwner, JS::Value options);+ static GC::Ptr<WebGL2RenderingContext> create(JS::Realm&, CanvasOwner, WebGLContextAttributes const&);};}diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContext.h b/Libraries/LibWeb/WebGL/WebGLRenderingContext.hindex 18b28e9ac66..207c9bbb4b6 100644--- a/Libraries/LibWeb/WebGL/WebGLRenderingContext.h+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContext.h@@ -17,7 +17,7 @@ class WEB_API WebGLRenderingContext final : public WebGLRenderingContextBase {WEB_RUST_INTERFACE(WebGLRenderingContext, web_gl_rendering_context)public:- static JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> create(JS::Realm&, CanvasOwner, JS::Value options);+ static GC::Ptr<WebGLRenderingContext> create(JS::Realm&, CanvasOwner, WebGLContextAttributes const&);};}diff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cppindex f4774b058db..3a05e64cd46 100644--- a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp@@ -229,11 +229,9 @@ Optional<RemoteWebGLContext> create_remote_webgl_context(HTML::CanvasHost& canva}-JS::ThrowCompletionOr<WebGLRenderingContextBase*> WebGLRenderingContextBase::create(JS::Realm& realm, CanvasOwner canvas, JS::Value options, WebGLVersion webgl_version)+WebGLRenderingContextBase* WebGLRenderingContextBase::create(JS::Realm& realm, CanvasOwner canvas, WebGLContextAttributes const& context_attributes, WebGLVersion webgl_version){// We should be coming here from getContext being called on a wrapped <canvas> element or OffscreenCanvas.- auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(realm.vm(), options));-auto& canvas_host = canvas_host_for(canvas);auto remote = create_remote_webgl_context(canvas_host, webgl_version, context_attributes.depth, context_attributes.stencil, context_attributes.antialias);if (!remote.has_value()) {@@ -255,14 +253,14 @@ JS::ThrowCompletionOr<WebGLRenderingContextBase*> WebGLRenderingContextBase::crereturn static_cast<WebGLRenderingContextBase*>(web_webgl_context_create(JS::HostABI::vm_to_abi(realm.vm()), &arguments));}-JS::ThrowCompletionOr<GC::Ptr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, CanvasOwner canvas, JS::Value options)+GC::Ptr<WebGLRenderingContext> WebGLRenderingContext::create(JS::Realm& realm, CanvasOwner canvas, WebGLContextAttributes const& context_attributes){- return static_cast<WebGLRenderingContext*>(TRY(WebGLRenderingContextBase::create(realm, canvas, options, WebGLVersion::WebGL1)));+ return static_cast<WebGLRenderingContext*>(WebGLRenderingContextBase::create(realm, canvas, context_attributes, WebGLVersion::WebGL1));}-JS::ThrowCompletionOr<GC::Ptr<WebGL2RenderingContext>> WebGL2RenderingContext::create(JS::Realm& realm, CanvasOwner canvas, JS::Value options)+GC::Ptr<WebGL2RenderingContext> WebGL2RenderingContext::create(JS::Realm& realm, CanvasOwner canvas, WebGLContextAttributes const& context_attributes){- return static_cast<WebGL2RenderingContext*>(TRY(WebGLRenderingContextBase::create(realm, canvas, options, WebGLVersion::WebGL2)));+ return static_cast<WebGL2RenderingContext*>(WebGLRenderingContextBase::create(realm, canvas, context_attributes, WebGLVersion::WebGL2));}HTML::CanvasHost& WebGLRenderingContextBase::canvas_host() constdiff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.hindex 1f86bb8c52a..689bab1be2a 100644--- a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.h@@ -16,7 +16,6 @@#include <LibGfx/Rect.h>#include <LibGfx/Size.h>#include <LibJS/Forward.h>-#include <LibJS/Runtime/Completion.h>#include <LibWeb/Bindings/RustInterface.h>#include <LibWeb/Export.h>#include <LibWeb/Forward.h>@@ -53,7 +52,7 @@ public:void restore_context_after_compositor_reconnect();protected:- static JS::ThrowCompletionOr<WebGLRenderingContextBase*> create(JS::Realm&, CanvasOwner, JS::Value options, WebGLVersion);+ static WebGLRenderingContextBase* create(JS::Realm&, CanvasOwner, WebGLContextAttributes const&, WebGLVersion);};}diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex e8508ef68f3..32f16f18734 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -65,7 +65,7 @@ ALLOWED_REALM_MENTIONS = {"FileAPI": (2, 9, "Blob glue still creates streams and buffers in selected realms"),"HTML": (63,- 168,+ 167,"HTML algorithms still contain structured serialization, including canvas, navigation transfer/reconstruction, worker/worklet, and event realm use, and cross-origin Window/Location property functions are created in the current realm; window postMessage glue supplies principal realms and routes remote messages; ""the C++ glue of the Rust structured serialization creates platform objects and SharedArrayBuffers in a given realm; ""the C++ glue of the Rust Storage and StorageEvent creates them in a given realm",diff --git a/Tests/LibWeb/Text/expected/canvas/getcontext-options-transfer-canvas.txt b/Tests/LibWeb/Text/expected/canvas/getcontext-options-transfer-canvas.txtnew file mode 100644index 00000000000..a9509139d67--- /dev/null+++ b/Tests/LibWeb/Text/expected/canvas/getcontext-options-transfer-canvas.txt@@ -0,0 +1,9 @@+getContext("2d") with options that transfer control: InvalidStateError, transferred: true+getContext("bitmaprenderer") with options that transfer control: InvalidStateError, transferred: true+getContext("webgl") with options that transfer control: InvalidStateError, transferred: true+getContext("webgl2") with options that transfer control: InvalidStateError, transferred: true+OffscreenCanvas getContext("2d") with options that transfer it: InvalidStateError, transferred: true+OffscreenCanvas getContext("webgl") with options that transfer it: InvalidStateError, transferred: true+OffscreenCanvas getContext("webgl2") with options that transfer it: InvalidStateError, transferred: true+HTMLCanvasElement 2d context created by a webgl options getter is kept: true+OffscreenCanvas 2d context created by a webgl options getter is kept: truediff --git a/Tests/LibWeb/Text/input/canvas/getcontext-options-transfer-canvas.html b/Tests/LibWeb/Text/input/canvas/getcontext-options-transfer-canvas.htmlnew file mode 100644index 00000000000..28ee538e56a--- /dev/null+++ b/Tests/LibWeb/Text/input/canvas/getcontext-options-transfer-canvas.html@@ -0,0 +1,43 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ function printGetContextResult(description, canvas, contextType, transfer) {+ let transferred = false;+ const options = {+ get alpha() {+ transfer();+ transferred = true;+ return true;+ },+ };+ try {+ println(`${description}: ${canvas.getContext(contextType, options)}, transferred: ${transferred}`);+ } catch (error) {+ println(`${description}: ${error.name}, transferred: ${transferred}`);+ }+ }++ test(() => {+ for (const contextType of ["2d", "bitmaprenderer", "webgl", "webgl2"]) {+ const canvas = document.createElement("canvas");+ printGetContextResult(`getContext("${contextType}") with options that transfer control`, canvas, contextType, () => canvas.transferControlToOffscreen());+ }++ for (const contextType of ["2d", "webgl", "webgl2"]) {+ const offscreenCanvas = new OffscreenCanvas(4, 4);+ printGetContextResult(`OffscreenCanvas getContext("${contextType}") with options that transfer it`, offscreenCanvas, contextType, () => structuredClone(offscreenCanvas, { transfer: [offscreenCanvas] }));+ }++ for (const canvas of [document.createElement("canvas"), new OffscreenCanvas(4, 4)]) {+ let contextFromGetter = null;+ const options = {+ get alpha() {+ contextFromGetter = canvas.getContext("2d");+ return true;+ },+ };+ canvas.getContext("webgl", options);+ println(`${canvas.constructor.name} 2d context created by a webgl options getter is kept: ${contextFromGetter !== null && canvas.getContext("2d") === contextFromGetter}`);+ }+ });+</script>