[WTF] Remove StaticLock
[WebKit.git] / Source / JavaScriptCore / runtime / VM.cpp
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 #include "config.h"
30 #include "VM.h"
31
32 #include "ArgList.h"
33 #include "ArrayBufferNeuteringWatchpoint.h"
34 #include "BuiltinExecutables.h"
35 #include "BytecodeIntrinsicRegistry.h"
36 #include "CodeBlock.h"
37 #include "CodeCache.h"
38 #include "CommonIdentifiers.h"
39 #include "CommonSlowPaths.h"
40 #include "CustomGetterSetter.h"
41 #include "DFGWorklist.h"
42 #include "DirectEvalExecutable.h"
43 #include "Disassembler.h"
44 #include "ErrorInstance.h"
45 #include "EvalCodeBlock.h"
46 #include "Exception.h"
47 #include "ExecutableToCodeBlockEdge.h"
48 #include "FTLThunks.h"
49 #include "FastMallocAlignedMemoryAllocator.h"
50 #include "FunctionCodeBlock.h"
51 #include "FunctionConstructor.h"
52 #include "FunctionExecutable.h"
53 #include "GCActivityCallback.h"
54 #include "GetterSetter.h"
55 #include "GigacageAlignedMemoryAllocator.h"
56 #include "HasOwnPropertyCache.h"
57 #include "Heap.h"
58 #include "HeapIterationScope.h"
59 #include "HeapProfiler.h"
60 #include "HostCallReturnValue.h"
61 #include "Identifier.h"
62 #include "IncrementalSweeper.h"
63 #include "IndirectEvalExecutable.h"
64 #include "InferredTypeTable.h"
65 #include "InferredValue.h"
66 #include "Interpreter.h"
67 #include "JITCode.h"
68 #include "JITWorklist.h"
69 #include "JSAPIValueWrapper.h"
70 #include "JSArray.h"
71 #include "JSAsyncFunction.h"
72 #include "JSBigInt.h"
73 #include "JSBoundFunction.h"
74 #include "JSCInlines.h"
75 #include "JSCustomGetterSetterFunction.h"
76 #include "JSDestructibleObjectHeapCellType.h"
77 #include "JSFixedArray.h"
78 #include "JSFunction.h"
79 #include "JSGlobalObjectFunctions.h"
80 #include "JSInternalPromiseDeferred.h"
81 #include "JSLock.h"
82 #include "JSMap.h"
83 #include "JSMapIterator.h"
84 #include "JSPromiseDeferred.h"
85 #include "JSPropertyNameEnumerator.h"
86 #include "JSSegmentedVariableObjectHeapCellType.h"
87 #include "JSScriptFetchParameters.h"
88 #include "JSScriptFetcher.h"
89 #include "JSSet.h"
90 #include "JSSetIterator.h"
91 #include "JSSourceCode.h"
92 #include "JSStringHeapCellType.h"
93 #include "JSTemplateObjectDescriptor.h"
94 #include "JSWeakMap.h"
95 #include "JSWeakSet.h"
96 #include "JSWebAssembly.h"
97 #include "JSWebAssemblyCodeBlockHeapCellType.h"
98 #include "JSWithScope.h"
99 #include "LLIntData.h"
100 #include "Lexer.h"
101 #include "Lookup.h"
102 #include "MinimumReservedZoneSize.h"
103 #include "ModuleProgramCodeBlock.h"
104 #include "ModuleProgramExecutable.h"
105 #include "NativeExecutable.h"
106 #include "NativeStdFunctionCell.h"
107 #include "Nodes.h"
108 #include "Parser.h"
109 #include "ProfilerDatabase.h"
110 #include "ProgramCodeBlock.h"
111 #include "ProgramExecutable.h"
112 #include "PromiseDeferredTimer.h"
113 #include "PropertyMapHashTable.h"
114 #include "RegExpCache.h"
115 #include "RegExpObject.h"
116 #include "RegisterAtOffsetList.h"
117 #include "RuntimeType.h"
118 #include "SamplingProfiler.h"
119 #include "ShadowChicken.h"
120 #include "SimpleTypedArrayController.h"
121 #include "SourceProviderCache.h"
122 #include "StackVisitor.h"
123 #include "StrictEvalActivation.h"
124 #include "StrongInlines.h"
125 #include "StructureInlines.h"
126 #include "TestRunnerUtils.h"
127 #include "ThreadLocalCacheInlines.h"
128 #include "ThunkGenerators.h"
129 #include "TypeProfiler.h"
130 #include "TypeProfilerLog.h"
131 #include "UnlinkedCodeBlock.h"
132 #include "VMEntryScope.h"
133 #include "VMInspector.h"
134 #include "WasmWorklist.h"
135 #include "Watchdog.h"
136 #include "WeakGCMapInlines.h"
137 #include "WebAssemblyFunction.h"
138 #include "WebAssemblyWrapperFunction.h"
139 #include <wtf/ProcessID.h>
140 #include <wtf/ReadWriteLock.h>
141 #include <wtf/SimpleStats.h>
142 #include <wtf/StringPrintStream.h>
143 #include <wtf/Threading.h>
144 #include <wtf/text/AtomicStringTable.h>
145 #include <wtf/text/SymbolRegistry.h>
146
147 #if !ENABLE(JIT)
148 #include "CLoopStack.h"
149 #include "CLoopStackInlines.h"
150 #endif
151
152 #if ENABLE(DFG_JIT)
153 #include "ConservativeRoots.h"
154 #endif
155
156 #if ENABLE(REGEXP_TRACING)
157 #include "RegExp.h"
158 #endif
159
160 using namespace WTF;
161
162 namespace JSC {
163
164 // Note: Platform.h will enforce that ENABLE(ASSEMBLER) is true if either
165 // ENABLE(JIT) or ENABLE(YARR_JIT) or both are enabled. The code below
166 // just checks for ENABLE(JIT) or ENABLE(YARR_JIT) with this premise in mind.
167
168 #if ENABLE(ASSEMBLER)
169 static bool enableAssembler(ExecutableAllocator& executableAllocator)
170 {
171     if (!Options::useJIT() && !Options::useRegExpJIT())
172         return false;
173
174     if (!executableAllocator.isValid()) {
175         if (Options::crashIfCantAllocateJITMemory())
176             CRASH();
177         return false;
178     }
179
180     char* canUseJITString = getenv("JavaScriptCoreUseJIT");
181     return !canUseJITString || atoi(canUseJITString);
182 }
183 #endif // ENABLE(!ASSEMBLER)
184
185 bool VM::canUseAssembler()
186 {
187 #if ENABLE(ASSEMBLER)
188     static std::once_flag onceKey;
189     static bool enabled = false;
190     std::call_once(onceKey, [] {
191         enabled = enableAssembler(ExecutableAllocator::singleton());
192     });
193     return enabled;
194 #else
195     return false; // interpreter only
196 #endif
197 }
198
199 bool VM::canUseJIT()
200 {
201 #if ENABLE(JIT)
202     static std::once_flag onceKey;
203     static bool enabled = false;
204     std::call_once(onceKey, [] {
205         enabled = VM::canUseAssembler() && Options::useJIT();
206     });
207     return enabled;
208 #else
209     return false; // interpreter only
210 #endif
211 }
212
213 bool VM::canUseRegExpJIT()
214 {
215 #if ENABLE(YARR_JIT)
216     static std::once_flag onceKey;
217     static bool enabled = false;
218     std::call_once(onceKey, [] {
219         enabled = VM::canUseAssembler() && Options::useRegExpJIT();
220     });
221     return enabled;
222 #else
223     return false; // interpreter only
224 #endif
225 }
226
227 VM::VM(VMType vmType, HeapType heapType)
228     : m_apiLock(adoptRef(new JSLock(this)))
229 #if USE(CF)
230     , m_runLoop(CFRunLoopGetCurrent())
231 #endif // USE(CF)
232     , heap(this, heapType)
233     , fastMallocAllocator(std::make_unique<FastMallocAlignedMemoryAllocator>())
234     , primitiveGigacageAllocator(std::make_unique<GigacageAlignedMemoryAllocator>(Gigacage::Primitive))
235     , jsValueGigacageAllocator(std::make_unique<GigacageAlignedMemoryAllocator>(Gigacage::JSValue))
236     , auxiliaryHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::Auxiliary)))
237     , cellJSValueOOBHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::JSCell)))
238     , cellDangerousBitsHeapCellType(std::make_unique<HeapCellType>(CellAttributes(DoesNotNeedDestruction, HeapCell::JSCell)))
239     , destructibleCellHeapCellType(std::make_unique<HeapCellType>(CellAttributes(NeedsDestruction, HeapCell::JSCell)))
240     , stringHeapCellType(std::make_unique<JSStringHeapCellType>())
241     , destructibleObjectHeapCellType(std::make_unique<JSDestructibleObjectHeapCellType>())
242     , segmentedVariableObjectHeapCellType(std::make_unique<JSSegmentedVariableObjectHeapCellType>())
243 #if ENABLE(WEBASSEMBLY)
244     , webAssemblyCodeBlockHeapCellType(std::make_unique<JSWebAssemblyCodeBlockHeapCellType>())
245 #endif
246     , primitiveGigacageAuxiliarySpace("Primitive Gigacage Auxiliary", heap, auxiliaryHeapCellType.get(), primitiveGigacageAllocator.get())
247     , jsValueGigacageAuxiliarySpace("JSValue Gigacage Auxiliary", heap, auxiliaryHeapCellType.get(), jsValueGigacageAllocator.get())
248     , cellJSValueOOBSpace("JSCell JSValueOOB", heap, cellJSValueOOBHeapCellType.get(), fastMallocAllocator.get())
249     , cellDangerousBitsSpace("JSCell DangerousBits", heap, cellDangerousBitsHeapCellType.get(), fastMallocAllocator.get())
250     , jsValueGigacageCellSpace("JSValue Gigacage JSCell", heap, cellJSValueOOBHeapCellType.get(), jsValueGigacageAllocator.get())
251     , destructibleCellSpace("Destructible JSCell", heap, destructibleCellHeapCellType.get(), fastMallocAllocator.get())
252     , stringSpace("JSString", heap, stringHeapCellType.get(), fastMallocAllocator.get())
253     , destructibleObjectSpace("JSDestructibleObject", heap, destructibleObjectHeapCellType.get(), fastMallocAllocator.get())
254     , eagerlySweptDestructibleObjectSpace("Eagerly Swept JSDestructibleObject", heap, destructibleObjectHeapCellType.get(), fastMallocAllocator.get())
255     , segmentedVariableObjectSpace("JSSegmentedVariableObjectSpace", heap, segmentedVariableObjectHeapCellType.get(), fastMallocAllocator.get())
256 #if ENABLE(WEBASSEMBLY)
257     , webAssemblyCodeBlockSpace("JSWebAssemblyCodeBlockSpace", heap, webAssemblyCodeBlockHeapCellType.get(), fastMallocAllocator.get())
258 #endif
259     , asyncFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSAsyncFunction)
260     , asyncGeneratorFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSAsyncGeneratorFunction)
261     , boundFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSBoundFunction)
262     , customGetterSetterFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSCustomGetterSetterFunction)
263     , directEvalExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), DirectEvalExecutable)
264     , executableToCodeBlockEdgeSpace ISO_SUBSPACE_INIT(heap, cellDangerousBitsHeapCellType.get(), ExecutableToCodeBlockEdge)
265     , functionExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), FunctionExecutable)
266     , functionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSFunction)
267     , generatorFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSGeneratorFunction)
268     , indirectEvalExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), IndirectEvalExecutable)
269     , inferredTypeSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), InferredType)
270     , inferredValueSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), InferredValue)
271     , moduleProgramExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ModuleProgramExecutable)
272     , nativeExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), NativeExecutable)
273     , nativeStdFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), JSNativeStdFunction)
274     , programExecutableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ProgramExecutable)
275     , propertyTableSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), PropertyTable)
276     , structureRareDataSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), StructureRareData)
277     , structureSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), Structure)
278     , weakSetSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSWeakSet)
279     , weakMapSpace ISO_SUBSPACE_INIT(heap, destructibleObjectHeapCellType.get(), JSWeakMap)
280 #if ENABLE(WEBASSEMBLY)
281     , webAssemblyFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), WebAssemblyFunction)
282     , webAssemblyWrapperFunctionSpace ISO_SUBSPACE_INIT(heap, cellJSValueOOBHeapCellType.get(), WebAssemblyWrapperFunction)
283 #endif
284     , executableToCodeBlockEdgesWithConstraints(executableToCodeBlockEdgeSpace)
285     , executableToCodeBlockEdgesWithFinalizers(executableToCodeBlockEdgeSpace)
286     , inferredTypesWithFinalizers(inferredTypeSpace)
287     , inferredValuesWithFinalizers(inferredValueSpace)
288     , evalCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), EvalCodeBlock)
289     , functionCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), FunctionCodeBlock)
290     , moduleProgramCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ModuleProgramCodeBlock)
291     , programCodeBlockSpace ISO_SUBSPACE_INIT(heap, destructibleCellHeapCellType.get(), ProgramCodeBlock)
292     , vmType(vmType)
293     , clientData(0)
294     , topEntryFrame(nullptr)
295     , topCallFrame(CallFrame::noCaller())
296     , promiseDeferredTimer(std::make_unique<PromiseDeferredTimer>(*this))
297     , m_atomicStringTable(vmType == Default ? Thread::current().atomicStringTable() : new AtomicStringTable)
298     , propertyNames(nullptr)
299     , emptyList(new ArgList)
300     , machineCodeBytesPerBytecodeWordForBaselineJIT(std::make_unique<SimpleStats>())
301     , customGetterSetterFunctionMap(*this)
302     , stringCache(*this)
303     , symbolImplToSymbolMap(*this)
304     , structureCache(*this)
305     , interpreter(0)
306     , entryScope(0)
307     , m_regExpCache(new RegExpCache(this))
308 #if ENABLE(REGEXP_TRACING)
309     , m_rtTraceList(new RTTraceList())
310 #endif
311 #if ENABLE(GC_VALIDATION)
312     , m_initializingObjectClass(0)
313 #endif
314     , m_stackPointerAtVMEntry(0)
315     , m_codeCache(std::make_unique<CodeCache>())
316     , m_builtinExecutables(std::make_unique<BuiltinExecutables>(*this))
317     , m_typeProfilerEnabledCount(0)
318     , m_primitiveGigacageEnabled(IsWatched)
319     , m_controlFlowProfilerEnabledCount(0)
320     , m_shadowChicken(std::make_unique<ShadowChicken>())
321 {
322     interpreter = new Interpreter(*this);
323     StackBounds stack = Thread::current().stack();
324     updateSoftReservedZoneSize(Options::softReservedZoneSize());
325     setLastStackTop(stack.origin());
326
327     defaultThreadLocalCache = ThreadLocalCache::create(heap);
328     defaultThreadLocalCache->install(*this);
329
330     // Need to be careful to keep everything consistent here
331     JSLockHolder lock(this);
332     AtomicStringTable* existingEntryAtomicStringTable = Thread::current().setCurrentAtomicStringTable(m_atomicStringTable);
333     propertyNames = new CommonIdentifiers(this);
334     structureStructure.set(*this, Structure::createStructure(*this));
335     structureRareDataStructure.set(*this, StructureRareData::createStructure(*this, 0, jsNull()));
336     terminatedExecutionErrorStructure.set(*this, TerminatedExecutionError::createStructure(*this, 0, jsNull()));
337     stringStructure.set(*this, JSString::createStructure(*this, 0, jsNull()));
338     propertyNameEnumeratorStructure.set(*this, JSPropertyNameEnumerator::createStructure(*this, 0, jsNull()));
339     customGetterSetterStructure.set(*this, CustomGetterSetter::createStructure(*this, 0, jsNull()));
340     domAttributeGetterSetterStructure.set(*this, DOMAttributeGetterSetter::createStructure(*this, 0, jsNull()));
341     scopedArgumentsTableStructure.set(*this, ScopedArgumentsTable::createStructure(*this, 0, jsNull()));
342     apiWrapperStructure.set(*this, JSAPIValueWrapper::createStructure(*this, 0, jsNull()));
343     nativeExecutableStructure.set(*this, NativeExecutable::createStructure(*this, 0, jsNull()));
344     evalExecutableStructure.set(*this, EvalExecutable::createStructure(*this, 0, jsNull()));
345     programExecutableStructure.set(*this, ProgramExecutable::createStructure(*this, 0, jsNull()));
346     functionExecutableStructure.set(*this, FunctionExecutable::createStructure(*this, 0, jsNull()));
347 #if ENABLE(WEBASSEMBLY)
348     webAssemblyCodeBlockStructure.set(*this, JSWebAssemblyCodeBlock::createStructure(*this, 0, jsNull()));
349 #endif
350     moduleProgramExecutableStructure.set(*this, ModuleProgramExecutable::createStructure(*this, 0, jsNull()));
351     regExpStructure.set(*this, RegExp::createStructure(*this, 0, jsNull()));
352     symbolStructure.set(*this, Symbol::createStructure(*this, 0, jsNull()));
353     symbolTableStructure.set(*this, SymbolTable::createStructure(*this, 0, jsNull()));
354     fixedArrayStructure.set(*this, JSFixedArray::createStructure(*this, 0, jsNull()));
355     sourceCodeStructure.set(*this, JSSourceCode::createStructure(*this, 0, jsNull()));
356     scriptFetcherStructure.set(*this, JSScriptFetcher::createStructure(*this, 0, jsNull()));
357     scriptFetchParametersStructure.set(*this, JSScriptFetchParameters::createStructure(*this, 0, jsNull()));
358     structureChainStructure.set(*this, StructureChain::createStructure(*this, 0, jsNull()));
359     sparseArrayValueMapStructure.set(*this, SparseArrayValueMap::createStructure(*this, 0, jsNull()));
360     templateObjectDescriptorStructure.set(*this, JSTemplateObjectDescriptor::createStructure(*this, 0, jsNull()));
361     arrayBufferNeuteringWatchpointStructure.set(*this, ArrayBufferNeuteringWatchpoint::createStructure(*this));
362     unlinkedFunctionExecutableStructure.set(*this, UnlinkedFunctionExecutable::createStructure(*this, 0, jsNull()));
363     unlinkedProgramCodeBlockStructure.set(*this, UnlinkedProgramCodeBlock::createStructure(*this, 0, jsNull()));
364     unlinkedEvalCodeBlockStructure.set(*this, UnlinkedEvalCodeBlock::createStructure(*this, 0, jsNull()));
365     unlinkedFunctionCodeBlockStructure.set(*this, UnlinkedFunctionCodeBlock::createStructure(*this, 0, jsNull()));
366     unlinkedModuleProgramCodeBlockStructure.set(*this, UnlinkedModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
367     propertyTableStructure.set(*this, PropertyTable::createStructure(*this, 0, jsNull()));
368     inferredTypeStructure.set(*this, InferredType::createStructure(*this, 0, jsNull()));
369     inferredTypeTableStructure.set(*this, InferredTypeTable::createStructure(*this, 0, jsNull()));
370     inferredValueStructure.set(*this, InferredValue::createStructure(*this, 0, jsNull()));
371     functionRareDataStructure.set(*this, FunctionRareData::createStructure(*this, 0, jsNull()));
372     exceptionStructure.set(*this, Exception::createStructure(*this, 0, jsNull()));
373     promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
374     internalPromiseDeferredStructure.set(*this, JSInternalPromiseDeferred::createStructure(*this, 0, jsNull()));
375     programCodeBlockStructure.set(*this, ProgramCodeBlock::createStructure(*this, 0, jsNull()));
376     moduleProgramCodeBlockStructure.set(*this, ModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
377     evalCodeBlockStructure.set(*this, EvalCodeBlock::createStructure(*this, 0, jsNull()));
378     functionCodeBlockStructure.set(*this, FunctionCodeBlock::createStructure(*this, 0, jsNull()));
379     hashMapBucketSetStructure.set(*this, HashMapBucket<HashMapBucketDataKey>::createStructure(*this, 0, jsNull()));
380     hashMapBucketMapStructure.set(*this, HashMapBucket<HashMapBucketDataKeyValue>::createStructure(*this, 0, jsNull()));
381     setIteratorStructure.set(*this, JSSetIterator::createStructure(*this, 0, jsNull()));
382     mapIteratorStructure.set(*this, JSMapIterator::createStructure(*this, 0, jsNull()));
383     bigIntStructure.set(*this, JSBigInt::createStructure(*this, 0, jsNull()));
384     executableToCodeBlockEdgeStructure.set(*this, ExecutableToCodeBlockEdge::createStructure(*this, nullptr, jsNull()));
385
386     sentinelSetBucket.set(*this, JSSet::BucketType::createSentinel(*this));
387     sentinelMapBucket.set(*this, JSMap::BucketType::createSentinel(*this));
388
389     nativeStdFunctionCellStructure.set(*this, NativeStdFunctionCell::createStructure(*this, 0, jsNull()));
390     smallStrings.initializeCommonStrings(*this);
391
392     Thread::current().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
393
394 #if ENABLE(JIT)
395     jitStubs = std::make_unique<JITThunks>();
396 #endif
397
398 #if ENABLE(FTL_JIT)
399     ftlThunks = std::make_unique<FTL::Thunks>();
400 #endif // ENABLE(FTL_JIT)
401     
402 #if ENABLE(JIT)
403     initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
404 #endif
405     
406     Gigacage::addPrimitiveDisableCallback(primitiveGigacageDisabledCallback, this);
407
408     heap.notifyIsSafeToCollect();
409     
410     LLInt::Data::performAssertions(*this);
411     
412     if (UNLIKELY(Options::useProfiler())) {
413         m_perBytecodeProfiler = std::make_unique<Profiler::Database>(*this);
414
415         StringPrintStream pathOut;
416         const char* profilerPath = getenv("JSC_PROFILER_PATH");
417         if (profilerPath)
418             pathOut.print(profilerPath, "/");
419         pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
420         m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
421     }
422
423     callFrameForCatch = nullptr;
424
425     // Initialize this last, as a free way of asserting that VM initialization itself
426     // won't use this.
427     m_typedArrayController = adoptRef(new SimpleTypedArrayController());
428
429     m_bytecodeIntrinsicRegistry = std::make_unique<BytecodeIntrinsicRegistry>(*this);
430
431     if (Options::useTypeProfiler())
432         enableTypeProfiler();
433     if (Options::useControlFlowProfiler())
434         enableControlFlowProfiler();
435 #if ENABLE(SAMPLING_PROFILER)
436     if (Options::useSamplingProfiler()) {
437         setShouldBuildPCToCodeOriginMapping();
438         Ref<Stopwatch> stopwatch = Stopwatch::create();
439         stopwatch->start();
440         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
441         if (Options::samplingProfilerPath())
442             m_samplingProfiler->registerForReportAtExit();
443         m_samplingProfiler->start();
444     }
445 #endif // ENABLE(SAMPLING_PROFILER)
446
447     if (Options::alwaysGeneratePCToCodeOriginMap())
448         setShouldBuildPCToCodeOriginMapping();
449
450     if (Options::watchdog()) {
451         Watchdog& watchdog = ensureWatchdog();
452         watchdog.setTimeLimit(Seconds::fromMilliseconds(Options::watchdog()));
453     }
454
455 #if ENABLE(JIT)
456     // Make sure that any stubs that the JIT is going to use are initialized in non-compilation threads.
457     if (canUseJIT()) {
458         getCTIInternalFunctionTrampolineFor(CodeForCall);
459         getCTIInternalFunctionTrampolineFor(CodeForConstruct);
460     }
461 #endif
462
463     VMInspector::instance().add(this);
464 }
465
466 static ReadWriteLock s_destructionLock;
467
468 void waitForVMDestruction()
469 {
470     auto locker = holdLock(s_destructionLock.write());
471 }
472
473 VM::~VM()
474 {
475     auto destructionLocker = holdLock(s_destructionLock.read());
476     
477     Gigacage::removePrimitiveDisableCallback(primitiveGigacageDisabledCallback, this);
478     promiseDeferredTimer->stopRunningTasks();
479 #if ENABLE(WEBASSEMBLY)
480     if (Wasm::existingWorklistOrNull())
481         Wasm::ensureWorklist().stopAllPlansForContext(wasmContext);
482 #endif
483     if (UNLIKELY(m_watchdog))
484         m_watchdog->willDestroyVM(this);
485     m_traps.willDestroyVM();
486     VMInspector::instance().remove(this);
487
488     // Never GC, ever again.
489     heap.incrementDeferralDepth();
490
491 #if ENABLE(SAMPLING_PROFILER)
492     if (m_samplingProfiler) {
493         m_samplingProfiler->reportDataToOptionFile();
494         m_samplingProfiler->shutdown();
495     }
496 #endif // ENABLE(SAMPLING_PROFILER)
497     
498 #if ENABLE(JIT)
499     JITWorklist::instance()->completeAllForVM(*this);
500 #endif // ENABLE(JIT)
501
502 #if ENABLE(DFG_JIT)
503     // Make sure concurrent compilations are done, but don't install them, since there is
504     // no point to doing so.
505     for (unsigned i = DFG::numberOfWorklists(); i--;) {
506         if (DFG::Worklist* worklist = DFG::existingWorklistForIndexOrNull(i)) {
507             worklist->removeNonCompilingPlansForVM(*this);
508             worklist->waitUntilAllPlansForVMAreReady(*this);
509             worklist->removeAllReadyPlansForVM(*this);
510         }
511     }
512 #endif // ENABLE(DFG_JIT)
513     
514     waitForAsynchronousDisassembly();
515     
516     // Clear this first to ensure that nobody tries to remove themselves from it.
517     m_perBytecodeProfiler = nullptr;
518
519     ASSERT(currentThreadIsHoldingAPILock());
520     m_apiLock->willDestroyVM(this);
521     heap.lastChanceToFinalize();
522     
523 #if !USE(FAST_TLS_FOR_TLC)
524     ThreadLocalCache::destructor(threadLocalCacheData);
525 #endif
526
527     delete interpreter;
528 #ifndef NDEBUG
529     interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
530 #endif
531
532     delete emptyList;
533
534     delete propertyNames;
535     if (vmType != Default)
536         delete m_atomicStringTable;
537
538     delete clientData;
539     delete m_regExpCache;
540
541 #if ENABLE(REGEXP_TRACING)
542     delete m_rtTraceList;
543 #endif
544
545 #if ENABLE(DFG_JIT)
546     for (unsigned i = 0; i < m_scratchBuffers.size(); ++i)
547         fastFree(m_scratchBuffers[i]);
548 #endif
549 }
550
551 void VM::primitiveGigacageDisabledCallback(void* argument)
552 {
553     static_cast<VM*>(argument)->primitiveGigacageDisabled();
554 }
555
556 void VM::primitiveGigacageDisabled()
557 {
558     if (m_apiLock->currentThreadIsHoldingLock()) {
559         m_primitiveGigacageEnabled.fireAll(*this, "Primitive gigacage disabled");
560         return;
561     }
562  
563     // This is totally racy, and that's OK. The point is, it's up to the user to ensure that they pass the
564     // uncaged buffer in a nicely synchronized manner.
565     m_needToFirePrimitiveGigacageEnabled = true;
566 }
567
568 void VM::setLastStackTop(void* lastStackTop)
569
570     m_lastStackTop = lastStackTop;
571 }
572
573 Ref<VM> VM::createContextGroup(HeapType heapType)
574 {
575     return adoptRef(*new VM(APIContextGroup, heapType));
576 }
577
578 Ref<VM> VM::create(HeapType heapType)
579 {
580     return adoptRef(*new VM(Default, heapType));
581 }
582
583 bool VM::sharedInstanceExists()
584 {
585     return sharedInstanceInternal();
586 }
587
588 VM& VM::sharedInstance()
589 {
590     GlobalJSLock globalLock;
591     VM*& instance = sharedInstanceInternal();
592     if (!instance)
593         instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
594     return *instance;
595 }
596
597 VM*& VM::sharedInstanceInternal()
598 {
599     static VM* sharedInstance;
600     return sharedInstance;
601 }
602
603 Watchdog& VM::ensureWatchdog()
604 {
605     if (!m_watchdog)
606         m_watchdog = adoptRef(new Watchdog(this));
607     return *m_watchdog;
608 }
609
610 HeapProfiler& VM::ensureHeapProfiler()
611 {
612     if (!m_heapProfiler)
613         m_heapProfiler = std::make_unique<HeapProfiler>(*this);
614     return *m_heapProfiler;
615 }
616
617 #if ENABLE(SAMPLING_PROFILER)
618 SamplingProfiler& VM::ensureSamplingProfiler(RefPtr<Stopwatch>&& stopwatch)
619 {
620     if (!m_samplingProfiler)
621         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
622     return *m_samplingProfiler;
623 }
624 #endif // ENABLE(SAMPLING_PROFILER)
625
626 #if ENABLE(JIT)
627 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
628 {
629     switch (intrinsic) {
630     case CharCodeAtIntrinsic:
631         return charCodeAtThunkGenerator;
632     case CharAtIntrinsic:
633         return charAtThunkGenerator;
634     case Clz32Intrinsic:
635         return clz32ThunkGenerator;
636     case FromCharCodeIntrinsic:
637         return fromCharCodeThunkGenerator;
638     case SqrtIntrinsic:
639         return sqrtThunkGenerator;
640     case AbsIntrinsic:
641         return absThunkGenerator;
642     case FloorIntrinsic:
643         return floorThunkGenerator;
644     case CeilIntrinsic:
645         return ceilThunkGenerator;
646     case TruncIntrinsic:
647         return truncThunkGenerator;
648     case RoundIntrinsic:
649         return roundThunkGenerator;
650     case ExpIntrinsic:
651         return expThunkGenerator;
652     case LogIntrinsic:
653         return logThunkGenerator;
654     case IMulIntrinsic:
655         return imulThunkGenerator;
656     case RandomIntrinsic:
657         return randomThunkGenerator;
658     case BoundThisNoArgsFunctionCallIntrinsic:
659         return boundThisNoArgsFunctionCallGenerator;
660     default:
661         return nullptr;
662     }
663 }
664
665 #endif // ENABLE(JIT)
666
667 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor, const String& name)
668 {
669     return getHostFunction(function, NoIntrinsic, constructor, nullptr, name);
670 }
671
672 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic, NativeFunction constructor, const DOMJIT::Signature* signature, const String& name)
673 {
674 #if ENABLE(JIT)
675     if (canUseJIT()) {
676         return jitStubs->hostFunctionStub(
677             this, function, constructor,
678             intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0,
679             intrinsic, signature, name);
680     }
681 #else // ENABLE(JIT)
682     UNUSED_PARAM(intrinsic);
683 #endif // ENABLE(JIT)
684     return NativeExecutable::create(*this,
685         adoptRef(*new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_call_trampoline), JITCode::HostCallThunk)), function,
686         adoptRef(*new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_construct_trampoline), JITCode::HostCallThunk)), constructor,
687         NoIntrinsic, signature, name);
688 }
689
690 MacroAssemblerCodePtr VM::getCTIInternalFunctionTrampolineFor(CodeSpecializationKind kind)
691 {
692 #if ENABLE(JIT)
693     if (canUseJIT()) {
694         if (kind == CodeForCall)
695             return jitStubs->ctiInternalFunctionCall(this);
696         return jitStubs->ctiInternalFunctionConstruct(this);
697     }
698 #endif
699     if (kind == CodeForCall)
700         return MacroAssemblerCodePtr::createLLIntCodePtr(llint_internal_function_call_trampoline);
701     return MacroAssemblerCodePtr::createLLIntCodePtr(llint_internal_function_construct_trampoline);
702 }
703
704 VM::ClientData::~ClientData()
705 {
706 }
707
708 void VM::resetDateCache()
709 {
710     localTimeOffsetCache.reset();
711     cachedDateString = String();
712     cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
713     dateInstanceCache.reset();
714 }
715
716 void VM::whenIdle(std::function<void()> callback)
717 {
718     if (!entryScope) {
719         callback();
720         return;
721     }
722
723     entryScope->addDidPopListener(callback);
724 }
725
726 void VM::deleteAllLinkedCode(DeleteAllCodeEffort effort)
727 {
728     whenIdle([=] () {
729         heap.deleteAllCodeBlocks(effort);
730     });
731 }
732
733 void VM::deleteAllCode(DeleteAllCodeEffort effort)
734 {
735     whenIdle([=] () {
736         m_codeCache->clear();
737         m_regExpCache->deleteAllCode();
738         heap.deleteAllCodeBlocks(effort);
739         heap.deleteAllUnlinkedCodeBlocks(effort);
740         heap.reportAbandonedObjectGraph();
741     });
742 }
743
744 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
745 {
746     auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
747     if (addResult.isNewEntry)
748         addResult.iterator->value = adoptRef(new SourceProviderCache);
749     return addResult.iterator->value.get();
750 }
751
752 void VM::clearSourceProviderCaches()
753 {
754     sourceProviderCacheMap.clear();
755 }
756
757 void VM::throwException(ExecState* exec, Exception* exception)
758 {
759     if (Options::breakOnThrow()) {
760         CodeBlock* codeBlock = exec->codeBlock();
761         dataLog("Throwing exception in call frame ", RawPointer(exec), " for code block ", codeBlock, "\n");
762         CRASH();
763     }
764
765     ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
766
767     interpreter->notifyDebuggerOfExceptionToBeThrown(*this, exec, exception);
768
769     setException(exception);
770
771 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
772     m_nativeStackTraceOfLastThrow = StackTrace::captureStackTrace(Options::unexpectedExceptionStackTraceLimit());
773     m_throwingThread = &Thread::current();
774 #endif
775 }
776
777 JSValue VM::throwException(ExecState* exec, JSValue thrownValue)
778 {
779     VM& vm = *this;
780     Exception* exception = jsDynamicCast<Exception*>(vm, thrownValue);
781     if (!exception)
782         exception = Exception::create(*this, thrownValue);
783
784     throwException(exec, exception);
785     return JSValue(exception);
786 }
787
788 JSObject* VM::throwException(ExecState* exec, JSObject* error)
789 {
790     return asObject(throwException(exec, JSValue(error)));
791 }
792
793 void VM::setStackPointerAtVMEntry(void* sp)
794 {
795     m_stackPointerAtVMEntry = sp;
796     updateStackLimits();
797 }
798
799 size_t VM::updateSoftReservedZoneSize(size_t softReservedZoneSize)
800 {
801     size_t oldSoftReservedZoneSize = m_currentSoftReservedZoneSize;
802     m_currentSoftReservedZoneSize = softReservedZoneSize;
803 #if !ENABLE(JIT)
804     interpreter->cloopStack().setSoftReservedZoneSize(softReservedZoneSize);
805 #endif
806
807     updateStackLimits();
808
809     return oldSoftReservedZoneSize;
810 }
811
812 #if OS(WINDOWS)
813 // On Windows the reserved stack space consists of committed memory, a guard page, and uncommitted memory,
814 // where the guard page is a barrier between committed and uncommitted memory.
815 // When data from the guard page is read or written, the guard page is moved, and memory is committed.
816 // This is how the system grows the stack.
817 // When using the C stack on Windows we need to precommit the needed stack space.
818 // Otherwise we might crash later if we access uncommitted stack memory.
819 // This can happen if we allocate stack space larger than the page guard size (4K).
820 // The system does not get the chance to move the guard page, and commit more memory,
821 // and we crash if uncommitted memory is accessed.
822 // The MSVC compiler fixes this by inserting a call to the _chkstk() function,
823 // when needed, see http://support.microsoft.com/kb/100775.
824 // By touching every page up to the stack limit with a dummy operation,
825 // we force the system to move the guard page, and commit memory.
826
827 static void preCommitStackMemory(void* stackLimit)
828 {
829     const int pageSize = 4096;
830     for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
831         char ch = *p;
832         *p = ch;
833     }
834 }
835 #endif
836
837 inline void VM::updateStackLimits()
838 {
839 #if OS(WINDOWS)
840     void* lastSoftStackLimit = m_softStackLimit;
841 #endif
842
843     const StackBounds& stack = Thread::current().stack();
844     size_t reservedZoneSize = Options::reservedZoneSize();
845     // We should have already ensured that Options::reservedZoneSize() >= minimumReserveZoneSize at
846     // options initialization time, and the option value should not have been changed thereafter.
847     // We don't have the ability to assert here that it hasn't changed, but we can at least assert
848     // that the value is sane.
849     RELEASE_ASSERT(reservedZoneSize >= minimumReservedZoneSize);
850
851     if (m_stackPointerAtVMEntry) {
852         ASSERT(stack.isGrowingDownward());
853         char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
854         m_softStackLimit = stack.recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_currentSoftReservedZoneSize);
855         m_stackLimit = stack.recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), reservedZoneSize);
856     } else {
857         m_softStackLimit = stack.recursionLimit(m_currentSoftReservedZoneSize);
858         m_stackLimit = stack.recursionLimit(reservedZoneSize);
859     }
860
861 #if OS(WINDOWS)
862     // We only need to precommit stack memory dictated by the VM::m_softStackLimit limit.
863     // This is because VM::m_softStackLimit applies to stack usage by LLINT asm or JIT
864     // generated code which can allocate stack space that the C++ compiler does not know
865     // about. As such, we have to precommit that stack memory manually.
866     //
867     // In contrast, we do not need to worry about VM::m_stackLimit because that limit is
868     // used exclusively by C++ code, and the C++ compiler will automatically commit the
869     // needed stack pages.
870     if (lastSoftStackLimit != m_softStackLimit)
871         preCommitStackMemory(m_softStackLimit);
872 #endif
873 }
874
875 #if ENABLE(DFG_JIT)
876 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
877 {
878     auto lock = holdLock(m_scratchBufferLock);
879     for (auto* scratchBuffer : m_scratchBuffers) {
880         if (scratchBuffer->activeLength()) {
881             void* bufferStart = scratchBuffer->dataBuffer();
882             conservativeRoots.add(bufferStart, static_cast<void*>(static_cast<char*>(bufferStart) + scratchBuffer->activeLength()));
883         }
884     }
885 }
886 #endif
887
888 void logSanitizeStack(VM* vm)
889 {
890     if (Options::verboseSanitizeStack() && vm->topCallFrame) {
891         int dummy;
892         auto& stackBounds = Thread::current().stack();
893         dataLog(
894             "Sanitizing stack for VM = ", RawPointer(vm), " with top call frame at ", RawPointer(vm->topCallFrame),
895             ", current stack pointer at ", RawPointer(&dummy), ", in ",
896             pointerDump(vm->topCallFrame->codeBlock()), ", last code origin = ",
897             vm->topCallFrame->codeOrigin(), ", last stack top = ", RawPointer(vm->lastStackTop()), ", in stack range [", RawPointer(stackBounds.origin()), ", ", RawPointer(stackBounds.end()), "]\n");
898     }
899 }
900
901 #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
902 char* VM::acquireRegExpPatternContexBuffer()
903 {
904     ASSERT(!m_regExpPatternContextLock.isLocked());
905
906     m_regExpPatternContextLock.lock();
907     if (!m_regExpPatternContexBuffer)
908         m_regExpPatternContexBuffer = makeUniqueArray<char>(VM::patternContextBufferSize);
909     return m_regExpPatternContexBuffer.get();
910 }
911
912 void VM::releaseRegExpPatternContexBuffer()
913 {
914     ASSERT(m_regExpPatternContextLock.isLocked());
915
916     m_regExpPatternContextLock.unlock();
917 }
918 #endif
919
920 #if ENABLE(REGEXP_TRACING)
921 void VM::addRegExpToTrace(RegExp* regExp)
922 {
923     gcProtect(regExp);
924     m_rtTraceList->add(regExp);
925 }
926
927 void VM::dumpRegExpTrace()
928 {
929     // The first RegExp object is ignored.  It is create by the RegExpPrototype ctor and not used.
930     RTTraceList::iterator iter = ++m_rtTraceList->begin();
931     
932     if (iter != m_rtTraceList->end()) {
933         dataLogF("\nRegExp Tracing\n");
934         dataLogF("Regular Expression                              8 Bit          16 Bit        match()    Matches    Average\n");
935         dataLogF(" <Match only / Match>                         JIT Addr      JIT Address       calls      found   String len\n");
936         dataLogF("----------------------------------------+----------------+----------------+----------+----------+-----------\n");
937     
938         unsigned reCount = 0;
939     
940         for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
941             (*iter)->printTraceData();
942             gcUnprotect(*iter);
943         }
944
945         dataLogF("%d Regular Expressions\n", reCount);
946     }
947     
948     m_rtTraceList->clear();
949 }
950 #else
951 void VM::dumpRegExpTrace()
952 {
953 }
954 #endif
955
956 WatchpointSet* VM::ensureWatchpointSetForImpureProperty(const Identifier& propertyName)
957 {
958     auto result = m_impurePropertyWatchpointSets.add(propertyName.string(), nullptr);
959     if (result.isNewEntry)
960         result.iterator->value = adoptRef(new WatchpointSet(IsWatched));
961     return result.iterator->value.get();
962 }
963
964 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
965 {
966     ensureWatchpointSetForImpureProperty(propertyName)->add(watchpoint);
967 }
968
969 void VM::addImpureProperty(const String& propertyName)
970 {
971     if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
972         watchpointSet->fireAll(*this, "Impure property added");
973 }
974
975 static bool enableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doEnableWork)
976 {
977     bool needsToRecompile = false;
978     if (!counter) {
979         doEnableWork();
980         needsToRecompile = true;
981     }
982     counter++;
983
984     return needsToRecompile;
985 }
986
987 static bool disableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doDisableWork)
988 {
989     RELEASE_ASSERT(counter > 0);
990     bool needsToRecompile = false;
991     counter--;
992     if (!counter) {
993         doDisableWork();
994         needsToRecompile = true;
995     }
996
997     return needsToRecompile;
998 }
999
1000 bool VM::enableTypeProfiler()
1001 {
1002     auto enableTypeProfiler = [this] () {
1003         this->m_typeProfiler = std::make_unique<TypeProfiler>();
1004         this->m_typeProfilerLog = std::make_unique<TypeProfilerLog>();
1005     };
1006
1007     return enableProfilerWithRespectToCount(m_typeProfilerEnabledCount, enableTypeProfiler);
1008 }
1009
1010 bool VM::disableTypeProfiler()
1011 {
1012     auto disableTypeProfiler = [this] () {
1013         this->m_typeProfiler.reset(nullptr);
1014         this->m_typeProfilerLog.reset(nullptr);
1015     };
1016
1017     return disableProfilerWithRespectToCount(m_typeProfilerEnabledCount, disableTypeProfiler);
1018 }
1019
1020 bool VM::enableControlFlowProfiler()
1021 {
1022     auto enableControlFlowProfiler = [this] () {
1023         this->m_controlFlowProfiler = std::make_unique<ControlFlowProfiler>();
1024     };
1025
1026     return enableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, enableControlFlowProfiler);
1027 }
1028
1029 bool VM::disableControlFlowProfiler()
1030 {
1031     auto disableControlFlowProfiler = [this] () {
1032         this->m_controlFlowProfiler.reset(nullptr);
1033     };
1034
1035     return disableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, disableControlFlowProfiler);
1036 }
1037
1038 void VM::dumpTypeProfilerData()
1039 {
1040     if (!typeProfiler())
1041         return;
1042
1043     typeProfilerLog()->processLogEntries(ASCIILiteral("VM Dump Types"));
1044     typeProfiler()->dumpTypeProfilerData(*this);
1045 }
1046
1047 void VM::queueMicrotask(JSGlobalObject& globalObject, Ref<Microtask>&& task)
1048 {
1049     m_microtaskQueue.append(std::make_unique<QueuedTask>(*this, &globalObject, WTFMove(task)));
1050 }
1051
1052 void VM::drainMicrotasks()
1053 {
1054     while (!m_microtaskQueue.isEmpty())
1055         m_microtaskQueue.takeFirst()->run();
1056 }
1057
1058 void QueuedTask::run()
1059 {
1060     m_microtask->run(m_globalObject->globalExec());
1061 }
1062
1063 void sanitizeStackForVM(VM* vm)
1064 {
1065     logSanitizeStack(vm);
1066     if (vm->topCallFrame) {
1067         auto& stackBounds = Thread::current().stack();
1068         ASSERT(vm->currentThreadIsHoldingAPILock());
1069         ASSERT_UNUSED(stackBounds, stackBounds.contains(vm->lastStackTop()));
1070     }
1071 #if !ENABLE(JIT)
1072     vm->interpreter->cloopStack().sanitizeStack();
1073 #else
1074     sanitizeStackForVMImpl(vm);
1075 #endif
1076 }
1077
1078 size_t VM::committedStackByteCount()
1079 {
1080 #if ENABLE(JIT)
1081     // When using the C stack, we don't know how many stack pages are actually
1082     // committed. So, we use the current stack usage as an estimate.
1083     ASSERT(Thread::current().stack().isGrowingDownward());
1084     int8_t* current = reinterpret_cast<int8_t*>(&current);
1085     int8_t* high = reinterpret_cast<int8_t*>(Thread::current().stack().origin());
1086     return high - current;
1087 #else
1088     return CLoopStack::committedByteCount();
1089 #endif
1090 }
1091
1092 #if !ENABLE(JIT)
1093 bool VM::ensureStackCapacityForCLoop(Register* newTopOfStack)
1094 {
1095     return interpreter->cloopStack().ensureCapacityFor(newTopOfStack);
1096 }
1097
1098 bool VM::isSafeToRecurseSoftCLoop() const
1099 {
1100     return interpreter->cloopStack().isSafeToRecurse();
1101 }
1102 #endif // !ENABLE(JIT)
1103
1104 #if ENABLE(EXCEPTION_SCOPE_VERIFICATION)
1105 void VM::verifyExceptionCheckNeedIsSatisfied(unsigned recursionDepth, ExceptionEventLocation& location)
1106 {
1107     if (!Options::validateExceptionChecks())
1108         return;
1109
1110     if (UNLIKELY(m_needExceptionCheck)) {
1111         auto throwDepth = m_simulatedThrowPointRecursionDepth;
1112         auto& throwLocation = m_simulatedThrowPointLocation;
1113
1114         dataLog(
1115             "ERROR: Unchecked JS exception:\n"
1116             "    This scope can throw a JS exception: ", throwLocation, "\n"
1117             "        (ExceptionScope::m_recursionDepth was ", throwDepth, ")\n"
1118             "    But the exception was unchecked as of this scope: ", location, "\n"
1119             "        (ExceptionScope::m_recursionDepth was ", recursionDepth, ")\n"
1120             "\n");
1121
1122         StringPrintStream out;
1123         std::unique_ptr<StackTrace> currentTrace = StackTrace::captureStackTrace(Options::unexpectedExceptionStackTraceLimit());
1124
1125         if (Options::dumpSimulatedThrows()) {
1126             out.println("The simulated exception was thrown at:");
1127             m_nativeStackTraceOfLastSimulatedThrow->dump(out, "    ");
1128             out.println();
1129         }
1130         out.println("Unchecked exception detected at:");
1131         currentTrace->dump(out, "    ");
1132         out.println();
1133
1134         dataLog(out.toCString());
1135         RELEASE_ASSERT(!m_needExceptionCheck);
1136     }
1137 }
1138 #endif
1139
1140 #if USE(CF)
1141 void VM::registerRunLoopTimer(JSRunLoopTimer* timer)
1142 {
1143     ASSERT(runLoop());
1144     ASSERT(!m_runLoopTimers.contains(timer));
1145     m_runLoopTimers.add(timer);
1146     timer->setRunLoop(runLoop());
1147 }
1148
1149 void VM::unregisterRunLoopTimer(JSRunLoopTimer* timer)
1150 {
1151     ASSERT(m_runLoopTimers.contains(timer));
1152     m_runLoopTimers.remove(timer);
1153     timer->setRunLoop(nullptr);
1154 }
1155
1156 void VM::setRunLoop(CFRunLoopRef runLoop)
1157 {
1158     ASSERT(runLoop);
1159     m_runLoop = runLoop;
1160     for (auto timer : m_runLoopTimers)
1161         timer->setRunLoop(runLoop);
1162 }
1163 #endif // USE(CF)
1164
1165 ScratchBuffer* VM::scratchBufferForSize(size_t size)
1166 {
1167     if (!size)
1168         return nullptr;
1169
1170     auto locker = holdLock(m_scratchBufferLock);
1171
1172     if (size > m_sizeOfLastScratchBuffer) {
1173         // Protect against a N^2 memory usage pathology by ensuring
1174         // that at worst, we get a geometric series, meaning that the
1175         // total memory usage is somewhere around
1176         // max(scratch buffer size) * 4.
1177         m_sizeOfLastScratchBuffer = size * 2;
1178
1179         ScratchBuffer* newBuffer = ScratchBuffer::create(m_sizeOfLastScratchBuffer);
1180         RELEASE_ASSERT(newBuffer);
1181         m_scratchBuffers.append(newBuffer);
1182     }
1183
1184     ScratchBuffer* result = m_scratchBuffers.last();
1185     return result;
1186 }
1187
1188 } // namespace JSC