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