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