Title: [265276] trunk/Source
Revision
265276
Author
[email protected]
Date
2020-08-04 21:19:05 -0700 (Tue, 04 Aug 2020)

Log Message

[JSC] Use LazyNeverDestroyed & std::call_once for complex singletons
https://bugs.webkit.org/show_bug.cgi?id=215153
<rdar://problem/65718983>

Reviewed by Mark Lam.

Source/_javascript_Core:

We are getting some crashes in RemoteInspector and this speculatively fixes the crash.
My guess is that NeverDestroyed<RemoteInspector> calls constructor twice in heavily contended situation:
WebKit's static does not have thread-safety. If two threads come here at the same time, it is possible that
constructor is invoked twice. In that case, later constructor will clear members, which involves clearing
Lock m_mutex field. This makes Lock's invariant broken.
This patch uses LazyNeverDestroyed and std::call_once to ensure invoking constructor only once.

* API/glib/JSCVirtualMachine.cpp:
* dfg/DFGCommonData.cpp:
* disassembler/Disassembler.cpp:
* inspector/remote/RemoteInspector.h:
* inspector/remote/cocoa/RemoteInspectorCocoa.mm:
(Inspector::RemoteInspector::singleton):
* inspector/remote/glib/RemoteInspectorGlib.cpp:
(Inspector::RemoteInspector::singleton):
* inspector/remote/socket/RemoteInspectorServer.cpp:
(Inspector::RemoteInspectorServer::singleton):
* inspector/remote/socket/RemoteInspectorServer.h:
* inspector/remote/socket/RemoteInspectorSocket.cpp:
(Inspector::RemoteInspector::singleton):
* inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp:
(Inspector::RemoteInspectorSocketEndpoint::singleton):
* interpreter/Interpreter.cpp:
(JSC::Interpreter::opcodeIDTable):
* runtime/IntlObject.cpp:
(JSC::intlAvailableLocales):
(JSC::intlCollatorAvailableLocales):
(JSC::defaultLocale):
(JSC::numberingSystemsForLocale):

Source/WTF:

Add lock's bits in crash information to investigate if this speculative fix does not work.

* wtf/LockAlgorithmInlines.h:
(WTF::Hooks>::lockSlow):
(WTF::Hooks>::unlockSlow):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/glib/JSCVirtualMachine.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/API/glib/JSCVirtualMachine.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/API/glib/JSCVirtualMachine.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -51,8 +51,12 @@
 
 static HashMap<JSContextGroupRef, JSCVirtualMachine*>& wrapperMap()
 {
-    static NeverDestroyed<HashMap<JSContextGroupRef, JSCVirtualMachine*>> map;
-    return map;
+    static LazyNeverDestroyed<HashMap<JSContextGroupRef, JSCVirtualMachine*>> shared;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        shared.construct();
+    });
+    return shared;
 }
 
 static void addWrapper(JSContextGroupRef group, JSCVirtualMachine* vm)

Modified: trunk/Source/_javascript_Core/ChangeLog (265275 => 265276)


--- trunk/Source/_javascript_Core/ChangeLog	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/ChangeLog	2020-08-05 04:19:05 UTC (rev 265276)
@@ -1,3 +1,41 @@
+2020-08-04  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Use LazyNeverDestroyed & std::call_once for complex singletons
+        https://bugs.webkit.org/show_bug.cgi?id=215153
+        <rdar://problem/65718983>
+
+        Reviewed by Mark Lam.
+
+        We are getting some crashes in RemoteInspector and this speculatively fixes the crash.
+        My guess is that NeverDestroyed<RemoteInspector> calls constructor twice in heavily contended situation:
+        WebKit's static does not have thread-safety. If two threads come here at the same time, it is possible that
+        constructor is invoked twice. In that case, later constructor will clear members, which involves clearing
+        Lock m_mutex field. This makes Lock's invariant broken.
+        This patch uses LazyNeverDestroyed and std::call_once to ensure invoking constructor only once.
+
+        * API/glib/JSCVirtualMachine.cpp:
+        * dfg/DFGCommonData.cpp:
+        * disassembler/Disassembler.cpp:
+        * inspector/remote/RemoteInspector.h:
+        * inspector/remote/cocoa/RemoteInspectorCocoa.mm:
+        (Inspector::RemoteInspector::singleton):
+        * inspector/remote/glib/RemoteInspectorGlib.cpp:
+        (Inspector::RemoteInspector::singleton):
+        * inspector/remote/socket/RemoteInspectorServer.cpp:
+        (Inspector::RemoteInspectorServer::singleton):
+        * inspector/remote/socket/RemoteInspectorServer.h:
+        * inspector/remote/socket/RemoteInspectorSocket.cpp:
+        (Inspector::RemoteInspector::singleton):
+        * inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp:
+        (Inspector::RemoteInspectorSocketEndpoint::singleton):
+        * interpreter/Interpreter.cpp:
+        (JSC::Interpreter::opcodeIDTable):
+        * runtime/IntlObject.cpp:
+        (JSC::intlAvailableLocales):
+        (JSC::intlCollatorAvailableLocales):
+        (JSC::defaultLocale):
+        (JSC::numberingSystemsForLocale):
+
 2020-08-04  Keith Miller  <[email protected]>
 
         CheckpointSideState shoud play nicely with StackOverflowException unwinding.

Modified: trunk/Source/_javascript_Core/dfg/DFGCommonData.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/dfg/DFGCommonData.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/dfg/DFGCommonData.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -61,7 +61,11 @@
 static Lock pcCodeBlockMapLock;
 inline HashMap<void*, CodeBlock*>& pcCodeBlockMap(AbstractLocker&)
 {
-    static NeverDestroyed<HashMap<void*, CodeBlock*>> pcCodeBlockMap;
+    static LazyNeverDestroyed<HashMap<void*, CodeBlock*>> pcCodeBlockMap;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        pcCodeBlockMap.construct();
+    });
     return pcCodeBlockMap;
 }
 

Modified: trunk/Source/_javascript_Core/disassembler/Disassembler.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/disassembler/Disassembler.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/disassembler/Disassembler.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -119,8 +119,12 @@
 
 AsynchronousDisassembler& asynchronousDisassembler()
 {
-    static NeverDestroyed<AsynchronousDisassembler> disassembler;
-    hadAnyAsynchronousDisassembly = true;
+    static LazyNeverDestroyed<AsynchronousDisassembler> disassembler;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        disassembler.construct();
+        hadAnyAsynchronousDisassembly = true;
+    });
     return disassembler.get();
 }
 

Modified: trunk/Source/_javascript_Core/inspector/remote/RemoteInspector.h (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/RemoteInspector.h	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/RemoteInspector.h	2020-08-05 04:19:05 UTC (rev 265276)
@@ -119,7 +119,7 @@
 #endif
     static void startDisabled();
     static RemoteInspector& singleton();
-    friend class NeverDestroyed<RemoteInspector>;
+    friend class LazyNeverDestroyed<RemoteInspector>;
 
     void registerTarget(RemoteControllableTarget*);
     void unregisterTarget(RemoteControllableTarget*);

Modified: trunk/Source/_javascript_Core/inspector/remote/cocoa/RemoteInspectorCocoa.mm (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/cocoa/RemoteInspectorCocoa.mm	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/cocoa/RemoteInspectorCocoa.mm	2020-08-05 04:19:05 UTC (rev 265276)
@@ -85,7 +85,11 @@
 
 RemoteInspector& RemoteInspector::singleton()
 {
-    static NeverDestroyed<RemoteInspector> shared;
+    static LazyNeverDestroyed<RemoteInspector> shared;
+    static dispatch_once_t onceConstructKey;
+    dispatch_once(&onceConstructKey, ^{
+        shared.construct();
+    });
 
 #if PLATFORM(COCOA)
     if (needMachSandboxExtension)

Modified: trunk/Source/_javascript_Core/inspector/remote/glib/RemoteInspectorGlib.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/glib/RemoteInspectorGlib.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/glib/RemoteInspectorGlib.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -40,7 +40,11 @@
 
 RemoteInspector& RemoteInspector::singleton()
 {
-    static NeverDestroyed<RemoteInspector> shared;
+    static LazyNeverDestroyed<RemoteInspector> shared;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        shared.construct();
+    });
     return shared;
 }
 

Modified: trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -35,8 +35,12 @@
 
 RemoteInspectorServer& RemoteInspectorServer::singleton()
 {
-    static NeverDestroyed<RemoteInspectorServer> server;
-    return server;
+    static LazyNeverDestroyed<RemoteInspectorServer> shared;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        shared.construct();
+    });
+    return shared;
 }
 
 RemoteInspectorServer::~RemoteInspectorServer()

Modified: trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.h (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.h	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorServer.h	2020-08-05 04:19:05 UTC (rev 265276)
@@ -43,7 +43,7 @@
     bool isRunning() const { return !!m_server; }
 
 private:
-    friend class NeverDestroyed<RemoteInspectorServer>;
+    friend class LazyNeverDestroyed<RemoteInspectorServer>;
     RemoteInspectorServer() { Socket::init(); }
 
     bool didAccept(ConnectionID acceptedID, ConnectionID listenerID, Socket::Domain) final;

Modified: trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocket.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -41,7 +41,11 @@
 
 RemoteInspector& RemoteInspector::singleton()
 {
-    static NeverDestroyed<RemoteInspector> shared;
+    static LazyNeverDestroyed<RemoteInspector> shared;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        shared.construct();
+    });
     return shared;
 }
 

Modified: trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/inspector/remote/socket/RemoteInspectorSocketEndpoint.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -37,7 +37,11 @@
 
 RemoteInspectorSocketEndpoint& RemoteInspectorSocketEndpoint::singleton()
 {
-    static NeverDestroyed<RemoteInspectorSocketEndpoint> shared;
+    static LazyNeverDestroyed<RemoteInspectorSocketEndpoint> shared;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [&] {
+        shared.construct();
+    });
     return shared;
 }
 

Modified: trunk/Source/_javascript_Core/interpreter/Interpreter.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/interpreter/Interpreter.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/interpreter/Interpreter.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -336,13 +336,14 @@
 #if !ENABLE(LLINT_EMBEDDED_OPCODE_ID) || ASSERT_ENABLED
 HashMap<Opcode, OpcodeID>& Interpreter::opcodeIDTable()
 {
-    static NeverDestroyed<HashMap<Opcode, OpcodeID>> opcodeIDTable;
+    static LazyNeverDestroyed<HashMap<Opcode, OpcodeID>> opcodeIDTable;
 
     static std::once_flag initializeKey;
     std::call_once(initializeKey, [&] {
+        opcodeIDTable.construct();
         const Opcode* opcodeTable = LLInt::opcodeMap();
         for (unsigned i = 0; i < NUMBER_OF_BYTECODE_IDS; ++i)
-            opcodeIDTable.get().add(opcodeTable[i], static_cast<OpcodeID>(i));
+            opcodeIDTable->add(opcodeTable[i], static_cast<OpcodeID>(i));
     });
 
     return opcodeIDTable;

Modified: trunk/Source/_javascript_Core/runtime/IntlObject.cpp (265275 => 265276)


--- trunk/Source/_javascript_Core/runtime/IntlObject.cpp	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/_javascript_Core/runtime/IntlObject.cpp	2020-08-05 04:19:05 UTC (rev 265276)
@@ -241,12 +241,11 @@
 
 const HashSet<String>& intlAvailableLocales()
 {
-    static NeverDestroyed<HashSet<String>> cachedAvailableLocales;
-    HashSet<String>& availableLocales = cachedAvailableLocales.get();
-
+    static LazyNeverDestroyed<HashSet<String>> availableLocales;
     static std::once_flag initializeOnce;
     std::call_once(initializeOnce, [&] {
-        ASSERT(availableLocales.isEmpty());
+        availableLocales.construct();
+        ASSERT(availableLocales->isEmpty());
         constexpr bool isImmortal = true;
         int32_t count = uloc_countAvailable();
         for (int32_t i = 0; i < count; ++i) {
@@ -253,8 +252,8 @@
             String locale = languageTagForLocaleID(uloc_getAvailable(i), isImmortal);
             if (locale.isEmpty())
                 continue;
-            availableLocales.add(locale);
-            addScriptlessLocaleIfNeeded(availableLocales, locale);
+            availableLocales->add(locale);
+            addScriptlessLocaleIfNeeded(availableLocales.get(), locale);
         }
     });
     return availableLocales;
@@ -262,12 +261,11 @@
 
 const HashSet<String>& intlCollatorAvailableLocales()
 {
-    static NeverDestroyed<HashSet<String>> cachedAvailableLocales;
-    HashSet<String>& availableLocales = cachedAvailableLocales.get();
-
+    static LazyNeverDestroyed<HashSet<String>> availableLocales;
     static std::once_flag initializeOnce;
     std::call_once(initializeOnce, [&] {
-        ASSERT(availableLocales.isEmpty());
+        availableLocales.construct();
+        ASSERT(availableLocales->isEmpty());
         constexpr bool isImmortal = true;
         int32_t count = ucol_countAvailable();
         for (int32_t i = 0; i < count; ++i) {
@@ -274,8 +272,8 @@
             String locale = languageTagForLocaleID(ucol_getAvailable(i), isImmortal);
             if (locale.isEmpty())
                 continue;
-            availableLocales.add(locale);
-            addScriptlessLocaleIfNeeded(availableLocales, locale);
+            availableLocales->add(locale);
+            addScriptlessLocaleIfNeeded(availableLocales.get(), locale);
         }
     });
     return availableLocales;
@@ -521,11 +519,11 @@
 
     // If all else fails, ask ICU. It will probably say something bogus like en_us even if the user
     // has configured some other language, but being wrong is better than crashing.
-    static NeverDestroyed<String> icuDefaultLocalString;
+    static LazyNeverDestroyed<String> icuDefaultLocalString;
     static std::once_flag initializeOnce;
     std::call_once(initializeOnce, [&] {
         constexpr bool isImmortal = true;
-        icuDefaultLocalString.get() = languageTagForLocaleID(uloc_getDefault(), isImmortal);
+        icuDefaultLocalString.construct(languageTagForLocaleID(uloc_getDefault(), isImmortal));
     });
     if (!icuDefaultLocalString->isEmpty())
         return icuDefaultLocalString.get();
@@ -778,12 +776,11 @@
 
 Vector<String> numberingSystemsForLocale(const String& locale)
 {
-    static NeverDestroyed<Vector<String>> cachedNumberingSystems;
-    Vector<String>& availableNumberingSystems = cachedNumberingSystems.get();
-
+    static LazyNeverDestroyed<Vector<String>> availableNumberingSystems;
     static std::once_flag initializeOnce;
     std::call_once(initializeOnce, [&] {
-        ASSERT(availableNumberingSystems.isEmpty());
+        availableNumberingSystems.construct();
+        ASSERT(availableNumberingSystems->isEmpty());
         UErrorCode status = U_ZERO_ERROR;
         UEnumeration* numberingSystemNames = unumsys_openAvailableNames(&status);
         ASSERT(U_SUCCESS(status));
@@ -796,7 +793,7 @@
             ASSERT(U_SUCCESS(status));
             // Only support algorithmic if it is the default fot the locale, handled below.
             if (!unumsys_isAlgorithmic(numsys))
-                availableNumberingSystems.append(String(StringImpl::createStaticStringImpl(result, resultLength)));
+                availableNumberingSystems->append(String(StringImpl::createStaticStringImpl(result, resultLength)));
             unumsys_close(numsys);
         }
         uenum_close(numberingSystemNames);
@@ -809,7 +806,7 @@
     unumsys_close(defaultSystem);
 
     Vector<String> numberingSystems({ defaultSystemName });
-    numberingSystems.appendVector(availableNumberingSystems);
+    numberingSystems.appendVector(availableNumberingSystems.get());
     return numberingSystems;
 }
 

Modified: trunk/Source/WTF/ChangeLog (265275 => 265276)


--- trunk/Source/WTF/ChangeLog	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/WTF/ChangeLog	2020-08-05 04:19:05 UTC (rev 265276)
@@ -1,3 +1,17 @@
+2020-08-04  Yusuke Suzuki  <[email protected]>
+
+        [JSC] Use LazyNeverDestroyed & std::call_once for complex singletons
+        https://bugs.webkit.org/show_bug.cgi?id=215153
+        <rdar://problem/65718983>
+
+        Reviewed by Mark Lam.
+
+        Add lock's bits in crash information to investigate if this speculative fix does not work.
+
+        * wtf/LockAlgorithmInlines.h:
+        (WTF::Hooks>::lockSlow):
+        (WTF::Hooks>::unlockSlow):
+
 2020-08-04  Keith Miller  <[email protected]>
 
         CheckpointSideState shoud play nicely with StackOverflowException unwinding.

Modified: trunk/Source/WTF/wtf/LockAlgorithmInlines.h (265275 => 265276)


--- trunk/Source/WTF/wtf/LockAlgorithmInlines.h	2020-08-05 02:32:36 UTC (rev 265275)
+++ trunk/Source/WTF/wtf/LockAlgorithmInlines.h	2020-08-05 04:19:05 UTC (rev 265276)
@@ -72,11 +72,11 @@
         
         if (!(currentValue & isHeldBit)) {
             dataLog("Lock not held!\n");
-            RELEASE_ASSERT_NOT_REACHED();
+            CRASH_WITH_INFO(currentValue);
         }
         if (!(currentValue & hasParkedBit)) {
             dataLog("Lock not parked!\n");
-            RELEASE_ASSERT_NOT_REACHED();
+            CRASH_WITH_INFO(currentValue);
         }
 
         // We now expect the value to be isHeld|hasParked. So long as that's the case, we can park.
@@ -113,7 +113,7 @@
         if ((oldByteValue & mask) != isHeldBit
             && (oldByteValue & mask) != (isHeldBit | hasParkedBit)) {
             dataLog("Invalid value for lock: ", oldByteValue, "\n");
-            RELEASE_ASSERT_NOT_REACHED();
+            CRASH_WITH_INFO(oldByteValue);
         }
         
         if ((oldByteValue & mask) == isHeldBit) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to