| 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 |
/* |
| 8 |
* Trace methods for all GC things, defined in a separate header to allow |
| 9 |
* inlining. |
| 10 |
* |
| 11 |
* This also includes eager inline marking versions. Both paths must end up |
| 12 |
* traversing equivalent subgraphs. |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef gc_TraceMethods_inl_h |
| 16 |
#define gc_TraceMethods_inl_h |
| 17 |
|
| 18 |
#include "gc/GCMarker.h" |
| 19 |
#include "gc/Tracer.h" |
| 20 |
#include "jit/JitCode.h" |
| 21 |
#include "vm/BigIntType.h" |
| 22 |
#include "vm/GetterSetter.h" |
| 23 |
#include "vm/GlobalObject.h" |
| 24 |
#include "vm/JSScript.h" |
| 25 |
#include "vm/PropMap.h" |
| 26 |
#include "vm/Realm.h" |
| 27 |
#include "vm/Scope.h" |
| 28 |
#include "vm/Shape.h" |
| 29 |
#include "vm/StringType.h" |
| 30 |
#include "vm/SymbolType.h" |
| 31 |
#include "wasm/WasmJS.h" |
| 32 |
|
| 33 |
#include "gc/Marking-inl.h" |
| 34 |
#include "vm/StringType-inl.h" |
| 35 |
|
| 36 |
inline void js::BaseScript::traceChildren(JSTracer* trc) { |
| 37 |
TraceNullableEdge(trc, &function_, "function"); |
| 38 |
TraceEdge(trc, &sourceObject_, "sourceObject"); |
| 39 |
|
| 40 |
warmUpData_.trace(trc); |
| 41 |
|
| 42 |
if (data_) { |
| 43 |
data_->trace(trc); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
inline void js::Shape::traceChildren(JSTracer* trc) { |
| 48 |
TraceCellHeaderEdge(trc, this, "base"); |
| 49 |
if (isNative()) { |
| 50 |
asNative().traceChildren(trc); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
inline void js::NativeShape::traceChildren(JSTracer* trc) { |
| 55 |
TraceNullableEdge(trc, &propMap_, "propertymap"); |
| 56 |
} |
| 57 |
|
| 58 |
template <uint32_t opts> |
| 59 |
void js::GCMarker::eagerlyMarkChildren(Shape* shape) { |
| 60 |
MOZ_ASSERT(shape->isMarked(markColor())); |
| 61 |
|
| 62 |
BaseShape* base = shape->base(); |
| 63 |
checkTraversedEdge(shape, base); |
| 64 |
if (mark<opts>(base)) { |
| 65 |
base->traceChildren(tracer()); |
| 66 |
} |
| 67 |
|
| 68 |
if (shape->isNative()) { |
| 69 |
if (PropMap* map = shape->asNative().propMap()) { |
| 70 |
markAndTraverseEdge<opts>(shape, map); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
inline void JSString::traceChildren(JSTracer* trc) { |
| 76 |
if (hasBase()) { |
| 77 |
traceBase(trc); |
| 78 |
} else if (isRope()) { |
| 79 |
asRope().traceChildren(trc); |
| 80 |
} |
| 81 |
} |
| 82 |
template <uint32_t opts> |
| 83 |
void js::GCMarker::eagerlyMarkChildren(JSString* str) { |
| 84 |
if (str->isLinear()) { |
| 85 |
eagerlyMarkChildren<opts>(&str->asLinear()); |
| 86 |
} else { |
| 87 |
eagerlyMarkChildren<opts>(&str->asRope()); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
inline void JSString::traceBase(JSTracer* trc) { |
| 92 |
MOZ_ASSERT(hasBase()); |
| 93 |
js::TraceManuallyBarrieredEdge(trc, &d.s.u3.base, "base"); |
| 94 |
} |
| 95 |
template <uint32_t opts> |
| 96 |
void js::GCMarker::eagerlyMarkChildren(JSLinearString* linearStr) { |
| 97 |
gc::AssertShouldMarkInZone(this, linearStr); |
| 98 |
MOZ_ASSERT(linearStr->isMarkedAny()); |
| 99 |
MOZ_ASSERT(linearStr->JSString::isLinear()); |
| 100 |
|
| 101 |
// Use iterative marking to avoid blowing out the stack. |
| 102 |
while (linearStr->hasBase()) { |
| 103 |
linearStr = linearStr->base(); |
| 104 |
|
| 105 |
// It's possible to observe a rope as the base of a linear string if we |
| 106 |
// process barriers during rope flattening. See the assignment of base in |
| 107 |
// JSRope::flattenInternal's finish_node section. |
| 108 |
if (static_cast<JSString*>(linearStr)->isRope()) { |
| 109 |
MOZ_ASSERT(!JS::RuntimeHeapIsMajorCollecting()); |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
MOZ_ASSERT(linearStr->JSString::isLinear()); |
| 114 |
gc::AssertShouldMarkInZone(this, linearStr); |
| 115 |
if (!mark<opts>(static_cast<JSString*>(linearStr))) { |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
inline void JSRope::traceChildren(JSTracer* trc) { |
| 122 |
js::TraceManuallyBarrieredEdge(trc, &d.s.u2.left, "left child"); |
| 123 |
js::TraceManuallyBarrieredEdge(trc, &d.s.u3.right, "right child"); |
| 124 |
} |
| 125 |
template <uint32_t opts> |
| 126 |
void js::GCMarker::eagerlyMarkChildren(JSRope* rope) { |
| 127 |
// This function tries to scan the whole rope tree using the marking stack |
| 128 |
// as temporary storage. If that becomes full, the unscanned ropes are |
| 129 |
// added to the delayed marking list. When the function returns, the |
| 130 |
// marking stack is at the same depth as it was on entry. This way we avoid |
| 131 |
// using tags when pushing ropes to the stack as ropes never leak to other |
| 132 |
// users of the stack. This also assumes that a rope can only point to |
| 133 |
// other ropes or linear strings, it cannot refer to GC things of other |
| 134 |
// types. |
| 135 |
size_t savedPos = stack.position(); |
| 136 |
MOZ_DIAGNOSTIC_ASSERT(rope->getTraceKind() == JS::TraceKind::String); |
| 137 |
while (true) { |
| 138 |
MOZ_DIAGNOSTIC_ASSERT(rope->getTraceKind() == JS::TraceKind::String); |
| 139 |
MOZ_DIAGNOSTIC_ASSERT(rope->JSString::isRope()); |
| 140 |
gc::AssertShouldMarkInZone(this, rope); |
| 141 |
MOZ_ASSERT(rope->isMarkedAny()); |
| 142 |
JSRope* next = nullptr; |
| 143 |
|
| 144 |
JSString* right = rope->rightChild(); |
| 145 |
if (mark<opts>(right)) { |
| 146 |
MOZ_ASSERT(!right->isPermanentAtom()); |
| 147 |
if (right->isLinear()) { |
| 148 |
eagerlyMarkChildren<opts>(&right->asLinear()); |
| 149 |
} else { |
| 150 |
next = &right->asRope(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
JSString* left = rope->leftChild(); |
| 155 |
if (mark<opts>(left)) { |
| 156 |
MOZ_ASSERT(!left->isPermanentAtom()); |
| 157 |
if (left->isLinear()) { |
| 158 |
eagerlyMarkChildren<opts>(&left->asLinear()); |
| 159 |
} else { |
| 160 |
// When both children are ropes, set aside the right one to |
| 161 |
// scan it later. |
| 162 |
if (next && !stack.pushTempRope(next)) { |
| 163 |
delayMarkingChildrenOnOOM(next); |
| 164 |
} |
| 165 |
next = &left->asRope(); |
| 166 |
} |
| 167 |
} |
| 168 |
if (next) { |
| 169 |
rope = next; |
| 170 |
} else if (savedPos != stack.position()) { |
| 171 |
MOZ_ASSERT(savedPos < stack.position()); |
| 172 |
rope = stack.popPtr().asTempRope(); |
| 173 |
} else { |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
MOZ_ASSERT(savedPos == stack.position()); |
| 178 |
} |
| 179 |
|
| 180 |
inline void JS::Symbol::traceChildren(JSTracer* trc) { |
| 181 |
js::TraceNullableCellHeaderEdge(trc, this, "symbol description"); |
| 182 |
} |
| 183 |
|
| 184 |
template <typename SlotInfo> |
| 185 |
void js::RuntimeScopeData<SlotInfo>::trace(JSTracer* trc) { |
| 186 |
TraceBindingNames(trc, GetScopeDataTrailingNamesPointer(this), length); |
| 187 |
} |
| 188 |
|
| 189 |
inline void js::FunctionScope::RuntimeData::trace(JSTracer* trc) { |
| 190 |
TraceNullableEdge(trc, &canonicalFunction, "scope canonical function"); |
| 191 |
TraceNullableBindingNames(trc, GetScopeDataTrailingNamesPointer(this), |
| 192 |
length); |
| 193 |
} |
| 194 |
inline void js::ModuleScope::RuntimeData::trace(JSTracer* trc) { |
| 195 |
TraceNullableEdge(trc, &module, "scope module"); |
| 196 |
TraceBindingNames(trc, GetScopeDataTrailingNamesPointer(this), length); |
| 197 |
} |
| 198 |
inline void js::WasmInstanceScope::RuntimeData::trace(JSTracer* trc) { |
| 199 |
TraceNullableEdge(trc, &instance, "wasm instance"); |
| 200 |
TraceBindingNames(trc, GetScopeDataTrailingNamesPointer(this), length); |
| 201 |
} |
| 202 |
|
| 203 |
inline void js::Scope::traceChildren(JSTracer* trc) { |
| 204 |
TraceNullableEdge(trc, &environmentShape_, "scope env shape"); |
| 205 |
TraceNullableEdge(trc, &enclosingScope_, "scope enclosing"); |
| 206 |
applyScopeDataTyped([trc](auto data) { data->trace(trc); }); |
| 207 |
} |
| 208 |
|
| 209 |
template <uint32_t opts> |
| 210 |
void js::GCMarker::eagerlyMarkChildren(Scope* scope) { |
| 211 |
do { |
| 212 |
if (Shape* shape = scope->environmentShape()) { |
| 213 |
markAndTraverseEdge<opts>(scope, shape); |
| 214 |
} |
| 215 |
mozilla::Span<AbstractBindingName<JSAtom>> names; |
| 216 |
switch (scope->kind()) { |
| 217 |
case ScopeKind::Function: { |
| 218 |
FunctionScope::RuntimeData& data = scope->as<FunctionScope>().data(); |
| 219 |
if (data.canonicalFunction) { |
| 220 |
markAndTraverseObjectEdge<opts>(scope, data.canonicalFunction); |
| 221 |
} |
| 222 |
names = GetScopeDataTrailingNames(&data); |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
case ScopeKind::FunctionBodyVar: { |
| 227 |
VarScope::RuntimeData& data = scope->as<VarScope>().data(); |
| 228 |
names = GetScopeDataTrailingNames(&data); |
| 229 |
break; |
| 230 |
} |
| 231 |
|
| 232 |
case ScopeKind::Lexical: |
| 233 |
case ScopeKind::SimpleCatch: |
| 234 |
case ScopeKind::Catch: |
| 235 |
case ScopeKind::NamedLambda: |
| 236 |
case ScopeKind::StrictNamedLambda: |
| 237 |
case ScopeKind::FunctionLexical: { |
| 238 |
LexicalScope::RuntimeData& data = scope->as<LexicalScope>().data(); |
| 239 |
names = GetScopeDataTrailingNames(&data); |
| 240 |
break; |
| 241 |
} |
| 242 |
|
| 243 |
case ScopeKind::ClassBody: { |
| 244 |
ClassBodyScope::RuntimeData& data = scope->as<ClassBodyScope>().data(); |
| 245 |
names = GetScopeDataTrailingNames(&data); |
| 246 |
break; |
| 247 |
} |
| 248 |
|
| 249 |
case ScopeKind::Global: |
| 250 |
case ScopeKind::NonSyntactic: { |
| 251 |
GlobalScope::RuntimeData& data = scope->as<GlobalScope>().data(); |
| 252 |
names = GetScopeDataTrailingNames(&data); |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
case ScopeKind::Eval: |
| 257 |
case ScopeKind::StrictEval: { |
| 258 |
EvalScope::RuntimeData& data = scope->as<EvalScope>().data(); |
| 259 |
names = GetScopeDataTrailingNames(&data); |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
case ScopeKind::Module: { |
| 264 |
ModuleScope::RuntimeData& data = scope->as<ModuleScope>().data(); |
| 265 |
if (data.module) { |
| 266 |
markAndTraverseObjectEdge<opts>(scope, data.module); |
| 267 |
} |
| 268 |
names = GetScopeDataTrailingNames(&data); |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
case ScopeKind::With: |
| 273 |
break; |
| 274 |
|
| 275 |
case ScopeKind::WasmInstance: { |
| 276 |
WasmInstanceScope::RuntimeData& data = |
| 277 |
scope->as<WasmInstanceScope>().data(); |
| 278 |
markAndTraverseObjectEdge<opts>(scope, data.instance); |
| 279 |
names = GetScopeDataTrailingNames(&data); |
| 280 |
break; |
| 281 |
} |
| 282 |
|
| 283 |
case ScopeKind::WasmFunction: { |
| 284 |
WasmFunctionScope::RuntimeData& data = |
| 285 |
scope->as<WasmFunctionScope>().data(); |
| 286 |
names = GetScopeDataTrailingNames(&data); |
| 287 |
break; |
| 288 |
} |
| 289 |
} |
| 290 |
if (scope->kind_ == ScopeKind::Function) { |
| 291 |
for (auto& binding : names) { |
| 292 |
if (JSAtom* name = binding.name()) { |
| 293 |
markAndTraverseStringEdge<opts>(scope, name); |
| 294 |
} |
| 295 |
} |
| 296 |
} else { |
| 297 |
for (auto& binding : names) { |
| 298 |
markAndTraverseStringEdge<opts>(scope, binding.name()); |
| 299 |
} |
| 300 |
} |
| 301 |
scope = scope->enclosing(); |
| 302 |
} while (scope && mark<opts>(scope)); |
| 303 |
} |
| 304 |
|
| 305 |
inline void js::BaseShape::traceChildren(JSTracer* trc) { |
| 306 |
// Note: the realm's global can be nullptr if we GC while creating the global. |
| 307 |
if (JSObject* global = realm()->unsafeUnbarrieredMaybeGlobal()) { |
| 308 |
TraceManuallyBarrieredEdge(trc, &global, "baseshape_global"); |
| 309 |
} |
| 310 |
|
| 311 |
if (proto_.isObject()) { |
| 312 |
TraceEdge(trc, &proto_, "baseshape_proto"); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
inline void js::GetterSetter::traceChildren(JSTracer* trc) { |
| 317 |
if (getter()) { |
| 318 |
TraceCellHeaderEdge(trc, this, "gettersetter_getter"); |
| 319 |
} |
| 320 |
if (setter()) { |
| 321 |
TraceEdge(trc, &setter_, "gettersetter_setter"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
inline void js::PropMap::traceChildren(JSTracer* trc) { |
| 326 |
if (hasPrevious()) { |
| 327 |
TraceEdge(trc, &asLinked()->data_.previous, "propmap_previous"); |
| 328 |
} |
| 329 |
|
| 330 |
if (isShared()) { |
| 331 |
SharedPropMap::TreeData& treeData = asShared()->treeDataRef(); |
| 332 |
if (SharedPropMap* parent = treeData.parent.maybeMap()) { |
| 333 |
TraceManuallyBarrieredEdge(trc, &parent, "propmap_parent"); |
| 334 |
if (parent != treeData.parent.map()) { |
| 335 |
treeData.setParent(parent, treeData.parent.index()); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
for (uint32_t i = 0; i < PropMap::Capacity; i++) { |
| 341 |
if (hasKey(i)) { |
| 342 |
TraceEdge(trc, &keys_[i], "propmap_key"); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (canHaveTable() && asLinked()->hasTable()) { |
| 347 |
asLinked()->data_.table->trace(trc); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
template <uint32_t opts> |
| 352 |
void js::GCMarker::eagerlyMarkChildren(PropMap* map) { |
| 353 |
MOZ_ASSERT(map->isMarkedAny()); |
| 354 |
do { |
| 355 |
for (uint32_t i = 0; i < PropMap::Capacity; i++) { |
| 356 |
if (map->hasKey(i)) { |
| 357 |
markAndTraverseEdge<opts>(map, map->getKey(i)); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
if (map->canHaveTable()) { |
| 362 |
// Special case: if a map has a table then all its pointers must point to |
| 363 |
// this map or an ancestor. Since these pointers will be traced by this |
| 364 |
// loop they do not need to be traced here as well. |
| 365 |
MOZ_ASSERT(map->asLinked()->canSkipMarkingTable()); |
| 366 |
} |
| 367 |
|
| 368 |
if (map->isDictionary()) { |
| 369 |
map = map->asDictionary()->previous(); |
| 370 |
} else { |
| 371 |
// For shared maps follow the |parent| link and not the |previous| link. |
| 372 |
// They're different when a map had a branch that wasn't at the end of the |
| 373 |
// map, but in this case they must have the same |previous| map. This is |
| 374 |
// asserted in SharedPropMap::addChild. In other words, marking all |
| 375 |
// |parent| maps will also mark all |previous| maps. |
| 376 |
map = map->asShared()->treeDataRef().parent.maybeMap(); |
| 377 |
} |
| 378 |
} while (map && mark<opts>(map)); |
| 379 |
} |
| 380 |
|
| 381 |
inline void JS::BigInt::traceChildren(JSTracer* trc) {} |
| 382 |
|
| 383 |
// JitCode::traceChildren is not defined inline due to its dependence on |
| 384 |
// MacroAssembler. |
| 385 |
|
| 386 |
#endif // gc_TraceMethods_inl_h |