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