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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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 jscntxtinlines_h
#define jscntxtinlines_h
#include "jscntxt.h"
#include "jscompartment.h"
#include "jsiter.h"
#include "builtin/Object.h"
#include "jit/JitFrames.h"
#include "vm/HelperThreads.h"
#include "vm/Interpreter.h"
#include "vm/ProxyObject.h"
#include "vm/Symbol.h"
namespace js {
class CompartmentChecker
{
JSCompartment* compartment;
public:
explicit CompartmentChecker(ExclusiveContext* cx)
: compartment(cx->compartment())
{
}
/*
* Set a breakpoint here (break js::CompartmentChecker::fail) to debug
* compartment mismatches.
*/
static void fail(JSCompartment* c1, JSCompartment* c2) {
printf("*** Compartment mismatch %p vs. %p\n", (void*) c1, (void*) c2);
MOZ_CRASH();
}
static void fail(JS::Zone* z1, JS::Zone* z2) {
printf("*** Zone mismatch %p vs. %p\n", (void*) z1, (void*) z2);
MOZ_CRASH();
}
/* Note: should only be used when neither c1 nor c2 may be the atoms compartment. */
static void check(JSCompartment* c1, JSCompartment* c2) {
MOZ_ASSERT(!c1->runtimeFromAnyThread()->isAtomsCompartment(c1));
MOZ_ASSERT(!c2->runtimeFromAnyThread()->isAtomsCompartment(c2));
if (c1 != c2)
fail(c1, c2);
}
void check(JSCompartment* c) {
if (c && !compartment->runtimeFromAnyThread()->isAtomsCompartment(c)) {
if (!compartment)
compartment = c;
else if (c != compartment)
fail(compartment, c);
}
}
void checkZone(JS::Zone* z) {
if (compartment && z != compartment->zone())
fail(compartment->zone(), z);
}
void check(JSObject* obj) {
MOZ_ASSERT_IF(obj, !JS::ObjectIsMarkedGray(obj));
if (obj)
check(obj->compartment());
}
template<typename T>
void check(const Rooted<T>& rooted) {
check(rooted.get());
}
template<typename T>
void check(Handle<T> handle) {
check(handle.get());
}
void check(JSString* str) {
MOZ_ASSERT(!js::gc::detail::CellIsMarkedGray(str));
if (!str->isAtom())
checkZone(str->zone());
}
void check(const js::Value& v) {
if (v.isObject())
check(&v.toObject());
else if (v.isString())
check(v.toString());
}
void check(const ValueArray& arr) {
for (size_t i = 0; i < arr.length; i++)
check(arr.array[i]);
}
void check(const JSValueArray& arr) {
for (size_t i = 0; i < arr.length; i++)
check(arr.array[i]);
}
void check(const JS::HandleValueArray& arr) {
for (size_t i = 0; i < arr.length(); i++)
check(arr[i]);
}
void check(const CallArgs& args) {
for (Value* p = args.base(); p != args.end(); ++p)
check(*p);
}
void check(jsid id) {}
void check(JSScript* script) {
MOZ_ASSERT_IF(script, !JS::ScriptIsMarkedGray(script));
if (script)
check(script->compartment());
}
void check(InterpreterFrame* fp);
void check(AbstractFramePtr frame);
void check(SavedStacks* stacks);
void check(Handle<PropertyDescriptor> desc) {
check(desc.object());
if (desc.hasGetterObject())
check(desc.getterObject());
if (desc.hasSetterObject())
check(desc.setterObject());
check(desc.value());
}
void check(TypeSet::Type type) {
check(type.maybeCompartment());
}
};
/*
* Don't perform these checks when called from a finalizer. The checking
* depends on other objects not having been swept yet.
*/
#define START_ASSERT_SAME_COMPARTMENT() \
if (cx->isJSContext() && cx->asJSContext()->runtime()->isHeapBusy()) \
return; \
CompartmentChecker c(cx)
template <class T1> inline void
releaseAssertSameCompartment(ExclusiveContext* cx, const T1& t1)
{
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
}
template <class T1> inline void
assertSameCompartment(ExclusiveContext* cx, const T1& t1)
{
#ifdef JS_CRASH_DIAGNOSTICS
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
#endif
}
template <class T1> inline void
assertSameCompartmentDebugOnly(ExclusiveContext* cx, const T1& t1)
{
#if defined(DEBUG) && defined(JS_CRASH_DIAGNOSTICS)
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
#endif
}
template <class T1, class T2> inline void
assertSameCompartment(ExclusiveContext* cx, const T1& t1, const T2& t2)
{
#ifdef JS_CRASH_DIAGNOSTICS
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
c.check(t2);
#endif
}
template <class T1, class T2, class T3> inline void
assertSameCompartment(ExclusiveContext* cx, const T1& t1, const T2& t2, const T3& t3)
{
#ifdef JS_CRASH_DIAGNOSTICS
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
c.check(t2);
c.check(t3);
#endif
}
template <class T1, class T2, class T3, class T4> inline void
assertSameCompartment(ExclusiveContext* cx,
const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
#ifdef JS_CRASH_DIAGNOSTICS
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
c.check(t2);
c.check(t3);
c.check(t4);
#endif
}
template <class T1, class T2, class T3, class T4, class T5> inline void
assertSameCompartment(ExclusiveContext* cx,
const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
{
#ifdef JS_CRASH_DIAGNOSTICS
START_ASSERT_SAME_COMPARTMENT();
c.check(t1);
c.check(t2);
c.check(t3);
c.check(t4);
c.check(t5);
#endif
}
#undef START_ASSERT_SAME_COMPARTMENT
STATIC_PRECONDITION_ASSUME(ubound(args.argv_) >= argc)
MOZ_ALWAYS_INLINE bool
CallJSNative(JSContext* cx, Native native, const CallArgs& args)
{
JS_CHECK_RECURSION(cx, return false);
#ifdef DEBUG
bool alreadyThrowing = cx->isExceptionPending();
#endif
assertSameCompartment(cx, args);
bool ok = native(cx, args.length(), args.base());
if (ok) {
assertSameCompartment(cx, args.rval());
MOZ_ASSERT_IF(!alreadyThrowing, !cx->isExceptionPending());
}
return ok;
}
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
assertSameCompartment(cx, args);
bool ok = impl(cx, args);
if (ok) {
assertSameCompartment(cx, args.rval());
MOZ_ASSERT_IF(!alreadyThrowing, !cx->isExceptionPending());
}
return ok;
}
STATIC_PRECONDITION(ubound(args.argv_) >= argc)
MOZ_ALWAYS_INLINE bool
CallJSNativeConstructor(JSContext* cx, Native native, const CallArgs& args)
{
#ifdef DEBUG
RootedObject callee(cx, &args.callee());
#endif
MOZ_ASSERT(args.thisv().isMagic());
if (!CallJSNative(cx, native, args))
return false;
/*
* Native constructors must return non-primitive values on success.
* Although it is legal, if a constructor returns the callee, there is a
* 99.9999% chance it is a bug. If any valid code actually wants the
* constructor to return the callee, the assertion can be removed or
* (another) conjunct can be added to the antecedent.
*
* Exceptions:
*
* - Proxies are exceptions to both rules: they can return primitives and
* they allow content to return the callee.
*
* - CallOrConstructBoundFunction is an exception as well because we might
* have used bind on a proxy function.
*
* - new Iterator(x) is user-hookable; it returns x.__iterator__() which
* could be any object.
*
* - (new Object(Object)) returns the callee.
*/
MOZ_ASSERT_IF(native != js::proxy_Construct &&
native != js::IteratorConstructor &&
(!callee->is<JSFunction>() || callee->as<JSFunction>().native() != obj_construct),
args.rval().isObject() && callee != &args.rval().toObject());
return true;
}
MOZ_ALWAYS_INLINE bool
CallJSGetterOp(JSContext* cx, GetterOp op, HandleObject obj, HandleId id,
MutableHandleValue vp)
{
JS_CHECK_RECURSION(cx, return false);
assertSameCompartment(cx, obj, id, vp);
bool ok = op(cx, obj, id, vp);
if (ok)
assertSameCompartment(cx, vp);
return ok;
}
MOZ_ALWAYS_INLINE bool
CallJSSetterOp(JSContext* cx, SetterOp op, HandleObject obj, HandleId id, MutableHandleValue vp,
ObjectOpResult& result)
{
JS_CHECK_RECURSION(cx, return false);
assertSameCompartment(cx, obj, id, vp);
return op(cx, obj, id, vp, result);
}
inline bool
CallJSAddPropertyOp(JSContext* cx, JSAddPropertyOp op, HandleObject obj, HandleId id,
HandleValue v)
{
JS_CHECK_RECURSION(cx, return false);
assertSameCompartment(cx, obj, id, v);
return op(cx, obj, id, v);
}
inline bool
CallJSDeletePropertyOp(JSContext* cx, JSDeletePropertyOp op, HandleObject receiver, HandleId id,
ObjectOpResult& result)
{
JS_CHECK_RECURSION(cx, return false);
assertSameCompartment(cx, receiver, id);
if (op)
return op(cx, receiver, id, result);
return result.succeed();
}
inline uintptr_t
GetNativeStackLimit(ExclusiveContext* cx)
{
StackKind kind;
if (cx->isJSContext()) {
kind = cx->asJSContext()->runningWithTrustedPrincipals()
? StackForTrustedScript : StackForUntrustedScript;
} else {
// For other threads, we just use the trusted stack depth, since it's
// unlikely that we'll be mixing trusted and untrusted code together.
kind = StackForTrustedScript;
}
return cx->nativeStackLimit[kind];
}
inline LifoAlloc&
ExclusiveContext::typeLifoAlloc()
{
return zone()->types.typeLifoAlloc;
}
} /* namespace js */
inline void
JSContext::setPendingException(const js::Value& v)
{
// overRecursed_ is set after the fact by ReportOverRecursed.
this->overRecursed_ = false;
this->throwing = true;
this->unwrappedException_ = v;
// We don't use assertSameCompartment here to allow
// js::SetPendingExceptionCrossContext to work.
MOZ_ASSERT_IF(v.isObject(), v.toObject().compartment() == compartment());
}
inline bool
JSContext::runningWithTrustedPrincipals() const
{
return !compartment() || compartment()->principals() == trustedPrincipals();
}
inline void
js::ExclusiveContext::enterCompartment(
JSCompartment* c,
const js::AutoLockForExclusiveAccess* maybeLock /* = nullptr */)
{
enterCompartmentDepth_++;
c->enter();
setCompartment(c, maybeLock);
}
inline void
js::ExclusiveContext::enterNullCompartment()
{
enterCompartmentDepth_++;
setCompartment(nullptr);
}
inline void
js::ExclusiveContext::leaveCompartment(
JSCompartment* oldCompartment,
const js::AutoLockForExclusiveAccess* maybeLock /* = nullptr */)
{
MOZ_ASSERT(hasEnteredCompartment());
enterCompartmentDepth_--;
// Only call leave() after we've setCompartment()-ed away from the current
// compartment.
JSCompartment* startingCompartment = compartment_;
setCompartment(oldCompartment, maybeLock);
if (startingCompartment)
startingCompartment->leave();
}
inline void
js::ExclusiveContext::setCompartment(JSCompartment* comp,
const AutoLockForExclusiveAccess* maybeLock /* = nullptr */)
{
// ExclusiveContexts can only be in the atoms zone or in exclusive zones.
MOZ_ASSERT_IF(!isJSContext() && !runtime_->isAtomsCompartment(comp),
comp->zone()->usedByExclusiveThread);
// Normal JSContexts cannot enter exclusive zones.
MOZ_ASSERT_IF(isJSContext() && comp,
!comp->zone()->usedByExclusiveThread);
// Only one thread can be in the atoms compartment at a time.
MOZ_ASSERT_IF(runtime_->isAtomsCompartment(comp), maybeLock != nullptr);
// Make sure that the atoms compartment has its own zone.
MOZ_ASSERT_IF(comp && !runtime_->isAtomsCompartment(comp),
!comp->zone()->isAtomsZone());
// Both the current and the new compartment should be properly marked as
// entered at this point.
MOZ_ASSERT_IF(compartment_, compartment_->hasBeenEntered());
MOZ_ASSERT_IF(comp, comp->hasBeenEntered());
compartment_ = comp;
zone_ = comp ? comp->zone() : nullptr;
arenas_ = zone_ ? &zone_->arenas : nullptr;
}
inline JSScript*
JSContext::currentScript(jsbytecode** ppc,
MaybeAllowCrossCompartment allowCrossCompartment) const
{
if (ppc)
*ppc = nullptr;
js::Activation* act = activation();
while (act && act->isJit() && !act->asJit()->isActive())
act = act->prev();
if (!act)
return nullptr;
MOZ_ASSERT(act->cx() == this);
if (act->isJit()) {
JSScript* script = nullptr;
js::jit::GetPcScript(const_cast<JSContext*>(this), &script, ppc);
if (!allowCrossCompartment && script->compartment() != compartment()) {
if (ppc)
*ppc = nullptr;
return nullptr;
}
return script;
}
if (act->isWasm())
return nullptr;
MOZ_ASSERT(act->isInterpreter());
js::InterpreterFrame* fp = act->asInterpreter()->current();
MOZ_ASSERT(!fp->runningInJit());
JSScript* script = fp->script();
if (!allowCrossCompartment && script->compartment() != compartment())
return nullptr;
if (ppc) {
*ppc = act->asInterpreter()->regs().pc;
MOZ_ASSERT(script->containsPC(*ppc));
}
return script;
}
#endif /* jscntxtinlines_h */
|