Ignore:
Timestamp:
Feb 6, 2015, 12:57:45 PM (11 years ago)
Author:
[email protected]
Message:

MachineThreads should be ref counted.
<https://webkit.org/b/141317>

Reviewed by Filip Pizlo.

The VM's MachineThreads registry object is being referenced from other
threads as a raw pointer. In a scenario where the VM is destructed on
the main thread, there is no guarantee that another thread isn't still
holding a reference to the registry and will eventually invoke
removeThread() on it on thread exit. Hence, there's a possible use
after free scenario here.

The fix is to make MachineThreads ThreadSafeRefCounted, and have all
threads that references keep a RefPtr to it to ensure that it stays
alive until the very last thread is done with it.

  • API/tests/testapi.mm:

(useVMFromOtherThread): - Renamed to be more descriptive.
(useVMFromOtherThreadAndOutliveVM):

  • Added a test that has another thread which uses the VM outlive the VM to confirm that there is no crash.

However, I was not actually able to get the VM to crash without this
patch because I wasn't always able to the thread destructor to be
called. With this patch applied, I did verify with some logging that
the MachineThreads registry is only destructed after all threads
have removed themselves from it.

(threadMain): Deleted.

  • heap/Heap.cpp:

(JSC::Heap::Heap):
(JSC::Heap::~Heap):
(JSC::Heap::gatherStackRoots):

  • heap/Heap.h:

(JSC::Heap::machineThreads):

  • heap/MachineStackMarker.cpp:

(JSC::MachineThreads::Thread::Thread):
(JSC::MachineThreads::addCurrentThread):
(JSC::MachineThreads::removeCurrentThread):

  • heap/MachineStackMarker.h:
File:
1 edited

Legend:

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

    r179728 r179753  
    312312    , m_storageSpace(this)
    313313    , m_extraMemoryUsage(0)
    314     , m_machineThreads(this)
    315314    , m_sharedData(vm)
    316315    , m_slotVisitor(m_sharedData)
     
    341340#endif
    342341{
     342    m_machineThreads = adoptRef(new MachineThreads(this));
    343343    m_storageSpace.init();
    344344    if (Options::verifyHeap())
     
    348348Heap::~Heap()
    349349{
     350    // We need to remove the main thread explicitly here because the main thread
     351    // may not terminate for a while though the Heap (and VM) is being shut down.
     352    m_machineThreads->removeCurrentThread();
    350353}
    351354
     
    588591    GCPHASE(GatherStackRoots);
    589592    m_jitStubRoutines.clearMarks();
    590     m_machineThreads.gatherConservativeRoots(roots, m_jitStubRoutines, m_codeBlocks, dummy, registers);
     593    m_machineThreads->gatherConservativeRoots(roots, m_jitStubRoutines, m_codeBlocks, dummy, registers);
    591594}
    592595
Note: See TracChangeset for help on using the changeset viewer.