Bug 1522968 - Bug 1386601 follow-up: Use StaticMutex in S/MIME thread serizalization. r=keeler a=jorgk
authorKai Engert <kaie@kuix.de>
Fri, 25 Jan 2019 20:54:15 +0000
changeset 34354 b334fb81d2a8
parent 34353 571b2d4921ab
child 34355 f9037fd1be3d
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewerskeeler, jorgk
bugs1522968, 1386601
Bug 1522968 - Bug 1386601 follow-up: Use StaticMutex in S/MIME thread serizalization. r=keeler a=jorgk Differential Revision: https://phabricator.services.mozilla.com/D17688
mailnews/mime/src/nsCMS.cpp
--- a/mailnews/mime/src/nsCMS.cpp
+++ b/mailnews/mime/src/nsCMS.cpp
@@ -19,16 +19,17 @@
 #include "nsIX509CertDB.h"
 #include "nsNSSCertificate.h"
 #include "nsNSSComponent.h"
 #include "nsNSSHelper.h"
 #include "nsServiceManagerUtils.h"
 #include "mozpkix/Result.h"
 #include "mozpkix/pkixtypes.h"
 #include "smime.h"
+#include "mozilla/StaticMutex.h"
 
 using namespace mozilla;
 using namespace mozilla::psm;
 using namespace mozilla::pkix;
 
 extern mozilla::LazyLogModule gPIPNSSLog;
 
 NS_IMPL_ISUPPORTS(nsCMSMessage, nsICMSMessage, nsICMSMessage2)
@@ -379,33 +380,22 @@ public:
   {
     MOZ_ASSERT(NS_IsMainThread());
     mMessage = aMessage;
     mListener = aListener;
     mDigestData.Assign(reinterpret_cast<char *>(aDigestData), aDigestDataLen);
     mDigestType = aDigestType;
   }
 
-  static void InitStaticLock()
-  {
-    // If we ensure this is only executed on the main thread, we know
-    // there cannot be a race to allocate the lock multiple times.
-    MOZ_ASSERT(NS_IsMainThread());
-    if (!mLock) {
-      // Deliberate leak, one time allocation.
-      mLock = new mozilla::Mutex("SMimeVerificationTask");
-    }
-  }
-
 private:
   virtual nsresult CalculateResult() override
   {
     MOZ_ASSERT(!NS_IsMainThread());
 
-    MutexAutoLock mon(*mLock);
+    mozilla::StaticMutexAutoLock lock(sMutex);
     nsresult rv;
     if (!mDigestData.IsEmpty()) {
       rv = mMessage->VerifyDetachedSignature(
         reinterpret_cast<uint8_t*>(const_cast<char *>(mDigestData.get())),
         mDigestData.Length(), mDigestType);
     } else {
       rv = mMessage->VerifySignature();
     }
@@ -420,26 +410,25 @@ private:
     mListener->Notify(m2, rv);
   }
 
   nsCOMPtr<nsICMSMessage> mMessage;
   nsCOMPtr<nsISMimeVerificationListener> mListener;
   nsCString mDigestData;
   int16_t mDigestType;
 
-  static mozilla::Mutex *mLock;
+  static mozilla::StaticMutex sMutex;
 };
 
-mozilla::Mutex * SMimeVerificationTask::mLock = nullptr;
+mozilla::StaticMutex SMimeVerificationTask::sMutex;
 
 nsresult nsCMSMessage::CommonAsyncVerifySignature(nsISMimeVerificationListener *aListener,
                                                   unsigned char* aDigestData, uint32_t aDigestDataLen,
                                                   int16_t aDigestType)
 {
-  SMimeVerificationTask::InitStaticLock();
   RefPtr<CryptoTask> task = new SMimeVerificationTask(this, aListener, aDigestData, aDigestDataLen, aDigestType);
   return task->Dispatch("SMimeVerify");
 }
 
 class nsZeroTerminatedCertArray
 {
 public:
   nsZeroTerminatedCertArray()