| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// GC Policy Mechanism |
| 8 |
|
| 9 |
// GCPolicy controls how the GC interacts with a given type for functionality |
| 10 |
// that is used by generic code. It is implemented for: |
| 11 |
// |
| 12 |
// - direct pointers to GC things, e.g. JSObject* or JSString* |
| 13 |
// |
| 14 |
// - tagged and/or optional pointers to GC things, e.g. JS::Value or jsid |
| 15 |
// |
| 16 |
// - structures containing GC pointers, e.g. JS::PropertyDescriptor |
| 17 |
// |
| 18 |
// - C++ container types, e.g. GCHashMap |
| 19 |
// |
| 20 |
// The GCPolicy for type |T| provides at a minimum: |
| 21 |
// |
| 22 |
// static void trace(JSTracer, T* tp, const char* name) |
| 23 |
// |
| 24 |
// Trace the edge |*tp|, calling the edge |name|. Generic containers |
| 25 |
// like GCHashMap and GCHashSet use this method to trace their children. |
| 26 |
// |
| 27 |
// static bool traceWeak(T* tp) |
| 28 |
// |
| 29 |
// Update any GC edges if their target has been moved. Remove or clear any |
| 30 |
// edges to GC things that are going to be collected by an incremental |
| 31 |
// GC. Return false if this edge itself should be removed. |
| 32 |
// |
| 33 |
// For GC thing pointers, this will clear the edge and return false if the |
| 34 |
// target is going to be collected. In general, for structures this should |
| 35 |
// call |traceWeak| on internal GC edges and return whether the result was |
| 36 |
// true for all of them. |
| 37 |
// |
| 38 |
// Containers can use this to remove entries containing GC things that are |
| 39 |
// going to be collected (e.g. GCVector). |
| 40 |
// |
| 41 |
// static bool isValid(const T& t) |
| 42 |
// |
| 43 |
// Check that |t| is valid and is not corrupt in some way. The built-in GC |
| 44 |
// types do some memory layout checks. This is for assertions only; it is ok |
| 45 |
// to always return true. |
| 46 |
// |
| 47 |
// The GCPolicy may also provide: |
| 48 |
// |
| 49 |
// static bool needsSweep(const T* tp) |
| 50 |
// |
| 51 |
// Return whether this edge should be removed, like a version of |traceWeak| |
| 52 |
// with the sense of the return value reversed. |
| 53 |
// |
| 54 |
// The argument is const and this does not update any moved GC pointers, so |
| 55 |
// should not be called when this is a possibility. |
| 56 |
// |
| 57 |
// This is used internally for incremental barriers on WeakCache hash |
| 58 |
// tables. |
| 59 |
// |
| 60 |
// The default GCPolicy<T> assumes that T has a default constructor and |trace| |
| 61 |
// and |traceWeak| methods, and forwards to them. GCPolicy has appropriate |
| 62 |
// specializations for pointers to GC things and pointer-like types like |
| 63 |
// JS::Heap<T> and mozilla::UniquePtr<T>. |
| 64 |
// |
| 65 |
// There are some stock structs your specializations can inherit from. |
| 66 |
// IgnoreGCPolicy<T> does nothing. StructGCPolicy<T> forwards the methods to the |
| 67 |
// referent type T. |
| 68 |
|
| 69 |
#ifndef GCPolicyAPI_h |
| 70 |
#define GCPolicyAPI_h |
| 71 |
|
| 72 |
#include "mozilla/Maybe.h" |
| 73 |
#include "mozilla/UniquePtr.h" |
| 74 |
|
| 75 |
#include <type_traits> |
| 76 |
|
| 77 |
#include "js/GCTypeMacros.h" // JS_FOR_EACH_PUBLIC_GC_POINTER_TYPE |
| 78 |
#include "js/TraceKind.h" |
| 79 |
#include "js/TracingAPI.h" |
| 80 |
#include "js/TypeDecls.h" |
| 81 |
|
| 82 |
namespace JS { |
| 83 |
|
| 84 |
// Defines a policy for container types with non-GC, i.e. C storage. This |
| 85 |
// policy dispatches to the underlying struct for GC interactions. Note that |
| 86 |
// currently a type can define only the subset of the methods (trace and/or |
| 87 |
// traceWeak) if it is never used in a context that requires the other. |
| 88 |
template <typename T> |
| 89 |
struct StructGCPolicy { |
| 90 |
static_assert(!std::is_pointer_v<T>, |
| 91 |
"Pointer type not allowed for StructGCPolicy"); |
| 92 |
|
| 93 |
static void trace(JSTracer* trc, T* tp, const char* name) { tp->trace(trc); } |
| 94 |
|
| 95 |
static bool traceWeak(JSTracer* trc, T* tp) { return tp->traceWeak(trc); } |
| 96 |
static bool needsSweep(JSTracer* trc, const T* tp) { |
| 97 |
return tp->needsSweep(trc); |
| 98 |
} |
| 99 |
|
| 100 |
static bool isValid(const T& tp) { return true; } |
| 101 |
}; |
| 102 |
|
| 103 |
// The default GC policy attempts to defer to methods on the underlying type. |
| 104 |
// Most C++ structures that contain a default constructor, a trace function and |
| 105 |
// a sweep function will work out of the box with Rooted, Handle, GCVector, |
| 106 |
// and GCHash{Set,Map}. |
| 107 |
template <typename T> |
| 108 |
struct GCPolicy : public StructGCPolicy<T> {}; |
| 109 |
|
| 110 |
// This policy ignores any GC interaction, e.g. for non-GC types. |
| 111 |
template <typename T> |
| 112 |
struct IgnoreGCPolicy { |
| 113 |
static void trace(JSTracer* trc, T* t, const char* name) {} |
| 114 |
static bool traceWeak(JSTracer*, T* v) { return true; } |
| 115 |
static bool needsSweep(JSTracer* trc, const T* v) { return false; } |
| 116 |
static bool isValid(const T& v) { return true; } |
| 117 |
}; |
| 118 |
template <> |
| 119 |
struct GCPolicy<uint32_t> : public IgnoreGCPolicy<uint32_t> {}; |
| 120 |
template <> |
| 121 |
struct GCPolicy<uint64_t> : public IgnoreGCPolicy<uint64_t> {}; |
| 122 |
template <> |
| 123 |
struct GCPolicy<bool> : public IgnoreGCPolicy<bool> {}; |
| 124 |
|
| 125 |
template <typename T> |
| 126 |
struct GCPointerPolicy { |
| 127 |
static_assert(std::is_pointer_v<T>, |
| 128 |
"Non-pointer type not allowed for GCPointerPolicy"); |
| 129 |
|
| 130 |
static void trace(JSTracer* trc, T* vp, const char* name) { |
| 131 |
// This should only be called as part of root marking since that's the only |
| 132 |
// time we should trace unbarriered GC thing pointers. This will assert if |
| 133 |
// called at other times. |
| 134 |
TraceRoot(trc, vp, name); |
| 135 |
} |
| 136 |
static bool isTenured(T v) { return !v || !js::gc::IsInsideNursery(v); } |
| 137 |
static bool isValid(T v) { return js::gc::IsCellPointerValidOrNull(v); } |
| 138 |
}; |
| 139 |
#define EXPAND_SPECIALIZE_GCPOLICY(Type) \ |
| 140 |
template <> \ |
| 141 |
struct GCPolicy<Type> : public GCPointerPolicy<Type> {}; \ |
| 142 |
template <> \ |
| 143 |
struct GCPolicy<Type const> : public GCPointerPolicy<Type const> {}; |
| 144 |
JS_FOR_EACH_PUBLIC_GC_POINTER_TYPE(EXPAND_SPECIALIZE_GCPOLICY) |
| 145 |
#undef EXPAND_SPECIALIZE_GCPOLICY |
| 146 |
|
| 147 |
template <typename T> |
| 148 |
struct NonGCPointerPolicy { |
| 149 |
static void trace(JSTracer* trc, T* vp, const char* name) { |
| 150 |
if (*vp) { |
| 151 |
(*vp)->trace(trc); |
| 152 |
} |
| 153 |
} |
| 154 |
static bool traceWeak(JSTracer* trc, T* vp) { |
| 155 |
return !*vp || (*vp)->traceWeak(trc); |
| 156 |
} |
| 157 |
static bool isValid(T v) { return true; } |
| 158 |
}; |
| 159 |
|
| 160 |
template <typename T> |
| 161 |
struct GCPolicy<JS::Heap<T>> { |
| 162 |
static void trace(JSTracer* trc, JS::Heap<T>* thingp, const char* name) { |
| 163 |
TraceEdge(trc, thingp, name); |
| 164 |
} |
| 165 |
static bool traceWeak(JSTracer* trc, JS::Heap<T>* thingp) { |
| 166 |
return !*thingp || js::gc::TraceWeakEdge(trc, thingp); |
| 167 |
} |
| 168 |
static bool needsSweep(JSTracer* trc, const JS::Heap<T>* thingp) { |
| 169 |
T* thing = const_cast<T*>(thingp->unsafeAddress()); |
| 170 |
return thing && js::gc::EdgeNeedsSweepUnbarrieredSlow(thing); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
// GCPolicy<UniquePtr<T>> forwards the contained pointer to GCPolicy<T>. |
| 175 |
template <typename T, typename D> |
| 176 |
struct GCPolicy<mozilla::UniquePtr<T, D>> { |
| 177 |
static void trace(JSTracer* trc, mozilla::UniquePtr<T, D>* tp, |
| 178 |
const char* name) { |
| 179 |
if (tp->get()) { |
| 180 |
GCPolicy<T>::trace(trc, tp->get(), name); |
| 181 |
} |
| 182 |
} |
| 183 |
static bool traceWeak(JSTracer* trc, mozilla::UniquePtr<T, D>* tp) { |
| 184 |
return !tp->get() || GCPolicy<T>::traceWeak(trc, tp->get()); |
| 185 |
} |
| 186 |
static bool needsSweep(JSTracer* trc, const mozilla::UniquePtr<T, D>* tp) { |
| 187 |
return tp->get() && GCPolicy<T>::needsSweep(trc, tp->get()); |
| 188 |
} |
| 189 |
static bool isValid(const mozilla::UniquePtr<T, D>& t) { |
| 190 |
return !t.get() || GCPolicy<T>::isValid(*t.get()); |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
template <> |
| 195 |
struct GCPolicy<mozilla::Nothing> : public IgnoreGCPolicy<mozilla::Nothing> {}; |
| 196 |
|
| 197 |
// GCPolicy<Maybe<T>> forwards tracing/sweeping to GCPolicy<T*> if |
| 198 |
// the Maybe<T> is filled and T* can be traced via GCPolicy<T*>. |
| 199 |
template <typename T> |
| 200 |
struct GCPolicy<mozilla::Maybe<T>> { |
| 201 |
static void trace(JSTracer* trc, mozilla::Maybe<T>* tp, const char* name) { |
| 202 |
if (tp->isSome()) { |
| 203 |
GCPolicy<T>::trace(trc, tp->ptr(), name); |
| 204 |
} |
| 205 |
} |
| 206 |
static bool traceWeak(JSTracer* trc, mozilla::Maybe<T>* tp) { |
| 207 |
return tp->isNothing() || GCPolicy<T>::traceWeak(trc, tp->ptr()); |
| 208 |
} |
| 209 |
static bool needsSweep(JSTracer* trc, const mozilla::Maybe<T>* tp) { |
| 210 |
return tp->isSome() && GCPolicy<T>::needsSweep(trc, tp->ptr()); |
| 211 |
} |
| 212 |
static bool isValid(const mozilla::Maybe<T>& t) { |
| 213 |
return t.isNothing() || GCPolicy<T>::isValid(t.ref()); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
template <typename T1, typename T2> |
| 218 |
struct GCPolicy<std::pair<T1, T2>> { |
| 219 |
static void trace(JSTracer* trc, std::pair<T1, T2>* tp, const char* name) { |
| 220 |
GCPolicy<T1>::trace(trc, &tp->first, name); |
| 221 |
GCPolicy<T2>::trace(trc, &tp->second, name); |
| 222 |
} |
| 223 |
static bool traceWeak(JSTracer* trc, std::pair<T1, T2>* tp) { |
| 224 |
return GCPolicy<T1>::traceWeak(trc, &tp->first) && |
| 225 |
GCPolicy<T2>::traceWeak(trc, &tp->second); |
| 226 |
} |
| 227 |
static bool needsSweep(JSTracer* trc, const std::pair<T1, T2>* tp) { |
| 228 |
return GCPolicy<T1>::needsSweep(trc, &tp->first) || |
| 229 |
GCPolicy<T2>::needsSweep(trc, &tp->second); |
| 230 |
} |
| 231 |
static bool isValid(const std::pair<T1, T2>& t) { |
| 232 |
return GCPolicy<T1>::isValid(t.first) && GCPolicy<T2>::isValid(t.second); |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
template <> |
| 237 |
struct GCPolicy<JS::Realm*>; // see Realm.h |
| 238 |
|
| 239 |
template <> |
| 240 |
struct GCPolicy<mozilla::Ok> : public IgnoreGCPolicy<mozilla::Ok> {}; |
| 241 |
|
| 242 |
template <typename V, typename E> |
| 243 |
struct GCPolicy<mozilla::Result<V, E>> { |
| 244 |
static void trace(JSTracer* trc, mozilla::Result<V, E>* tp, |
| 245 |
const char* name) { |
| 246 |
if (tp->isOk()) { |
| 247 |
V tmp = tp->unwrap(); |
| 248 |
JS::GCPolicy<V>::trace(trc, &tmp, "Result value"); |
| 249 |
tp->updateAfterTracing(std::move(tmp)); |
| 250 |
} |
| 251 |
|
| 252 |
if (tp->isErr()) { |
| 253 |
E tmp = tp->unwrapErr(); |
| 254 |
JS::GCPolicy<E>::trace(trc, &tmp, "Result error"); |
| 255 |
tp->updateErrorAfterTracing(std::move(tmp)); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
static bool isValid(const mozilla::Result<V, E>& t) { return true; } |
| 260 |
}; |
| 261 |
|
| 262 |
template <typename... Fs> |
| 263 |
struct GCPolicy<std::tuple<Fs...>> { |
| 264 |
using T = std::tuple<Fs...>; |
| 265 |
static void trace(JSTracer* trc, T* tp, const char* name) { |
| 266 |
traceFieldsFrom<0>(trc, *tp, name); |
| 267 |
} |
| 268 |
static bool isValid(const T& t) { return areFieldsValidFrom<0>(t); } |
| 269 |
|
| 270 |
private: |
| 271 |
template <size_t N> |
| 272 |
static void traceFieldsFrom(JSTracer* trc, T& tuple, const char* name) { |
| 273 |
if constexpr (N != std::tuple_size_v<T>) { |
| 274 |
using F = std::tuple_element_t<N, T>; |
| 275 |
GCPolicy<F>::trace(trc, &std::get<N>(tuple), name); |
| 276 |
traceFieldsFrom<N + 1>(trc, tuple, name); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
template <size_t N> |
| 281 |
static bool areFieldsValidFrom(const T& tuple) { |
| 282 |
if constexpr (N != std::tuple_size_v<T>) { |
| 283 |
using F = std::tuple_element_t<N, T>; |
| 284 |
return GCPolicy<F>::isValid(std::get<N>(tuple)) && |
| 285 |
areFieldsValidFrom<N + 1>(tuple); |
| 286 |
} |
| 287 |
|
| 288 |
return true; |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
} // namespace JS |
| 293 |
|
| 294 |
#endif // GCPolicyAPI_h |