2014-05-05 21:30:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +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/. */
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2006-04-07 01:48:47 +04:00
|
|
|
#if defined(XP_WIN)
|
2005-11-22 00:01:45 +03:00
|
|
|
#include <windows.h>
|
2006-02-19 10:43:19 +03:00
|
|
|
#include <objbase.h>
|
2006-04-07 01:48:47 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2005-11-22 00:01:45 +03:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2006-04-07 01:48:47 +04:00
|
|
|
#include "prrng.h"
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
|
|
|
|
2011-04-01 03:57:38 +04:00
|
|
|
#include "nsUUIDGenerator.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-03-31 23:51:19 +04:00
|
|
|
|
2014-06-03 02:42:00 +04:00
|
|
|
#ifdef ANDROID
|
2014-08-25 23:17:15 +04:00
|
|
|
extern "C" NS_EXPORT void arc4random_buf(void*, size_t);
|
2014-06-03 02:42:00 +04:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsUUIDGenerator, nsIUUIDGenerator)
|
2005-11-22 00:01:45 +03:00
|
|
|
|
|
|
|
nsUUIDGenerator::nsUUIDGenerator()
|
2014-05-05 21:30:39 +04:00
|
|
|
: mLock("nsUUIDGenerator.mLock")
|
2005-11-22 00:01:45 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-15 02:41:47 +04:00
|
|
|
nsUUIDGenerator::~nsUUIDGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-22 00:01:45 +03:00
|
|
|
nsresult
|
|
|
|
nsUUIDGenerator::Init()
|
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// We're a service, so we're guaranteed that Init() is not going
|
|
|
|
// to be reentered while we're inside Init().
|
|
|
|
|
2014-06-03 02:41:00 +04:00
|
|
|
#if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(HAVE_ARC4RANDOM)
|
2014-05-05 21:30:39 +04:00
|
|
|
/* initialize random number generator using NSPR random noise */
|
|
|
|
unsigned int seed;
|
|
|
|
|
|
|
|
size_t bytes = 0;
|
|
|
|
while (bytes < sizeof(seed)) {
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t nbytes = PR_GetRandomNoise(((unsigned char*)&seed) + bytes,
|
|
|
|
sizeof(seed) - bytes);
|
2014-05-05 21:30:39 +04:00
|
|
|
if (nbytes == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-11-22 00:01:45 +03:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
bytes += nbytes;
|
|
|
|
}
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
/* Initialize a new RNG state, and immediately switch
|
|
|
|
* back to the previous one -- we want to use mState
|
|
|
|
* only for our own calls to random().
|
|
|
|
*/
|
|
|
|
mSavedState = initstate(seed, mState, sizeof(mState));
|
|
|
|
setstate(mSavedState);
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
mRBytes = 4;
|
2005-11-22 00:01:45 +03:00
|
|
|
#ifdef RAND_MAX
|
2014-05-13 21:41:38 +04:00
|
|
|
if ((unsigned long)RAND_MAX < 0xffffffffUL) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mRBytes = 3;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
if ((unsigned long)RAND_MAX < 0x00ffffffUL) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mRBytes = 2;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
if ((unsigned long)RAND_MAX < 0x0000ffffUL) {
|
2014-05-05 21:30:39 +04:00
|
|
|
mRBytes = 1;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
|
|
|
if ((unsigned long)RAND_MAX < 0x000000ffUL) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
|
|
|
|
2014-06-03 02:41:00 +04:00
|
|
|
#endif /* non XP_WIN and non XP_MACOSX and non ARC4RANDOM */
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2005-11-22 00:01:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsUUIDGenerator::GenerateUUID(nsID** aRet)
|
2005-11-22 00:01:45 +03:00
|
|
|
{
|
2015-04-01 08:29:55 +03:00
|
|
|
nsID* id = static_cast<nsID*>(moz_xmalloc(sizeof(nsID)));
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!id) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
nsresult rv = GenerateUUIDInPlace(id);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-04-01 08:29:55 +03:00
|
|
|
free(id);
|
2005-11-22 00:01:45 +03:00
|
|
|
return rv;
|
2014-05-05 21:30:39 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
*aRet = id;
|
2014-05-05 21:30:39 +04:00
|
|
|
return rv;
|
2005-11-22 00:01:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsUUIDGenerator::GenerateUUIDInPlace(nsID* aId)
|
2005-11-22 00:01:45 +03:00
|
|
|
{
|
2014-05-05 21:30:39 +04:00
|
|
|
// The various code in this method is probably not threadsafe, so lock
|
|
|
|
// across the whole method.
|
|
|
|
MutexAutoLock lock(mLock);
|
2009-02-11 02:10:07 +03:00
|
|
|
|
2011-05-01 22:59:24 +04:00
|
|
|
#if defined(XP_WIN)
|
2014-05-13 21:41:38 +04:00
|
|
|
HRESULT hr = CoCreateGuid((GUID*)aId);
|
|
|
|
if (FAILED(hr)) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2006-04-07 01:48:47 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
2014-05-05 21:30:39 +04:00
|
|
|
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (!uuid) {
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2006-04-07 01:48:47 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
|
2014-05-13 21:41:38 +04:00
|
|
|
memcpy(aId, &bytes, sizeof(nsID));
|
2006-04-07 01:48:47 +04:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
CFRelease(uuid);
|
2006-04-07 01:48:47 +04:00
|
|
|
#else /* not windows or OS X; generate randomness using random(). */
|
2014-05-05 21:30:39 +04:00
|
|
|
/* XXX we should be saving the return of setstate here and switching
|
|
|
|
* back to it; instead, we use the value returned when we called
|
|
|
|
* initstate, since older glibc's have broken setstate() return values
|
|
|
|
*/
|
2014-06-03 02:41:00 +04:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
2014-05-05 21:30:39 +04:00
|
|
|
setstate(mState);
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
2006-08-14 22:53:34 +04:00
|
|
|
|
2014-06-03 02:42:00 +04:00
|
|
|
#ifdef HAVE_ARC4RANDOM_BUF
|
|
|
|
arc4random_buf(aId, sizeof(nsID));
|
|
|
|
#else /* HAVE_ARC4RANDOM_BUF */
|
2014-05-05 21:30:39 +04:00
|
|
|
size_t bytesLeft = sizeof(nsID);
|
|
|
|
while (bytesLeft > 0) {
|
2014-06-03 02:41:00 +04:00
|
|
|
#ifdef HAVE_ARC4RANDOM
|
2014-05-05 21:30:39 +04:00
|
|
|
long rval = arc4random();
|
|
|
|
const size_t mRBytes = 4;
|
2010-06-01 23:02:42 +04:00
|
|
|
#else
|
2014-05-05 21:30:39 +04:00
|
|
|
long rval = random();
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
|
|
|
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
uint8_t* src = (uint8_t*)&rval;
|
2014-05-05 21:30:39 +04:00
|
|
|
// We want to grab the mRBytes least significant bytes of rval, since
|
|
|
|
// mRBytes less than sizeof(rval) means the high bytes are 0.
|
2006-06-15 08:40:21 +04:00
|
|
|
#ifdef IS_BIG_ENDIAN
|
2014-05-05 21:30:39 +04:00
|
|
|
src += sizeof(rval) - mRBytes;
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
2014-05-13 21:41:38 +04:00
|
|
|
uint8_t* dst = ((uint8_t*)aId) + (sizeof(nsID) - bytesLeft);
|
2014-05-05 21:30:39 +04:00
|
|
|
size_t toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes);
|
2014-05-13 21:41:38 +04:00
|
|
|
for (size_t i = 0; i < toWrite; i++) {
|
2014-05-05 21:30:39 +04:00
|
|
|
dst[i] = src[i];
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
bytesLeft -= toWrite;
|
|
|
|
}
|
2014-06-03 02:42:00 +04:00
|
|
|
#endif /* HAVE_ARC4RANDOM_BUF */
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
/* Put in the version */
|
2014-05-13 21:41:38 +04:00
|
|
|
aId->m2 &= 0x0fff;
|
|
|
|
aId->m2 |= 0x4000;
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
/* Put in the variant */
|
2014-05-13 21:41:38 +04:00
|
|
|
aId->m3[0] &= 0x3f;
|
|
|
|
aId->m3[0] |= 0x80;
|
2006-08-14 22:53:34 +04:00
|
|
|
|
2014-06-03 02:41:00 +04:00
|
|
|
#ifndef HAVE_ARC4RANDOM
|
2014-05-05 21:30:39 +04:00
|
|
|
/* Restore the previous RNG state */
|
|
|
|
setstate(mSavedState);
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 21:30:39 +04:00
|
|
|
return NS_OK;
|
2005-11-22 00:01:45 +03:00
|
|
|
}
|