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