Make a NativeFunction into a class to support pointer profiling.
[WebKit-https.git] / Source / JavaScriptCore / runtime / VM.h
1 /*
2  * Copyright (C) 2008-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #pragma once
30
31 #include "CallData.h"
32 #include "CodeSpecializationKind.h"
33 #include "CompleteSubspace.h"
34 #include "ConcurrentJSLock.h"
35 #include "ControlFlowProfiler.h"
36 #include "DateInstanceCache.h"
37 #include "DeleteAllCodeEffort.h"
38 #include "ExceptionEventLocation.h"
39 #include "ExecutableAllocator.h"
40 #include "FunctionHasExecutedCache.h"
41 #include "Heap.h"
42 #include "Intrinsic.h"
43 #include "IsoCellSet.h"
44 #include "IsoSubspace.h"
45 #include "JITThunks.h"
46 #include "JSCJSValue.h"
47 #include "JSLock.h"
48 #include "MacroAssemblerCodeRef.h"
49 #include "Microtask.h"
50 #include "NumericStrings.h"
51 #include "SmallStrings.h"
52 #include "Strong.h"
53 #include "StructureCache.h"
54 #include "VMEntryRecord.h"
55 #include "VMTraps.h"
56 #include "ThreadLocalCache.h"
57 #include "WasmContext.h"
58 #include "Watchpoint.h"
59 #include <wtf/BumpPointerAllocator.h>
60 #include <wtf/CheckedArithmetic.h>
61 #include <wtf/DateMath.h>
62 #include <wtf/Deque.h>
63 #include <wtf/DoublyLinkedList.h>
64 #include <wtf/Forward.h>
65 #include <wtf/Gigacage.h>
66 #include <wtf/HashMap.h>
67 #include <wtf/HashSet.h>
68 #include <wtf/StackBounds.h>
69 #include <wtf/Stopwatch.h>
70 #include <wtf/ThreadSafeRefCounted.h>
71 #include <wtf/ThreadSpecific.h>
72 #include <wtf/UniqueArray.h>
73 #include <wtf/text/SymbolRegistry.h>
74 #include <wtf/text/WTFString.h>
75 #if ENABLE(REGEXP_TRACING)
76 #include <wtf/ListHashSet.h>
77 #endif
78
79 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
80 #include <wtf/StackTrace.h>
81 #endif
82
83 namespace WTF {
84 class SimpleStats;
85 } // namespace WTF
86 using WTF::SimpleStats;
87
88 namespace JSC {
89
90 class BuiltinExecutables;
91 class BytecodeIntrinsicRegistry;
92 class CodeBlock;
93 class CodeCache;
94 class CommonIdentifiers;
95 class CustomGetterSetter;
96 class DOMAttributeGetterSetter;
97 class ExecState;
98 class Exception;
99 class ExceptionScope;
100 class FastMallocAlignedMemoryAllocator;
101 class GigacageAlignedMemoryAllocator;
102 class HandleStack;
103 class TypeProfiler;
104 class TypeProfilerLog;
105 class HasOwnPropertyCache;
106 class HeapProfiler;
107 class Identifier;
108 class Interpreter;
109 class JSCustomGetterSetterFunction;
110 class JSDestructibleObjectHeapCellType;
111 class JSGlobalObject;
112 class JSObject;
113 class JSRunLoopTimer;
114 class JSSegmentedVariableObjectHeapCellType;
115 class JSStringHeapCellType;
116 class JSWebAssemblyCodeBlockHeapCellType;
117 class JSWebAssemblyInstance;
118 class LLIntOffsetsExtractor;
119 class NativeExecutable;
120 class PromiseDeferredTimer;
121 class RegExpCache;
122 class Register;
123 class RegisterAtOffsetList;
124 #if ENABLE(SAMPLING_PROFILER)
125 class SamplingProfiler;
126 #endif
127 class ShadowChicken;
128 class ScriptExecutable;
129 class SourceProvider;
130 class SourceProviderCache;
131 class StackFrame;
132 class Structure;
133 #if ENABLE(REGEXP_TRACING)
134 class RegExp;
135 #endif
136 class Symbol;
137 class TypedArrayController;
138 class UnlinkedCodeBlock;
139 class UnlinkedEvalCodeBlock;
140 class UnlinkedFunctionExecutable;
141 class UnlinkedProgramCodeBlock;
142 class UnlinkedModuleProgramCodeBlock;
143 class VirtualRegister;
144 class VMEntryScope;
145 class Watchdog;
146 class Watchpoint;
147 class WatchpointSet;
148
149 #if ENABLE(FTL_JIT)
150 namespace FTL {
151 class Thunks;
152 }
153 #endif // ENABLE(FTL_JIT)
154 namespace Profiler {
155 class Database;
156 }
157 namespace DOMJIT {
158 class Signature;
159 }
160
161 struct HashTable;
162 struct Instruction;
163
164 struct LocalTimeOffsetCache {
165     LocalTimeOffsetCache()
166         : start(0.0)
167         , end(-1.0)
168         , increment(0.0)
169         , timeType(WTF::UTCTime)
170     {
171     }
172
173     void reset()
174     {
175         offset = LocalTimeOffset();
176         start = 0.0;
177         end = -1.0;
178         increment = 0.0;
179         timeType = WTF::UTCTime;
180     }
181
182     LocalTimeOffset offset;
183     double start;
184     double end;
185     double increment;
186     WTF::TimeType timeType;
187 };
188
189 class QueuedTask {
190     WTF_MAKE_NONCOPYABLE(QueuedTask);
191     WTF_MAKE_FAST_ALLOCATED;
192 public:
193     void run();
194
195     QueuedTask(VM& vm, JSGlobalObject* globalObject, Ref<Microtask>&& microtask)
196         : m_globalObject(vm, globalObject)
197         , m_microtask(WTFMove(microtask))
198     {
199     }
200
201 private:
202     Strong<JSGlobalObject> m_globalObject;
203     Ref<Microtask> m_microtask;
204 };
205
206 class ConservativeRoots;
207
208 #if COMPILER(MSVC)
209 #pragma warning(push)
210 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
211 #endif
212 struct ScratchBuffer {
213     ScratchBuffer()
214     {
215         u.m_activeLength = 0;
216     }
217
218     static ScratchBuffer* create(size_t size)
219     {
220         ScratchBuffer* result = new (fastMalloc(ScratchBuffer::allocationSize(size))) ScratchBuffer;
221
222         return result;
223     }
224
225     static size_t allocationSize(Checked<size_t> bufferSize) { return (sizeof(ScratchBuffer) + bufferSize).unsafeGet(); }
226     void setActiveLength(size_t activeLength) { u.m_activeLength = activeLength; }
227     size_t activeLength() const { return u.m_activeLength; };
228     size_t* addressOfActiveLength() { return &u.m_activeLength; };
229     void* dataBuffer() { return m_buffer; }
230
231     union {
232         size_t m_activeLength;
233         double pad; // Make sure m_buffer is double aligned.
234     } u;
235 #if CPU(MIPS) && (defined WTF_MIPS_ARCH_REV && WTF_MIPS_ARCH_REV == 2)
236     void* m_buffer[0] __attribute__((aligned(8)));
237 #else
238     void* m_buffer[0];
239 #endif
240 };
241 #if COMPILER(MSVC)
242 #pragma warning(pop)
243 #endif
244
245 class VM : public ThreadSafeRefCounted<VM>, public DoublyLinkedListNode<VM> {
246 public:
247     // WebCore has a one-to-one mapping of threads to VMs;
248     // create() should only be called once
249     // on a thread, this is the 'default' VM (it uses the
250     // thread's default string uniquing table from Thread::current()).
251     // API contexts created using the new context group aware interface
252     // create APIContextGroup objects which require less locking of JSC
253     // than the old singleton APIShared VM created for use by
254     // the original API.
255     enum VMType { Default, APIContextGroup, APIShared };
256
257     struct ClientData {
258         JS_EXPORT_PRIVATE virtual ~ClientData() = 0;
259     };
260
261     bool isSharedInstance() { return vmType == APIShared; }
262     bool usingAPI() { return vmType != Default; }
263     JS_EXPORT_PRIVATE static bool sharedInstanceExists();
264     JS_EXPORT_PRIVATE static VM& sharedInstance();
265
266     JS_EXPORT_PRIVATE static Ref<VM> create(HeapType = SmallHeap);
267     static Ref<VM> createContextGroup(HeapType = SmallHeap);
268     JS_EXPORT_PRIVATE ~VM();
269
270     Watchdog& ensureWatchdog();
271     Watchdog* watchdog() { return m_watchdog.get(); }
272
273     HeapProfiler* heapProfiler() const { return m_heapProfiler.get(); }
274     JS_EXPORT_PRIVATE HeapProfiler& ensureHeapProfiler();
275
276 #if ENABLE(SAMPLING_PROFILER)
277     SamplingProfiler* samplingProfiler() { return m_samplingProfiler.get(); }
278     JS_EXPORT_PRIVATE SamplingProfiler& ensureSamplingProfiler(RefPtr<Stopwatch>&&);
279 #endif
280
281 private:
282     RefPtr<JSLock> m_apiLock;
283 #if USE(CF)
284     // These need to be initialized before heap below.
285     HashSet<JSRunLoopTimer*> m_runLoopTimers;
286     RetainPtr<CFRunLoopRef> m_runLoop;
287 #endif
288
289 public:
290     Heap heap;
291     
292     std::unique_ptr<FastMallocAlignedMemoryAllocator> fastMallocAllocator;
293     std::unique_ptr<GigacageAlignedMemoryAllocator> primitiveGigacageAllocator;
294     std::unique_ptr<GigacageAlignedMemoryAllocator> jsValueGigacageAllocator;
295
296     std::unique_ptr<HeapCellType> auxiliaryJSValueStrictHeapCellType;
297     std::unique_ptr<HeapCellType> auxiliaryDangerousBitsHeapCellType;
298     std::unique_ptr<HeapCellType> cellJSValueOOBHeapCellType;
299     std::unique_ptr<HeapCellType> cellDangerousBitsHeapCellType;
300     std::unique_ptr<HeapCellType> destructibleCellHeapCellType;
301     std::unique_ptr<JSStringHeapCellType> stringHeapCellType;
302     std::unique_ptr<JSDestructibleObjectHeapCellType> destructibleObjectHeapCellType;
303     std::unique_ptr<JSSegmentedVariableObjectHeapCellType> segmentedVariableObjectHeapCellType;
304 #if ENABLE(WEBASSEMBLY)
305     std::unique_ptr<JSWebAssemblyCodeBlockHeapCellType> webAssemblyCodeBlockHeapCellType;
306 #endif
307     
308     CompleteSubspace primitiveGigacageAuxiliarySpace; // Typed arrays, strings, bitvectors, etc go here.
309     CompleteSubspace jsValueGigacageAuxiliarySpace; // Butterflies, arrays of JSValues, etc go here.
310
311     // We make cross-cutting assumptions about typed arrays being in the primitive Gigacage and butterflies
312     // being in the JSValue gigacage. For some types, it's super obvious where they should go, and so we
313     // can hardcode that fact. But sometimes it's not clear, so we abstract it by having a Gigacage::Kind
314     // constant somewhere.
315     // FIXME: Maybe it would be better if everyone abstracted this?
316     // https://bugs.webkit.org/show_bug.cgi?id=175248
317     ALWAYS_INLINE CompleteSubspace& gigacageAuxiliarySpace(Gigacage::Kind kind)
318     {
319         switch (kind) {
320         case Gigacage::Primitive:
321             return primitiveGigacageAuxiliarySpace;
322         case Gigacage::JSValue:
323             return jsValueGigacageAuxiliarySpace;
324         case Gigacage::String:
325             break;
326         }
327         RELEASE_ASSERT_NOT_REACHED();
328         return primitiveGigacageAuxiliarySpace;
329     }
330     
331     // Whenever possible, use subspaceFor<CellType>(vm) to get one of these subspaces.
332     CompleteSubspace cellJSValueOOBSpace;
333     CompleteSubspace cellDangerousBitsSpace;
334     CompleteSubspace jsValueGigacageCellSpace; // FIXME: This space is problematic because we have things in here like DirectArguments and ScopedArguments; those should be split into JSValueOOB cells and JSValueStrict auxiliaries. https://bugs.webkit.org/show_bug.cgi?id=182858
335     CompleteSubspace destructibleCellSpace;
336     CompleteSubspace stringSpace;
337     CompleteSubspace destructibleObjectSpace;
338     CompleteSubspace eagerlySweptDestructibleObjectSpace;
339     CompleteSubspace segmentedVariableObjectSpace;
340 #if ENABLE(WEBASSEMBLY)
341     CompleteSubspace webAssemblyCodeBlockSpace;
342 #endif
343     
344     IsoSubspace asyncFunctionSpace;
345     IsoSubspace asyncGeneratorFunctionSpace;
346     IsoSubspace boundFunctionSpace;
347     IsoSubspace customGetterSetterFunctionSpace;
348     IsoSubspace directArgumentsSpace;
349     IsoSubspace directEvalExecutableSpace;
350     IsoSubspace executableToCodeBlockEdgeSpace;
351     IsoSubspace functionExecutableSpace;
352     IsoSubspace functionSpace;
353     IsoSubspace generatorFunctionSpace;
354     IsoSubspace indirectEvalExecutableSpace;
355     IsoSubspace inferredTypeSpace;
356     IsoSubspace inferredValueSpace;
357     IsoSubspace moduleProgramExecutableSpace;
358     IsoSubspace nativeExecutableSpace;
359     IsoSubspace nativeStdFunctionSpace;
360     IsoSubspace programExecutableSpace;
361     IsoSubspace propertyTableSpace;
362     IsoSubspace structureRareDataSpace;
363     IsoSubspace structureSpace;
364     IsoSubspace weakSetSpace;
365     IsoSubspace weakMapSpace;
366 #if ENABLE(WEBASSEMBLY)
367     IsoSubspace webAssemblyFunctionSpace;
368     IsoSubspace webAssemblyWrapperFunctionSpace;
369 #endif
370     
371     IsoCellSet executableToCodeBlockEdgesWithConstraints;
372     IsoCellSet executableToCodeBlockEdgesWithFinalizers;
373     IsoCellSet inferredTypesWithFinalizers;
374     IsoCellSet inferredValuesWithFinalizers;
375     
376     struct SpaceAndFinalizerSet {
377         IsoSubspace space;
378         IsoCellSet finalizerSet;
379         
380         template<typename... Arguments>
381         SpaceAndFinalizerSet(Arguments&&... arguments)
382             : space(std::forward<Arguments>(arguments)...)
383             , finalizerSet(space)
384         {
385         }
386         
387         static IsoCellSet& finalizerSetFor(Subspace& space)
388         {
389             return *bitwise_cast<IsoCellSet*>(
390                 bitwise_cast<char*>(&space) -
391                 OBJECT_OFFSETOF(SpaceAndFinalizerSet, space) +
392                 OBJECT_OFFSETOF(SpaceAndFinalizerSet, finalizerSet));
393         }
394     };
395     
396     SpaceAndFinalizerSet evalCodeBlockSpace;
397     SpaceAndFinalizerSet functionCodeBlockSpace;
398     SpaceAndFinalizerSet moduleProgramCodeBlockSpace;
399     SpaceAndFinalizerSet programCodeBlockSpace;
400
401     template<typename Func>
402     void forEachCodeBlockSpace(const Func& func)
403     {
404         // This should not include webAssemblyCodeBlockSpace because this is about subsclasses of
405         // JSC::CodeBlock.
406         func(evalCodeBlockSpace);
407         func(functionCodeBlockSpace);
408         func(moduleProgramCodeBlockSpace);
409         func(programCodeBlockSpace);
410     }
411     
412     VMType vmType;
413     ClientData* clientData;
414     EntryFrame* topEntryFrame;
415     // NOTE: When throwing an exception while rolling back the call frame, this may be equal to
416     // topEntryFrame.
417     // FIXME: This should be a void*, because it might not point to a CallFrame.
418     // https://bugs.webkit.org/show_bug.cgi?id=160441
419     ExecState* topCallFrame { nullptr };
420 #if ENABLE(WEBASSEMBLY)
421     Wasm::Context wasmContext;
422 #endif
423     Strong<Structure> structureStructure;
424     Strong<Structure> structureRareDataStructure;
425     Strong<Structure> terminatedExecutionErrorStructure;
426     Strong<Structure> stringStructure;
427     Strong<Structure> propertyNameIteratorStructure;
428     Strong<Structure> propertyNameEnumeratorStructure;
429     Strong<Structure> customGetterSetterStructure;
430     Strong<Structure> domAttributeGetterSetterStructure;
431     Strong<Structure> scopedArgumentsTableStructure;
432     Strong<Structure> apiWrapperStructure;
433     Strong<Structure> nativeExecutableStructure;
434     Strong<Structure> evalExecutableStructure;
435     Strong<Structure> programExecutableStructure;
436     Strong<Structure> functionExecutableStructure;
437 #if ENABLE(WEBASSEMBLY)
438     Strong<Structure> webAssemblyCodeBlockStructure;
439 #endif
440     Strong<Structure> moduleProgramExecutableStructure;
441     Strong<Structure> regExpStructure;
442     Strong<Structure> symbolStructure;
443     Strong<Structure> symbolTableStructure;
444     Strong<Structure> fixedArrayStructure;
445     Strong<Structure> sourceCodeStructure;
446     Strong<Structure> scriptFetcherStructure;
447     Strong<Structure> scriptFetchParametersStructure;
448     Strong<Structure> structureChainStructure;
449     Strong<Structure> sparseArrayValueMapStructure;
450     Strong<Structure> templateObjectDescriptorStructure;
451     Strong<Structure> arrayBufferNeuteringWatchpointStructure;
452     Strong<Structure> unlinkedFunctionExecutableStructure;
453     Strong<Structure> unlinkedProgramCodeBlockStructure;
454     Strong<Structure> unlinkedEvalCodeBlockStructure;
455     Strong<Structure> unlinkedFunctionCodeBlockStructure;
456     Strong<Structure> unlinkedModuleProgramCodeBlockStructure;
457     Strong<Structure> propertyTableStructure;
458     Strong<Structure> inferredTypeStructure;
459     Strong<Structure> inferredTypeTableStructure;
460     Strong<Structure> inferredValueStructure;
461     Strong<Structure> functionRareDataStructure;
462     Strong<Structure> exceptionStructure;
463     Strong<Structure> promiseDeferredStructure;
464     Strong<Structure> internalPromiseDeferredStructure;
465     Strong<Structure> nativeStdFunctionCellStructure;
466     Strong<Structure> programCodeBlockStructure;
467     Strong<Structure> moduleProgramCodeBlockStructure;
468     Strong<Structure> evalCodeBlockStructure;
469     Strong<Structure> functionCodeBlockStructure;
470     Strong<Structure> hashMapBucketSetStructure;
471     Strong<Structure> hashMapBucketMapStructure;
472     Strong<Structure> setIteratorStructure;
473     Strong<Structure> mapIteratorStructure;
474     Strong<Structure> bigIntStructure;
475     Strong<Structure> executableToCodeBlockEdgeStructure;
476
477     Strong<JSCell> emptyPropertyNameEnumerator;
478     Strong<JSCell> sentinelSetBucket;
479     Strong<JSCell> sentinelMapBucket;
480
481     std::unique_ptr<PromiseDeferredTimer> promiseDeferredTimer;
482     
483     JSCell* currentlyDestructingCallbackObject;
484     PoisonedClassInfoPtr currentlyDestructingCallbackObjectClassInfo;
485
486     AtomicStringTable* m_atomicStringTable;
487     WTF::SymbolRegistry m_symbolRegistry;
488     CommonIdentifiers* propertyNames;
489     const ArgList* emptyList;
490     SmallStrings smallStrings;
491     NumericStrings numericStrings;
492     DateInstanceCache dateInstanceCache;
493     std::unique_ptr<SimpleStats> machineCodeBytesPerBytecodeWordForBaselineJIT;
494     WeakGCMap<std::pair<CustomGetterSetter*, int>, JSCustomGetterSetterFunction> customGetterSetterFunctionMap;
495     WeakGCMap<StringImpl*, JSString, PtrHash<StringImpl*>> stringCache;
496     Strong<JSString> lastCachedString;
497
498     AtomicStringTable* atomicStringTable() const { return m_atomicStringTable; }
499     WTF::SymbolRegistry& symbolRegistry() { return m_symbolRegistry; }
500
501     WeakGCMap<SymbolImpl*, Symbol, PtrHash<SymbolImpl*>> symbolImplToSymbolMap;
502
503     enum class DeletePropertyMode {
504         // Default behaviour of deleteProperty, matching the spec.
505         Default,
506         // This setting causes deleteProperty to force deletion of all
507         // properties including those that are non-configurable (DontDelete).
508         IgnoreConfigurable
509     };
510
511     DeletePropertyMode deletePropertyMode()
512     {
513         return m_deletePropertyMode;
514     }
515
516     class DeletePropertyModeScope {
517     public:
518         DeletePropertyModeScope(VM& vm, DeletePropertyMode mode)
519             : m_vm(vm)
520             , m_previousMode(vm.m_deletePropertyMode)
521         {
522             m_vm.m_deletePropertyMode = mode;
523         }
524
525         ~DeletePropertyModeScope()
526         {
527             m_vm.m_deletePropertyMode = m_previousMode;
528         }
529
530     private:
531         VM& m_vm;
532         DeletePropertyMode m_previousMode;
533     };
534
535     static JS_EXPORT_PRIVATE bool canUseAssembler();
536     static JS_EXPORT_PRIVATE bool canUseJIT();
537     static JS_EXPORT_PRIVATE bool canUseRegExpJIT();
538
539     SourceProviderCache* addSourceProviderCache(SourceProvider*);
540     void clearSourceProviderCaches();
541
542     StructureCache structureCache;
543
544     typedef HashMap<RefPtr<SourceProvider>, RefPtr<SourceProviderCache>> SourceProviderCacheMap;
545     SourceProviderCacheMap sourceProviderCacheMap;
546     Interpreter* interpreter;
547 #if ENABLE(JIT)
548     std::unique_ptr<JITThunks> jitStubs;
549     MacroAssemblerCodeRef getCTIStub(ThunkGenerator generator)
550     {
551         return jitStubs->ctiStub(this, generator);
552     }
553
554 #endif // ENABLE(JIT)
555 #if ENABLE(FTL_JIT)
556     std::unique_ptr<FTL::Thunks> ftlThunks;
557 #endif
558     NativeExecutable* getHostFunction(NativeFunction, NativeFunction constructor, const String& name);
559     NativeExecutable* getHostFunction(NativeFunction, Intrinsic, NativeFunction constructor, const DOMJIT::Signature*, const String& name);
560
561     MacroAssemblerCodePtr getCTIInternalFunctionTrampolineFor(CodeSpecializationKind);
562
563     static ptrdiff_t exceptionOffset()
564     {
565         return OBJECT_OFFSETOF(VM, m_exception);
566     }
567
568     static ptrdiff_t callFrameForCatchOffset()
569     {
570         return OBJECT_OFFSETOF(VM, callFrameForCatch);
571     }
572
573     static ptrdiff_t topEntryFrameOffset()
574     {
575         return OBJECT_OFFSETOF(VM, topEntryFrame);
576     }
577
578     void restorePreviousException(Exception* exception) { setException(exception); }
579
580     void clearLastException() { m_lastException = nullptr; }
581
582     ExecState** addressOfCallFrameForCatch() { return &callFrameForCatch; }
583
584     JSCell** addressOfException() { return reinterpret_cast<JSCell**>(&m_exception); }
585
586     Exception* lastException() const { return m_lastException; }
587     JSCell** addressOfLastException() { return reinterpret_cast<JSCell**>(&m_lastException); }
588
589     void setFailNextNewCodeBlock() { m_failNextNewCodeBlock = true; }
590     bool getAndClearFailNextNewCodeBlock()
591     {
592         bool result = m_failNextNewCodeBlock;
593         m_failNextNewCodeBlock = false;
594         return result;
595     }
596     
597     ALWAYS_INLINE Structure* getStructure(StructureID id)
598     {
599         return heap.structureIDTable().get(decontaminate(id));
600     }
601     
602     void* stackPointerAtVMEntry() const { return m_stackPointerAtVMEntry; }
603     void setStackPointerAtVMEntry(void*);
604
605     size_t softReservedZoneSize() const { return m_currentSoftReservedZoneSize; }
606     size_t updateSoftReservedZoneSize(size_t softReservedZoneSize);
607     
608     static size_t committedStackByteCount();
609     inline bool ensureStackCapacityFor(Register* newTopOfStack);
610
611     void* stackLimit() { return m_stackLimit; }
612     void* softStackLimit() { return m_softStackLimit; }
613     void** addressOfSoftStackLimit() { return &m_softStackLimit; }
614 #if !ENABLE(JIT)
615     void* cloopStackLimit() { return m_cloopStackLimit; }
616     void setCLoopStackLimit(void* limit) { m_cloopStackLimit = limit; }
617 #endif
618
619     inline bool isSafeToRecurseSoft() const;
620     bool isSafeToRecurse() const
621     {
622         return isSafeToRecurse(m_stackLimit);
623     }
624
625     void** addressOfLastStackTop() { return &m_lastStackTop; }
626     void* lastStackTop() { return m_lastStackTop; }
627     void setLastStackTop(void*);
628     
629     void firePrimitiveGigacageEnabledIfNecessary()
630     {
631         if (m_needToFirePrimitiveGigacageEnabled) {
632             m_needToFirePrimitiveGigacageEnabled = false;
633             m_primitiveGigacageEnabled.fireAll(*this, "Primitive gigacage disabled asynchronously");
634         }
635     }
636
637     JSValue hostCallReturnValue;
638     unsigned varargsLength;
639     ExecState* newCallFrameReturnValue;
640     ExecState* callFrameForCatch;
641     void* targetMachinePCForThrow;
642     Instruction* targetInterpreterPCForThrow;
643     uint32_t osrExitIndex;
644     void* osrExitJumpDestination;
645     bool isExecutingInRegExpJIT { false };
646
647     // The threading protocol here is as follows:
648     // - You can call scratchBufferForSize from any thread.
649     // - You can only set the ScratchBuffer's activeLength from the main thread.
650     ScratchBuffer* scratchBufferForSize(size_t size);
651
652     EncodedJSValue* exceptionFuzzingBuffer(size_t size)
653     {
654         ASSERT(Options::useExceptionFuzz());
655         if (!m_exceptionFuzzBuffer)
656             m_exceptionFuzzBuffer = MallocPtr<EncodedJSValue>::malloc(size);
657         return m_exceptionFuzzBuffer.get();
658     }
659
660     void gatherConservativeRoots(ConservativeRoots&);
661
662     VMEntryScope* entryScope;
663
664     JSObject* stringRecursionCheckFirstObject { nullptr };
665     HashSet<JSObject*> stringRecursionCheckVisitedObjects;
666     
667 #if !USE(FAST_TLS_FOR_TLC)
668     ThreadLocalCache::Data* threadLocalCacheData { nullptr };
669 #endif
670     RefPtr<ThreadLocalCache> defaultThreadLocalCache;
671
672     LocalTimeOffsetCache localTimeOffsetCache;
673
674     String cachedDateString;
675     double cachedDateStringValue;
676
677     std::unique_ptr<Profiler::Database> m_perBytecodeProfiler;
678     RefPtr<TypedArrayController> m_typedArrayController;
679     RegExpCache* m_regExpCache;
680     BumpPointerAllocator m_regExpAllocator;
681     ConcurrentJSLock m_regExpAllocatorLock;
682
683 #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
684     static constexpr size_t patternContextBufferSize = 8192; // Space allocated to save nested parenthesis context
685     UniqueArray<char> m_regExpPatternContexBuffer;
686     Lock m_regExpPatternContextLock;
687     char* acquireRegExpPatternContexBuffer();
688     void releaseRegExpPatternContexBuffer();
689 #endif
690
691
692     std::unique_ptr<HasOwnPropertyCache> m_hasOwnPropertyCache;
693     ALWAYS_INLINE HasOwnPropertyCache* hasOwnPropertyCache() { return m_hasOwnPropertyCache.get(); }
694     HasOwnPropertyCache* ensureHasOwnPropertyCache();
695
696 #if ENABLE(REGEXP_TRACING)
697     typedef ListHashSet<RegExp*> RTTraceList;
698     RTTraceList* m_rtTraceList;
699 #endif
700
701     JS_EXPORT_PRIVATE void resetDateCache();
702
703     RegExpCache* regExpCache() { return m_regExpCache; }
704 #if ENABLE(REGEXP_TRACING)
705     void addRegExpToTrace(RegExp*);
706 #endif
707     JS_EXPORT_PRIVATE void dumpRegExpTrace();
708
709     bool isCollectorBusyOnCurrentThread() { return heap.isCurrentThreadBusy(); }
710
711 #if ENABLE(GC_VALIDATION)
712     bool isInitializingObject() const; 
713     void setInitializingObjectClass(const ClassInfo*);
714 #endif
715
716     bool currentThreadIsHoldingAPILock() const { return m_apiLock->currentThreadIsHoldingLock(); }
717
718     JSLock& apiLock() { return *m_apiLock; }
719     CodeCache* codeCache() { return m_codeCache.get(); }
720
721     JS_EXPORT_PRIVATE void whenIdle(std::function<void()>);
722
723     JS_EXPORT_PRIVATE void deleteAllCode(DeleteAllCodeEffort);
724     JS_EXPORT_PRIVATE void deleteAllLinkedCode(DeleteAllCodeEffort);
725
726     WatchpointSet* ensureWatchpointSetForImpureProperty(const Identifier&);
727     void registerWatchpointForImpureProperty(const Identifier&, Watchpoint*);
728     
729     // FIXME: Use AtomicString once it got merged with Identifier.
730     JS_EXPORT_PRIVATE void addImpureProperty(const String&);
731     
732     InlineWatchpointSet& primitiveGigacageEnabled() { return m_primitiveGigacageEnabled; }
733
734     BuiltinExecutables* builtinExecutables() { return m_builtinExecutables.get(); }
735
736     bool enableTypeProfiler();
737     bool disableTypeProfiler();
738     TypeProfilerLog* typeProfilerLog() { return m_typeProfilerLog.get(); }
739     TypeProfiler* typeProfiler() { return m_typeProfiler.get(); }
740     JS_EXPORT_PRIVATE void dumpTypeProfilerData();
741
742     FunctionHasExecutedCache* functionHasExecutedCache() { return &m_functionHasExecutedCache; }
743
744     ControlFlowProfiler* controlFlowProfiler() { return m_controlFlowProfiler.get(); }
745     bool enableControlFlowProfiler();
746     bool disableControlFlowProfiler();
747
748     void queueMicrotask(JSGlobalObject&, Ref<Microtask>&&);
749     JS_EXPORT_PRIVATE void drainMicrotasks();
750     void setGlobalConstRedeclarationShouldThrow(bool globalConstRedeclarationThrow) { m_globalConstRedeclarationShouldThrow = globalConstRedeclarationThrow; }
751     ALWAYS_INLINE bool globalConstRedeclarationShouldThrow() const { return m_globalConstRedeclarationShouldThrow; }
752
753     void setShouldBuildPCToCodeOriginMapping() { m_shouldBuildPCToCodeOriginMapping = true; }
754     bool shouldBuilderPCToCodeOriginMapping() const { return m_shouldBuildPCToCodeOriginMapping; }
755
756     BytecodeIntrinsicRegistry& bytecodeIntrinsicRegistry() { return *m_bytecodeIntrinsicRegistry; }
757     
758     ShadowChicken& shadowChicken() { return *m_shadowChicken; }
759     
760     template<typename Func>
761     void logEvent(CodeBlock*, const char* summary, const Func& func);
762
763     std::optional<RefPtr<Thread>> ownerThread() const { return m_apiLock->ownerThread(); }
764
765     VMTraps& traps() { return m_traps; }
766
767     void handleTraps(ExecState* exec, VMTraps::Mask mask = VMTraps::Mask::allEventTypes()) { m_traps.handleTraps(exec, mask); }
768
769     bool needTrapHandling() { return m_traps.needTrapHandling(); }
770     bool needTrapHandling(VMTraps::Mask mask) { return m_traps.needTrapHandling(mask); }
771     void* needTrapHandlingAddress() { return m_traps.needTrapHandlingAddress(); }
772
773     void notifyNeedDebuggerBreak() { m_traps.fireTrap(VMTraps::NeedDebuggerBreak); }
774     void notifyNeedTermination() { m_traps.fireTrap(VMTraps::NeedTermination); }
775     void notifyNeedWatchdogCheck() { m_traps.fireTrap(VMTraps::NeedWatchdogCheck); }
776
777 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
778     StackTrace* nativeStackTraceOfLastThrow() const { return m_nativeStackTraceOfLastThrow.get(); }
779     Thread* throwingThread() const { return m_throwingThread.get(); }
780 #endif
781
782 #if USE(CF)
783     CFRunLoopRef runLoop() const { return m_runLoop.get(); }
784     void registerRunLoopTimer(JSRunLoopTimer*);
785     void unregisterRunLoopTimer(JSRunLoopTimer*);
786     JS_EXPORT_PRIVATE void setRunLoop(CFRunLoopRef);
787 #endif // USE(CF)
788
789 private:
790     friend class LLIntOffsetsExtractor;
791
792     VM(VMType, HeapType);
793     static VM*& sharedInstanceInternal();
794     void createNativeThunk();
795
796     void updateStackLimits();
797
798     bool isSafeToRecurse(void* stackLimit) const
799     {
800         ASSERT(Thread::current().stack().isGrowingDownward());
801         void* curr = reinterpret_cast<void*>(&curr);
802         return curr >= stackLimit;
803     }
804
805     void setException(Exception* exception)
806     {
807         m_exception = exception;
808         m_lastException = exception;
809     }
810     Exception* exception() const
811     {
812 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
813         m_needExceptionCheck = false;
814 #endif
815         return m_exception;
816     }
817     void clearException()
818     {
819 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
820         m_needExceptionCheck = false;
821         m_nativeStackTraceOfLastThrow = nullptr;
822         m_throwingThread = nullptr;
823 #endif
824         m_exception = nullptr;
825     }
826
827 #if !ENABLE(JIT)    
828     bool ensureStackCapacityForCLoop(Register* newTopOfStack);
829     bool isSafeToRecurseSoftCLoop() const;
830 #endif // !ENABLE(JIT)
831
832     JS_EXPORT_PRIVATE void throwException(ExecState*, Exception*);
833     JS_EXPORT_PRIVATE JSValue throwException(ExecState*, JSValue);
834     JS_EXPORT_PRIVATE JSObject* throwException(ExecState*, JSObject*);
835
836 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
837     void verifyExceptionCheckNeedIsSatisfied(unsigned depth, ExceptionEventLocation&);
838 #endif
839     
840     static void primitiveGigacageDisabledCallback(void*);
841     void primitiveGigacageDisabled();
842
843 #if ENABLE(GC_VALIDATION)
844     const ClassInfo* m_initializingObjectClass;
845 #endif
846
847     void* m_stackPointerAtVMEntry;
848     size_t m_currentSoftReservedZoneSize;
849     void* m_stackLimit { nullptr };
850     void* m_softStackLimit { nullptr };
851 #if !ENABLE(JIT)
852     void* m_cloopStackLimit { nullptr };
853 #endif
854     void* m_lastStackTop { nullptr };
855
856     Exception* m_exception { nullptr };
857     Exception* m_lastException { nullptr };
858 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
859     ExceptionScope* m_topExceptionScope { nullptr };
860     ExceptionEventLocation m_simulatedThrowPointLocation;
861     unsigned m_simulatedThrowPointRecursionDepth { 0 };
862     mutable bool m_needExceptionCheck { false };
863     std::unique_ptr<StackTrace> m_nativeStackTraceOfLastThrow;
864     std::unique_ptr<StackTrace> m_nativeStackTraceOfLastSimulatedThrow;
865     RefPtr<Thread> m_throwingThread;
866 #endif
867
868     bool m_failNextNewCodeBlock { false };
869     DeletePropertyMode m_deletePropertyMode { DeletePropertyMode::Default };
870     bool m_globalConstRedeclarationShouldThrow { true };
871     bool m_shouldBuildPCToCodeOriginMapping { false };
872     std::unique_ptr<CodeCache> m_codeCache;
873     std::unique_ptr<BuiltinExecutables> m_builtinExecutables;
874     HashMap<String, RefPtr<WatchpointSet>> m_impurePropertyWatchpointSets;
875     std::unique_ptr<TypeProfiler> m_typeProfiler;
876     std::unique_ptr<TypeProfilerLog> m_typeProfilerLog;
877     unsigned m_typeProfilerEnabledCount;
878     bool m_needToFirePrimitiveGigacageEnabled { false };
879     Lock m_scratchBufferLock;
880     Vector<ScratchBuffer*> m_scratchBuffers;
881     size_t m_sizeOfLastScratchBuffer { 0 };
882     InlineWatchpointSet m_primitiveGigacageEnabled;
883     FunctionHasExecutedCache m_functionHasExecutedCache;
884     std::unique_ptr<ControlFlowProfiler> m_controlFlowProfiler;
885     unsigned m_controlFlowProfilerEnabledCount;
886     Deque<std::unique_ptr<QueuedTask>> m_microtaskQueue;
887     MallocPtr<EncodedJSValue> m_exceptionFuzzBuffer;
888     VMTraps m_traps;
889     RefPtr<Watchdog> m_watchdog;
890     std::unique_ptr<HeapProfiler> m_heapProfiler;
891 #if ENABLE(SAMPLING_PROFILER)
892     RefPtr<SamplingProfiler> m_samplingProfiler;
893 #endif
894     std::unique_ptr<ShadowChicken> m_shadowChicken;
895     std::unique_ptr<BytecodeIntrinsicRegistry> m_bytecodeIntrinsicRegistry;
896
897     VM* m_prev; // Required by DoublyLinkedListNode.
898     VM* m_next; // Required by DoublyLinkedListNode.
899
900     // Friends for exception checking purpose only.
901     friend class Heap;
902     friend class CatchScope;
903     friend class ExceptionScope;
904     friend class ThrowScope;
905     friend class VMTraps;
906     friend class WTF::DoublyLinkedListNode<VM>;
907 };
908
909 #if ENABLE(GC_VALIDATION)
910 inline bool VM::isInitializingObject() const
911 {
912     return !!m_initializingObjectClass;
913 }
914
915 inline void VM::setInitializingObjectClass(const ClassInfo* initializingObjectClass)
916 {
917     m_initializingObjectClass = initializingObjectClass;
918 }
919 #endif
920
921 inline Heap* WeakSet::heap() const
922 {
923     return &m_vm->heap;
924 }
925
926 #if ENABLE(JIT)
927 extern "C" void sanitizeStackForVMImpl(VM*);
928 #endif
929
930 void sanitizeStackForVM(VM*);
931 void logSanitizeStack(VM*);
932
933 } // namespace JSC