1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gc/Tracer.h"
#include "mozilla/DebugOnly.h"
#include "NamespaceImports.h"
#include "gc/PublicIterators.h"
#include "jit/JitCode.h"
#include "util/Memory.h"
#include "util/Text.h"
#include "vm/BigIntType.h"
#include "vm/JSContext.h"
#include "vm/JSFunction.h"
#include "vm/JSScript.h"
#include "vm/RegExpShared.h"
#include "vm/Scope.h"
#include "vm/Shape.h"
#include "vm/StringType.h"
#include "vm/SymbolType.h"
#include "gc/TraceMethods-inl.h"
#include "vm/Shape-inl.h"
using namespace js;
using namespace js::gc;
using mozilla::DebugOnly;
template void RuntimeScopeData<LexicalScope::SlotInfo>::trace(JSTracer* trc);
template void RuntimeScopeData<ClassBodyScope::SlotInfo>::trace(JSTracer* trc);
template void RuntimeScopeData<VarScope::SlotInfo>::trace(JSTracer* trc);
template void RuntimeScopeData<GlobalScope::SlotInfo>::trace(JSTracer* trc);
template void RuntimeScopeData<EvalScope::SlotInfo>::trace(JSTracer* trc);
template void RuntimeScopeData<WasmFunctionScope::SlotInfo>::trace(
JSTracer* trc);
void JS::TracingContext::getEdgeName(const char* name, char* buffer,
size_t bufferSize) {
MOZ_ASSERT(bufferSize > 0);
if (functor_) {
(*functor_)(this, buffer, bufferSize);
return;
}
if (index_ != InvalidIndex) {
snprintf(buffer, bufferSize, "%s[%zu]", name, index_);
return;
}
snprintf(buffer, bufferSize, "%s", name);
}
/*** Public Tracing API *****************************************************/
JS_PUBLIC_API void JS::TraceChildren(JSTracer* trc, GCCellPtr thing) {
ApplyGCThingTyped(thing.asCell(), thing.kind(), [trc](auto t) {
MOZ_ASSERT_IF(t->runtimeFromAnyThread() != trc->runtime(),
t->isPermanentAndMayBeShared());
t->traceChildren(trc);
});
}
void js::gc::TraceIncomingCCWs(JSTracer* trc,
const JS::CompartmentSet& compartments) {
for (CompartmentsIter source(trc->runtime()); !source.done(); source.next()) {
if (compartments.has(source)) {
continue;
}
// Iterate over all compartments that |source| has wrappers for.
for (Compartment::WrappedObjectCompartmentEnum dest(source); !dest.empty();
dest.popFront()) {
if (!compartments.has(dest)) {
continue;
}
// Iterate over all wrappers from |source| to |dest| compartments.
for (Compartment::ObjectWrapperEnum e(source, dest); !e.empty();
e.popFront()) {
JSObject* obj = e.front().key();
MOZ_ASSERT(compartments.has(obj->compartment()));
mozilla::DebugOnly<JSObject*> prior = obj;
TraceManuallyBarrieredEdge(trc, &obj,
"cross-compartment wrapper target");
MOZ_ASSERT(obj == prior);
}
}
}
}
/*** Cycle Collector Helpers ************************************************/
// This function is used by the Cycle Collector (CC) to trace through -- or in
// CC parlance, traverse -- a Shape. The CC does not care about Shapes,
// BaseShapes or PropMaps, only the JSObjects held live by them. Thus, we only
// report non-Shape things.
void gc::TraceCycleCollectorChildren(JS::CallbackTracer* trc, Shape* shape) {
shape->base()->traceChildren(trc);
// Don't trace the PropMap because the CC doesn't care about PropertyKey.
}
/*** Traced Edge Printer ****************************************************/
static size_t CountDecimalDigits(size_t num) {
size_t numDigits = 0;
do {
num /= 10;
numDigits++;
} while (num > 0);
return numDigits;
}
static const char* StringKindHeader(JSString* str) {
MOZ_ASSERT(str->isLinear());
if (str->isAtom()) {
if (str->isPermanentAtom()) {
return "permanent atom: ";
}
return "atom: ";
}
if (str->isExtensible()) {
return "extensible: ";
}
if (str->isInline()) {
if (str->isFatInline()) {
return "fat inline: ";
}
return "inline: ";
}
if (str->isDependent()) {
return "dependent: ";
}
if (str->isExternal()) {
return "external: ";
}
return "linear: ";
}
void js::gc::GetTraceThingInfo(char* buf, size_t bufsize, void* thing,
JS::TraceKind kind, bool details) {
const char* name = nullptr; /* silence uninitialized warning */
size_t n;
if (bufsize == 0) {
return;
}
switch (kind) {
case JS::TraceKind::BaseShape:
name = "base_shape";
break;
case JS::TraceKind::GetterSetter:
name = "getter_setter";
break;
case JS::TraceKind::PropMap:
name = "prop_map";
break;
case JS::TraceKind::JitCode:
name = "jitcode";
break;
case JS::TraceKind::Null:
name = "null_pointer";
break;
case JS::TraceKind::Object: {
name = static_cast<JSObject*>(thing)->getClass()->name;
break;
}
case JS::TraceKind::RegExpShared:
name = "reg_exp_shared";
break;
case JS::TraceKind::Scope:
name = "scope";
break;
case JS::TraceKind::Script:
name = "script";
break;
case JS::TraceKind::Shape:
name = "shape";
break;
case JS::TraceKind::String:
name = ((JSString*)thing)->isDependent() ? "substring" : "string";
break;
case JS::TraceKind::Symbol:
name = "symbol";
break;
case JS::TraceKind::BigInt:
name = "BigInt";
break;
default:
name = "INVALID";
break;
}
n = strlen(name);
if (n > bufsize - 1) {
n = bufsize - 1;
}
js_memcpy(buf, name, n + 1);
buf += n;
bufsize -= n;
*buf = '\0';
if (details && bufsize > 2) {
switch (kind) {
case JS::TraceKind::Object: {
JSObject* obj = (JSObject*)thing;
if (obj->is<JSFunction>()) {
JSFunction* fun = &obj->as<JSFunction>();
if (fun->maybePartialDisplayAtom()) {
*buf++ = ' ';
bufsize--;
PutEscapedString(buf, bufsize, fun->maybePartialDisplayAtom(), 0);
}
} else {
snprintf(buf, bufsize, " <unknown object>");
}
break;
}
case JS::TraceKind::Script: {
auto* script = static_cast<js::BaseScript*>(thing);
snprintf(buf, bufsize, " %s:%u", script->filename(), script->lineno());
break;
}
case JS::TraceKind::String: {
*buf++ = ' ';
bufsize--;
JSString* str = (JSString*)thing;
if (str->isLinear()) {
const char* header = StringKindHeader(str);
bool willFit = str->length() + strlen("<length > ") + strlen(header) +
CountDecimalDigits(str->length()) <
bufsize;
n = snprintf(buf, bufsize, "<%slength %zu%s> ", header, str->length(),
willFit ? "" : " (truncated)");
buf += n;
bufsize -= n;
PutEscapedString(buf, bufsize, &str->asLinear(), 0);
} else {
snprintf(buf, bufsize, "<rope: length %zu>", str->length());
}
break;
}
case JS::TraceKind::Symbol: {
*buf++ = ' ';
bufsize--;
auto* sym = static_cast<JS::Symbol*>(thing);
if (JSAtom* desc = sym->description()) {
PutEscapedString(buf, bufsize, desc, 0);
} else {
snprintf(buf, bufsize, "<null>");
}
break;
}
case JS::TraceKind::Scope: {
auto* scope = static_cast<js::Scope*>(thing);
snprintf(buf, bufsize, " %s", js::ScopeKindString(scope->kind()));
break;
}
default:
break;
}
}
buf[bufsize - 1] = '\0';
}
JS::CallbackTracer::CallbackTracer(JSContext* cx, JS::TracerKind kind,
JS::TraceOptions options)
: CallbackTracer(cx->runtime(), kind, options) {}
|