Truly independent web browser
LibGC: Require precise rooting for vectors of GC pointers
Andreas Klingd7858dc6a316
diff --git a/Libraries/LibGC/ConservativeVector.h b/Libraries/LibGC/ConservativeVector.hindex 7b86995d608..3e1f96e3753 100644--- a/Libraries/LibGC/ConservativeVector.h+++ b/Libraries/LibGC/ConservativeVector.h@@ -31,7 +31,7 @@ public:};template<typename T, size_t inline_capacity>-class GC_API ConservativeVector final+requires(!IsSpecializationOf<RemoveCVReference<T>, Ptr> && !IsSpecializationOf<RemoveCVReference<T>, Ref>) class GC_API ConservativeVector final: public ConservativeVectorBase, public Vector<T, inline_capacity> {diff --git a/Libraries/LibGC/Forward.h b/Libraries/LibGC/Forward.hindex d28248c4eab..759ee6ece2a 100644--- a/Libraries/LibGC/Forward.h+++ b/Libraries/LibGC/Forward.h@@ -6,6 +6,7 @@#pragma once+#include <AK/StdLibExtras.h>#include <AK/Types.h>#include <LibGC/Export.h>@@ -43,8 +44,9 @@ class Ref;template<class T>class Ptr;+// Precisely traceable pointers belong in RootVector rather than conservative storage.template<class T, size_t inline_capacity = 0>-class ConservativeVector;+requires(!IsSpecializationOf<RemoveCVReference<T>, Ptr> && !IsSpecializationOf<RemoveCVReference<T>, Ref>) class ConservativeVector;template<typename K, typename V, typename KeyTraits, typename ValueTraits, bool IsOrdered>class ConservativeHashMap;diff --git a/Libraries/LibWeb/Animations/AnimationEffect.cpp b/Libraries/LibWeb/Animations/AnimationEffect.cppindex ef3537a4937..7aaef5273c9 100644--- a/Libraries/LibWeb/Animations/AnimationEffect.cpp+++ b/Libraries/LibWeb/Animations/AnimationEffect.cpp@@ -5,6 +5,8 @@*/#include <AK/ScopeGuard.h>+#include <LibGC/ConservativeVector.h>+#include <LibGC/RootVector.h>#include <LibJS/Runtime/VM.h>#include <LibWeb/Animations/Animation.h>#include <LibWeb/Animations/AnimationEffect.h>@@ -856,7 +858,7 @@ void AnimationUpdateContext::publish()struct Sample {DOM::AbstractElement element;ElementData const& data;- GC::ConservativeVector<GC::Ref<KeyframeEffect>> effects;+ GC::RootVector<GC::Ref<KeyframeEffect>> effects;};GC::ConservativeVector<Sample> samples;for (auto& it : elements) {@@ -872,7 +874,7 @@ void AnimationUpdateContext::publish()// never among the collected effects, but their values are already part of the published// style. Collect them first, in composite order below every associated effect, or this// update would rebuild the style without them.- GC::ConservativeVector<GC::Ref<KeyframeEffect>> effects_to_collect;+ GC::RootVector<GC::Ref<KeyframeEffect>> effects_to_collect;target->document().style_computer().for_each_provisional_transition_effect(element, [&](KeyframeEffect& effect) {effects_to_collect.append(effect);});diff --git a/Libraries/LibWeb/Animations/AnimationEffect.h b/Libraries/LibWeb/Animations/AnimationEffect.hindex b4547d02c3f..47864a88ab3 100644--- a/Libraries/LibWeb/Animations/AnimationEffect.h+++ b/Libraries/LibWeb/Animations/AnimationEffect.h@@ -11,7 +11,7 @@#include <AK/Utf16String.h>#include <AK/Variant.h>#include <LibGC/ConservativeHashMap.h>-#include <LibGC/ConservativeVector.h>+#include <LibGC/RootVector.h>#include <LibWeb/Animations/TimeValue.h>#include <LibWeb/Bindings/AnimationEffect.h>#include <LibWeb/Bindings/Wrappable.h>@@ -48,7 +48,7 @@ struct AnimationUpdateContext {CSS::StyleRecordID style_record_before_update;RefPtr<CSS::ComputedStyleWorkingSet> target_style;- GC::ConservativeVector<GC::Ref<KeyframeEffect>> effects;+ GC::RootVector<GC::Ref<KeyframeEffect>> effects;// Whether the record the update samples over was computed by this style update, so that the base// adjustments the sampled values feed into were made over the values the element holds now.bool base_is_current { false };diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cppindex 8bf0796995c..56381e5b219 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -17,6 +17,7 @@#include <LibGC/ConservativeVector.h>#include <LibGC/DeferGC.h>#include <LibGC/Heap.h>+#include <LibGC/RootVector.h>#include <LibJS/Runtime/ExternalMemory.h>#include <LibWeb/Animations/Animation.h>#include <LibWeb/Bindings/Node.h>@@ -1053,7 +1054,7 @@ static void run_post_connection_steps(Nodes const& nodes)// post-connection steps while we’re traversing the node tree. This is because the post-connection steps can// modify the tree’s structure, making live traversal unsafe, possibly leading to the post-connection steps// being called multiple times on the same node.- GC::ConservativeVector<GC::Ref<Node>, 1> static_node_list;+ GC::RootVector<GC::Ref<Node>, 1> static_node_list;// 11. For each node of nodes, in tree order:for (auto& node : nodes) {diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cppindex fc18daf8220..1f71fdf1c85 100644--- a/Libraries/LibWeb/HTML/Focus.cpp+++ b/Libraries/LibWeb/HTML/Focus.cpp@@ -9,7 +9,9 @@#include <AK/QuickSort.h>#include <AK/TypeCasts.h>#include <AK/Vector.h>+#include <LibGC/ConservativeVector.h>#include <LibGC/Root.h>+#include <LibGC/RootVector.h>#include <LibWeb/Bindings/CSS.h>#include <LibWeb/CSS/Invalidation/PseudoClassInvalidator.h>#include <LibWeb/CSS/StyleEngineInput.h>@@ -777,13 +779,13 @@ static void append_focus_navigation_scope_descendants(DOM::Node& parent, Vector<}// https://html.spec.whatwg.org/multipage/interaction.html#focus-navigation-scope-static GC::ConservativeVector<GC::Ref<DOM::Element>> focus_navigation_scope(DOM::Node& owner)+static GC::RootVector<GC::Ref<DOM::Element>> focus_navigation_scope(DOM::Node& owner){// The contents of a given focus navigation scope owner owner's focus navigation scope are all elements whose// associated focus navigation owner is owner.// NB: We collect them in tree order by walking down from owner, instead of computing the associated focus// navigation owner of every element.- GC::ConservativeVector<GC::Ref<DOM::Element>> scope;+ GC::RootVector<GC::Ref<DOM::Element>> scope;if (auto* document = as_if<DOM::Document>(owner)) {// NB: We treat the document element as being in its document's scope.@@ -852,7 +854,7 @@ static bool is_sequentially_focusable(DOM::Node const& node)}// https://html.spec.whatwg.org/multipage/interaction.html#tabindex-ordered-focus-navigation-scope-static GC::ConservativeVector<GC::Ref<DOM::Element>> tabindex_ordered_focus_navigation_scope(DOM::Node& owner)+static GC::RootVector<GC::Ref<DOM::Element>> tabindex_ordered_focus_navigation_scope(DOM::Node& owner){// A tabindex-ordered focus navigation scope is a list of focusable areas and focus navigation scope owners.// Every focus navigation scope owner owner has tabindex-ordered focus navigation scope, whose contents are@@ -870,7 +872,7 @@ static GC::ConservativeVector<GC::Ref<DOM::Element>> tabindex_ordered_focus_navisize_t tree_index;};GC::ConservativeVector<Item> positive_items;- GC::ConservativeVector<GC::Ref<DOM::Element>> result;+ GC::RootVector<GC::Ref<DOM::Element>> result;for (auto const& element : focus_navigation_scope(owner)) {if (!is_focus_navigation_scope_owner(*element) && !is_sequential_focus_navigation_focusable_area(*element))@@ -893,7 +895,7 @@ static GC::ConservativeVector<GC::Ref<DOM::Element>> tabindex_ordered_focus_navireturn a.tree_index < b.tree_index;});- GC::ConservativeVector<GC::Ref<DOM::Element>> ordered;+ GC::RootVector<GC::Ref<DOM::Element>> ordered;ordered.ensure_capacity(positive_items.size() + result.size());for (auto const& item : positive_items)ordered.unchecked_append(item.element);@@ -927,12 +929,12 @@ static void append_flattened_tabindex_ordered_focus_navigation_scope(DOM::Node&}// https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation-order-GC::ConservativeVector<GC::Ref<DOM::Element>> sequential_focus_navigation_order(DOM::Document& document)+GC::RootVector<GC::Ref<DOM::Element>> sequential_focus_navigation_order(DOM::Document& document){// Each Document has a sequential focus navigation order, which orders some or all of the focusable areas in the// Document relative to each other. Its contents and ordering are given by the flattened tabindex-ordered focus// navigation scope of the Document.- GC::ConservativeVector<GC::Ref<DOM::Element>> order;+ GC::RootVector<GC::Ref<DOM::Element>> order;append_flattened_tabindex_ordered_focus_navigation_scope(document, order);return order;}diff --git a/Libraries/LibWeb/HTML/Focus.h b/Libraries/LibWeb/HTML/Focus.hindex 2f8effa9281..a68e5f29323 100644--- a/Libraries/LibWeb/HTML/Focus.h+++ b/Libraries/LibWeb/HTML/Focus.h@@ -8,8 +8,8 @@#include <AK/Variant.h>#include <AK/Vector.h>-#include <LibGC/ConservativeVector.h>#include <LibGC/Root.h>+#include <LibGC/RootVector.h>#include <LibWeb/Export.h>#include <LibWeb/Forward.h>#include <LibWebCommon/HTML/FocusTrigger.h>@@ -39,7 +39,7 @@ enum class SelectionMechanism : u8 {Sequential,};-WEB_API GC::ConservativeVector<GC::Ref<DOM::Element>> sequential_focus_navigation_order(DOM::Document&);+WEB_API GC::RootVector<GC::Ref<DOM::Element>> sequential_focus_navigation_order(DOM::Document&);WEB_API GC::Ptr<DOM::Element> sequential_navigation_search(Variant<GC::Ref<LocalNavigable>, GC::Ref<DOM::Node>> starting_point, SequentialFocusDirection, SelectionMechanism);}diff --git a/Libraries/LibWeb/HTML/HTMLSlotElement.cpp b/Libraries/LibWeb/HTML/HTMLSlotElement.cppindex abbaea0ac4a..2655210694c 100644--- a/Libraries/LibWeb/HTML/HTMLSlotElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLSlotElement.cpp@@ -7,6 +7,7 @@*/#include <LibGC/ConservativeHashTable.h>+#include <LibGC/RootVector.h>#include <LibWeb/Bindings/HTMLSlotElement.h>#include <LibWeb/DOM/Element.h>#include <LibWeb/DOM/ShadowRoot.h>@@ -129,7 +130,7 @@ void HTMLSlotElement::assign(GC::ConservativeVector<SlottableHandle> nodes)// INTEROP: The spec only assigns slottables for this's root below, so a slot in another tree that loses a node// to this slot keeps it as an assigned node and fires no slotchange event. Browsers also assign// slottables for the trees of those slots, and so do we.- GC::ConservativeVector<GC::Ref<DOM::Node>> roots_of_previous_slots;+ GC::RootVector<GC::Ref<DOM::Node>> roots_of_previous_slots;// 3. For each node of nodes:for (auto& node_handle : nodes) {diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex c9e62997570..2f39189e652 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -15,6 +15,7 @@#include <LibGC/ConservativeHashTable.h>#include <LibGC/Heap.h>#include <LibGC/RootHashTable.h>+#include <LibGC/RootVector.h>#include <LibGfx/Bitmap.h>#include <LibUnicode/CharacterTypes.h>#include <LibUnicode/Segmenter.h>@@ -4955,7 +4956,7 @@ EventResult EventHandler::handle_pointer_event(Web::MouseEvent const& input, CSSauto click_coordinates = coordinates;if (auto* click_layout = click_target->layout_node(read))click_coordinates = compute_mouse_event_coordinates(visual_viewport_position, viewport_position, *click_layout);- GC::ConservativeVector<GC::Ref<DOM::Node>> touch_hover_ancestors;+ GC::RootVector<GC::Ref<DOM::Node>> touch_hover_ancestors;if (is_touch) {for (auto* ancestor = mouse_target.ptr(); ancestor; ancestor = ancestor->parent_or_shadow_host())touch_hover_ancestors.append(*ancestor);diff --git a/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h b/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.hindex 7b353354355..97a25a85546 100644--- a/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h+++ b/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h@@ -11,8 +11,8 @@#include <AK/Utf16String.h>#include <AK/Vector.h>#include <LibGC/ConservativeHashMap.h>-#include <LibGC/ConservativeVector.h>#include <LibGC/Ptr.h>+#include <LibGC/RootVector.h>#include <LibWeb/Forward.h>#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>#include <LibWebCommon/PixelUnits.h>@@ -64,7 +64,7 @@ struct LargestContentfulPaintCandidate {// NB: The elements that get a text node in their set of owned text nodes for the first time as a frame is painted,// with those text nodes.-using PaintedTextNodes = GC::OrderedConservativeHashMap<GC::Ref<DOM::Element>, GC::ConservativeVector<GC::Ref<DOM::Text>>>;+using PaintedTextNodes = GC::OrderedConservativeHashMap<GC::Ref<DOM::Element>, GC::RootVector<GC::Ref<DOM::Text>>>;struct DocumentPaintTimingState {// https://w3c.github.io/paint-timing/#set-of-previously-reported-paintsdiff --git a/Libraries/LibWeb/PaintTiming/PaintTiming.cpp b/Libraries/LibWeb/PaintTiming/PaintTiming.cppindex f038249e56f..daec4e4ac57 100644--- a/Libraries/LibWeb/PaintTiming/PaintTiming.cpp+++ b/Libraries/LibWeb/PaintTiming/PaintTiming.cpp@@ -6,6 +6,7 @@#include <AK/NeverDestroyed.h>#include <LibGC/ConservativeVector.h>+#include <LibGC/RootVector.h>#include <LibWeb/CSS/ComputedValues.h>#include <LibWeb/CSS/FontComputer.h>#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>@@ -496,7 +497,7 @@ void mark_paint_timing(DOM::Document& document)});// NB: This is when the user agent paints a Text node for the first time.- GC::ConservativeVector<GC::Ref<DOM::Text>> text_nodes_no_longer_pending;+ GC::RootVector<GC::Ref<DOM::Text>> text_nodes_no_longer_pending;auto is_no_longer_pending = [&](GC::Ref<DOM::Text> text) {if (!text->is_connected() || &text->document() != &document)return true;diff --git a/Libraries/LibWeb/UIEvents/TouchEvent.cpp b/Libraries/LibWeb/UIEvents/TouchEvent.cppindex 3ee5617fcee..334ccd45de0 100644--- a/Libraries/LibWeb/UIEvents/TouchEvent.cpp+++ b/Libraries/LibWeb/UIEvents/TouchEvent.cpp@@ -5,7 +5,6 @@*/#include <LibGC/ConservativeHashMap.h>-#include <LibGC/ConservativeVector.h>#include <LibGC/Heap.h>#include <LibGC/RootVector.h>#include <LibWeb/UIEvents/TouchEvent.h>@@ -57,7 +56,7 @@ void TouchEvent::retarget_touch_lists()GC::RootVector<GC::Ref<Touch>> cloned_touches;size_t offset = 0;auto retarget = [&](GC::Ref<TouchList> original) {- GC::ConservativeVector<GC::Ref<Touch>> touches;+ GC::RootVector<GC::Ref<Touch>> touches;bool changed = false;for (u32 index = 0; index < original->length(); ++index) {auto touch = original->item(index);diff --git a/Libraries/LibWeb/UIEvents/TouchList.cpp b/Libraries/LibWeb/UIEvents/TouchList.cppindex 68ee49b5173..2b8fe4a847e 100644--- a/Libraries/LibWeb/UIEvents/TouchList.cpp+++ b/Libraries/LibWeb/UIEvents/TouchList.cpp@@ -5,18 +5,19 @@*/#include <LibGC/Heap.h>+#include <LibGC/RootVector.h>#include <LibWeb/UIEvents/TouchList.h>namespace Web::UIEvents {GC_DEFINE_ALLOCATOR(TouchList);-GC::Ref<TouchList> TouchList::create(GC::ConservativeVector<GC::Ref<Touch>> touches)+GC::Ref<TouchList> TouchList::create(GC::RootVector<GC::Ref<Touch>> touches){return GC::Heap::the().allocate<TouchList>(move(touches));}-TouchList::TouchList(GC::ConservativeVector<GC::Ref<Touch>> touches)+TouchList::TouchList(GC::RootVector<GC::Ref<Touch>> touches): m_touches(move(touches)){}diff --git a/Libraries/LibWeb/UIEvents/TouchList.h b/Libraries/LibWeb/UIEvents/TouchList.hindex cb03e5c9cde..78c3dad0d2f 100644--- a/Libraries/LibWeb/UIEvents/TouchList.h+++ b/Libraries/LibWeb/UIEvents/TouchList.h@@ -6,7 +6,7 @@#pragma once-#include <LibGC/ConservativeVector.h>+#include <LibGC/RootVector.h>#include <LibWeb/Bindings/Wrappable.h>#include <LibWeb/UIEvents/Touch.h>@@ -17,12 +17,12 @@ class TouchList final : public Bindings::GCAllocatedWrappable {GC_DECLARE_ALLOCATOR(TouchList);public:- static GC::Ref<TouchList> create(GC::ConservativeVector<GC::Ref<Touch>>);+ static GC::Ref<TouchList> create(GC::RootVector<GC::Ref<Touch>>);u32 length() const { return m_touches.size(); }GC::Ptr<Touch> item(u32 index) const { return index < m_touches.size() ? m_touches[index].ptr() : nullptr; }private:- explicit TouchList(GC::ConservativeVector<GC::Ref<Touch>>);+ explicit TouchList(GC::RootVector<GC::Ref<Touch>>);virtual void visit_edges(GC::Cell::Visitor&) override;Vector<GC::Ref<Touch>> m_touches;diff --git a/Tests/LibGC/TestGCContainers.cpp b/Tests/LibGC/TestGCContainers.cppindex 38e5b5f36b1..94ac7e65267 100644--- a/Tests/LibGC/TestGCContainers.cpp+++ b/Tests/LibGC/TestGCContainers.cpp@@ -32,6 +32,40 @@ class TestCell : public GC::Cell {GC_DEFINE_ALLOCATOR(TestCell);+// Probe the public container type, including aliases and incomplete pointees, rather than its implementation trait.+template<typename T>+concept ConservativeVectorAccepts = requires { typename GC::ConservativeVector<T>; };++class ForwardDeclaredCell;+using AliasedCellPtr = GC::Ptr<ForwardDeclaredCell>;+using AliasedCellRef = GC::Ref<ForwardDeclaredCell>;+struct ConservativeRecord {+ GC::Ref<TestCell> cell;+ FlatPtr payload;+};++static_assert(ConservativeVectorAccepts<FlatPtr>);+static_assert(ConservativeVectorAccepts<TestCell*>);+static_assert(ConservativeVectorAccepts<ConservativeRecord>);+static_assert(!ConservativeVectorAccepts<GC::Ptr<TestCell>>);+static_assert(!ConservativeVectorAccepts<GC::Ref<TestCell>>);+static_assert(!ConservativeVectorAccepts<GC::Ptr<TestCell const>>);+static_assert(!ConservativeVectorAccepts<GC::Ref<TestCell const>>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr const>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef volatile>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr volatile>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef const>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr const volatile>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef const volatile>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr&>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef&>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr const&>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef const&>);+static_assert(!ConservativeVectorAccepts<AliasedCellPtr&&>);+static_assert(!ConservativeVectorAccepts<AliasedCellRef&&>);+class FinalizerObservingCell : public GC::Cell {GC_CELL(FinalizerObservingCell, GC::Cell);GC_DECLARE_ALLOCATOR(FinalizerObservingCell);@@ -257,14 +291,39 @@ TEST_CASE(root_vector_adopts_moved_vector)EXPECT_EQ(roots.size(), 1u);}+TEST_CASE(root_vector_native_member_survives_relocation)+{+ auto& heap = test_heap();+ struct NativeState {+ GC::RootVector<GC::Ref<TestCell>> cells;+ };+ Vector<NativeState> states;+ states.append(NativeState {});+ auto cell = heap.allocate<TestCell>();+ states[0].cells.append(cell);++ // Moving the enclosing native storage must transfer the member's registration in the heap's root list.+ states.ensure_capacity(states.capacity() + 1);+ heap.collect_garbage();++ HashMap<GC::Cell*, GC::HeapRoot> roots;+ states[0].cells.gather_roots(roots);+ EXPECT(roots.contains(cell.ptr()));+ EXPECT_EQ(roots.size(), 1u);++ // Destruction must unregister the member before the collector next walks its root list.+ states.clear();+ heap.collect_garbage();+}+TEST_CASE(conservative_vector_adopts_moved_vector){auto& heap = test_heap();- Vector<GC::Ref<TestCell>> vector;+ Vector<FlatPtr> vector;auto cell = heap.allocate<TestCell>();- vector.append(cell);- GC::ConservativeVector<GC::Ref<TestCell>> conservative_vector { move(vector) };+ vector.append(bit_cast<FlatPtr>(cell.ptr()));+ GC::ConservativeVector<FlatPtr> conservative_vector { move(vector) };EXPECT(possible_values_contain(conservative_vector, cell.ptr()));}@@ -272,10 +331,10 @@ TEST_CASE(conservative_vector_adopts_moved_vector)TEST_CASE(conservative_vector_reports_possible_values){auto& heap = test_heap();- GC::ConservativeVector<GC::Ref<TestCell>> vector;+ GC::ConservativeVector<ConservativeRecord> vector;auto cell = heap.allocate<TestCell>();- vector.append(cell);+ vector.append({ cell, 0x1234 });EXPECT(possible_values_contain(vector, cell.ptr()));}