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