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
|
/* -*- 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 "vm/Instrumentation.h"
#include <iterator>
#include "jsapi.h"
#include "debugger/DebugAPI.h"
#include "frontend/ParserAtom.h"
#include "js/Object.h" // JS::GetReservedSlot
#include "proxy/DeadObjectProxy.h"
#include "vm/JSObject-inl.h"
namespace js {
RealmInstrumentation::RealmInstrumentation(Zone* zone, JSObject* callback,
JSObject* dbgObject, uint32_t kinds)
: callback(callback), dbgObject(dbgObject), kinds(kinds) {}
void RealmInstrumentation::trace(JSTracer* trc) {
TraceEdge(trc, &callback, "RealmInstrumentation::callback");
TraceEdge(trc, &dbgObject, "RealmInstrumentation::dbgObject");
}
enum InstrumentationHolderSlots {
RealmInstrumentationSlot,
ReservedSlotCount,
};
static RealmInstrumentation* GetInstrumentation(JSObject* obj) {
Value v = JS::GetReservedSlot(obj, RealmInstrumentationSlot);
return static_cast<RealmInstrumentation*>(v.isUndefined() ? nullptr
: v.toPrivate());
}
/* static */
void RealmInstrumentation::holderFinalize(JSFreeOp* fop, JSObject* obj) {
RealmInstrumentation* instrumentation = GetInstrumentation(obj);
fop->delete_(obj, instrumentation, MemoryUse::RealmInstrumentation);
}
/* static */
void RealmInstrumentation::holderTrace(JSTracer* trc, JSObject* obj) {
RealmInstrumentation* instrumentation = GetInstrumentation(obj);
if (instrumentation) {
instrumentation->trace(trc);
}
}
static const JSClassOps InstrumentationHolderClassOps = {
nullptr, // addProperty
nullptr, // delProperty
nullptr, // enumerate
nullptr, // newEnumerate
nullptr, // resolve
nullptr, // mayResolve
RealmInstrumentation::holderFinalize, // finalize
nullptr, // call
nullptr, // hasInstance
nullptr, // construct
RealmInstrumentation::holderTrace, // trace
};
static const JSClass InstrumentationHolderClass = {
"Instrumentation Holder",
JSCLASS_HAS_RESERVED_SLOTS(ReservedSlotCount) | JSCLASS_FOREGROUND_FINALIZE,
&InstrumentationHolderClassOps, JS_NULL_CLASS_SPEC, JS_NULL_CLASS_EXT};
static const char* instrumentationNames[] = {
#define DEFINE_INSTRUMENTATION_STRING(_1, String, _2) String,
FOR_EACH_INSTRUMENTATION_KIND(DEFINE_INSTRUMENTATION_STRING)
#undef DEFINE_INSTRUMENTATION_STRING
};
static bool StringToInstrumentationKind(JSContext* cx, HandleString str,
InstrumentationKind* result) {
for (size_t i = 0; i < std::size(instrumentationNames); i++) {
bool match;
if (!JS_StringEqualsAscii(cx, str, instrumentationNames[i], &match)) {
return false;
}
if (match) {
*result = (InstrumentationKind)(1 << i);
return true;
}
}
JS_ReportErrorASCII(cx, "Unknown instrumentation kind");
return false;
}
/* static */
const frontend::ParserAtom* RealmInstrumentation::getInstrumentationKindName(
JSContext* cx, frontend::ParserAtomsTable& parserAtoms,
InstrumentationKind kind) {
for (size_t i = 0; i < std::size(instrumentationNames); i++) {
if (kind == (InstrumentationKind)(1 << i)) {
return parserAtoms.internAscii(cx, instrumentationNames[i],
strlen(instrumentationNames[i]));
}
}
MOZ_CRASH("Unexpected instrumentation kind");
}
/* static */
bool RealmInstrumentation::install(JSContext* cx, Handle<GlobalObject*> global,
HandleObject callbackArg,
HandleObject dbgObjectArg,
Handle<StringVector> kindStrings) {
MOZ_ASSERT(global == cx->global());
if (global->getInstrumentationHolder()) {
JS_ReportErrorASCII(cx, "Global already has instrumentation specified");
return false;
}
RootedObject callback(cx, callbackArg);
if (!cx->compartment()->wrap(cx, &callback)) {
return false;
}
RootedObject dbgObject(cx, dbgObjectArg);
if (!cx->compartment()->wrap(cx, &dbgObject)) {
return false;
}
uint32_t kinds = 0;
for (size_t i = 0; i < kindStrings.length(); i++) {
HandleString str = kindStrings[i];
InstrumentationKind kind;
if (!StringToInstrumentationKind(cx, str, &kind)) {
return false;
}
kinds |= (uint32_t)kind;
}
Rooted<UniquePtr<RealmInstrumentation>> instrumentation(
cx,
MakeUnique<RealmInstrumentation>(cx->zone(), callback, dbgObject, kinds));
if (!instrumentation) {
ReportOutOfMemory(cx);
return false;
}
JSObject* holder = NewBuiltinClassInstance(cx, &InstrumentationHolderClass);
if (!holder) {
return false;
}
InitReservedSlot(&holder->as<NativeObject>(), RealmInstrumentationSlot,
instrumentation.release(), MemoryUse::RealmInstrumentation);
global->setInstrumentationHolder(holder);
return true;
}
/* static */
bool RealmInstrumentation::setActive(JSContext* cx,
Handle<GlobalObject*> global,
Debugger* dbg, bool active) {
MOZ_ASSERT(global == cx->global());
RootedObject holder(cx, global->getInstrumentationHolder());
if (!holder) {
JS_ReportErrorASCII(cx, "Global does not have instrumentation specified");
return false;
}
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
if (active != instrumentation->active) {
instrumentation->active = active;
// For simplicity, discard all Ion code in the entire zone when
// instrumentation activity changes.
js::CancelOffThreadIonCompile(cx->runtime());
cx->zone()->setPreservingCode(false);
cx->zone()->discardJitCode(cx->runtime()->defaultFreeOp(),
Zone::KeepBaselineCode);
}
return true;
}
/* static */
bool RealmInstrumentation::isActive(GlobalObject* global) {
JSObject* holder = global->getInstrumentationHolder();
MOZ_ASSERT(holder);
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
return instrumentation->active;
}
/* static */
const int32_t* RealmInstrumentation::addressOfActive(GlobalObject* global) {
JSObject* holder = global->getInstrumentationHolder();
MOZ_ASSERT(holder);
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
return &instrumentation->active;
}
/* static */
JSObject* RealmInstrumentation::getCallback(GlobalObject* global) {
JSObject* holder = global->getInstrumentationHolder();
MOZ_ASSERT(holder);
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
return instrumentation->callback;
}
/* static */
uint32_t RealmInstrumentation::getInstrumentationKinds(GlobalObject* global) {
JSObject* holder = global->getInstrumentationHolder();
if (!holder) {
return 0;
}
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
return instrumentation->kinds;
}
/* static */
bool RealmInstrumentation::getScriptId(JSContext* cx,
Handle<GlobalObject*> global,
HandleScript script, int32_t* id) {
MOZ_ASSERT(global == cx->global());
RootedObject holder(cx, global->getInstrumentationHolder());
RealmInstrumentation* instrumentation = GetInstrumentation(holder);
MOZ_ASSERT(instrumentation);
RootedObject dbgObject(cx, UncheckedUnwrap(instrumentation->dbgObject));
if (IsDeadProxyObject(dbgObject)) {
JS_ReportErrorASCII(cx, "Instrumentation debugger object is dead");
return false;
}
AutoRealm ar(cx, dbgObject);
RootedValue idValue(cx);
if (!DebugAPI::getScriptInstrumentationId(cx, dbgObject, script, &idValue)) {
return false;
}
if (!idValue.isNumber()) {
JS_ReportErrorASCII(cx, "Instrumentation ID not set for script");
return false;
}
*id = idValue.toNumber();
return true;
}
bool InstrumentationActiveOperation(JSContext* cx, MutableHandleValue rv) {
rv.setBoolean(RealmInstrumentation::isActive(cx->global()));
return true;
}
JSObject* InstrumentationCallbackOperation(JSContext* cx) {
return RealmInstrumentation::getCallback(cx->global());
}
bool InstrumentationScriptIdOperation(JSContext* cx, HandleScript script,
MutableHandleValue rv) {
int32_t id;
if (!RealmInstrumentation::getScriptId(cx, cx->global(), script, &id)) {
return false;
}
rv.setInt32(id);
return true;
}
bool GlobalHasInstrumentation(JSObject* global) {
return global->is<js::GlobalObject>() &&
global->as<js::GlobalObject>().getInstrumentationHolder();
}
} // namespace js
|