| 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 |
#ifndef js_Tracer_h |
| 8 |
#define js_Tracer_h |
| 9 |
|
| 10 |
#include "gc/Allocator.h" |
| 11 |
#include "gc/Barrier.h" |
| 12 |
#include "gc/TraceKind.h" |
| 13 |
#include "js/HashTable.h" |
| 14 |
#include "js/TracingAPI.h" |
| 15 |
|
| 16 |
namespace JS { |
| 17 |
using CompartmentSet = |
| 18 |
js::HashSet<Compartment*, js::DefaultHasher<Compartment*>, |
| 19 |
js::SystemAllocPolicy>; |
| 20 |
} // namespace JS |
| 21 |
|
| 22 |
namespace js { |
| 23 |
|
| 24 |
class TaggedProto; |
| 25 |
namespace wasm { |
| 26 |
class AnyRef; |
| 27 |
} // namespace wasm |
| 28 |
|
| 29 |
// Internal Tracing API |
| 30 |
// |
| 31 |
// Tracing is an abstract visitation of each edge in a JS heap graph.[1] The |
| 32 |
// most common (and performance sensitive) use of this infrastructure is for GC |
| 33 |
// "marking" as part of the mark-and-sweep collector; however, this |
| 34 |
// infrastructure is much more general than that and is used for many other |
| 35 |
// purposes as well. |
| 36 |
// |
| 37 |
// One commonly misunderstood subtlety of the tracing architecture is the role |
| 38 |
// of graph vertices versus graph edges. Graph vertices are the heap |
| 39 |
// allocations -- GC things -- that are returned by Allocate. Graph edges are |
| 40 |
// pointers -- including tagged pointers like Value and jsid -- that link the |
| 41 |
// allocations into a complex heap. The tracing API deals *only* with edges. |
| 42 |
// Any action taken on the target of a graph edge is independent of the tracing |
| 43 |
// itself. |
| 44 |
// |
| 45 |
// Another common misunderstanding relates to the role of the JSTracer. The |
| 46 |
// JSTracer instance determines what tracing does when visiting an edge; it |
| 47 |
// does not itself participate in the tracing process, other than to be passed |
| 48 |
// through as opaque data. It works like a closure in that respect. |
| 49 |
// |
| 50 |
// Tracing implementations internal to SpiderMonkey should use these interfaces |
| 51 |
// instead of the public interfaces in js/TracingAPI.h. Unlike the public |
| 52 |
// tracing methods, these work on internal types and avoid an external call. |
| 53 |
// |
| 54 |
// Note that the implementations for these methods are, surprisingly, in |
| 55 |
// js/src/gc/Marking.cpp. This is so that the compiler can inline as much as |
| 56 |
// possible in the common, marking pathways. Conceptually, however, they remain |
| 57 |
// as part of the generic "tracing" architecture, rather than the more specific |
| 58 |
// marking implementation of tracing. |
| 59 |
// |
| 60 |
// 1 - In SpiderMonkey, we call this concept tracing rather than visiting |
| 61 |
// because "visiting" is already used by the compiler. Also, it's been |
| 62 |
// called "tracing" forever and changing it would be extremely difficult at |
| 63 |
// this point. |
| 64 |
|
| 65 |
class GCMarker; |
| 66 |
|
| 67 |
// Debugging functions to check tracing invariants. |
| 68 |
#ifdef DEBUG |
| 69 |
template <typename T> |
| 70 |
void CheckTracedThing(JSTracer* trc, T* thing); |
| 71 |
template <typename T> |
| 72 |
void CheckTracedThing(JSTracer* trc, const T& thing); |
| 73 |
#else |
| 74 |
template <typename T> |
| 75 |
inline void CheckTracedThing(JSTracer* trc, T* thing) {} |
| 76 |
template <typename T> |
| 77 |
inline void CheckTracedThing(JSTracer* trc, const T& thing) {} |
| 78 |
#endif |
| 79 |
|
| 80 |
namespace gc { |
| 81 |
|
| 82 |
// Our barrier templates are parameterized on the pointer types so that we can |
| 83 |
// share the definitions with Value and jsid. Thus, we need to strip the |
| 84 |
// pointer before sending the type to BaseGCType and re-add it on the other |
| 85 |
// side. As such: |
| 86 |
template <typename T> |
| 87 |
struct PtrBaseGCType { |
| 88 |
using type = T; |
| 89 |
}; |
| 90 |
template <typename T> |
| 91 |
struct PtrBaseGCType<T*> { |
| 92 |
using type = typename BaseGCType<T>::type*; |
| 93 |
}; |
| 94 |
|
| 95 |
// Cast a possibly-derived T** pointer to a base class pointer. |
| 96 |
template <typename T> |
| 97 |
typename PtrBaseGCType<T>::type* ConvertToBase(T* thingp) { |
| 98 |
return reinterpret_cast<typename PtrBaseGCType<T>::type*>(thingp); |
| 99 |
} |
| 100 |
|
| 101 |
// Internal methods to trace edges. |
| 102 |
|
| 103 |
#define DEFINE_TRACE_FUNCTION(name, type, _1, _2) \ |
| 104 |
MOZ_ALWAYS_INLINE bool TraceEdgeInternal(JSTracer* trc, type** thingp, \ |
| 105 |
const char* name) { \ |
| 106 |
CheckTracedThing(trc, *thingp); \ |
| 107 |
trc->on##name##Edge(thingp, name); \ |
| 108 |
return *thingp; \ |
| 109 |
} |
| 110 |
JS_FOR_EACH_TRACEKIND(DEFINE_TRACE_FUNCTION) |
| 111 |
#undef DEFINE_TRACE_FUNCTION |
| 112 |
|
| 113 |
bool TraceEdgeInternal(JSTracer* trc, Value* thingp, const char* name); |
| 114 |
bool TraceEdgeInternal(JSTracer* trc, jsid* thingp, const char* name); |
| 115 |
bool TraceEdgeInternal(JSTracer* trc, TaggedProto* thingp, const char* name); |
| 116 |
bool TraceEdgeInternal(JSTracer* trc, wasm::AnyRef* thingp, const char* name); |
| 117 |
|
| 118 |
template <typename T> |
| 119 |
void TraceRangeInternal(JSTracer* trc, size_t len, T* vec, const char* name); |
| 120 |
template <typename T> |
| 121 |
bool TraceWeakMapKeyInternal(JSTracer* trc, Zone* zone, T* thingp, |
| 122 |
const char* name); |
| 123 |
|
| 124 |
#ifdef DEBUG |
| 125 |
void AssertRootMarkingPhase(JSTracer* trc); |
| 126 |
template <typename T> |
| 127 |
void AssertShouldMarkInZone(GCMarker* marker, T* thing); |
| 128 |
#else |
| 129 |
inline void AssertRootMarkingPhase(JSTracer* trc) {} |
| 130 |
template <typename T> |
| 131 |
void AssertShouldMarkInZone(GCMarker* marker, T* thing) {} |
| 132 |
#endif |
| 133 |
|
| 134 |
} // namespace gc |
| 135 |
|
| 136 |
// Trace through a strong edge in the live object graph on behalf of |
| 137 |
// tracing. The effect of tracing the edge depends on the JSTracer being |
| 138 |
// used. For pointer types, |*thingp| must not be null. |
| 139 |
// |
| 140 |
// Note that weak edges are handled separately. GC things with weak edges must |
| 141 |
// not trace those edges during marking tracing (which would keep the referent |
| 142 |
// alive) but instead arrange for the edge to be swept by calling |
| 143 |
// js::gc::IsAboutToBeFinalized or TraceWeakEdge during sweeping. |
| 144 |
// |
| 145 |
// GC things that are weakly held in containers can use WeakMap or a container |
| 146 |
// wrapped in the WeakCache<> template to perform the appropriate sweeping. |
| 147 |
|
| 148 |
template <typename T> |
| 149 |
inline void TraceEdge(JSTracer* trc, const WriteBarriered<T>* thingp, |
| 150 |
const char* name) { |
| 151 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp->unbarrieredAddress()), |
| 152 |
name); |
| 153 |
} |
| 154 |
|
| 155 |
template <typename T> |
| 156 |
inline void TraceEdge(JSTracer* trc, WeakHeapPtr<T>* thingp, const char* name) { |
| 157 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp->unbarrieredAddress()), |
| 158 |
name); |
| 159 |
} |
| 160 |
|
| 161 |
template <class BC, class T> |
| 162 |
inline void TraceCellHeaderEdge(JSTracer* trc, |
| 163 |
gc::CellWithTenuredGCPointer<BC, T>* thingp, |
| 164 |
const char* name) { |
| 165 |
T* thing = thingp->headerPtr(); |
| 166 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(&thing), name); |
| 167 |
if (thing != thingp->headerPtr()) { |
| 168 |
thingp->unbarrieredSetHeaderPtr(thing); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
template <class T> |
| 173 |
inline void TraceCellHeaderEdge(JSTracer* trc, |
| 174 |
gc::TenuredCellWithGCPointer<T>* thingp, |
| 175 |
const char* name) { |
| 176 |
T* thing = thingp->headerPtr(); |
| 177 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(&thing), name); |
| 178 |
if (thing != thingp->headerPtr()) { |
| 179 |
thingp->unbarrieredSetHeaderPtr(thing); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// Trace through a possibly-null edge in the live object graph on behalf of |
| 184 |
// tracing. |
| 185 |
|
| 186 |
template <typename T> |
| 187 |
inline void TraceNullableEdge(JSTracer* trc, const WriteBarriered<T>* thingp, |
| 188 |
const char* name) { |
| 189 |
if (InternalBarrierMethods<T>::isMarkable(thingp->get())) { |
| 190 |
TraceEdge(trc, thingp, name); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
template <typename T> |
| 195 |
inline void TraceNullableEdge(JSTracer* trc, WeakHeapPtr<T>* thingp, |
| 196 |
const char* name) { |
| 197 |
if (InternalBarrierMethods<T>::isMarkable(thingp->unbarrieredGet())) { |
| 198 |
TraceEdge(trc, thingp, name); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
template <class BC, class T> |
| 203 |
inline void TraceNullableCellHeaderEdge( |
| 204 |
JSTracer* trc, gc::CellWithTenuredGCPointer<BC, T>* thingp, |
| 205 |
const char* name) { |
| 206 |
T* thing = thingp->headerPtr(); |
| 207 |
if (thing) { |
| 208 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(&thing), name); |
| 209 |
if (thing != thingp->headerPtr()) { |
| 210 |
thingp->unbarrieredSetHeaderPtr(thing); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Trace through a "root" edge. These edges are the initial edges in the object |
| 216 |
// graph traversal. Root edges are asserted to only be traversed in the initial |
| 217 |
// phase of a GC. |
| 218 |
|
| 219 |
template <typename T> |
| 220 |
inline void TraceRoot(JSTracer* trc, T* thingp, const char* name) { |
| 221 |
gc::AssertRootMarkingPhase(trc); |
| 222 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp), name); |
| 223 |
} |
| 224 |
|
| 225 |
template <typename T> |
| 226 |
inline void TraceRoot(JSTracer* trc, const HeapPtr<T>* thingp, |
| 227 |
const char* name) { |
| 228 |
TraceRoot(trc, thingp->unbarrieredAddress(), name); |
| 229 |
} |
| 230 |
|
| 231 |
// Idential to TraceRoot, except that this variant will not crash if |*thingp| |
| 232 |
// is null. |
| 233 |
|
| 234 |
template <typename T> |
| 235 |
inline void TraceNullableRoot(JSTracer* trc, T* thingp, const char* name) { |
| 236 |
gc::AssertRootMarkingPhase(trc); |
| 237 |
if (InternalBarrierMethods<T>::isMarkable(*thingp)) { |
| 238 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp), name); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
template <typename T> |
| 243 |
inline void TraceNullableRoot(JSTracer* trc, WeakHeapPtr<T>* thingp, |
| 244 |
const char* name) { |
| 245 |
TraceNullableRoot(trc, thingp->unbarrieredAddress(), name); |
| 246 |
} |
| 247 |
|
| 248 |
// Like TraceEdge, but for edges that do not use one of the automatic barrier |
| 249 |
// classes and, thus, must be treated specially for moving GC. This method is |
| 250 |
// separate from TraceEdge to make accidental use of such edges more obvious. |
| 251 |
|
| 252 |
template <typename T> |
| 253 |
inline void TraceManuallyBarrieredEdge(JSTracer* trc, T* thingp, |
| 254 |
const char* name) { |
| 255 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp), name); |
| 256 |
} |
| 257 |
|
| 258 |
template <typename T> |
| 259 |
inline void TraceManuallyBarrieredNullableEdge(JSTracer* trc, T* thingp, |
| 260 |
const char* name) { |
| 261 |
if (InternalBarrierMethods<T>::isMarkable(*thingp)) { |
| 262 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp), name); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// Trace through a weak edge. If *thingp is not marked at the end of marking, |
| 267 |
// it is replaced by nullptr, and this method will return false to indicate that |
| 268 |
// the edge no longer exists. |
| 269 |
template <typename T> |
| 270 |
inline bool TraceManuallyBarrieredWeakEdge(JSTracer* trc, T* thingp, |
| 271 |
const char* name) { |
| 272 |
return gc::TraceEdgeInternal(trc, gc::ConvertToBase(thingp), name); |
| 273 |
} |
| 274 |
|
| 275 |
// The result of tracing a weak edge, which can be either: |
| 276 |
// |
| 277 |
// - the target is dead (and the edge has been cleared), or |
| 278 |
// - the target is alive (and the edge may have been updated) |
| 279 |
// |
| 280 |
// This includes the initial and final values of the edge to allow cleanup if |
| 281 |
// the target is dead or access to the referent if it is alive. |
| 282 |
template <typename T> |
| 283 |
struct TraceWeakResult { |
| 284 |
const bool live_; |
| 285 |
const T initial_; |
| 286 |
const T final_; |
| 287 |
|
| 288 |
bool isLive() const { return live_; } |
| 289 |
bool isDead() const { return !live_; } |
| 290 |
bool wasMoved() const { return isLive() && final_ != initial_; } |
| 291 |
|
| 292 |
MOZ_IMPLICIT operator bool() const { return isLive(); } |
| 293 |
|
| 294 |
T initialTarget() const { |
| 295 |
MOZ_ASSERT(isDead()); |
| 296 |
return initial_; |
| 297 |
} |
| 298 |
|
| 299 |
T finalTarget() const { |
| 300 |
MOZ_ASSERT(isLive()); |
| 301 |
return final_; |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
template <typename T> |
| 306 |
inline TraceWeakResult<T> TraceWeakEdge(JSTracer* trc, BarrieredBase<T>* thingp, |
| 307 |
const char* name) { |
| 308 |
T* addr = thingp->unbarrieredAddress(); |
| 309 |
T initial = *addr; |
| 310 |
bool live = !InternalBarrierMethods<T>::isMarkable(initial) || |
| 311 |
gc::TraceEdgeInternal(trc, gc::ConvertToBase(addr), name); |
| 312 |
return TraceWeakResult<T>{live, initial, *addr}; |
| 313 |
} |
| 314 |
|
| 315 |
// Trace all edges contained in the given array. |
| 316 |
|
| 317 |
template <typename T> |
| 318 |
void TraceRange(JSTracer* trc, size_t len, BarrieredBase<T>* vec, |
| 319 |
const char* name) { |
| 320 |
gc::TraceRangeInternal(trc, len, |
| 321 |
gc::ConvertToBase(vec[0].unbarrieredAddress()), name); |
| 322 |
} |
| 323 |
|
| 324 |
// Trace all root edges in the given array. |
| 325 |
|
| 326 |
template <typename T> |
| 327 |
void TraceRootRange(JSTracer* trc, size_t len, T* vec, const char* name) { |
| 328 |
gc::AssertRootMarkingPhase(trc); |
| 329 |
gc::TraceRangeInternal(trc, len, gc::ConvertToBase(vec), name); |
| 330 |
} |
| 331 |
|
| 332 |
// Note that this doesn't trace the contents of the alloc. |
| 333 |
// TODO: Unify this with other TraceEdge methods. |
| 334 |
template <typename T> |
| 335 |
void TraceBufferEdge(JSTracer* trc, gc::Cell* owner, T** bufferp, |
| 336 |
const char* name) { |
| 337 |
void** ptrp = reinterpret_cast<void**>(bufferp); |
| 338 |
gc::TraceBufferEdgeInternal(trc, owner, ptrp, name); |
| 339 |
} |
| 340 |
|
| 341 |
// As below but with manual barriers. |
| 342 |
template <typename T> |
| 343 |
void TraceManuallyBarrieredCrossCompartmentEdge(JSTracer* trc, JSObject* src, |
| 344 |
T* dst, const char* name); |
| 345 |
|
| 346 |
// Trace an edge that crosses compartment boundaries. If the compartment of the |
| 347 |
// destination thing is not being GC'd, then the edge will not be traced. |
| 348 |
template <typename T> |
| 349 |
void TraceCrossCompartmentEdge(JSTracer* trc, JSObject* src, |
| 350 |
const WriteBarriered<T>* dst, const char* name) { |
| 351 |
TraceManuallyBarrieredCrossCompartmentEdge( |
| 352 |
trc, src, gc::ConvertToBase(dst->unbarrieredAddress()), name); |
| 353 |
} |
| 354 |
|
| 355 |
// Trace an edge that's guaranteed to be same-zone but may cross a compartment |
| 356 |
// boundary. This should NOT be used for object => object edges, as those have |
| 357 |
// to be in the cross-compartment wrapper map. |
| 358 |
// |
| 359 |
// WARNING: because this turns off certain compartment checks, you most likely |
| 360 |
// don't want to use this! If you still think you need this function, talk to a |
| 361 |
// GC peer first. |
| 362 |
template <typename T> |
| 363 |
void TraceSameZoneCrossCompartmentEdge(JSTracer* trc, |
| 364 |
const BarrieredBase<T>* dst, |
| 365 |
const char* name); |
| 366 |
|
| 367 |
// Trace a weak map key. For debugger weak maps these may be cross compartment, |
| 368 |
// but the compartment must always be within the current sweep group. |
| 369 |
template <typename T> |
| 370 |
void TraceWeakMapKeyEdgeInternal(JSTracer* trc, Zone* weakMapZone, T** thingp, |
| 371 |
const char* name); |
| 372 |
|
| 373 |
template <typename T> |
| 374 |
void TraceWeakMapKeyEdgeInternal(JSTracer* trc, Zone* weakMapZone, T* thingp, |
| 375 |
const char* name); |
| 376 |
|
| 377 |
template <typename T> |
| 378 |
inline void TraceWeakMapKeyEdge(JSTracer* trc, Zone* weakMapZone, |
| 379 |
const WriteBarriered<T>* thingp, |
| 380 |
const char* name) { |
| 381 |
TraceWeakMapKeyEdgeInternal( |
| 382 |
trc, weakMapZone, gc::ConvertToBase(thingp->unbarrieredAddress()), name); |
| 383 |
} |
| 384 |
|
| 385 |
// Trace a root edge that uses the base GC thing type, instead of a more |
| 386 |
// specific type. |
| 387 |
void TraceGenericPointerRoot(JSTracer* trc, gc::Cell** thingp, |
| 388 |
const char* name); |
| 389 |
|
| 390 |
// Trace a non-root edge that uses the base GC thing type, instead of a more |
| 391 |
// specific type. |
| 392 |
void TraceManuallyBarrieredGenericPointerEdge(JSTracer* trc, gc::Cell** thingp, |
| 393 |
const char* name); |
| 394 |
|
| 395 |
void TraceGCCellPtrRoot(JSTracer* trc, JS::GCCellPtr* thingp, const char* name); |
| 396 |
|
| 397 |
void TraceManuallyBarrieredGCCellPtr(JSTracer* trc, JS::GCCellPtr* thingp, |
| 398 |
const char* name); |
| 399 |
|
| 400 |
namespace gc { |
| 401 |
|
| 402 |
// Trace through a shape or group iteratively during cycle collection to avoid |
| 403 |
// deep or infinite recursion. |
| 404 |
void TraceCycleCollectorChildren(JS::CallbackTracer* trc, Shape* shape); |
| 405 |
|
| 406 |
/** |
| 407 |
* Trace every value within |compartments| that is wrapped by a |
| 408 |
* cross-compartment wrapper from a compartment that is not an element of |
| 409 |
* |compartments|. |
| 410 |
*/ |
| 411 |
void TraceIncomingCCWs(JSTracer* trc, const JS::CompartmentSet& compartments); |
| 412 |
|
| 413 |
/* Get information about a GC thing. Used when dumping the heap. */ |
| 414 |
void GetTraceThingInfo(char* buf, size_t bufsize, void* thing, |
| 415 |
JS::TraceKind kind, bool includeDetails); |
| 416 |
|
| 417 |
// Overloaded function to call the correct GenericTracer method based on the |
| 418 |
// argument type. |
| 419 |
#define DEFINE_DISPATCH_FUNCTION(name, type, _1, _2) \ |
| 420 |
inline void DispatchToOnEdge(JSTracer* trc, type** thingp, \ |
| 421 |
const char* name) { \ |
| 422 |
trc->on##name##Edge(thingp, name); \ |
| 423 |
} |
| 424 |
JS_FOR_EACH_TRACEKIND(DEFINE_DISPATCH_FUNCTION) |
| 425 |
#undef DEFINE_DISPATCH_FUNCTION |
| 426 |
|
| 427 |
} // namespace gc |
| 428 |
} // namespace js |
| 429 |
|
| 430 |
#endif /* js_Tracer_h */ |