Ignore:
Timestamp:
Apr 28, 2012, 1:51:27 PM (13 years ago)
Author:
[email protected]
Message:

Clarified JSGlobalData (JavaScript VM) lifetime
https://bugs.webkit.org/show_bug.cgi?id=85142

Reviewed by Anders Carlsson.

Source/JavaScriptCore:

This was so confusing that I didn't feel like I could reason about
memory lifetime in the heap without fixing it.

The rules are:

(1) JSGlobalData owns the virtual machine and all memory in it.

(2) Deleting a JSGlobalData frees the virtual machine and all memory
in it.

(Caveat emptor: if you delete the virtual machine while you're running
JIT code or accessing GC objects, you're gonna have a bad time.)

(I opted not to make arbitrary sub-objects keep the virtual machine
alive automatically because:

(a) doing that right would be complex and slow;

(b) in the case of an exiting thread or process, there's no
clear way to give the garbage collector a chance to try again
later;

(c) continuing to run the garbage collector after we've been
asked to shut down the virtual machine seems rude;

(d) we've never really supported that feature, anyway.)

(3) Normal ref-counting will do. No need to call a battery of
specialty functions to tear down a JSGlobalData. Its foibles
notwithstanding, C++ does in fact know how to execute destructors in
order.

  • API/JSContextRef.cpp:

(JSGlobalContextCreate): Removed compatibility shim for older
operating systems because it's no longer used.

(JSGlobalContextRelease): Now that we can rely on JSGlobalData to "do
the right thing", this code is much simpler. We still have one special
case to notify the garbage collector if we're removing the last
reference to the global object, since this can improve memory behavior.

  • heap/CopiedSpace.cpp:

(JSC::CopiedSpace::freeAllBlocks):

  • heap/CopiedSpace.h:

(CopiedSpace): Renamed "destroy" => "freeAllBlocks" because true
destruction-time behaviors should be limited to our C++ destructor.

  • heap/Heap.cpp:

(JSC::Heap::~Heap):
(JSC):
(JSC::Heap::lastChanceToFinalize):

  • heap/Heap.h:

(Heap):
(JSC::Heap::heap): Renamed "destroy" => "lastChanceToFinalize" because
true destruction-time behaviors should be limited to our C++
destructor.

Reorganized the code, putting code that must run before any objects
get torn down into lastChanceToFinalize, and code that just tears down
objects into our destructor.

  • heap/Local.h:

(JSC::LocalStack::LocalStack):
(JSC::LocalStack::push):
(LocalStack): See rule (2).

  • jsc.cpp:

(functionQuit):
(main):
(printUsageStatement):
(parseArguments):
(jscmain):

  • testRegExp.cpp:

(main):
(printUsageStatement):
(parseArguments):
(realMain): See rule (3).

I removed the feature of ensuring orderly tear-down when calling quit()
or running in --help mode because it didn't seem very useful and
making it work with Windows structured exception handling and
NO_RETURN didn't seem like a fun way to spend a Saturday.

  • runtime/JSGlobalData.h:
  • runtime/JSGlobalData.cpp:

(JSC::JSGlobalData::JSGlobalData): Moved heap to be the first data
member in JSGlobalData to ensure that it's destructed last, so other
objects that reference it destruct without crashing. This allowed me
to remove clearBuiltinStructures() altogether, and helped guarantee
rule (3).

(JSC::JSGlobalData::~JSGlobalData): Explicitly call
lastChanceToFinalize() at the head of our destructor to ensure that
all pending finalizers run while the virtual machine is still in a
valid state. Trying to resurrect (re-ref) the virtual machine at this
point is not valid, but all other operations are.

Changed a null to a 0xbbadbeef to clarify just how bad this beef is.

  • runtime/JSGlobalObject.cpp:

(JSC::JSGlobalObject::init):

  • runtime/JSGlobalObject.h:

(JSGlobalObject):
(JSC::JSGlobalObject::globalData): See rule (3).

Source/WebCore:

  • bindings/js/WorkerScriptController.cpp:

(WebCore::WorkerScriptController::~WorkerScriptController): Slightly
simpler than before. We can't just rely on our default destructor
because we need to hold the JSLock when we tear down the VM.

  • bridge/NP_jsobject.cpp:

(_NPN_InvokeDefault):
(_NPN_Invoke):
(_NPN_Evaluate):
(_NPN_Construct): Don't RefPtr<> the JSGlobalData because it makes it
seem like you know something the rest of our code doesn't know. The
plugin JSGlobalData is immortal, anyway.

I also removed some timeout checker related code because that feature
doesn't work anymore, so it was effectively dead code.

Source/WebKit/mac:

  • Plugins/Hosted/NetscapePluginInstanceProxy.mm:

(WebKit::NetscapePluginInstanceProxy::invoke):
(WebKit::NetscapePluginInstanceProxy::invokeDefault):
(WebKit::NetscapePluginInstanceProxy::construct):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/heap/Heap.cpp

    r115545 r115579  
    342342Heap::~Heap()
    343343{
    344     // Destroy our block freeing thread.
     344    delete m_markListSet;
     345
     346    m_objectSpace.shrink();
     347    m_storageSpace.freeAllBlocks();
     348    releaseFreeBlocks();
    345349    {
    346350        MutexLocker locker(m_freeBlockLock);
     
    350354    waitForThreadCompletion(m_blockFreeingThread);
    351355
    352     // The destroy function must already have been called, so assert this.
    353     ASSERT(!m_globalData);
    354 }
    355 
    356 void Heap::destroy()
    357 {
    358     JSLock lock(SilenceAssertionsOnly);
    359 
    360     if (!m_globalData)
    361         return;
    362 
     356    ASSERT(!size());
     357    ASSERT(!capacity());
     358}
     359
     360// The JSGlobalData is being destroyed and the collector will never run again.
     361// Run all pending finalizers now because we won't get another chance.
     362void Heap::lastChanceToFinalize()
     363{
    363364    ASSERT(!m_globalData->dynamicGlobalObject);
    364365    ASSERT(m_operationInProgress == NoOperation);
    365    
    366     // The global object is not GC protected at this point, so sweeping may delete it
    367     // (and thus the global data) before other objects that may use the global data.
    368     RefPtr<JSGlobalData> protect(m_globalData);
    369 
    370 #if ENABLE(JIT)
    371     m_globalData->jitStubs->clearHostFunctionStubs();
    372 #endif
    373 
    374     delete m_markListSet;
    375     m_markListSet = 0;
    376 
     366
     367    // FIXME: Make this a release-mode crash once we're sure no one's doing this.
     368    if (size_t size = m_protectedValues.size())
     369        LOG_ERROR("JavaScriptCore heap deallocated while %ld values were still protected", size);
     370
     371    m_weakSet.finalizeAll();
    377372    canonicalizeCellLivenessData();
    378373    clearMarks();
    379 
    380     m_weakSet.finalizeAll();
     374    sweep();
    381375    m_globalData->smallStrings.finalizeSmallStrings();
    382     m_objectSpace.shrink();
    383     m_storageSpace.destroy();
    384     ASSERT(!size());
    385376
    386377#if ENABLE(SIMPLE_HEAP_PROFILING)
     
    388379    m_destroyedTypeCounts.dump(WTF::dataFile(), "Destroyed Type Counts");
    389380#endif
    390    
    391     releaseFreeBlocks();
    392 
    393     m_globalData = 0;
    394381}
    395382
Note: See TracChangeset for help on using the changeset viewer.