bug 1417680 - explore the feasibility of making XPCOM responsible for shutting down NSS r=jcj r=franziskus r=erahm
☠☠ backed out by 24b4c8683fc4 ☠ ☠
authorDavid Keeler <dkeeler@mozilla.com>
Fri, 10 Nov 2017 15:03:23 -0800
changeset 452964 28f876e75d47e8c2843332e631145b6a1acf0263
parent 452963 6e74b98b535425ea96ecbf65a391e9b6f3f51bc4
child 452965 02f8ce099ade4c097dab1685c58deacc7608cbed
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjcj, franziskus, erahm
bugs1417680, 1421084
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 1417680 - explore the feasibility of making XPCOM responsible for shutting down NSS r=jcj r=franziskus r=erahm Historically, PSM has handled tracking NSS resources, releasing them, and shutting down NSS in a coordinated manner (i.e. preventing races, use-after-frees, etc.). This approach has proved intractable. This patch introduces a new approach: have XPCOM shut down NSS after all threads have been joined and the component manager has been shut down (and so there shouldn't be any XPCOM objects holding NSS resources). Note that this patch only attempts to determine if this approach will work. If it does, we will have to go through alter and remove the remnants of the old approach (i.e. nsNSSShutDownPreventionLock and related machinery). This will be done in bug 1421084. MozReview-Commit-ID: LjgEl1UZqkC
security/manager/ssl/moz.build
security/manager/ssl/nsNSSComponent.cpp
security/manager/ssl/nsNSSShutDown.cpp
security/manager/ssl/nsNSSShutDown.h
xpcom/build/XPCOMInit.cpp
--- a/security/manager/ssl/moz.build
+++ b/security/manager/ssl/moz.build
@@ -109,17 +109,16 @@ UNIFIED_SOURCES += [
     'nsNSSCertificate.cpp',
     'nsNSSCertificateDB.cpp',
     'nsNSSCertTrust.cpp',
     'nsNSSCertValidity.cpp',
     'nsNSSComponent.cpp',
     'nsNSSErrors.cpp',
     'nsNSSIOLayer.cpp',
     'nsNSSModule.cpp',
-    'nsNSSShutDown.cpp',
     'nsNSSVersion.cpp',
     'nsNTLMAuthModule.cpp',
     'nsPK11TokenDB.cpp',
     'nsPKCS11Slot.cpp',
     'nsPKCS12Blob.cpp',
     'nsProtectedAuthThread.cpp',
     'nsRandomGenerator.cpp',
     'nsSecureBrowserUIImpl.cpp',
--- a/security/manager/ssl/nsNSSComponent.cpp
+++ b/security/manager/ssl/nsNSSComponent.cpp
@@ -2138,26 +2138,20 @@ nsNSSComponent::InitializeNSS()
 }
 
 void
 nsNSSComponent::ShutdownNSS()
 {
   MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("nsNSSComponent::ShutdownNSS\n"));
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
 
-  // This is idempotent and can happen as a result of observing
-  // profile-before-change and being called from nsNSSComponent's destructor.
-  // We need to do this before other cleanup because we must avoid acquiring
-  // mMutex and then preventing threads holding nsNSSShutDownPreventionLocks
-  // from continuing (which is what evaporateAllNSSResourcesAndShutDown does).
-  MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("evaporating psm resources"));
-  if (NS_FAILED(nsNSSShutDownList::evaporateAllNSSResourcesAndShutDown())) {
-    MOZ_LOG(gPIPNSSLog, LogLevel::Error, ("failed to evaporate resources"));
-    return;
-  }
+  // If we don't do this we might try to unload the loadable roots while the
+  // loadable roots loading thread is setting up EV information, which can cause
+  // it to fail to find the roots it is expecting.
+  Unused << BlockUntilLoadableRootsLoaded();
 
   // This currently calls GetPIPNSSBundleString, which acquires mMutex, so we
   // can't call it while already holding mMutex. This is fine as mMutex doesn't
   // actually protect anything that UnloadLoadableRoots is modifying. Also,
   // UnloadLoadableRoots is idempotent.
   UnloadLoadableRoots();
 
   MutexAutoLock lock(mMutex);
@@ -2179,25 +2173,21 @@ nsNSSComponent::ShutdownNSS()
   Preferences::RemoveObserver(this, "security.");
 
   SSL_ClearSessionCache();
   // TLSServerSocket may be run with the session cache enabled. This ensures
   // those resources are cleaned up.
   Unused << SSL_ShutdownServerSessionIDCache();
 
   // Release the default CertVerifier. This will cause any held NSS resources
-  // to be released (it's not an nsNSSShutDownObject, so we have to do this
-  // manually).
+  // to be released.
   mDefaultCertVerifier = nullptr;
-
-  if (NSS_Shutdown() != SECSuccess) {
-    MOZ_LOG(gPIPNSSLog, LogLevel::Error, ("NSS SHUTDOWN FAILURE"));
-  } else {
-    MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("NSS shutdown =====>> OK <<====="));
-  }
+  // We don't actually shut down NSS - XPCOM does, after all threads have been
+  // joined and the component manager has been shut down (and so there shouldn't
+  // be any XPCOM objects holding NSS resources).
 }
 
 nsresult
 nsNSSComponent::Init()
 {
   MOZ_RELEASE_ASSERT(NS_IsMainThread());
   if (!NS_IsMainThread()) {
     return NS_ERROR_NOT_SAME_THREAD;
deleted file mode 100644
--- a/security/manager/ssl/nsNSSShutDown.cpp
+++ /dev/null
@@ -1,240 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "nsNSSShutDown.h"
-
-#include "mozilla/Casting.h"
-#include "nsCOMPtr.h"
-
-using namespace mozilla;
-
-extern LazyLogModule gPIPNSSLog;
-
-struct ObjectHashEntry : PLDHashEntryHdr {
-  nsNSSShutDownObject *obj;
-};
-
-static bool
-ObjectSetMatchEntry(const PLDHashEntryHdr *hdr, const void *key)
-{
-  const ObjectHashEntry *entry = static_cast<const ObjectHashEntry*>(hdr);
-  return entry->obj == static_cast<const nsNSSShutDownObject*>(key);
-}
-
-static void
-ObjectSetInitEntry(PLDHashEntryHdr *hdr, const void *key)
-{
-  ObjectHashEntry *entry = static_cast<ObjectHashEntry*>(hdr);
-  entry->obj = const_cast<nsNSSShutDownObject*>(static_cast<const nsNSSShutDownObject*>(key));
-}
-
-static const PLDHashTableOps gSetOps = {
-  PLDHashTable::HashVoidPtrKeyStub,
-  ObjectSetMatchEntry,
-  PLDHashTable::MoveEntryStub,
-  PLDHashTable::ClearEntryStub,
-  ObjectSetInitEntry
-};
-
-StaticMutex sListLock;
-Atomic<bool> sInShutdown(false);
-nsNSSShutDownList *singleton = nullptr;
-
-nsNSSShutDownList::nsNSSShutDownList()
-  : mObjects(&gSetOps, sizeof(ObjectHashEntry))
-{
-}
-
-nsNSSShutDownList::~nsNSSShutDownList()
-{
-  MOZ_ASSERT(this == singleton);
-  MOZ_ASSERT(sInShutdown,
-             "evaporateAllNSSResourcesAndShutDown() should have been called");
-  singleton = nullptr;
-}
-
-void nsNSSShutDownList::remember(nsNSSShutDownObject *o)
-{
-  StaticMutexAutoLock lock(sListLock);
-  if (!nsNSSShutDownList::construct(lock)) {
-    return;
-  }
-
-  MOZ_ASSERT(o);
-  singleton->mObjects.Add(o, fallible);
-}
-
-void nsNSSShutDownList::forget(nsNSSShutDownObject *o)
-{
-  StaticMutexAutoLock lock(sListLock);
-  if (!singleton) {
-    return;
-  }
-
-  MOZ_ASSERT(o);
-  singleton->mObjects.Remove(o);
-}
-
-nsresult nsNSSShutDownList::evaporateAllNSSResourcesAndShutDown()
-{
-  MOZ_RELEASE_ASSERT(NS_IsMainThread());
-  if (!NS_IsMainThread()) {
-    return NS_ERROR_NOT_SAME_THREAD;
-  }
-
-  // This makes this function idempotent and protects against reentering it
-  // (which really shouldn't happen anyway, but just in case).
-  if (sInShutdown) {
-    return NS_OK;
-  }
-
-  StaticMutexAutoLock lock(sListLock);
-  // Other threads can acquire an nsNSSShutDownPreventionLock and cause this
-  // thread to block when it calls restructActivityToCurrentThread, below. If
-  // those other threads then attempt to create an nsNSSShutDownObject, they
-  // will call nsNSSShutDownList::remember, which attempts to acquire sListLock.
-  // Consequently, holding sListLock while we're in
-  // restrictActivityToCurrentThread would result in deadlock. sListLock
-  // protects the singleton, so if we enforce that the singleton only be created
-  // and destroyed on the main thread, and if we similarly enforce that this
-  // function is only called on the main thread, what we can do is check that
-  // the singleton hasn't already gone away and then we don't actually have to
-  // hold sListLock while calling restrictActivityToCurrentThread.
-  if (!singleton) {
-    return NS_OK;
-  }
-
-  // Setting sInShutdown here means that threads calling
-  // nsNSSShutDownList::remember will return early (because
-  // nsNSSShutDownList::construct will return false) and not attempt to remember
-  // new objects. If they properly check isAlreadyShutDown(), they will also not
-  // attempt to call NSS functions or use NSS resources.
-  sInShutdown = true;
-
-  {
-    StaticMutexAutoUnlock unlock(sListLock);
-    PRStatus rv = singleton->mActivityState.restrictActivityToCurrentThread();
-    if (rv != PR_SUCCESS) {
-      MOZ_LOG(gPIPNSSLog, LogLevel::Debug,
-              ("failed to restrict activity to current thread"));
-      return NS_ERROR_FAILURE;
-    }
-  }
-
-  MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("now evaporating NSS resources"));
-  for (auto iter = singleton->mObjects.Iter(); !iter.Done(); iter.Next()) {
-    auto entry = static_cast<ObjectHashEntry*>(iter.Get());
-    entry->obj->shutdown(nsNSSShutDownObject::ShutdownCalledFrom::List);
-    iter.Remove();
-  }
-
-  singleton->mActivityState.releaseCurrentThreadActivityRestriction();
-  delete singleton;
-
-  return NS_OK;
-}
-
-void nsNSSShutDownList::enterActivityState(/*out*/ bool& enteredActivityState)
-{
-  StaticMutexAutoLock lock(sListLock);
-  if (nsNSSShutDownList::construct(lock)) {
-    singleton->mActivityState.enter();
-    enteredActivityState = true;
-  }
-}
-
-void nsNSSShutDownList::leaveActivityState()
-{
-  StaticMutexAutoLock lock(sListLock);
-  if (singleton) {
-    singleton->mActivityState.leave();
-  }
-}
-
-bool nsNSSShutDownList::construct(const StaticMutexAutoLock& /*proofOfLock*/)
-{
-  if (sInShutdown) {
-    return false;
-  }
-
-  if (!singleton && XRE_IsParentProcess()) {
-    singleton = new nsNSSShutDownList();
-  }
-
-  return !!singleton;
-}
-
-nsNSSActivityState::nsNSSActivityState()
-:mNSSActivityStateLock("nsNSSActivityState.mNSSActivityStateLock"),
- mNSSActivityChanged(mNSSActivityStateLock,
-                     "nsNSSActivityState.mNSSActivityStateLock"),
- mNSSActivityCounter(0),
- mNSSRestrictedThread(nullptr)
-{
-}
-
-nsNSSActivityState::~nsNSSActivityState()
-{
-}
-
-void nsNSSActivityState::enter()
-{
-  MutexAutoLock lock(mNSSActivityStateLock);
-
-  while (mNSSRestrictedThread && mNSSRestrictedThread != PR_GetCurrentThread()) {
-    mNSSActivityChanged.Wait();
-  }
-
-  ++mNSSActivityCounter;
-}
-
-void nsNSSActivityState::leave()
-{
-  MutexAutoLock lock(mNSSActivityStateLock);
-
-  --mNSSActivityCounter;
-
-  mNSSActivityChanged.NotifyAll();
-}
-
-PRStatus nsNSSActivityState::restrictActivityToCurrentThread()
-{
-  MutexAutoLock lock(mNSSActivityStateLock);
-
-  while (mNSSActivityCounter > 0) {
-    mNSSActivityChanged.Wait(PR_TicksPerSecond());
-  }
-
-  mNSSRestrictedThread = PR_GetCurrentThread();
-
-  return PR_SUCCESS;
-}
-
-void nsNSSActivityState::releaseCurrentThreadActivityRestriction()
-{
-  MutexAutoLock lock(mNSSActivityStateLock);
-
-  mNSSRestrictedThread = nullptr;
-
-  mNSSActivityChanged.NotifyAll();
-}
-
-nsNSSShutDownPreventionLock::nsNSSShutDownPreventionLock()
-  : mEnteredActivityState(false)
-{
-  nsNSSShutDownList::enterActivityState(mEnteredActivityState);
-}
-
-nsNSSShutDownPreventionLock::~nsNSSShutDownPreventionLock()
-{
-  if (mEnteredActivityState) {
-    nsNSSShutDownList::leaveActivityState();
-  }
-}
-
-bool
-nsNSSShutDownObject::isAlreadyShutDown() const
-{
-  return mAlreadyShutDown || sInShutdown;
-}
--- a/security/manager/ssl/nsNSSShutDown.h
+++ b/security/manager/ssl/nsNSSShutDown.h
@@ -1,235 +1,53 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsNSSShutDown_h
 #define nsNSSShutDown_h
 
-#include "PLDHashTable.h"
-#include "mozilla/Assertions.h"
-#include "mozilla/CondVar.h"
-#include "mozilla/Mutex.h"
-#include "mozilla/StaticMutex.h"
-#include "nscore.h"
-#include "nspr.h"
-
-class nsNSSShutDownObject;
-
-// Singleton, owned by nsNSSShutDownList
-class nsNSSActivityState
-{
-public:
-  nsNSSActivityState();
-  ~nsNSSActivityState();
+// This is the vestigial remains of the old NSS shutdown tracking
+// infrastructure. It will be removed entirely in bug 1421084 when we've
+// demonstrated that shutting down NSS only when all non-main-threads have been
+// joined is feasible (and beneficial).
 
-  // Call enter/leave when PSM enters a scope during which
-  // shutting down NSS is prohibited.
-  void enter();
-  void leave();
-  // Wait for all activity to stop, and block any other thread on entering
-  // relevant PSM code.
-  PRStatus restrictActivityToCurrentThread();
-
-  // Go back to normal state.
-  void releaseCurrentThreadActivityRestriction();
-
-private:
-  // The lock protecting all our member variables.
-  mozilla::Mutex mNSSActivityStateLock;
+// Yes, this races. We don't care because we're only temporarily using this to
+// silence compiler warnings. Without it every instance of a
+// nsNSSShutDownPreventionLock would be unused, causing the compiler to
+// complain.
+static int sSilenceCompilerWarnings;
 
-  // The activity variable, bound to our lock,
-  // used either to signal the activity counter reaches zero,
-  // or a thread restriction has been released.
-  mozilla::CondVar mNSSActivityChanged;
-
-  // The number of active scopes holding resources.
-  int mNSSActivityCounter;
-
-  // nullptr means "no restriction"
-  // if not null, activity is only allowed on that thread
-  PRThread* mNSSRestrictedThread;
-};
-
-// Helper class that automatically enters/leaves the global activity state
 class nsNSSShutDownPreventionLock
 {
 public:
-  nsNSSShutDownPreventionLock();
-  ~nsNSSShutDownPreventionLock();
-private:
-  // Keeps track of whether or not we actually managed to enter the NSS activity
-  // state. This is important because if we're attempting to shut down and we
-  // can't enter the NSS activity state, we need to not attempt to leave it when
-  // our destructor runs.
-  bool mEnteredActivityState;
-};
-
-// Singleton, used by nsNSSComponent to track the list of PSM objects,
-// which hold NSS resources and support the "early cleanup mechanism".
-class nsNSSShutDownList
-{
-public:
-  // track instances that support early cleanup
-  static void remember(nsNSSShutDownObject *o);
-  static void forget(nsNSSShutDownObject *o);
-
-  // Release all tracked NSS resources and prevent nsNSSShutDownObjects from
-  // using NSS functions.
-  static nsresult evaporateAllNSSResourcesAndShutDown();
-
-  // Signal entering/leaving a scope where shutting down NSS is prohibited.
-  // enteredActivityState will be set to true if we actually managed to enter
-  // the NSS activity state.
-  static void enterActivityState(/*out*/ bool& enteredActivityState);
-  static void leaveActivityState();
-
-private:
-  static bool construct(const mozilla::StaticMutexAutoLock& /*proofOfLock*/);
-
-  nsNSSShutDownList();
-  ~nsNSSShutDownList();
-
-protected:
-  PLDHashTable mObjects;
-  nsNSSActivityState mActivityState;
-};
-
-/*
-  A class deriving from nsNSSShutDownObject will have its instances
-  automatically tracked in a list. However, it must follow some rules
-  to assure correct behaviour.
-
-  The tricky part is that it is not possible to call virtual
-  functions from a destructor.
-
-  The deriving class must override virtualDestroyNSSReference().
-  Within this function, it should clean up all resources held to NSS.
-  The function will be called by the global list, if it is time to
-  shut down NSS before all references have been freed.
-
-  The same code that goes into virtualDestroyNSSReference must
-  also be called from the destructor of the deriving class,
-  which is the standard cleanup (not called from the tracking list).
-
-  Because of that duplication, it is suggested to implement a
-  function destructorSafeDestroyNSSReference() in the deriving
-  class, and make the implementation of virtualDestroyNSSReference()
-  call destructorSafeDestroyNSSReference().
+  nsNSSShutDownPreventionLock()
+  {
+    sSilenceCompilerWarnings++;
+  }
 
-  The destructor of the derived class must prevent NSS shutdown on
-  another thread by acquiring an nsNSSShutDownPreventionLock. It must
-  then check to see if NSS has already been shut down by calling
-  isAlreadyShutDown(). If NSS has not been shut down, the destructor
-  must then call destructorSafeDestroyNSSReference() and then
-  shutdown(ShutdownCalledFrom::Object). The second call will deregister with
-  the tracking list, to ensure no additional attempt to free the resources
-  will be made.
-
-  ----------------------------------------------------------------------------
-  IMPORTANT NOTE REGARDING CLASSES THAT IMPLEMENT nsNSSShutDownObject BUT DO
-  NOT DIRECTLY HOLD NSS RESOURCES:
-  ----------------------------------------------------------------------------
-  Currently, classes that do not hold NSS resources but do call NSS functions
-  inherit from nsNSSShutDownObject (and use the lock/isAlreadyShutDown
-  mechanism) as a way of ensuring it is safe to call those functions. Because
-  these classes do not hold any resources, however, it is tempting to skip the
-  destructor component of this interface. This MUST NOT be done, because
-  if an object of such a class is destructed before the nsNSSShutDownList
-  processes all of its entries, this essentially causes a use-after-free when
-  nsNSSShutDownList reaches the entry that has been destroyed. The safe way to
-  do this is to implement the destructor as usual but omit the call to
-  destructorSafeDestroyNSSReference() as it is unnecessary and probably isn't
-  defined for that class.
-
-  destructorSafeDestroyNSSReference() does not need to acquire an
-  nsNSSShutDownPreventionLock or check isAlreadyShutDown() as long as it
-  is only called by the destructor that has already acquired the lock and
-  checked for shutdown or by the NSS shutdown code itself (which acquires
-  the same lock and checks if objects it cleans up have already cleaned
-  up themselves).
-
-  destructorSafeDestroyNSSReference() MUST NOT cause any other
-  nsNSSShutDownObject to be deconstructed. Doing so can cause
-  unsupported concurrent operations on the hash table in the
-  nsNSSShutDownList.
-
-  class derivedClass : public nsISomeInterface,
-                       public nsNSSShutDownObject
+  ~nsNSSShutDownPreventionLock()
   {
-    virtual void virtualDestroyNSSReference()
-    {
-      destructorSafeDestroyNSSReference();
-    }
-
-    void destructorSafeDestroyNSSReference()
-    {
-      // clean up all NSS resources here
-    }
-
-    virtual ~derivedClass()
-    {
-      nsNSSShutDownPreventionLock locker;
-      if (isAlreadyShutDown()) {
-        return;
-      }
-      destructorSafeDestroyNSSReference();
-      shutdown(ShutdownCalledFrom::Object);
-    }
-
-    NS_IMETHODIMP doSomething()
-    {
-      if (isAlreadyShutDown())
-        return NS_ERROR_NOT_AVAILABLE;
-
-      // use the NSS resources and do something
-    }
-  };
-*/
+    sSilenceCompilerWarnings--;
+  }
+};
 
 class nsNSSShutDownObject
 {
 public:
   enum class ShutdownCalledFrom {
     List,
     Object,
   };
 
-  nsNSSShutDownObject()
-  {
-    mAlreadyShutDown = false;
-    nsNSSShutDownList::remember(this);
-  }
+  nsNSSShutDownObject() {}
 
-  virtual ~nsNSSShutDownObject()
-  {
-    // The derived class must call
-    //   shutdown(ShutdownCalledFrom::Object);
-    // in its destructor
-  }
+  virtual ~nsNSSShutDownObject() {}
 
-  void shutdown(ShutdownCalledFrom calledFrom)
-  {
-    if (!mAlreadyShutDown) {
-      switch (calledFrom) {
-        case ShutdownCalledFrom::Object:
-          nsNSSShutDownList::forget(this);
-          break;
-        case ShutdownCalledFrom::List:
-          virtualDestroyNSSReference();
-          break;
-        default:
-          MOZ_CRASH("shutdown() called from an unknown source");
-      }
-      mAlreadyShutDown = true;
-    }
-  }
+  void shutdown(ShutdownCalledFrom) {}
 
-  bool isAlreadyShutDown() const;
+  bool isAlreadyShutDown() const { return false; }
 
 protected:
   virtual void virtualDestroyNSSReference() = 0;
-private:
-  volatile bool mAlreadyShutDown;
 };
 
 #endif // nsNSSShutDown_h
--- a/xpcom/build/XPCOMInit.cpp
+++ b/xpcom/build/XPCOMInit.cpp
@@ -98,16 +98,17 @@ extern nsresult nsStringInputStreamConst
 #include "nsMacUtilsImpl.h"
 #endif
 
 #include "nsSystemInfo.h"
 #include "nsMemoryReporterManager.h"
 #include "nsMemoryInfoDumper.h"
 #include "nsSecurityConsoleMessage.h"
 #include "nsMessageLoop.h"
+#include "nss.h"
 
 #include <locale.h>
 #include "mozilla/Services.h"
 #include "mozilla/Omnijar.h"
 #include "mozilla/HangMonitor.h"
 #include "mozilla/ScriptPreloader.h"
 #include "mozilla/SystemGroup.h"
 #include "mozilla/Telemetry.h"
@@ -1000,16 +1001,32 @@ ShutdownXPCOM(nsIServiceManager* aServMg
   PROFILER_CLEAR_JS_CONTEXT();
 
   if (sInitializedJS) {
     // Shut down the JS engine.
     JS_ShutDown();
     sInitializedJS = false;
   }
 
+  // After all threads have been joined and the component manager has been shut
+  // down, any remaining objects that could be holding NSS resources (should)
+  // have been released, so we can safely shut down NSS.
+  if (NSS_IsInitialized()) {
+    // It would be nice to enforce that this succeeds, at least on debug builds.
+    // This would alert us to NSS resource leaks. Unfortunately there are some
+    // architectural roadblocks in the way. Some tests (e.g. pkix gtests) need
+    // to be re-worked to release their NSS resources when they're done. In the
+    // meantime, just emit a warning. Chasing down these leaks is tracked in
+    // bug 1230312.
+    if (NSS_Shutdown() != SECSuccess) {
+      NS_WARNING("NSS_Shutdown failed - some NSS resources are still in use "
+                 "(see bugs 1417680 and 1230312)");
+    }
+  }
+
   // Release our own singletons
   // Do this _after_ shutting down the component manager, because the
   // JS component loader will use XPConnect to call nsIModule::canUnload,
   // and that will spin up the InterfaceInfoManager again -- bad mojo
   XPTInterfaceInfoManager::FreeInterfaceInfoManager();
 
   // Finally, release the component manager last because it unloads the
   // libraries: