Baseline JIT should be concurrent
[WebKit-https.git] / Source / JavaScriptCore / runtime / VM.cpp
1 /*
2  * Copyright (C) 2008, 2011, 2013-2016 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 "DFGLongLivedState.h"
42 #include "DFGWorklist.h"
43 #include "Disassembler.h"
44 #include "ErrorInstance.h"
45 #include "Exception.h"
46 #include "FTLThunks.h"
47 #include "FunctionConstructor.h"
48 #include "GCActivityCallback.h"
49 #include "GeneratorFrame.h"
50 #include "GetterSetter.h"
51 #include "Heap.h"
52 #include "HeapIterationScope.h"
53 #include "HeapProfiler.h"
54 #include "HostCallReturnValue.h"
55 #include "Identifier.h"
56 #include "IncrementalSweeper.h"
57 #include "InferredTypeTable.h"
58 #include "Interpreter.h"
59 #include "JITCode.h"
60 #include "JITWorklist.h"
61 #include "JSAPIValueWrapper.h"
62 #include "JSArray.h"
63 #include "JSCInlines.h"
64 #include "JSFunction.h"
65 #include "JSGlobalObjectFunctions.h"
66 #include "JSInternalPromiseDeferred.h"
67 #include "JSLexicalEnvironment.h"
68 #include "JSLock.h"
69 #include "JSPromiseDeferred.h"
70 #include "JSPropertyNameEnumerator.h"
71 #include "JSTemplateRegistryKey.h"
72 #include "JSWithScope.h"
73 #include "Lexer.h"
74 #include "Lookup.h"
75 #include "MapData.h"
76 #include "NativeStdFunctionCell.h"
77 #include "Nodes.h"
78 #include "Parser.h"
79 #include "ProfilerDatabase.h"
80 #include "PropertyMapHashTable.h"
81 #include "RegExpCache.h"
82 #include "RegExpObject.h"
83 #include "RegisterAtOffsetList.h"
84 #include "RuntimeType.h"
85 #include "SamplingProfiler.h"
86 #include "ShadowChicken.h"
87 #include "SimpleTypedArrayController.h"
88 #include "SourceProviderCache.h"
89 #include "StackVisitor.h"
90 #include "StrictEvalActivation.h"
91 #include "StrongInlines.h"
92 #include "StructureInlines.h"
93 #include "TypeProfiler.h"
94 #include "TypeProfilerLog.h"
95 #include "UnlinkedCodeBlock.h"
96 #include "VMEntryScope.h"
97 #include "Watchdog.h"
98 #include "WeakGCMapInlines.h"
99 #include "WeakMapData.h"
100 #include <wtf/CurrentTime.h>
101 #include <wtf/ProcessID.h>
102 #include <wtf/RetainPtr.h>
103 #include <wtf/StringPrintStream.h>
104 #include <wtf/Threading.h>
105 #include <wtf/WTFThreadData.h>
106 #include <wtf/text/AtomicStringTable.h>
107 #include <wtf/text/SymbolRegistry.h>
108
109 #if ENABLE(DFG_JIT)
110 #include "ConservativeRoots.h"
111 #endif
112
113 #if ENABLE(REGEXP_TRACING)
114 #include "RegExp.h"
115 #endif
116
117 #if USE(CF)
118 #include <CoreFoundation/CoreFoundation.h>
119 #endif
120
121 using namespace WTF;
122
123 namespace JSC {
124
125 // Note: Platform.h will enforce that ENABLE(ASSEMBLER) is true if either
126 // ENABLE(JIT) or ENABLE(YARR_JIT) or both are enabled. The code below
127 // just checks for ENABLE(JIT) or ENABLE(YARR_JIT) with this premise in mind.
128
129 #if ENABLE(ASSEMBLER)
130 static bool enableAssembler(ExecutableAllocator& executableAllocator)
131 {
132     if (!Options::useJIT() && !Options::useRegExpJIT())
133         return false;
134
135     if (!executableAllocator.isValid()) {
136         if (Options::crashIfCantAllocateJITMemory())
137             CRASH();
138         return false;
139     }
140
141 #if USE(CF) || OS(UNIX)
142     char* canUseJITString = getenv("JavaScriptCoreUseJIT");
143     return !canUseJITString || atoi(canUseJITString);
144 #else
145     return true;
146 #endif
147 }
148 #endif // ENABLE(!ASSEMBLER)
149
150 VM::VM(VMType vmType, HeapType heapType)
151     : m_apiLock(adoptRef(new JSLock(this)))
152 #if ENABLE(ASSEMBLER)
153     , executableAllocator(*this)
154 #endif
155     , heap(this, heapType)
156     , vmType(vmType)
157     , clientData(0)
158     , topVMEntryFrame(nullptr)
159     , topCallFrame(CallFrame::noCaller())
160     , m_atomicStringTable(vmType == Default ? wtfThreadData().atomicStringTable() : new AtomicStringTable)
161     , propertyNames(nullptr)
162     , emptyList(new MarkedArgumentBuffer)
163     , customGetterSetterFunctionMap(*this)
164     , stringCache(*this)
165     , prototypeMap(*this)
166     , interpreter(0)
167     , jsArrayClassInfo(JSArray::info())
168     , jsFinalObjectClassInfo(JSFinalObject::info())
169     , sizeOfLastScratchBuffer(0)
170     , entryScope(0)
171     , m_regExpCache(new RegExpCache(this))
172 #if ENABLE(REGEXP_TRACING)
173     , m_rtTraceList(new RTTraceList())
174 #endif
175 #if ENABLE(ASSEMBLER)
176     , m_canUseAssembler(enableAssembler(executableAllocator))
177 #endif
178 #if ENABLE(JIT)
179     , m_canUseJIT(m_canUseAssembler && Options::useJIT())
180 #endif
181 #if ENABLE(YARR_JIT)
182     , m_canUseRegExpJIT(m_canUseAssembler && Options::useRegExpJIT())
183 #endif
184 #if ENABLE(GC_VALIDATION)
185     , m_initializingObjectClass(0)
186 #endif
187     , m_stackPointerAtVMEntry(0)
188     , m_stackLimit(0)
189 #if !ENABLE(JIT)
190     , m_jsStackLimit(0)
191 #endif
192     , m_codeCache(std::make_unique<CodeCache>())
193     , m_builtinExecutables(std::make_unique<BuiltinExecutables>(*this))
194     , m_typeProfilerEnabledCount(0)
195     , m_controlFlowProfilerEnabledCount(0)
196     , m_shadowChicken(std::make_unique<ShadowChicken>())
197 {
198     interpreter = new Interpreter(*this);
199     StackBounds stack = wtfThreadData().stack();
200     updateReservedZoneSize(Options::reservedZoneSize());
201 #if !ENABLE(JIT)
202     interpreter->stack().setReservedZoneSize(Options::reservedZoneSize());
203 #endif
204     setLastStackTop(stack.origin());
205
206     // Need to be careful to keep everything consistent here
207     JSLockHolder lock(this);
208     AtomicStringTable* existingEntryAtomicStringTable = wtfThreadData().setCurrentAtomicStringTable(m_atomicStringTable);
209     propertyNames = new CommonIdentifiers(this);
210     structureStructure.set(*this, Structure::createStructure(*this));
211     structureRareDataStructure.set(*this, StructureRareData::createStructure(*this, 0, jsNull()));
212     terminatedExecutionErrorStructure.set(*this, TerminatedExecutionError::createStructure(*this, 0, jsNull()));
213     stringStructure.set(*this, JSString::createStructure(*this, 0, jsNull()));
214     propertyNameEnumeratorStructure.set(*this, JSPropertyNameEnumerator::createStructure(*this, 0, jsNull()));
215     customGetterSetterStructure.set(*this, CustomGetterSetter::createStructure(*this, 0, jsNull()));
216     scopedArgumentsTableStructure.set(*this, ScopedArgumentsTable::createStructure(*this, 0, jsNull()));
217     apiWrapperStructure.set(*this, JSAPIValueWrapper::createStructure(*this, 0, jsNull()));
218     JSScopeStructure.set(*this, JSScope::createStructure(*this, 0, jsNull()));
219     executableStructure.set(*this, ExecutableBase::createStructure(*this, 0, jsNull()));
220     nativeExecutableStructure.set(*this, NativeExecutable::createStructure(*this, 0, jsNull()));
221     evalExecutableStructure.set(*this, EvalExecutable::createStructure(*this, 0, jsNull()));
222     programExecutableStructure.set(*this, ProgramExecutable::createStructure(*this, 0, jsNull()));
223     functionExecutableStructure.set(*this, FunctionExecutable::createStructure(*this, 0, jsNull()));
224 #if ENABLE(WEBASSEMBLY)
225     webAssemblyExecutableStructure.set(*this, WebAssemblyExecutable::createStructure(*this, 0, jsNull()));
226 #endif
227     moduleProgramExecutableStructure.set(*this, ModuleProgramExecutable::createStructure(*this, 0, jsNull()));
228     regExpStructure.set(*this, RegExp::createStructure(*this, 0, jsNull()));
229     symbolStructure.set(*this, Symbol::createStructure(*this, 0, jsNull()));
230     symbolTableStructure.set(*this, SymbolTable::createStructure(*this, 0, jsNull()));
231     structureChainStructure.set(*this, StructureChain::createStructure(*this, 0, jsNull()));
232     sparseArrayValueMapStructure.set(*this, SparseArrayValueMap::createStructure(*this, 0, jsNull()));
233     templateRegistryKeyStructure.set(*this, JSTemplateRegistryKey::createStructure(*this, 0, jsNull()));
234     arrayBufferNeuteringWatchpointStructure.set(*this, ArrayBufferNeuteringWatchpoint::createStructure(*this));
235     unlinkedFunctionExecutableStructure.set(*this, UnlinkedFunctionExecutable::createStructure(*this, 0, jsNull()));
236     unlinkedProgramCodeBlockStructure.set(*this, UnlinkedProgramCodeBlock::createStructure(*this, 0, jsNull()));
237     unlinkedEvalCodeBlockStructure.set(*this, UnlinkedEvalCodeBlock::createStructure(*this, 0, jsNull()));
238     unlinkedFunctionCodeBlockStructure.set(*this, UnlinkedFunctionCodeBlock::createStructure(*this, 0, jsNull()));
239     unlinkedModuleProgramCodeBlockStructure.set(*this, UnlinkedModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
240     propertyTableStructure.set(*this, PropertyTable::createStructure(*this, 0, jsNull()));
241     weakMapDataStructure.set(*this, WeakMapData::createStructure(*this, 0, jsNull()));
242     inferredValueStructure.set(*this, InferredValue::createStructure(*this, 0, jsNull()));
243     inferredTypeStructure.set(*this, InferredType::createStructure(*this, 0, jsNull()));
244     inferredTypeTableStructure.set(*this, InferredTypeTable::createStructure(*this, 0, jsNull()));
245     functionRareDataStructure.set(*this, FunctionRareData::createStructure(*this, 0, jsNull()));
246     generatorFrameStructure.set(*this, GeneratorFrame::createStructure(*this, 0, jsNull()));
247     exceptionStructure.set(*this, Exception::createStructure(*this, 0, jsNull()));
248     promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
249     internalPromiseDeferredStructure.set(*this, JSInternalPromiseDeferred::createStructure(*this, 0, jsNull()));
250     programCodeBlockStructure.set(*this, ProgramCodeBlock::createStructure(*this, 0, jsNull()));
251     moduleProgramCodeBlockStructure.set(*this, ModuleProgramCodeBlock::createStructure(*this, 0, jsNull()));
252     evalCodeBlockStructure.set(*this, EvalCodeBlock::createStructure(*this, 0, jsNull()));
253     functionCodeBlockStructure.set(*this, FunctionCodeBlock::createStructure(*this, 0, jsNull()));
254 #if ENABLE(WEBASSEMBLY)
255     webAssemblyCodeBlockStructure.set(*this, WebAssemblyCodeBlock::createStructure(*this, 0, jsNull()));
256 #endif
257
258     iterationTerminator.set(*this, JSFinalObject::create(*this, JSFinalObject::createStructure(*this, 0, jsNull(), 1)));
259     nativeStdFunctionCellStructure.set(*this, NativeStdFunctionCell::createStructure(*this, 0, jsNull()));
260     smallStrings.initializeCommonStrings(*this);
261
262     wtfThreadData().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
263
264 #if ENABLE(JIT)
265     jitStubs = std::make_unique<JITThunks>();
266     allCalleeSaveRegisterOffsets = std::make_unique<RegisterAtOffsetList>(RegisterSet::vmCalleeSaveRegisters(), RegisterAtOffsetList::ZeroBased);
267 #endif
268     arityCheckData = std::make_unique<CommonSlowPaths::ArityCheckData>();
269
270 #if ENABLE(FTL_JIT)
271     ftlThunks = std::make_unique<FTL::Thunks>();
272 #endif // ENABLE(FTL_JIT)
273     
274     interpreter->initialize();
275     
276 #if ENABLE(JIT)
277     initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
278 #endif
279
280     heap.notifyIsSafeToCollect();
281     
282     LLInt::Data::performAssertions(*this);
283     
284     if (Options::useProfiler()) {
285         m_perBytecodeProfiler = std::make_unique<Profiler::Database>(*this);
286
287         StringPrintStream pathOut;
288         const char* profilerPath = getenv("JSC_PROFILER_PATH");
289         if (profilerPath)
290             pathOut.print(profilerPath, "/");
291         pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
292         m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
293     }
294
295     callFrameForCatch = nullptr;
296
297 #if ENABLE(DFG_JIT)
298     if (canUseJIT())
299         dfgState = std::make_unique<DFG::LongLivedState>();
300 #endif
301     
302     // Initialize this last, as a free way of asserting that VM initialization itself
303     // won't use this.
304     m_typedArrayController = adoptRef(new SimpleTypedArrayController());
305
306     m_bytecodeIntrinsicRegistry = std::make_unique<BytecodeIntrinsicRegistry>(*this);
307
308     if (Options::useTypeProfiler())
309         enableTypeProfiler();
310     if (Options::useControlFlowProfiler())
311         enableControlFlowProfiler();
312 #if ENABLE(SAMPLING_PROFILER)
313     if (Options::useSamplingProfiler()) {
314         setShouldBuildPCToCodeOriginMapping();
315         Ref<Stopwatch> stopwatch = Stopwatch::create();
316         stopwatch->start();
317         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
318         if (Options::samplingProfilerPath())
319             m_samplingProfiler->registerForReportAtExit();
320         m_samplingProfiler->start();
321     }
322 #endif // ENABLE(SAMPLING_PROFILER)
323
324     if (Options::alwaysGeneratePCToCodeOriginMap())
325         setShouldBuildPCToCodeOriginMapping();
326
327     if (Options::watchdog()) {
328         std::chrono::milliseconds timeoutMillis(Options::watchdog());
329         Watchdog& watchdog = ensureWatchdog();
330         watchdog.setTimeLimit(timeoutMillis);
331     }
332 }
333
334 VM::~VM()
335 {
336     // Never GC, ever again.
337     heap.incrementDeferralDepth();
338
339 #if ENABLE(SAMPLING_PROFILER)
340     if (m_samplingProfiler) {
341         m_samplingProfiler->reportDataToOptionFile();
342         m_samplingProfiler->shutdown();
343     }
344 #endif // ENABLE(SAMPLING_PROFILER)
345     
346 #if ENABLE(JIT)
347     JITWorklist::instance()->completeAllForVM(*this);
348 #endif // ENABLE(JIT)
349
350 #if ENABLE(DFG_JIT)
351     // Make sure concurrent compilations are done, but don't install them, since there is
352     // no point to doing so.
353     for (unsigned i = DFG::numberOfWorklists(); i--;) {
354         if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i)) {
355             worklist->waitUntilAllPlansForVMAreReady(*this);
356             worklist->removeAllReadyPlansForVM(*this);
357         }
358     }
359 #endif // ENABLE(DFG_JIT)
360     
361     waitForAsynchronousDisassembly();
362     
363     // Clear this first to ensure that nobody tries to remove themselves from it.
364     m_perBytecodeProfiler = nullptr;
365
366     ASSERT(m_apiLock->currentThreadIsHoldingLock());
367     m_apiLock->willDestroyVM(this);
368     heap.lastChanceToFinalize();
369
370     delete interpreter;
371 #ifndef NDEBUG
372     interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
373 #endif
374
375     delete emptyList;
376
377     delete propertyNames;
378     if (vmType != Default)
379         delete m_atomicStringTable;
380
381     delete clientData;
382     delete m_regExpCache;
383 #if ENABLE(REGEXP_TRACING)
384     delete m_rtTraceList;
385 #endif
386
387 #if ENABLE(DFG_JIT)
388     for (unsigned i = 0; i < scratchBuffers.size(); ++i)
389         fastFree(scratchBuffers[i]);
390 #endif
391 }
392
393 void VM::setLastStackTop(void* lastStackTop)
394
395     m_lastStackTop = lastStackTop;
396 }
397
398 Ref<VM> VM::createContextGroup(HeapType heapType)
399 {
400     return adoptRef(*new VM(APIContextGroup, heapType));
401 }
402
403 Ref<VM> VM::create(HeapType heapType)
404 {
405     return adoptRef(*new VM(Default, heapType));
406 }
407
408 Ref<VM> VM::createLeaked(HeapType heapType)
409 {
410     return create(heapType);
411 }
412
413 bool VM::sharedInstanceExists()
414 {
415     return sharedInstanceInternal();
416 }
417
418 VM& VM::sharedInstance()
419 {
420     GlobalJSLock globalLock;
421     VM*& instance = sharedInstanceInternal();
422     if (!instance)
423         instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
424     return *instance;
425 }
426
427 VM*& VM::sharedInstanceInternal()
428 {
429     static VM* sharedInstance;
430     return sharedInstance;
431 }
432
433 Watchdog& VM::ensureWatchdog()
434 {
435     if (!m_watchdog) {
436         m_watchdog = adoptRef(new Watchdog());
437         
438         // The LLINT peeks into the Watchdog object directly. In order to do that,
439         // the LLINT assumes that the internal shape of a std::unique_ptr is the
440         // same as a plain C++ pointer, and loads the address of Watchdog from it.
441         RELEASE_ASSERT(*reinterpret_cast<Watchdog**>(&m_watchdog) == m_watchdog.get());
442
443         // And if we've previously compiled any functions, we need to revert
444         // them because they don't have the needed polling checks for the watchdog
445         // yet.
446         deleteAllCode();
447     }
448     return *m_watchdog;
449 }
450
451 HeapProfiler& VM::ensureHeapProfiler()
452 {
453     if (!m_heapProfiler)
454         m_heapProfiler = std::make_unique<HeapProfiler>(*this);
455     return *m_heapProfiler;
456 }
457
458 #if ENABLE(SAMPLING_PROFILER)
459 SamplingProfiler& VM::ensureSamplingProfiler(RefPtr<Stopwatch>&& stopwatch)
460 {
461     if (!m_samplingProfiler)
462         m_samplingProfiler = adoptRef(new SamplingProfiler(*this, WTFMove(stopwatch)));
463     return *m_samplingProfiler;
464 }
465 #endif // ENABLE(SAMPLING_PROFILER)
466
467 #if ENABLE(JIT)
468 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
469 {
470     switch (intrinsic) {
471     case CharCodeAtIntrinsic:
472         return charCodeAtThunkGenerator;
473     case CharAtIntrinsic:
474         return charAtThunkGenerator;
475     case Clz32Intrinsic:
476         return clz32ThunkGenerator;
477     case FromCharCodeIntrinsic:
478         return fromCharCodeThunkGenerator;
479     case SqrtIntrinsic:
480         return sqrtThunkGenerator;
481     case AbsIntrinsic:
482         return absThunkGenerator;
483     case FloorIntrinsic:
484         return floorThunkGenerator;
485     case CeilIntrinsic:
486         return ceilThunkGenerator;
487     case TruncIntrinsic:
488         return truncThunkGenerator;
489     case RoundIntrinsic:
490         return roundThunkGenerator;
491     case ExpIntrinsic:
492         return expThunkGenerator;
493     case LogIntrinsic:
494         return logThunkGenerator;
495     case IMulIntrinsic:
496         return imulThunkGenerator;
497     case RandomIntrinsic:
498         return randomThunkGenerator;
499     case BoundThisNoArgsFunctionCallIntrinsic:
500         return boundThisNoArgsFunctionCallGenerator;
501     default:
502         return nullptr;
503     }
504 }
505
506 #endif // ENABLE(JIT)
507
508 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor, const String& name)
509 {
510     return getHostFunction(function, NoIntrinsic, constructor, name);
511 }
512
513 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic, NativeFunction constructor, const String& name)
514 {
515 #if ENABLE(JIT)
516     if (canUseJIT()) {
517         return jitStubs->hostFunctionStub(
518             this, function, constructor,
519             intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0,
520             intrinsic, name);
521     }
522 #else // ENABLE(JIT)
523     UNUSED_PARAM(intrinsic);
524 #endif // ENABLE(JIT)
525     return NativeExecutable::create(*this,
526         adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_call_trampoline), JITCode::HostCallThunk)), function,
527         adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_construct_trampoline), JITCode::HostCallThunk)), constructor,
528         NoIntrinsic, name);
529 }
530
531 VM::ClientData::~ClientData()
532 {
533 }
534
535 void VM::resetDateCache()
536 {
537     localTimeOffsetCache.reset();
538     cachedDateString = String();
539     cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
540     dateInstanceCache.reset();
541 }
542
543 void VM::whenIdle(std::function<void()> callback)
544 {
545     if (!entryScope) {
546         callback();
547         return;
548     }
549
550     entryScope->addDidPopListener(callback);
551 }
552
553 void VM::deleteAllLinkedCode()
554 {
555     whenIdle([this]() {
556         heap.deleteAllCodeBlocks();
557     });
558 }
559
560 void VM::deleteAllCode()
561 {
562     whenIdle([this]() {
563         m_codeCache->clear();
564         m_regExpCache->deleteAllCode();
565         heap.deleteAllCodeBlocks();
566         heap.deleteAllUnlinkedCodeBlocks();
567         heap.reportAbandonedObjectGraph();
568     });
569 }
570
571 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
572 {
573     auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
574     if (addResult.isNewEntry)
575         addResult.iterator->value = adoptRef(new SourceProviderCache);
576     return addResult.iterator->value.get();
577 }
578
579 void VM::clearSourceProviderCaches()
580 {
581     sourceProviderCacheMap.clear();
582 }
583
584 void VM::throwException(ExecState* exec, Exception* exception)
585 {
586     if (Options::breakOnThrow()) {
587         dataLog("In call frame ", RawPointer(exec), " for code block ", *exec->codeBlock(), "\n");
588         CRASH();
589     }
590
591     ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
592
593     interpreter->notifyDebuggerOfExceptionToBeThrown(exec, exception);
594
595     setException(exception);
596 }
597
598 JSValue VM::throwException(ExecState* exec, JSValue thrownValue)
599 {
600     Exception* exception = jsDynamicCast<Exception*>(thrownValue);
601     if (!exception)
602         exception = Exception::create(*this, thrownValue);
603
604     throwException(exec, exception);
605     return JSValue(exception);
606 }
607
608 JSObject* VM::throwException(ExecState* exec, JSObject* error)
609 {
610     return asObject(throwException(exec, JSValue(error)));
611 }
612
613 void VM::setStackPointerAtVMEntry(void* sp)
614 {
615     m_stackPointerAtVMEntry = sp;
616     updateStackLimit();
617 }
618
619 size_t VM::updateReservedZoneSize(size_t reservedZoneSize)
620 {
621     size_t oldReservedZoneSize = m_reservedZoneSize;
622     m_reservedZoneSize = reservedZoneSize;
623
624     updateStackLimit();
625
626     return oldReservedZoneSize;
627 }
628
629 #if PLATFORM(WIN)
630 // On Windows the reserved stack space consists of committed memory, a guard page, and uncommitted memory,
631 // where the guard page is a barrier between committed and uncommitted memory.
632 // When data from the guard page is read or written, the guard page is moved, and memory is committed.
633 // This is how the system grows the stack.
634 // When using the C stack on Windows we need to precommit the needed stack space.
635 // Otherwise we might crash later if we access uncommitted stack memory.
636 // This can happen if we allocate stack space larger than the page guard size (4K).
637 // The system does not get the chance to move the guard page, and commit more memory,
638 // and we crash if uncommitted memory is accessed.
639 // The MSVC compiler fixes this by inserting a call to the _chkstk() function,
640 // when needed, see http://support.microsoft.com/kb/100775.
641 // By touching every page up to the stack limit with a dummy operation,
642 // we force the system to move the guard page, and commit memory.
643
644 static void preCommitStackMemory(void* stackLimit)
645 {
646     const int pageSize = 4096;
647     for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
648         char ch = *p;
649         *p = ch;
650     }
651 }
652 #endif
653
654 inline void VM::updateStackLimit()
655 {
656 #if PLATFORM(WIN)
657     void* lastStackLimit = m_stackLimit;
658 #endif
659
660     if (m_stackPointerAtVMEntry) {
661         ASSERT(wtfThreadData().stack().isGrowingDownward());
662         char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
663         m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize);
664     } else {
665         m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize);
666     }
667
668 #if PLATFORM(WIN)
669     if (lastStackLimit != m_stackLimit)
670         preCommitStackMemory(m_stackLimit);
671 #endif
672 }
673
674 #if ENABLE(DFG_JIT)
675 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
676 {
677     for (size_t i = 0; i < scratchBuffers.size(); i++) {
678         ScratchBuffer* scratchBuffer = scratchBuffers[i];
679         if (scratchBuffer->activeLength()) {
680             void* bufferStart = scratchBuffer->dataBuffer();
681             conservativeRoots.add(bufferStart, static_cast<void*>(static_cast<char*>(bufferStart) + scratchBuffer->activeLength()));
682         }
683     }
684 }
685 #endif
686
687 void logSanitizeStack(VM* vm)
688 {
689     if (Options::verboseSanitizeStack() && vm->topCallFrame) {
690         int dummy;
691         dataLog(
692             "Sanitizing stack with top call frame at ", RawPointer(vm->topCallFrame),
693             ", current stack pointer at ", RawPointer(&dummy), ", in ",
694             pointerDump(vm->topCallFrame->codeBlock()), " and last code origin = ",
695             vm->topCallFrame->codeOrigin(), "\n");
696     }
697 }
698
699 #if ENABLE(REGEXP_TRACING)
700 void VM::addRegExpToTrace(RegExp* regExp)
701 {
702     gcProtect(regExp);
703     m_rtTraceList->add(regExp);
704 }
705
706 void VM::dumpRegExpTrace()
707 {
708     // The first RegExp object is ignored.  It is create by the RegExpPrototype ctor and not used.
709     RTTraceList::iterator iter = ++m_rtTraceList->begin();
710     
711     if (iter != m_rtTraceList->end()) {
712         dataLogF("\nRegExp Tracing\n");
713         dataLogF("Regular Expression                              8 Bit          16 Bit        match()    Matches    Average\n");
714         dataLogF(" <Match only / Match>                         JIT Addr      JIT Address       calls      found   String len\n");
715         dataLogF("----------------------------------------+----------------+----------------+----------+----------+-----------\n");
716     
717         unsigned reCount = 0;
718     
719         for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
720             (*iter)->printTraceData();
721             gcUnprotect(*iter);
722         }
723
724         dataLogF("%d Regular Expressions\n", reCount);
725     }
726     
727     m_rtTraceList->clear();
728 }
729 #else
730 void VM::dumpRegExpTrace()
731 {
732 }
733 #endif
734
735 WatchpointSet* VM::ensureWatchpointSetForImpureProperty(const Identifier& propertyName)
736 {
737     auto result = m_impurePropertyWatchpointSets.add(propertyName.string(), nullptr);
738     if (result.isNewEntry)
739         result.iterator->value = adoptRef(new WatchpointSet(IsWatched));
740     return result.iterator->value.get();
741 }
742
743 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
744 {
745     ensureWatchpointSetForImpureProperty(propertyName)->add(watchpoint);
746 }
747
748 void VM::addImpureProperty(const String& propertyName)
749 {
750     if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
751         watchpointSet->fireAll("Impure property added");
752 }
753
754 static bool enableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doEnableWork)
755 {
756     bool needsToRecompile = false;
757     if (!counter) {
758         doEnableWork();
759         needsToRecompile = true;
760     }
761     counter++;
762
763     return needsToRecompile;
764 }
765
766 static bool disableProfilerWithRespectToCount(unsigned& counter, std::function<void()> doDisableWork)
767 {
768     RELEASE_ASSERT(counter > 0);
769     bool needsToRecompile = false;
770     counter--;
771     if (!counter) {
772         doDisableWork();
773         needsToRecompile = true;
774     }
775
776     return needsToRecompile;
777 }
778
779 bool VM::enableTypeProfiler()
780 {
781     auto enableTypeProfiler = [this] () {
782         this->m_typeProfiler = std::make_unique<TypeProfiler>();
783         this->m_typeProfilerLog = std::make_unique<TypeProfilerLog>();
784     };
785
786     return enableProfilerWithRespectToCount(m_typeProfilerEnabledCount, enableTypeProfiler);
787 }
788
789 bool VM::disableTypeProfiler()
790 {
791     auto disableTypeProfiler = [this] () {
792         this->m_typeProfiler.reset(nullptr);
793         this->m_typeProfilerLog.reset(nullptr);
794     };
795
796     return disableProfilerWithRespectToCount(m_typeProfilerEnabledCount, disableTypeProfiler);
797 }
798
799 bool VM::enableControlFlowProfiler()
800 {
801     auto enableControlFlowProfiler = [this] () {
802         this->m_controlFlowProfiler = std::make_unique<ControlFlowProfiler>();
803     };
804
805     return enableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, enableControlFlowProfiler);
806 }
807
808 bool VM::disableControlFlowProfiler()
809 {
810     auto disableControlFlowProfiler = [this] () {
811         this->m_controlFlowProfiler.reset(nullptr);
812     };
813
814     return disableProfilerWithRespectToCount(m_controlFlowProfilerEnabledCount, disableControlFlowProfiler);
815 }
816
817 void VM::dumpTypeProfilerData()
818 {
819     if (!typeProfiler())
820         return;
821
822     typeProfilerLog()->processLogEntries(ASCIILiteral("VM Dump Types"));
823     typeProfiler()->dumpTypeProfilerData(*this);
824 }
825
826 void VM::queueMicrotask(JSGlobalObject* globalObject, PassRefPtr<Microtask> task)
827 {
828     m_microtaskQueue.append(std::make_unique<QueuedTask>(*this, globalObject, task));
829 }
830
831 void VM::drainMicrotasks()
832 {
833     while (!m_microtaskQueue.isEmpty())
834         m_microtaskQueue.takeFirst()->run();
835 }
836
837 void QueuedTask::run()
838 {
839     m_microtask->run(m_globalObject->globalExec());
840 }
841
842 void sanitizeStackForVM(VM* vm)
843 {
844     logSanitizeStack(vm);
845 #if !ENABLE(JIT)
846     vm->interpreter->stack().sanitizeStack();
847 #else
848     sanitizeStackForVMImpl(vm);
849 #endif
850 }
851
852 } // namespace JSC