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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* -*- 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/. */
#ifndef vm_JSContext_inl_h
#define vm_JSContext_inl_h
#include "vm/JSContext.h"
#include <type_traits>
#include <utility>
#include "builtin/Object.h"
#include "gc/Zone.h"
#include "jit/JitFrames.h"
#include "js/friend/StackLimits.h" // js::CheckRecursionLimit
#include "proxy/Proxy.h"
#include "util/DiagnosticAssertions.h"
#include "vm/BigIntType.h"
#include "vm/GlobalObject.h"
#include "vm/HelperThreads.h"
#include "vm/Interpreter.h"
#include "vm/Iteration.h"
#include "vm/Realm.h"
#include "vm/SymbolType.h"
#include "vm/Activation-inl.h" // js::Activation::hasWasmExitFP
namespace js {
class ContextChecks {
JSContext* cx;
JS::Realm* realm() const { return cx->realm(); }
JS::Compartment* compartment() const { return cx->compartment(); }
JS::Zone* zone() const { return cx->zone(); }
public:
explicit ContextChecks(JSContext* cx) : cx(cx) {
#ifdef DEBUG
if (realm()) {
GlobalObject* global = realm()->unsafeUnbarrieredMaybeGlobal();
if (global) {
checkObject(global);
}
}
#endif
}
/*
* Set a breakpoint here (break js::ContextChecks::fail) to debug
* realm/compartment/zone mismatches.
*/
static void fail(JS::Realm* r1, JS::Realm* r2, int argIndex) {
MOZ_CRASH_UNSAFE_PRINTF("*** Realm mismatch %p vs. %p at argument %d", r1,
r2, argIndex);
}
static void fail(JS::Compartment* c1, JS::Compartment* c2, int argIndex) {
MOZ_CRASH_UNSAFE_PRINTF("*** Compartment mismatch %p vs. %p at argument %d",
c1, c2, argIndex);
}
static void fail(JS::Zone* z1, JS::Zone* z2, int argIndex) {
MOZ_CRASH_UNSAFE_PRINTF("*** Zone mismatch %p vs. %p at argument %d", z1,
z2, argIndex);
}
void check(JS::Realm* r, int argIndex) {
if (r && r != realm()) {
fail(realm(), r, argIndex);
}
}
void check(JS::Compartment* c, int argIndex) {
if (c && c != compartment()) {
fail(compartment(), c, argIndex);
}
}
void check(JS::Zone* z, int argIndex) {
if (zone() && z != zone()) {
fail(zone(), z, argIndex);
}
}
void check(JSObject* obj, int argIndex) {
if (obj) {
checkObject(obj);
check(obj->compartment(), argIndex);
}
}
void checkObject(JSObject* obj) {
JS::AssertObjectIsNotGray(obj);
MOZ_ASSERT(!js::gc::IsAboutToBeFinalizedUnbarriered(&obj));
}
template <typename T>
void checkAtom(T* thing, int argIndex) {
static_assert(std::is_same_v<T, JSAtom> || std::is_same_v<T, JS::Symbol>,
"Should only be called with JSAtom* or JS::Symbol* argument");
#ifdef DEBUG
// Atoms which move across zone boundaries need to be marked in the new
// zone, see JS_MarkCrossZoneId.
if (zone()) {
if (!cx->runtime()->gc.atomMarking.atomIsMarked(zone(), thing)) {
MOZ_CRASH_UNSAFE_PRINTF(
"*** Atom not marked for zone %p at argument %d", zone(), argIndex);
}
}
#endif
}
void check(JSString* str, int argIndex) {
JS::AssertCellIsNotGray(str);
if (str->isAtom()) {
checkAtom(&str->asAtom(), argIndex);
} else {
check(str->zone(), argIndex);
}
}
void check(JS::Symbol* symbol, int argIndex) { checkAtom(symbol, argIndex); }
void check(JS::BigInt* bi, int argIndex) { check(bi->zone(), argIndex); }
void check(const js::Value& v, int argIndex) {
if (v.isObject()) {
check(&v.toObject(), argIndex);
} else if (v.isString()) {
check(v.toString(), argIndex);
} else if (v.isSymbol()) {
check(v.toSymbol(), argIndex);
} else if (v.isBigInt()) {
check(v.toBigInt(), argIndex);
}
}
// Check the contents of any container class that supports the C++
// iteration protocol, eg GCVector<jsid>.
template <typename Container>
std::enable_if_t<std::is_same_v<decltype(std::declval<Container>().begin()),
decltype(std::declval<Container>().end())>>
check(const Container& container, int argIndex) {
for (auto i : container) {
check(i, argIndex);
}
}
void check(const JS::HandleValueArray& arr, int argIndex) {
for (size_t i = 0; i < arr.length(); i++) {
check(arr[i], argIndex);
}
}
void check(const CallArgs& args, int argIndex) {
for (Value* p = args.base(); p != args.end(); ++p) {
check(*p, argIndex);
}
}
void check(jsid id, int argIndex) {
if (JSID_IS_ATOM(id)) {
checkAtom(JSID_TO_ATOM(id), argIndex);
} else if (JSID_IS_SYMBOL(id)) {
checkAtom(JSID_TO_SYMBOL(id), argIndex);
} else {
MOZ_ASSERT(!id.isGCThing());
}
}
void check(JSScript* script, int argIndex) {
JS::AssertCellIsNotGray(script);
if (script) {
check(script->realm(), argIndex);
}
}
void check(AbstractFramePtr frame, int argIndex);
void check(Handle<PropertyDescriptor> desc, int argIndex) {
check(desc.object(), argIndex);
if (desc.hasGetterObject()) {
check(desc.getterObject(), argIndex);
}
if (desc.hasSetterObject()) {
check(desc.setterObject(), argIndex);
}
check(desc.value(), argIndex);
}
void check(JS::Handle<mozilla::Maybe<JS::Value>> maybe, int argIndex) {
if (maybe.get().isSome()) {
check(maybe.get().ref(), argIndex);
}
}
};
} // namespace js
template <class... Args>
inline void JSContext::checkImpl(const Args&... args) {
int argIndex = 0;
(..., js::ContextChecks(this).check(args, argIndex++));
}
template <class... Args>
inline void JSContext::check(const Args&... args) {
#ifdef JS_CRASH_DIAGNOSTICS
if (contextChecksEnabled()) {
checkImpl(args...);
}
#endif
}
template <class... Args>
inline void JSContext::releaseCheck(const Args&... args) {
if (contextChecksEnabled()) {
checkImpl(args...);
}
}
template <class... Args>
MOZ_ALWAYS_INLINE void JSContext::debugOnlyCheck(const Args&... args) {
#if defined(DEBUG) && defined(JS_CRASH_DIAGNOSTICS)
if (contextChecksEnabled()) {
checkImpl(args...);
}
#endif
}
namespace js {
STATIC_PRECONDITION_ASSUME(ubound(args.argv_) >= argc)
MOZ_ALWAYS_INLINE bool CallNativeImpl(JSContext* cx, NativeImpl impl,
const CallArgs& args) {
#ifdef DEBUG
bool alreadyThrowing = cx->isExceptionPending();
#endif
cx->check(args);
bool ok = impl(cx, args);
if (ok) {
cx->check(args.rval());
MOZ_ASSERT_IF(!alreadyThrowing, !cx->isExceptionPending());
}
return ok;
}
MOZ_ALWAYS_INLINE bool CallJSGetterOp(JSContext* cx, GetterOp op,
HandleObject obj, HandleId id,
MutableHandleValue vp) {
if (!CheckRecursionLimit(cx)) {
return false;
}
cx->check(obj, id, vp);
bool ok = op(cx, obj, id, vp);
if (ok) {
cx->check(vp);
}
return ok;
}
MOZ_ALWAYS_INLINE bool CallJSSetterOp(JSContext* cx, SetterOp op,
HandleObject obj, HandleId id,
HandleValue v, ObjectOpResult& result) {
if (!CheckRecursionLimit(cx)) {
return false;
}
cx->check(obj, id, v);
return op(cx, obj, id, v, result);
}
inline bool CallJSAddPropertyOp(JSContext* cx, JSAddPropertyOp op,
HandleObject obj, HandleId id, HandleValue v) {
if (!CheckRecursionLimit(cx)) {
return false;
}
cx->check(obj, id, v);
return op(cx, obj, id, v);
}
inline bool CallJSDeletePropertyOp(JSContext* cx, JSDeletePropertyOp op,
HandleObject receiver, HandleId id,
ObjectOpResult& result) {
if (!CheckRecursionLimit(cx)) {
return false;
}
cx->check(receiver, id);
if (op) {
return op(cx, receiver, id, result);
}
return result.succeed();
}
MOZ_ALWAYS_INLINE bool CheckForInterrupt(JSContext* cx) {
MOZ_ASSERT(!cx->isExceptionPending());
// Add an inline fast-path since we have to check for interrupts in some hot
// C++ loops of library builtins.
if (MOZ_UNLIKELY(cx->hasAnyPendingInterrupt())) {
return cx->handleInterrupt();
}
JS_INTERRUPT_POSSIBLY_FAIL();
return true;
}
} /* namespace js */
inline js::Nursery& JSContext::nursery() { return runtime()->gc.nursery(); }
inline void JSContext::minorGC(JS::GCReason reason) {
runtime()->gc.minorGC(reason);
}
inline bool JSContext::runningWithTrustedPrincipals() {
if (!realm()) {
return true;
}
if (!runtime()->trustedPrincipals()) {
return false;
}
return realm()->principals() == runtime()->trustedPrincipals();
}
inline void JSContext::enterRealm(JS::Realm* realm) {
// We should never enter a realm while in the atoms zone.
MOZ_ASSERT_IF(zone(), !zone()->isAtomsZone());
realm->enter();
setRealm(realm);
}
inline void JSContext::enterAtomsZone() {
realm_ = nullptr;
setZone(runtime_->unsafeAtomsZone(), AtomsZone);
}
inline void JSContext::setZone(js::Zone* zone,
JSContext::IsAtomsZone isAtomsZone) {
if (zone_) {
zone_->addTenuredAllocsSinceMinorGC(allocsThisZoneSinceMinorGC_);
}
allocsThisZoneSinceMinorGC_ = 0;
zone_ = zone;
if (zone == nullptr) {
freeLists_ = nullptr;
return;
}
if (isAtomsZone == AtomsZone && isHelperThreadContext()) {
MOZ_ASSERT(!zone_->wasGCStarted());
freeLists_ = atomsZoneFreeLists_;
} else {
freeLists_ = &zone_->arenas.freeLists();
}
}
inline void JSContext::enterRealmOf(JSObject* target) {
JS::AssertCellIsNotGray(target);
enterRealm(target->nonCCWRealm());
}
inline void JSContext::enterRealmOf(JSScript* target) {
JS::AssertCellIsNotGray(target);
enterRealm(target->realm());
}
inline void JSContext::enterRealmOf(js::ObjectGroup* target) {
JS::AssertCellIsNotGray(target);
enterRealm(target->realm());
}
inline void JSContext::enterNullRealm() {
// We should never enter a realm while in the atoms zone.
MOZ_ASSERT_IF(zone(), !zone()->isAtomsZone());
setRealm(nullptr);
}
inline void JSContext::leaveRealm(JS::Realm* oldRealm) {
// Only call leave() after we've setRealm()-ed away from the current realm.
JS::Realm* startingRealm = realm_;
// The current realm should be marked as entered-from-C++ at this point.
MOZ_ASSERT_IF(startingRealm, startingRealm->hasBeenEnteredIgnoringJit());
setRealm(oldRealm);
if (startingRealm) {
startingRealm->leave();
}
}
inline void JSContext::leaveAtomsZone(JS::Realm* oldRealm) {
setRealm(oldRealm);
}
inline void JSContext::setRealm(JS::Realm* realm) {
realm_ = realm;
if (realm) {
// This thread must have exclusive access to the zone.
MOZ_ASSERT(CurrentThreadCanAccessZone(realm->zone()));
MOZ_ASSERT(!realm->zone()->isAtomsZone());
setZone(realm->zone(), NotAtomsZone);
} else {
setZone(nullptr, NotAtomsZone);
}
}
inline void JSContext::setRealmForJitExceptionHandler(JS::Realm* realm) {
// JIT code enters (same-compartment) realms without calling realm->enter()
// so we don't call realm->leave() here.
MOZ_ASSERT(realm->compartment() == compartment());
realm_ = realm;
}
inline JSScript* JSContext::currentScript(
jsbytecode** ppc, AllowCrossRealm allowCrossRealm) const {
if (ppc) {
*ppc = nullptr;
}
js::Activation* act = activation();
if (!act) {
return nullptr;
}
MOZ_ASSERT(act->cx() == this);
// Cross-compartment implies cross-realm.
if (allowCrossRealm == AllowCrossRealm::DontAllow &&
act->compartment() != compartment()) {
return nullptr;
}
JSScript* script = nullptr;
jsbytecode* pc = nullptr;
if (act->isJit()) {
if (act->hasWasmExitFP()) {
return nullptr;
}
js::jit::GetPcScript(const_cast<JSContext*>(this), &script, &pc);
} else {
js::InterpreterFrame* fp = act->asInterpreter()->current();
MOZ_ASSERT(!fp->runningInJit());
script = fp->script();
pc = act->asInterpreter()->regs().pc;
}
MOZ_ASSERT(script->containsPC(pc));
if (allowCrossRealm == AllowCrossRealm::DontAllow &&
script->realm() != realm()) {
return nullptr;
}
if (ppc) {
*ppc = pc;
}
return script;
}
inline js::RuntimeCaches& JSContext::caches() { return runtime()->caches(); }
#endif /* vm_JSContext_inl_h */
|