From 6913847c616758791214954dc3adc1492bdc688d Mon Sep 17 00:00:00 2001 From: "davidben@chromium.org" Date: Fri, 25 Jun 2010 22:44:48 +0000 Subject: Add a unit test to check KeygenHandler's thread-safety We'll want some semblance of thread-safety when we make keygen asynchronous. R=wtc,mattm BUG=148 TEST=unit test Review URL: http://codereview.chromium.org/2838010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50903 0039d316-1c4b-4281-b951-d872f2087c98 --- base/nss_util.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'base/nss_util.cc') diff --git a/base/nss_util.cc b/base/nss_util.cc index 7043b8d..091ca05 100644 --- a/base/nss_util.cc +++ b/base/nss_util.cc @@ -18,6 +18,11 @@ #include "base/singleton.h" #include "base/string_util.h" +#if defined(USE_NSS) +#include "base/lock.h" +#include "base/scoped_ptr.h" +#endif // defined(USE_NSS) + // On some platforms, we use NSS for SSL only -- we don't use NSS for crypto // or certificate verification, and we don't use the NSS certificate and key // databases. @@ -163,11 +168,18 @@ class NSSInitSingleton { // log in. PK11SlotInfo* slot = PK11_GetInternalKeySlot(); if (slot) { + // PK11_InitPin may write to the keyDB, but no other thread can use NSS + // yet, so we don't need to lock. if (PK11_NeedUserInit(slot)) PK11_InitPin(slot, NULL, NULL); PK11_FreeSlot(slot); } + // TODO(davidben): When https://bugzilla.mozilla.org/show_bug.cgi?id=564011 + // is fixed, we will no longer need the lock. We should detect this and not + // initialize a Lock here. + write_lock_.reset(new Lock()); + root_ = InitDefaultRootCerts(); #endif // defined(USE_NSS_FOR_SSL_ONLY) } @@ -219,10 +231,19 @@ class NSSInitSingleton { return PK11_GetInternalKeySlot(); } +#if defined(USE_NSS) + Lock* write_lock() { + return write_lock_.get(); + } +#endif // defined(USE_NSS) + private: PK11SlotInfo* real_db_slot_; // Overrides internal key slot if non-NULL. SECMODModule *root_; bool chromeos_user_logged_in_; +#if defined(USE_NSS) + scoped_ptr write_lock_; +#endif // defined(USE_NSS) }; } // namespace @@ -237,6 +258,25 @@ void EnsureNSSInit() { Singleton::get(); } +#if defined(USE_NSS) +Lock* GetNSSWriteLock() { + return Singleton::get()->write_lock(); +} + +AutoNSSWriteLock::AutoNSSWriteLock() : lock_(GetNSSWriteLock()) { + // May be NULL if the lock is not needed in our version of NSS. + if (lock_) + lock_->Acquire(); +} + +AutoNSSWriteLock::~AutoNSSWriteLock() { + if (lock_) { + lock_->AssertAcquired(); + lock_->Release(); + } +} +#endif // defined(USE_NSS) + #if defined(OS_CHROMEOS) void OpenPersistentNSSDB() { Singleton::get()->OpenPersistentNSSDB(); -- cgit v1.1