|
| 1 | +2018-08-21 Saam barati <sbarati@apple.com> |
| 2 | + |
| 3 | + JSRunLoopTimer may run part of a member function after it's destroyed |
| 4 | + https://bugs.webkit.org/show_bug.cgi?id=188426 |
| 5 | + |
| 6 | + Reviewed by Mark Lam. |
| 7 | + |
| 8 | + When I was reading the JSRunLoopTimer code, I noticed that it is possible |
| 9 | + to end up running timer code after the class had been destroyed. |
| 10 | + |
| 11 | + The issue I spotted was in this function: |
| 12 | + ``` |
| 13 | + void JSRunLoopTimer::timerDidFire() |
| 14 | + { |
| 15 | + JSLock* apiLock = m_apiLock.get(); |
| 16 | + if (!apiLock) { |
| 17 | + // Likely a buggy usage: the timer fired while JSRunLoopTimer was being destroyed. |
| 18 | + return; |
| 19 | + } |
| 20 | + // HERE |
| 21 | + std::lock_guard<JSLock> lock(*apiLock); |
| 22 | + RefPtr<VM> vm = apiLock->vm(); |
| 23 | + if (!vm) { |
| 24 | + // The VM has been destroyed, so we should just give up. |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + doWork(); |
| 29 | + } |
| 30 | + ``` |
| 31 | + |
| 32 | + Look at the comment 'HERE'. Let's say that the timer callback thread gets context |
| 33 | + switched before grabbing the API lock. Then, some other thread destroys the VM. |
| 34 | + And let's say that the VM owns (perhaps transitively) this timer. Then, the |
| 35 | + timer would run code and access member variables after it was destroyed. |
| 36 | + |
| 37 | + This patch fixes this issue by introducing a new timer manager class. |
| 38 | + This class manages timers on a per VM basis. When a timer is scheduled, |
| 39 | + this class refs the timer. It also calls the timer callback while actively |
| 40 | + maintaining a +1 ref to it. So, it's no longer possible to call the timer |
| 41 | + callback after the timer has been destroyed. However, calling a timer callback |
| 42 | + can still race with the VM being destroyed. We continue to detect this case and |
| 43 | + bail out of the callback early. |
| 44 | + |
| 45 | + This patch also removes a lot of duplicate code between GCActivityCallback |
| 46 | + and JSRunLoopTimer. |
| 47 | + |
| 48 | + * heap/EdenGCActivityCallback.cpp: |
| 49 | + (JSC::EdenGCActivityCallback::doCollection): |
| 50 | + (JSC::EdenGCActivityCallback::lastGCLength): |
| 51 | + (JSC::EdenGCActivityCallback::deathRate): |
| 52 | + * heap/EdenGCActivityCallback.h: |
| 53 | + * heap/FullGCActivityCallback.cpp: |
| 54 | + (JSC::FullGCActivityCallback::doCollection): |
| 55 | + (JSC::FullGCActivityCallback::lastGCLength): |
| 56 | + (JSC::FullGCActivityCallback::deathRate): |
| 57 | + * heap/FullGCActivityCallback.h: |
| 58 | + * heap/GCActivityCallback.cpp: |
| 59 | + (JSC::GCActivityCallback::doWork): |
| 60 | + (JSC::GCActivityCallback::scheduleTimer): |
| 61 | + (JSC::GCActivityCallback::didAllocate): |
| 62 | + (JSC::GCActivityCallback::willCollect): |
| 63 | + (JSC::GCActivityCallback::cancel): |
| 64 | + (JSC::GCActivityCallback::cancelTimer): Deleted. |
| 65 | + (JSC::GCActivityCallback::nextFireTime): Deleted. |
| 66 | + * heap/GCActivityCallback.h: |
| 67 | + * heap/Heap.cpp: |
| 68 | + (JSC::Heap::reportAbandonedObjectGraph): |
| 69 | + (JSC::Heap::notifyIncrementalSweeper): |
| 70 | + (JSC::Heap::updateAllocationLimits): |
| 71 | + (JSC::Heap::didAllocate): |
| 72 | + * heap/IncrementalSweeper.cpp: |
| 73 | + (JSC::IncrementalSweeper::scheduleTimer): |
| 74 | + (JSC::IncrementalSweeper::doWork): |
| 75 | + (JSC::IncrementalSweeper::doSweep): |
| 76 | + (JSC::IncrementalSweeper::sweepNextBlock): |
| 77 | + (JSC::IncrementalSweeper::startSweeping): |
| 78 | + (JSC::IncrementalSweeper::stopSweeping): |
| 79 | + * heap/IncrementalSweeper.h: |
| 80 | + * heap/StopIfNecessaryTimer.cpp: |
| 81 | + (JSC::StopIfNecessaryTimer::doWork): |
| 82 | + (JSC::StopIfNecessaryTimer::scheduleSoon): |
| 83 | + * heap/StopIfNecessaryTimer.h: |
| 84 | + * runtime/JSRunLoopTimer.cpp: |
| 85 | + (JSC::epochTime): |
| 86 | + (JSC::JSRunLoopTimer::Manager::timerDidFireCallback): |
| 87 | + (JSC::JSRunLoopTimer::Manager::PerVMData::setRunLoop): |
| 88 | + (JSC::JSRunLoopTimer::Manager::PerVMData::PerVMData): |
| 89 | + (JSC::JSRunLoopTimer::Manager::PerVMData::~PerVMData): |
| 90 | + (JSC::JSRunLoopTimer::Manager::timerDidFire): |
| 91 | + (JSC::JSRunLoopTimer::Manager::shared): |
| 92 | + (JSC::JSRunLoopTimer::Manager::registerVM): |
| 93 | + (JSC::JSRunLoopTimer::Manager::unregisterVM): |
| 94 | + (JSC::JSRunLoopTimer::Manager::scheduleTimer): |
| 95 | + (JSC::JSRunLoopTimer::Manager::cancelTimer): |
| 96 | + (JSC::JSRunLoopTimer::Manager::timeUntilFire): |
| 97 | + (JSC::JSRunLoopTimer::Manager::didChangeRunLoop): |
| 98 | + (JSC::JSRunLoopTimer::timerDidFire): |
| 99 | + (JSC::JSRunLoopTimer::JSRunLoopTimer): |
| 100 | + (JSC::JSRunLoopTimer::timeUntilFire): |
| 101 | + (JSC::JSRunLoopTimer::setTimeUntilFire): |
| 102 | + (JSC::JSRunLoopTimer::cancelTimer): |
| 103 | + (JSC::JSRunLoopTimer::setRunLoop): Deleted. |
| 104 | + (JSC::JSRunLoopTimer::timerDidFireCallback): Deleted. |
| 105 | + (JSC::JSRunLoopTimer::scheduleTimer): Deleted. |
| 106 | + * runtime/JSRunLoopTimer.h: |
| 107 | + (JSC::JSRunLoopTimer::Manager::PerVMData::PerVMData): |
| 108 | + * runtime/PromiseDeferredTimer.cpp: |
| 109 | + (JSC::PromiseDeferredTimer::doWork): |
| 110 | + (JSC::PromiseDeferredTimer::runRunLoop): |
| 111 | + (JSC::PromiseDeferredTimer::addPendingPromise): |
| 112 | + (JSC::PromiseDeferredTimer::hasPendingPromise): |
| 113 | + (JSC::PromiseDeferredTimer::hasDependancyInPendingPromise): |
| 114 | + (JSC::PromiseDeferredTimer::cancelPendingPromise): |
| 115 | + (JSC::PromiseDeferredTimer::scheduleWorkSoon): |
| 116 | + * runtime/PromiseDeferredTimer.h: |
| 117 | + * runtime/VM.cpp: |
| 118 | + (JSC::VM::VM): |
| 119 | + (JSC::VM::~VM): |
| 120 | + (JSC::VM::setRunLoop): |
| 121 | + (JSC::VM::registerRunLoopTimer): Deleted. |
| 122 | + (JSC::VM::unregisterRunLoopTimer): Deleted. |
| 123 | + * runtime/VM.h: |
| 124 | + (JSC::VM::runLoop const): |
| 125 | + * wasm/js/WebAssemblyPrototype.cpp: |
| 126 | + (JSC::webAssemblyModuleValidateAsyncInternal): |
| 127 | + (JSC::instantiate): |
| 128 | + (JSC::compileAndInstantiate): |
| 129 | + (JSC::webAssemblyModuleInstantinateAsyncInternal): |
| 130 | + (JSC::webAssemblyCompileStreamingInternal): |
| 131 | + (JSC::webAssemblyInstantiateStreamingInternal): |
| 132 | + |
1 | 133 | 2018-08-20 Saam barati <sbarati@apple.com> |
2 | 134 |
|
3 | 135 | Inline DataView accesses into DFG/FTL |
|
0 commit comments