2012-05-31 13:33:35 +04:00
|
|
|
/* 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/. */
|
2003-01-18 17:03:00 +03:00
|
|
|
|
2016-08-20 18:00:19 +03:00
|
|
|
#ifndef nsNSSShutDown_h
|
|
|
|
#define nsNSSShutDown_h
|
2003-01-18 17:03:00 +03:00
|
|
|
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2016-08-20 18:00:19 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
#include "mozilla/CondVar.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2016-04-13 12:06:44 +03:00
|
|
|
#include "mozilla/StaticMutex.h"
|
2016-08-20 18:00:19 +03:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nspr.h"
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
class nsNSSShutDownObject;
|
|
|
|
class nsOnPK11LogoutCancelObject;
|
|
|
|
|
2016-04-13 12:06:44 +03:00
|
|
|
// Singleton, owned by nsNSSShutDownList
|
2003-01-18 17:03:00 +03:00
|
|
|
class nsNSSActivityState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsNSSActivityState();
|
|
|
|
~nsNSSActivityState();
|
|
|
|
|
|
|
|
// Call enter/leave when PSM enters a scope during which
|
|
|
|
// shutting down NSS is prohibited.
|
|
|
|
void enter();
|
|
|
|
void leave();
|
bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin
nsPSMUITracker was problematic. Apparently it was originally intended to prevent
NSS shutdown while NSS-related UI operations were going on (such as choosing a
client certificate). However, when nsNSSComponent would receive the event that
told it to shutdown NSS, it would attempt to call
mShutdownObjectList->evaporateAllNSSResources(), which would call
mActivityState.restrictActivityToCurrentThread(), which failed if such a UI
operation was in progress. This actually prevented the important part of
evaporateAllNSSResources, which is the releasing of all NSS objects in use by
PSM objects. Importantly, nsNSSComponent didn't check for or handle this failure
and proceeded to call NSS_Shutdown(), leaving PSM in an inconsistent state where
it thought it was okay to keep using the NSS objects it had when in fact it
wasn't.
In any case, nsPSMUITracker isn't really necessary as long as we have the
nsNSSShutDownPreventionLock mechanism, which mostly works and is what we should
use instead (or not at all, if no such lock is needed for the operation being
performed (for example, if no NSS functions are being called)).
2015-10-17 00:31:57 +03:00
|
|
|
// Wait for all activity to stop, and block any other thread on entering
|
|
|
|
// relevant PSM code.
|
2003-01-18 17:03:00 +03:00
|
|
|
PRStatus restrictActivityToCurrentThread();
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
// Go back to normal state.
|
|
|
|
void releaseCurrentThreadActivityRestriction();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The lock protecting all our member variables.
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
mozilla::Mutex mNSSActivityStateLock;
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
// The activity variable, bound to our lock,
|
|
|
|
// used either to signal the activity counter reaches zero,
|
|
|
|
// or a thread restriction has been released.
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
mozilla::CondVar mNSSActivityChanged;
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
// The number of active scopes holding resources.
|
|
|
|
int mNSSActivityCounter;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
// nullptr means "no restriction"
|
2012-10-18 00:48:36 +04:00
|
|
|
// if not null, activity is only allowed on that thread
|
2003-01-18 17:03:00 +03:00
|
|
|
PRThread* mNSSRestrictedThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper class that automatically enters/leaves the global activity state
|
|
|
|
class nsNSSShutDownPreventionLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsNSSShutDownPreventionLock();
|
|
|
|
~nsNSSShutDownPreventionLock();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Singleton, used by nsNSSComponent to track the list of PSM objects,
|
|
|
|
// which hold NSS resources and support the "early cleanup mechanism".
|
|
|
|
class nsNSSShutDownList
|
|
|
|
{
|
|
|
|
public:
|
2016-04-13 12:06:44 +03:00
|
|
|
static void shutdown();
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
// track instances that support early cleanup
|
|
|
|
static void remember(nsNSSShutDownObject *o);
|
|
|
|
static void forget(nsNSSShutDownObject *o);
|
|
|
|
|
|
|
|
// track instances that would like notification when
|
|
|
|
// a PK11 logout operation is performed.
|
|
|
|
static void remember(nsOnPK11LogoutCancelObject *o);
|
|
|
|
static void forget(nsOnPK11LogoutCancelObject *o);
|
|
|
|
|
|
|
|
// Do the "early cleanup", if possible.
|
2016-04-13 12:06:44 +03:00
|
|
|
static nsresult evaporateAllNSSResources();
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
// PSM has been asked to log out of a token.
|
|
|
|
// Notify all registered instances that want to react to that event.
|
2016-04-13 12:06:44 +03:00
|
|
|
static nsresult doPK11Logout();
|
|
|
|
|
|
|
|
// Signal entering/leaving a scope where shutting down NSS is prohibited.
|
|
|
|
static void enterActivityState();
|
|
|
|
static void leaveActivityState();
|
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
private:
|
2016-04-13 12:06:44 +03:00
|
|
|
static bool construct(const mozilla::StaticMutexAutoLock& /*proofOfLock*/);
|
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownList();
|
2016-04-13 12:06:44 +03:00
|
|
|
~nsNSSShutDownList();
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
protected:
|
2015-05-20 02:46:17 +03:00
|
|
|
PLDHashTable mObjects;
|
|
|
|
PLDHashTable mPK11LogoutCancelObjects;
|
2003-01-18 17:03:00 +03:00
|
|
|
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.
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
The tricky part is that it is not possible to call virtual
|
|
|
|
functions from a destructor.
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
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().
|
|
|
|
|
2014-01-14 21:28:43 +04:00
|
|
|
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
|
2016-08-20 18:00:19 +03:00
|
|
|
shutdown(ShutdownCalledFrom::Object). The second call will deregister with
|
2014-01-14 21:28:43 +04:00
|
|
|
the tracking list, to ensure no additional attempt to free the resources
|
2003-01-18 17:03:00 +03:00
|
|
|
will be made.
|
2014-01-14 21:28:43 +04:00
|
|
|
|
2016-01-23 01:49:39 +03:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
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.
|
|
|
|
|
2014-01-14 21:28:43 +04:00
|
|
|
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).
|
|
|
|
|
2014-02-05 04:14:51 +04:00
|
|
|
destructorSafeDestroyNSSReference() MUST NOT cause any other
|
|
|
|
nsNSSShutDownObject to be deconstructed. Doing so can cause
|
|
|
|
unsupported concurrent operations on the hash table in the
|
|
|
|
nsNSSShutDownList.
|
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
class derivedClass : public nsISomeInterface,
|
|
|
|
public nsNSSShutDownObject
|
|
|
|
{
|
|
|
|
virtual void virtualDestroyNSSReference()
|
|
|
|
{
|
|
|
|
destructorSafeDestroyNSSReference();
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
void destructorSafeDestroyNSSReference()
|
|
|
|
{
|
|
|
|
// clean up all NSS resources here
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~derivedClass()
|
|
|
|
{
|
2014-01-14 21:28:43 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
2003-01-18 17:03:00 +03:00
|
|
|
destructorSafeDestroyNSSReference();
|
2016-08-20 18:00:19 +03:00
|
|
|
shutdown(ShutdownCalledFrom::Object);
|
2003-01-18 17:03:00 +03:00
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
NS_IMETHODIMP doSomething()
|
|
|
|
{
|
|
|
|
if (isAlreadyShutDown())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
// use the NSS resources and do something
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
class nsNSSShutDownObject
|
|
|
|
{
|
|
|
|
public:
|
2016-08-20 18:00:19 +03:00
|
|
|
enum class ShutdownCalledFrom {
|
|
|
|
List,
|
|
|
|
Object,
|
|
|
|
};
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
nsNSSShutDownObject()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mAlreadyShutDown = false;
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownList::remember(this);
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
virtual ~nsNSSShutDownObject()
|
|
|
|
{
|
2016-08-20 18:00:19 +03:00
|
|
|
// The derived class must call
|
|
|
|
// shutdown(ShutdownCalledFrom::Object);
|
2003-01-18 17:03:00 +03:00
|
|
|
// in its destructor
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
|
|
|
void shutdown(ShutdownCalledFrom calledFrom)
|
2003-01-18 17:03:00 +03:00
|
|
|
{
|
|
|
|
if (!mAlreadyShutDown) {
|
2016-08-20 18:00:19 +03:00
|
|
|
switch (calledFrom) {
|
|
|
|
case ShutdownCalledFrom::Object:
|
|
|
|
nsNSSShutDownList::forget(this);
|
|
|
|
break;
|
|
|
|
case ShutdownCalledFrom::List:
|
|
|
|
virtualDestroyNSSReference();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("shutdown() called from an unknown source");
|
2003-01-18 17:03:00 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mAlreadyShutDown = true;
|
2003-01-18 17:03:00 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2014-04-29 19:45:00 +04:00
|
|
|
bool isAlreadyShutDown() const { return mAlreadyShutDown; }
|
2003-01-18 17:03:00 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void virtualDestroyNSSReference() = 0;
|
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
volatile bool mAlreadyShutDown;
|
2003-01-18 17:03:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsOnPK11LogoutCancelObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsOnPK11LogoutCancelObject()
|
2016-08-20 18:00:19 +03:00
|
|
|
: mIsLoggedOut(false)
|
2003-01-18 17:03:00 +03:00
|
|
|
{
|
|
|
|
nsNSSShutDownList::remember(this);
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
virtual ~nsOnPK11LogoutCancelObject()
|
|
|
|
{
|
|
|
|
nsNSSShutDownList::forget(this);
|
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
void logout()
|
|
|
|
{
|
|
|
|
// We do not care for a race condition.
|
|
|
|
// Once the bool arrived at false,
|
|
|
|
// later calls to isPK11LoggedOut() will see it.
|
|
|
|
// This is a one-time change from 0 to 1.
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsLoggedOut = true;
|
2003-01-18 17:03:00 +03:00
|
|
|
}
|
2016-08-20 18:00:19 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isPK11LoggedOut()
|
2003-01-18 17:03:00 +03:00
|
|
|
{
|
|
|
|
return mIsLoggedOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
volatile bool mIsLoggedOut;
|
2003-01-18 17:03:00 +03:00
|
|
|
};
|
|
|
|
|
2016-08-20 18:00:19 +03:00
|
|
|
#endif // nsNSSShutDown_h
|