Skip to content

Truly independent web browser

LibGC: Keep roots out of captured values

Andreas Kling3773e7f86d7b
diff --git a/Libraries/LibGC/CaptureTraits.h b/Libraries/LibGC/CaptureTraits.h
index fd817e3e244..ccb39b12838 100644
--- a/Libraries/LibGC/CaptureTraits.h
+++ b/Libraries/LibGC/CaptureTraits.h
@@ -65,14 +65,6 @@ struct IsUntracedCapture<AK::Detail::ByteBuffer<inline_capacity>> : TrueType { }
template<typename T>
struct IsUntracedCapture<Weak<T>> : TrueType { };
-// NB: A root traces itself, so a value that holds one, such as a generated dictionary, holds nothing for its owner to
-// trace. GC::captures() of a root itself holds it as a pointer instead, so that a function never holds a root.
-template<typename T>
-struct IsUntracedCapture<Root<T>> : TrueType { };
-
-template<typename T, size_t inline_capacity>
-struct IsUntracedCapture<RootVector<T, inline_capacity>> : TrueType { };
-
template<typename T>
requires(!IsCellLike<RemoveCV<T>>)
struct IsUntracedCapture<AK::WeakPtr<T>> : TrueType { };
@@ -180,11 +172,23 @@ template<typename T>
requires(!IsCellLike<RemoveCV<T>>)
struct IsTracedCapture<AK::NonnullRefPtr<T>> : IsTracedCapture<RemoveCV<T>> { };
+template<typename T>
+inline constexpr bool IsRoot = false;
+
+template<typename T>
+inline constexpr bool IsRoot<Root<T>> = true;
+
+template<typename T, size_t inline_capacity>
+inline constexpr bool IsRoot<RootVector<T, inline_capacity>> = true;
+
// Visits a member of a value type, such as a generated dictionary, from its visit_edges(). Every member must be traced or
// hold no cell edges, so that the value can be captured by a GC::Function.
+// NB: A root member would keep what it names alive for as long as the function lives, even through a cycle back to the
+// function, so a value with one isn't capturable.
template<typename Visitor, typename T>
void visit_capture_member(Visitor& visitor, T const& member)
{
+ static_assert(!IsRoot<T>, "A captured value can't hold a root");
static_assert(IsCapturable<T>, "A member of a captured value must be traced, or hold no cell edges");
if constexpr (IsVisitable<T>::value)
visitor.visit(member);
diff --git a/Tests/LibGC/TestGCFunction.cpp b/Tests/LibGC/TestGCFunction.cpp
index 92ed1918407..9f10788f742 100644
--- a/Tests/LibGC/TestGCFunction.cpp
+++ b/Tests/LibGC/TestGCFunction.cpp
@@ -101,6 +101,12 @@ static_assert(GC::IsCapturable<OwnPtr<CountedWithoutCells>>);
static_assert(GC::IsCapturable<NonnullRefPtr<CountedAndTraced>>);
static_assert(GC::IsCapturable<Optional<RefPtr<CountedAndTraced>>>);
+// A root is only capturable on its own, which holds it as a pointer, so that a function never keeps a root.
+static_assert(!GC::IsCapturable<GC::Root<TestCell>>);
+static_assert(!GC::IsCapturable<Optional<GC::Root<TestCell>>>);
+static_assert(!GC::IsCapturable<Vector<GC::Root<TestCell>>>);
+static_assert(IsSame<GC::Detail::CaptureStorage<GC::Root<TestCell>&>::Type, GC::Detail::CapturedRoot<TestCell>>);
+
struct Holders {
GC::Root<GC::Function<int()>> function;
GC::Weak<TestCell> cell;