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