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