-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSContext.h
1328 lines (1063 loc) · 42.4 KB
/
JSContext.h
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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/. */
/* JS execution context. */
#ifndef vm_JSContext_h
#define vm_JSContext_h
#include "mozilla/MemoryReporting.h"
#include "jstypes.h" // JS_PUBLIC_API
#include "ds/TraceableFifo.h"
#include "gc/Memory.h"
#include "irregexp/RegExpTypes.h"
#include "js/CharacterEncoding.h"
#include "js/ContextOptions.h" // JS::ContextOptions
#include "js/GCVector.h"
#include "js/Promise.h"
#include "js/Result.h"
#include "js/Utility.h"
#include "js/Vector.h"
#include "threading/ProtectedData.h"
#include "util/StructuredSpewer.h"
#include "vm/Activation.h" // js::Activation
#include "vm/ErrorReporting.h"
#include "vm/MallocProvider.h"
#include "vm/Runtime.h"
struct JS_PUBLIC_API JSContext;
struct DtoaState;
namespace js {
class AutoAllocInAtomsZone;
class AutoMaybeLeaveAtomsZone;
class AutoRealm;
namespace frontend {
class WellKnownParserAtoms;
} // namespace frontend
namespace jit {
class ICScript;
class JitActivation;
class JitContext;
class DebugModeOSRVolatileJitFrameIter;
} // namespace jit
namespace gc {
class AutoCheckCanAccessAtomsDuringGC;
class AutoSuppressNurseryCellAlloc;
} // namespace gc
/* Detects cycles when traversing an object graph. */
class MOZ_RAII AutoCycleDetector {
public:
using Vector = GCVector<JSObject*, 8>;
AutoCycleDetector(JSContext* cx,
HandleObject objArg MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: cx(cx), obj(cx, objArg), cyclic(true) {
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
}
~AutoCycleDetector();
bool init();
bool foundCycle() { return cyclic; }
private:
JSContext* cx;
RootedObject obj;
bool cyclic;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
struct AutoResolving;
struct HelperThread;
struct ParseTask;
class InternalJobQueue : public JS::JobQueue {
public:
explicit InternalJobQueue(JSContext* cx)
: queue(cx, SystemAllocPolicy()), draining_(false), interrupted_(false) {}
~InternalJobQueue() = default;
// JS::JobQueue methods.
JSObject* getIncumbentGlobal(JSContext* cx) override;
bool enqueuePromiseJob(JSContext* cx, JS::HandleObject promise,
JS::HandleObject job, JS::HandleObject allocationSite,
JS::HandleObject incumbentGlobal) override;
void runJobs(JSContext* cx) override;
bool empty() const override;
// If we are currently in a call to runJobs(), make that call stop processing
// jobs once the current one finishes, and return. If we are not currently in
// a call to runJobs, make all future calls return immediately.
void interrupt() { interrupted_ = true; }
// Return the front element of the queue, or nullptr if the queue is empty.
// This is only used by shell testing functions.
JSObject* maybeFront() const;
private:
using Queue = js::TraceableFifo<JSObject*, 0, SystemAllocPolicy>;
JS::PersistentRooted<Queue> queue;
// True if we are in the midst of draining jobs from this queue. We use this
// to avoid re-entry (nested calls simply return immediately).
bool draining_;
// True if we've been asked to interrupt draining jobs. Set by interrupt().
bool interrupted_;
class SavedQueue;
js::UniquePtr<JobQueue::SavedJobQueue> saveJobQueue(JSContext*) override;
};
class AutoLockScriptData;
void ReportOverRecursed(JSContext* cx, unsigned errorNumber);
/* Thread Local Storage slot for storing the context for a thread. */
extern MOZ_THREAD_LOCAL(JSContext*) TlsContext;
enum class ContextKind {
// Context for the main thread of a JSRuntime.
MainThread,
// Context for a helper thread.
HelperThread
};
#ifdef DEBUG
bool CurrentThreadIsParseThread();
#endif
enum class InterruptReason : uint32_t {
GC = 1 << 0,
AttachIonCompilations = 1 << 1,
CallbackUrgent = 1 << 2,
CallbackCanWait = 1 << 3,
};
} /* namespace js */
/*
* A JSContext encapsulates the thread local state used when using the JS
* runtime.
*/
struct JS_PUBLIC_API JSContext : public JS::RootingContext,
public js::MallocProvider<JSContext> {
JSContext(JSRuntime* runtime, const JS::ContextOptions& options);
~JSContext();
bool init(js::ContextKind kind);
private:
js::UnprotectedData<JSRuntime*> runtime_;
js::WriteOnceData<js::ContextKind> kind_;
friend class js::gc::AutoSuppressNurseryCellAlloc;
js::ContextData<size_t> nurserySuppressions_;
js::ContextData<JS::ContextOptions> options_;
// Free lists for allocating in the current zone.
js::ContextData<js::gc::FreeLists*> freeLists_;
// This is reset each time we switch zone, then added to the variable in the
// zone when we switch away from it. This would be a js::ThreadData but we
// need to take its address.
uint32_t allocsThisZoneSinceMinorGC_;
// Free lists for parallel allocation in the atoms zone on helper threads.
js::ContextData<js::gc::FreeLists*> atomsZoneFreeLists_;
js::ContextData<JSFreeOp> defaultFreeOp_;
// Thread that the JSContext is currently running on, if in use.
js::ThreadId currentThread_;
js::ParseTask* parseTask_;
// When a helper thread is using a context, it may need to periodically
// free unused memory.
mozilla::Atomic<bool, mozilla::ReleaseAcquire> freeUnusedMemory;
public:
// This is used by helper threads to change the runtime their context is
// currently operating on.
void setRuntime(JSRuntime* rt);
void setHelperThread(js::AutoLockHelperThreadState& locked);
void clearHelperThread(js::AutoLockHelperThreadState& locked);
bool contextAvailable(js::AutoLockHelperThreadState& locked) {
MOZ_ASSERT(kind_ == js::ContextKind::HelperThread);
return currentThread_ == js::ThreadId();
}
void setFreeUnusedMemory(bool shouldFree) { freeUnusedMemory = shouldFree; }
bool shouldFreeUnusedMemory() const {
return kind_ == js::ContextKind::HelperThread && freeUnusedMemory;
}
bool isMainThreadContext() const {
return kind_ == js::ContextKind::MainThread;
}
bool isHelperThreadContext() const {
return kind_ == js::ContextKind::HelperThread;
}
js::gc::FreeLists& freeLists() {
MOZ_ASSERT(freeLists_);
return *freeLists_;
}
js::gc::FreeLists& atomsZoneFreeLists() {
MOZ_ASSERT(atomsZoneFreeLists_);
return *atomsZoneFreeLists_;
}
template <typename T>
bool isInsideCurrentZone(T thing) const {
return thing->zoneFromAnyThread() == zone_;
}
template <typename T>
inline bool isInsideCurrentCompartment(T thing) const {
return thing->compartment() == compartment();
}
void* onOutOfMemory(js::AllocFunction allocFunc, arena_id_t arena,
size_t nbytes, void* reallocPtr = nullptr) {
if (isHelperThreadContext()) {
addPendingOutOfMemory();
return nullptr;
}
return runtime_->onOutOfMemory(allocFunc, arena, nbytes, reallocPtr, this);
}
/* Clear the pending exception (if any) due to OOM. */
void recoverFromOutOfMemory();
void reportAllocationOverflow() { js::ReportAllocationOverflow(this); }
void noteTenuredAlloc() { allocsThisZoneSinceMinorGC_++; }
uint32_t* addressOfTenuredAllocCount() {
return &allocsThisZoneSinceMinorGC_;
}
uint32_t getAndResetAllocsThisZoneSinceMinorGC() {
uint32_t allocs = allocsThisZoneSinceMinorGC_;
allocsThisZoneSinceMinorGC_ = 0;
return allocs;
}
// Accessors for immutable runtime data.
JSAtomState& names() { return *runtime_->commonNames; }
js::frontend::WellKnownParserAtoms& parserNames() {
return *runtime_->commonParserNames;
}
js::StaticStrings& staticStrings() { return *runtime_->staticStrings; }
js::SharedImmutableStringsCache& sharedImmutableStrings() {
return runtime_->sharedImmutableStrings();
}
bool permanentAtomsPopulated() { return runtime_->permanentAtomsPopulated(); }
const js::FrozenAtomSet& permanentAtoms() {
return *runtime_->permanentAtoms();
}
js::WellKnownSymbols& wellKnownSymbols() {
return *runtime_->wellKnownSymbols;
}
js::PropertyName* emptyString() { return runtime_->emptyString; }
JSFreeOp* defaultFreeOp() { return &defaultFreeOp_.ref(); }
uintptr_t stackLimit(JS::StackKind kind) { return nativeStackLimit[kind]; }
uintptr_t stackLimitForJitCode(JS::StackKind kind);
size_t gcSystemPageSize() { return js::gc::SystemPageSize(); }
/*
* "Entering" a realm changes cx->realm (which changes cx->global). Note
* that this does not push an Activation so it's possible for the caller's
* realm to be != cx->realm(). This is not a problem since, in general, most
* places in the VM cannot know that they were called from script (e.g.,
* they may have been called through the JSAPI via JS_CallFunction) and thus
* cannot expect there is a scripted caller.
*
* Realms should be entered/left in a LIFO fasion. To enter a realm, code
* should prefer using AutoRealm over JS::EnterRealm/JS::LeaveRealm.
*
* Also note that the JIT can enter (same-compartment) realms without going
* through these methods - it will update cx->realm_ directly.
*/
private:
inline void setRealm(JS::Realm* realm);
inline void enterRealm(JS::Realm* realm);
inline void enterAtomsZone();
inline void leaveAtomsZone(JS::Realm* oldRealm);
enum IsAtomsZone { AtomsZone, NotAtomsZone };
inline void setZone(js::Zone* zone, IsAtomsZone isAtomsZone);
friend class js::AutoAllocInAtomsZone;
friend class js::AutoMaybeLeaveAtomsZone;
friend class js::AutoRealm;
public:
inline void enterRealmOf(JSObject* target);
inline void enterRealmOf(JSScript* target);
inline void enterRealmOf(js::ObjectGroup* target);
inline void enterNullRealm();
inline void setRealmForJitExceptionHandler(JS::Realm* realm);
inline void leaveRealm(JS::Realm* oldRealm);
void setParseTask(js::ParseTask* parseTask) { parseTask_ = parseTask; }
js::ParseTask* parseTask() const { return parseTask_; }
bool isNurseryAllocSuppressed() const { return nurserySuppressions_; }
// Threads may freely access any data in their realm, compartment and zone.
JS::Compartment* compartment() const {
return realm_ ? JS::GetCompartmentForRealm(realm_) : nullptr;
}
JS::Realm* realm() const { return realm_; }
#ifdef DEBUG
bool inAtomsZone() const;
#endif
JS::Zone* zone() const {
MOZ_ASSERT_IF(!realm() && zone_, inAtomsZone());
MOZ_ASSERT_IF(realm(), js::GetRealmZone(realm()) == zone_);
return zoneRaw();
}
// For use when the context's zone is being read by another thread and the
// compartment and zone pointers might not be in sync.
JS::Zone* zoneRaw() const { return zone_; }
// For JIT use.
static size_t offsetOfZone() { return offsetof(JSContext, zone_); }
// Zone local methods that can be used freely.
inline js::LifoAlloc& typeLifoAlloc();
// Current global. This is only safe to use within the scope of the
// AutoRealm from which it's called.
inline js::Handle<js::GlobalObject*> global() const;
js::AtomsTable& atoms() { return runtime_->atoms(); }
const JS::Zone* atomsZone(const js::AutoAccessAtomsZone& access) {
return runtime_->atomsZone(access);
}
js::SymbolRegistry& symbolRegistry() { return runtime_->symbolRegistry(); }
// Methods to access runtime data that must be protected by locks.
js::RuntimeScriptDataTable& scriptDataTable(js::AutoLockScriptData& lock) {
return runtime_->scriptDataTable(lock);
}
// Methods to access other runtime data that checks locking internally.
js::gc::AtomMarkingRuntime& atomMarking() { return runtime_->gc.atomMarking; }
void markAtom(JSAtom* atom) { atomMarking().markAtom(this, atom); }
void markAtom(JS::Symbol* symbol) { atomMarking().markAtom(this, symbol); }
void markId(jsid id) { atomMarking().markId(this, id); }
void markAtomValue(const js::Value& value) {
atomMarking().markAtomValue(this, value);
}
// Methods specific to any HelperThread for the context.
bool addPendingCompileError(js::CompileError** err);
void addPendingOverRecursed();
void addPendingOutOfMemory();
bool isCompileErrorPending() const;
JSRuntime* runtime() { return runtime_; }
const JSRuntime* runtime() const { return runtime_; }
static size_t offsetOfRealm() { return offsetof(JSContext, realm_); }
friend class JS::AutoSaveExceptionState;
friend class js::jit::DebugModeOSRVolatileJitFrameIter;
friend void js::ReportOverRecursed(JSContext*, unsigned errorNumber);
private:
static JS::Error reportedError;
static JS::OOM reportedOOM;
public:
inline JS::Result<> boolToResult(bool ok);
/**
* Intentionally awkward signpost method that is stationed on the
* boundary between Result-using and non-Result-using code.
*/
template <typename V, typename E>
bool resultToBool(const JS::Result<V, E>& result) {
return result.isOk();
}
template <typename V, typename E>
V* resultToPtr(const JS::Result<V*, E>& result) {
return result.isOk() ? result.unwrap() : nullptr;
}
mozilla::GenericErrorResult<JS::OOM&> alreadyReportedOOM();
mozilla::GenericErrorResult<JS::Error&> alreadyReportedError();
/*
* Points to the most recent JitActivation pushed on the thread.
* See JitActivation constructor in vm/Stack.cpp
*/
js::ContextData<js::jit::JitActivation*> jitActivation;
// Shim for V8 interfaces used by irregexp code
js::ContextData<js::irregexp::Isolate*> isolate;
/*
* Points to the most recent activation running on the thread.
* See Activation comment in vm/Stack.h.
*/
js::ContextData<js::Activation*> activation_;
/*
* Points to the most recent profiling activation running on the
* thread.
*/
js::Activation* volatile profilingActivation_;
public:
js::Activation* activation() const { return activation_; }
static size_t offsetOfActivation() {
return offsetof(JSContext, activation_);
}
js::Activation* profilingActivation() const { return profilingActivation_; }
static size_t offsetOfProfilingActivation() {
return offsetof(JSContext, profilingActivation_);
}
static size_t offsetOfJitActivation() {
return offsetof(JSContext, jitActivation);
}
#ifdef DEBUG
static size_t offsetOfInUnsafeCallWithABI() {
return offsetof(JSContext, inUnsafeCallWithABI);
}
#endif
static size_t offsetOfInlinedICScript() {
return offsetof(JSContext, inlinedICScript_);
}
public:
js::InterpreterStack& interpreterStack() {
return runtime()->interpreterStack();
}
/* Base address of the native stack for the current thread. */
uintptr_t nativeStackBase;
public:
/* If non-null, report JavaScript entry points to this monitor. */
js::ContextData<JS::dbg::AutoEntryMonitor*> entryMonitor;
/*
* Stack of debuggers that currently disallow debuggee execution.
*
* When we check for NX we are inside the debuggee compartment, and thus a
* stack of Debuggers that have prevented execution need to be tracked to
* enter the correct Debugger compartment to report the error.
*/
js::ContextData<js::EnterDebuggeeNoExecute*> noExecuteDebuggerTop;
#ifdef DEBUG
js::ContextData<uint32_t> inUnsafeCallWithABI;
js::ContextData<bool> hasAutoUnsafeCallWithABI;
#endif
#ifdef JS_SIMULATOR
private:
js::ContextData<js::jit::Simulator*> simulator_;
public:
js::jit::Simulator* simulator() const;
uintptr_t* addressOfSimulatorStackLimit();
#endif
#ifdef JS_TRACE_LOGGING
js::UnprotectedData<js::TraceLoggerThread*> traceLogger;
#endif
public:
// State used by util/DoubleToString.cpp.
js::ContextData<DtoaState*> dtoaState;
/*
* When this flag is non-zero, any attempt to GC will be skipped. It is used
* to suppress GC when reporting an OOM (see ReportOutOfMemory) and in
* debugging facilities that cannot tolerate a GC and would rather OOM
* immediately, such as utilities exposed to GDB. Setting this flag is
* extremely dangerous and should only be used when in an OOM situation or
* in non-exposed debugging facilities.
*/
js::ContextData<int32_t> suppressGC;
// clang-format off
enum class GCUse {
// This thread is not running in the garbage collector.
None,
// This thread is currently marking GC things. This thread could be the main
// thread or a helper thread doing sweep-marking.
Marking,
// This thread is currently sweeping GC things. This thread could be the
// main thread or a helper thread while the main thread is running the
// mutator.
Sweeping,
// Whether this thread is currently finalizing GC things. This thread could
// be the main thread or a helper thread doing finalization while the main
// thread is running the mutator.
Finalizing
};
// clang-format on
#ifdef DEBUG
// Which part of the garbage collector this context is running at the moment.
js::ContextData<GCUse> gcUse;
// The specific zone currently being swept, if any.
js::ContextData<JS::Zone*> gcSweepZone;
// Whether this thread is currently manipulating possibly-gray GC things.
js::ContextData<size_t> isTouchingGrayThings;
js::ContextData<size_t> noNurseryAllocationCheck;
/*
* If this is 0, all cross-compartment proxies must be registered in the
* wrapper map. This checking must be disabled temporarily while creating
* new wrappers. When non-zero, this records the recursion depth of wrapper
* creation.
*/
js::ContextData<uintptr_t> disableStrictProxyCheckingCount;
bool isNurseryAllocAllowed() { return noNurseryAllocationCheck == 0; }
void disallowNurseryAlloc() { ++noNurseryAllocationCheck; }
void allowNurseryAlloc() {
MOZ_ASSERT(!isNurseryAllocAllowed());
--noNurseryAllocationCheck;
}
bool isStrictProxyCheckingEnabled() {
return disableStrictProxyCheckingCount == 0;
}
void disableStrictProxyChecking() { ++disableStrictProxyCheckingCount; }
void enableStrictProxyChecking() {
MOZ_ASSERT(disableStrictProxyCheckingCount > 0);
--disableStrictProxyCheckingCount;
}
#endif
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT)
// We are currently running a simulated OOM test.
js::ContextData<bool> runningOOMTest;
#endif
// True if we should assert that
// !comp->validAccessPtr || *comp->validAccessPtr
// is true for every |comp| that we run JS code in.
js::ContextData<unsigned> enableAccessValidation;
/*
* Some regions of code are hard for the static rooting hazard analysis to
* understand. In those cases, we trade the static analysis for a dynamic
* analysis. When this is non-zero, we should assert if we trigger, or
* might trigger, a GC.
*/
js::ContextData<int> inUnsafeRegion;
// Count of AutoDisableGenerationalGC instances on the thread's stack.
js::ContextData<unsigned> generationalDisabled;
// Some code cannot tolerate compacting GC so it can be disabled temporarily
// with AutoDisableCompactingGC which uses this counter.
js::ContextData<unsigned> compactingDisabledCount;
bool canCollectAtoms() const {
// TODO: We may be able to improve this by collecting if
// !isOffThreadParseRunning() (bug 1468422).
return !runtime()->hasHelperThreadZones();
}
private:
// Pools used for recycling name maps and vectors when parsing and
// emitting bytecode. Purged on GC when there are no active script
// compilations.
js::ContextData<js::frontend::NameCollectionPool> frontendCollectionPool_;
public:
js::frontend::NameCollectionPool& frontendCollectionPool() {
return frontendCollectionPool_.ref();
}
void verifyIsSafeToGC() {
MOZ_DIAGNOSTIC_ASSERT(!inUnsafeRegion,
"[AutoAssertNoGC] possible GC in GC-unsafe region");
}
/* Whether sampling should be enabled or not. */
private:
mozilla::Atomic<bool, mozilla::SequentiallyConsistent>
suppressProfilerSampling;
public:
bool isProfilerSamplingEnabled() const { return !suppressProfilerSampling; }
void disableProfilerSampling() { suppressProfilerSampling = true; }
void enableProfilerSampling() { suppressProfilerSampling = false; }
// Used by wasm::EnsureThreadSignalHandlers(cx) to install thread signal
// handlers once per JSContext/thread.
bool wasmTriedToInstallSignalHandlers;
bool wasmHaveSignalHandlers;
/* Temporary arena pool used while compiling and decompiling. */
static const size_t TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE = 4 * 1024;
private:
js::ContextData<js::LifoAlloc> tempLifoAlloc_;
public:
js::LifoAlloc& tempLifoAlloc() { return tempLifoAlloc_.ref(); }
const js::LifoAlloc& tempLifoAlloc() const { return tempLifoAlloc_.ref(); }
js::LifoAlloc& tempLifoAllocNoCheck() { return tempLifoAlloc_.refNoCheck(); }
js::ContextData<uint32_t> debuggerMutations;
// Cache for jit::GetPcScript().
js::ContextData<js::UniquePtr<js::jit::PcScriptCache>> ionPcScriptCache;
private:
/* Exception state -- the exception member is a GC root by definition. */
js::ContextData<bool> throwing; /* is there a pending exception? */
js::ContextData<JS::PersistentRooted<JS::Value>>
unwrappedException_; /* most-recently-thrown exception */
js::ContextData<JS::PersistentRooted<js::SavedFrame*>>
unwrappedExceptionStack_; /* stack when the exception was thrown */
JS::Value& unwrappedException() {
if (!unwrappedException_.ref().initialized()) {
unwrappedException_.ref().init(this);
}
return unwrappedException_.ref().get();
}
js::SavedFrame*& unwrappedExceptionStack() {
if (!unwrappedExceptionStack_.ref().initialized()) {
unwrappedExceptionStack_.ref().init(this);
}
return unwrappedExceptionStack_.ref().get();
}
// True if the exception currently being thrown is by result of
// ReportOverRecursed. See Debugger::slowPathOnExceptionUnwind.
js::ContextData<bool> overRecursed_;
// True if propagating a forced return from an interrupt handler during
// debug mode.
js::ContextData<bool> propagatingForcedReturn_;
public:
js::ContextData<int32_t> reportGranularity; /* see vm/Probes.h */
js::ContextData<js::AutoResolving*> resolvingList;
#ifdef DEBUG
js::ContextData<js::AutoEnterPolicy*> enteredPolicy;
#endif
/* True if generating an error, to prevent runaway recursion. */
js::ContextData<bool> generatingError;
private:
/* State for object and array toSource conversion. */
js::ContextData<js::AutoCycleDetector::Vector> cycleDetectorVector_;
public:
js::AutoCycleDetector::Vector& cycleDetectorVector() {
return cycleDetectorVector_.ref();
}
const js::AutoCycleDetector::Vector& cycleDetectorVector() const {
return cycleDetectorVector_.ref();
}
/* Client opaque pointer. */
js::UnprotectedData<void*> data;
void initJitStackLimit();
void resetJitStackLimit();
public:
JS::ContextOptions& options() { return options_.ref(); }
bool runtimeMatches(JSRuntime* rt) const { return runtime_ == rt; }
private:
/*
* Youngest frame of a saved stack that will be picked up as an async stack
* by any new Activation, and is nullptr when no async stack should be used.
*
* The JS::AutoSetAsyncStackForNewCalls class can be used to set this.
*
* New activations will reset this to nullptr on construction after getting
* the current value, and will restore the previous value on destruction.
*/
js::ContextData<JS::PersistentRooted<js::SavedFrame*>>
asyncStackForNewActivations_;
public:
js::SavedFrame*& asyncStackForNewActivations() {
if (!asyncStackForNewActivations_.ref().initialized()) {
asyncStackForNewActivations_.ref().init(this);
}
return asyncStackForNewActivations_.ref().get();
}
/*
* Value of asyncCause to be attached to asyncStackForNewActivations.
*/
js::ContextData<const char*> asyncCauseForNewActivations;
/*
* True if the async call was explicitly requested, e.g. via
* callFunctionWithAsyncStack.
*/
js::ContextData<bool> asyncCallIsExplicit;
bool currentlyRunningInInterpreter() const {
return activation()->isInterpreter();
}
bool currentlyRunningInJit() const { return activation()->isJit(); }
js::InterpreterFrame* interpreterFrame() const {
return activation()->asInterpreter()->current();
}
js::InterpreterRegs& interpreterRegs() const {
return activation()->asInterpreter()->regs();
}
/*
* Get the topmost script and optional pc on the stack. By default, this
* function only returns a JSScript in the current realm, returning nullptr
* if the current script is in a different realm. This behavior can be
* overridden by passing AllowCrossRealm::Allow.
*/
enum class AllowCrossRealm { DontAllow = false, Allow = true };
inline JSScript* currentScript(
jsbytecode** pc = nullptr,
AllowCrossRealm allowCrossRealm = AllowCrossRealm::DontAllow) const;
inline js::Nursery& nursery();
inline void minorGC(JS::GCReason reason);
public:
bool isExceptionPending() const { return throwing; }
MOZ_MUST_USE
bool getPendingException(JS::MutableHandleValue rval);
js::SavedFrame* getPendingExceptionStack();
bool isThrowingOutOfMemory();
bool isThrowingDebuggeeWouldRun();
bool isClosingGenerator();
void setPendingException(JS::HandleValue v, js::HandleSavedFrame stack);
void setPendingExceptionAndCaptureStack(JS::HandleValue v);
void clearPendingException() {
throwing = false;
overRecursed_ = false;
unwrappedException().setUndefined();
unwrappedExceptionStack() = nullptr;
}
bool isThrowingOverRecursed() const { return throwing && overRecursed_; }
bool isPropagatingForcedReturn() const { return propagatingForcedReturn_; }
void setPropagatingForcedReturn() { propagatingForcedReturn_ = true; }
void clearPropagatingForcedReturn() { propagatingForcedReturn_ = false; }
/*
* See JS_SetTrustedPrincipals in jsapi.h.
* Note: !cx->realm() is treated as trusted.
*/
inline bool runningWithTrustedPrincipals();
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
void trace(JSTracer* trc);
inline js::RuntimeCaches& caches();
public:
using InterruptCallbackVector =
js::Vector<JSInterruptCallback, 2, js::SystemAllocPolicy>;
private:
js::ContextData<InterruptCallbackVector> interruptCallbacks_;
public:
InterruptCallbackVector& interruptCallbacks() {
return interruptCallbacks_.ref();
}
js::ContextData<bool> interruptCallbackDisabled;
// Bitfield storing InterruptReason values.
mozilla::Atomic<uint32_t, mozilla::Relaxed> interruptBits_;
// Any thread can call requestInterrupt() to request that this thread
// stop running. To stop this thread, requestInterrupt sets two fields:
// interruptBits_ (a bitset of InterruptReasons) and jitStackLimit_ (set to
// UINTPTR_MAX). The JS engine must continually poll one of these fields
// and call handleInterrupt if either field has the interrupt value.
//
// The point of setting jitStackLimit_ to UINTPTR_MAX is that JIT code
// already needs to guard on jitStackLimit_ in every function prologue to
// avoid stack overflow, so we avoid a second branch on interruptBits_ by
// setting jitStackLimit_ to a value that is guaranteed to fail the guard.)
//
// Note that the writes to interruptBits_ and jitStackLimit_ use a Relaxed
// Atomic so, while the writes are guaranteed to eventually be visible to
// this thread, it can happen in any order. handleInterrupt calls the
// interrupt callback if either is set, so it really doesn't matter as long
// as the JS engine is continually polling at least one field. In corner
// cases, this relaxed ordering could lead to an interrupt handler being
// called twice in succession after a single requestInterrupt call, but
// that's fine.
void requestInterrupt(js::InterruptReason reason);
bool handleInterrupt();
MOZ_ALWAYS_INLINE bool hasAnyPendingInterrupt() const {
static_assert(sizeof(interruptBits_) == sizeof(uint32_t),
"Assumed by JIT callers");
return interruptBits_ != 0;
}
bool hasPendingInterrupt(js::InterruptReason reason) const {
return interruptBits_ & uint32_t(reason);
}
// For JIT use. Points to the inlined ICScript for a baseline script
// being invoked as part of a trial inlining. Contains nullptr at
// all times except for the brief moment between being set in the
// caller and read in the callee's prologue.
js::ContextData<js::jit::ICScript*> inlinedICScript_;
public:
void* addressOfInterruptBits() { return &interruptBits_; }
void* addressOfJitStackLimit() { return &jitStackLimit; }
void* addressOfJitStackLimitNoInterrupt() {
return &jitStackLimitNoInterrupt;
}
void* addressOfZone() { return &zone_; }
const void* addressOfRealm() const { return &realm_; }
void* addressOfInlinedICScript() { return &inlinedICScript_; }
// Futex state, used by Atomics.wait() and Atomics.wake() on the Atomics
// object.
js::FutexThread fx;
// In certain cases, we want to optimize certain opcodes to typed
// instructions, to avoid carrying an extra register to feed into an unbox.
// Unfortunately, that's not always possible. For example, a GetPropertyCacheT
// could return a typed double, but if it takes its out-of-line path, it could
// return an object, and trigger invalidation. The invalidation bailout will
// consider the return value to be a double, and create a garbage Value.
//
// To allow the GetPropertyCacheT optimization, we allow the ability for
// GetPropertyCache to override the return value at the top of the stack - the
// value that will be temporarily corrupt. This special override value is set
// only in callVM() targets that are about to return *and* have invalidated
// their callee.
js::ContextData<js::Value> ionReturnOverride_;
bool hasIonReturnOverride() const {
return !ionReturnOverride_.ref().isMagic(JS_ARG_POISON);
}
js::Value takeIonReturnOverride() {
js::Value v = ionReturnOverride_;
ionReturnOverride_ = js::MagicValue(JS_ARG_POISON);
return v;
}
void setIonReturnOverride(const js::Value& v) {
MOZ_ASSERT(!hasIonReturnOverride());
MOZ_ASSERT(!v.isMagic());
ionReturnOverride_ = v;
}
mozilla::Atomic<uintptr_t, mozilla::Relaxed> jitStackLimit;
// Like jitStackLimit, but not reset to trigger interrupts.
js::ContextData<uintptr_t> jitStackLimitNoInterrupt;
// Queue of pending jobs as described in ES2016 section 8.4.
//
// This is a non-owning pointer to either:
// - a JobQueue implementation the embedding provided by calling
// JS::SetJobQueue, owned by the embedding, or
// - our internal JobQueue implementation, established by calling
// js::UseInternalJobQueues, owned by JSContext::internalJobQueue below.
js::ContextData<JS::JobQueue*> jobQueue;
// If the embedding has called js::UseInternalJobQueues, this is the owning
// pointer to our internal JobQueue implementation, which JSContext::jobQueue
// borrows.
js::ContextData<js::UniquePtr<js::InternalJobQueue>> internalJobQueue;
// True if jobQueue is empty, or we are running the last job in the queue.
// Such conditions permit optimizations around `await` expressions.
js::ContextData<bool> canSkipEnqueuingJobs;
js::ContextData<JS::PromiseRejectionTrackerCallback>
promiseRejectionTrackerCallback;
js::ContextData<void*> promiseRejectionTrackerCallbackData;
JSObject* getIncumbentGlobal(JSContext* cx);
bool enqueuePromiseJob(JSContext* cx, js::HandleFunction job,
js::HandleObject promise,
js::HandleObject incumbentGlobal);
void addUnhandledRejectedPromise(JSContext* cx, js::HandleObject promise);
void removeUnhandledRejectedPromise(JSContext* cx, js::HandleObject promise);
private:
template <class... Args>
inline void checkImpl(const Args&... args);
bool contextChecksEnabled() const {
// Don't perform these checks when called from a finalizer. The checking
// depends on other objects not having been swept yet.
return !RuntimeHeapIsCollecting(runtime()->heapState());
}
public:
// Assert the arguments are in this context's realm (for scripts),
// compartment (for objects) or zone (for strings, symbols).
template <class... Args>
inline void check(const Args&... args);
template <class... Args>
inline void releaseCheck(const Args&... args);
template <class... Args>
MOZ_ALWAYS_INLINE void debugOnlyCheck(const Args&... args);
#ifdef JS_STRUCTURED_SPEW
private:
// Spewer for this thread
js::UnprotectedData<js::StructuredSpewer> structuredSpewer_;
public:
js::StructuredSpewer& spewer() { return structuredSpewer_.ref(); }
#endif
// During debugger evaluations which need to observe native calls, JITs are
// completely disabled. This flag indicates whether we are in this state, and
// the debugger which initiated the evaluation. This debugger has other
// references on the stack and does not need to be traced.
js::ContextData<js::Debugger*> insideDebuggerEvaluationWithOnNativeCallHook;
}; /* struct JSContext */
inline JS::Result<> JSContext::boolToResult(bool ok) {
if (MOZ_LIKELY(ok)) {
MOZ_ASSERT(!isExceptionPending());
MOZ_ASSERT(!isPropagatingForcedReturn());
return JS::Ok();
}
return JS::Result<>(reportedError);