2 * Copyright (C) 2008, 2011, 2013, 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
33 #include "ArityCheckFailReturnThunks.h"
34 #include "ArrayBufferNeuteringWatchpoint.h"
35 #include "BuiltinExecutables.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 "DebuggerScope.h"
44 #include "ErrorInstance.h"
45 #include "FTLThunks.h"
46 #include "FunctionConstructor.h"
47 #include "GCActivityCallback.h"
48 #include "GetterSetter.h"
50 #include "HeapIterationScope.h"
51 #include "HighFidelityTypeProfiler.h"
52 #include "HighFidelityLog.h"
53 #include "HostCallReturnValue.h"
54 #include "Identifier.h"
55 #include "IncrementalSweeper.h"
56 #include "Interpreter.h"
58 #include "JSAPIValueWrapper.h"
59 #include "JSActivation.h"
61 #include "JSCInlines.h"
62 #include "JSFunction.h"
63 #include "JSGlobalObjectFunctions.h"
65 #include "JSNameScope.h"
66 #include "JSNotAnObject.h"
67 #include "JSPromiseDeferred.h"
68 #include "JSPromiseReaction.h"
69 #include "JSPropertyNameEnumerator.h"
70 #include "JSWithScope.h"
76 #include "ParserArena.h"
77 #include "ProfilerDatabase.h"
78 #include "PropertyMapHashTable.h"
79 #include "RegExpCache.h"
80 #include "RegExpObject.h"
81 #include "SimpleTypedArrayController.h"
82 #include "SourceProviderCache.h"
83 #include "StrictEvalActivation.h"
84 #include "StrongInlines.h"
85 #include "StructureInlines.h"
86 #include "UnlinkedCodeBlock.h"
87 #include "WeakMapData.h"
88 #include <wtf/ProcessID.h>
89 #include <wtf/RetainPtr.h>
90 #include <wtf/StringPrintStream.h>
91 #include <wtf/Threading.h>
92 #include <wtf/WTFThreadData.h>
93 #include <wtf/text/AtomicStringTable.h>
94 #include <wtf/CurrentTime.h>
97 #include "ConservativeRoots.h"
100 #if ENABLE(REGEXP_TRACING)
105 #include <CoreFoundation/CoreFoundation.h>
112 // Note: Platform.h will enforce that ENABLE(ASSEMBLER) is true if either
113 // ENABLE(JIT) or ENABLE(YARR_JIT) or both are enabled. The code below
114 // just checks for ENABLE(JIT) or ENABLE(YARR_JIT) with this premise in mind.
116 #if ENABLE(ASSEMBLER)
117 static bool enableAssembler(ExecutableAllocator& executableAllocator)
119 if (!Options::useJIT() && !Options::useRegExpJIT())
122 if (!executableAllocator.isValid()) {
123 if (Options::crashIfCantAllocateJITMemory())
129 CFStringRef canUseJITKey = CFSTR("JavaScriptCoreUseJIT");
130 RetainPtr<CFTypeRef> canUseJIT = adoptCF(CFPreferencesCopyAppValue(canUseJITKey, kCFPreferencesCurrentApplication));
132 return kCFBooleanTrue == canUseJIT.get();
135 #if USE(CF) || OS(UNIX)
136 char* canUseJITString = getenv("JavaScriptCoreUseJIT");
137 return !canUseJITString || atoi(canUseJITString);
142 #endif // ENABLE(!ASSEMBLER)
144 VM::VM(VMType vmType, HeapType heapType)
145 : m_apiLock(adoptRef(new JSLock(this)))
146 #if ENABLE(ASSEMBLER)
147 , executableAllocator(*this)
149 , heap(this, heapType)
152 , topCallFrame(CallFrame::noCaller())
153 , m_atomicStringTable(vmType == Default ? wtfThreadData().atomicStringTable() : new AtomicStringTable)
154 , propertyNames(nullptr)
155 , emptyList(new MarkedArgumentBuffer)
156 , parserArena(adoptPtr(new ParserArena))
157 , keywords(adoptPtr(new Keywords(*this)))
159 , jsArrayClassInfo(JSArray::info())
160 , jsFinalObjectClassInfo(JSFinalObject::info())
161 , sizeOfLastScratchBuffer(0)
163 , m_regExpCache(new RegExpCache(this))
164 #if ENABLE(REGEXP_TRACING)
165 , m_rtTraceList(new RTTraceList())
167 , m_newStringsSinceLastHashCons(0)
168 #if ENABLE(ASSEMBLER)
169 , m_canUseAssembler(enableAssembler(executableAllocator))
172 , m_canUseJIT(m_canUseAssembler && Options::useJIT())
175 , m_canUseRegExpJIT(m_canUseAssembler && Options::useRegExpJIT())
177 #if ENABLE(GC_VALIDATION)
178 , m_initializingObjectClass(0)
180 , m_stackPointerAtVMEntry(0)
187 , m_largestFTLStackSize(0)
189 , m_inDefineOwnProperty(false)
190 , m_codeCache(CodeCache::create())
191 , m_enabledProfiler(nullptr)
192 , m_builtinExecutables(BuiltinExecutables::create(*this))
193 , m_nextUniqueVariableID(1)
195 interpreter = new Interpreter(*this);
196 StackBounds stack = wtfThreadData().stack();
197 updateReservedZoneSize(Options::reservedZoneSize());
199 interpreter->stack().setReservedZoneSize(Options::reservedZoneSize());
201 setLastStackTop(stack.origin());
203 // Need to be careful to keep everything consistent here
204 JSLockHolder lock(this);
205 AtomicStringTable* existingEntryAtomicStringTable = wtfThreadData().setCurrentAtomicStringTable(m_atomicStringTable);
206 propertyNames = new CommonIdentifiers(this);
207 structureStructure.set(*this, Structure::createStructure(*this));
208 structureRareDataStructure.set(*this, StructureRareData::createStructure(*this, 0, jsNull()));
209 debuggerScopeStructure.set(*this, DebuggerScope::createStructure(*this, 0, jsNull()));
210 terminatedExecutionErrorStructure.set(*this, TerminatedExecutionError::createStructure(*this, 0, jsNull()));
211 stringStructure.set(*this, JSString::createStructure(*this, 0, jsNull()));
212 notAnObjectStructure.set(*this, JSNotAnObject::createStructure(*this, 0, jsNull()));
213 propertyNameEnumeratorStructure.set(*this, JSPropertyNameEnumerator::createStructure(*this, 0, jsNull()));
214 getterSetterStructure.set(*this, GetterSetter::createStructure(*this, 0, jsNull()));
215 customGetterSetterStructure.set(*this, CustomGetterSetter::createStructure(*this, 0, jsNull()));
216 apiWrapperStructure.set(*this, JSAPIValueWrapper::createStructure(*this, 0, jsNull()));
217 JSScopeStructure.set(*this, JSScope::createStructure(*this, 0, jsNull()));
218 executableStructure.set(*this, ExecutableBase::createStructure(*this, 0, jsNull()));
219 nativeExecutableStructure.set(*this, NativeExecutable::createStructure(*this, 0, jsNull()));
220 evalExecutableStructure.set(*this, EvalExecutable::createStructure(*this, 0, jsNull()));
221 programExecutableStructure.set(*this, ProgramExecutable::createStructure(*this, 0, jsNull()));
222 functionExecutableStructure.set(*this, FunctionExecutable::createStructure(*this, 0, jsNull()));
223 regExpStructure.set(*this, RegExp::createStructure(*this, 0, jsNull()));
224 symbolTableStructure.set(*this, SymbolTable::createStructure(*this, 0, jsNull()));
225 structureChainStructure.set(*this, StructureChain::createStructure(*this, 0, jsNull()));
226 sparseArrayValueMapStructure.set(*this, SparseArrayValueMap::createStructure(*this, 0, jsNull()));
227 arrayBufferNeuteringWatchpointStructure.set(*this, ArrayBufferNeuteringWatchpoint::createStructure(*this));
228 unlinkedFunctionExecutableStructure.set(*this, UnlinkedFunctionExecutable::createStructure(*this, 0, jsNull()));
229 unlinkedProgramCodeBlockStructure.set(*this, UnlinkedProgramCodeBlock::createStructure(*this, 0, jsNull()));
230 unlinkedEvalCodeBlockStructure.set(*this, UnlinkedEvalCodeBlock::createStructure(*this, 0, jsNull()));
231 unlinkedFunctionCodeBlockStructure.set(*this, UnlinkedFunctionCodeBlock::createStructure(*this, 0, jsNull()));
232 propertyTableStructure.set(*this, PropertyTable::createStructure(*this, 0, jsNull()));
233 mapDataStructure.set(*this, MapData::createStructure(*this, 0, jsNull()));
234 weakMapDataStructure.set(*this, WeakMapData::createStructure(*this, 0, jsNull()));
236 promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
237 promiseReactionStructure.set(*this, JSPromiseReaction::createStructure(*this, 0, jsNull()));
239 iterationTerminator.set(*this, JSFinalObject::create(*this, JSFinalObject::createStructure(*this, 0, jsNull(), 1)));
240 smallStrings.initializeCommonStrings(*this);
242 wtfThreadData().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
245 jitStubs = adoptPtr(new JITThunks());
246 arityCheckFailReturnThunks = std::make_unique<ArityCheckFailReturnThunks>();
248 arityCheckData = std::make_unique<CommonSlowPaths::ArityCheckData>();
251 ftlThunks = std::make_unique<FTL::Thunks>();
252 #endif // ENABLE(FTL_JIT)
254 interpreter->initialize(this->canUseJIT());
257 initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
260 heap.notifyIsSafeToCollect();
262 LLInt::Data::performAssertions(*this);
264 if (Options::enableProfiler()) {
265 m_perBytecodeProfiler = adoptPtr(new Profiler::Database(*this));
267 StringPrintStream pathOut;
269 const char* profilerPath = getenv("JSC_PROFILER_PATH");
271 pathOut.print(profilerPath, "/");
273 pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
274 m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
279 dfgState = adoptPtr(new DFG::LongLivedState());
282 // Initialize this last, as a free way of asserting that VM initialization itself
284 m_typedArrayController = adoptRef(new SimpleTypedArrayController());
286 // FIXME: conditionally allocate this stuff based on whether or not the inspector is running OR this flag is enabled (not just if the flag is enabled).
287 if (Options::profileTypesWithHighFidelity()) {
288 m_highFidelityTypeProfiler = std::make_unique<HighFidelityTypeProfiler>();
289 m_highFidelityLog = std::make_unique<HighFidelityLog>();
295 // Never GC, ever again.
296 heap.incrementDeferralDepth();
299 // Make sure concurrent compilations are done, but don't install them, since there is
300 // no point to doing so.
301 for (unsigned i = DFG::numberOfWorklists(); i--;) {
302 if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i)) {
303 worklist->waitUntilAllPlansForVMAreReady(*this);
304 worklist->removeAllReadyPlansForVM(*this);
307 #endif // ENABLE(DFG_JIT)
309 // Clear this first to ensure that nobody tries to remove themselves from it.
310 m_perBytecodeProfiler.clear();
312 ASSERT(m_apiLock->currentThreadIsHoldingLock());
313 m_apiLock->willDestroyVM(this);
314 heap.lastChanceToFinalize();
318 interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
323 delete propertyNames;
324 if (vmType != Default)
325 delete m_atomicStringTable;
328 delete m_regExpCache;
329 #if ENABLE(REGEXP_TRACING)
330 delete m_rtTraceList;
334 for (unsigned i = 0; i < scratchBuffers.size(); ++i)
335 fastFree(scratchBuffers[i]);
339 PassRefPtr<VM> VM::createContextGroup(HeapType heapType)
341 return adoptRef(new VM(APIContextGroup, heapType));
344 PassRefPtr<VM> VM::create(HeapType heapType)
346 return adoptRef(new VM(Default, heapType));
349 PassRefPtr<VM> VM::createLeaked(HeapType heapType)
351 return create(heapType);
354 bool VM::sharedInstanceExists()
356 return sharedInstanceInternal();
359 VM& VM::sharedInstance()
361 GlobalJSLock globalLock;
362 VM*& instance = sharedInstanceInternal();
364 instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
365 instance->makeUsableFromMultipleThreads();
370 VM*& VM::sharedInstanceInternal()
372 static VM* sharedInstance;
373 return sharedInstance;
377 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
380 case CharCodeAtIntrinsic:
381 return charCodeAtThunkGenerator;
382 case CharAtIntrinsic:
383 return charAtThunkGenerator;
384 case FromCharCodeIntrinsic:
385 return fromCharCodeThunkGenerator;
387 return sqrtThunkGenerator;
389 return powThunkGenerator;
391 return absThunkGenerator;
393 return floorThunkGenerator;
395 return ceilThunkGenerator;
397 return roundThunkGenerator;
399 return expThunkGenerator;
401 return logThunkGenerator;
403 return imulThunkGenerator;
404 case ArrayIteratorNextKeyIntrinsic:
405 return arrayIteratorNextKeyThunkGenerator;
406 case ArrayIteratorNextValueIntrinsic:
407 return arrayIteratorNextValueThunkGenerator;
413 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
415 return jitStubs->hostFunctionStub(this, function, constructor);
417 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic)
420 return jitStubs->hostFunctionStub(this, function, intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0, intrinsic);
423 #else // !ENABLE(JIT)
425 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
427 return NativeExecutable::create(*this,
428 adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_call_trampoline), JITCode::HostCallThunk)), function,
429 adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_construct_trampoline), JITCode::HostCallThunk)), constructor,
433 #endif // !ENABLE(JIT)
435 VM::ClientData::~ClientData()
439 void VM::resetDateCache()
441 localTimeOffsetCache.reset();
442 cachedDateString = String();
443 cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
444 dateInstanceCache.reset();
447 void VM::startSampling()
449 interpreter->startSampling();
452 void VM::stopSampling()
454 interpreter->stopSampling();
457 void VM::waitForCompilationsToComplete()
460 for (unsigned i = DFG::numberOfWorklists(); i--;) {
461 if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i))
462 worklist->completeAllPlansForVM(*this);
464 #endif // ENABLE(DFG_JIT)
467 void VM::discardAllCode()
469 waitForCompilationsToComplete();
470 m_codeCache->clear();
471 m_regExpCache->invalidateCode();
472 heap.deleteAllCompiledCode();
473 heap.deleteAllUnlinkedFunctionCode();
474 heap.reportAbandonedObjectGraph();
477 void VM::dumpSampleData(ExecState* exec)
479 interpreter->dumpSampleData(exec);
480 #if ENABLE(ASSEMBLER)
481 ExecutableAllocator::dumpProfile();
485 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
487 auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
488 if (addResult.isNewEntry)
489 addResult.iterator->value = adoptRef(new SourceProviderCache);
490 return addResult.iterator->value.get();
493 void VM::clearSourceProviderCaches()
495 sourceProviderCacheMap.clear();
498 struct StackPreservingRecompiler : public MarkedBlock::VoidFunctor {
499 HashSet<FunctionExecutable*> currentlyExecutingFunctions;
500 void operator()(JSCell* cell)
502 if (!cell->inherits(FunctionExecutable::info()))
504 FunctionExecutable* executable = jsCast<FunctionExecutable*>(cell);
505 if (currentlyExecutingFunctions.contains(executable))
507 executable->clearCodeIfNotCompiling();
511 void VM::releaseExecutableMemory()
513 waitForCompilationsToComplete();
516 StackPreservingRecompiler recompiler;
517 HeapIterationScope iterationScope(heap);
518 HashSet<JSCell*> roots;
519 heap.getConservativeRegisterRoots(roots);
520 HashSet<JSCell*>::iterator end = roots.end();
521 for (HashSet<JSCell*>::iterator ptr = roots.begin(); ptr != end; ++ptr) {
522 ScriptExecutable* executable = 0;
524 if (cell->inherits(ScriptExecutable::info()))
525 executable = static_cast<ScriptExecutable*>(*ptr);
526 else if (cell->inherits(JSFunction::info())) {
527 JSFunction* function = jsCast<JSFunction*>(*ptr);
528 if (function->isHostFunction())
530 executable = function->jsExecutable();
533 ASSERT(executable->inherits(ScriptExecutable::info()));
534 executable->unlinkCalls();
535 if (executable->inherits(FunctionExecutable::info()))
536 recompiler.currentlyExecutingFunctions.add(static_cast<FunctionExecutable*>(executable));
539 heap.objectSpace().forEachLiveCell<StackPreservingRecompiler>(iterationScope, recompiler);
541 m_regExpCache->invalidateCode();
542 heap.collectAllGarbage();
545 static void appendSourceToError(CallFrame* callFrame, ErrorInstance* exception, unsigned bytecodeOffset)
547 exception->clearAppendSourceToMessage();
549 if (!callFrame->codeBlock()->hasExpressionInfo())
558 CodeBlock* codeBlock = callFrame->codeBlock();
559 codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divotPoint, startOffset, endOffset, line, column);
561 int expressionStart = divotPoint - startOffset;
562 int expressionStop = divotPoint + endOffset;
564 const String& sourceString = codeBlock->source()->source();
565 if (!expressionStop || expressionStart > static_cast<int>(sourceString.length()))
568 VM* vm = &callFrame->vm();
569 JSValue jsMessage = exception->getDirect(*vm, vm->propertyNames->message);
570 if (!jsMessage || !jsMessage.isString())
573 String message = asString(jsMessage)->value(callFrame);
575 if (expressionStart < expressionStop)
576 message = makeString(message, " (evaluating '", codeBlock->source()->getRange(expressionStart, expressionStop), "')");
578 // No range information, so give a few characters of context.
579 const StringImpl* data = sourceString.impl();
580 int dataLength = sourceString.length();
581 int start = expressionStart;
582 int stop = expressionStart;
583 // Get up to 20 characters of context to the left and right of the divot, clamping to the line.
584 // Then strip whitespace.
585 while (start > 0 && (expressionStart - start < 20) && (*data)[start - 1] != '\n')
587 while (start < (expressionStart - 1) && isStrWhiteSpace((*data)[start]))
589 while (stop < dataLength && (stop - expressionStart < 20) && (*data)[stop] != '\n')
591 while (stop > expressionStart && isStrWhiteSpace((*data)[stop - 1]))
593 message = makeString(message, " (near '...", codeBlock->source()->getRange(start, stop), "...')");
596 exception->putDirect(*vm, vm->propertyNames->message, jsString(vm, message));
599 JSValue VM::throwException(ExecState* exec, JSValue error)
601 if (Options::breakOnThrow()) {
602 dataLog("In call frame ", RawPointer(exec), " for code block ", *exec->codeBlock(), "\n");
606 ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
608 Vector<StackFrame> stackTrace;
609 interpreter->getStackTrace(stackTrace);
610 m_exceptionStack = RefCountedArray<StackFrame>(stackTrace);
613 if (stackTrace.isEmpty() || !error.isObject())
615 JSObject* exception = asObject(error);
617 StackFrame stackFrame;
618 for (unsigned i = 0 ; i < stackTrace.size(); ++i) {
619 stackFrame = stackTrace.at(i);
620 if (stackFrame.bytecodeOffset)
623 unsigned bytecodeOffset = stackFrame.bytecodeOffset;
624 if (!hasErrorInfo(exec, exception)) {
625 // FIXME: We should only really be adding these properties to VM generated exceptions,
626 // but the inspector currently requires these for all thrown objects.
629 stackFrame.computeLineAndColumn(line, column);
630 exception->putDirect(*this, Identifier(this, "line"), jsNumber(line), ReadOnly | DontDelete);
631 exception->putDirect(*this, Identifier(this, "column"), jsNumber(column), ReadOnly | DontDelete);
632 if (!stackFrame.sourceURL.isEmpty())
633 exception->putDirect(*this, Identifier(this, "sourceURL"), jsString(this, stackFrame.sourceURL), ReadOnly | DontDelete);
635 if (exception->isErrorInstance() && static_cast<ErrorInstance*>(exception)->appendSourceToMessage()) {
636 unsigned stackIndex = 0;
637 CallFrame* callFrame;
638 for (callFrame = exec; callFrame && !callFrame->codeBlock(); ) {
640 callFrame = callFrame->callerFrameSkippingVMEntrySentinel();
642 if (callFrame && callFrame->codeBlock()) {
643 stackFrame = stackTrace.at(stackIndex);
644 bytecodeOffset = stackFrame.bytecodeOffset;
645 appendSourceToError(callFrame, static_cast<ErrorInstance*>(exception), bytecodeOffset);
649 if (exception->hasProperty(exec, this->propertyNames->stack))
652 exception->putDirect(*this, propertyNames->stack, interpreter->stackTraceAsString(topCallFrame, stackTrace), DontEnum);
656 JSObject* VM::throwException(ExecState* exec, JSObject* error)
658 return asObject(throwException(exec, JSValue(error)));
660 void VM::getExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
662 exception = m_exception;
663 exceptionStack = m_exceptionStack;
665 void VM::setExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
667 m_exception = exception;
668 m_exceptionStack = exceptionStack;
671 void VM::clearException()
673 m_exception = JSValue();
675 void VM:: clearExceptionStack()
677 m_exceptionStack = RefCountedArray<StackFrame>();
680 void VM::setStackPointerAtVMEntry(void* sp)
682 m_stackPointerAtVMEntry = sp;
686 size_t VM::updateReservedZoneSize(size_t reservedZoneSize)
688 size_t oldReservedZoneSize = m_reservedZoneSize;
689 m_reservedZoneSize = reservedZoneSize;
693 return oldReservedZoneSize;
697 // On Windows the reserved stack space consists of committed memory, a guard page, and uncommitted memory,
698 // where the guard page is a barrier between committed and uncommitted memory.
699 // When data from the guard page is read or written, the guard page is moved, and memory is committed.
700 // This is how the system grows the stack.
701 // When using the C stack on Windows we need to precommit the needed stack space.
702 // Otherwise we might crash later if we access uncommitted stack memory.
703 // This can happen if we allocate stack space larger than the page guard size (4K).
704 // The system does not get the chance to move the guard page, and commit more memory,
705 // and we crash if uncommitted memory is accessed.
706 // The MSVC compiler fixes this by inserting a call to the _chkstk() function,
707 // when needed, see http://support.microsoft.com/kb/100775.
708 // By touching every page up to the stack limit with a dummy operation,
709 // we force the system to move the guard page, and commit memory.
711 static void preCommitStackMemory(void* stackLimit)
713 const int pageSize = 4096;
714 for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
721 inline void VM::updateStackLimit()
724 void* lastStackLimit = m_stackLimit;
727 if (m_stackPointerAtVMEntry) {
728 ASSERT(wtfThreadData().stack().isGrowingDownward());
729 char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
731 m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize + m_largestFTLStackSize);
732 m_ftlStackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize + 2 * m_largestFTLStackSize);
734 m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize);
738 m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + m_largestFTLStackSize);
739 m_ftlStackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + 2 * m_largestFTLStackSize);
741 m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize);
746 if (lastStackLimit != m_stackLimit)
747 preCommitStackMemory(m_stackLimit);
752 void VM::updateFTLLargestStackSize(size_t stackSize)
754 if (stackSize > m_largestFTLStackSize) {
755 m_largestFTLStackSize = stackSize;
761 void releaseExecutableMemory(VM& vm)
763 vm.releaseExecutableMemory();
767 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
769 for (size_t i = 0; i < scratchBuffers.size(); i++) {
770 ScratchBuffer* scratchBuffer = scratchBuffers[i];
771 if (scratchBuffer->activeLength()) {
772 void* bufferStart = scratchBuffer->dataBuffer();
773 conservativeRoots.add(bufferStart, static_cast<void*>(static_cast<char*>(bufferStart) + scratchBuffer->activeLength()));
779 void logSanitizeStack(VM* vm)
781 if (Options::verboseSanitizeStack() && vm->topCallFrame) {
784 "Sanitizing stack with top call frame at ", RawPointer(vm->topCallFrame),
785 ", current stack pointer at ", RawPointer(&dummy), ", in ",
786 pointerDump(vm->topCallFrame->codeBlock()), " and last code origin = ",
787 vm->topCallFrame->codeOrigin(), "\n");
791 #if ENABLE(REGEXP_TRACING)
792 void VM::addRegExpToTrace(RegExp* regExp)
795 m_rtTraceList->add(regExp);
798 void VM::dumpRegExpTrace()
800 // The first RegExp object is ignored. It is create by the RegExpPrototype ctor and not used.
801 RTTraceList::iterator iter = ++m_rtTraceList->begin();
803 if (iter != m_rtTraceList->end()) {
804 dataLogF("\nRegExp Tracing\n");
805 dataLogF("Regular Expression 8 Bit 16 Bit match() Matches Average\n");
806 dataLogF(" <Match only / Match> JIT Addr JIT Address calls found String len\n");
807 dataLogF("----------------------------------------+----------------+----------------+----------+----------+-----------\n");
809 unsigned reCount = 0;
811 for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
812 (*iter)->printTraceData();
816 dataLogF("%d Regular Expressions\n", reCount);
819 m_rtTraceList->clear();
822 void VM::dumpRegExpTrace()
827 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
829 auto result = m_impurePropertyWatchpointSets.add(propertyName.string(), nullptr);
830 if (result.isNewEntry)
831 result.iterator->value = adoptRef(new WatchpointSet(IsWatched));
832 result.iterator->value->add(watchpoint);
835 void VM::addImpureProperty(const String& propertyName)
837 if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
838 watchpointSet->fireAll("Impure property added");
841 class SetEnabledProfilerFunctor {
843 bool operator()(CodeBlock* codeBlock)
845 if (JITCode::isOptimizingJIT(codeBlock->jitType()))
846 codeBlock->jettison(Profiler::JettisonDueToLegacyProfiler);
851 void VM::setEnabledProfiler(LegacyProfiler* profiler)
853 m_enabledProfiler = profiler;
854 if (m_enabledProfiler) {
855 waitForCompilationsToComplete();
856 SetEnabledProfilerFunctor functor;
857 heap.forEachCodeBlock(functor);
861 void VM::dumpHighFidelityProfilingTypes()
863 if (!isProfilingTypesWithHighFidelity())
866 highFidelityLog()->processHighFidelityLog("VM Dump Types");
867 HighFidelityTypeProfiler* profiler = m_highFidelityTypeProfiler.get();
868 for (Bag<TypeLocation>::iterator iter = m_locationInfo.begin(); !!iter; ++iter) {
869 TypeLocation* location = *iter;
870 profiler->logTypesForTypeLocation(location);
874 void sanitizeStackForVM(VM* vm)
876 logSanitizeStack(vm);
878 vm->interpreter->stack().sanitizeStack();
880 sanitizeStackForVMImpl(vm);