Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/bin/util/softhsm2-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@

#ifdef HAVE_CXX11

std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
std::unique_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(nullptr);
std::unique_ptr<MutexFactory>* MutexFactory::instance = new std::unique_ptr<MutexFactory>();
std::unique_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::unique_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::unique_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(nullptr);
std::unique_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::unique_ptr<OSSLCryptoFactory>();
#else
std::unique_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(nullptr);
std::unique_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::unique_ptr<BotanCryptoFactory>();
#endif

#else

std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
std::auto_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(NULL);
std::auto_ptr<MutexFactory>* MutexFactory::instance = new std::auto_ptr<MutexFactory>();
std::auto_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::auto_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::auto_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(NULL);
std::auto_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::auto_ptr<OSSLCryptoFactory>();
#else
std::auto_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(NULL);
std::auto_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::auto_ptr<BotanCryptoFactory>();
#endif

#endif
Expand Down
36 changes: 18 additions & 18 deletions src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,25 @@

#ifdef HAVE_CXX11

std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
std::unique_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(nullptr);
std::unique_ptr<MutexFactory>* MutexFactory::instance = new std::unique_ptr<MutexFactory>();
std::unique_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::unique_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::unique_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(nullptr);
std::unique_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::unique_ptr<OSSLCryptoFactory>();
#else
std::unique_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(nullptr);
std::unique_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::unique_ptr<BotanCryptoFactory>();
#endif
std::unique_ptr<SoftHSM> SoftHSM::instance(nullptr);
std::unique_ptr<SoftHSM>* SoftHSM::instance = new std::unique_ptr<SoftHSM>();

#else

std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
std::auto_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(NULL);
std::auto_ptr<MutexFactory>* MutexFactory::instance = new std::auto_ptr<MutexFactory>();
std::auto_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::auto_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::auto_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(NULL);
std::auto_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::auto_ptr<OSSLCryptoFactory>();
#else
std::auto_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(NULL);
std::auto_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::auto_ptr<BotanCryptoFactory>();
#endif
std::auto_ptr<SoftHSM> SoftHSM::instance(NULL);
std::auto_ptr<SoftHSM>* SoftHSM::instance = new std::auto_ptr<SoftHSM>();

#endif

Expand Down Expand Up @@ -411,11 +411,11 @@ static void resetMutexFactoryCallbacks()
// Return the one-and-only instance
SoftHSM* SoftHSM::i()
{
if (!instance.get())
if (!instance->get())
{
instance.reset(new SoftHSM());
instance->reset(new SoftHSM());
}
else if(instance->detectFork())
else if((*instance)->detectFork())
{
if (Configuration::i()->getBool("library.reset_on_fork", false))
{
Expand All @@ -424,18 +424,18 @@ SoftHSM* SoftHSM::i()
* the old instance is first destroyed as some
* static structures are erased in the destructor.
*/
instance.reset(NULL);
instance.reset(new SoftHSM());
instance->reset(NULL);
instance->reset(new SoftHSM());
}
}

return instance.get();
return instance->get();
}

void SoftHSM::reset()
{
if (instance.get())
instance.reset();
if (instance->get())
instance->reset();
}

// Constructor
Expand Down
25 changes: 22 additions & 3 deletions src/lib/SoftHSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,30 @@ class SoftHSM
// Constructor
SoftHSM();

// The one-and-only instance
// The one-and-only instance.
//
// Smart pointers must not be used directly as class-level
// static storage: their destructor runs as part of the C++
// runtime's ordinary static-object teardown at process exit, an
// indeterminate time relative to unrelated code's own cleanup
// (see the "Static and Global Variables" section of the Google
// C++ Style Guide, which explicitly recommends "a pointer that
// is never freed" for exactly this situation). Concretely here:
// a PKCS#11 caller (e.g. a provider) may still be closing its
// own open sessions via C_CloseSession() when this destructor
// fires, and would then be operating on an already-destroyed
// singleton.
//
// The smart pointer itself is therefore heap-allocated once and
// deliberately never freed - the same "leaky singleton" idiom
// used by e.g. Drake's never_destroyed<T> - while still using it
// (not a raw pointer) to own and manage the SoftHSM object's own
// lifetime, which is destroyed exactly once, only via the
// explicit C_Finalize()/reset() path below.
#ifdef HAVE_CXX11
static std::unique_ptr<SoftHSM> instance;
static std::unique_ptr<SoftHSM>* instance;
#else
static std::auto_ptr<SoftHSM> instance;
static std::auto_ptr<SoftHSM>* instance;
#endif

// Is the SoftHSM PKCS #11 library initialised?
Expand Down
6 changes: 3 additions & 3 deletions src/lib/common/MutexFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ MutexFactory::~MutexFactory()
// Return the one-and-only instance
MutexFactory* MutexFactory::i()
{
if (!instance.get())
if (!instance->get())
{
instance.reset(new MutexFactory());
instance->reset(new MutexFactory());
}

return instance.get();
return instance->get();
}

// Get a mutex instance
Expand Down
10 changes: 7 additions & 3 deletions src/lib/common/MutexFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ class MutexFactory
CK_RV LockMutex(CK_VOID_PTR mutex);
CK_RV UnlockMutex(CK_VOID_PTR mutex);

// The one-and-only instance
// The one-and-only instance.
//
// A pointer to a heap-allocated, deliberately never-freed smart
// pointer, not a smart pointer directly: see the rationale
// beside SoftHSM::instance in SoftHSM.h.
#ifdef HAVE_CXX11
static std::unique_ptr<MutexFactory> instance;
static std::unique_ptr<MutexFactory>* instance;
#else
static std::auto_ptr<MutexFactory> instance;
static std::auto_ptr<MutexFactory>* instance;
#endif

// The function pointers
Expand Down
8 changes: 4 additions & 4 deletions src/lib/crypto/BotanCryptoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ BotanCryptoFactory::~BotanCryptoFactory()
// Return the one-and-only instance
BotanCryptoFactory* BotanCryptoFactory::i()
{
if (!instance.get())
if (!instance->get())
{
instance.reset(new BotanCryptoFactory());
instance->reset(new BotanCryptoFactory());
}

return instance.get();
return instance->get();
}

// This will destroy the one-and-only instance.
void BotanCryptoFactory::reset()
{
instance.reset();
instance->reset();
}

// Create a concrete instance of a symmetric algorithm
Expand Down
10 changes: 7 additions & 3 deletions src/lib/crypto/BotanCryptoFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ class BotanCryptoFactory : public CryptoFactory
// Constructor
BotanCryptoFactory();

// The one-and-only instance
// The one-and-only instance.
//
// A pointer to a heap-allocated, deliberately never-freed smart
// pointer, not a smart pointer directly: see the rationale
// beside SoftHSM::instance in SoftHSM.h.
#ifdef HAVE_CXX11
static std::unique_ptr<BotanCryptoFactory> instance;
static std::unique_ptr<BotanCryptoFactory>* instance;
#else
static std::auto_ptr<BotanCryptoFactory> instance;
static std::auto_ptr<BotanCryptoFactory>* instance;
#endif

// Thread specific RNG
Expand Down
8 changes: 4 additions & 4 deletions src/lib/crypto/OSSLCryptoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,18 @@ OSSLCryptoFactory::~OSSLCryptoFactory()
// Return the one-and-only instance
OSSLCryptoFactory* OSSLCryptoFactory::i()
{
if (!instance.get())
if (!instance->get())
{
instance.reset(new OSSLCryptoFactory());
instance->reset(new OSSLCryptoFactory());
}

return instance.get();
return instance->get();
}

// This will destroy the one-and-only instance.
void OSSLCryptoFactory::reset()
{
instance.reset();
instance->reset();
}

#ifdef WITH_FIPS
Expand Down
10 changes: 7 additions & 3 deletions src/lib/crypto/OSSLCryptoFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ class OSSLCryptoFactory : public CryptoFactory
// Constructor
OSSLCryptoFactory();

// The one-and-only instance
// The one-and-only instance.
//
// A pointer to a heap-allocated, deliberately never-freed smart
// pointer, not a smart pointer directly: see the rationale
// beside SoftHSM::instance in SoftHSM.h.
#ifdef HAVE_CXX11
static std::unique_ptr<OSSLCryptoFactory> instance;
static std::unique_ptr<OSSLCryptoFactory>* instance;
#else
static std::auto_ptr<OSSLCryptoFactory> instance;
static std::auto_ptr<OSSLCryptoFactory>* instance;
#endif

#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
Expand Down
16 changes: 8 additions & 8 deletions src/lib/crypto/test/cryptotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@
// Initialise the one-and-only instance
#ifdef HAVE_CXX11

std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
std::unique_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(nullptr);
std::unique_ptr<MutexFactory>* MutexFactory::instance = new std::unique_ptr<MutexFactory>();
std::unique_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::unique_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::unique_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(nullptr);
std::unique_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::unique_ptr<OSSLCryptoFactory>();
#else
std::unique_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(nullptr);
std::unique_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::unique_ptr<BotanCryptoFactory>();
#endif

#else

std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
std::auto_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(NULL);
std::auto_ptr<MutexFactory>* MutexFactory::instance = new std::auto_ptr<MutexFactory>();
std::auto_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::auto_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::auto_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(NULL);
std::auto_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::auto_ptr<OSSLCryptoFactory>();
#else
std::auto_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(NULL);
std::auto_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::auto_ptr<BotanCryptoFactory>();
#endif

#endif
Expand Down
10 changes: 5 additions & 5 deletions src/lib/data_mgr/SecureMemoryRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ SecureMemoryRegistry::~SecureMemoryRegistry()
// Return the one-and-only instance
SecureMemoryRegistry* SecureMemoryRegistry::i()
{
if (instance.get() == NULL)
if (instance->get() == NULL)
{
instance.reset(new SecureMemoryRegistry());
instance->reset(new SecureMemoryRegistry());

if (instance.get() == NULL)
if (instance->get() == NULL)
{
// This is very bad!
ERROR_MSG("failed to instantiate SecureMemoryRegistry");

}
}

return instance.get();
return instance->get();
}

// This will destroy the one-and-only instance.
void SecureMemoryRegistry::reset()
{
instance.reset();
instance->reset();
}

// Register a block of memory
Expand Down
7 changes: 5 additions & 2 deletions src/lib/data_mgr/SecureMemoryRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ class SecureMemoryRegistry
void wipe();

private:
// A pointer to a heap-allocated, deliberately never-freed smart
// pointer, not a smart pointer directly: see the rationale
// beside SoftHSM::instance in SoftHSM.h.
#ifdef HAVE_CXX11
static std::unique_ptr<SecureMemoryRegistry> instance;
static std::unique_ptr<SecureMemoryRegistry>* instance;
#else
static std::auto_ptr<SecureMemoryRegistry> instance;
static std::auto_ptr<SecureMemoryRegistry>* instance;
#endif

std::map<void*, size_t> registry;
Expand Down
16 changes: 8 additions & 8 deletions src/lib/data_mgr/test/datamgrtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@
// Initialise the one-and-only instance
#ifdef HAVE_CXX11

std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
std::unique_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(nullptr);
std::unique_ptr<MutexFactory>* MutexFactory::instance = new std::unique_ptr<MutexFactory>();
std::unique_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::unique_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::unique_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(nullptr);
std::unique_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::unique_ptr<OSSLCryptoFactory>();
#else
std::unique_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(nullptr);
std::unique_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::unique_ptr<BotanCryptoFactory>();
#endif

#else

std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
std::auto_ptr<SecureMemoryRegistry> SecureMemoryRegistry::instance(NULL);
std::auto_ptr<MutexFactory>* MutexFactory::instance = new std::auto_ptr<MutexFactory>();
std::auto_ptr<SecureMemoryRegistry>* SecureMemoryRegistry::instance = new std::auto_ptr<SecureMemoryRegistry>();
#if defined(WITH_OPENSSL)
std::auto_ptr<OSSLCryptoFactory> OSSLCryptoFactory::instance(NULL);
std::auto_ptr<OSSLCryptoFactory>* OSSLCryptoFactory::instance = new std::auto_ptr<OSSLCryptoFactory>();
#else
std::auto_ptr<BotanCryptoFactory> BotanCryptoFactory::instance(NULL);
std::auto_ptr<BotanCryptoFactory>* BotanCryptoFactory::instance = new std::auto_ptr<BotanCryptoFactory>();
#endif

#endif
Expand Down
4 changes: 2 additions & 2 deletions src/lib/handle_mgr/test/handlemgrtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
#include "MutexFactory.h"

#ifdef HAVE_CXX11
std::unique_ptr<MutexFactory> MutexFactory::instance(nullptr);
std::unique_ptr<MutexFactory>* MutexFactory::instance = new std::unique_ptr<MutexFactory>();
#else
std::auto_ptr<MutexFactory> MutexFactory::instance(NULL);
std::auto_ptr<MutexFactory>* MutexFactory::instance = new std::auto_ptr<MutexFactory>();
#endif

class MyProgressListener : public CppUnit::TextTestProgressListener
Expand Down
Loading