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