Bug 1425462 Refactor the static members into static globals to avoid leakchecks. r=ckerschb

MozReview-Commit-ID: 2lm1Skf3KEH

--HG--
extra : rebase_source : 86c040ec1981464166db47919d00d36817bc2e90
This commit is contained in:
Tom Ritter 2018-03-01 05:24:36 -06:00
parent c095f67f73
commit e7fb93bb60
2 changed files with 15 additions and 27 deletions

View File

@ -86,9 +86,7 @@ static uint32_t sVideoDroppedRatio;
static uint32_t sTargetVideoRes;
nsDataHashtable<KeyboardHashKey, const SpoofingKeyboardCode*>*
nsRFPService::sSpoofingKeyboardCodes = nullptr;
UniquePtr<LRUCache> nsRFPService::sCache;
UniquePtr<uint8_t[]> nsRFPService::sSecretMidpointSeed;
mozilla::Mutex* nsRFPService::sLock = nullptr;
static mozilla::StaticMutex sLock;
/* static */
nsRFPService*
@ -235,6 +233,9 @@ private:
mozilla::Mutex mLock;
};
// We make a single LRUCache
static StaticAutoPtr<LRUCache> sCache;
/**
* The purpose of this function is to deterministicly generate a random midpoint
* between a lower clamped value and an upper clamped value. Assuming a clamping
@ -300,11 +301,13 @@ nsRFPService::RandomMidpoint(long long aClampedTimeUSec,
nsresult rv;
const int kSeedSize = 16;
const int kClampTimesPerDigest = HASH_DIGEST_SIZE_BITS / 32;
static uint8_t * sSecretMidpointSeed = nullptr;
if(MOZ_UNLIKELY(!sCache)) {
MutexAutoLock lock(*sLock);
StaticMutexAutoLock lock(sLock);
if(MOZ_LIKELY(!sCache)) {
sCache = MakeUnique<LRUCache>();
sCache = new LRUCache();
ClearOnShutdown(&sCache);
}
}
@ -340,27 +343,24 @@ nsRFPService::RandomMidpoint(long long aClampedTimeUSec,
if(hashResult.Length() != HASH_DIGEST_SIZE_BYTES) { // Cache Miss =(
// If someone has pased in the testing-only parameter, replace our seed with it
if (aSecretSeed != nullptr) {
MutexAutoLock lock(*sLock);
StaticMutexAutoLock lock(sLock);
if (sSecretMidpointSeed) {
// Deletes the object pointed to as well
sSecretMidpointSeed = nullptr;
delete[] sSecretMidpointSeed;
}
sSecretMidpointSeed = MakeUnique<uint8_t[]>(kSeedSize);
memcpy(sSecretMidpointSeed.get(), aSecretSeed, kSeedSize);
sSecretMidpointSeed = new uint8_t[kSeedSize];
memcpy(sSecretMidpointSeed, aSecretSeed, kSeedSize);
}
// If we don't have a seed, we need to get one.
if(MOZ_UNLIKELY(!sSecretMidpointSeed)) {
MutexAutoLock lock(*sLock);
StaticMutexAutoLock lock(sLock);
if(MOZ_LIKELY(!sSecretMidpointSeed)) {
nsCOMPtr<nsIRandomGenerator> randomGenerator =
do_GetService("@mozilla.org/security/random-generator;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
uint8_t* buffer;
rv = randomGenerator->GenerateRandomBytes(kSeedSize, &buffer);
rv = randomGenerator->GenerateRandomBytes(kSeedSize, &sSecretMidpointSeed);
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
sSecretMidpointSeed.reset(buffer);
}
}
@ -394,7 +394,7 @@ nsRFPService::RandomMidpoint(long long aClampedTimeUSec,
rv = hasher->Init(nsICryptoHash::SHA256);
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Update(sSecretMidpointSeed.get(), kSeedSize);
rv = hasher->Update(sSecretMidpointSeed, kSeedSize);
NS_ENSURE_SUCCESS(rv, rv);
rv = hasher->Update((const uint8_t *)&extraClampedTime, sizeof(extraClampedTime));
@ -642,8 +642,6 @@ nsRFPService::Init()
nsresult rv;
sLock = new mozilla::Mutex("mozilla.resistFingerprinting.mLock");
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
NS_ENSURE_TRUE(obs, NS_ERROR_NOT_AVAILABLE);
@ -780,12 +778,6 @@ nsRFPService::StartShutdown()
prefs->RemoveObserver(RFP_JITTER_VALUE_PREF, this);
}
}
sSecretMidpointSeed = nullptr;
sCache = nullptr;
delete sLock;
sLock = nullptr;
}
/* static */

View File

@ -273,10 +273,6 @@ private:
static nsDataHashtable<KeyboardHashKey, const SpoofingKeyboardCode*>* sSpoofingKeyboardCodes;
static mozilla::Mutex* sLock;
static UniquePtr<LRUCache> sCache;
static UniquePtr<uint8_t[]> sSecretMidpointSeed;
nsCString mInitialTZValue;
};