Skip to content

Truly independent web browser

LibWeb: Preserve wrappers whose identity script holds onto

Andreas Kling463c8eee9f5d
diff --git a/Libraries/LibWeb/Bindings/PlatformObject.cpp b/Libraries/LibWeb/Bindings/PlatformObject.cpp
index dbb1c300293..0983db9ed62 100644
--- a/Libraries/LibWeb/Bindings/PlatformObject.cpp
+++ b/Libraries/LibWeb/Bindings/PlatformObject.cpp
@@ -55,14 +55,20 @@ JS::ThrowCompletionOr<bool> ordinary_define_own_property_and_preserve_wrapper_if
return result;
}
+void preserve_identity_of_platform_object(JS::HostObject& wrapper)
+{
+ if (!wrapper_realm(wrapper).host_defined())
+ return;
+ if (auto* wrappable = wrappable_of(wrapper))
+ preserve_wrapper(*wrappable, wrapper);
+}
+
static JS::ThrowCompletionOr<bool> ordinary_set_prototype_of_and_preserve_wrapper_if_needed(JS::HostObject& wrapper, JS::Object* prototype)
{
auto* old_prototype = wrapper.shape().prototype();
auto result = TRY(wrapper.ordinary_set_prototype_of(prototype));
- if (result && old_prototype != wrapper.shape().prototype() && wrapper_realm(wrapper).host_defined()) {
- if (auto* wrappable = wrappable_of(wrapper))
- preserve_wrapper(*wrappable, wrapper);
- }
+ if (result && old_prototype != wrapper.shape().prototype())
+ preserve_identity_of_platform_object(wrapper);
return result;
}
@@ -70,10 +76,8 @@ static JS::ThrowCompletionOr<bool> ordinary_prevent_extensions_and_preserve_wrap
{
auto was_extensible = wrapper.extensible();
auto result = TRY(wrapper.ordinary_prevent_extensions());
- if (result && was_extensible && !wrapper.extensible() && wrapper_realm(wrapper).host_defined()) {
- if (auto* wrappable = wrappable_of(wrapper))
- preserve_wrapper(*wrappable, wrapper);
- }
+ if (result && was_extensible && !wrapper.extensible())
+ preserve_identity_of_platform_object(wrapper);
return result;
}
@@ -375,6 +379,11 @@ struct PlatformObjectTraits {
return ordinary_define_own_property_and_preserve_wrapper_if_needed(wrapper, property_name, property_descriptor, precomputed_get_own_property);
}
+ static void preserve_identity(JS::HostObject& wrapper)
+ {
+ preserve_identity_of_platform_object(wrapper);
+ }
+
static void finalize(JS::HostObject& wrapper)
{
finalize_platform_object(wrapper);
@@ -399,6 +408,11 @@ struct GlobalPlatformObjectTraits {
return ordinary_define_own_property_and_preserve_wrapper_if_needed(wrapper, property_name, property_descriptor, precomputed_get_own_property);
}
+ static void preserve_identity(JS::HostObject& wrapper)
+ {
+ preserve_identity_of_platform_object(wrapper);
+ }
+
static void finalize(JS::HostObject& wrapper)
{
finalize_platform_object(wrapper);
@@ -605,6 +619,11 @@ struct LegacyPlatformObjectTraits {
return is<DOM::Document>(wrappable_of(wrapper)) && host_defined_wrapper_world(wrapper_realm(wrapper)).is_main_world();
}
+ static void preserve_identity(JS::HostObject& wrapper)
+ {
+ preserve_identity_of_platform_object(wrapper);
+ }
+
static void finalize(JS::HostObject& wrapper)
{
finalize_platform_object(wrapper);
diff --git a/Libraries/LibWeb/Bindings/PlatformObject.h b/Libraries/LibWeb/Bindings/PlatformObject.h
index d30d9f481e6..33a84c6596c 100644
--- a/Libraries/LibWeb/Bindings/PlatformObject.h
+++ b/Libraries/LibWeb/Bindings/PlatformObject.h
@@ -106,6 +106,9 @@ extern JSHostObjectHooks const dom_exception_wrapper_hooks;
// The finalize hook of every wrapper host class calls this to remove the wrapper from its world.
void finalize_platform_object(JS::HostObject& wrapper);
+// The preserve_identity hook of wrappers: script made the identity of the wrapper outlive the references to it, so the
+// wrapper must not be dropped and recreated anymore.
+void preserve_identity_of_platform_object(JS::HostObject& wrapper);
// https://webidl.spec.whatwg.org/#dfn-platform-object
// Only host classes give objects the platform object flag, so every platform object is a host object: a wrapper, whose
diff --git a/Libraries/LibWeb/HTML/Location.cpp b/Libraries/LibWeb/HTML/Location.cpp
index 629b5943c05..22527b1bcc3 100644
--- a/Libraries/LibWeb/HTML/Location.cpp
+++ b/Libraries/LibWeb/HTML/Location.cpp
@@ -266,6 +266,11 @@ struct LocationWrapperTraits {
return HTML::cross_origin_own_property_keys(location);
}
+ static void preserve_identity(JS::HostObject& wrapper)
+ {
+ preserve_identity_of_platform_object(wrapper);
+ }
+
static void finalize(JS::HostObject& wrapper)
{
finalize_platform_object(wrapper);
diff --git a/Tests/LibWeb/Text/expected/DOM/weakly-held-wrapper-survives-gc.txt b/Tests/LibWeb/Text/expected/DOM/weakly-held-wrapper-survives-gc.txt
new file mode 100644
index 00000000000..e6cfdafdc95
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/weakly-held-wrapper-survives-gc.txt
@@ -0,0 +1,6 @@
+WeakRef: true
+WeakMap: value
+WeakSet: true
+FinalizationRegistry: not finalized
+Private element: stamped
+Removed node collected: true
diff --git a/Tests/LibWeb/Text/expected/form-elements-keeps-form-alive.txt b/Tests/LibWeb/Text/expected/form-elements-keeps-form-alive.txt
index 0b68fa4ae5a..63841811e3d 100644
--- a/Tests/LibWeb/Text/expected/form-elements-keeps-form-alive.txt
+++ b/Tests/LibWeb/Text/expected/form-elements-keeps-form-alive.txt
@@ -1,2 +1,2 @@
-form wrapper alive while collection is held: false
+form wrapper alive while collection is held: true
collection.length: 1
diff --git a/Tests/LibWeb/Text/input/DOM/weakly-held-wrapper-survives-gc.html b/Tests/LibWeb/Text/input/DOM/weakly-held-wrapper-survives-gc.html
new file mode 100644
index 00000000000..c64c77556cd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/weakly-held-wrapper-survives-gc.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Holding a node weakly or giving it a private element makes the identity of its wrapper observable, so the wrapper
+ // must survive collections for as long as the node lives, even when script holds no reference to it.
+ promiseTest(async () => {
+ const weakMap = new WeakMap();
+ const weakSet = new WeakSet();
+ const finalized = [];
+ const registry = new FinalizationRegistry(name => finalized.push(name));
+ class ReturnsArgument {
+ constructor(object) {
+ return object;
+ }
+ }
+ class Stamp extends ReturnsArgument {
+ #stamp = "stamped";
+ static read(object) {
+ return #stamp in object ? object.#stamp : "missing";
+ }
+ }
+ let weakRef;
+ let removedWeakRef;
+ (() => {
+ const appendDiv = id => {
+ const div = document.createElement("div");
+ div.id = id;
+ return document.body.appendChild(div);
+ };
+ weakRef = new WeakRef(appendDiv("weak-ref"));
+ weakMap.set(appendDiv("weak-map"), "value");
+ weakSet.add(appendDiv("weak-set"));
+ registry.register(appendDiv("finalization-registry"), "finalization-registry");
+ new Stamp(appendDiv("private-element"));
+
+ const removed = appendDiv("removed");
+ removedWeakRef = new WeakRef(removed);
+ registry.register(removed, "removed");
+ removed.remove();
+ })();
+
+ // NB: The registry handles every collected target in one cleanup task, so once the removed node's callback has
+ // run, a collected wrapper of the registered node would have been reported as well.
+ await collectGarbageUntil(() => finalized.includes("removed"));
+ if (!finalized.includes("removed"))
+ throw new Error("Removed node cleanup callback did not run");
+
+ println(`WeakRef: ${weakRef.deref() === document.getElementById("weak-ref")}`);
+ println(`WeakMap: ${weakMap.get(document.getElementById("weak-map"))}`);
+ println(`WeakSet: ${weakSet.has(document.getElementById("weak-set"))}`);
+ println(`FinalizationRegistry: ${finalized.includes("finalization-registry") ? "finalized" : "not finalized"}`);
+ println(`Private element: ${Stamp.read(document.getElementById("private-element"))}`);
+ println(`Removed node collected: ${removedWeakRef.deref() === undefined}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/form-elements-keeps-form-alive.html b/Tests/LibWeb/Text/input/form-elements-keeps-form-alive.html
index 81e33445cfb..e4fc5a43c99 100644
--- a/Tests/LibWeb/Text/input/form-elements-keeps-form-alive.html
+++ b/Tests/LibWeb/Text/input/form-elements-keeps-form-alive.html
@@ -3,7 +3,7 @@
<body></body>
<script>
// Regression test: a live HTMLFormControlsCollection captures its form in the filter, so the form implementation
- // must stay alive while the collection is reachable. The JS wrapper for the form may be collected separately.
+ // must stay alive while the collection is reachable, and so must its wrapper, which a WeakRef holds.
// Previously the form was garbage-collected out from under the live collection, leaving a dangling pointer in the
// filter (a use-after-free). See issue #9948.
asyncTest(done => {
@@ -16,12 +16,12 @@
form.remove();
form = null;
- setTimeout(() => {
- internals.gc();
+ setTimeout(async () => {
+ await internals.gcAsync();
// Churn the heap to dislodge any stale references, then collect again.
for (let i = 0; i < 10000; ++i)
document.body.appendChild(document.createElement("div"));
- internals.gc();
+ await internals.gcAsync();
println(`form wrapper alive while collection is held: ${weakForm.deref() !== undefined}`);
println(`collection.length: ${collection.length}`);