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

--HG--
extra : rebase_source : 95050b060a93223c6f2fce90f44e563fa6ed4fa2
This commit is contained in:
David Keeler 2017-11-10 15:03:23 -08:00
Родитель bae5308aff
Коммит dc98f8d1f2
5 изменённых файлов: 46 добавлений и 462 удалений

Просмотреть файл

@ -114,7 +114,6 @@ UNIFIED_SOURCES += [
'nsNSSErrors.cpp',
'nsNSSIOLayer.cpp',
'nsNSSModule.cpp',
'nsNSSShutDown.cpp',
'nsNSSVersion.cpp',
'nsNTLMAuthModule.cpp',
'nsPK11TokenDB.cpp',

Просмотреть файл

@ -2143,16 +2143,10 @@ 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
@ -2184,15 +2178,11 @@ nsNSSComponent::ShutdownNSS()
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

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -5,186 +5,30 @@
#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"
// 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).
class nsNSSShutDownObject;
// 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;
// Singleton, owned by nsNSSShutDownList
class nsNSSActivityState
{
public:
nsNSSActivityState();
~nsNSSActivityState();
// 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;
// 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().
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();
}
sSilenceCompilerWarnings++;
}
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
}
};
*/
~nsNSSShutDownPreventionLock()
{
sSilenceCompilerWarnings--;
}
};
class nsNSSShutDownObject
{
@ -194,42 +38,16 @@ public:
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

Просмотреть файл

@ -103,6 +103,7 @@ extern nsresult nsStringInputStreamConstructor(nsISupports*, REFNSIID, void**);
#include "nsMemoryInfoDumper.h"
#include "nsSecurityConsoleMessage.h"
#include "nsMessageLoop.h"
#include "nss.h"
#include <locale.h>
#include "mozilla/Services.h"
@ -1005,6 +1006,22 @@ ShutdownXPCOM(nsIServiceManager* aServMgr)
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,