]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/VM.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / 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 "DebuggerActivation.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 "HostCallReturnValue.h"
52 #include "Identifier.h"
53 #include "IncrementalSweeper.h"
54 #include "Interpreter.h"
55 #include "JITCode.h"
56 #include "JSAPIValueWrapper.h"
57 #include "JSActivation.h"
58 #include "JSArray.h"
59 #include "JSCInlines.h"
60 #include "JSFunction.h"
61 #include "JSGlobalObjectFunctions.h"
62 #include "JSLock.h"
63 #include "JSNameScope.h"
64 #include "JSNotAnObject.h"
65 #include "JSPromiseDeferred.h"
66 #include "JSPromiseReaction.h"
67 #include "JSPropertyNameIterator.h"
68 #include "JSWithScope.h"
69 #include "Lexer.h"
70 #include "Lookup.h"
71 #include "MapData.h"
72 #include "Nodes.h"
73 #include "Parser.h"
74 #include "ParserArena.h"
75 #include "ProfilerDatabase.h"
76 #include "PropertyMapHashTable.h"
77 #include "RegExpCache.h"
78 #include "RegExpObject.h"
79 #include "SimpleTypedArrayController.h"
80 #include "SourceProviderCache.h"
81 #include "StrictEvalActivation.h"
82 #include "StrongInlines.h"
83 #include "StructureInlines.h"
84 #include "UnlinkedCodeBlock.h"
85 #include "WeakMapData.h"
86 #include <wtf/ProcessID.h>
87 #include <wtf/RetainPtr.h>
88 #include <wtf/StringPrintStream.h>
89 #include <wtf/Threading.h>
90 #include <wtf/WTFThreadData.h>
91 #include <wtf/text/AtomicStringTable.h>
92
93 #if ENABLE(DFG_JIT)
94 #include "ConservativeRoots.h"
95 #endif
96
97 #if ENABLE(REGEXP_TRACING)
98 #include "RegExp.h"
99 #endif
100
101 #if USE(CF)
102 #include <CoreFoundation/CoreFoundation.h>
103 #endif
104
105 using namespace WTF;
106
107 namespace JSC {
108
109 extern const HashTable arrayConstructorTable;
110 extern const HashTable arrayPrototypeTable;
111 extern const HashTable booleanPrototypeTable;
112 extern const HashTable jsonTable;
113 extern const HashTable dataViewTable;
114 extern const HashTable dateTable;
115 extern const HashTable dateConstructorTable;
116 extern const HashTable errorPrototypeTable;
117 extern const HashTable globalObjectTable;
118 extern const HashTable numberConstructorTable;
119 extern const HashTable numberPrototypeTable;
120 JS_EXPORTDATA extern const HashTable objectConstructorTable;
121 extern const HashTable privateNamePrototypeTable;
122 extern const HashTable regExpTable;
123 extern const HashTable regExpConstructorTable;
124 extern const HashTable regExpPrototypeTable;
125 extern const HashTable stringConstructorTable;
126 #if ENABLE(PROMISES)
127 extern const HashTable promisePrototypeTable;
128 extern const HashTable promiseConstructorTable;
129 #endif
130
131 // Note: Platform.h will enforce that ENABLE(ASSEMBLER) is true if either
132 // ENABLE(JIT) or ENABLE(YARR_JIT) or both are enabled. The code below
133 // just checks for ENABLE(JIT) or ENABLE(YARR_JIT) with this premise in mind.
134
135 #if ENABLE(ASSEMBLER)
136 static bool enableAssembler(ExecutableAllocator& executableAllocator)
137 {
138 if (!Options::useJIT() && !Options::useRegExpJIT())
139 return false;
140
141 if (!executableAllocator.isValid()) {
142 if (Options::crashIfCantAllocateJITMemory())
143 CRASH();
144 return false;
145 }
146
147 #if USE(CF)
148 CFStringRef canUseJITKey = CFSTR("JavaScriptCoreUseJIT");
149 RetainPtr<CFTypeRef> canUseJIT = adoptCF(CFPreferencesCopyAppValue(canUseJITKey, kCFPreferencesCurrentApplication));
150 if (canUseJIT)
151 return kCFBooleanTrue == canUseJIT.get();
152 #endif
153
154 #if USE(CF) || OS(UNIX)
155 char* canUseJITString = getenv("JavaScriptCoreUseJIT");
156 return !canUseJITString || atoi(canUseJITString);
157 #else
158 return true;
159 #endif
160 }
161 #endif // ENABLE(!ASSEMBLER)
162
163 VM::VM(VMType vmType, HeapType heapType)
164 : m_apiLock(adoptRef(new JSLock(this)))
165 #if ENABLE(ASSEMBLER)
166 , executableAllocator(*this)
167 #endif
168 , heap(this, heapType)
169 , vmType(vmType)
170 , clientData(0)
171 , topCallFrame(CallFrame::noCaller())
172 , arrayConstructorTable(adoptPtr(new HashTable(JSC::arrayConstructorTable)))
173 , arrayPrototypeTable(adoptPtr(new HashTable(JSC::arrayPrototypeTable)))
174 , booleanPrototypeTable(adoptPtr(new HashTable(JSC::booleanPrototypeTable)))
175 , dataViewTable(adoptPtr(new HashTable(JSC::dataViewTable)))
176 , dateTable(adoptPtr(new HashTable(JSC::dateTable)))
177 , dateConstructorTable(adoptPtr(new HashTable(JSC::dateConstructorTable)))
178 , errorPrototypeTable(adoptPtr(new HashTable(JSC::errorPrototypeTable)))
179 , globalObjectTable(adoptPtr(new HashTable(JSC::globalObjectTable)))
180 , jsonTable(adoptPtr(new HashTable(JSC::jsonTable)))
181 , numberConstructorTable(adoptPtr(new HashTable(JSC::numberConstructorTable)))
182 , numberPrototypeTable(adoptPtr(new HashTable(JSC::numberPrototypeTable)))
183 , objectConstructorTable(adoptPtr(new HashTable(JSC::objectConstructorTable)))
184 , privateNamePrototypeTable(adoptPtr(new HashTable(JSC::privateNamePrototypeTable)))
185 , regExpTable(adoptPtr(new HashTable(JSC::regExpTable)))
186 , regExpConstructorTable(adoptPtr(new HashTable(JSC::regExpConstructorTable)))
187 , regExpPrototypeTable(adoptPtr(new HashTable(JSC::regExpPrototypeTable)))
188 , stringConstructorTable(adoptPtr(new HashTable(JSC::stringConstructorTable)))
189 #if ENABLE(PROMISES)
190 , promisePrototypeTable(adoptPtr(new HashTable(JSC::promisePrototypeTable)))
191 , promiseConstructorTable(adoptPtr(new HashTable(JSC::promiseConstructorTable)))
192 #endif
193 , m_atomicStringTable(vmType == Default ? wtfThreadData().atomicStringTable() : new AtomicStringTable)
194 , propertyNames(nullptr)
195 , emptyList(new MarkedArgumentBuffer)
196 , parserArena(adoptPtr(new ParserArena))
197 , keywords(adoptPtr(new Keywords(*this)))
198 , interpreter(0)
199 , jsArrayClassInfo(JSArray::info())
200 , jsFinalObjectClassInfo(JSFinalObject::info())
201 , varargsLength(0)
202 , sizeOfLastScratchBuffer(0)
203 , entryScope(0)
204 , m_regExpCache(new RegExpCache(this))
205 #if ENABLE(REGEXP_TRACING)
206 , m_rtTraceList(new RTTraceList())
207 #endif
208 , m_newStringsSinceLastHashCons(0)
209 #if ENABLE(ASSEMBLER)
210 , m_canUseAssembler(enableAssembler(executableAllocator))
211 #endif
212 #if ENABLE(JIT)
213 , m_canUseJIT(m_canUseAssembler && Options::useJIT())
214 #endif
215 #if ENABLE(YARR_JIT)
216 , m_canUseRegExpJIT(m_canUseAssembler && Options::useRegExpJIT())
217 #endif
218 #if ENABLE(GC_VALIDATION)
219 , m_initializingObjectClass(0)
220 #endif
221 , m_stackPointerAtVMEntry(0)
222 , m_stackLimit(0)
223 #if !ENABLE(JIT)
224 , m_jsStackLimit(0)
225 #endif
226 #if ENABLE(FTL_JIT)
227 , m_ftlStackLimit(0)
228 , m_largestFTLStackSize(0)
229 #endif
230 , m_inDefineOwnProperty(false)
231 , m_codeCache(CodeCache::create())
232 , m_enabledProfiler(nullptr)
233 , m_builtinExecutables(BuiltinExecutables::create(*this))
234 {
235 interpreter = new Interpreter(*this);
236 StackBounds stack = wtfThreadData().stack();
237 updateReservedZoneSize(Options::reservedZoneSize());
238 #if !ENABLE(JIT)
239 interpreter->stack().setReservedZoneSize(Options::reservedZoneSize());
240 #endif
241 setLastStackTop(stack.origin());
242
243 // Need to be careful to keep everything consistent here
244 JSLockHolder lock(this);
245 AtomicStringTable* existingEntryAtomicStringTable = wtfThreadData().setCurrentAtomicStringTable(m_atomicStringTable);
246 propertyNames = new CommonIdentifiers(this);
247 structureStructure.set(*this, Structure::createStructure(*this));
248 structureRareDataStructure.set(*this, StructureRareData::createStructure(*this, 0, jsNull()));
249 debuggerActivationStructure.set(*this, DebuggerActivation::createStructure(*this, 0, jsNull()));
250 terminatedExecutionErrorStructure.set(*this, TerminatedExecutionError::createStructure(*this, 0, jsNull()));
251 stringStructure.set(*this, JSString::createStructure(*this, 0, jsNull()));
252 notAnObjectStructure.set(*this, JSNotAnObject::createStructure(*this, 0, jsNull()));
253 propertyNameIteratorStructure.set(*this, JSPropertyNameIterator::createStructure(*this, 0, jsNull()));
254 getterSetterStructure.set(*this, GetterSetter::createStructure(*this, 0, jsNull()));
255 customGetterSetterStructure.set(*this, CustomGetterSetter::createStructure(*this, 0, jsNull()));
256 apiWrapperStructure.set(*this, JSAPIValueWrapper::createStructure(*this, 0, jsNull()));
257 JSScopeStructure.set(*this, JSScope::createStructure(*this, 0, jsNull()));
258 executableStructure.set(*this, ExecutableBase::createStructure(*this, 0, jsNull()));
259 nativeExecutableStructure.set(*this, NativeExecutable::createStructure(*this, 0, jsNull()));
260 evalExecutableStructure.set(*this, EvalExecutable::createStructure(*this, 0, jsNull()));
261 programExecutableStructure.set(*this, ProgramExecutable::createStructure(*this, 0, jsNull()));
262 functionExecutableStructure.set(*this, FunctionExecutable::createStructure(*this, 0, jsNull()));
263 regExpStructure.set(*this, RegExp::createStructure(*this, 0, jsNull()));
264 symbolTableStructure.set(*this, SymbolTable::createStructure(*this, 0, jsNull()));
265 structureChainStructure.set(*this, StructureChain::createStructure(*this, 0, jsNull()));
266 sparseArrayValueMapStructure.set(*this, SparseArrayValueMap::createStructure(*this, 0, jsNull()));
267 arrayBufferNeuteringWatchpointStructure.set(*this, ArrayBufferNeuteringWatchpoint::createStructure(*this));
268 withScopeStructure.set(*this, JSWithScope::createStructure(*this, 0, jsNull()));
269 unlinkedFunctionExecutableStructure.set(*this, UnlinkedFunctionExecutable::createStructure(*this, 0, jsNull()));
270 unlinkedProgramCodeBlockStructure.set(*this, UnlinkedProgramCodeBlock::createStructure(*this, 0, jsNull()));
271 unlinkedEvalCodeBlockStructure.set(*this, UnlinkedEvalCodeBlock::createStructure(*this, 0, jsNull()));
272 unlinkedFunctionCodeBlockStructure.set(*this, UnlinkedFunctionCodeBlock::createStructure(*this, 0, jsNull()));
273 propertyTableStructure.set(*this, PropertyTable::createStructure(*this, 0, jsNull()));
274 mapDataStructure.set(*this, MapData::createStructure(*this, 0, jsNull()));
275 weakMapDataStructure.set(*this, WeakMapData::createStructure(*this, 0, jsNull()));
276 #if ENABLE(PROMISES)
277 promiseDeferredStructure.set(*this, JSPromiseDeferred::createStructure(*this, 0, jsNull()));
278 promiseReactionStructure.set(*this, JSPromiseReaction::createStructure(*this, 0, jsNull()));
279 #endif
280 iterationTerminator.set(*this, JSFinalObject::create(*this, JSFinalObject::createStructure(*this, 0, jsNull(), 1)));
281 smallStrings.initializeCommonStrings(*this);
282
283 wtfThreadData().setCurrentAtomicStringTable(existingEntryAtomicStringTable);
284
285 #if ENABLE(JIT)
286 jitStubs = adoptPtr(new JITThunks());
287 arityCheckFailReturnThunks = std::make_unique<ArityCheckFailReturnThunks>();
288 #endif
289 arityCheckData = std::make_unique<CommonSlowPaths::ArityCheckData>();
290
291 #if ENABLE(FTL_JIT)
292 ftlThunks = std::make_unique<FTL::Thunks>();
293 #endif // ENABLE(FTL_JIT)
294
295 interpreter->initialize(this->canUseJIT());
296
297 #if ENABLE(JIT)
298 initializeHostCallReturnValue(); // This is needed to convince the linker not to drop host call return support.
299 #endif
300
301 heap.notifyIsSafeToCollect();
302
303 LLInt::Data::performAssertions(*this);
304
305 if (Options::enableProfiler()) {
306 m_perBytecodeProfiler = adoptPtr(new Profiler::Database(*this));
307
308 StringPrintStream pathOut;
309 #if !OS(WINCE)
310 const char* profilerPath = getenv("JSC_PROFILER_PATH");
311 if (profilerPath)
312 pathOut.print(profilerPath, "/");
313 #endif
314 pathOut.print("JSCProfile-", getCurrentProcessID(), "-", m_perBytecodeProfiler->databaseID(), ".json");
315 m_perBytecodeProfiler->registerToSaveAtExit(pathOut.toCString().data());
316 }
317
318 #if ENABLE(DFG_JIT)
319 if (canUseJIT())
320 dfgState = adoptPtr(new DFG::LongLivedState());
321 #endif
322
323 // Initialize this last, as a free way of asserting that VM initialization itself
324 // won't use this.
325 m_typedArrayController = adoptRef(new SimpleTypedArrayController());
326 }
327
328 VM::~VM()
329 {
330 // Never GC, ever again.
331 heap.incrementDeferralDepth();
332
333 #if ENABLE(DFG_JIT)
334 // Make sure concurrent compilations are done, but don't install them, since there is
335 // no point to doing so.
336 for (unsigned i = DFG::numberOfWorklists(); i--;) {
337 if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i)) {
338 worklist->waitUntilAllPlansForVMAreReady(*this);
339 worklist->removeAllReadyPlansForVM(*this);
340 }
341 }
342 #endif // ENABLE(DFG_JIT)
343
344 // Clear this first to ensure that nobody tries to remove themselves from it.
345 m_perBytecodeProfiler.clear();
346
347 ASSERT(m_apiLock->currentThreadIsHoldingLock());
348 m_apiLock->willDestroyVM(this);
349 heap.lastChanceToFinalize();
350
351 delete interpreter;
352 #ifndef NDEBUG
353 interpreter = reinterpret_cast<Interpreter*>(0xbbadbeef);
354 #endif
355
356 arrayPrototypeTable->deleteTable();
357 arrayConstructorTable->deleteTable();
358 booleanPrototypeTable->deleteTable();
359 dataViewTable->deleteTable();
360 dateTable->deleteTable();
361 dateConstructorTable->deleteTable();
362 errorPrototypeTable->deleteTable();
363 globalObjectTable->deleteTable();
364 jsonTable->deleteTable();
365 numberConstructorTable->deleteTable();
366 numberPrototypeTable->deleteTable();
367 objectConstructorTable->deleteTable();
368 privateNamePrototypeTable->deleteTable();
369 regExpTable->deleteTable();
370 regExpConstructorTable->deleteTable();
371 regExpPrototypeTable->deleteTable();
372 stringConstructorTable->deleteTable();
373 #if ENABLE(PROMISES)
374 promisePrototypeTable->deleteTable();
375 promiseConstructorTable->deleteTable();
376 #endif
377
378 delete emptyList;
379
380 delete propertyNames;
381 if (vmType != Default)
382 delete m_atomicStringTable;
383
384 delete clientData;
385 delete m_regExpCache;
386 #if ENABLE(REGEXP_TRACING)
387 delete m_rtTraceList;
388 #endif
389
390 #if ENABLE(DFG_JIT)
391 for (unsigned i = 0; i < scratchBuffers.size(); ++i)
392 fastFree(scratchBuffers[i]);
393 #endif
394 }
395
396 PassRefPtr<VM> VM::createContextGroup(HeapType heapType)
397 {
398 return adoptRef(new VM(APIContextGroup, heapType));
399 }
400
401 PassRefPtr<VM> VM::create(HeapType heapType)
402 {
403 return adoptRef(new VM(Default, heapType));
404 }
405
406 PassRefPtr<VM> VM::createLeaked(HeapType heapType)
407 {
408 return create(heapType);
409 }
410
411 bool VM::sharedInstanceExists()
412 {
413 return sharedInstanceInternal();
414 }
415
416 VM& VM::sharedInstance()
417 {
418 GlobalJSLock globalLock;
419 VM*& instance = sharedInstanceInternal();
420 if (!instance) {
421 instance = adoptRef(new VM(APIShared, SmallHeap)).leakRef();
422 instance->makeUsableFromMultipleThreads();
423 }
424 return *instance;
425 }
426
427 VM*& VM::sharedInstanceInternal()
428 {
429 static VM* sharedInstance;
430 return sharedInstance;
431 }
432
433 #if ENABLE(JIT)
434 static ThunkGenerator thunkGeneratorForIntrinsic(Intrinsic intrinsic)
435 {
436 switch (intrinsic) {
437 case CharCodeAtIntrinsic:
438 return charCodeAtThunkGenerator;
439 case CharAtIntrinsic:
440 return charAtThunkGenerator;
441 case FromCharCodeIntrinsic:
442 return fromCharCodeThunkGenerator;
443 case SqrtIntrinsic:
444 return sqrtThunkGenerator;
445 case PowIntrinsic:
446 return powThunkGenerator;
447 case AbsIntrinsic:
448 return absThunkGenerator;
449 case FloorIntrinsic:
450 return floorThunkGenerator;
451 case CeilIntrinsic:
452 return ceilThunkGenerator;
453 case RoundIntrinsic:
454 return roundThunkGenerator;
455 case ExpIntrinsic:
456 return expThunkGenerator;
457 case LogIntrinsic:
458 return logThunkGenerator;
459 case IMulIntrinsic:
460 return imulThunkGenerator;
461 case ArrayIteratorNextKeyIntrinsic:
462 return arrayIteratorNextKeyThunkGenerator;
463 case ArrayIteratorNextValueIntrinsic:
464 return arrayIteratorNextValueThunkGenerator;
465 default:
466 return 0;
467 }
468 }
469
470 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
471 {
472 return jitStubs->hostFunctionStub(this, function, constructor);
473 }
474 NativeExecutable* VM::getHostFunction(NativeFunction function, Intrinsic intrinsic)
475 {
476 ASSERT(canUseJIT());
477 return jitStubs->hostFunctionStub(this, function, intrinsic != NoIntrinsic ? thunkGeneratorForIntrinsic(intrinsic) : 0, intrinsic);
478 }
479
480 #else // !ENABLE(JIT)
481
482 NativeExecutable* VM::getHostFunction(NativeFunction function, NativeFunction constructor)
483 {
484 return NativeExecutable::create(*this,
485 adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_call_trampoline), JITCode::HostCallThunk)), function,
486 adoptRef(new NativeJITCode(MacroAssemblerCodeRef::createLLIntCodeRef(llint_native_construct_trampoline), JITCode::HostCallThunk)), constructor,
487 NoIntrinsic);
488 }
489
490 #endif // !ENABLE(JIT)
491
492 VM::ClientData::~ClientData()
493 {
494 }
495
496 void VM::resetDateCache()
497 {
498 localTimeOffsetCache.reset();
499 cachedDateString = String();
500 cachedDateStringValue = std::numeric_limits<double>::quiet_NaN();
501 dateInstanceCache.reset();
502 }
503
504 void VM::startSampling()
505 {
506 interpreter->startSampling();
507 }
508
509 void VM::stopSampling()
510 {
511 interpreter->stopSampling();
512 }
513
514 void VM::waitForCompilationsToComplete()
515 {
516 #if ENABLE(DFG_JIT)
517 for (unsigned i = DFG::numberOfWorklists(); i--;) {
518 if (DFG::Worklist* worklist = DFG::worklistForIndexOrNull(i))
519 worklist->completeAllPlansForVM(*this);
520 }
521 #endif // ENABLE(DFG_JIT)
522 }
523
524 void VM::discardAllCode()
525 {
526 waitForCompilationsToComplete();
527 m_codeCache->clear();
528 m_regExpCache->invalidateCode();
529 heap.deleteAllCompiledCode();
530 heap.deleteAllUnlinkedFunctionCode();
531 heap.reportAbandonedObjectGraph();
532 }
533
534 void VM::dumpSampleData(ExecState* exec)
535 {
536 interpreter->dumpSampleData(exec);
537 #if ENABLE(ASSEMBLER)
538 ExecutableAllocator::dumpProfile();
539 #endif
540 }
541
542 SourceProviderCache* VM::addSourceProviderCache(SourceProvider* sourceProvider)
543 {
544 auto addResult = sourceProviderCacheMap.add(sourceProvider, nullptr);
545 if (addResult.isNewEntry)
546 addResult.iterator->value = adoptRef(new SourceProviderCache);
547 return addResult.iterator->value.get();
548 }
549
550 void VM::clearSourceProviderCaches()
551 {
552 sourceProviderCacheMap.clear();
553 }
554
555 struct StackPreservingRecompiler : public MarkedBlock::VoidFunctor {
556 HashSet<FunctionExecutable*> currentlyExecutingFunctions;
557 void operator()(JSCell* cell)
558 {
559 if (!cell->inherits(FunctionExecutable::info()))
560 return;
561 FunctionExecutable* executable = jsCast<FunctionExecutable*>(cell);
562 if (currentlyExecutingFunctions.contains(executable))
563 return;
564 executable->clearCodeIfNotCompiling();
565 }
566 };
567
568 void VM::releaseExecutableMemory()
569 {
570 waitForCompilationsToComplete();
571
572 if (entryScope) {
573 StackPreservingRecompiler recompiler;
574 HeapIterationScope iterationScope(heap);
575 HashSet<JSCell*> roots;
576 heap.getConservativeRegisterRoots(roots);
577 HashSet<JSCell*>::iterator end = roots.end();
578 for (HashSet<JSCell*>::iterator ptr = roots.begin(); ptr != end; ++ptr) {
579 ScriptExecutable* executable = 0;
580 JSCell* cell = *ptr;
581 if (cell->inherits(ScriptExecutable::info()))
582 executable = static_cast<ScriptExecutable*>(*ptr);
583 else if (cell->inherits(JSFunction::info())) {
584 JSFunction* function = jsCast<JSFunction*>(*ptr);
585 if (function->isHostFunction())
586 continue;
587 executable = function->jsExecutable();
588 } else
589 continue;
590 ASSERT(executable->inherits(ScriptExecutable::info()));
591 executable->unlinkCalls();
592 if (executable->inherits(FunctionExecutable::info()))
593 recompiler.currentlyExecutingFunctions.add(static_cast<FunctionExecutable*>(executable));
594
595 }
596 heap.objectSpace().forEachLiveCell<StackPreservingRecompiler>(iterationScope, recompiler);
597 }
598 m_regExpCache->invalidateCode();
599 heap.collectAllGarbage();
600 }
601
602 static void appendSourceToError(CallFrame* callFrame, ErrorInstance* exception, unsigned bytecodeOffset)
603 {
604 exception->clearAppendSourceToMessage();
605
606 if (!callFrame->codeBlock()->hasExpressionInfo())
607 return;
608
609 int startOffset = 0;
610 int endOffset = 0;
611 int divotPoint = 0;
612 unsigned line = 0;
613 unsigned column = 0;
614
615 CodeBlock* codeBlock = callFrame->codeBlock();
616 codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divotPoint, startOffset, endOffset, line, column);
617
618 int expressionStart = divotPoint - startOffset;
619 int expressionStop = divotPoint + endOffset;
620
621 const String& sourceString = codeBlock->source()->source();
622 if (!expressionStop || expressionStart > static_cast<int>(sourceString.length()))
623 return;
624
625 VM* vm = &callFrame->vm();
626 JSValue jsMessage = exception->getDirect(*vm, vm->propertyNames->message);
627 if (!jsMessage || !jsMessage.isString())
628 return;
629
630 String message = asString(jsMessage)->value(callFrame);
631
632 if (expressionStart < expressionStop)
633 message = makeString(message, " (evaluating '", codeBlock->source()->getRange(expressionStart, expressionStop), "')");
634 else {
635 // No range information, so give a few characters of context.
636 const StringImpl* data = sourceString.impl();
637 int dataLength = sourceString.length();
638 int start = expressionStart;
639 int stop = expressionStart;
640 // Get up to 20 characters of context to the left and right of the divot, clamping to the line.
641 // Then strip whitespace.
642 while (start > 0 && (expressionStart - start < 20) && (*data)[start - 1] != '\n')
643 start--;
644 while (start < (expressionStart - 1) && isStrWhiteSpace((*data)[start]))
645 start++;
646 while (stop < dataLength && (stop - expressionStart < 20) && (*data)[stop] != '\n')
647 stop++;
648 while (stop > expressionStart && isStrWhiteSpace((*data)[stop - 1]))
649 stop--;
650 message = makeString(message, " (near '...", codeBlock->source()->getRange(start, stop), "...')");
651 }
652
653 exception->putDirect(*vm, vm->propertyNames->message, jsString(vm, message));
654 }
655
656 JSValue VM::throwException(ExecState* exec, JSValue error)
657 {
658 if (Options::breakOnThrow()) {
659 dataLog("In call frame ", RawPointer(exec), " for code block ", *exec->codeBlock(), "\n");
660 CRASH();
661 }
662
663 ASSERT(exec == topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
664
665 Vector<StackFrame> stackTrace;
666 interpreter->getStackTrace(stackTrace);
667 m_exceptionStack = RefCountedArray<StackFrame>(stackTrace);
668 m_exception = error;
669
670 if (stackTrace.isEmpty() || !error.isObject())
671 return error;
672 JSObject* exception = asObject(error);
673
674 StackFrame stackFrame;
675 for (unsigned i = 0 ; i < stackTrace.size(); ++i) {
676 stackFrame = stackTrace.at(i);
677 if (stackFrame.bytecodeOffset)
678 break;
679 }
680 unsigned bytecodeOffset = stackFrame.bytecodeOffset;
681 if (!hasErrorInfo(exec, exception)) {
682 // FIXME: We should only really be adding these properties to VM generated exceptions,
683 // but the inspector currently requires these for all thrown objects.
684 unsigned line;
685 unsigned column;
686 stackFrame.computeLineAndColumn(line, column);
687 exception->putDirect(*this, Identifier(this, "line"), jsNumber(line), ReadOnly | DontDelete);
688 exception->putDirect(*this, Identifier(this, "column"), jsNumber(column), ReadOnly | DontDelete);
689 if (!stackFrame.sourceURL.isEmpty())
690 exception->putDirect(*this, Identifier(this, "sourceURL"), jsString(this, stackFrame.sourceURL), ReadOnly | DontDelete);
691 }
692 if (exception->isErrorInstance() && static_cast<ErrorInstance*>(exception)->appendSourceToMessage()) {
693 unsigned stackIndex = 0;
694 CallFrame* callFrame;
695 for (callFrame = exec; callFrame && !callFrame->codeBlock(); ) {
696 stackIndex++;
697 callFrame = callFrame->callerFrameSkippingVMEntrySentinel();
698 }
699 if (callFrame && callFrame->codeBlock()) {
700 stackFrame = stackTrace.at(stackIndex);
701 bytecodeOffset = stackFrame.bytecodeOffset;
702 appendSourceToError(callFrame, static_cast<ErrorInstance*>(exception), bytecodeOffset);
703 }
704 }
705
706 if (exception->hasProperty(exec, this->propertyNames->stack))
707 return error;
708
709 exception->putDirect(*this, propertyNames->stack, interpreter->stackTraceAsString(topCallFrame, stackTrace), DontEnum);
710 return error;
711 }
712
713 JSObject* VM::throwException(ExecState* exec, JSObject* error)
714 {
715 return asObject(throwException(exec, JSValue(error)));
716 }
717 void VM::getExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
718 {
719 exception = m_exception;
720 exceptionStack = m_exceptionStack;
721 }
722 void VM::setExceptionInfo(JSValue& exception, RefCountedArray<StackFrame>& exceptionStack)
723 {
724 m_exception = exception;
725 m_exceptionStack = exceptionStack;
726 }
727
728 void VM::clearException()
729 {
730 m_exception = JSValue();
731 }
732 void VM:: clearExceptionStack()
733 {
734 m_exceptionStack = RefCountedArray<StackFrame>();
735 }
736
737 void VM::setStackPointerAtVMEntry(void* sp)
738 {
739 m_stackPointerAtVMEntry = sp;
740 updateStackLimit();
741 }
742
743 size_t VM::updateReservedZoneSize(size_t reservedZoneSize)
744 {
745 size_t oldReservedZoneSize = m_reservedZoneSize;
746 m_reservedZoneSize = reservedZoneSize;
747
748 updateStackLimit();
749
750 return oldReservedZoneSize;
751 }
752
753 #if PLATFORM(WIN)
754 // On Windows the reserved stack space consists of committed memory, a guard page, and uncommitted memory,
755 // where the guard page is a barrier between committed and uncommitted memory.
756 // When data from the guard page is read or written, the guard page is moved, and memory is committed.
757 // This is how the system grows the stack.
758 // When using the C stack on Windows we need to precommit the needed stack space.
759 // Otherwise we might crash later if we access uncommitted stack memory.
760 // This can happen if we allocate stack space larger than the page guard size (4K).
761 // The system does not get the chance to move the guard page, and commit more memory,
762 // and we crash if uncommitted memory is accessed.
763 // The MSVC compiler fixes this by inserting a call to the _chkstk() function,
764 // when needed, see http://support.microsoft.com/kb/100775.
765 // By touching every page up to the stack limit with a dummy operation,
766 // we force the system to move the guard page, and commit memory.
767
768 static void preCommitStackMemory(void* stackLimit)
769 {
770 const int pageSize = 4096;
771 for (volatile char* p = reinterpret_cast<char*>(&stackLimit); p > stackLimit; p -= pageSize) {
772 char ch = *p;
773 *p = ch;
774 }
775 }
776 #endif
777
778 inline void VM::updateStackLimit()
779 {
780 #if PLATFORM(WIN)
781 void* lastStackLimit = m_stackLimit;
782 #endif
783
784 if (m_stackPointerAtVMEntry) {
785 ASSERT(wtfThreadData().stack().isGrowingDownward());
786 char* startOfStack = reinterpret_cast<char*>(m_stackPointerAtVMEntry);
787 #if ENABLE(FTL_JIT)
788 m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize + m_largestFTLStackSize);
789 m_ftlStackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize + 2 * m_largestFTLStackSize);
790 #else
791 m_stackLimit = wtfThreadData().stack().recursionLimit(startOfStack, Options::maxPerThreadStackUsage(), m_reservedZoneSize);
792 #endif
793 } else {
794 #if ENABLE(FTL_JIT)
795 m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + m_largestFTLStackSize);
796 m_ftlStackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize + 2 * m_largestFTLStackSize);
797 #else
798 m_stackLimit = wtfThreadData().stack().recursionLimit(m_reservedZoneSize);
799 #endif
800 }
801
802 #if PLATFORM(WIN)
803 if (lastStackLimit != m_stackLimit)
804 preCommitStackMemory(m_stackLimit);
805 #endif
806 }
807
808 #if ENABLE(FTL_JIT)
809 void VM::updateFTLLargestStackSize(size_t stackSize)
810 {
811 if (stackSize > m_largestFTLStackSize) {
812 m_largestFTLStackSize = stackSize;
813 updateStackLimit();
814 }
815 }
816 #endif
817
818 void releaseExecutableMemory(VM& vm)
819 {
820 vm.releaseExecutableMemory();
821 }
822
823 #if ENABLE(DFG_JIT)
824 void VM::gatherConservativeRoots(ConservativeRoots& conservativeRoots)
825 {
826 for (size_t i = 0; i < scratchBuffers.size(); i++) {
827 ScratchBuffer* scratchBuffer = scratchBuffers[i];
828 if (scratchBuffer->activeLength()) {
829 void* bufferStart = scratchBuffer->dataBuffer();
830 conservativeRoots.add(bufferStart, static_cast<void*>(static_cast<char*>(bufferStart) + scratchBuffer->activeLength()));
831 }
832 }
833 }
834 #endif
835
836 void logSanitizeStack(VM* vm)
837 {
838 if (Options::verboseSanitizeStack() && vm->topCallFrame) {
839 int dummy;
840 dataLog(
841 "Sanitizing stack with top call frame at ", RawPointer(vm->topCallFrame),
842 ", current stack pointer at ", RawPointer(&dummy), ", in ",
843 pointerDump(vm->topCallFrame->codeBlock()), " and last code origin = ",
844 vm->topCallFrame->codeOrigin(), "\n");
845 }
846 }
847
848 #if ENABLE(REGEXP_TRACING)
849 void VM::addRegExpToTrace(RegExp* regExp)
850 {
851 gcProtect(regExp);
852 m_rtTraceList->add(regExp);
853 }
854
855 void VM::dumpRegExpTrace()
856 {
857 // The first RegExp object is ignored. It is create by the RegExpPrototype ctor and not used.
858 RTTraceList::iterator iter = ++m_rtTraceList->begin();
859
860 if (iter != m_rtTraceList->end()) {
861 dataLogF("\nRegExp Tracing\n");
862 dataLogF("Regular Expression 8 Bit 16 Bit match() Matches Average\n");
863 dataLogF(" <Match only / Match> JIT Addr JIT Address calls found String len\n");
864 dataLogF("----------------------------------------+----------------+----------------+----------+----------+-----------\n");
865
866 unsigned reCount = 0;
867
868 for (; iter != m_rtTraceList->end(); ++iter, ++reCount) {
869 (*iter)->printTraceData();
870 gcUnprotect(*iter);
871 }
872
873 dataLogF("%d Regular Expressions\n", reCount);
874 }
875
876 m_rtTraceList->clear();
877 }
878 #else
879 void VM::dumpRegExpTrace()
880 {
881 }
882 #endif
883
884 void VM::registerWatchpointForImpureProperty(const Identifier& propertyName, Watchpoint* watchpoint)
885 {
886 auto result = m_impurePropertyWatchpointSets.add(propertyName.string(), nullptr);
887 if (result.isNewEntry)
888 result.iterator->value = adoptRef(new WatchpointSet(IsWatched));
889 result.iterator->value->add(watchpoint);
890 }
891
892 void VM::addImpureProperty(const String& propertyName)
893 {
894 if (RefPtr<WatchpointSet> watchpointSet = m_impurePropertyWatchpointSets.take(propertyName))
895 watchpointSet->fireAll();
896 }
897
898 class SetEnabledProfilerFunctor {
899 public:
900 bool operator()(CodeBlock* codeBlock)
901 {
902 if (JITCode::isOptimizingJIT(codeBlock->jitType()))
903 codeBlock->jettison(Profiler::JettisonDueToLegacyProfiler);
904 return false;
905 }
906 };
907
908 void VM::setEnabledProfiler(LegacyProfiler* profiler)
909 {
910 m_enabledProfiler = profiler;
911 if (m_enabledProfiler) {
912 waitForCompilationsToComplete();
913 SetEnabledProfilerFunctor functor;
914 heap.forEachCodeBlock(functor);
915 }
916 }
917
918 void sanitizeStackForVM(VM* vm)
919 {
920 logSanitizeStack(vm);
921 #if !ENABLE(JIT)
922 vm->interpreter->stack().sanitizeStack();
923 #else
924 sanitizeStackForVMImpl(vm);
925 #endif
926 }
927
928 } // namespace JSC