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