Bug 1382329: Part 4 - Hold mMonitor while accessing scripts in the write thread. r?erahm draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 19 Jul 2017 14:16:56 -0700
changeset 611646 e5b81397e9fe17a23a1606053a595ef8fefe4762
parent 611645 89491b1bda252e5bbf9e0390a6aa10fb473c26d5
child 638219 9f3752cff65c9706ddf64f5a1e5d8352b43d86bc
push id69284
push usermaglione.k@gmail.com
push dateThu, 20 Jul 2017 00:17:58 +0000
reviewerserahm
bugs1382329
milestone56.0a1
Bug 1382329: Part 4 - Hold mMonitor while accessing scripts in the write thread. r?erahm MozReview-Commit-ID: 66se8G27sqQ
js/xpconnect/loader/ScriptPreloader.cpp
--- a/js/xpconnect/loader/ScriptPreloader.cpp
+++ b/js/xpconnect/loader/ScriptPreloader.cpp
@@ -267,20 +267,20 @@ ScriptPreloader::Cleanup()
     }
 
     // Wait for any pending parses to finish before clearing the mScripts
     // hashtable, since the parse tasks depend on memory allocated by those
     // scripts.
     {
         MonitorAutoLock mal(mMonitor);
         FinishPendingParses(mal);
+
+        mScripts.Clear();
     }
 
-    mScripts.Clear();
-
     AutoSafeJSAPI jsapi;
     JS_RemoveExtraGCRootsTracer(jsapi.cx(), TraceOp, this);
 
     UnregisterWeakMemoryReporter(this);
 }
 
 void
 ScriptPreloader::InvalidateCache()
@@ -631,16 +631,21 @@ ScriptPreloader::WriteCache()
     if (exists) {
         NS_TRY(cacheFile->Remove(false));
     }
 
     {
         AutoFDClose fd;
         NS_TRY(cacheFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE, 0644, &fd.rwget()));
 
+        // We also need to hold mMonitor while we're touching scripts in
+        // mScripts, or they may be freed before we're done with them.
+        mMonitor.AssertNotCurrentThreadOwns();
+        MonitorAutoLock mal(mMonitor);
+
         nsTArray<CachedScript*> scripts;
         for (auto& script : IterHash(mScripts, Match<ScriptStatus::Saved>())) {
             scripts.AppendElement(script);
         }
 
         // Sort scripts by load time, with async loaded scripts before sync scripts.
         // Since async scripts are always loaded immediately at startup, it helps to
         // have them stored contiguously.