REGRESSION: Inspector crashes when debugger is paused and injected scripts access...
[WebKit-https.git] / Source / JavaScriptCore / runtime / VM.cpp
1 /*
2  * Copyright (C) 2008, 2011, 2013, 2014 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 "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"
49 #include "Heap.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"
57 #include "JITCode.h"
58 #include "JSAPIValueWrapper.h"
59 #include "JSActivation.h"
60 #include "JSArray.h"
61 #include "JSCInlines.h"
62 #include "JSFunction.h"
63 #include "JSGlobalObjectFunctions.h"
64 #include "JSLock.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"
71 #include "Lexer.h"
72 #include "Lookup.h"
73 #include "MapData.h"
74 #include "Nodes.h"
75 #include "Parser.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>
95
96 #if ENABLE(DFG_JIT)
97 #include "ConservativeRoots.h"
98 #endif
99
100 #if ENABLE(REGEXP_TRACING)
101 #include "RegExp.h"
102 #endif
103
104 #if USE(CF)
105 #include <CoreFoundation/CoreFoundation.h>
106 #endif
107
108 using namespace WTF;
109
110 namespace JSC {
111
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.
115
116 #if ENABLE(ASSEMBLER)
117 static bool enableAssembler(ExecutableAllocator& executableAllocator)
118 {
119     if (!Options::useJIT() && !Options::useRegExpJIT())
120         return false;
121
122     if (!executableAllocator.isValid()) {
123         if (Options::crashIfCantAllocateJITMemory())
124             CRASH();
125         return false;
126     }
127
128 #if USE(CF)
129     CFStringRef canUseJITKey = CFSTR("JavaScriptCoreUseJIT");
130     RetainPtr<CFTypeRef> canUseJIT = adoptCF(CFPreferencesCopyAppValue(canUseJITKey, kCFPreferencesCurrentApplication));
131     if (canUseJIT)
132         return kCFBooleanTrue == canUseJIT.get();
133 #endif
134
135 #if USE(CF) || OS(UNIX)
136     char* canUseJITString = getenv("JavaScriptCoreUseJIT");
137     return !canUseJITString || atoi(canUseJITString);
138 #else
139     return true;
140 #endif
141 }
142 #endif // ENABLE(!ASSEMBLER)
143
144 VM::VM(VMType vmType, HeapType heapType)
145     : m_apiLock(adoptRef(new JSLock(this)))
146 #if ENABLE(ASSEMBLER)
147     , executableAllocator(*this)
148 #endif
149     , heap(this, heapType)
150     , vmType(vmType)
151     , clientData(0)
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)))
158     , interpreter(0)
159     , jsArrayClassInfo(JSArray::info())
160     , jsFinalObjectClassInfo(JSFinalObject::info())
161     , sizeOfLastScratchBuffer(0)
162     , entryScope(0)
163     , m_regExpCache(new RegExpCache(this))
164 #if ENABLE(REGEXP_TRACING)
165     , m_rtTraceList(new RTTraceList())
166 #endif
167     , m_newStringsSinceLastHashCons(0)
168 #if ENABLE(ASSEMBLER)
169     , m_canUseAssembler(enableAssembler(executableAllocator))
170 #endif
171 #if ENABLE(JIT)
172     , m_canUseJIT(m_canUseAssembler && Options::useJIT())
173 #endif
174 #if ENABLE(YARR_JIT)
175     , m_canUseRegExpJIT(m_canUseAssembler && Options::useRegExpJIT())
176 #endif
177 #if ENABLE(GC_VALIDATION)
178     , m_initializingObjectClass(0)
179 #endif
180     , m_stackPointerAtVMEntry(0)
181     , m_stackLimit(0)
182 #if !ENABLE(JIT)
183     , m_jsStackLimit(0)
184 #endif
185 #if ENABLE(FTL_JIT)
186     , m_ftlStackLimit(0)
187     , m_largestFTLStackSize(0)
188 #endif
189     , m_inDefineOwnProperty(false)
190     , m_codeCache(CodeCache::create())
191     , m_enabledProfiler(nullptr)
192     , m_builtinExecutables(BuiltinExecutables::create(*this))
193     , m_nextUniqueVariableID(1)
194 {
195     interpreter = new Interpreter(*this);
196     StackBounds stack = wtfThreadData().stack();
197     updateReservedZoneSize(Options::reservedZoneSize());
198 #if !ENABLE(JIT)
199     interpreter->stack().setReservedZoneSize(Options::reservedZoneSize());
200 #endif
201     setLastStackTop(stack.origin());
202
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()));
235 #if ENABLE(PROMISES)
236     promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
237     promiseReactionStructure.set(*this, JSPromiseReaction::createStructure(*this, 0, jsNull()));
238 #endif
239     iterationTerminator.set(*this, JSFinalObject::create(*this, JSFinalObject::createStructure(*this, 0, jsNull(), 1)));
240     smallStrings.initializeCommonStrings(*this);
241
242     wtfThreadData().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
243
244 #if ENABLE(JIT)
245     jitStubs = adoptPtr(new JITThunks());
246     arityCheckFailReturnThunks = std::make_unique<ArityCheckFailReturnThunks>();
247 #endif
248     arityCheckData = std::make_unique<CommonSlowPaths::ArityCheckData>();
249
250 #if ENABLE(FTL_JIT)
251     ftlThunks = std::make_unique<FTL::Thunks>();
252 #endif // ENABLE(FTL_JIT)
253     
254     interpreter->initialize(this->canUseJIT());
255     
256 #if ENABLE(JIT)
257     initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
258 #endif
259
260     heap.notifyIsSafeToCollect();
261     
262     LLInt::Data::performAssertions(*this);
263     
264     if (Options::enableProfiler()) {
265         m_perBytecodeProfiler = adoptPtr(new Profiler::Database(*this));
266
267         StringPrintStream pathOut;
268 #if !OS(WINCE)
269         const char* profilerPath = getenv("JSC_PROFILER_PATH");
270         if (profilerPath)
271             pathOut.print(profilerPath, "/");
272 #endif
273         pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
274         m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
275     }
276
277 #if ENABLE(DFG_JIT)
278     if (canUseJIT())
279         dfgState = adoptPtr(new DFG::LongLivedState());
280 #endif
281     
282     // Initialize this last, as a free way of asserting that VM initialization itself
283     // won't use this.
284     m_typedArrayController = adoptRef(new SimpleTypedArrayController());
285
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>();
290     }
291 }
292
293 VM::~VM()
294 {
295     // Never GC, ever again.
296     heap.incrementDeferralDepth();
297     
298 #if ENABLE(DFG_JIT)
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);
305         }
306     }
307 #endif // ENABLE(DFG_JIT)
308     
309     // Clear this first to ensure that nobody tries to remove themselves from it.
310     m_perBytecodeProfiler.clear();
311     
312     ASSERT(m_apiLock->currentThreadIsHoldingLock());
313     m_apiLock->willDestroyVM(this);
314     heap.lastChanceToFinalize();
315
316     delete interpreter;
317 #ifndef NDEBUG
318     interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
319 #endif
320
321     delete emptyList;
322
323     delete propertyNames;
324     if (vmType != Default)
325         delete m_atomicStringTable;
326
327     delete clientData;
328     delete m_regExpCache;
329 #if ENABLE(REGEXP_TRACING)
330     delete m_rtTraceList;
331 #endif
332
333 #if ENABLE(DFG_JIT)
334     for (unsigned i = 0; i < scratchBuffers.size(); ++i)
335         fastFree(scratchBuffers[i]);
336 #endif
337 }
338
339 PassRefPtr<VM> VM::createContextGroup(HeapType heapType)
340 {
341     return adoptRef(new VM(APIContextGroup, heapType));
342 }
343
344 PassRefPtr<VM> VM::create(HeapType heapType)
345 {
346     return adoptRef(new VM(Default, heapType));
347 }
348
349 PassRefPtr<VM> VM::createLeaked(HeapType heapType)
350 {
351     return create(heapType);
352 }
353
354 bool VM::sharedInstanceExists()
355 {
356     return sharedInstanceInternal();
357 }
358
359 VM& VM::sharedInstance()
360 {
361     GlobalJSLock globalLock;
362     VM*& instance = sharedInstanceInternal();
363     if (!instance) {
364         instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
365         instance->makeUsableFromMultipleThreads();
366     }
367     return *instance;
368 }
369
370 VM*& VM::sharedInstanceInternal()
371 {
372     static VM* sharedInstance;
373     return sharedInstance;
374 }
375
376 #if ENABLE(JIT)
377 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
378 {
379     switch (intrinsic) {
380     case CharCodeAtIntrinsic:
381         return charCodeAtThunkGenerator;
382     case CharAtIntrinsic:
383         return charAtThunkGenerator;
384     case FromCharCodeIntrinsic:
385         return fromCharCodeThunkGenerator;
386     case SqrtIntrinsic:
387         return sqrtThunkGenerator;
388     case PowIntrinsic:
389         return powThunkGenerator;
390     case AbsIntrinsic:
391         return absThunkGenerator;
392     case FloorIntrinsic:
393         return floorThunkGenerator;
394     case CeilIntrinsic:
395         return ceilThunkGenerator;
396     case RoundIntrinsic:
397         return roundThunkGenerator;
398     case ExpIntrinsic:
399         return expThunkGenerator;
400     case LogIntrinsic:
401         return logThunkGenerator;
402     case IMulIntrinsic:
403         return imulThunkGenerator;
404     case ArrayIteratorNextKeyIntrinsic:
405         return arrayIteratorNextKeyThunkGenerator;
406     case ArrayIteratorNextValueIntrinsic:
407         return arrayIteratorNextValueThunkGenerator;
408     default:
409         return 0;
410     }
411 }
412
413 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
414 {
415     return jitStubs->hostFunctionStub(this, function, constructor);
416 }
417 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic)
418 {
419     ASSERT(canUseJIT());
420     return jitStubs->hostFunctionStub(this, function, intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0, intrinsic);
421 }
422
423 #else // !ENABLE(JIT)
424
425 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
426 {
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,
430         NoIntrinsic);
431 }
432
433 #endif // !ENABLE(JIT)
434
435 VM::ClientData::~ClientData()
436 {
437 }
438
439 void VM::resetDateCache()
440 {
441     localTimeOffsetCache.reset();
442     cachedDateString = String();
443     cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
444     dateInstanceCache.reset();
445 }
446
447 void VM::startSampling()
448 {
449     interpreter->startSampling();
450 }
451
452 void VM::stopSampling()
453 {
454     interpreter->stopSampling();
455 }
456
457 void VM::waitForCompilationsToComplete()
458 {
459 #if ENABLE(DFG_JIT)
460     for (unsigned i = DFG::numberOfWorklists(); i--;) {
461         if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i))
462             worklist->completeAllPlansForVM(*this);
463     }
464 #endif // ENABLE(DFG_JIT)
465 }
466
467 void VM::discardAllCode()
468 {
469     waitForCompilationsToComplete();
470     m_codeCache->clear();
471     m_regExpCache->invalidateCode();
472     heap.deleteAllCompiledCode();
473     heap.deleteAllUnlinkedFunctionCode();
474     heap.reportAbandonedObjectGraph();
475 }
476
477 void VM::dumpSampleData(ExecState* exec)
478 {
479     interpreter->dumpSampleData(exec);
480 #if ENABLE(ASSEMBLER)
481     ExecutableAllocator::dumpProfile();
482 #endif
483 }
484
485 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
486 {
487     auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
488     if (addResult.isNewEntry)
489         addResult.iterator->value = adoptRef(new SourceProviderCache);
490     return addResult.iterator->value.get();
491 }
492
493 void VM::clearSourceProviderCaches()
494 {
495     sourceProviderCacheMap.clear();
496 }
497
498 struct StackPreservingRecompiler : public MarkedBlock::VoidFunctor {
499     HashSet<FunctionExecutable*> currentlyExecutingFunctions;
500     void operator()(JSCell* cell)
501     {
502         if (!cell->inherits(FunctionExecutable::info()))
503             return;
504         FunctionExecutable* executable = jsCast<FunctionExecutable*>(cell);
505         if (currentlyExecutingFunctions.contains(executable))
506             return;
507         executable->clearCodeIfNotCompiling();
508     }
509 };
510
511 void VM::releaseExecutableMemory()
512 {
513     waitForCompilationsToComplete();
514     
515     if (entryScope) {
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;
523             JSCell* cell = *ptr;
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())
529                     continue;
530                 executable = function->jsExecutable();
531             } else
532                 continue;
533             ASSERT(executable->inherits(ScriptExecutable::info()));
534             executable->unlinkCalls();
535             if (executable->inherits(FunctionExecutable::info()))
536                 recompiler.currentlyExecutingFunctions.add(static_cast<FunctionExecutable*>(executable));
537                 
538         }
539         heap.objectSpace().forEachLiveCell<StackPreservingRecompiler>(iterationScope, recompiler);
540     }
541     m_regExpCache->invalidateCode();
542     heap.collectAllGarbage();
543 }
544
545 static void appendSourceToError(CallFrame* callFrame, ErrorInstance* exception, unsigned bytecodeOffset)
546 {
547     exception->clearAppendSourceToMessage();
548     
549     if (!callFrame->codeBlock()->hasExpressionInfo())
550         return;
551     
552     int startOffset = 0;
553     int endOffset = 0;
554     int divotPoint = 0;
555     unsigned line = 0;
556     unsigned column = 0;
557     
558     CodeBlock* codeBlock = callFrame->codeBlock();
559     codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divotPoint, startOffset, endOffset, line, column);
560     
561     int expressionStart = divotPoint - startOffset;
562     int expressionStop = divotPoint + endOffset;
563     
564     const String& sourceString = codeBlock->source()->source();
565     if (!expressionStop || expressionStart > static_cast<int>(sourceString.length()))
566         return;
567     
568     VM* vm = &callFrame->vm();
569     JSValue jsMessage = exception->getDirect(*vm, vm->propertyNames->message);
570     if (!jsMessage || !jsMessage.isString())
571         return;
572     
573     String message = asString(jsMessage)->value(callFrame);
574     
575     if (expressionStart < expressionStop)
576         message =  makeString(message, " (evaluating '", codeBlock->source()->getRange(expressionStart, expressionStop), "')");
577     else {
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')
586             start--;
587         while (start < (expressionStart - 1) && isStrWhiteSpace((*data)[start]))
588             start++;
589         while (stop < dataLength && (stop - expressionStart < 20) && (*data)[stop] != '\n')
590             stop++;
591         while (stop > expressionStart && isStrWhiteSpace((*data)[stop - 1]))
592             stop--;
593         message = makeString(message, " (near '...", codeBlock->source()->getRange(start, stop), "...')");
594     }
595     
596     exception->putDirect(*vm, vm->propertyNames->message, jsString(vm, message));
597 }
598     
599 JSValue VM::throwException(ExecState* exec, JSValue error)
600 {
601     if (Options::breakOnThrow()) {
602         dataLog("In call frame ", RawPointer(exec), " for code block ", *exec->codeBlock(), "\n");
603         CRASH();
604     }
605     
606     ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
607     
608     Vector<StackFrame> stackTrace;
609     interpreter->getStackTrace(stackTrace);
610     m_exceptionStack = RefCountedArray<StackFrame>(stackTrace);
611     m_exception = error;
612     
613     if (stackTrace.isEmpty() || !error.isObject())
614         return error;
615     JSObject* exception = asObject(error);
616     
617     StackFrame stackFrame;
618     for (unsigned i = 0 ; i < stackTrace.size(); ++i) {
619         stackFrame = stackTrace.at(i);
620         if (stackFrame.bytecodeOffset)
621             break;
622     }
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.
627         unsigned line;
628         unsigned column;
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);
634     }
635     if (exception->isErrorInstance() && static_cast<ErrorInstance*>(exception)->appendSourceToMessage()) {
636         unsigned stackIndex = 0;
637         CallFrame* callFrame;
638         for (callFrame = exec; callFrame && !callFrame->codeBlock(); ) {
639             stackIndex++;
640             callFrame = callFrame->callerFrameSkippingVMEntrySentinel();
641         }
642         if (callFrame && callFrame->codeBlock()) {
643             stackFrame = stackTrace.at(stackIndex);
644             bytecodeOffset = stackFrame.bytecodeOffset;
645             appendSourceToError(callFrame, static_cast<ErrorInstance*>(exception), bytecodeOffset);
646         }
647     }
648
649     if (exception->hasProperty(exec, this->propertyNames->stack))
650         return error;
651     
652     exception->putDirect(*this, propertyNames->stack, interpreter->stackTraceAsString(topCallFrame, stackTrace), DontEnum);
653     return error;
654 }
655     
656 JSObject* VM::throwException(ExecState* exec, JSObject* error)
657 {
658     return asObject(throwException(exec, JSValue(error)));
659 }
660 void VM::getExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
661 {
662     exception = m_exception;
663     exceptionStack = m_exceptionStack;
664 }
665 void VM::setExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
666 {
667     m_exception = exception;
668     m_exceptionStack = exceptionStack;
669 }
670
671 void VM::clearException()
672 {
673     m_exception = JSValue();
674 }
675 void VM:: clearExceptionStack()
676 {
677     m_exceptionStack = RefCountedArray<StackFrame>();
678 }
679
680 void VM::setStackPointerAtVMEntry(void* sp)
681 {
682     m_stackPointerAtVMEntry = sp;
683     updateStackLimit();
684 }
685
686 size_t VM::updateReservedZoneSize(size_t reservedZoneSize)
687 {
688     size_t oldReservedZoneSize = m_reservedZoneSize;
689     m_reservedZoneSize = reservedZoneSize;
690
691     updateStackLimit();
692
693     return oldReservedZoneSize;
694 }
695
696 #if PLATFORM(WIN)
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.
710
711 static void preCommitStackMemory(void* stackLimit)
712 {
713     const int pageSize = 4096;
714     for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
715         char ch = *p;
716         *p = ch;
717     }
718 }
719 #endif
720
721 inline void VM::updateStackLimit()
722 {
723 #if PLATFORM(WIN)
724     void* lastStackLimit = m_stackLimit;
725 #endif
726
727     if (m_stackPointerAtVMEntry) {
728         ASSERT(wtfThreadData().stack().isGrowingDownward());
729         char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
730 #if ENABLE(FTL_JIT)
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);
733 #else
734         m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize);
735 #endif
736     } else {
737 #if ENABLE(FTL_JIT)
738         m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + m_largestFTLStackSize);
739         m_ftlStackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + 2 * m_largestFTLStackSize);
740 #else
741         m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize);
742 #endif
743     }
744
745 #if PLATFORM(WIN)
746     if (lastStackLimit != m_stackLimit)
747         preCommitStackMemory(m_stackLimit);
748 #endif
749 }
750
751 #if ENABLE(FTL_JIT)
752 void VM::updateFTLLargestStackSize(size_t stackSize)
753 {
754     if (stackSize > m_largestFTLStackSize) {
755         m_largestFTLStackSize = stackSize;
756         updateStackLimit();
757     }
758 }
759 #endif
760
761 void releaseExecutableMemory(VM& vm)
762 {
763     vm.releaseExecutableMemory();
764 }
765
766 #if ENABLE(DFG_JIT)
767 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
768 {
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()));
774         }
775     }
776 }
777 #endif
778
779 void logSanitizeStack(VM* vm)
780 {
781     if (Options::verboseSanitizeStack() && vm->topCallFrame) {
782         int dummy;
783         dataLog(
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");
788     }
789 }
790
791 #if ENABLE(REGEXP_TRACING)
792 void VM::addRegExpToTrace(RegExp* regExp)
793 {
794     gcProtect(regExp);
795     m_rtTraceList->add(regExp);
796 }
797
798 void VM::dumpRegExpTrace()
799 {
800     // The first RegExp object is ignored.  It is create by the RegExpPrototype ctor and not used.
801     RTTraceList::iterator iter = ++m_rtTraceList->begin();
802     
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");
808     
809         unsigned reCount = 0;
810     
811         for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
812             (*iter)->printTraceData();
813             gcUnprotect(*iter);
814         }
815
816         dataLogF("%d Regular Expressions\n", reCount);
817     }
818     
819     m_rtTraceList->clear();
820 }
821 #else
822 void VM::dumpRegExpTrace()
823 {
824 }
825 #endif
826
827 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
828 {
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);
833 }
834
835 void VM::addImpureProperty(const String& propertyName)
836 {
837     if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
838         watchpointSet->fireAll("Impure property added");
839 }
840
841 class SetEnabledProfilerFunctor {
842 public:
843     bool operator()(CodeBlock* codeBlock)
844     {
845         if (JITCode::isOptimizingJIT(codeBlock->jitType()))
846             codeBlock->jettison(Profiler::JettisonDueToLegacyProfiler);
847         return false;
848     }
849 };
850
851 void VM::setEnabledProfiler(LegacyProfiler* profiler)
852 {
853     m_enabledProfiler = profiler;
854     if (m_enabledProfiler) {
855         waitForCompilationsToComplete();
856         SetEnabledProfilerFunctor functor;
857         heap.forEachCodeBlock(functor);
858     }
859 }
860
861 void VM::dumpHighFidelityProfilingTypes()
862 {
863     if (!isProfilingTypesWithHighFidelity())
864         return;
865
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);
871     }
872 }
873
874 void sanitizeStackForVM(VM* vm)
875 {
876     logSanitizeStack(vm);
877 #if !ENABLE(JIT)
878     vm->interpreter->stack().sanitizeStack();
879 #else
880     sanitizeStackForVMImpl(vm);
881 #endif
882 }
883
884 } // namespace JSC