Debian Codegraph @

js/src/gc/Tracer.cpp

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