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
|
/* -*- 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 "gc/Marking.h"
#include "gc/Zone.h"
#include "jit/JitFrames.h"
#include "util/DiagnosticAssertions.h"
#include "vm/BigIntType.h"
#include "vm/GlobalObject.h"
#include "vm/Realm.h"
#include "gc/Allocator-inl.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 (id.isAtom()) {
checkAtom(id.toAtom(), argIndex);
} else if (id.isSymbol()) {
checkAtom(id.toSymbol(), 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(const PropertyDescriptor& desc, int argIndex) {
if (desc.hasGetter()) {
check(desc.getter(), argIndex);
}
if (desc.hasSetter()) {
check(desc.setter(), argIndex);
}
if (desc.hasValue()) {
check(desc.value(), argIndex);
}
}
void check(Handle<mozilla::Maybe<Value>> maybe, int argIndex) {
if (maybe.get().isSome()) {
check(maybe.get().ref(), argIndex);
}
}
void check(Handle<mozilla::Maybe<PropertyDescriptor>> 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 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 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());
}
inline void JSContext::setZone(js::Zone* zone) { zone_ = zone; }
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::Shape* 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());
} else {
setZone(nullptr);
}
}
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 js::RuntimeCaches& JSContext::caches() { return runtime()->caches(); }
template <typename T, js::AllowGC allowGC, typename... Args>
T* JSContext::newCell(Args&&... args) {
return js::gc::CellAllocator::template NewCell<T, allowGC>(
this, std::forward<Args>(args)...);
}
#endif /* vm_JSContext_inl_h */
|